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"
33 struct _GtkCellRendererComboPrivate
47 static void gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass);
48 static void gtk_cell_renderer_combo_init (GtkCellRendererCombo *self);
49 static void gtk_cell_renderer_combo_finalize (GObject *object);
50 static void gtk_cell_renderer_combo_get_property (GObject *object,
55 static void gtk_cell_renderer_combo_set_property (GObject *object,
60 static GtkCellEditable *gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
64 const GdkRectangle *background_area,
65 const GdkRectangle *cell_area,
66 GtkCellRendererState flags);
80 static guint cell_renderer_combo_signals[LAST_SIGNAL] = { 0, };
82 #define GTK_CELL_RENDERER_COMBO_PATH "gtk-cell-renderer-combo-path"
84 G_DEFINE_TYPE (GtkCellRendererCombo, gtk_cell_renderer_combo, GTK_TYPE_CELL_RENDERER_TEXT)
87 gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass)
89 GObjectClass *object_class = G_OBJECT_CLASS (klass);
90 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
92 object_class->finalize = gtk_cell_renderer_combo_finalize;
93 object_class->get_property = gtk_cell_renderer_combo_get_property;
94 object_class->set_property = gtk_cell_renderer_combo_set_property;
96 cell_class->start_editing = gtk_cell_renderer_combo_start_editing;
99 * GtkCellRendererCombo:model:
101 * Holds a tree model containing the possible values for the combo box.
102 * Use the text_column property to specify the column holding the values.
106 g_object_class_install_property (object_class,
108 g_param_spec_object ("model",
110 P_("The model containing the possible values for the combo box"),
112 GTK_PARAM_READWRITE));
115 * GtkCellRendererCombo:text-column:
117 * Specifies the model column which holds the possible values for the
120 * Note that this refers to the model specified in the model property,
121 * <emphasis>not</emphasis> the model backing the tree view to which
122 * this cell renderer is attached.
124 * #GtkCellRendererCombo automatically adds a text cell renderer for
125 * this column to its combo box.
129 g_object_class_install_property (object_class,
131 g_param_spec_int ("text-column",
133 P_("A column in the data source model to get the strings from"),
137 GTK_PARAM_READWRITE));
140 * GtkCellRendererCombo:has-entry:
142 * If %TRUE, the cell renderer will include an entry and allow to enter
143 * values other than the ones in the popup list.
147 g_object_class_install_property (object_class,
149 g_param_spec_boolean ("has-entry",
151 P_("If FALSE, don't allow to enter strings other than the chosen ones"),
153 GTK_PARAM_READWRITE));
157 * GtkCellRendererCombo::changed:
158 * @combo: the object on which the signal is emitted
159 * @path_string: a string of the path identifying the edited cell
160 * (relative to the tree view model)
161 * @new_iter: the new iter selected in the combo box
162 * (relative to the combo box model)
164 * This signal is emitted each time after the user selected an item in
165 * the combo box, either by using the mouse or the arrow keys. Contrary
166 * to GtkComboBox, GtkCellRendererCombo::changed is not emitted for
167 * changes made to a selected item in the entry. The argument @new_iter
168 * corresponds to the newly selected item in the combo box and it is relative
169 * to the GtkTreeModel set via the model property on GtkCellRendererCombo.
171 * Note that as soon as you change the model displayed in the tree view,
172 * the tree view will immediately cease the editing operating. This
173 * means that you most probably want to refrain from changing the model
174 * until the combo cell renderer emits the edited or editing_canceled signal.
178 cell_renderer_combo_signals[CHANGED] =
179 g_signal_new (I_("changed"),
180 G_TYPE_FROM_CLASS (object_class),
184 _gtk_marshal_VOID__STRING_BOXED,
189 g_type_class_add_private (klass, sizeof (GtkCellRendererComboPrivate));
193 gtk_cell_renderer_combo_init (GtkCellRendererCombo *self)
195 GtkCellRendererComboPrivate *priv;
197 self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
198 GTK_TYPE_CELL_RENDERER_COMBO,
199 GtkCellRendererComboPrivate);
203 priv->text_column = -1;
204 priv->has_entry = TRUE;
205 priv->focus_out_id = 0;
209 * gtk_cell_renderer_combo_new:
211 * Creates a new #GtkCellRendererCombo.
212 * Adjust how text is drawn using object properties.
213 * Object properties can be set globally (with g_object_set()).
214 * Also, with #GtkTreeViewColumn, you can bind a property to a value
215 * in a #GtkTreeModel. For example, you can bind the "text" property
216 * on the cell renderer to a string value in the model, thus rendering
217 * a different string in each row of the #GtkTreeView.
219 * Returns: the new cell renderer
224 gtk_cell_renderer_combo_new (void)
226 return g_object_new (GTK_TYPE_CELL_RENDERER_COMBO, NULL);
230 gtk_cell_renderer_combo_finalize (GObject *object)
232 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
233 GtkCellRendererComboPrivate *priv = cell->priv;
237 g_object_unref (priv->model);
241 G_OBJECT_CLASS (gtk_cell_renderer_combo_parent_class)->finalize (object);
245 gtk_cell_renderer_combo_get_property (GObject *object,
250 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
251 GtkCellRendererComboPrivate *priv = cell->priv;
256 g_value_set_object (value, priv->model);
258 case PROP_TEXT_COLUMN:
259 g_value_set_int (value, priv->text_column);
262 g_value_set_boolean (value, priv->has_entry);
265 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
271 gtk_cell_renderer_combo_set_property (GObject *object,
276 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
277 GtkCellRendererComboPrivate *priv = cell->priv;
284 g_object_unref (priv->model);
285 priv->model = GTK_TREE_MODEL (g_value_get_object (value));
287 g_object_ref (priv->model);
290 case PROP_TEXT_COLUMN:
291 priv->text_column = g_value_get_int (value);
294 priv->has_entry = g_value_get_boolean (value);
297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
303 gtk_cell_renderer_combo_changed (GtkComboBox *combo,
307 GtkCellRendererCombo *cell;
309 cell = GTK_CELL_RENDERER_COMBO (data);
311 if (gtk_combo_box_get_active_iter (combo, &iter))
315 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
316 g_signal_emit (cell, cell_renderer_combo_signals[CHANGED], 0,
322 gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
326 gchar *new_text = NULL;
329 GtkCellRendererCombo *cell;
332 GtkCellRendererComboPrivate *priv;
334 cell = GTK_CELL_RENDERER_COMBO (data);
337 if (priv->focus_out_id > 0)
339 g_signal_handler_disconnect (combo, priv->focus_out_id);
340 priv->focus_out_id = 0;
344 "editing-canceled", &canceled,
346 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
353 if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (combo)))
355 entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo)));
356 new_text = g_strdup (gtk_entry_get_text (entry));
360 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
363 && gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
364 gtk_tree_model_get (model, &iter, priv->text_column, &new_text, -1);
367 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
368 g_signal_emit_by_name (cell, "edited", path, new_text);
376 gtk_cell_renderer_combo_focus_out_event (GtkWidget *widget,
381 gtk_cell_renderer_combo_editing_done (GTK_CELL_EDITABLE (widget), data);
388 GtkCellRendererCombo *cell;
394 find_text (GtkTreeModel *model,
399 GtkCellRendererComboPrivate *priv;
400 SearchData *search_data = (SearchData *)data;
401 gchar *text, *cell_text;
403 priv = search_data->cell->priv;
405 gtk_tree_model_get (model, iter, priv->text_column, &text, -1);
406 g_object_get (GTK_CELL_RENDERER_TEXT (search_data->cell),
409 if (text && cell_text && g_strcmp0 (text, cell_text) == 0)
411 search_data->iter = *iter;
412 search_data->found = TRUE;
418 return search_data->found;
421 static GtkCellEditable *
422 gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
426 const GdkRectangle *background_area,
427 const GdkRectangle *cell_area,
428 GtkCellRendererState flags)
430 GtkCellRendererCombo *cell_combo;
431 GtkCellRendererText *cell_text;
433 SearchData search_data;
434 GtkCellRendererComboPrivate *priv;
438 cell_text = GTK_CELL_RENDERER_TEXT (cell);
439 g_object_get (cell_text, "editable", &editable, NULL);
440 if (editable == FALSE)
443 cell_combo = GTK_CELL_RENDERER_COMBO (cell);
444 priv = cell_combo->priv;
446 if (priv->text_column < 0)
451 combo = g_object_new (GTK_TYPE_COMBO_BOX, "has-entry", TRUE, NULL);
454 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), priv->model);
455 gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (combo),
458 g_object_get (cell_text, "text", &text, NULL);
460 gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo))),
466 cell = gtk_cell_renderer_text_new ();
468 combo = gtk_combo_box_new ();
470 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), priv->model);
472 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
473 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo),
474 cell, "text", priv->text_column,
477 /* determine the current value */
480 search_data.cell = cell_combo;
481 search_data.found = FALSE;
482 gtk_tree_model_foreach (priv->model, find_text, &search_data);
483 if (search_data.found)
484 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo),
485 &(search_data.iter));
489 g_object_set (combo, "has-frame", FALSE, NULL);
490 g_object_set_data_full (G_OBJECT (combo),
491 I_(GTK_CELL_RENDERER_COMBO_PATH),
492 g_strdup (path), g_free);
494 gtk_widget_show (combo);
496 g_signal_connect (GTK_CELL_EDITABLE (combo), "editing-done",
497 G_CALLBACK (gtk_cell_renderer_combo_editing_done),
499 g_signal_connect (GTK_CELL_EDITABLE (combo), "changed",
500 G_CALLBACK (gtk_cell_renderer_combo_changed),
502 priv->focus_out_id = g_signal_connect (combo, "focus-out-event",
503 G_CALLBACK (gtk_cell_renderer_combo_focus_out_event),
508 return GTK_CELL_EDITABLE (combo);