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