]> Pileus Git - ~andy/gtk/blob - gtk/gtkcomboboxtext.c
Bug 619148 - "active ID" properties (GtkComboBox)
[~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 (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);
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                        "id-column", 1,
106                        NULL);
107 }
108
109 /**
110  * gtk_combo_box_text_new_with_entry:
111  *
112  * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
113  * strings. The combo box created by this function has an entry.
114  *
115  * Return value: a new #GtkComboBoxText
116  *
117  * Since: 2.24
118  */
119 GtkWidget *
120 gtk_combo_box_text_new_with_entry (void)
121 {
122   return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
123                        "has-entry", TRUE,
124                        "entry-text-column", 0,
125                        "id-column", 1,
126                        NULL);
127 }
128
129 /**
130  * gtk_combo_box_text_append_text:
131  * @combo_box: A #GtkComboBoxText
132  * @text: A string
133  *
134  * Appends @text to the list of strings stored in @combo_box.
135  *
136  * This is the same as calling gtk_combo_box_text_insert_text() with a
137  * position of -1.
138  *
139  * Since: 2.24
140  */
141 void
142 gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
143                                 const gchar     *text)
144 {
145   gtk_combo_box_text_insert (combo_box, -1, NULL, text);
146 }
147
148 /**
149  * gtk_combo_box_text_prepend_text:
150  * @combo_box: A #GtkComboBox
151  * @text: A string
152  *
153  * Prepends @text to the list of strings stored in @combo_box.
154  *
155  * This is the same as calling gtk_combo_box_text_insert_text() with a
156  * position of 0.
157  *
158  * Since: 2.24
159  */
160 void
161 gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
162                                  const gchar     *text)
163 {
164   gtk_combo_box_text_insert (combo_box, 0, NULL, text);
165 }
166
167 /**
168  * gtk_combo_box_text_insert_text:
169  * @combo_box: A #GtkComboBoxText
170  * @position: An index to insert @text
171  * @text: A string
172  *
173  * Inserts @text at @position in the list of strings stored in @combo_box.
174  *
175  * If @position is negative then @text is appended.
176  *
177  * This is the same as calling gtk_combo_box_text_insert() with a %NULL
178  * ID string.
179  *
180  * Since: 2.24
181  */
182 void
183 gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
184                                 gint             position,
185                                 const gchar     *text)
186 {
187   gtk_combo_box_text_insert (combo_box, position, NULL, text);
188 }
189
190 /**
191  * gtk_combo_box_text_append:
192  * @combo_box: A #GtkComboBoxText
193  * @text: A string
194  *
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.
197  *
198  * This is the same as calling gtk_combo_box_text_insert() with a
199  * position of -1.
200  *
201  * Since: 2.24
202  */
203 void
204 gtk_combo_box_text_append (GtkComboBoxText *combo_box,
205                            const gchar     *id,
206                            const gchar     *text)
207 {
208   gtk_combo_box_text_insert (combo_box, -1, id, text);
209 }
210
211 /**
212  * gtk_combo_box_text_prepend:
213  * @combo_box: A #GtkComboBox
214  * @text: A string
215  *
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.
218  *
219  * This is the same as calling gtk_combo_box_text_insert() with a
220  * position of 0.
221  *
222  * Since: 2.24
223  */
224 void
225 gtk_combo_box_text_prepend (GtkComboBoxText *combo_box,
226                             const gchar     *id,
227                             const gchar     *text)
228 {
229   gtk_combo_box_text_insert (combo_box, 0, id, text);
230 }
231
232
233 /**
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
239  *
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.
243  *
244  * If @position is negative then @text is appended.
245  *
246  * Since: 3.0
247  */
248 void
249 gtk_combo_box_text_insert (GtkComboBoxText *combo_box,
250                            gint             position,
251                            const gchar     *id,
252                            const gchar     *text)
253 {
254   GtkListStore *store;
255   GtkTreeIter iter;
256   gint text_column;
257   gint column_type;
258
259   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
260   g_return_if_fail (text != NULL);
261
262   if (position < 0)
263     position = G_MAXINT;
264
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   column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
269   g_return_if_fail (column_type == G_TYPE_STRING);
270
271   gtk_list_store_insert (store, &iter, position);
272   gtk_list_store_set (store, &iter, text_column, text, -1);
273
274   if (id != NULL)
275     {
276       gint id_column;
277
278       id_column = gtk_combo_box_get_id_column (GTK_COMBO_BOX (combo_box));
279       column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), id_column);
280       g_return_if_fail (column_type == G_TYPE_STRING);
281
282       gtk_list_store_set (store, &iter, id_column, id, -1);
283     }
284 }
285
286 /**
287  * gtk_combo_box_text_remove:
288  * @combo_box: A #GtkComboBox
289  * @position: Index of the item to remove
290  *
291  * Removes the string at @position from @combo_box.
292  *
293  * Since: 2.24
294  */
295 void
296 gtk_combo_box_text_remove (GtkComboBoxText *combo_box,
297                            gint             position)
298 {
299   GtkTreeModel *model;
300   GtkListStore *store;
301   GtkTreeIter iter;
302
303   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
304   g_return_if_fail (position >= 0);
305
306   model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
307   store = GTK_LIST_STORE (model);
308   g_return_if_fail (GTK_IS_LIST_STORE (store));
309
310   if (gtk_tree_model_iter_nth_child (model, &iter, NULL, position))
311     gtk_list_store_remove (store, &iter);
312 }
313
314 /**
315  * gtk_combo_box_text_remove_all:
316  * @combo_box: A #GtkComboBoxText
317  *
318  * Removes all the text entries from the combo box.
319  *
320  * Since: 3.0
321  */
322 void
323 gtk_combo_box_text_remove_all (GtkComboBoxText *combo_box)
324 {
325   GtkListStore *store;
326
327   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
328
329   store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
330   gtk_list_store_clear (store);
331 }
332
333 /**
334  * gtk_combo_box_text_get_active_text:
335  * @combo_box: A #GtkComboBoxText
336  *
337  * Returns the currently active string in @combo_box or %NULL if none
338  * is selected.
339  *
340  * Returns: a newly allocated string containing the currently active text.
341  *     Must be freed with g_free().
342  *
343  * Since: 2.24
344  */
345 gchar *
346 gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box)
347 {
348   GtkTreeIter iter;
349   gchar *text = NULL;
350
351   g_return_val_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box), NULL);
352
353   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
354     {
355       GtkTreeModel *model;
356       gint text_column;
357       gint column_type;
358
359       model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
360       g_return_val_if_fail (GTK_IS_LIST_STORE (model), NULL);
361       text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
362       column_type = gtk_tree_model_get_column_type (model, text_column);
363       g_return_val_if_fail (column_type == G_TYPE_STRING, NULL);
364       gtk_tree_model_get (model, &iter, text_column, &text, -1);
365     }
366
367   return text;
368 }