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"
26 G_DEFINE_TYPE (GtkComboBoxText, gtk_combo_box_text, GTK_TYPE_COMBO_BOX);
29 gtk_combo_box_text_class_init (GtkComboBoxTextClass *klass)
34 gtk_combo_box_text_init (GtkComboBoxText *combo_box)
37 GtkCellRenderer *cell;
39 store = gtk_list_store_new (1, G_TYPE_STRING);
40 gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
41 g_object_unref (store);
43 cell = gtk_cell_renderer_text_new ();
44 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
45 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
51 * gtk_combo_box_text_new:
53 * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
54 * strings. See gtk_combo_box_entry_new_with_text().
56 * Return value: A new #GtkComboBoxText
61 gtk_combo_box_text_new (void)
63 return g_object_new (GTK_TYPE_COMBO_BOX_TEXT, NULL);
67 * gtk_combo_box_text_append_text:
68 * @combo_box: A #GtkComboBoxText
71 * Appends @string to the list of strings stored in @combo_box.
76 gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
83 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
84 g_return_if_fail (text != NULL);
86 store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
87 g_return_if_fail (GTK_IS_LIST_STORE (store));
88 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 0);
89 g_return_if_fail (column_type == G_TYPE_STRING);
91 gtk_list_store_append (store, &iter);
92 gtk_list_store_set (store, &iter, 0, text, -1);
96 * gtk_combo_box_text_insert_text:
97 * @combo_box: A #GtkComboBoxText
98 * @position: An index to insert @text
101 * Inserts @string at @position in the list of strings stored in @combo_box.
106 gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
114 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
115 g_return_if_fail (position >= 0);
116 g_return_if_fail (text != NULL);
118 store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
119 g_return_if_fail (GTK_IS_LIST_STORE (store));
120 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 0);
121 g_return_if_fail (column_type == G_TYPE_STRING);
123 gtk_list_store_insert (store, &iter, position);
124 gtk_list_store_set (store, &iter, 0, text, -1);
128 * gtk_combo_box_text_prepend_text:
129 * @combo_box: A #GtkComboBox
132 * Prepends @string to the list of strings stored in @combo_box.
137 gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
144 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
145 g_return_if_fail (text != NULL);
147 store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
148 g_return_if_fail (GTK_IS_LIST_STORE (store));
149 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 0);
150 g_return_if_fail (column_type == G_TYPE_STRING);
152 gtk_list_store_prepend (store, &iter);
153 gtk_list_store_set (store, &iter, 0, text, -1);
157 * gtk_combo_box_text_remove:
158 * @combo_box: A #GtkComboBox
159 * @position: Index of the item to remove
161 * Removes the string at @position from @combo_box.
166 gtk_combo_box_text_remove (GtkComboBoxText *combo_box,
174 g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
175 g_return_if_fail (position >= 0);
177 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
178 store = GTK_LIST_STORE (model);
179 g_return_if_fail (GTK_IS_LIST_STORE (store));
180 column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), 0);
181 g_return_if_fail (column_type == G_TYPE_STRING);
183 if (gtk_tree_model_iter_nth_child (model, &iter, NULL, position))
184 gtk_list_store_remove (store, &iter);
188 * gtk_combo_box_text_get_active_text:
189 * @combo_box: A #GtkComboBoxText
191 * Returns the currently active string in @combo_box or %NULL if none
194 * Returns: a newly allocated string containing the currently active text.
195 * Must be freed with g_free().
200 gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box)
205 g_return_val_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box), NULL);
207 if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
212 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
213 g_return_val_if_fail (GTK_IS_LIST_STORE (model), NULL);
214 column_type = gtk_tree_model_get_column_type (model, 0);
215 g_return_val_if_fail (column_type == G_TYPE_STRING, NULL);
216 gtk_tree_model_get (model, &iter, 0, &text, -1);