]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/entry_completion.c
Use gtk_box_new() instead gtk_[v|h]box_new()
[~andy/gtk] / demos / gtk-demo / entry_completion.c
1 /* Entry/Entry Completion
2  *
3  * GtkEntryCompletion provides a mechanism for adding support for
4  * completion in GtkEntry.
5  *
6  */
7
8 #include <gtk/gtk.h>
9
10 static GtkWidget *window = NULL;
11
12 /* Creates a tree model containing the completions */
13 GtkTreeModel *
14 create_completion_model (void)
15 {
16   GtkListStore *store;
17   GtkTreeIter iter;
18   
19   store = gtk_list_store_new (1, G_TYPE_STRING);
20
21   /* Append one word */
22   gtk_list_store_append (store, &iter);
23   gtk_list_store_set (store, &iter, 0, "GNOME", -1);
24
25   /* Append another word */
26   gtk_list_store_append (store, &iter);
27   gtk_list_store_set (store, &iter, 0, "total", -1);
28
29   /* And another word */
30   gtk_list_store_append (store, &iter);
31   gtk_list_store_set (store, &iter, 0, "totally", -1);
32   
33   return GTK_TREE_MODEL (store);
34 }
35
36
37 GtkWidget *
38 do_entry_completion (GtkWidget *do_widget)
39 {
40   GtkWidget *content_area;
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     content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));
63
64     vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 5);
65     gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
66     gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
67
68     label = gtk_label_new (NULL);
69     gtk_label_set_markup (GTK_LABEL (label), "Completion demo, try writing <b>total</b> or <b>gnome</b> for example.");
70     gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
71
72     /* Create our entry */
73     entry = gtk_entry_new ();
74     gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
75
76     /* Create the completion object */
77     completion = gtk_entry_completion_new ();
78
79     /* Assign the completion to the entry */
80     gtk_entry_set_completion (GTK_ENTRY (entry), completion);
81     g_object_unref (completion);
82     
83     /* Create a tree model and use it as the completion model */
84     completion_model = create_completion_model ();
85     gtk_entry_completion_set_model (completion, completion_model);
86     g_object_unref (completion_model);
87     
88     /* Use model column 0 as the text column */
89     gtk_entry_completion_set_text_column (completion, 0);
90   }
91
92   if (!gtk_widget_get_visible (window))
93     gtk_widget_show_all (window);
94   else
95     gtk_widget_destroy (window);
96
97   return window;
98 }
99
100