1 /* GtkCellRendererCombo
2 * Copyright (C) 2004 Lorenzo Gil Sanchez
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
24 #include "gtkcelllayout.h"
25 #include "gtkcellrenderercombo.h"
26 #include "gtkcellrenderertext.h"
27 #include "gtkcombobox.h"
28 #include "gtkmarshalers.h"
29 #include "gtkprivate.h"
33 * SECTION:gtkcellrenderercombo
34 * @Short_description: Renders a combobox in a cell
35 * @Title: GtkCellRendererCombo
37 * #GtkCellRendererCombo renders text in a cell like #GtkCellRendererText from
38 * which it is derived. But while #GtkCellRendererText offers a simple entry to
39 * edit the text, #GtkCellRendererCombo offers a #GtkComboBox
40 * widget to edit the text. The values to display in the combo box are taken from
41 * the tree model specified in the #GtkCellRendererCombo:model property.
43 * The combo cell renderer takes care of adding a text cell renderer to the combo
44 * box and sets it to display the column specified by its
45 * #GtkCellRendererCombo:text-column property. Further properties of the combo box
46 * can be set in a handler for the #GtkCellRenderer::editing-started signal.
48 * The #GtkCellRendererCombo cell renderer was added in GTK+ 2.6.
52 struct _GtkCellRendererComboPrivate
66 static void gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass);
67 static void gtk_cell_renderer_combo_init (GtkCellRendererCombo *self);
68 static void gtk_cell_renderer_combo_finalize (GObject *object);
69 static void gtk_cell_renderer_combo_get_property (GObject *object,
74 static void gtk_cell_renderer_combo_set_property (GObject *object,
79 static GtkCellEditable *gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
83 const GdkRectangle *background_area,
84 const GdkRectangle *cell_area,
85 GtkCellRendererState flags);
99 static guint cell_renderer_combo_signals[LAST_SIGNAL] = { 0, };
101 #define GTK_CELL_RENDERER_COMBO_PATH "gtk-cell-renderer-combo-path"
103 G_DEFINE_TYPE (GtkCellRendererCombo, gtk_cell_renderer_combo, GTK_TYPE_CELL_RENDERER_TEXT)
106 gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass)
108 GObjectClass *object_class = G_OBJECT_CLASS (klass);
109 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
111 object_class->finalize = gtk_cell_renderer_combo_finalize;
112 object_class->get_property = gtk_cell_renderer_combo_get_property;
113 object_class->set_property = gtk_cell_renderer_combo_set_property;
115 cell_class->start_editing = gtk_cell_renderer_combo_start_editing;
118 * GtkCellRendererCombo:model:
120 * Holds a tree model containing the possible values for the combo box.
121 * Use the text_column property to specify the column holding the values.
125 g_object_class_install_property (object_class,
127 g_param_spec_object ("model",
129 P_("The model containing the possible values for the combo box"),
131 GTK_PARAM_READWRITE));
134 * GtkCellRendererCombo:text-column:
136 * Specifies the model column which holds the possible values for the
139 * Note that this refers to the model specified in the model property,
140 * <emphasis>not</emphasis> the model backing the tree view to which
141 * this cell renderer is attached.
143 * #GtkCellRendererCombo automatically adds a text cell renderer for
144 * this column to its combo box.
148 g_object_class_install_property (object_class,
150 g_param_spec_int ("text-column",
152 P_("A column in the data source model to get the strings from"),
156 GTK_PARAM_READWRITE));
159 * GtkCellRendererCombo:has-entry:
161 * If %TRUE, the cell renderer will include an entry and allow to enter
162 * values other than the ones in the popup list.
166 g_object_class_install_property (object_class,
168 g_param_spec_boolean ("has-entry",
170 P_("If FALSE, don't allow to enter strings other than the chosen ones"),
172 GTK_PARAM_READWRITE));
176 * GtkCellRendererCombo::changed:
177 * @combo: the object on which the signal is emitted
178 * @path_string: a string of the path identifying the edited cell
179 * (relative to the tree view model)
180 * @new_iter: the new iter selected in the combo box
181 * (relative to the combo box model)
183 * This signal is emitted each time after the user selected an item in
184 * the combo box, either by using the mouse or the arrow keys. Contrary
185 * to GtkComboBox, GtkCellRendererCombo::changed is not emitted for
186 * changes made to a selected item in the entry. The argument @new_iter
187 * corresponds to the newly selected item in the combo box and it is relative
188 * to the GtkTreeModel set via the model property on GtkCellRendererCombo.
190 * Note that as soon as you change the model displayed in the tree view,
191 * the tree view will immediately cease the editing operating. This
192 * means that you most probably want to refrain from changing the model
193 * until the combo cell renderer emits the edited or editing_canceled signal.
197 cell_renderer_combo_signals[CHANGED] =
198 g_signal_new (I_("changed"),
199 G_TYPE_FROM_CLASS (object_class),
203 _gtk_marshal_VOID__STRING_BOXED,
208 g_type_class_add_private (klass, sizeof (GtkCellRendererComboPrivate));
212 gtk_cell_renderer_combo_init (GtkCellRendererCombo *self)
214 GtkCellRendererComboPrivate *priv;
216 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
217 GTK_TYPE_CELL_RENDERER_COMBO,
218 GtkCellRendererComboPrivate);
222 priv->text_column = -1;
223 priv->has_entry = TRUE;
224 priv->focus_out_id = 0;
228 * gtk_cell_renderer_combo_new:
230 * Creates a new #GtkCellRendererCombo.
231 * Adjust how text is drawn using object properties.
232 * Object properties can be set globally (with g_object_set()).
233 * Also, with #GtkTreeViewColumn, you can bind a property to a value
234 * in a #GtkTreeModel. For example, you can bind the "text" property
235 * on the cell renderer to a string value in the model, thus rendering
236 * a different string in each row of the #GtkTreeView.
238 * Returns: the new cell renderer
243 gtk_cell_renderer_combo_new (void)
245 return g_object_new (GTK_TYPE_CELL_RENDERER_COMBO, NULL);
249 gtk_cell_renderer_combo_finalize (GObject *object)
251 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
252 GtkCellRendererComboPrivate *priv = cell->priv;
256 g_object_unref (priv->model);
260 G_OBJECT_CLASS (gtk_cell_renderer_combo_parent_class)->finalize (object);
264 gtk_cell_renderer_combo_get_property (GObject *object,
269 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
270 GtkCellRendererComboPrivate *priv = cell->priv;
275 g_value_set_object (value, priv->model);
277 case PROP_TEXT_COLUMN:
278 g_value_set_int (value, priv->text_column);
281 g_value_set_boolean (value, priv->has_entry);
284 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
290 gtk_cell_renderer_combo_set_property (GObject *object,
295 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
296 GtkCellRendererComboPrivate *priv = cell->priv;
303 g_object_unref (priv->model);
304 priv->model = GTK_TREE_MODEL (g_value_get_object (value));
306 g_object_ref (priv->model);
309 case PROP_TEXT_COLUMN:
310 priv->text_column = g_value_get_int (value);
313 priv->has_entry = g_value_get_boolean (value);
316 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322 gtk_cell_renderer_combo_changed (GtkComboBox *combo,
326 GtkCellRendererCombo *cell;
328 cell = GTK_CELL_RENDERER_COMBO (data);
330 if (gtk_combo_box_get_active_iter (combo, &iter))
334 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
335 g_signal_emit (cell, cell_renderer_combo_signals[CHANGED], 0,
341 gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
345 gchar *new_text = NULL;
348 GtkCellRendererCombo *cell;
351 GtkCellRendererComboPrivate *priv;
353 cell = GTK_CELL_RENDERER_COMBO (data);
356 if (priv->focus_out_id > 0)
358 g_signal_handler_disconnect (combo, priv->focus_out_id);
359 priv->focus_out_id = 0;
363 "editing-canceled", &canceled,
365 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
372 if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (combo)))
374 entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo)));
375 new_text = g_strdup (gtk_entry_get_text (entry));
379 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
382 && gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
383 gtk_tree_model_get (model, &iter, priv->text_column, &new_text, -1);
386 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
387 g_signal_emit_by_name (cell, "edited", path, new_text);
395 gtk_cell_renderer_combo_focus_out_event (GtkWidget *widget,
400 gtk_cell_renderer_combo_editing_done (GTK_CELL_EDITABLE (widget), data);
407 GtkCellRendererCombo *cell;
413 find_text (GtkTreeModel *model,
418 GtkCellRendererComboPrivate *priv;
419 SearchData *search_data = (SearchData *)data;
420 gchar *text, *cell_text;
422 priv = search_data->cell->priv;
424 gtk_tree_model_get (model, iter, priv->text_column, &text, -1);
425 g_object_get (GTK_CELL_RENDERER_TEXT (search_data->cell),
428 if (text && cell_text && g_strcmp0 (text, cell_text) == 0)
430 search_data->iter = *iter;
431 search_data->found = TRUE;
437 return search_data->found;
440 static GtkCellEditable *
441 gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
445 const GdkRectangle *background_area,
446 const GdkRectangle *cell_area,
447 GtkCellRendererState flags)
449 GtkCellRendererCombo *cell_combo;
450 GtkCellRendererText *cell_text;
452 SearchData search_data;
453 GtkCellRendererComboPrivate *priv;
457 cell_text = GTK_CELL_RENDERER_TEXT (cell);
458 g_object_get (cell_text, "editable", &editable, NULL);
459 if (editable == FALSE)
462 cell_combo = GTK_CELL_RENDERER_COMBO (cell);
463 priv = cell_combo->priv;
465 if (priv->text_column < 0)
470 combo = g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
473 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), priv->model);
474 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (combo),
477 g_object_get (cell_text, "text", &text, NULL);
479 gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo))),
485 cell = gtk_cell_renderer_text_new ();
487 combo = gtk_combo_box_new ();
489 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), priv->model);
491 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
492 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo),
493 cell, "text", priv->text_column,
496 /* determine the current value */
499 search_data.cell = cell_combo;
500 search_data.found = FALSE;
501 gtk_tree_model_foreach (priv->model, find_text, &search_data);
502 if (search_data.found)
503 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo),
504 &(search_data.iter));
508 g_object_set (combo, "has-frame", FALSE, NULL);
509 g_object_set_data_full (G_OBJECT (combo),
510 I_(GTK_CELL_RENDERER_COMBO_PATH),
511 g_strdup (path), g_free);
513 gtk_widget_show (combo);
515 g_signal_connect (GTK_CELL_EDITABLE (combo), "editing-done",
516 G_CALLBACK (gtk_cell_renderer_combo_editing_done),
518 g_signal_connect (GTK_CELL_EDITABLE (combo), "changed",
519 G_CALLBACK (gtk_cell_renderer_combo_changed),
521 priv->focus_out_id = g_signal_connect (combo, "focus-out-event",
522 G_CALLBACK (gtk_cell_renderer_combo_focus_out_event),
527 return GTK_CELL_EDITABLE (combo);