]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/entry_completion.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / demos / gtk-demo / entry_completion.c
1 /* Entry Completion
2  *
3  * GtkEntryCompletion provides a mechanism for adding support for
4  * completion in GtkEntry.
5  *
6  */
7
8 #include <config.h>
9 #include <gtk/gtk.h>
10
11 static GtkWidget *window = NULL;
12
13 /* Creates a tree model containing the completions */
14 GtkTreeModel *
15 create_completion_model (void)
16 {
17   GtkListStore *store;
18   GtkTreeIter iter;
19   
20   store = gtk_list_store_new (1, G_TYPE_STRING);
21
22   /* Append one word */
23   gtk_list_store_append (store, &iter);
24   gtk_list_store_set (store, &iter, 0, "GNOME", -1);
25
26   /* Append another word */
27   gtk_list_store_append (store, &iter);
28   gtk_list_store_set (store, &iter, 0, "total", -1);
29
30   /* And another word */
31   gtk_list_store_append (store, &iter);
32   gtk_list_store_set (store, &iter, 0, "totally", -1);
33   
34   return GTK_TREE_MODEL (store);
35 }
36
37
38 GtkWidget *
39 do_entry_completion (GtkWidget *do_widget)
40 {
41   GtkWidget *vbox;
42   GtkWidget *label;
43   GtkWidget *entry;
44   GtkEntryCompletion *completion;
45   GtkTreeModel *completion_model;
46   
47   if (!window)
48   {
49     window = gtk_dialog_new_with_buttons ("GtkEntryCompletion",
50                                           GTK_WINDOW (do_widget),
51                                           0,
52                                           GTK_STOCK_CLOSE,
53                                           GTK_RESPONSE_NONE,
54                                           NULL);
55     gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
56
57     g_signal_connect (window, "response",
58                       G_CALLBACK (gtk_widget_destroy), NULL);
59     g_signal_connect (window, "destroy",
60                       G_CALLBACK (gtk_widget_destroyed), &window);
61
62     vbox = gtk_vbox_new (FALSE, 5);
63     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, TRUE, TRUE, 0);
64     gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
65
66     label = gtk_label_new (NULL);
67     gtk_label_set_markup (GTK_LABEL (label), "Completion demo, try writing <b>total</b> or <b>gnome</b> for example.");
68     gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
69
70     /* Create our entry */
71     entry = gtk_entry_new ();
72     gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
73
74     /* Create the completion object */
75     completion = gtk_entry_completion_new ();
76
77     /* Assign the completion to the entry */
78     gtk_entry_set_completion (GTK_ENTRY (entry), completion);
79     g_object_unref (completion);
80     
81     /* Create a tree model and use it as the completion model */
82     completion_model = create_completion_model ();
83     gtk_entry_completion_set_model (completion, completion_model);
84     g_object_unref (completion_model);
85     
86     /* Use model column 0 as the text column */
87     gtk_entry_completion_set_text_column (completion, 0);
88   }
89
90   if (!GTK_WIDGET_VISIBLE (window))
91     gtk_widget_show_all (window);
92   else
93     gtk_widget_destroy (window);
94
95   return window;
96 }
97
98