Debian Codegraph @

demos/gtk-demo/combobox.c

raw
1 /* Combo Boxes
2 *
3 * The GtkComboBox widget allows to select one option out of a list.
4 * The GtkComboBoxEntry 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 <glib/gi18n.h>
11 #include <gtk/gtk.h>
12
13 enum
14 {
15 ICON_NAME_COL,
16 TEXT_COL
17 };
18
19 static GtkTreeModel *
20 create_icon_store (void)
21 {
22 const gchar *icon_names[6] = {
23 "dialog-warning",
24 "process-stop",
25 "document-new",
26 "edit-clear",
27 NULL,
28 "document-open"
29 };
30 const gchar *labels[6] = {
31 N_("Warning"),
32 N_("Stop"),
33 N_("New"),
34 N_("Clear"),
35 NULL,
36 N_("Open")
37 };
38
39 GtkTreeIter iter;
40 GtkListStore *store;
41 gint i;
42
43 store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
44
45 for (i = 0; i < G_N_ELEMENTS (icon_names); i++)
46 {
47 if (icon_names[i])
48 {
49 gtk_list_store_append (store, &iter);
50 gtk_list_store_set (store, &iter,
51 ICON_NAME_COL, icon_names[i],
52 TEXT_COL, _(labels[i]),
53 -1);
54 }
55 else
56 {
57 gtk_list_store_append (store, &iter);
58 gtk_list_store_set (store, &iter,
59 ICON_NAME_COL, NULL,
60 TEXT_COL, "separator",
61 -1);
62 }
63 }
64
65 return GTK_TREE_MODEL (store);
66 }
67
68 /* A GtkCellLayoutDataFunc that demonstrates how one can control
69 * sensitivity of rows. This particular function does nothing
70 * useful and just makes the second row insensitive.
71 */
72 static void
73 set_sensitive (GtkCellLayout *cell_layout,
74 GtkCellRenderer *cell,
75 GtkTreeModel *tree_model,
76 GtkTreeIter *iter,
77 gpointer data)
78 {
79 GtkTreePath *path;
80 gint *indices;
81 gboolean sensitive;
82
83 path = gtk_tree_model_get_path (tree_model, iter);
84 indices = gtk_tree_path_get_indices (path);
85 sensitive = indices[0] != 1;
86 gtk_tree_path_free (path);
87
88 g_object_set (cell, "sensitive", sensitive, NULL);
89 }
90
91 /* A GtkTreeViewRowSeparatorFunc that demonstrates how rows can be
92 * rendered as separators. This particular function does nothing
93 * useful and just turns the fourth row into a separator.
94 */
95 static gboolean
96 is_separator (GtkTreeModel *model,
97 GtkTreeIter *iter,
98 gpointer data)
99 {
100 GtkTreePath *path;
101 gboolean result;
102
103 path = gtk_tree_model_get_path (model, iter);
104 result = gtk_tree_path_get_indices (path)[0] == 4;
105 gtk_tree_path_free (path);
106
107 return result;
108 }
109
110 static GtkTreeModel *
111 create_capital_store (void)
112 {
113 struct {
114 gchar *group;
115 gchar *capital;
116 } capitals[] = {
117 { "A - B", NULL },
118 { NULL, "Albany" },
119 { NULL, "Annapolis" },
120 { NULL, "Atlanta" },
121 { NULL, "Augusta" },
122 { NULL, "Austin" },
123 { NULL, "Baton Rouge" },
124 { NULL, "Bismarck" },
125 { NULL, "Boise" },
126 { NULL, "Boston" },
127 { "C - D", NULL },
128 { NULL, "Carson City" },
129 { NULL, "Charleston" },
130 { NULL, "Cheyenne" },
131 { NULL, "Columbia" },
132 { NULL, "Columbus" },
133 { NULL, "Concord" },
134 { NULL, "Denver" },
135 { NULL, "Des Moines" },
136 { NULL, "Dover" },
137 { "E - J", NULL },
138 { NULL, "Frankfort" },
139 { NULL, "Harrisburg" },
140 { NULL, "Hartford" },
141 { NULL, "Helena" },
142 { NULL, "Honolulu" },
143 { NULL, "Indianapolis" },
144 { NULL, "Jackson" },
145 { NULL, "Jefferson City" },
146 { NULL, "Juneau" },
147 { "K - O", NULL },
148 { NULL, "Lansing" },
149 { NULL, "Lincoln" },
150 { NULL, "Little Rock" },
151 { NULL, "Madison" },
152 { NULL, "Montgomery" },
153 { NULL, "Montpelier" },
154 { NULL, "Nashville" },
155 { NULL, "Oklahoma City" },
156 { NULL, "Olympia" },
157 { "P - S", NULL },
158 { NULL, "Phoenix" },
159 { NULL, "Pierre" },
160 { NULL, "Providence" },
161 { NULL, "Raleigh" },
162 { NULL, "Richmond" },
163 { NULL, "Sacramento" },
164 { NULL, "Salem" },
165 { NULL, "Salt Lake City" },
166 { NULL, "Santa Fe" },
167 { NULL, "Springfield" },
168 { NULL, "St. Paul" },
169 { "T - Z", NULL },
170 { NULL, "Tallahassee" },
171 { NULL, "Topeka" },
172 { NULL, "Trenton" },
173 { NULL, NULL }
174 };
175
176 GtkTreeIter iter, iter2;
177 GtkTreeStore *store;
178 gint i;
179
180 store = gtk_tree_store_new (1, G_TYPE_STRING);
181
182 for (i = 0; capitals[i].group || capitals[i].capital; i++)
183 {
184 if (capitals[i].group)
185 {
186 gtk_tree_store_append (store, &iter, NULL);
187 gtk_tree_store_set (store, &iter, 0, capitals[i].group, -1);
188 }
189 else if (capitals[i].capital)
190 {
191 gtk_tree_store_append (store, &iter2, &iter);
192 gtk_tree_store_set (store, &iter2, 0, capitals[i].capital, -1);
193 }
194 }
195
196 return GTK_TREE_MODEL (store);
197 }
198
199 static void
200 is_capital_sensitive (GtkCellLayout *cell_layout,
201 GtkCellRenderer *cell,
202 GtkTreeModel *tree_model,
203 GtkTreeIter *iter,
204 gpointer data)
205 {
206 gboolean sensitive;
207
208 sensitive = !gtk_tree_model_iter_has_child (tree_model, iter);
209
210 g_object_set (cell, "sensitive", sensitive, NULL);
211 }
212
213 static void
214 fill_combo_entry (GtkWidget *combo)
215 {
216 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "One");
217 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Two");
218 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "2\302\275");
219 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo), "Three");
220 }
221
222
223 /* A simple validating entry */
224
225 #define TYPE_MASK_ENTRY (mask_entry_get_type ())
226 #define MASK_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MASK_ENTRY, MaskEntry))
227 #define MASK_ENTRY_CLASS(vtable) (G_TYPE_CHECK_CLASS_CAST ((vtable), TYPE_MASK_ENTRY, MaskEntryClass))
228 #define IS_MASK_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MASK_ENTRY))
229 #define IS_MASK_ENTRY_CLASS(vtable) (G_TYPE_CHECK_CLASS_TYPE ((vtable), TYPE_MASK_ENTRY))
230 #define MASK_ENTRY_GET_CLASS(inst) (G_TYPE_INSTANCE_GET_CLASS ((inst), TYPE_MASK_ENTRY, MaskEntryClass))
231
232
233 typedef struct _MaskEntry MaskEntry;
234 struct _MaskEntry
235 {
236 GtkEntry entry;
237 gchar *mask;
238 };
239
240 typedef struct _MaskEntryClass MaskEntryClass;
241 struct _MaskEntryClass
242 {
243 GtkEntryClass parent_class;
244 };
245
246
247 static void mask_entry_editable_init (GtkEditableInterface *iface);
248
249 G_DEFINE_TYPE_WITH_CODE (MaskEntry, mask_entry, GTK_TYPE_ENTRY,
250 G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
251 mask_entry_editable_init));
252
253
254 static void
255 mask_entry_set_background (MaskEntry *entry)
256 {
257 if (entry->mask)
258 {
259 if (!g_regex_match_simple (entry->mask, gtk_entry_get_text (GTK_ENTRY (entry)), 0, 0))
260 {
261 PangoAttrList *attrs;
262
263 attrs = pango_attr_list_new ();
264 pango_attr_list_insert (attrs, pango_attr_foreground_new (65535, 32767, 32767));
265 gtk_entry_set_attributes (GTK_ENTRY (entry), attrs);
266 pango_attr_list_unref (attrs);
267 return;
268 }
269 }
270
271 gtk_entry_set_attributes (GTK_ENTRY (entry), NULL);
272 }
273
274
275 static void
276 mask_entry_changed (GtkEditable *editable)
277 {
278 mask_entry_set_background (MASK_ENTRY (editable));
279 }
280
281
282 static void
283 mask_entry_init (MaskEntry *entry)
284 {
285 entry->mask = NULL;
286 }
287
288
289 static void
290 mask_entry_class_init (MaskEntryClass *klass)
291 { }
292
293
294 static void
295 mask_entry_editable_init (GtkEditableInterface *iface)
296 {
297 iface->changed = mask_entry_changed;
298 }
299
300
301 GtkWidget *
302 do_combobox (GtkWidget *do_widget)
303 {
304 static GtkWidget *window = NULL;
305 GtkWidget *vbox, *frame, *box, *combo, *entry;
306 GtkTreeModel *model;
307 GtkCellRenderer *renderer;
308 GtkTreePath *path;
309 GtkTreeIter iter;
310
311 if (!window)
312 {
313 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
314 gtk_window_set_screen (GTK_WINDOW (window),
315 gtk_widget_get_screen (do_widget));
316 gtk_window_set_title (GTK_WINDOW (window), "Combo Boxes");
317
318 g_signal_connect (window, "destroy",
319 G_CALLBACK (gtk_widget_destroyed), &window);
320
321 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
322
323 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
324 gtk_container_add (GTK_CONTAINER (window), vbox);
325
326 /* A combobox demonstrating cell renderers, separators and
327 * insensitive rows
328 */
329 frame = gtk_frame_new ("Items with icons");
330 gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
331
332 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
333 gtk_container_set_border_width (GTK_CONTAINER (box), 5);
334 gtk_container_add (GTK_CONTAINER (frame), box);
335
336 model = create_icon_store ();
337 combo = gtk_combo_box_new_with_model (model);
338 g_object_unref (model);
339 gtk_container_add (GTK_CONTAINER (box), combo);
340
341 renderer = gtk_cell_renderer_pixbuf_new ();
342 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, FALSE);
343 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
344 "icon-name", ICON_NAME_COL,
345 NULL);
346
347 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
348 renderer,
349 set_sensitive,
350 NULL, NULL);
351
352 renderer = gtk_cell_renderer_text_new ();
353 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
354 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
355 "text", TEXT_COL,
356 NULL);
357
358 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
359 renderer,
360 set_sensitive,
361 NULL, NULL);
362
363 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
364 is_separator, NULL, NULL);
365
366 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
367
368 /* A combobox demonstrating trees.
369 */
370 frame = gtk_frame_new ("Where are we ?");
371 gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
372
373 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
374 gtk_container_set_border_width (GTK_CONTAINER (box), 5);
375 gtk_container_add (GTK_CONTAINER (frame), box);
376
377 model = create_capital_store ();
378 combo = gtk_combo_box_new_with_model (model);
379 g_object_unref (model);
380 gtk_container_add (GTK_CONTAINER (box), combo);
381
382 renderer = gtk_cell_renderer_text_new ();
383 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
384 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
385 "text", 0,
386 NULL);
387 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
388 renderer,
389 is_capital_sensitive,
390 NULL, NULL);
391
392 path = gtk_tree_path_new_from_indices (0, 8, -1);
393 gtk_tree_model_get_iter (model, &iter, path);
394 gtk_tree_path_free (path);
395 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo), &iter);
396
397 /* A GtkComboBoxEntry with validation */
398 frame = gtk_frame_new ("Editable");
399 gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
400
401 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
402 gtk_container_set_border_width (GTK_CONTAINER (box), 5);
403 gtk_container_add (GTK_CONTAINER (frame), box);
404
405 combo = gtk_combo_box_text_new_with_entry ();
406 fill_combo_entry (combo);
407 gtk_container_add (GTK_CONTAINER (box), combo);
408
409 entry = g_object_new (TYPE_MASK_ENTRY, NULL);
410 MASK_ENTRY (entry)->mask = "^([0-9]*|One|Two|2\302\275|Three)$";
411
412 gtk_container_remove (GTK_CONTAINER (combo), gtk_bin_get_child (GTK_BIN (combo)));
413 gtk_container_add (GTK_CONTAINER (combo), entry);
414
415 /* A combobox with string IDs */
416 frame = gtk_frame_new ("String IDs");
417 gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
418
419 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
420 gtk_container_set_border_width (GTK_CONTAINER (box), 5);
421 gtk_container_add (GTK_CONTAINER (frame), box);
422
423 combo = gtk_combo_box_text_new ();
424 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), "never", "Not visible");
425 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), "when-active", "Visible when active");
426 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), "always", "Always visible");
427 gtk_container_add (GTK_CONTAINER (box), combo);
428
429 entry = gtk_entry_new ();
430 g_object_bind_property (combo, "active-id",
431 entry, "text",
432 G_BINDING_BIDIRECTIONAL);
433 gtk_container_add (GTK_CONTAINER (box), entry);
434 }
435
436 if (!gtk_widget_get_visible (window))
437 gtk_widget_show_all (window);
438 else
439 gtk_widget_destroy (window);
440
441 return window;
442 }