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.
22 #include "gtkcomboboxentry.h"
23 #include "gtkcelllayout.h"
26 #include "gtkcellrenderertext.h"
28 #include "gtkprivate.h"
30 #include "gtkbuildable.h"
33 #define GTK_COMBO_BOX_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX_ENTRY, GtkComboBoxEntryPrivate))
35 struct _GtkComboBoxEntryPrivate
37 GtkCellRenderer *text_renderer;
41 static void gtk_combo_box_entry_set_property (GObject *object,
45 static void gtk_combo_box_entry_get_property (GObject *object,
49 static void gtk_combo_box_entry_add (GtkContainer *container,
51 static void gtk_combo_box_entry_remove (GtkContainer *container,
54 static gchar *gtk_combo_box_entry_get_active_text (GtkComboBox *combo_box);
55 static void gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
57 static void gtk_combo_box_entry_contents_changed (GtkEntry *entry,
59 static gboolean gtk_combo_box_entry_mnemonic_activate (GtkWidget *entry,
60 gboolean group_cycling);
61 static void gtk_combo_box_entry_grab_focus (GtkWidget *widget);
62 static void has_frame_changed (GtkComboBoxEntry *entry_box,
65 static void gtk_combo_box_entry_buildable_interface_init (GtkBuildableIface *iface);
66 static GObject * gtk_combo_box_entry_buildable_get_internal_child (GtkBuildable *buildable,
68 const gchar *childname);
76 G_DEFINE_TYPE_WITH_CODE (GtkComboBoxEntry, gtk_combo_box_entry, GTK_TYPE_COMBO_BOX,
77 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
78 gtk_combo_box_entry_buildable_interface_init))
81 gtk_combo_box_entry_class_init (GtkComboBoxEntryClass *klass)
83 GObjectClass *object_class;
84 GtkWidgetClass *widget_class;
85 GtkContainerClass *container_class;
86 GtkComboBoxClass *combo_class;
88 object_class = (GObjectClass *)klass;
89 object_class->set_property = gtk_combo_box_entry_set_property;
90 object_class->get_property = gtk_combo_box_entry_get_property;
92 widget_class = (GtkWidgetClass *)klass;
93 widget_class->mnemonic_activate = gtk_combo_box_entry_mnemonic_activate;
94 widget_class->grab_focus = gtk_combo_box_entry_grab_focus;
96 container_class = (GtkContainerClass *)klass;
97 container_class->add = gtk_combo_box_entry_add;
98 container_class->remove = gtk_combo_box_entry_remove;
100 combo_class = (GtkComboBoxClass *)klass;
101 combo_class->get_active_text = gtk_combo_box_entry_get_active_text;
103 g_object_class_install_property (object_class,
105 g_param_spec_int ("text-column",
107 P_("A column in the data source model to get the strings from"),
111 GTK_PARAM_READWRITE));
113 g_type_class_add_private ((GObjectClass *) klass,
114 sizeof (GtkComboBoxEntryPrivate));
118 gtk_combo_box_entry_init (GtkComboBoxEntry *entry_box)
122 entry_box->priv = GTK_COMBO_BOX_ENTRY_GET_PRIVATE (entry_box);
123 entry_box->priv->text_column = -1;
125 entry = gtk_entry_new ();
126 gtk_widget_show (entry);
127 gtk_container_add (GTK_CONTAINER (entry_box), entry);
129 entry_box->priv->text_renderer = gtk_cell_renderer_text_new ();
130 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (entry_box),
131 entry_box->priv->text_renderer, TRUE);
133 gtk_combo_box_set_active (GTK_COMBO_BOX (entry_box), -1);
135 g_signal_connect (entry_box, "changed",
136 G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
137 g_signal_connect (entry_box, "notify::has-frame", G_CALLBACK (has_frame_changed), NULL);
141 gtk_combo_box_entry_buildable_interface_init (GtkBuildableIface *iface)
143 iface->get_internal_child = gtk_combo_box_entry_buildable_get_internal_child;
147 gtk_combo_box_entry_buildable_get_internal_child (GtkBuildable *buildable,
149 const gchar *childname)
151 if (strcmp (childname, "entry") == 0)
152 return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
158 gtk_combo_box_entry_set_property (GObject *object,
163 GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (object);
167 case PROP_TEXT_COLUMN:
168 gtk_combo_box_entry_set_text_column (entry_box,
169 g_value_get_int (value));
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 gtk_combo_box_entry_get_property (GObject *object,
184 GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (object);
188 case PROP_TEXT_COLUMN:
189 g_value_set_int (value, entry_box->priv->text_column);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 gtk_combo_box_entry_add (GtkContainer *container,
202 GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (container);
204 if (!GTK_IS_ENTRY (child))
206 g_warning ("Attempting to add a widget with type %s to a GtkComboBoxEntry "
207 "(need an instance of GtkEntry or of a subclass)",
208 G_OBJECT_TYPE_NAME (child));
212 GTK_CONTAINER_CLASS (gtk_combo_box_entry_parent_class)->add (container, child);
214 /* this flag is a hack to tell the entry to fill its allocation.
216 GTK_ENTRY (child)->is_cell_renderer = TRUE;
218 g_signal_connect (child, "changed",
219 G_CALLBACK (gtk_combo_box_entry_contents_changed),
221 has_frame_changed (entry_box, NULL, NULL);
225 gtk_combo_box_entry_remove (GtkContainer *container,
228 if (child && child == GTK_BIN (container)->child)
230 g_signal_handlers_disconnect_by_func (child,
231 gtk_combo_box_entry_contents_changed,
233 GTK_ENTRY (child)->is_cell_renderer = FALSE;
236 GTK_CONTAINER_CLASS (gtk_combo_box_entry_parent_class)->remove (container, child);
240 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
243 GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (combo_box);
248 if (gtk_combo_box_get_active_iter (combo_box, &iter))
250 GtkEntry *entry = GTK_ENTRY (GTK_BIN (combo_box)->child);
254 g_signal_handlers_block_by_func (entry,
255 gtk_combo_box_entry_contents_changed,
258 model = gtk_combo_box_get_model (combo_box);
260 gtk_tree_model_get (model, &iter,
261 entry_box->priv->text_column, &str,
263 gtk_entry_set_text (entry, str);
266 g_signal_handlers_unblock_by_func (entry,
267 gtk_combo_box_entry_contents_changed,
274 has_frame_changed (GtkComboBoxEntry *entry_box,
278 if (GTK_BIN (entry_box)->child)
282 g_object_get (entry_box, "has-frame", &has_frame, NULL);
284 gtk_entry_set_has_frame (GTK_ENTRY (GTK_BIN (entry_box)->child), has_frame);
289 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
292 GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
295 * Fixes regression reported in bug #574059. The old functionality relied on
296 * bug #572478. As a bugfix, we now emit the "changed" signal ourselves
297 * when the selection was already set to -1.
299 if (gtk_combo_box_get_active(combo_box) == -1)
300 g_signal_emit_by_name (combo_box, "changed");
302 gtk_combo_box_set_active (combo_box, -1);
308 * gtk_combo_box_entry_new:
310 * Creates a new #GtkComboBoxEntry which has a #GtkEntry as child. After
311 * construction, you should set a model using gtk_combo_box_set_model() and a
312 * text column using gtk_combo_box_entry_set_text_column().
314 * Return value: A new #GtkComboBoxEntry.
319 gtk_combo_box_entry_new (void)
321 return g_object_new (gtk_combo_box_entry_get_type (), NULL);
325 * gtk_combo_box_entry_new_with_model:
326 * @model: A #GtkTreeModel.
327 * @text_column: A column in @model to get the strings from.
329 * Creates a new #GtkComboBoxEntry which has a #GtkEntry as child and a list
330 * of strings as popup. You can get the #GtkEntry from a #GtkComboBoxEntry
331 * using GTK_ENTRY (GTK_BIN (combo_box_entry)->child). To add and remove
332 * strings from the list, just modify @model using its data manipulation
335 * Return value: A new #GtkComboBoxEntry.
340 gtk_combo_box_entry_new_with_model (GtkTreeModel *model,
345 g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
346 g_return_val_if_fail (text_column >= 0, NULL);
347 g_return_val_if_fail (text_column < gtk_tree_model_get_n_columns (model), NULL);
349 ret = g_object_new (gtk_combo_box_entry_get_type (),
351 "text-column", text_column,
358 * gtk_combo_box_entry_set_text_column:
359 * @entry_box: A #GtkComboBoxEntry.
360 * @text_column: A column in @model to get the strings from.
362 * Sets the model column which @entry_box should use to get strings from
363 * to be @text_column.
368 gtk_combo_box_entry_set_text_column (GtkComboBoxEntry *entry_box,
371 GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (entry_box));
373 g_return_if_fail (text_column >= 0);
374 g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
376 entry_box->priv->text_column = text_column;
378 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (entry_box),
379 entry_box->priv->text_renderer,
385 * gtk_combo_box_entry_get_text_column:
386 * @entry_box: A #GtkComboBoxEntry.
388 * Returns the column which @entry_box is using to get the strings from.
390 * Return value: A column in the data source model of @entry_box.
395 gtk_combo_box_entry_get_text_column (GtkComboBoxEntry *entry_box)
397 g_return_val_if_fail (GTK_IS_COMBO_BOX_ENTRY (entry_box), 0);
399 return entry_box->priv->text_column;
403 gtk_combo_box_entry_mnemonic_activate (GtkWidget *widget,
404 gboolean group_cycling)
406 GtkBin *entry_box = GTK_BIN (widget);
408 if (entry_box->child)
409 gtk_widget_grab_focus (entry_box->child);
415 gtk_combo_box_entry_grab_focus (GtkWidget *widget)
417 GtkBin *entry_box = GTK_BIN (widget);
419 if (entry_box->child)
420 gtk_widget_grab_focus (entry_box->child);
425 /* convenience API for simple text combos */
428 * gtk_combo_box_entry_new_text:
430 * Convenience function which constructs a new editable text combo box, which
431 * is a #GtkComboBoxEntry just displaying strings. If you use this function to
432 * create a text combo box, you should only manipulate its data source with
433 * the following convenience functions: gtk_combo_box_append_text(),
434 * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
435 * gtk_combo_box_remove_text().
437 * Return value: A new text #GtkComboBoxEntry.
442 gtk_combo_box_entry_new_text (void)
444 GtkWidget *entry_box;
447 store = gtk_list_store_new (1, G_TYPE_STRING);
448 entry_box = gtk_combo_box_entry_new_with_model (GTK_TREE_MODEL (store), 0);
449 g_object_unref (store);
455 gtk_combo_box_entry_get_active_text (GtkComboBox *combo_box)
457 GtkBin *combo = GTK_BIN (combo_box);
460 return g_strdup (gtk_entry_get_text (GTK_ENTRY (combo->child)));
465 #define __GTK_COMBO_BOX_ENTRY_C__
466 #include "gtkaliasdef.c"