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"
27 * SECTION:gtkcomboboxtext
28 * @Short_description: A simple, text-only combo box
29 * @Title: GtkComboBoxText
30 * @See_also: #GtkComboBox
32 * A GtkComboBoxText is a simple variant of #GtkComboBox that hides
33 * the model-view complexity for simple text-only use cases.
35 * To create a GtkComboBoxText, use gtk_combo_box_text_new() or
36 * gtk_combo_box_text_new_with_entry().
38 * You can add items to a GtkComboBoxText with
39 * gtk_combo_box_text_append_text(), gtk_combo_box_text_insert_text()
40 * or gtk_combo_box_text_prepend_text() and remove options with
41 * gtk_combo_box_text_remove().
44 G_DEFINE_TYPE (GtkComboBoxText, gtk_combo_box_text, GTK_TYPE_COMBO_BOX);
47 gtk_combo_box_text_constructor (GType type,
48 guint n_construct_properties,
49 GObjectConstructParam *construct_properties)
53 object = G_OBJECT_CLASS (gtk_combo_box_text_parent_class)->constructor
54 (type, n_construct_properties, construct_properties);
56 if (!gtk_combo_box_get_has_entry (GTK_COMBO_BOX (object)))
58 GtkCellRenderer *cell;
60 cell = gtk_cell_renderer_text_new ();
61 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (object), cell, TRUE);
62 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (object), cell,
71 gtk_combo_box_text_init (GtkComboBoxText *combo_box)
75 store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
76 gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
77 g_object_unref (store);
81 gtk_combo_box_text_class_init (GtkComboBoxTextClass *klass)
83 GObjectClass *object_class;
85 object_class = (GObjectClass *)klass;
86 object_class->constructor = gtk_combo_box_text_constructor;
91 * gtk_combo_box_text_new:
93 * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
94 * strings. See gtk_combo_box_entry_new_with_text().
96 * Return value: A new #GtkComboBoxText
101 gtk_combo_box_text_new (void)
103 return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
104 "entry-text-column", 0,
110 * gtk_combo_box_text_new_with_entry:
112 * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
113 * strings. The combo box created by this function has an entry.
115 * Return value: a new #GtkComboBoxText
120 gtk_combo_box_text_new_with_entry (void)
122 return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
124 "entry-text-column", 0,
130 * gtk_combo_box_text_append_text:
131 * @combo_box: A #GtkComboBoxText
134 * Appends @text to the list of strings stored in @combo_box.
136 * This is the same as calling gtk_combo_box_text_insert_text() with a
142 gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
145 gtk_combo_box_text_insert (combo_box, -1, NULL, text);
149 * gtk_combo_box_text_prepend_text:
150 * @combo_box: A #GtkComboBox
153 * Prepends @text to the list of strings stored in @combo_box.
155 * This is the same as calling gtk_combo_box_text_insert_text() with a
161 gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
164 gtk_combo_box_text_insert (combo_box, 0, NULL, text);
168 * gtk_combo_box_text_insert_text:
169 * @combo_box: A #GtkComboBoxText
170 * @position: An index to insert @text
173 * Inserts @text at @position in the list of strings stored in @combo_box.
175 * If @position is negative then @text is appended.
177 * This is the same as calling gtk_combo_box_text_insert() with a %NULL
183 gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
187 gtk_combo_box_text_insert (combo_box, position, NULL, text);
191 * gtk_combo_box_text_append:
192 * @combo_box: A #GtkComboBoxText
195 * Appends @text to the list of strings stored in @combo_box. If @id is
196 * non-%NULL then it is used as the ID of the row.
198 * This is the same as calling gtk_combo_box_text_insert() with a
204 gtk_combo_box_text_append (GtkComboBoxText *combo_box,
208 gtk_combo_box_text_insert (combo_box, -1, id, text);
212 * gtk_combo_box_text_prepend:
213 * @combo_box: A #GtkComboBox
216 * Prepends @text to the list of strings stored in @combo_box. If @id
217 * is non-%NULL then it is used as the ID of the row.
219 * This is the same as calling gtk_combo_box_text_insert() with a
225 gtk_combo_box_text_prepend (GtkComboBoxText *combo_box,
229 gtk_combo_box_text_insert (combo_box, 0, id, text);
234 * gtk_combo_box_text_insert:
235 * @combo_box: A #GtkComboBoxText
236 * @position: An index to insert @text
237 * @id: a string ID for this value, or %NULL
238 * @text: A string to display
240 * Inserts @text at @position in the list of strings stored in @combo_box.
241 * If @id is non-%NULL then it is used as the ID of the row. See
242 * #GtkComboBox::id-column.
244 * If @position is negative then @text is appended.
249 gtk_combo_box_text_insert (GtkComboBoxText *combo_box,
259 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
260 g_return_if_fail (text != NULL);
265 store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
266 g_return_if_fail (GTK_IS_LIST_STORE (store));
267 text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
268 g_return_if_fail (text_column >= 0);
269 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
270 g_return_if_fail (column_type == G_TYPE_STRING);
272 gtk_list_store_insert (store, &iter, position);
273 gtk_list_store_set (store, &iter, text_column, text, -1);
279 id_column = gtk_combo_box_get_id_column (GTK_COMBO_BOX (combo_box));
280 g_return_if_fail (id_column >= 0);
281 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), id_column);
282 g_return_if_fail (column_type == G_TYPE_STRING);
284 gtk_list_store_set (store, &iter, id_column, id, -1);
289 * gtk_combo_box_text_remove:
290 * @combo_box: A #GtkComboBox
291 * @position: Index of the item to remove
293 * Removes the string at @position from @combo_box.
298 gtk_combo_box_text_remove (GtkComboBoxText *combo_box,
305 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
306 g_return_if_fail (position >= 0);
308 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
309 store = GTK_LIST_STORE (model);
310 g_return_if_fail (GTK_IS_LIST_STORE (store));
312 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, position))
313 gtk_list_store_remove (store, &iter);
317 * gtk_combo_box_text_remove_all:
318 * @combo_box: A #GtkComboBoxText
320 * Removes all the text entries from the combo box.
325 gtk_combo_box_text_remove_all (GtkComboBoxText *combo_box)
329 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
331 store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
332 gtk_list_store_clear (store);
336 * gtk_combo_box_text_get_active_text:
337 * @combo_box: A #GtkComboBoxText
339 * Returns the currently active string in @combo_box or %NULL if none
342 * Returns: a newly allocated string containing the currently active text.
343 * Must be freed with g_free().
348 gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box)
353 g_return_val_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box), NULL);
355 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
361 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
362 g_return_val_if_fail (GTK_IS_LIST_STORE (model), NULL);
363 text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
364 g_return_val_if_fail (text_column >= 0, NULL);
365 column_type = gtk_tree_model_get_column_type (model, text_column);
366 g_return_val_if_fail (column_type == G_TYPE_STRING, NULL);
367 gtk_tree_model_get (model, &iter, text_column, &text, -1);