]> Pileus Git - ~andy/gtk/blob - gtk/gtkcomboboxtext.c
Fix some GtkComboBoxText problems
[~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 void
47 gtk_combo_box_text_class_init (GtkComboBoxTextClass *klass)
48 {
49 }
50
51 static void
52 gtk_combo_box_text_init (GtkComboBoxText *combo_box)
53 {
54   GtkListStore *store;
55   GtkCellRenderer *cell;
56
57   store = gtk_list_store_new (1, G_TYPE_STRING);
58   gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (store));
59   g_object_unref (store);
60
61   cell = gtk_cell_renderer_text_new ();
62   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), cell, TRUE);
63   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), cell,
64                                   "text", 0,
65                                   NULL);
66 }
67
68 /**
69  * gtk_combo_box_text_new:
70  *
71  * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
72  * strings. See gtk_combo_box_entry_new_with_text().
73  *
74  * Return value: A new #GtkComboBoxText
75  *
76  * Since: 2.24
77  */
78 GtkWidget *
79 gtk_combo_box_text_new (void)
80 {
81   return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
82                        "entry-text-column", 0,
83                        NULL);
84 }
85
86
87 /**
88  * gtk_combo_box_text_new_with_entry:
89  *
90  * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
91  * strings. The combo box created by this function has an entry.
92  *
93  * Return value: a new #GtkComboBoxText
94  *
95  * Since: 2.24
96  */
97 GtkWidget *
98 gtk_combo_box_text_new_with_entry (void)
99 {
100   return g_object_new (GTK_TYPE_COMBO_BOX_TEXT,
101                        "has-entry", TRUE,
102                        "entry-text-column", 0,
103                        NULL);
104 }
105
106 /**
107  * gtk_combo_box_text_append_text:
108  * @combo_box: A #GtkComboBoxText
109  * @text: A string
110  *
111  * Appends @string to the list of strings stored in @combo_box.
112  *
113  * Since: 2.24
114  */
115 void
116 gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
117                                 const gchar     *text)
118 {
119   GtkListStore *store;
120   GtkTreeIter iter;
121   gint text_column;
122   gint column_type;
123
124   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
125   g_return_if_fail (text != NULL);
126
127   store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
128   g_return_if_fail (GTK_IS_LIST_STORE (store));
129   text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
130   column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
131   g_return_if_fail (column_type == G_TYPE_STRING);
132
133   gtk_list_store_append (store, &iter);
134   gtk_list_store_set (store, &iter, text_column, text, -1);
135 }
136
137 /**
138  * gtk_combo_box_text_insert_text:
139  * @combo_box: A #GtkComboBoxText
140  * @position: An index to insert @text
141  * @text: A string
142  *
143  * Inserts @string at @position in the list of strings stored in @combo_box.
144  *
145  * Since: 2.24
146  */
147 void
148 gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
149                                 gint             position,
150                                 const gchar     *text)
151 {
152   GtkListStore *store;
153   GtkTreeIter iter;
154   gint text_column;
155   gint column_type;
156
157   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
158   g_return_if_fail (position >= 0);
159   g_return_if_fail (text != NULL);
160
161   store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
162   g_return_if_fail (GTK_IS_LIST_STORE (store));
163   text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
164   column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
165   g_return_if_fail (column_type == G_TYPE_STRING);
166
167   gtk_list_store_insert (store, &iter, position);
168   gtk_list_store_set (store, &iter, text_column, text, -1);
169 }
170
171 /**
172  * gtk_combo_box_text_prepend_text:
173  * @combo_box: A #GtkComboBox
174  * @text: A string
175  *
176  * Prepends @string to the list of strings stored in @combo_box.
177  *
178  * Since: 2.24
179  */
180 void
181 gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
182                                  const gchar     *text)
183 {
184   GtkListStore *store;
185   GtkTreeIter iter;
186   gint text_column;
187   gint column_type;
188
189   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
190   g_return_if_fail (text != NULL);
191
192   store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box)));
193   g_return_if_fail (GTK_IS_LIST_STORE (store));
194   text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
195   column_type = gtk_tree_model_get_column_type (GTK_TREE_MODEL (store), text_column);
196   g_return_if_fail (column_type == G_TYPE_STRING);
197
198   gtk_list_store_prepend (store, &iter);
199   gtk_list_store_set (store, &iter, text_column, text, -1);
200 }
201
202 /**
203  * gtk_combo_box_text_remove:
204  * @combo_box: A #GtkComboBox
205  * @position: Index of the item to remove
206  *
207  * Removes the string at @position from @combo_box.
208  *
209  * Since: 2.24
210  */
211 void
212 gtk_combo_box_text_remove (GtkComboBoxText *combo_box,
213                            gint             position)
214 {
215   GtkTreeModel *model;
216   GtkListStore *store;
217   GtkTreeIter iter;
218
219   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
220   g_return_if_fail (position >= 0);
221
222   model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
223   store = GTK_LIST_STORE (model);
224   g_return_if_fail (GTK_IS_LIST_STORE (store));
225
226   if (gtk_tree_model_iter_nth_child (model, &iter, NULL, position))
227     gtk_list_store_remove (store, &iter);
228 }
229
230 /**
231  * gtk_combo_box_text_get_active_text:
232  * @combo_box: A #GtkComboBoxText
233  *
234  * Returns the currently active string in @combo_box or %NULL if none
235  * is selected.
236  *
237  * Returns: a newly allocated string containing the currently active text.
238  *     Must be freed with g_free().
239  *
240  * Since: 2.24
241  */
242 gchar *
243 gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box)
244 {
245   GtkTreeIter iter;
246   gchar *text = NULL;
247
248   g_return_val_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box), NULL);
249
250   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
251     {
252       GtkTreeModel *model;
253       gint text_column;
254       gint column_type;
255
256       model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo_box));
257       g_return_val_if_fail (GTK_IS_LIST_STORE (model), NULL);
258       text_column = gtk_combo_box_get_entry_text_column (GTK_COMBO_BOX (combo_box));
259       column_type = gtk_tree_model_get_column_type (model, text_column);
260       g_return_val_if_fail (column_type == G_TYPE_STRING, NULL);
261       gtk_tree_model_get (model, &iter, text_column, &text, -1);
262     }
263
264   return text;
265 }