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>
76 static void gtk_combo_box_text_buildable_interface_init (GtkBuildableIface *iface);
77 static gboolean gtk_combo_box_text_buildable_custom_tag_start (GtkBuildable *buildable,
81 GMarkupParser *parser,
84 static void gtk_combo_box_text_buildable_custom_finished (GtkBuildable *buildable,
90 static GtkBuildableIface *buildable_parent_iface = NULL;
92 G_DEFINE_TYPE_WITH_CODE (GtkComboBoxText, gtk_combo_box_text, GTK_TYPE_COMBO_BOX,
93 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
94 gtk_combo_box_text_buildable_interface_init));
97 gtk_combo_box_text_constructor (GType type,
98 guint n_construct_properties,
99 GObjectConstructParam *construct_properties)
103 object = G_OBJECT_CLASS (gtk_combo_box_text_parent_class)->constructor
104 (type, n_construct_properties, construct_properties);
106 if (!gtk_combo_box_get_has_entry (GTK_COMBO_BOX (object)))
108 GtkCellRenderer *cell;
110 cell = gtk_cell_renderer_text_new ();
111 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), cell, TRUE);
112 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), cell,
121 gtk_combo_box_text_init (GtkComboBoxText *combo_box)
125 store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
126 gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
127 g_object_unref (store);
131 gtk_combo_box_text_class_init (GtkComboBoxTextClass *klass)
133 GObjectClass *object_class;
135 object_class = (GObjectClass *)klass;
136 object_class->constructor = gtk_combo_box_text_constructor;
140 gtk_combo_box_text_buildable_interface_init (GtkBuildableIface *iface)
142 buildable_parent_iface = g_type_interface_peek_parent (iface);
144 iface->custom_tag_start = gtk_combo_box_text_buildable_custom_tag_start;
145 iface->custom_finished = gtk_combo_box_text_buildable_custom_finished;
155 guint translatable : 1;
161 item_start_element (GMarkupParseContext *context,
162 const gchar *element_name,
164 const gchar **values,
168 ItemParserData *data = (ItemParserData*)user_data;
171 if (strcmp (element_name, "item") == 0)
173 data->is_text = TRUE;
175 for (i = 0; names[i]; i++)
177 if (strcmp (names[i], "translatable") == 0)
181 if (!_gtk_builder_boolean_from_string (values[i], &bval,
185 data->translatable = bval;
187 else if (strcmp (names[i], "comments") == 0)
189 /* do nothing, comments are for translators */
191 else if (strcmp (names[i], "context") == 0)
192 data->context = g_strdup (values[i]);
194 g_warning ("Unknown custom combo box item attribute: %s", names[i]);
200 item_text (GMarkupParseContext *context,
206 ItemParserData *data = (ItemParserData*)user_data;
212 string = g_strndup (text, text_len);
214 if (data->translatable && text_len)
218 /* FIXME: This will not use the domain set in the .ui file,
219 * since the parser is not telling the builder about the domain.
220 * However, it will work for gtk_builder_set_translation_domain() calls.
222 translated = _gtk_builder_parser_translate (data->domain,
229 data->string = string;
233 item_end_element (GMarkupParseContext *context,
234 const gchar *element_name,
238 ItemParserData *data = (ItemParserData*)user_data;
240 /* Append the translated strings */
242 gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (data->object), data->string);
244 data->translatable = FALSE;
245 g_free (data->context);
246 g_free (data->string);
247 data->context = NULL;
249 data->is_text = FALSE;
252 static const GMarkupParser item_parser =
260 gtk_combo_box_text_buildable_custom_tag_start (GtkBuildable *buildable,
263 const gchar *tagname,
264 GMarkupParser *parser,
267 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
268 tagname, parser, data))
271 if (strcmp (tagname, "items") == 0)
273 ItemParserData *parser_data;
275 parser_data = g_slice_new0 (ItemParserData);
276 parser_data->builder = g_object_ref (builder);
277 parser_data->object = g_object_ref (buildable);
278 parser_data->domain = gtk_builder_get_translation_domain (builder);
279 *parser = item_parser;
287 gtk_combo_box_text_buildable_custom_finished (GtkBuildable *buildable,
290 const gchar *tagname,
293 ItemParserData *data;
295 buildable_parent_iface->custom_finished (buildable, builder, child,
298 if (strcmp (tagname, "items") == 0)
300 data = (ItemParserData*)user_data;
302 g_object_unref (data->object);
303 g_object_unref (data->builder);
304 g_slice_free (ItemParserData, data);
309 * gtk_combo_box_text_new:
311 * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
312 * strings. See gtk_combo_box_entry_new_with_text().
314 * Return value: A new #GtkComboBoxText
319 gtk_combo_box_text_new (void)
321 return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
322 "entry-text-column", 0,
328 * gtk_combo_box_text_new_with_entry:
330 * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
331 * strings. The combo box created by this function has an entry.
333 * Return value: a new #GtkComboBoxText
338 gtk_combo_box_text_new_with_entry (void)
340 return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
342 "entry-text-column", 0,
348 * gtk_combo_box_text_append_text:
349 * @combo_box: A #GtkComboBoxText
352 * Appends @text to the list of strings stored in @combo_box.
354 * This is the same as calling gtk_combo_box_text_insert_text() with a
360 gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
363 gtk_combo_box_text_insert (combo_box, -1, NULL, text);
367 * gtk_combo_box_text_prepend_text:
368 * @combo_box: A #GtkComboBox
371 * Prepends @text to the list of strings stored in @combo_box.
373 * This is the same as calling gtk_combo_box_text_insert_text() with a
379 gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
382 gtk_combo_box_text_insert (combo_box, 0, NULL, text);
386 * gtk_combo_box_text_insert_text:
387 * @combo_box: A #GtkComboBoxText
388 * @position: An index to insert @text
391 * Inserts @text at @position in the list of strings stored in @combo_box.
393 * If @position is negative then @text is appended.
395 * This is the same as calling gtk_combo_box_text_insert() with a %NULL
401 gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
405 gtk_combo_box_text_insert (combo_box, position, NULL, text);
409 * gtk_combo_box_text_append:
410 * @combo_box: A #GtkComboBoxText
413 * Appends @text to the list of strings stored in @combo_box. If @id is
414 * non-%NULL then it is used as the ID of the row.
416 * This is the same as calling gtk_combo_box_text_insert() with a
422 gtk_combo_box_text_append (GtkComboBoxText *combo_box,
426 gtk_combo_box_text_insert (combo_box, -1, id, text);
430 * gtk_combo_box_text_prepend:
431 * @combo_box: A #GtkComboBox
432 * @id: (allow-none): a string ID for this value, or %NULL
435 * Prepends @text to the list of strings stored in @combo_box. If @id
436 * is non-%NULL then it is used as the ID of the row.
438 * This is the same as calling gtk_combo_box_text_insert() with a
444 gtk_combo_box_text_prepend (GtkComboBoxText *combo_box,
448 gtk_combo_box_text_insert (combo_box, 0, id, text);
453 * gtk_combo_box_text_insert:
454 * @combo_box: A #GtkComboBoxText
455 * @position: An index to insert @text
456 * @id: (allow-none): a string ID for this value, or %NULL
457 * @text: A string to display
459 * Inserts @text at @position in the list of strings stored in @combo_box.
460 * If @id is non-%NULL then it is used as the ID of the row. See
461 * #GtkComboBox::id-column.
463 * If @position is negative then @text is appended.
468 gtk_combo_box_text_insert (GtkComboBoxText *combo_box,
478 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
479 g_return_if_fail (text != NULL);
481 store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
482 g_return_if_fail (GTK_IS_LIST_STORE (store));
484 text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
486 if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (combo_box)))
487 g_return_if_fail (text_column >= 0);
488 else if (text_column < 0)
491 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
492 g_return_if_fail (column_type == G_TYPE_STRING);
495 gtk_list_store_append (store, &iter);
497 gtk_list_store_insert (store, &iter, position);
499 gtk_list_store_set (store, &iter, text_column, text, -1);
505 id_column = gtk_combo_box_get_id_column (GTK_COMBO_BOX (combo_box));
506 g_return_if_fail (id_column >= 0);
507 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), id_column);
508 g_return_if_fail (column_type == G_TYPE_STRING);
510 gtk_list_store_set (store, &iter, id_column, id, -1);
515 * gtk_combo_box_text_remove:
516 * @combo_box: A #GtkComboBox
517 * @position: Index of the item to remove
519 * Removes the string at @position from @combo_box.
524 gtk_combo_box_text_remove (GtkComboBoxText *combo_box,
531 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
532 g_return_if_fail (position >= 0);
534 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
535 store = GTK_LIST_STORE (model);
536 g_return_if_fail (GTK_IS_LIST_STORE (store));
538 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, position))
539 gtk_list_store_remove (store, &iter);
543 * gtk_combo_box_text_remove_all:
544 * @combo_box: A #GtkComboBoxText
546 * Removes all the text entries from the combo box.
551 gtk_combo_box_text_remove_all (GtkComboBoxText *combo_box)
555 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
557 store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
558 gtk_list_store_clear (store);
562 * gtk_combo_box_text_get_active_text:
563 * @combo_box: A #GtkComboBoxText
565 * Returns the currently active string in @combo_box, or %NULL if none
566 * is selected. If @combo_box contains an entry, this function will return
567 * its contents (which will not necessarily be an item from the list).
569 * Returns: a newly allocated string containing the currently active text.
570 * Must be freed with g_free().
575 gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box)
580 g_return_val_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box), NULL);
582 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
588 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
589 g_return_val_if_fail (GTK_IS_LIST_STORE (model), NULL);
590 text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
591 g_return_val_if_fail (text_column >= 0, NULL);
592 column_type = gtk_tree_model_get_column_type (model, text_column);
593 g_return_val_if_fail (column_type == G_TYPE_STRING, NULL);
594 gtk_tree_model_get (model, &iter, text_column, &text, -1);