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"
36 #define GTK_CELL_RENDERER_COMBO_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER_COMBO, GtkCellRendererComboPrivate))
38 typedef struct _GtkCellRendererComboPrivate GtkCellRendererComboPrivate;
39 struct _GtkCellRendererComboPrivate
45 static void gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass);
46 static void gtk_cell_renderer_combo_init (GtkCellRendererCombo *self);
47 static void gtk_cell_renderer_combo_finalize (GObject *object);
48 static void gtk_cell_renderer_combo_get_property (GObject *object,
53 static void gtk_cell_renderer_combo_set_property (GObject *object,
58 static GtkCellEditable *gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
62 GdkRectangle *background_area,
63 GdkRectangle *cell_area,
64 GtkCellRendererState flags);
78 static guint cell_renderer_combo_signals[LAST_SIGNAL] = { 0, };
80 #define GTK_CELL_RENDERER_COMBO_PATH "gtk-cell-renderer-combo-path"
82 G_DEFINE_TYPE (GtkCellRendererCombo, gtk_cell_renderer_combo, GTK_TYPE_CELL_RENDERER_TEXT)
85 gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass)
87 GObjectClass *object_class = G_OBJECT_CLASS (klass);
88 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
90 object_class->finalize = gtk_cell_renderer_combo_finalize;
91 object_class->get_property = gtk_cell_renderer_combo_get_property;
92 object_class->set_property = gtk_cell_renderer_combo_set_property;
94 cell_class->start_editing = gtk_cell_renderer_combo_start_editing;
97 * GtkCellRendererCombo:model:
99 * Holds a tree model containing the possible values for the combo box.
100 * Use the text_column property to specify the column holding the values.
104 g_object_class_install_property (object_class,
106 g_param_spec_object ("model",
108 P_("The model containing the possible values for the combo box"),
110 GTK_PARAM_READWRITE));
113 * GtkCellRendererCombo:text-column:
115 * Specifies the model column which holds the possible values for the
118 * Note that this refers to the model specified in the model property,
119 * <emphasis>not</emphasis> the model backing the tree view to which
120 * this cell renderer is attached.
122 * #GtkCellRendererCombo automatically adds a text cell renderer for
123 * this column to its combo box.
127 g_object_class_install_property (object_class,
129 g_param_spec_int ("text-column",
131 P_("A column in the data source model to get the strings from"),
135 GTK_PARAM_READWRITE));
138 * GtkCellRendererCombo:has-entry:
140 * If %TRUE, the cell renderer will include an entry and allow to enter
141 * values other than the ones in the popup list.
145 g_object_class_install_property (object_class,
147 g_param_spec_boolean ("has-entry",
149 P_("If FALSE, don't allow to enter strings other than the chosen ones"),
151 GTK_PARAM_READWRITE));
155 * GtkCellRendererCombo::changed:
156 * @combo: the object on which the signal is emitted
157 * @path_string: a string of the path identifying the edited cell
158 * (relative to the tree view model)
159 * @new_iter: the new iter selected in the combo box
160 * (relative to the combo box model)
162 * This signal is emitted each time after the user selected an item in
163 * the combo box, either by using the mouse or the arrow keys. Contrary
164 * to GtkComboBox, GtkCellRendererCombo::changed is not emitted for
165 * changes made to a selected item in the entry. The argument @new_iter
166 * corresponds to the newly selected item in the combo box and it is relative
167 * to the GtkTreeModel set via the model property on GtkCellRendererCombo.
169 * Note that as soon as you change the model displayed in the tree view,
170 * the tree view will immediately cease the editing operating. This
171 * means that you most probably want to refrain from changing the model
172 * until the combo cell renderer emits the edited or editing_canceled signal.
176 cell_renderer_combo_signals[CHANGED] =
177 g_signal_new (I_("changed"),
178 G_TYPE_FROM_CLASS (object_class),
182 _gtk_marshal_VOID__STRING_BOXED,
187 g_type_class_add_private (klass, sizeof (GtkCellRendererComboPrivate));
191 gtk_cell_renderer_combo_init (GtkCellRendererCombo *self)
194 self->text_column = -1;
195 self->has_entry = TRUE;
196 self->focus_out_id = 0;
200 * gtk_cell_renderer_combo_new:
202 * Creates a new #GtkCellRendererCombo.
203 * Adjust how text is drawn using object properties.
204 * Object properties can be set globally (with g_object_set()).
205 * Also, with #GtkTreeViewColumn, you can bind a property to a value
206 * in a #GtkTreeModel. For example, you can bind the "text" property
207 * on the cell renderer to a string value in the model, thus rendering
208 * a different string in each row of the #GtkTreeView.
210 * Returns: the new cell renderer
215 gtk_cell_renderer_combo_new (void)
217 return g_object_new (GTK_TYPE_CELL_RENDERER_COMBO, NULL);
221 gtk_cell_renderer_combo_finalize (GObject *object)
223 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
227 g_object_unref (cell->model);
231 G_OBJECT_CLASS (gtk_cell_renderer_combo_parent_class)->finalize (object);
235 gtk_cell_renderer_combo_get_property (GObject *object,
240 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
245 g_value_set_object (value, cell->model);
247 case PROP_TEXT_COLUMN:
248 g_value_set_int (value, cell->text_column);
251 g_value_set_boolean (value, cell->has_entry);
254 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260 gtk_cell_renderer_combo_set_property (GObject *object,
265 GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
272 GtkCellRendererComboPrivate *priv;
274 priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (cell);
279 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo), NULL);
280 g_object_unref (cell->model);
284 object = g_value_get_object (value);
287 g_return_if_fail (GTK_IS_TREE_MODEL (object));
288 g_object_ref (object);
290 cell->model = GTK_TREE_MODEL (object);
292 gtk_combo_box_set_model (GTK_COMBO_BOX (priv->combo), cell->model);
296 case PROP_TEXT_COLUMN:
297 cell->text_column = g_value_get_int (value);
300 cell->has_entry = g_value_get_boolean (value);
303 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
309 gtk_cell_renderer_combo_changed (GtkComboBox *combo,
313 GtkCellRendererCombo *cell;
315 cell = GTK_CELL_RENDERER_COMBO (data);
317 if (gtk_combo_box_get_active_iter (combo, &iter))
321 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
322 g_signal_emit (cell, cell_renderer_combo_signals[CHANGED], 0,
328 gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
332 gchar *new_text = NULL;
335 GtkCellRendererCombo *cell;
338 GtkCellRendererComboPrivate *priv;
340 cell = GTK_CELL_RENDERER_COMBO (data);
341 priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (data);
343 if (cell->focus_out_id > 0)
345 g_signal_handler_disconnect (combo, cell->focus_out_id);
346 cell->focus_out_id = 0;
349 canceled = _gtk_combo_box_editing_canceled (GTK_COMBO_BOX (combo));
350 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
357 if (GTK_IS_COMBO_BOX_ENTRY (combo))
359 entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo)));
360 new_text = g_strdup (gtk_entry_get_text (entry));
364 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
367 && gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
368 gtk_tree_model_get (model, &iter, cell->text_column, &new_text, -1);
371 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
372 g_signal_emit_by_name (cell, "edited", path, new_text);
380 gtk_cell_renderer_combo_focus_out_event (GtkWidget *widget,
385 gtk_cell_renderer_combo_editing_done (GTK_CELL_EDITABLE (widget), data);
392 GtkCellRendererCombo *cell;
398 find_text (GtkTreeModel *model,
403 SearchData *search_data = (SearchData *)data;
406 gtk_tree_model_get (model, iter, search_data->cell->text_column, &text, -1);
407 if (text && GTK_CELL_RENDERER_TEXT (search_data->cell)->text &&
408 strcmp (text, GTK_CELL_RENDERER_TEXT (search_data->cell)->text) == 0)
410 search_data->iter = *iter;
411 search_data->found = TRUE;
416 return search_data->found;
419 static GtkCellEditable *
420 gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
424 GdkRectangle *background_area,
425 GdkRectangle *cell_area,
426 GtkCellRendererState flags)
428 GtkCellRendererCombo *cell_combo;
429 GtkCellRendererText *cell_text;
431 SearchData search_data;
432 GtkCellRendererComboPrivate *priv;
434 cell_text = GTK_CELL_RENDERER_TEXT (cell);
435 if (cell_text->editable == FALSE)
438 cell_combo = GTK_CELL_RENDERER_COMBO (cell);
439 if (cell_combo->text_column < 0)
442 priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (cell_combo);
444 if (cell_combo->has_entry)
446 combo = gtk_combo_box_entry_new ();
448 if (cell_combo->model)
449 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), cell_combo->model);
450 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combo),
451 cell_combo->text_column);
454 gtk_entry_set_text (GTK_ENTRY (GTK_BIN (combo)->child),
459 cell = gtk_cell_renderer_text_new ();
461 combo = gtk_combo_box_new ();
462 if (cell_combo->model)
463 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), cell_combo->model);
465 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
466 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo),
467 cell, "text", cell_combo->text_column,
470 /* determine the current value */
471 if (cell_combo->model)
473 search_data.cell = cell_combo;
474 search_data.found = FALSE;
475 gtk_tree_model_foreach (cell_combo->model, find_text, &search_data);
476 if (search_data.found)
477 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo),
478 &(search_data.iter));
482 g_object_set (combo, "has-frame", FALSE, NULL);
483 g_object_set_data_full (G_OBJECT (combo),
484 I_(GTK_CELL_RENDERER_COMBO_PATH),
485 g_strdup (path), g_free);
487 gtk_widget_show (combo);
489 g_signal_connect (GTK_CELL_EDITABLE (combo), "editing_done",
490 G_CALLBACK (gtk_cell_renderer_combo_editing_done),
492 g_signal_connect (GTK_CELL_EDITABLE (combo), "changed",
493 G_CALLBACK (gtk_cell_renderer_combo_changed),
495 cell_combo->focus_out_id =
496 g_signal_connect (combo, "focus_out_event",
497 G_CALLBACK (gtk_cell_renderer_combo_focus_out_event),
502 return GTK_CELL_EDITABLE (combo);
505 #define __GTK_CELL_RENDERER_COMBO_C__
506 #include "gtkaliasdef.c"