]> Pileus Git - ~andy/gtk/blob - gtk/gtkcomboboxtext.c
Added GtkComboBoxText api
[~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 G_DEFINE_TYPE (GtkComboBoxText, gtk_combo_box_text, GTK_TYPE_COMBO_BOX);
27
28 static void
29 gtk_combo_box_text_class_init (GtkComboBoxTextClass *klass)
30 {
31 }
32
33 static void
34 gtk_combo_box_text_init (GtkComboBoxText *combo_box)
35 {
36   GtkListStore *store;
37   GtkCellRenderer *cell;
38
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);
42
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,
46                                   "text", 0,
47                                   NULL);
48 }
49
50 /**
51  * gtk_combo_box_text_new:
52  *
53  * Creates a new #GtkComboBoxText, which is a #GtkComboBox just displaying
54  * strings. See gtk_combo_box_entry_new_with_text().
55  *
56  * Return value: A new #GtkComboBoxText
57  *
58  * Since: 2.24
59  */
60 GtkWidget *
61 gtk_combo_box_text_new (void)
62 {
63   return g_object_new (GTK_TYPE_COMBO_BOX_TEXT, NULL);
64 }
65
66 /**
67  * gtk_combo_box_text_append_text:
68  * @combo_box: A #GtkComboBoxText
69  * @text: A string
70  *
71  * Appends @string to the list of strings stored in @combo_box.
72  *
73  * Since: 2.24
74  */
75 void
76 gtk_combo_box_text_append_text (GtkComboBoxText *combo_box,
77                                 const gchar     *text)
78 {
79   GtkListStore *store;
80   GtkTreeIter iter;
81   gint column_type;
82
83   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
84   g_return_if_fail (text != NULL);
85
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);
90
91   gtk_list_store_append (store, &iter);
92   gtk_list_store_set (store, &iter, 0, text, -1);
93 }
94
95 /**
96  * gtk_combo_box_text_insert_text:
97  * @combo_box: A #GtkComboBoxText
98  * @position: An index to insert @text
99  * @text: A string
100  *
101  * Inserts @string at @position in the list of strings stored in @combo_box.
102  *
103  * Since: 2.24
104  */
105 void
106 gtk_combo_box_text_insert_text (GtkComboBoxText *combo_box,
107                                 gint             position,
108                                 const gchar     *text)
109 {
110   GtkListStore *store;
111   GtkTreeIter iter;
112   gint column_type;
113
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);
117
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);
122
123   gtk_list_store_insert (store, &iter, position);
124   gtk_list_store_set (store, &iter, 0, text, -1);
125 }
126
127 /**
128  * gtk_combo_box_text_prepend_text:
129  * @combo_box: A #GtkComboBox
130  * @text: A string
131  *
132  * Prepends @string to the list of strings stored in @combo_box.
133  *
134  * Since: 2.24
135  */
136 void
137 gtk_combo_box_text_prepend_text (GtkComboBoxText *combo_box,
138                                  const gchar     *text)
139 {
140   GtkListStore *store;
141   GtkTreeIter iter;
142   gint column_type;
143
144   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
145   g_return_if_fail (text != NULL);
146
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);
151
152   gtk_list_store_prepend (store, &iter);
153   gtk_list_store_set (store, &iter, 0, text, -1);
154 }
155
156 /**
157  * gtk_combo_box_text_remove:
158  * @combo_box: A #GtkComboBox
159  * @position: Index of the item to remove
160  *
161  * Removes the string at @position from @combo_box.
162  *
163  * Since: 2.24
164  */
165 void
166 gtk_combo_box_text_remove (GtkComboBoxText *combo_box,
167                            gint             position)
168 {
169   GtkTreeModel *model;
170   GtkListStore *store;
171   GtkTreeIter iter;
172   gint column_type;
173
174   g_return_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box));
175   g_return_if_fail (position >= 0);
176
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);
182
183   if (gtk_tree_model_iter_nth_child (model, &iter, NULL, position))
184     gtk_list_store_remove (store, &iter);
185 }
186
187 /**
188  * gtk_combo_box_text_get_active_text:
189  * @combo_box: A #GtkComboBoxText
190  *
191  * Returns the currently active string in @combo_box or %NULL if none
192  * is selected.
193  *
194  * Returns: a newly allocated string containing the currently active text.
195  *     Must be freed with g_free().
196  *
197  * Since: 2.24
198  */
199 gchar *
200 gtk_combo_box_text_get_active_text (GtkComboBoxText *combo_box)
201 {
202   GtkTreeIter iter;
203   gchar *text = NULL;
204
205   g_return_val_if_fail (GTK_IS_COMBO_BOX_TEXT (combo_box), NULL);
206
207   if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo_box), &iter))
208     {
209       GtkTreeModel *model;
210       gint column_type;
211
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);
217     }
218
219   return text;
220 }