7 /* Don't copy this bad example; inline RGB data is always a better
8 * idea than inline XPMs.
10 static char *book_closed_xpm[] = {
36 static GtkWidget *window = NULL;
39 /* Creates a tree model containing the completions */
41 create_simple_completion_model (void)
46 store = gtk_list_store_new (1, G_TYPE_STRING);
48 gtk_list_store_append (store, &iter);
49 gtk_list_store_set (store, &iter, 0, "GNOME", -1);
50 gtk_list_store_append (store, &iter);
51 gtk_list_store_set (store, &iter, 0, "gnominious", -1);
52 gtk_list_store_append (store, &iter);
53 gtk_list_store_set (store, &iter, 0, "Gnomonic projection", -1);
55 gtk_list_store_append (store, &iter);
56 gtk_list_store_set (store, &iter, 0, "total", -1);
57 gtk_list_store_append (store, &iter);
58 gtk_list_store_set (store, &iter, 0, "totally", -1);
59 gtk_list_store_append (store, &iter);
60 gtk_list_store_set (store, &iter, 0, "toto", -1);
61 gtk_list_store_append (store, &iter);
62 gtk_list_store_set (store, &iter, 0, "tottery", -1);
63 gtk_list_store_append (store, &iter);
64 gtk_list_store_set (store, &iter, 0, "totterer", -1);
65 gtk_list_store_append (store, &iter);
66 gtk_list_store_set (store, &iter, 0, "Totten trust", -1);
67 gtk_list_store_append (store, &iter);
68 gtk_list_store_set (store, &iter, 0, "totipotent", -1);
69 gtk_list_store_append (store, &iter);
70 gtk_list_store_set (store, &iter, 0, "totipotency", -1);
71 gtk_list_store_append (store, &iter);
72 gtk_list_store_set (store, &iter, 0, "totemism", -1);
73 gtk_list_store_append (store, &iter);
74 gtk_list_store_set (store, &iter, 0, "totem pole", -1);
75 gtk_list_store_append (store, &iter);
76 gtk_list_store_set (store, &iter, 0, "Totara", -1);
77 gtk_list_store_append (store, &iter);
78 gtk_list_store_set (store, &iter, 0, "totalizer", -1);
79 gtk_list_store_append (store, &iter);
80 gtk_list_store_set (store, &iter, 0, "totalizator", -1);
81 gtk_list_store_append (store, &iter);
82 gtk_list_store_set (store, &iter, 0, "totalitarianism", -1);
83 gtk_list_store_append (store, &iter);
84 gtk_list_store_set (store, &iter, 0, "total parenteral nutrition", -1);
85 gtk_list_store_append (store, &iter);
86 gtk_list_store_set (store, &iter, 0, "total hysterectomy", -1);
87 gtk_list_store_append (store, &iter);
88 gtk_list_store_set (store, &iter, 0, "total eclipse", -1);
89 gtk_list_store_append (store, &iter);
90 gtk_list_store_set (store, &iter, 0, "Totipresence", -1);
91 gtk_list_store_append (store, &iter);
92 gtk_list_store_set (store, &iter, 0, "Totipalmi", -1);
93 gtk_list_store_append (store, &iter);
94 gtk_list_store_set (store, &iter, 0, "zombie", -1);
96 return GTK_TREE_MODEL (store);
99 /* Creates a tree model containing the completions */
101 create_completion_model (void)
107 pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **)book_closed_xpm);
109 store = gtk_list_store_new (2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
111 gtk_list_store_append (store, &iter);
112 gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambient", -1);
113 gtk_list_store_append (store, &iter);
114 gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambidextrously", -1);
115 gtk_list_store_append (store, &iter);
116 gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambidexter", -1);
117 gtk_list_store_append (store, &iter);
118 gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambiguity", -1);
119 gtk_list_store_append (store, &iter);
120 gtk_list_store_set (store, &iter, 0, pixbuf, 1, "American Party", -1);
121 gtk_list_store_append (store, &iter);
122 gtk_list_store_set (store, &iter, 0, pixbuf, 1, "American mountain ash", -1);
123 gtk_list_store_append (store, &iter);
124 gtk_list_store_set (store, &iter, 0, pixbuf, 1, "amelioration", -1);
125 gtk_list_store_append (store, &iter);
126 gtk_list_store_set (store, &iter, 0, pixbuf, 1, "Amelia Earhart", -1);
127 gtk_list_store_append (store, &iter);
128 gtk_list_store_set (store, &iter, 0, pixbuf, 1, "Totten trust", -1);
129 gtk_list_store_append (store, &iter);
130 gtk_list_store_set (store, &iter, 0, pixbuf, 1, "Laminated arch", -1);
132 return GTK_TREE_MODEL (store);
136 match_func (GtkEntryCompletion *completion,
144 gboolean ret = FALSE;
146 model = gtk_entry_completion_get_model (completion);
148 gtk_tree_model_get (model, iter, 1, &item, -1);
152 g_print ("compare %s %s\n", key, item);
153 if (strncmp (key, item, strlen (key)) == 0)
163 activated_cb (GtkEntryCompletion *completion,
167 g_print ("action activated: %d\n", index);
170 static gint timer_count = 0;
172 static gchar *dynamic_completions[] = {
175 "Gnomonic projection",
190 "total parenteral nutrition",
191 "total hysterectomy",
199 animation_timer (GtkEntryCompletion *completion)
202 gint n_completions = G_N_ELEMENTS (dynamic_completions);
205 GtkListStore *store = GTK_LIST_STORE (gtk_entry_completion_get_model (completion));
207 if ((timer_count / n_completions) % 2 == 0)
209 n = timer_count % n_completions;
210 gtk_list_store_append (store, &iter);
211 gtk_list_store_set (store, &iter, 0, dynamic_completions[n], -1);
216 gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter);
217 gtk_list_store_remove (store, &iter);
225 match_selected_cb (GtkEntryCompletion *completion,
232 entry = gtk_entry_completion_get_entry (completion);
233 gtk_tree_model_get (GTK_TREE_MODEL (model), iter, 1, &str, -1);
234 gtk_entry_set_text (GTK_ENTRY (entry), str);
235 gtk_editable_set_position (GTK_EDITABLE (entry), -1);
242 main (int argc, char *argv[])
247 GtkEntryCompletion *completion;
248 GtkTreeModel *completion_model;
249 GtkCellRenderer *cell;
251 gtk_init (&argc, &argv);
253 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
254 gtk_container_set_border_width (GTK_CONTAINER (window), 5);
255 g_signal_connect (window, "delete_event", gtk_main_quit, NULL);
257 vbox = gtk_vbox_new (FALSE, 2);
258 gtk_container_add (GTK_CONTAINER (window), vbox);
260 gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
262 label = gtk_label_new (NULL);
264 gtk_label_set_markup (GTK_LABEL (label), "Completion demo, try writing <b>total</b> or <b>gnome</b> for example.");
265 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
267 /* Create our first entry */
268 entry = gtk_entry_new ();
269 gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
271 /* Create the completion object */
272 completion = gtk_entry_completion_new ();
274 /* Assign the completion to the entry */
275 gtk_entry_set_completion (GTK_ENTRY (entry), completion);
276 g_object_unref (completion);
278 /* Create a tree model and use it as the completion model */
279 completion_model = create_simple_completion_model ();
280 gtk_entry_completion_set_model (completion, completion_model);
281 g_object_unref (completion_model);
283 /* Use model column 0 as the text column */
284 gtk_entry_completion_set_text_column (completion, 0);
286 /* Create our second entry */
287 entry = gtk_entry_new ();
288 gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
290 /* Create the completion object */
291 completion = gtk_entry_completion_new ();
293 /* Assign the completion to the entry */
294 gtk_entry_set_completion (GTK_ENTRY (entry), completion);
295 g_object_unref (completion);
297 /* Create a tree model and use it as the completion model */
298 completion_model = create_completion_model ();
299 gtk_entry_completion_set_model (completion, completion_model);
300 gtk_entry_completion_set_minimum_key_length (completion, 2);
301 g_object_unref (completion_model);
303 /* Use model column 1 as the text column */
304 cell = gtk_cell_renderer_pixbuf_new ();
305 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), cell, FALSE);
306 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (completion), cell,
309 cell = gtk_cell_renderer_text_new ();
310 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), cell, FALSE);
311 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (completion), cell,
314 gtk_entry_completion_set_match_func (completion, match_func, NULL, NULL);
315 g_signal_connect (G_OBJECT (completion), "match-selected",
316 G_CALLBACK (match_selected_cb), NULL);
318 gtk_entry_completion_insert_action_text (completion, 100, "action!");
319 gtk_entry_completion_insert_action_text (completion, 101, "'nother action!");
320 g_signal_connect (completion, "action_activated", G_CALLBACK (activated_cb), 0);
322 /* Create our third entry */
323 entry = gtk_entry_new ();
324 gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
326 /* Create the completion object */
327 completion = gtk_entry_completion_new ();
329 /* Assign the completion to the entry */
330 gtk_entry_set_completion (GTK_ENTRY (entry), completion);
331 g_object_unref (completion);
333 /* Create a tree model and use it as the completion model */
334 completion_model = GTK_TREE_MODEL (gtk_list_store_new (1, G_TYPE_STRING));
336 gtk_entry_completion_set_model (completion, completion_model);
337 g_object_unref (completion_model);
339 /* Use model column 0 as the text column */
340 gtk_entry_completion_set_text_column (completion, 0);
342 /* Fill the completion dynamically */
343 g_timeout_add (1000, (GSourceFunc) animation_timer, completion);
345 gtk_widget_show_all (window);