2 * Copyright (C) 2002, 2003 Kristian Rietveld <kris@gtk.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "gtkcomboboxentry.h"
21 #include "gtkcelllayout.h"
24 #include "gtkcellrenderertext.h"
28 #define GTK_COMBO_BOX_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX_ENTRY, GtkComboBoxEntryPrivate))
30 struct _GtkComboBoxEntryPrivate
34 GtkCellRenderer *text_renderer;
38 static void gtk_combo_box_entry_class_init (GtkComboBoxEntryClass *klass);
39 static void gtk_combo_box_entry_init (GtkComboBoxEntry *entry_box);
41 static void gtk_combo_box_entry_set_property (GObject *object,
45 static void gtk_combo_box_entry_get_property (GObject *object,
50 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
52 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
64 gtk_combo_box_entry_get_type (void)
66 static GType combo_box_entry_type = 0;
68 if (!combo_box_entry_type)
70 static const GTypeInfo combo_box_entry_info =
72 sizeof (GtkComboBoxEntryClass),
74 NULL, /* base_finalize */
75 (GClassInitFunc) gtk_combo_box_entry_class_init,
76 NULL, /* class_finalize */
77 NULL, /* class_data */
78 sizeof (GtkComboBoxEntry),
80 (GInstanceInitFunc) gtk_combo_box_entry_init
83 combo_box_entry_type = g_type_register_static (GTK_TYPE_COMBO_BOX,
85 &combo_box_entry_info,
89 return combo_box_entry_type;
93 gtk_combo_box_entry_class_init (GtkComboBoxEntryClass *klass)
95 GObjectClass *object_class;
97 object_class = (GObjectClass *)klass;
98 object_class->set_property = gtk_combo_box_entry_set_property;
99 object_class->get_property = gtk_combo_box_entry_get_property;
101 g_object_class_install_property (object_class,
103 g_param_spec_int ("text_column",
105 _("A column in the data source model to get the strings from"),
111 g_type_class_add_private ((GObjectClass *) klass,
112 sizeof (GtkComboBoxEntryPrivate));
116 gtk_combo_box_entry_init (GtkComboBoxEntry *entry_box)
118 entry_box->priv = GTK_COMBO_BOX_ENTRY_GET_PRIVATE (entry_box);
119 entry_box->priv->text_column = -1;
121 entry_box->priv->entry = gtk_entry_new ();
122 gtk_container_add (GTK_CONTAINER (entry_box), entry_box->priv->entry);
123 gtk_widget_show (entry_box->priv->entry);
125 entry_box->priv->text_renderer = gtk_cell_renderer_text_new ();
126 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (entry_box),
127 entry_box->priv->text_renderer, TRUE);
129 gtk_combo_box_set_active (GTK_COMBO_BOX (entry_box), -1);
131 g_signal_connect (entry_box->priv->entry, "changed",
132 G_CALLBACK (gtk_combo_box_entry_contents_changed),
134 g_signal_connect (entry_box, "changed",
135 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
139 gtk_combo_box_entry_set_property (GObject *object,
144 GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (object);
148 case PROP_TEXT_COLUMN:
149 gtk_combo_box_entry_set_text_column (entry_box,
150 g_value_get_int (value));
159 gtk_combo_box_entry_get_property (GObject *object,
164 GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (object);
168 case PROP_TEXT_COLUMN:
169 g_value_set_int (value, entry_box->priv->text_column);
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
183 GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (combo_box);
185 index = gtk_combo_box_get_active (combo_box);
187 g_signal_handlers_block_by_func (entry_box->priv->entry,
188 gtk_combo_box_entry_contents_changed,
197 model = gtk_combo_box_get_model (combo_box);
199 gtk_tree_model_iter_nth_child (model, &iter, NULL, index);
200 gtk_tree_model_get (model, &iter,
201 entry_box->priv->text_column, &str,
204 gtk_entry_set_text (GTK_ENTRY (entry_box->priv->entry), str);
209 g_signal_handlers_unblock_by_func (entry_box->priv->entry,
210 gtk_combo_box_entry_contents_changed,
215 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
218 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
220 g_signal_handlers_block_by_func (combo_box,
221 gtk_combo_box_entry_active_changed,
223 gtk_combo_box_set_active (combo_box, -1);
224 g_signal_handlers_unblock_by_func (combo_box,
225 gtk_combo_box_entry_active_changed,
232 * gtk_combo_box_entry_new:
234 * Creates a new #GtkComboBoxEntry which has a #GtkEntry as child. After
235 * construction, you should set a model using gtk_combo_box_set_model() and a
236 * text_column * using gtk_combo_box_entry_set_text_column().
238 * Return value: A new #GtkComboBoxEntry.
243 gtk_combo_box_entry_new (void)
245 return GTK_WIDGET (g_object_new (gtk_combo_box_entry_get_type (), NULL));
249 * gtk_combo_box_entry_new_with_model:
250 * @model: A #GtkTreeModel.
251 * @text_column: A column in @model to get the strings from.
253 * Creates a new #GtkComboBoxEntry which has a #GtkEntry as child and a list
254 * of strings as popup. You can get the #GtkEntry from a #GtkComboBoxEntry
255 * using GTK_ENTRY (GTK_BIN (combo_box_entry)->child). To add and remove
256 * strings from the list, just modify @model using its data manipulation
259 * Return value: A new #GtkComboBoxEntry.
264 gtk_combo_box_entry_new_with_model (GtkTreeModel *model,
269 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
270 g_return_val_if_fail (text_column >= 0, NULL);
271 g_return_val_if_fail (text_column < gtk_tree_model_get_n_columns (model), NULL);
273 ret = g_object_new (gtk_combo_box_entry_get_type (),
275 "text_column", text_column,
282 * gtk_combo_box_entry_set_text_column:
283 * @entry_box: A #GtkComboBoxEntry.
284 * @text_column: A column in @model to get the strings from.
286 * Sets the model column which @entry_box should use to get strings from
287 * to be @text_column.
292 gtk_combo_box_entry_set_text_column (GtkComboBoxEntry *entry_box,
295 g_return_if_fail (text_column >= 0);
296 g_return_if_fail (text_column < gtk_tree_model_get_n_columns (gtk_combo_box_get_model (GTK_COMBO_BOX (entry_box))));
297 g_return_if_fail (entry_box->priv->text_column == -1);
299 entry_box->priv->text_column = text_column;
301 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (entry_box),
302 entry_box->priv->text_renderer,
308 * gtk_combo_box_entry_get_text_column:
309 * @entry_box: A #GtkComboBoxEntry.
311 * Returns the column which @entry_box is using to get the strings from.
313 * Return value: A column in the data source model of @entry_box.
318 gtk_combo_box_entry_get_text_column (GtkComboBoxEntry *entry_box)
320 g_return_val_if_fail (GTK_IS_COMBO_BOX_ENTRY (entry_box), 0);
322 return entry_box->priv->text_column;