1 /* GTK - The GIMP Toolkit
3 * Copyright (C) 2010 Christian Dywan
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 #include "gtkcomboboxtext.h"
22 #include "gtkcombobox.h"
23 #include "gtkcellrenderertext.h"
24 #include "gtkcelllayout.h"
25 #include "gtkbuildable.h"
26 #include "gtkbuilderprivate.h"
31 * SECTION:gtkcomboboxtext
32 * @Short_description: A simple, text-only combo box
33 * @Title: GtkComboBoxText
34 * @See_also: #GtkComboBox
36 * A GtkComboBoxText is a simple variant of #GtkComboBox that hides
37 * the model-view complexity for simple text-only use cases.
39 * To create a GtkComboBoxText, use gtk_combo_box_text_new() or
40 * gtk_combo_box_text_new_with_entry().
42 * You can add items to a GtkComboBoxText with
43 * gtk_combo_box_text_append_text(), gtk_combo_box_text_insert_text()
44 * or gtk_combo_box_text_prepend_text() and remove options with
45 * gtk_combo_box_text_remove().
47 * If the GtkComboBoxText contains an entry (via the 'has-entry' property),
48 * its contents can be retrieved using gtk_combo_box_text_get_active_text().
49 * The entry itself can be accessed by calling gtk_bin_get_child() on the
52 * <refsect2 id="GtkComboBoxText-BUILDER-UI">
53 * <title>GtkComboBoxText as GtkBuildable</title>
55 * The GtkComboBoxText implementation of the GtkBuildable interface
56 * supports adding items directly using the <items> element
57 * and specifying <item> elements for each item. Each <item>
58 * element supports the regular translation attributes "translatable",
59 * "context" and "comments".
62 * <title>A UI definition fragment specifying GtkComboBoxText items</title>
63 * <programlisting><![CDATA[
64 * <object class="GtkComboBoxText">
66 * <item translatable="yes">Factory</item>
67 * <item translatable="yes">Home</item>
68 * <item translatable="yes">Subway</item>
71 * ]]></programlisting>
77 static void gtk_combo_box_text_buildable_interface_init (GtkBuildableIface *iface);
78 static gboolean gtk_combo_box_text_buildable_custom_tag_start (GtkBuildable *buildable,
82 GMarkupParser *parser,
85 static void gtk_combo_box_text_buildable_custom_finished (GtkBuildable *buildable,
91 static GtkBuildableIface *buildable_parent_iface = NULL;
93 G_DEFINE_TYPE_WITH_CODE (GtkComboBoxText, gtk_combo_box_text, GTK_TYPE_COMBO_BOX,
94 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
95 gtk_combo_box_text_buildable_interface_init));
98 gtk_combo_box_text_constructor (GType type,
99 guint n_construct_properties,
100 GObjectConstructParam *construct_properties)
104 object = G_OBJECT_CLASS (gtk_combo_box_text_parent_class)->constructor
105 (type, n_construct_properties, construct_properties);
107 if (!gtk_combo_box_get_has_entry (GTK_COMBO_BOX (object)))
109 GtkCellRenderer *cell;
111 cell = gtk_cell_renderer_text_new ();
112 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), cell, TRUE);
113 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), cell,
122 gtk_combo_box_text_init (GtkComboBoxText *combo_box)
126 store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
127 gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
128 g_object_unref (store);
132 gtk_combo_box_text_class_init (GtkComboBoxTextClass *klass)
134 GObjectClass *object_class;
136 object_class = (GObjectClass *)klass;
137 object_class->constructor = gtk_combo_box_text_constructor;
141 gtk_combo_box_text_buildable_interface_init (GtkBuildableIface *iface)
143 buildable_parent_iface = g_type_interface_peek_parent (iface);
145 iface->custom_tag_start = gtk_combo_box_text_buildable_custom_tag_start;
146 iface->custom_finished = gtk_combo_box_text_buildable_custom_finished;
156 guint translatable : 1;
162 item_start_element (GMarkupParseContext *context,
163 const gchar *element_name,
165 const gchar **values,
169 ItemParserData *data = (ItemParserData*)user_data;
172 if (strcmp (element_name, "item") == 0)
174 data->is_text = TRUE;
176 for (i = 0; names[i]; i++)
178 if (strcmp (names[i], "translatable") == 0)
182 if (!_gtk_builder_boolean_from_string (values[i], &bval,
186 data->translatable = bval;
188 else if (strcmp (names[i], "comments") == 0)
190 /* do nothing, comments are for translators */
192 else if (strcmp (names[i], "context") == 0)
193 data->context = g_strdup (values[i]);
195 g_warning ("Unknown custom combo box item attribute: %s", names[i]);
201 item_text (GMarkupParseContext *context,
207 ItemParserData *data = (ItemParserData*)user_data;
213 string = g_strndup (text, text_len);
215 if (data->translatable && text_len)
219 /* FIXME: This will not use the domain set in the .ui file,
220 * since the parser is not telling the builder about the domain.
221 * However, it will work for gtk_builder_set_translation_domain() calls.
223 translated = _gtk_builder_parser_translate (data->domain,
230 data->string = string;
234 item_end_element (GMarkupParseContext *context,
235 const gchar *element_name,
239 ItemParserData *data = (ItemParserData*)user_data;
241 /* Append the translated strings */
243 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (data->object), data->string);
245 data->translatable = FALSE;
246 g_free (data->context);
247 g_free (data->string);
248 data->context = NULL;
250 data->is_text = FALSE;
253 static const GMarkupParser item_parser =
261 gtk_combo_box_text_buildable_custom_tag_start (GtkBuildable *buildable,
264 const gchar *tagname,
265 GMarkupParser *parser,
268 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
269 tagname, parser, data))
272 if (strcmp (tagname, "items") == 0)
274 ItemParserData *parser_data;
276 parser_data = g_slice_new0 (ItemParserData);
277 parser_data->builder = g_object_ref (builder);
278 parser_data->object = g_object_ref (buildable);
279 parser_data->domain = gtk_builder_get_translation_domain (builder);
280 *parser = item_parser;
288 gtk_combo_box_text_buildable_custom_finished (GtkBuildable *buildable,
291 const gchar *tagname,
294 ItemParserData *data;
296 buildable_parent_iface->custom_finished (buildable, builder, child,
299 if (strcmp (tagname, "items") == 0)
301 data = (ItemParserData*)user_data;
303 g_object_unref (data->object);
304 g_object_unref (data->builder);
305 g_slice_free (ItemParserData, data);
310 * gtk_combo_box_text_new:
312 * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
313 * strings. See gtk_combo_box_entry_new_with_text().
315 * Return value: A new #GtkComboBoxText
320 gtk_combo_box_text_new (void)
322 return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
323 "entry-text-column", 0,
329 * gtk_combo_box_text_new_with_entry:
331 * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
332 * strings. The combo box created by this function has an entry.
334 * Return value: a new #GtkComboBoxText
339 gtk_combo_box_text_new_with_entry (void)
341 return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
343 "entry-text-column", 0,
349 * gtk_combo_box_text_append_text:
350 * @combo_box: A #GtkComboBoxText
353 * Appends @text to the list of strings stored in @combo_box.
355 * This is the same as calling gtk_combo_box_text_insert_text() with a
361 gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
364 gtk_combo_box_text_insert (combo_box, -1, NULL, text);
368 * gtk_combo_box_text_prepend_text:
369 * @combo_box: A #GtkComboBox
372 * Prepends @text to the list of strings stored in @combo_box.
374 * This is the same as calling gtk_combo_box_text_insert_text() with a
380 gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
383 gtk_combo_box_text_insert (combo_box, 0, NULL, text);
387 * gtk_combo_box_text_insert_text:
388 * @combo_box: A #GtkComboBoxText
389 * @position: An index to insert @text
392 * Inserts @text at @position in the list of strings stored in @combo_box.
394 * If @position is negative then @text is appended.
396 * This is the same as calling gtk_combo_box_text_insert() with a %NULL
402 gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
406 gtk_combo_box_text_insert (combo_box, position, NULL, text);
410 * gtk_combo_box_text_append:
411 * @combo_box: A #GtkComboBoxText
414 * Appends @text to the list of strings stored in @combo_box. If @id is
415 * non-%NULL then it is used as the ID of the row.
417 * This is the same as calling gtk_combo_box_text_insert() with a
423 gtk_combo_box_text_append (GtkComboBoxText *combo_box,
427 gtk_combo_box_text_insert (combo_box, -1, id, text);
431 * gtk_combo_box_text_prepend:
432 * @combo_box: A #GtkComboBox
433 * @id: (allow-none): a string ID for this value, or %NULL
436 * Prepends @text to the list of strings stored in @combo_box. If @id
437 * is non-%NULL then it is used as the ID of the row.
439 * This is the same as calling gtk_combo_box_text_insert() with a
445 gtk_combo_box_text_prepend (GtkComboBoxText *combo_box,
449 gtk_combo_box_text_insert (combo_box, 0, id, text);
454 * gtk_combo_box_text_insert:
455 * @combo_box: A #GtkComboBoxText
456 * @position: An index to insert @text
457 * @id: (allow-none): a string ID for this value, or %NULL
458 * @text: A string to display
460 * Inserts @text at @position in the list of strings stored in @combo_box.
461 * If @id is non-%NULL then it is used as the ID of the row. See
462 * #GtkComboBox::id-column.
464 * If @position is negative then @text is appended.
469 gtk_combo_box_text_insert (GtkComboBoxText *combo_box,
479 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
480 g_return_if_fail (text != NULL);
482 store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
483 g_return_if_fail (GTK_IS_LIST_STORE (store));
485 text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
487 if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (combo_box)))
488 g_return_if_fail (text_column >= 0);
489 else if (text_column < 0)
492 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
493 g_return_if_fail (column_type == G_TYPE_STRING);
496 gtk_list_store_append (store, &iter);
498 gtk_list_store_insert (store, &iter, position);
500 gtk_list_store_set (store, &iter, text_column, text, -1);
506 id_column = gtk_combo_box_get_id_column (GTK_COMBO_BOX (combo_box));
507 g_return_if_fail (id_column >= 0);
508 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), id_column);
509 g_return_if_fail (column_type == G_TYPE_STRING);
511 gtk_list_store_set (store, &iter, id_column, id, -1);
516 * gtk_combo_box_text_remove:
517 * @combo_box: A #GtkComboBox
518 * @position: Index of the item to remove
520 * Removes the string at @position from @combo_box.
525 gtk_combo_box_text_remove (GtkComboBoxText *combo_box,
532 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
533 g_return_if_fail (position >= 0);
535 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
536 store = GTK_LIST_STORE (model);
537 g_return_if_fail (GTK_IS_LIST_STORE (store));
539 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, position))
540 gtk_list_store_remove (store, &iter);
544 * gtk_combo_box_text_remove_all:
545 * @combo_box: A #GtkComboBoxText
547 * Removes all the text entries from the combo box.
552 gtk_combo_box_text_remove_all (GtkComboBoxText *combo_box)
556 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
558 store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
559 gtk_list_store_clear (store);
563 * gtk_combo_box_text_get_active_text:
564 * @combo_box: A #GtkComboBoxText
566 * Returns the currently active string in @combo_box, or %NULL if none
567 * is selected. If @combo_box contains an entry, this function will return
568 * its contents (which will not necessarily be an item from the list).
570 * Returns: a newly allocated string containing the currently active text.
571 * Must be freed with g_free().
576 gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box)
581 g_return_val_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box), NULL);
583 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
589 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
590 g_return_val_if_fail (GTK_IS_LIST_STORE (model), NULL);
591 text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
592 g_return_val_if_fail (text_column >= 0, NULL);
593 column_type = gtk_tree_model_get_column_type (model, text_column);
594 g_return_val_if_fail (column_type == G_TYPE_STRING, NULL);
595 gtk_tree_model_get (model, &iter, text_column, &text, -1);