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 * You should not call gtk_combo_box_set_model() or attempt to pack more cells
53 * into this combo box via its GtkCellLayout interface.
55 * <refsect2 id="GtkComboBoxText-BUILDER-UI">
56 * <title>GtkComboBoxText as GtkBuildable</title>
58 * The GtkComboBoxText implementation of the GtkBuildable interface
59 * supports adding items directly using the <items> element
60 * and specifying <item> elements for each item. Each <item>
61 * element supports the regular translation attributes "translatable",
62 * "context" and "comments".
65 * <title>A UI definition fragment specifying GtkComboBoxText items</title>
66 * <programlisting><![CDATA[
67 * <object class="GtkComboBoxText">
69 * <item translatable="yes">Factory</item>
70 * <item translatable="yes">Home</item>
71 * <item translatable="yes">Subway</item>
74 * ]]></programlisting>
80 static void gtk_combo_box_text_buildable_interface_init (GtkBuildableIface *iface);
81 static gboolean gtk_combo_box_text_buildable_custom_tag_start (GtkBuildable *buildable,
85 GMarkupParser *parser,
88 static void gtk_combo_box_text_buildable_custom_finished (GtkBuildable *buildable,
94 static GtkBuildableIface *buildable_parent_iface = NULL;
96 G_DEFINE_TYPE_WITH_CODE (GtkComboBoxText, gtk_combo_box_text, GTK_TYPE_COMBO_BOX,
97 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
98 gtk_combo_box_text_buildable_interface_init));
101 gtk_combo_box_text_constructor (GType type,
102 guint n_construct_properties,
103 GObjectConstructParam *construct_properties)
106 const gint text_column = 0;
108 object = G_OBJECT_CLASS (gtk_combo_box_text_parent_class)->constructor
109 (type, n_construct_properties, construct_properties);
111 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (object), text_column);
112 gtk_combo_box_set_id_column (GTK_COMBO_BOX (object), 1);
114 if (!gtk_combo_box_get_has_entry (GTK_COMBO_BOX (object)))
116 GtkCellRenderer *cell;
118 cell = gtk_cell_renderer_text_new ();
119 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), cell, TRUE);
120 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), cell,
129 gtk_combo_box_text_init (GtkComboBoxText *combo_box)
133 store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
134 gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
135 g_object_unref (store);
139 gtk_combo_box_text_class_init (GtkComboBoxTextClass *klass)
141 GObjectClass *object_class;
143 object_class = (GObjectClass *)klass;
144 object_class->constructor = gtk_combo_box_text_constructor;
148 gtk_combo_box_text_buildable_interface_init (GtkBuildableIface *iface)
150 buildable_parent_iface = g_type_interface_peek_parent (iface);
152 iface->custom_tag_start = gtk_combo_box_text_buildable_custom_tag_start;
153 iface->custom_finished = gtk_combo_box_text_buildable_custom_finished;
165 guint translatable : 1;
171 item_start_element (GMarkupParseContext *context,
172 const gchar *element_name,
174 const gchar **values,
178 ItemParserData *data = (ItemParserData*)user_data;
181 if (strcmp (element_name, "item") == 0)
183 data->is_text = TRUE;
185 for (i = 0; names[i]; i++)
187 if (strcmp (names[i], "translatable") == 0)
191 if (!_gtk_builder_boolean_from_string (values[i], &bval,
195 data->translatable = bval;
197 else if (strcmp (names[i], "comments") == 0)
199 /* do nothing, comments are for translators */
201 else if (strcmp (names[i], "context") == 0)
202 data->context = g_strdup (values[i]);
203 else if (strcmp (names[i], "id") == 0)
204 data->id = g_strdup (values[i]);
206 g_warning ("Unknown custom combo box item attribute: %s", names[i]);
212 item_text (GMarkupParseContext *context,
218 ItemParserData *data = (ItemParserData*)user_data;
221 g_string_append_len (data->string, text, text_len);
225 item_end_element (GMarkupParseContext *context,
226 const gchar *element_name,
230 ItemParserData *data = (ItemParserData*)user_data;
232 /* Append the translated strings */
233 if (data->string->len)
235 if (data->translatable)
239 /* FIXME: This will not use the domain set in the .ui file,
240 * since the parser is not telling the builder about the domain.
241 * However, it will work for gtk_builder_set_translation_domain() calls.
243 translated = _gtk_builder_parser_translate (data->domain,
246 g_string_set_size (data->string, 0);
247 g_string_append (data->string, translated);
250 gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (data->object), data->id, data->string->str);
253 data->translatable = FALSE;
254 g_string_set_size (data->string, 0);
255 g_free (data->context);
256 data->context = NULL;
259 data->is_text = FALSE;
262 static const GMarkupParser item_parser =
270 gtk_combo_box_text_buildable_custom_tag_start (GtkBuildable *buildable,
273 const gchar *tagname,
274 GMarkupParser *parser,
277 if (buildable_parent_iface->custom_tag_start (buildable, builder, child,
278 tagname, parser, data))
281 if (strcmp (tagname, "items") == 0)
283 ItemParserData *parser_data;
285 parser_data = g_slice_new0 (ItemParserData);
286 parser_data->builder = g_object_ref (builder);
287 parser_data->object = g_object_ref (buildable);
288 parser_data->domain = gtk_builder_get_translation_domain (builder);
289 parser_data->string = g_string_new ("");
290 *parser = item_parser;
298 gtk_combo_box_text_buildable_custom_finished (GtkBuildable *buildable,
301 const gchar *tagname,
304 ItemParserData *data;
306 buildable_parent_iface->custom_finished (buildable, builder, child,
309 if (strcmp (tagname, "items") == 0)
311 data = (ItemParserData*)user_data;
313 g_object_unref (data->object);
314 g_object_unref (data->builder);
315 g_string_free (data->string, TRUE);
316 g_slice_free (ItemParserData, data);
321 * gtk_combo_box_text_new:
323 * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
326 * Return value: A new #GtkComboBoxText
331 gtk_combo_box_text_new (void)
333 return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
338 * gtk_combo_box_text_new_with_entry:
340 * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
341 * strings. The combo box created by this function has an entry.
343 * Return value: a new #GtkComboBoxText
348 gtk_combo_box_text_new_with_entry (void)
350 return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
356 * gtk_combo_box_text_append_text:
357 * @combo_box: A #GtkComboBoxText
360 * Appends @text to the list of strings stored in @combo_box.
362 * This is the same as calling gtk_combo_box_text_insert_text() with a
368 gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
371 gtk_combo_box_text_insert (combo_box, -1, NULL, text);
375 * gtk_combo_box_text_prepend_text:
376 * @combo_box: A #GtkComboBox
379 * Prepends @text to the list of strings stored in @combo_box.
381 * This is the same as calling gtk_combo_box_text_insert_text() with a
387 gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
390 gtk_combo_box_text_insert (combo_box, 0, NULL, text);
394 * gtk_combo_box_text_insert_text:
395 * @combo_box: A #GtkComboBoxText
396 * @position: An index to insert @text
399 * Inserts @text at @position in the list of strings stored in @combo_box.
401 * If @position is negative then @text is appended.
403 * This is the same as calling gtk_combo_box_text_insert() with a %NULL
409 gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
413 gtk_combo_box_text_insert (combo_box, position, NULL, text);
417 * gtk_combo_box_text_append:
418 * @combo_box: A #GtkComboBoxText
419 * @id: (allow-none): a string ID for this value, or %NULL
422 * Appends @text to the list of strings stored in @combo_box.
423 * If @id is non-%NULL then it is used as the ID of the row.
425 * This is the same as calling gtk_combo_box_text_insert() with a
431 gtk_combo_box_text_append (GtkComboBoxText *combo_box,
435 gtk_combo_box_text_insert (combo_box, -1, id, text);
439 * gtk_combo_box_text_prepend:
440 * @combo_box: A #GtkComboBox
441 * @id: (allow-none): a string ID for this value, or %NULL
444 * Prepends @text to the list of strings stored in @combo_box.
445 * If @id is non-%NULL then it is used as the ID of the row.
447 * This is the same as calling gtk_combo_box_text_insert() with a
453 gtk_combo_box_text_prepend (GtkComboBoxText *combo_box,
457 gtk_combo_box_text_insert (combo_box, 0, id, text);
462 * gtk_combo_box_text_insert:
463 * @combo_box: A #GtkComboBoxText
464 * @position: An index to insert @text
465 * @id: (allow-none): a string ID for this value, or %NULL
466 * @text: A string to display
468 * Inserts @text at @position in the list of strings stored in @combo_box.
469 * If @id is non-%NULL then it is used as the ID of the row. See
470 * #GtkComboBox:id-column.
472 * If @position is negative then @text is appended.
477 gtk_combo_box_text_insert (GtkComboBoxText *combo_box,
487 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
488 g_return_if_fail (text != NULL);
490 store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
491 g_return_if_fail (GTK_IS_LIST_STORE (store));
493 text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
495 if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (combo_box)))
496 g_return_if_fail (text_column >= 0);
497 else if (text_column < 0)
500 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
501 g_return_if_fail (column_type == G_TYPE_STRING);
504 gtk_list_store_append (store, &iter);
506 gtk_list_store_insert (store, &iter, position);
508 gtk_list_store_set (store, &iter, text_column, text, -1);
514 id_column = gtk_combo_box_get_id_column (GTK_COMBO_BOX (combo_box));
515 g_return_if_fail (id_column >= 0);
516 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), id_column);
517 g_return_if_fail (column_type == G_TYPE_STRING);
519 gtk_list_store_set (store, &iter, id_column, id, -1);
524 * gtk_combo_box_text_remove:
525 * @combo_box: A #GtkComboBox
526 * @position: Index of the item to remove
528 * Removes the string at @position from @combo_box.
533 gtk_combo_box_text_remove (GtkComboBoxText *combo_box,
540 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
541 g_return_if_fail (position >= 0);
543 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
544 store = GTK_LIST_STORE (model);
545 g_return_if_fail (GTK_IS_LIST_STORE (store));
547 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, position))
548 gtk_list_store_remove (store, &iter);
552 * gtk_combo_box_text_remove_all:
553 * @combo_box: A #GtkComboBoxText
555 * Removes all the text entries from the combo box.
560 gtk_combo_box_text_remove_all (GtkComboBoxText *combo_box)
564 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
566 store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
567 gtk_list_store_clear (store);
571 * gtk_combo_box_text_get_active_text:
572 * @combo_box: A #GtkComboBoxText
574 * Returns the currently active string in @combo_box, or %NULL
575 * if none is selected. If @combo_box contains an entry, this
576 * function will return its contents (which will not necessarily
577 * be an item from the list).
579 * Returns: (transfer full): a newly allocated string containing the
580 * currently active text. Must be freed with g_free().
585 gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box)
590 g_return_val_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box), NULL);
592 if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (combo_box)))
596 entry = gtk_bin_get_child (GTK_BIN (combo_box));
597 text = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
599 else if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
605 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
606 g_return_val_if_fail (GTK_IS_LIST_STORE (model), NULL);
607 text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
608 g_return_val_if_fail (text_column >= 0, NULL);
609 column_type = gtk_tree_model_get_column_type (model, text_column);
610 g_return_val_if_fail (column_type == G_TYPE_STRING, NULL);
611 gtk_tree_model_get (model, &iter, text_column, &text, -1);