1/* Entry/Completion
2 *
3 * GtkEntryCompletion provides a mechanism for adding support for
4 * completion in GtkEntry.
5 *
6 */
7
8#include <glib/gi18n.h>
9#include <gtk/gtk.h>
10
11/* Creates a tree model containing the completions */
12static GtkTreeModel *
13create_completion_model (void)
14{
15 const char *strings[] = {
16 "GNOME",
17 "gnominious",
18 "Gnomonic projection",
19 "Gnosophy",
20 "total",
21 "totally",
22 "toto",
23 "tottery",
24 "totterer",
25 "Totten trust",
26 "Tottenham hotspurs",
27 "totipotent",
28 "totipotency",
29 "totemism",
30 "totem pole",
31 "Totara",
32 "totalizer",
33 "totalizator",
34 "totalitarianism",
35 "total parenteral nutrition",
36 "total eclipse",
37 "Totipresence",
38 "Totipalmi",
39 "zombie",
40 "aæx",
41 "aæy",
42 "aæz",
43 NULL
44 };
45 int i;
46 GtkListStore *store;
47 GtkTreeIter iter;
48
49 store = gtk_list_store_new (n_columns: 1, G_TYPE_STRING);
50
51 for (i = 0; strings[i]; i++)
52 {
53 /* Append one word */
54 gtk_list_store_append (list_store: store, iter: &iter);
55 gtk_list_store_set (list_store: store, iter: &iter, 0, strings[i], -1);
56 }
57
58 return GTK_TREE_MODEL (store);
59}
60
61
62GtkWidget *
63do_entry_completion (GtkWidget *do_widget)
64{
65 static GtkWidget *window = NULL;
66 GtkWidget *vbox;
67 GtkWidget *label;
68 GtkWidget *entry;
69 GtkEntryCompletion *completion;
70 GtkTreeModel *completion_model;
71
72 if (!window)
73 {
74 window = gtk_window_new ();
75 gtk_window_set_display (GTK_WINDOW (window),
76 display: gtk_widget_get_display (widget: do_widget));
77 gtk_window_set_title (GTK_WINDOW (window), title: "Completion");
78 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
79 g_object_add_weak_pointer (G_OBJECT (window), weak_pointer_location: (gpointer *)&window);
80
81 vbox = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 12);
82 gtk_widget_set_margin_start (widget: vbox, margin: 18);
83 gtk_widget_set_margin_end (widget: vbox, margin: 18);
84 gtk_widget_set_margin_top (widget: vbox, margin: 18);
85 gtk_widget_set_margin_bottom (widget: vbox, margin: 18);
86 gtk_window_set_child (GTK_WINDOW (window), child: vbox);
87
88 label = gtk_label_new (NULL);
89 gtk_label_set_markup (GTK_LABEL (label), str: "Try writing <b>total</b> or <b>gnome</b> for example.");
90 gtk_box_append (GTK_BOX (vbox), child: label);
91
92 /* Create our entry */
93 entry = gtk_entry_new ();
94 gtk_box_append (GTK_BOX (vbox), child: entry);
95
96 /* Create the completion object */
97 completion = gtk_entry_completion_new ();
98
99 /* Assign the completion to the entry */
100 gtk_entry_set_completion (GTK_ENTRY (entry), completion);
101 g_object_unref (object: completion);
102
103 /* Create a tree model and use it as the completion model */
104 completion_model = create_completion_model ();
105 gtk_entry_completion_set_model (completion, model: completion_model);
106 g_object_unref (object: completion_model);
107
108 /* Use model column 0 as the text column */
109 gtk_entry_completion_set_text_column (completion, column: 0);
110
111 gtk_entry_completion_set_inline_completion (completion, TRUE);
112 gtk_entry_completion_set_inline_selection (completion, TRUE);
113 }
114
115 if (!gtk_widget_get_visible (widget: window))
116 gtk_widget_show (widget: window);
117 else
118 gtk_window_destroy (GTK_WINDOW (window));
119
120 return window;
121}
122

source code of gtk/demos/gtk-demo/entry_completion.c