]> Pileus Git - ~andy/gtk/blob - gtk/gtkcomboboxtext.c
comboboxtext: Add gtk_combo_box_text_remove_all()
[~andy/gtk] / gtk / gtkcomboboxtext.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2010 Christian Dywan
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include "config.h"
20
21 #include "gtkcomboboxtext.h"
22 #include "gtkcombobox.h"
23 #include "gtkcellrenderertext.h"
24 #include "gtkcelllayout.h"
25
26 /**
27  * SECTION:gtkcomboboxtext
28  * @Short_description: A simple, text-only combo box
29  * @Title: GtkComboBoxText
30  * @See_also: @GtkComboBox
31  *
32  * A GtkComboBoxText is a simple variant of #GtkComboBox that hides
33  * the model-view complexity for simple text-only use cases.
34  *
35  * To create a GtkComboBoxText, use gtk_combo_box_text_new() or
36  * gtk_combo_box_text_new_with_entry().
37  *
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().
42  */
43
44 G_DEFINE_TYPE (GtkComboBoxText, gtk_combo_box_text, GTK_TYPE_COMBO_BOX);
45
46 static GObject *
47 gtk_combo_box_text_constructor (GType                  type,
48                                 guint                  n_construct_properties,
49                                 GObjectConstructParam *construct_properties)
50 {
51   GObject            *object;
52
53   object = G_OBJECT_CLASS (gtk_combo_box_text_parent_class)->constructor
54     (type, n_construct_properties, construct_properties);
55
56   if (!gtk_combo_box_get_has_entry (GTK_COMBO_BOX (object)))
57     {
58       GtkCellRenderer *cell;
59
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,
63                                       "text", 0,
64                                       NULL);
65     }
66
67   return object;
68 }
69
70 static void
71 gtk_combo_box_text_init (GtkComboBoxText *combo_box)
72 {
73   GtkListStore *store;
74
75   store = gtk_list_store_new (1, G_TYPE_STRING);
76   gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
77   g_object_unref (store);
78 }
79
80 static void
81 gtk_combo_box_text_class_init (GtkComboBoxTextClass *klass)
82 {
83   GObjectClass *object_class;
84
85   object_class = (GObjectClass *)klass;
86   object_class->constructor = gtk_combo_box_text_constructor;
87 }
88
89
90 /**
91  * gtk_combo_box_text_new:
92  *
93  * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
94  * strings. See gtk_combo_box_entry_new_with_text().
95  *
96  * Return value: A new #GtkComboBoxText
97  *
98  * Since: 2.24
99  */
100 GtkWidget *
101 gtk_combo_box_text_new (void)
102 {
103   return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
104                        "entry-text-column", 0,
105                        NULL);
106 }
107
108 /**
109  * gtk_combo_box_text_new_with_entry:
110  *
111  * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
112  * strings. The combo box created by this function has an entry.
113  *
114  * Return value: a new #GtkComboBoxText
115  *
116  * Since: 2.24
117  */
118 GtkWidget *
119 gtk_combo_box_text_new_with_entry (void)
120 {
121   return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
122                        "has-entry", TRUE,
123                        "entry-text-column", 0,
124                        NULL);
125 }
126
127 /**
128  * gtk_combo_box_text_append_text:
129  * @combo_box: A #GtkComboBoxText
130  * @text: A string
131  *
132  * Appends @string to the list of strings stored in @combo_box.
133  *
134  * Since: 2.24
135  */
136 void
137 gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
138                                 const gchar     *text)
139 {
140   GtkListStore *store;
141   GtkTreeIter iter;
142   gint text_column;
143   gint column_type;
144
145   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
146   g_return_if_fail (text != NULL);
147
148   store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
149   g_return_if_fail (GTK_IS_LIST_STORE (store));
150   text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
151   column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
152   g_return_if_fail (column_type == G_TYPE_STRING);
153
154   gtk_list_store_append (store, &iter);
155   gtk_list_store_set (store, &iter, text_column, text, -1);
156 }
157
158 /**
159  * gtk_combo_box_text_insert_text:
160  * @combo_box: A #GtkComboBoxText
161  * @position: An index to insert @text
162  * @text: A string
163  *
164  * Inserts @string at @position in the list of strings stored in @combo_box.
165  *
166  * Since: 2.24
167  */
168 void
169 gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
170                                 gint             position,
171                                 const gchar     *text)
172 {
173   GtkListStore *store;
174   GtkTreeIter iter;
175   gint text_column;
176   gint column_type;
177
178   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
179   g_return_if_fail (position >= 0);
180   g_return_if_fail (text != NULL);
181
182   store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
183   g_return_if_fail (GTK_IS_LIST_STORE (store));
184   text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
185   column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
186   g_return_if_fail (column_type == G_TYPE_STRING);
187
188   gtk_list_store_insert (store, &iter, position);
189   gtk_list_store_set (store, &iter, text_column, text, -1);
190 }
191
192 /**
193  * gtk_combo_box_text_prepend_text:
194  * @combo_box: A #GtkComboBox
195  * @text: A string
196  *
197  * Prepends @string to the list of strings stored in @combo_box.
198  *
199  * Since: 2.24
200  */
201 void
202 gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
203                                  const gchar     *text)
204 {
205   GtkListStore *store;
206   GtkTreeIter iter;
207   gint text_column;
208   gint column_type;
209
210   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
211   g_return_if_fail (text != NULL);
212
213   store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
214   g_return_if_fail (GTK_IS_LIST_STORE (store));
215   text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
216   column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
217   g_return_if_fail (column_type == G_TYPE_STRING);
218
219   gtk_list_store_prepend (store, &iter);
220   gtk_list_store_set (store, &iter, text_column, text, -1);
221 }
222
223 /**
224  * gtk_combo_box_text_remove:
225  * @combo_box: A #GtkComboBox
226  * @position: Index of the item to remove
227  *
228  * Removes the string at @position from @combo_box.
229  *
230  * Since: 2.24
231  */
232 void
233 gtk_combo_box_text_remove (GtkComboBoxText *combo_box,
234                            gint             position)
235 {
236   GtkTreeModel *model;
237   GtkListStore *store;
238   GtkTreeIter iter;
239
240   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
241   g_return_if_fail (position >= 0);
242
243   model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
244   store = GTK_LIST_STORE (model);
245   g_return_if_fail (GTK_IS_LIST_STORE (store));
246
247   if (gtk_tree_model_iter_nth_child (model, &iter, NULL, position))
248     gtk_list_store_remove (store, &iter);
249 }
250
251 /**
252  * gtk_combo_box_text_remove_all:
253  * @combo_box: A #GtkComboBoxText
254  *
255  * Removes all the text entries from the combo box.
256  *
257  * Since: 3.0
258  */
259 void
260 gtk_combo_box_text_remove_all (GtkComboBoxText *combo_box)
261 {
262   GtkListStore *store;
263
264   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
265
266   store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
267   gtk_list_store_clear (store);
268 }
269
270 /**
271  * gtk_combo_box_text_get_active_text:
272  * @combo_box: A #GtkComboBoxText
273  *
274  * Returns the currently active string in @combo_box or %NULL if none
275  * is selected.
276  *
277  * Returns: a newly allocated string containing the currently active text.
278  *     Must be freed with g_free().
279  *
280  * Since: 2.24
281  */
282 gchar *
283 gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box)
284 {
285   GtkTreeIter iter;
286   gchar *text = NULL;
287
288   g_return_val_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box), NULL);
289
290   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
291     {
292       GtkTreeModel *model;
293       gint text_column;
294       gint column_type;
295
296       model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
297       g_return_val_if_fail (GTK_IS_LIST_STORE (model), NULL);
298       text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
299       column_type = gtk_tree_model_get_column_type (model, text_column);
300       g_return_val_if_fail (column_type == G_TYPE_STRING, NULL);
301       gtk_tree_model_get (model, &iter, text_column, &text, -1);
302     }
303
304   return text;
305 }