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);
271 GtkCellRendererComboPrivate *priv;
273 priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (cell);
276 g_object_unref (cell->model);
277 cell->model = GTK_TREE_MODEL (g_value_get_object (value));
279 g_object_ref (cell->model);
282 case PROP_TEXT_COLUMN:
283 cell->text_column = g_value_get_int (value);
286 cell->has_entry = g_value_get_boolean (value);
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
295 gtk_cell_renderer_combo_changed (GtkComboBox *combo,
299 GtkCellRendererCombo *cell;
301 cell = GTK_CELL_RENDERER_COMBO (data);
303 if (gtk_combo_box_get_active_iter (combo, &iter))
307 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
308 g_signal_emit (cell, cell_renderer_combo_signals[CHANGED], 0,
314 gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
318 gchar *new_text = NULL;
321 GtkCellRendererCombo *cell;
324 GtkCellRendererComboPrivate *priv;
326 cell = GTK_CELL_RENDERER_COMBO (data);
327 priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (data);
329 if (cell->focus_out_id > 0)
331 g_signal_handler_disconnect (combo, cell->focus_out_id);
332 cell->focus_out_id = 0;
336 "editing-canceled", &canceled,
338 gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
345 if (GTK_IS_COMBO_BOX_ENTRY (combo))
347 entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo)));
348 new_text = g_strdup (gtk_entry_get_text (entry));
352 model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
355 && gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
356 gtk_tree_model_get (model, &iter, cell->text_column, &new_text, -1);
359 path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
360 g_signal_emit_by_name (cell, "edited", path, new_text);
368 gtk_cell_renderer_combo_focus_out_event (GtkWidget *widget,
373 gtk_cell_renderer_combo_editing_done (GTK_CELL_EDITABLE (widget), data);
380 GtkCellRendererCombo *cell;
386 find_text (GtkTreeModel *model,
391 SearchData *search_data = (SearchData *)data;
394 gtk_tree_model_get (model, iter, search_data->cell->text_column, &text, -1);
395 if (text && GTK_CELL_RENDERER_TEXT (search_data->cell)->text &&
396 strcmp (text, GTK_CELL_RENDERER_TEXT (search_data->cell)->text) == 0)
398 search_data->iter = *iter;
399 search_data->found = TRUE;
404 return search_data->found;
407 static GtkCellEditable *
408 gtk_cell_renderer_combo_start_editing (GtkCellRenderer *cell,
412 GdkRectangle *background_area,
413 GdkRectangle *cell_area,
414 GtkCellRendererState flags)
416 GtkCellRendererCombo *cell_combo;
417 GtkCellRendererText *cell_text;
419 SearchData search_data;
420 GtkCellRendererComboPrivate *priv;
422 cell_text = GTK_CELL_RENDERER_TEXT (cell);
423 if (cell_text->editable == FALSE)
426 cell_combo = GTK_CELL_RENDERER_COMBO (cell);
427 if (cell_combo->text_column < 0)
430 priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (cell_combo);
432 if (cell_combo->has_entry)
434 combo = gtk_combo_box_entry_new ();
436 if (cell_combo->model)
437 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), cell_combo->model);
438 gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combo),
439 cell_combo->text_column);
442 gtk_entry_set_text (GTK_ENTRY (GTK_BIN (combo)->child),
447 cell = gtk_cell_renderer_text_new ();
449 combo = gtk_combo_box_new ();
450 if (cell_combo->model)
451 gtk_combo_box_set_model (GTK_COMBO_BOX (combo), cell_combo->model);
453 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
454 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo),
455 cell, "text", cell_combo->text_column,
458 /* determine the current value */
459 if (cell_combo->model)
461 search_data.cell = cell_combo;
462 search_data.found = FALSE;
463 gtk_tree_model_foreach (cell_combo->model, find_text, &search_data);
464 if (search_data.found)
465 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo),
466 &(search_data.iter));
470 g_object_set (combo, "has-frame", FALSE, NULL);
471 g_object_set_data_full (G_OBJECT (combo),
472 I_(GTK_CELL_RENDERER_COMBO_PATH),
473 g_strdup (path), g_free);
475 gtk_widget_show (combo);
477 g_signal_connect (GTK_CELL_EDITABLE (combo), "editing-done",
478 G_CALLBACK (gtk_cell_renderer_combo_editing_done),
480 g_signal_connect (GTK_CELL_EDITABLE (combo), "changed",
481 G_CALLBACK (gtk_cell_renderer_combo_changed),
483 cell_combo->focus_out_id =
484 g_signal_connect (combo, "focus-out-event",
485 G_CALLBACK (gtk_cell_renderer_combo_focus_out_event),
490 return GTK_CELL_EDITABLE (combo);
493 #define __GTK_CELL_RENDERER_COMBO_C__
494 #include "gtkaliasdef.c"