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.
7 * How the options are displayed is controlled by cell renderers.
19 strip_underscore (const gchar *text)
24 result = g_strdup (text);
41 create_stock_icon_store (void)
43 gchar *stock_id[6] = {
44 GTK_STOCK_DIALOG_WARNING,
60 cellview = gtk_cell_view_new ();
62 store = gtk_list_store_new (2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
64 for (i = 0; i < G_N_ELEMENTS (stock_id); i++)
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,
77 g_object_unref (pixbuf);
82 gtk_list_store_append (store, &iter);
83 gtk_list_store_set (store, &iter,
85 TEXT_COL, "separator",
90 gtk_widget_destroy (cellview);
92 return GTK_TREE_MODEL (store);
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.
100 set_sensitive (GtkCellLayout *cell_layout,
101 GtkCellRenderer *cell,
102 GtkTreeModel *tree_model,
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);
115 g_object_set (cell, "sensitive", sensitive, NULL);
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.
123 is_separator (GtkTreeModel *model,
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);
137 static GtkTreeModel *
138 create_capital_store (void)
146 { NULL, "Annapolis" },
150 { NULL, "Baton Rouge" },
151 { NULL, "Bismarck" },
155 { NULL, "Carson City" },
156 { NULL, "Charleston" },
157 { NULL, "Cheyenne" },
158 { NULL, "Columbia" },
159 { NULL, "Columbus" },
162 { NULL, "Des Moines" },
165 { NULL, "Frankfort" },
166 { NULL, "Harrisburg" },
167 { NULL, "Hartford" },
169 { NULL, "Honolulu" },
170 { NULL, "Indianapolis" },
172 { NULL, "Jefferson City" },
177 { NULL, "Little Rock" },
179 { NULL, "Montgomery" },
180 { NULL, "Montpelier" },
181 { NULL, "Nashville" },
182 { NULL, "Oklahoma City" },
187 { NULL, "Providence" },
189 { NULL, "Richmond" },
190 { NULL, "Sacramento" },
192 { NULL, "Salt Lake City" },
193 { NULL, "Santa Fe" },
194 { NULL, "Springfield" },
195 { NULL, "St. Paul" },
197 { NULL, "Tallahassee" },
203 GtkTreeIter iter, iter2;
207 store = gtk_tree_store_new (1, G_TYPE_STRING);
209 for (i = 0; capitals[i].group || capitals[i].capital; i++)
211 if (capitals[i].group)
213 gtk_tree_store_append (store, &iter, NULL);
214 gtk_tree_store_set (store, &iter, 0, capitals[i].group, -1);
216 else if (capitals[i].capital)
218 gtk_tree_store_append (store, &iter2, &iter);
219 gtk_tree_store_set (store, &iter2, 0, capitals[i].capital, -1);
223 return GTK_TREE_MODEL (store);
227 is_capital_sensitive (GtkCellLayout *cell_layout,
228 GtkCellRenderer *cell,
229 GtkTreeModel *tree_model,
235 sensitive = !gtk_tree_model_iter_has_child (tree_model, iter);
237 g_object_set (cell, "sensitive", sensitive, NULL);
241 fill_combo_entry (GtkWidget *entry)
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");
250 do_combobox (GtkWidget *do_widget)
252 static GtkWidget *window = NULL;
253 GtkWidget *vbox, *frame, *box, *combo;
255 GtkCellRenderer *renderer;
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");
266 g_signal_connect (window, "destroy",
267 G_CALLBACK (gtk_widget_destroyed),
270 gtk_container_set_border_width (GTK_CONTAINER (window), 10);
272 vbox = gtk_vbox_new (FALSE, 2);
273 gtk_container_add (GTK_CONTAINER (window), vbox);
275 /* A combobox demonstrating cell renderers, separators and
278 frame = gtk_frame_new ("Some stock icons");
279 gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
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);
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);
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,
296 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
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,
307 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
312 gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo),
313 is_separator, NULL, NULL);
315 gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
317 /* A combobox demonstrating trees.
319 frame = gtk_frame_new ("Where are we ?");
320 gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
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);
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);
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,
336 gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo),
338 is_capital_sensitive,
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);
346 /* A GtkComboBoxEntry
348 frame = gtk_frame_new ("Editable");
349 gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
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);
355 combo = gtk_combo_box_entry_new_text ();
356 fill_combo_entry (combo);
357 gtk_container_add (GTK_CONTAINER (box), combo);
361 if (!GTK_WIDGET_VISIBLE (window))
363 gtk_widget_show_all (window);
367 gtk_widget_destroy (window);