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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include "gtkcelllayout.h"
27 #include "gtkcellrenderercombo.h"
28 #include "gtkcellrenderertext.h"
29 #include "gtkcombobox.h"
30 #include "gtkmarshalers.h"
31 #include "gtkprivate.h"
35 * SECTION:gtkcellrenderercombo
36 * @Short_description: Renders a combobox in a cell
37 * @Title: GtkCellRendererCombo
39 * #GtkCellRendererCombo renders text in a cell like #GtkCellRendererText from
40 * which it is derived. But while #GtkCellRendererText offers a simple entry to
41 * edit the text, #GtkCellRendererCombo offers a #GtkComboBox or #GtkComboBoxEntry
42 * widget to edit the text. The values to display in the combo box are taken from
43 * the tree model specified in the #GtkCellRendererCombo:model property.
45 * The combo cell renderer takes care of adding a text cell renderer to the combo
46 * box and sets it to display the column specified by its
47 * #GtkCellRendererCombo:text-column property. Further properties of the comnbo box
48 * can be set in a handler for the #GtkCellRenderer::editing-started signal.
50 * The #GtkCellRendererCombo cell renderer was added in GTK+ 2.6.
54 struct _GtkCellRendererComboPrivate
68 static void gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass);
69 static void gtk_cell_renderer_combo_init (GtkCellRendererCombo *self);
70 static void gtk_cell_renderer_combo_finalize (GObject *object);
71 static void gtk_cell_renderer_combo_get_property (GObject *object,
76 static void gtk_cell_renderer_combo_set_property (GObject *object,
81 static GtkCellEditable *gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
85 const GdkRectangle *background_area,
86 const GdkRectangle *cell_area,
87 GtkCellRendererState flags);
101 static guint cell_renderer_combo_signals[LAST_SIGNAL] = { 0, };
103 #define GTK_CELL_RENDERER_COMBO_PATH "gtk-cell-renderer-combo-path"
105 G_DEFINE_TYPE (GtkCellRendererCombo, gtk_cell_renderer_combo, GTK_TYPE_CELL_RENDERER_TEXT)
108 gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass)
110 GObjectClass *object_class = G_OBJECT_CLASS (klass);
111 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
113 object_class->finalize = gtk_cell_renderer_combo_finalize;
114 object_class->get_property = gtk_cell_renderer_combo_get_property;
115 object_class->set_property = gtk_cell_renderer_combo_set_property;
117 cell_class->start_editing = gtk_cell_renderer_combo_start_editing;
120 * GtkCellRendererCombo:model:
122 * Holds a tree model containing the possible values for the combo box.
123 * Use the text_column property to specify the column holding the values.
127 g_object_class_install_property (object_class,
129 g_param_spec_object ("model",
131 P_("The model containing the possible values for the combo box"),
133 GTK_PARAM_READWRITE));
136 * GtkCellRendererCombo:text-column:
138 * Specifies the model column which holds the possible values for the
141 * Note that this refers to the model specified in the model property,
142 * <emphasis>not</emphasis> the model backing the tree view to which
143 * this cell renderer is attached.
145 * #GtkCellRendererCombo automatically adds a text cell renderer for
146 * this column to its combo box.
150 g_object_class_install_property (object_class,
152 g_param_spec_int ("text-column",
154 P_("A column in the data source model to get the strings from"),
158 GTK_PARAM_READWRITE));
161 * GtkCellRendererCombo:has-entry:
163 * If %TRUE, the cell renderer will include an entry and allow to enter
164 * values other than the ones in the popup list.
168 g_object_class_install_property (object_class,
170 g_param_spec_boolean ("has-entry",
172 P_("If FALSE, don't allow to enter strings other than the chosen ones"),
174 GTK_PARAM_READWRITE));
178 * GtkCellRendererCombo::changed:
179 * @combo: the object on which the signal is emitted
180 * @path_string: a string of the path identifying the edited cell
181 * (relative to the tree view model)
182 * @new_iter: the new iter selected in the combo box
183 * (relative to the combo box model)
185 * This signal is emitted each time after the user selected an item in
186 * the combo box, either by using the mouse or the arrow keys. Contrary
187 * to GtkComboBox, GtkCellRendererCombo::changed is not emitted for
188 * changes made to a selected item in the entry. The argument @new_iter
189 * corresponds to the newly selected item in the combo box and it is relative
190 * to the GtkTreeModel set via the model property on GtkCellRendererCombo.
192 * Note that as soon as you change the model displayed in the tree view,
193 * the tree view will immediately cease the editing operating. This
194 * means that you most probably want to refrain from changing the model
195 * until the combo cell renderer emits the edited or editing_canceled signal.
199 cell_renderer_combo_signals[CHANGED] =
200 g_signal_new (I_("changed"),
201 G_TYPE_FROM_CLASS (object_class),
205 _gtk_marshal_VOID__STRING_BOXED,
210 g_type_class_add_private (klass, sizeof (GtkCellRendererComboPrivate));
214 gtk_cell_renderer_combo_init (GtkCellRendererCombo *self)
216 GtkCellRendererComboPrivate *priv;
218 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
219 GTK_TYPE_CELL_RENDERER_COMBO,
220 GtkCellRendererComboPrivate);
224 priv->text_column = -1;
225 priv->has_entry = TRUE;
226 priv->focus_out_id = 0;
230 * gtk_cell_renderer_combo_new:
232 * Creates a new #GtkCellRendererCombo.
233 * Adjust how text is drawn using object properties.
234 * Object properties can be set globally (with g_object_set()).
235 * Also, with #GtkTreeViewColumn, you can bind a property to a value
236 * in a #GtkTreeModel. For example, you can bind the "text" property
237 * on the cell renderer to a string value in the model, thus rendering
238 * a different string in each row of the #GtkTreeView.
240 * Returns: the new cell renderer
245 gtk_cell_renderer_combo_new (void)
247 return g_object_new (GTK_TYPE_CELL_RENDERER_COMBO, NULL);
251 gtk_cell_renderer_combo_finalize (GObject *object)
253 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
254 GtkCellRendererComboPrivate *priv = cell->priv;
258 g_object_unref (priv->model);
262 G_OBJECT_CLASS (gtk_cell_renderer_combo_parent_class)->finalize (object);
266 gtk_cell_renderer_combo_get_property (GObject *object,
271 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
272 GtkCellRendererComboPrivate *priv = cell->priv;
277 g_value_set_object (value, priv->model);
279 case PROP_TEXT_COLUMN:
280 g_value_set_int (value, priv->text_column);
283 g_value_set_boolean (value, priv->has_entry);
286 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
292 gtk_cell_renderer_combo_set_property (GObject *object,
297 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
298 GtkCellRendererComboPrivate *priv = cell->priv;
305 g_object_unref (priv->model);
306 priv->model = GTK_TREE_MODEL (g_value_get_object (value));
308 g_object_ref (priv->model);
311 case PROP_TEXT_COLUMN:
312 priv->text_column = g_value_get_int (value);
315 priv->has_entry = g_value_get_boolean (value);
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
324 gtk_cell_renderer_combo_changed (GtkComboBox *combo,
328 GtkCellRendererCombo *cell;
330 cell = GTK_CELL_RENDERER_COMBO (data);
332 if (gtk_combo_box_get_active_iter (combo, &iter))
336 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
337 g_signal_emit (cell, cell_renderer_combo_signals[CHANGED], 0,
343 gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
347 gchar *new_text = NULL;
350 GtkCellRendererCombo *cell;
353 GtkCellRendererComboPrivate *priv;
355 cell = GTK_CELL_RENDERER_COMBO (data);
358 if (priv->focus_out_id > 0)
360 g_signal_handler_disconnect (combo, priv->focus_out_id);
361 priv->focus_out_id = 0;
365 "editing-canceled", &canceled,
367 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
374 if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (combo)))
376 entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo)));
377 new_text = g_strdup (gtk_entry_get_text (entry));
381 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
384 && gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
385 gtk_tree_model_get (model, &iter, priv->text_column, &new_text, -1);
388 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
389 g_signal_emit_by_name (cell, "edited", path, new_text);
397 gtk_cell_renderer_combo_focus_out_event (GtkWidget *widget,
402 gtk_cell_renderer_combo_editing_done (GTK_CELL_EDITABLE (widget), data);
409 GtkCellRendererCombo *cell;
415 find_text (GtkTreeModel *model,
420 GtkCellRendererComboPrivate *priv;
421 SearchData *search_data = (SearchData *)data;
422 gchar *text, *cell_text;
424 priv = search_data->cell->priv;
426 gtk_tree_model_get (model, iter, priv->text_column, &text, -1);
427 g_object_get (GTK_CELL_RENDERER_TEXT (search_data->cell),
430 if (text && cell_text && g_strcmp0 (text, cell_text) == 0)
432 search_data->iter = *iter;
433 search_data->found = TRUE;
439 return search_data->found;
442 static GtkCellEditable *
443 gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
447 const GdkRectangle *background_area,
448 const GdkRectangle *cell_area,
449 GtkCellRendererState flags)
451 GtkCellRendererCombo *cell_combo;
452 GtkCellRendererText *cell_text;
454 SearchData search_data;
455 GtkCellRendererComboPrivate *priv;
459 cell_text = GTK_CELL_RENDERER_TEXT (cell);
460 g_object_get (cell_text, "editable", &editable, NULL);
461 if (editable == FALSE)
464 cell_combo = GTK_CELL_RENDERER_COMBO (cell);
465 priv = cell_combo->priv;
467 if (priv->text_column < 0)
472 combo = g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
475 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), priv->model);
476 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (combo),
479 g_object_get (cell_text, "text", &text, NULL);
481 gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo))),
487 cell = gtk_cell_renderer_text_new ();
489 combo = gtk_combo_box_new ();
491 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), priv->model);
493 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
494 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo),
495 cell, "text", priv->text_column,
498 /* determine the current value */
501 search_data.cell = cell_combo;
502 search_data.found = FALSE;
503 gtk_tree_model_foreach (priv->model, find_text, &search_data);
504 if (search_data.found)
505 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo),
506 &(search_data.iter));
510 g_object_set (combo, "has-frame", FALSE, NULL);
511 g_object_set_data_full (G_OBJECT (combo),
512 I_(GTK_CELL_RENDERER_COMBO_PATH),
513 g_strdup (path), g_free);
515 gtk_widget_show (combo);
517 g_signal_connect (GTK_CELL_EDITABLE (combo), "editing-done",
518 G_CALLBACK (gtk_cell_renderer_combo_editing_done),
520 g_signal_connect (GTK_CELL_EDITABLE (combo), "changed",
521 G_CALLBACK (gtk_cell_renderer_combo_changed),
523 priv->focus_out_id = g_signal_connect (combo, "focus-out-event",
524 G_CALLBACK (gtk_cell_renderer_combo_focus_out_event),
529 return GTK_CELL_EDITABLE (combo);