]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/combobox.c
Fix some leaks. (#348108, Chris Wilson)
[~andy/gtk] / demos / gtk-demo / combobox.c
1 /* Combo boxes 
2  *
3  * The ComboBox widget allows to select one option out of a list.
4  * The ComboBoxEntry additionally allows the user to enter a value
5  * that is not in the list of options. 
6  *
7  * How the options are displayed is controlled by cell renderers.
8  */
9
10 #include <gtk/gtk.h>
11
12 enum 
13 {
14   PIXBUF_COL,
15   TEXT_COL
16 };
17
18 static gchar *
19 strip_underscore (const gchar *text)
20 {
21   gchar *p, *q;
22   gchar *result;
23   
24   result = g_strdup (text);
25   p = q = result;
26   while (*p) 
27     {
28       if (*p != '_')
29         {
30           *q = *p;
31           q++;
32         }
33       p++;
34     }
35   *q = '\0';
36
37   return result;
38 }
39
40 static GtkTreeModel *
41 create_stock_icon_store (void)
42 {
43   gchar *stock_id[6] = {
44     GTK_STOCK_DIALOG_WARNING,
45     GTK_STOCK_STOP,
46     GTK_STOCK_NEW,
47     GTK_STOCK_CLEAR,
48     NULL,
49     GTK_STOCK_OPEN    
50   };
51
52   GtkStockItem item;
53   GdkPixbuf *pixbuf;
54   GtkWidget *cellview;
55   GtkTreeIter iter;
56   GtkListStore *store;
57   gchar *label;
58   gint i;
59
60   cellview = gtk_cell_view_new ();
61   
62   store = gtk_list_store_new (2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
63
64   for (i = 0; i < G_N_ELEMENTS (stock_id); i++)
65     {
66       if (stock_id[i])
67         {
68           pixbuf = gtk_widget_render_icon (cellview, stock_id[i],
69                                            GTK_ICON_SIZE_BUTTON, NULL);
70           gtk_stock_lookup (stock_id[i], &item);
71           label = strip_underscore (item.label);
72           gtk_list_store_append (store, &iter);
73           gtk_list_store_set (store, &iter,
74                               PIXBUF_COL, pixbuf,
75                               TEXT_COL, label,
76                               -1);
77           g_object_unref (pixbuf);
78           g_free (label);
79         }
80       else
81         {
82           gtk_list_store_append (store, &iter);
83           gtk_list_store_set (store, &iter,
84                               PIXBUF_COL, NULL,
85                               TEXT_COL, "separator",
86                               -1);
87         }
88     }
89
90   gtk_widget_destroy (cellview);
91   
92   return GTK_TREE_MODEL (store);
93 }
94
95 /* A GtkCellLayoutDataFunc that demonstrates how one can control
96  * sensitivity of rows. This particular function does nothing 
97  * useful and just makes the second row insensitive.
98  */
99 static void
100 set_sensitive (GtkCellLayout   *cell_layout,
101                GtkCellRenderer *cell,
102                GtkTreeModel    *tree_model,
103                GtkTreeIter     *iter,
104                gpointer         data)
105 {
106   GtkTreePath *path;
107   gint *indices;
108   gboolean sensitive;
109
110   path = gtk_tree_model_get_path (tree_model, iter);
111   indices = gtk_tree_path_get_indices (path);
112   sensitive = indices[0] != 1;
113   gtk_tree_path_free (path);
114
115   g_object_set (cell, "sensitive", sensitive, NULL);
116 }
117
118 /* A GtkTreeViewRowSeparatorFunc that demonstrates how rows can be
119  * rendered as separators. This particular function does nothing 
120  * useful and just turns the fourth row into a separator.
121  */
122 static gboolean
123 is_separator (GtkTreeModel *model,
124               GtkTreeIter  *iter,
125               gpointer      data)
126 {
127   GtkTreePath *path;
128   gboolean result;
129
130   path = gtk_tree_model_get_path (model, iter);
131   result = gtk_tree_path_get_indices (path)[0] == 4;
132   gtk_tree_path_free (path);
133
134   return result;
135 }
136
137 static GtkTreeModel *
138 create_capital_store (void)
139 {
140   struct {
141     gchar *group;
142     gchar *capital;
143   } capitals[] = {
144     { "A - B", NULL }, 
145     { NULL, "Albany" },
146     { NULL, "Annapolis" },
147     { NULL, "Atlanta" },
148     { NULL, "Augusta" }, 
149     { NULL, "Austin" },
150     { NULL, "Baton Rouge" },
151     { NULL, "Bismarck" },
152     { NULL, "Boise" },
153     { NULL, "Boston" },
154     { "C - D", NULL },
155     { NULL, "Carson City" },
156     { NULL, "Charleston" },
157     { NULL, "Cheyenne" },
158     { NULL, "Columbia" },
159     { NULL, "Columbus" },
160     { NULL, "Concord" },
161     { NULL, "Denver" },
162     { NULL, "Des Moines" },
163     { NULL, "Dover" },
164     { "E - J", NULL },
165     { NULL, "Frankfort" },
166     { NULL, "Harrisburg" },
167     { NULL, "Hartford" },
168     { NULL, "Helena" },
169     { NULL, "Honolulu" },
170     { NULL, "Indianapolis" },
171     { NULL, "Jackson" },
172     { NULL, "Jefferson City" },
173     { NULL, "Juneau" },
174     { "K - O" },
175     { NULL, "Lansing" },
176     { NULL, "Lincoln" },
177     { NULL, "Little Rock" },
178     { NULL, "Madison" },
179     { NULL, "Montgomery" },
180     { NULL, "Montpelier" },
181     { NULL, "Nashville" },
182     { NULL, "Oklahoma City" },
183     { NULL, "Olympia" },
184     { NULL, "P - S" },
185     { NULL, "Phoenix" },
186     { NULL, "Pierre" },
187     { NULL, "Providence" },
188     { NULL, "Raleigh" },
189     { NULL, "Richmond" },
190     { NULL, "Sacramento" },
191     { NULL, "Salem" },
192     { NULL, "Salt Lake City" },
193     { NULL, "Santa Fe" },
194     { NULL, "Springfield" },
195     { NULL, "St. Paul" },
196     { "T - Z", NULL },
197     { NULL, "Tallahassee" },
198     { NULL, "Topeka" },
199     { NULL, "Trenton" },
200     { NULL, NULL }
201   };
202   
203   GtkTreeIter iter, iter2;
204   GtkTreeStore *store;
205   gint i;
206
207   store = gtk_tree_store_new (1, G_TYPE_STRING);
208   
209   for (i = 0; capitals[i].group || capitals[i].capital; i++)
210     {
211       if (capitals[i].group)
212         {
213           gtk_tree_store_append (store, &iter, NULL);
214           gtk_tree_store_set (store, &iter, 0, capitals[i].group, -1);
215         }
216       else if (capitals[i].capital)
217         {
218           gtk_tree_store_append (store, &iter2, &iter);
219           gtk_tree_store_set (store, &iter2, 0, capitals[i].capital, -1);
220         }
221     }
222   
223   return GTK_TREE_MODEL (store);
224 }
225
226 static void
227 is_capital_sensitive (GtkCellLayout   *cell_layout,
228                       GtkCellRenderer *cell,
229                       GtkTreeModel    *tree_model,
230                       GtkTreeIter     *iter,
231                       gpointer         data)
232 {
233   gboolean sensitive;
234
235   sensitive = !gtk_tree_model_iter_has_child (tree_model, iter);
236
237   g_object_set (cell, "sensitive", sensitive, NULL);
238 }
239
240 static void
241 fill_combo_entry (GtkWidget *entry)
242 {
243   gtk_combo_box_append_text (GTK_COMBO_BOX (entry), "One");
244   gtk_combo_box_append_text (GTK_COMBO_BOX (entry), "Two");
245   gtk_combo_box_append_text (GTK_COMBO_BOX (entry), "2\302\275");
246   gtk_combo_box_append_text (GTK_COMBO_BOX (entry), "Three");
247 }
248
249 GtkWidget *
250 do_combobox (GtkWidget *do_widget)
251 {
252   static GtkWidget *window = NULL;
253   GtkWidget *vbox, *frame, *box, *combo;
254   GtkTreeModel *model;
255   GtkCellRenderer *renderer;
256   GtkTreePath *path;
257   GtkTreeIter iter;
258
259   if (!window)
260   {
261     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
262     gtk_window_set_screen (GTK_WINDOW (window),
263                            gtk_widget_get_screen (do_widget));
264     gtk_window_set_title (GTK_WINDOW (window), "Combo boxes");
265    
266     g_signal_connect (window, "destroy",
267                       G_CALLBACK (gtk_widget_destroyed),
268                       &window);
269     
270     gtk_container_set_border_width (GTK_CONTAINER (window), 10);
271
272     vbox = gtk_vbox_new (FALSE, 2);
273     gtk_container_add (GTK_CONTAINER (window), vbox);
274
275     /* A combobox demonstrating cell renderers, separators and
276      *  insensitive rows 
277      */
278     frame = gtk_frame_new ("Some stock icons");
279     gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
280     
281     box = gtk_vbox_new (FALSE, 0);
282     gtk_container_set_border_width (GTK_CONTAINER (box), 5);
283     gtk_container_add (GTK_CONTAINER (frame), box);
284     
285     model = create_stock_icon_store ();
286     combo = gtk_combo_box_new_with_model (model);
287     g_object_unref (model);
288     gtk_container_add (GTK_CONTAINER (box), combo);
289     
290     renderer = gtk_cell_renderer_pixbuf_new ();
291     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, FALSE);
292     gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
293                                     "pixbuf", PIXBUF_COL, 
294                                     NULL);
295
296     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
297                                         renderer,
298                                         set_sensitive,
299                                         NULL, NULL);
300     
301     renderer = gtk_cell_renderer_text_new ();
302     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
303     gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
304                                     "text", TEXT_COL,
305                                     NULL);
306
307     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
308                                         renderer,
309                                         set_sensitive,
310                                         NULL, NULL);
311
312     gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo), 
313                                           is_separator, NULL, NULL);
314     
315     gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
316     
317     /* A combobox demonstrating trees.
318      */
319     frame = gtk_frame_new ("Where are we ?");
320     gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
321
322     box = gtk_vbox_new (FALSE, 0);
323     gtk_container_set_border_width (GTK_CONTAINER (box), 5);
324     gtk_container_add (GTK_CONTAINER (frame), box);
325     
326     model = create_capital_store ();
327     combo = gtk_combo_box_new_with_model (model);
328     g_object_unref (model);
329     gtk_container_add (GTK_CONTAINER (box), combo);
330
331     renderer = gtk_cell_renderer_text_new ();
332     gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
333     gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
334                                     "text", 0,
335                                     NULL);
336     gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
337                                         renderer,
338                                         is_capital_sensitive,
339                                         NULL, NULL);
340
341     path = gtk_tree_path_new_from_indices (0, 8, -1);
342     gtk_tree_model_get_iter (model, &iter, path);
343     gtk_tree_path_free (path);
344     gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo), &iter);
345
346     /* A GtkComboBoxEntry 
347      */
348     frame = gtk_frame_new ("Editable");
349     gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
350     
351     box = gtk_vbox_new (FALSE, 0);
352     gtk_container_set_border_width (GTK_CONTAINER (box), 5);
353     gtk_container_add (GTK_CONTAINER (frame), box);
354     
355     combo = gtk_combo_box_entry_new_text ();
356     fill_combo_entry (combo);
357     gtk_container_add (GTK_CONTAINER (box), combo);
358     
359   }
360
361   if (!GTK_WIDGET_VISIBLE (window))
362     {
363       gtk_widget_show_all (window);
364     }
365   else
366     {    
367       gtk_widget_destroy (window);
368       window = NULL;
369     }
370
371   return window;
372 }