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 "gtkcomboboxentry.h"
31 #include "gtkmarshalers.h"
32 #include "gtkprivate.h"
34 struct _GtkCellRendererComboPrivate
48 static void gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass);
49 static void gtk_cell_renderer_combo_init (GtkCellRendererCombo *self);
50 static void gtk_cell_renderer_combo_finalize (GObject *object);
51 static void gtk_cell_renderer_combo_get_property (GObject *object,
56 static void gtk_cell_renderer_combo_set_property (GObject *object,
61 static GtkCellEditable *gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
65 const GdkRectangle *background_area,
66 const GdkRectangle *cell_area,
67 GtkCellRendererState flags);
81 static guint cell_renderer_combo_signals[LAST_SIGNAL] = { 0, };
83 #define GTK_CELL_RENDERER_COMBO_PATH "gtk-cell-renderer-combo-path"
85 G_DEFINE_TYPE (GtkCellRendererCombo, gtk_cell_renderer_combo, GTK_TYPE_CELL_RENDERER_TEXT)
88 gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass)
90 GObjectClass *object_class = G_OBJECT_CLASS (klass);
91 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
93 object_class->finalize = gtk_cell_renderer_combo_finalize;
94 object_class->get_property = gtk_cell_renderer_combo_get_property;
95 object_class->set_property = gtk_cell_renderer_combo_set_property;
97 cell_class->start_editing = gtk_cell_renderer_combo_start_editing;
100 * GtkCellRendererCombo:model:
102 * Holds a tree model containing the possible values for the combo box.
103 * Use the text_column property to specify the column holding the values.
107 g_object_class_install_property (object_class,
109 g_param_spec_object ("model",
111 P_("The model containing the possible values for the combo box"),
113 GTK_PARAM_READWRITE));
116 * GtkCellRendererCombo:text-column:
118 * Specifies the model column which holds the possible values for the
121 * Note that this refers to the model specified in the model property,
122 * <emphasis>not</emphasis> the model backing the tree view to which
123 * this cell renderer is attached.
125 * #GtkCellRendererCombo automatically adds a text cell renderer for
126 * this column to its combo box.
130 g_object_class_install_property (object_class,
132 g_param_spec_int ("text-column",
134 P_("A column in the data source model to get the strings from"),
138 GTK_PARAM_READWRITE));
141 * GtkCellRendererCombo:has-entry:
143 * If %TRUE, the cell renderer will include an entry and allow to enter
144 * values other than the ones in the popup list.
148 g_object_class_install_property (object_class,
150 g_param_spec_boolean ("has-entry",
152 P_("If FALSE, don't allow to enter strings other than the chosen ones"),
154 GTK_PARAM_READWRITE));
158 * GtkCellRendererCombo::changed:
159 * @combo: the object on which the signal is emitted
160 * @path_string: a string of the path identifying the edited cell
161 * (relative to the tree view model)
162 * @new_iter: the new iter selected in the combo box
163 * (relative to the combo box model)
165 * This signal is emitted each time after the user selected an item in
166 * the combo box, either by using the mouse or the arrow keys. Contrary
167 * to GtkComboBox, GtkCellRendererCombo::changed is not emitted for
168 * changes made to a selected item in the entry. The argument @new_iter
169 * corresponds to the newly selected item in the combo box and it is relative
170 * to the GtkTreeModel set via the model property on GtkCellRendererCombo.
172 * Note that as soon as you change the model displayed in the tree view,
173 * the tree view will immediately cease the editing operating. This
174 * means that you most probably want to refrain from changing the model
175 * until the combo cell renderer emits the edited or editing_canceled signal.
179 cell_renderer_combo_signals[CHANGED] =
180 g_signal_new (I_("changed"),
181 G_TYPE_FROM_CLASS (object_class),
185 _gtk_marshal_VOID__STRING_BOXED,
190 g_type_class_add_private (klass, sizeof (GtkCellRendererComboPrivate));
194 gtk_cell_renderer_combo_init (GtkCellRendererCombo *self)
196 GtkCellRendererComboPrivate *priv;
198 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
199 GTK_TYPE_CELL_RENDERER_COMBO,
200 GtkCellRendererComboPrivate);
204 priv->text_column = -1;
205 priv->has_entry = TRUE;
206 priv->focus_out_id = 0;
210 * gtk_cell_renderer_combo_new:
212 * Creates a new #GtkCellRendererCombo.
213 * Adjust how text is drawn using object properties.
214 * Object properties can be set globally (with g_object_set()).
215 * Also, with #GtkTreeViewColumn, you can bind a property to a value
216 * in a #GtkTreeModel. For example, you can bind the "text" property
217 * on the cell renderer to a string value in the model, thus rendering
218 * a different string in each row of the #GtkTreeView.
220 * Returns: the new cell renderer
225 gtk_cell_renderer_combo_new (void)
227 return g_object_new (GTK_TYPE_CELL_RENDERER_COMBO, NULL);
231 gtk_cell_renderer_combo_finalize (GObject *object)
233 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
234 GtkCellRendererComboPrivate *priv = cell->priv;
238 g_object_unref (priv->model);
242 G_OBJECT_CLASS (gtk_cell_renderer_combo_parent_class)->finalize (object);
246 gtk_cell_renderer_combo_get_property (GObject *object,
251 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
252 GtkCellRendererComboPrivate *priv = cell->priv;
257 g_value_set_object (value, priv->model);
259 case PROP_TEXT_COLUMN:
260 g_value_set_int (value, priv->text_column);
263 g_value_set_boolean (value, priv->has_entry);
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272 gtk_cell_renderer_combo_set_property (GObject *object,
277 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
278 GtkCellRendererComboPrivate *priv = cell->priv;
285 g_object_unref (priv->model);
286 priv->model = GTK_TREE_MODEL (g_value_get_object (value));
288 g_object_ref (priv->model);
291 case PROP_TEXT_COLUMN:
292 priv->text_column = g_value_get_int (value);
295 priv->has_entry = g_value_get_boolean (value);
298 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
304 gtk_cell_renderer_combo_changed (GtkComboBox *combo,
308 GtkCellRendererCombo *cell;
310 cell = GTK_CELL_RENDERER_COMBO (data);
312 if (gtk_combo_box_get_active_iter (combo, &iter))
316 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
317 g_signal_emit (cell, cell_renderer_combo_signals[CHANGED], 0,
323 gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
327 gchar *new_text = NULL;
330 GtkCellRendererCombo *cell;
333 GtkCellRendererComboPrivate *priv;
335 cell = GTK_CELL_RENDERER_COMBO (data);
338 if (priv->focus_out_id > 0)
340 g_signal_handler_disconnect (combo, priv->focus_out_id);
341 priv->focus_out_id = 0;
345 "editing-canceled", &canceled,
347 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
354 if (GTK_IS_COMBO_BOX_ENTRY (combo))
356 entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo)));
357 new_text = g_strdup (gtk_entry_get_text (entry));
361 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
364 && gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
365 gtk_tree_model_get (model, &iter, priv->text_column, &new_text, -1);
368 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
369 g_signal_emit_by_name (cell, "edited", path, new_text);
377 gtk_cell_renderer_combo_focus_out_event (GtkWidget *widget,
382 gtk_cell_renderer_combo_editing_done (GTK_CELL_EDITABLE (widget), data);
389 GtkCellRendererCombo *cell;
395 find_text (GtkTreeModel *model,
400 GtkCellRendererComboPrivate *priv;
401 SearchData *search_data = (SearchData *)data;
402 gchar *text, *cell_text;
404 priv = search_data->cell->priv;
406 gtk_tree_model_get (model, iter, priv->text_column, &text, -1);
407 g_object_get (GTK_CELL_RENDERER_TEXT (search_data->cell),
410 if (text && cell_text && g_strcmp0 (text, cell_text) == 0)
412 search_data->iter = *iter;
413 search_data->found = TRUE;
419 return search_data->found;
422 static GtkCellEditable *
423 gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
427 const GdkRectangle *background_area,
428 const GdkRectangle *cell_area,
429 GtkCellRendererState flags)
431 GtkCellRendererCombo *cell_combo;
432 GtkCellRendererText *cell_text;
434 SearchData search_data;
435 GtkCellRendererComboPrivate *priv;
439 cell_text = GTK_CELL_RENDERER_TEXT (cell);
440 g_object_get (cell_text, "editable", &editable, NULL);
441 if (editable == FALSE)
444 cell_combo = GTK_CELL_RENDERER_COMBO (cell);
445 priv = cell_combo->priv;
447 if (priv->text_column < 0)
452 combo = gtk_combo_box_entry_new ();
455 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), priv->model);
456 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combo),
459 g_object_get (cell_text, "text", &text, NULL);
461 gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo))),
467 cell = gtk_cell_renderer_text_new ();
469 combo = gtk_combo_box_new ();
471 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), priv->model);
473 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
474 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo),
475 cell, "text", priv->text_column,
478 /* determine the current value */
481 search_data.cell = cell_combo;
482 search_data.found = FALSE;
483 gtk_tree_model_foreach (priv->model, find_text, &search_data);
484 if (search_data.found)
485 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo),
486 &(search_data.iter));
490 g_object_set (combo, "has-frame", FALSE, NULL);
491 g_object_set_data_full (G_OBJECT (combo),
492 I_(GTK_CELL_RENDERER_COMBO_PATH),
493 g_strdup (path), g_free);
495 gtk_widget_show (combo);
497 g_signal_connect (GTK_CELL_EDITABLE (combo), "editing-done",
498 G_CALLBACK (gtk_cell_renderer_combo_editing_done),
500 g_signal_connect (GTK_CELL_EDITABLE (combo), "changed",
501 G_CALLBACK (gtk_cell_renderer_combo_changed),
503 priv->focus_out_id = g_signal_connect (combo, "focus-out-event",
504 G_CALLBACK (gtk_cell_renderer_combo_focus_out_event),
509 return GTK_CELL_EDITABLE (combo);