]> Pileus Git - ~andy/gtk/blob - tests/testentrycompletion.c
Fixes #136082 and #135265, patch by Morten Welinder.
[~andy/gtk] / tests / testentrycompletion.c
1
2 #include <config.h>
3 #include <gtk/gtk.h>
4
5 /* Don't copy this bad example; inline RGB data is always a better
6  * idea than inline XPMs.
7  */
8 static char  *book_closed_xpm[] = {
9 "16 16 6 1",
10 "       c None s None",
11 ".      c black",
12 "X      c red",
13 "o      c yellow",
14 "O      c #808080",
15 "#      c white",
16 "                ",
17 "       ..       ",
18 "     ..XX.      ",
19 "   ..XXXXX.     ",
20 " ..XXXXXXXX.    ",
21 ".ooXXXXXXXXX.   ",
22 "..ooXXXXXXXXX.  ",
23 ".X.ooXXXXXXXXX. ",
24 ".XX.ooXXXXXX..  ",
25 " .XX.ooXXX..#O  ",
26 "  .XX.oo..##OO. ",
27 "   .XX..##OO..  ",
28 "    .X.#OO..    ",
29 "     ..O..      ",
30 "      ..        ",
31 "                "
32 };
33
34 static GtkWidget *window = NULL;
35
36
37 /* Creates a tree model containing the completions */
38 GtkTreeModel *
39 create_simple_completion_model (void)
40 {
41   GtkListStore *store;
42   GtkTreeIter iter;
43   
44   store = gtk_list_store_new (1, G_TYPE_STRING);
45
46   gtk_list_store_append (store, &iter);
47   gtk_list_store_set (store, &iter, 0, "GNOME", -1);
48   gtk_list_store_append (store, &iter);
49   gtk_list_store_set (store, &iter, 0, "gnominious", -1);
50   gtk_list_store_append (store, &iter);
51   gtk_list_store_set (store, &iter, 0, "Gnomonic projection", -1);
52
53   gtk_list_store_append (store, &iter);
54   gtk_list_store_set (store, &iter, 0, "total", -1);
55   gtk_list_store_append (store, &iter);
56   gtk_list_store_set (store, &iter, 0, "totally", -1);
57   gtk_list_store_append (store, &iter);
58   gtk_list_store_set (store, &iter, 0, "toto", -1);
59   gtk_list_store_append (store, &iter);
60   gtk_list_store_set (store, &iter, 0, "tottery", -1);
61   gtk_list_store_append (store, &iter);
62   gtk_list_store_set (store, &iter, 0, "totterer", -1);
63   gtk_list_store_append (store, &iter);
64   gtk_list_store_set (store, &iter, 0, "Totten trust", -1);
65   gtk_list_store_append (store, &iter);
66   gtk_list_store_set (store, &iter, 0, "totipotent", -1);
67   gtk_list_store_append (store, &iter);
68   gtk_list_store_set (store, &iter, 0, "totipotency", -1);
69   gtk_list_store_append (store, &iter);
70   gtk_list_store_set (store, &iter, 0, "totemism", -1);
71   gtk_list_store_append (store, &iter);
72   gtk_list_store_set (store, &iter, 0, "totem pole", -1);
73   gtk_list_store_append (store, &iter);
74   gtk_list_store_set (store, &iter, 0, "Totara", -1);
75   gtk_list_store_append (store, &iter);
76   gtk_list_store_set (store, &iter, 0, "totalizer", -1);
77   gtk_list_store_append (store, &iter);
78   gtk_list_store_set (store, &iter, 0, "totalizator", -1);
79   gtk_list_store_append (store, &iter);
80   gtk_list_store_set (store, &iter, 0, "totalitarianism", -1);
81   gtk_list_store_append (store, &iter);
82   gtk_list_store_set (store, &iter, 0, "total parenteral nutrition", -1);
83   gtk_list_store_append (store, &iter);
84   gtk_list_store_set (store, &iter, 0, "total hysterectomy", -1);
85   gtk_list_store_append (store, &iter);
86   gtk_list_store_set (store, &iter, 0, "total eclipse", -1);
87   gtk_list_store_append (store, &iter);
88   gtk_list_store_set (store, &iter, 0, "Totipresence", -1);
89   gtk_list_store_append (store, &iter);
90   gtk_list_store_set (store, &iter, 0, "Totipalmi", -1);
91   gtk_list_store_append (store, &iter);
92   gtk_list_store_set (store, &iter, 0, "zombie", -1);
93  
94   return GTK_TREE_MODEL (store);
95 }
96
97 /* Creates a tree model containing the completions */
98 GtkTreeModel *
99 create_completion_model (void)
100 {
101   GtkListStore *store;
102   GtkTreeIter iter;
103   GdkPixbuf *pixbuf;
104
105   pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **)book_closed_xpm);
106
107   store = gtk_list_store_new (2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
108
109   gtk_list_store_append (store, &iter);
110   gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambient", -1);
111   gtk_list_store_append (store, &iter);
112   gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambidextrously", -1);
113   gtk_list_store_append (store, &iter);
114   gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambidexter", -1);
115   gtk_list_store_append (store, &iter);
116   gtk_list_store_set (store, &iter, 0, pixbuf, 1, "ambiguity", -1);
117   gtk_list_store_append (store, &iter);
118   gtk_list_store_set (store, &iter, 0, pixbuf, 1, "American Party", -1);
119   gtk_list_store_append (store, &iter);
120   gtk_list_store_set (store, &iter, 0, pixbuf, 1, "American mountain ash", -1);
121   gtk_list_store_append (store, &iter);
122   gtk_list_store_set (store, &iter, 0, pixbuf, 1, "amelioration", -1);
123   gtk_list_store_append (store, &iter);
124   gtk_list_store_set (store, &iter, 0, pixbuf, 1, "Amelia Earhart", -1);
125   gtk_list_store_append (store, &iter);
126   gtk_list_store_set (store, &iter, 0, pixbuf, 1, "Totten trust", -1);
127   gtk_list_store_append (store, &iter);
128   gtk_list_store_set (store, &iter, 0, pixbuf, 1, "Laminated arch", -1);
129  
130   return GTK_TREE_MODEL (store);
131 }
132
133 #if 0
134 static gboolean
135 match_func (GtkEntryCompletion *completion,
136             const gchar        *key,
137             GtkTreeIter        *iter,
138             gpointer            user_data)
139 {
140   return FALSE;
141 }
142 #endif
143
144 static void
145 activated_cb (GtkEntryCompletion *completion, 
146               gint                index,
147               gpointer            user_data)
148 {
149   g_print ("action activated: %d\n", index);
150 }
151
152 int 
153 main (int argc, char *argv[])
154 {
155   GtkWidget *vbox;
156   GtkWidget *label;
157   GtkWidget *entry;
158   GtkEntryCompletion *completion;
159   GtkTreeModel *completion_model;
160   GtkCellRenderer *cell;
161
162   gtk_init (&argc, &argv);
163
164   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
165   gtk_container_set_border_width (GTK_CONTAINER (window), 5);
166   g_signal_connect (window, "delete_event", gtk_main_quit, NULL);
167   
168   vbox = gtk_vbox_new (FALSE, 2);
169   gtk_container_add (GTK_CONTAINER (window), vbox);
170     
171   gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
172   
173   label = gtk_label_new (NULL);
174   gtk_label_set_markup (GTK_LABEL (label), "Completion demo, try writing <b>total</b> or <b>gnome</b> for example.");
175   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
176
177   /* Create our first entry */
178   entry = gtk_entry_new ();
179   gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
180   
181   /* Create the completion object */
182   completion = gtk_entry_completion_new ();
183   
184   /* Assign the completion to the entry */
185   gtk_entry_set_completion (GTK_ENTRY (entry), completion);
186   g_object_unref (completion);
187   
188   /* Create a tree model and use it as the completion model */
189   completion_model = create_simple_completion_model ();
190   gtk_entry_completion_set_model (completion, completion_model);
191   g_object_unref (completion_model);
192   
193   /* Use model column 0 as the text column */
194   gtk_entry_completion_set_text_column (completion, 0);
195
196   /* Create our second entry */
197   entry = gtk_entry_new ();
198   gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
199
200   /* Create the completion object */
201   completion = gtk_entry_completion_new ();
202   
203   /* Assign the completion to the entry */
204   gtk_entry_set_completion (GTK_ENTRY (entry), completion);
205   g_object_unref (completion);
206   
207   /* Create a tree model and use it as the completion model */
208   completion_model = create_completion_model ();
209   gtk_entry_completion_set_model (completion, completion_model);
210   gtk_entry_completion_set_minimum_key_length (completion, 2);
211   g_object_unref (completion_model);
212   
213   /* Use model column 1 as the text column */
214   cell = gtk_cell_renderer_pixbuf_new ();
215   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), cell, FALSE);
216   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (completion), cell, 
217                                   "pixbuf", 0, NULL); 
218   /* Use model column 1 as the text column */
219   gtk_entry_completion_set_text_column (completion, 1);
220
221 #if 0
222   cell = gtk_cell_renderer_text_new ();
223   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (completion), cell, FALSE);
224   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (completion), cell, 
225                                   "text", 1, NULL); 
226   
227   gtk_entry_completion_set_match_func (completion, match_func, NULL, NULL);
228 #endif
229
230   gtk_entry_completion_insert_action_text (completion, 100, "action!");
231   gtk_entry_completion_insert_action_text (completion, 101, "'nother action!");
232   g_signal_connect (completion, "action_activated", G_CALLBACK (activated_cb), 0);
233
234   gtk_widget_show_all (window);
235
236   gtk_main ();
237   
238   return 0;
239 }
240
241