]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrenderercombo.c
Make GdkRectangle arguments in GtkCellRenderer use const consistently
[~andy/gtk] / gtk / gtkcellrenderercombo.c
1 /* GtkCellRendererCombo
2  * Copyright (C) 2004 Lorenzo Gil Sanchez
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21 #include <string.h>
22
23 #include "gtkintl.h"
24 #include "gtkbin.h"
25 #include "gtkentry.h"
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"
33
34 struct _GtkCellRendererComboPrivate
35 {
36   GtkTreeModel *model;
37
38   GtkWidget *combo;
39
40   gboolean has_entry;
41
42   gint text_column;
43
44   guint focus_out_id;
45 };
46
47
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,
52                                                   guint         prop_id,
53                                                   GValue       *value,
54                                                   GParamSpec   *pspec);
55
56 static void gtk_cell_renderer_combo_set_property (GObject      *object,
57                                                   guint         prop_id,
58                                                   const GValue *value,
59                                                   GParamSpec   *pspec);
60
61 static GtkCellEditable *gtk_cell_renderer_combo_start_editing (GtkCellRenderer     *cell,
62                                                                GdkEvent            *event,
63                                                                GtkWidget           *widget,
64                                                                const gchar         *path,
65                                                                const GdkRectangle  *background_area,
66                                                                const GdkRectangle  *cell_area,
67                                                                GtkCellRendererState flags);
68
69 enum {
70   PROP_0,
71   PROP_MODEL,
72   PROP_TEXT_COLUMN,
73   PROP_HAS_ENTRY
74 };
75
76 enum {
77   CHANGED,
78   LAST_SIGNAL
79 };
80
81 static guint cell_renderer_combo_signals[LAST_SIGNAL] = { 0, };
82
83 #define GTK_CELL_RENDERER_COMBO_PATH "gtk-cell-renderer-combo-path"
84
85 G_DEFINE_TYPE (GtkCellRendererCombo, gtk_cell_renderer_combo, GTK_TYPE_CELL_RENDERER_TEXT)
86
87 static void
88 gtk_cell_renderer_combo_class_init (GtkCellRendererComboClass *klass)
89 {
90   GObjectClass *object_class = G_OBJECT_CLASS (klass);
91   GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
92
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;
96
97   cell_class->start_editing = gtk_cell_renderer_combo_start_editing;
98
99   /**
100    * GtkCellRendererCombo:model:
101    *
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.
104    * 
105    * Since: 2.6
106    */
107   g_object_class_install_property (object_class,
108                                    PROP_MODEL,
109                                    g_param_spec_object ("model",
110                                                         P_("Model"),
111                                                         P_("The model containing the possible values for the combo box"),
112                                                         GTK_TYPE_TREE_MODEL,
113                                                         GTK_PARAM_READWRITE));
114
115   /**
116    * GtkCellRendererCombo:text-column:
117    *
118    * Specifies the model column which holds the possible values for the 
119    * combo box. 
120    *
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.
124    * 
125    * #GtkCellRendererCombo automatically adds a text cell renderer for 
126    * this column to its combo box.
127    *
128    * Since: 2.6
129    */
130   g_object_class_install_property (object_class,
131                                    PROP_TEXT_COLUMN,
132                                    g_param_spec_int ("text-column",
133                                                      P_("Text Column"),
134                                                      P_("A column in the data source model to get the strings from"),
135                                                      -1,
136                                                      G_MAXINT,
137                                                      -1,
138                                                      GTK_PARAM_READWRITE));
139
140   /** 
141    * GtkCellRendererCombo:has-entry:
142    *
143    * If %TRUE, the cell renderer will include an entry and allow to enter 
144    * values other than the ones in the popup list. 
145    *
146    * Since: 2.6
147    */
148   g_object_class_install_property (object_class,
149                                    PROP_HAS_ENTRY,
150                                    g_param_spec_boolean ("has-entry",
151                                                          P_("Has Entry"),
152                                                          P_("If FALSE, don't allow to enter strings other than the chosen ones"),
153                                                          TRUE,
154                                                          GTK_PARAM_READWRITE));
155
156
157   /**
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)
164    *
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.
171    *
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.
176    *
177    * Since: 2.14
178    */
179   cell_renderer_combo_signals[CHANGED] =
180     g_signal_new (I_("changed"),
181                   G_TYPE_FROM_CLASS (object_class),
182                   G_SIGNAL_RUN_LAST,
183                   0,
184                   NULL, NULL,
185                   _gtk_marshal_VOID__STRING_BOXED,
186                   G_TYPE_NONE, 2,
187                   G_TYPE_STRING,
188                   GTK_TYPE_TREE_ITER);
189
190   g_type_class_add_private (klass, sizeof (GtkCellRendererComboPrivate));
191 }
192
193 static void
194 gtk_cell_renderer_combo_init (GtkCellRendererCombo *self)
195 {
196   GtkCellRendererComboPrivate *priv;
197
198   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
199                                             GTK_TYPE_CELL_RENDERER_COMBO,
200                                             GtkCellRendererComboPrivate);
201   priv = self->priv;
202
203   priv->model = NULL;
204   priv->text_column = -1;
205   priv->has_entry = TRUE;
206   priv->focus_out_id = 0;
207 }
208
209 /**
210  * gtk_cell_renderer_combo_new: 
211  * 
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.
219  * 
220  * Returns: the new cell renderer
221  *
222  * Since: 2.6
223  */
224 GtkCellRenderer *
225 gtk_cell_renderer_combo_new (void)
226 {
227   return g_object_new (GTK_TYPE_CELL_RENDERER_COMBO, NULL); 
228 }
229
230 static void
231 gtk_cell_renderer_combo_finalize (GObject *object)
232 {
233   GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
234   GtkCellRendererComboPrivate *priv = cell->priv;
235   
236   if (priv->model)
237     {
238       g_object_unref (priv->model);
239       priv->model = NULL;
240     }
241   
242   G_OBJECT_CLASS (gtk_cell_renderer_combo_parent_class)->finalize (object);
243 }
244
245 static void
246 gtk_cell_renderer_combo_get_property (GObject    *object,
247                                       guint       prop_id,
248                                       GValue     *value,
249                                       GParamSpec *pspec)
250 {
251   GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
252   GtkCellRendererComboPrivate *priv = cell->priv;
253
254   switch (prop_id)
255     {
256     case PROP_MODEL:
257       g_value_set_object (value, priv->model);
258       break; 
259     case PROP_TEXT_COLUMN:
260       g_value_set_int (value, priv->text_column);
261       break;
262     case PROP_HAS_ENTRY:
263       g_value_set_boolean (value, priv->has_entry);
264       break;
265    default:
266       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267       break;
268     }
269 }
270
271 static void
272 gtk_cell_renderer_combo_set_property (GObject      *object,
273                                       guint         prop_id,
274                                       const GValue *value,
275                                       GParamSpec   *pspec)
276 {
277   GtkCellRendererCombo *cell = GTK_CELL_RENDERER_COMBO (object);
278   GtkCellRendererComboPrivate *priv = cell->priv;
279
280   switch (prop_id)
281     {
282     case PROP_MODEL:
283       {
284         if (priv->model)
285           g_object_unref (priv->model);
286         priv->model = GTK_TREE_MODEL (g_value_get_object (value));
287         if (priv->model)
288           g_object_ref (priv->model);
289         break;
290       }
291     case PROP_TEXT_COLUMN:
292       priv->text_column = g_value_get_int (value);
293       break;
294     case PROP_HAS_ENTRY:
295       priv->has_entry = g_value_get_boolean (value);
296       break;
297     default:
298       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299       break;
300     }
301 }
302
303 static void
304 gtk_cell_renderer_combo_changed (GtkComboBox *combo,
305                                  gpointer     data)
306 {
307   GtkTreeIter iter;
308   GtkCellRendererCombo *cell;
309
310   cell = GTK_CELL_RENDERER_COMBO (data);
311
312   if (gtk_combo_box_get_active_iter (combo, &iter))
313     {
314       const char *path;
315
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,
318                      path, &iter);
319     }
320 }
321
322 static void
323 gtk_cell_renderer_combo_editing_done (GtkCellEditable *combo,
324                                       gpointer         data)
325 {
326   const gchar *path;
327   gchar *new_text = NULL;
328   GtkTreeModel *model;
329   GtkTreeIter iter;
330   GtkCellRendererCombo *cell;
331   GtkEntry *entry;
332   gboolean canceled;
333   GtkCellRendererComboPrivate *priv;
334
335   cell = GTK_CELL_RENDERER_COMBO (data);
336   priv = cell->priv;
337
338   if (priv->focus_out_id > 0)
339     {
340       g_signal_handler_disconnect (combo, priv->focus_out_id);
341       priv->focus_out_id = 0;
342     }
343
344   g_object_get (combo,
345                 "editing-canceled", &canceled,
346                 NULL);
347   gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
348   if (canceled)
349     {
350       priv->combo = NULL;
351       return;
352     }
353
354   if (GTK_IS_COMBO_BOX_ENTRY (combo))
355     {
356       entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo)));
357       new_text = g_strdup (gtk_entry_get_text (entry));
358     }
359   else 
360     {
361       model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
362
363       if (model
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);
366     }
367
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);
370
371   priv->combo = NULL;
372
373   g_free (new_text);
374 }
375
376 static gboolean
377 gtk_cell_renderer_combo_focus_out_event (GtkWidget *widget,
378                                          GdkEvent  *event,
379                                          gpointer   data)
380 {
381   
382   gtk_cell_renderer_combo_editing_done (GTK_CELL_EDITABLE (widget), data);
383
384   return FALSE;
385 }
386
387 typedef struct 
388 {
389   GtkCellRendererCombo *cell;
390   gboolean found;
391   GtkTreeIter iter;
392 } SearchData;
393
394 static gboolean 
395 find_text (GtkTreeModel *model, 
396            GtkTreePath  *path, 
397            GtkTreeIter  *iter, 
398            gpointer      data)
399 {
400   GtkCellRendererComboPrivate *priv;
401   SearchData *search_data = (SearchData *)data;
402   gchar *text, *cell_text;
403
404   priv = search_data->cell->priv;
405   
406   gtk_tree_model_get (model, iter, priv->text_column, &text, -1);
407   g_object_get (GTK_CELL_RENDERER_TEXT (search_data->cell),
408                 "text", &cell_text,
409                 NULL);
410   if (text && cell_text && g_strcmp0 (text, cell_text) == 0)
411     {
412       search_data->iter = *iter;
413       search_data->found = TRUE;
414     }
415
416   g_free (cell_text);
417   g_free (text);
418   
419   return search_data->found;
420 }
421
422 static GtkCellEditable *
423 gtk_cell_renderer_combo_start_editing (GtkCellRenderer     *cell,
424                                        GdkEvent            *event,
425                                        GtkWidget           *widget,
426                                        const gchar         *path,
427                                        const GdkRectangle  *background_area,
428                                        const GdkRectangle  *cell_area,
429                                        GtkCellRendererState flags)
430 {
431   GtkCellRendererCombo *cell_combo;
432   GtkCellRendererText *cell_text;
433   GtkWidget *combo;
434   SearchData search_data;
435   GtkCellRendererComboPrivate *priv;
436   gboolean editable;
437   gchar *text;
438
439   cell_text = GTK_CELL_RENDERER_TEXT (cell);
440   g_object_get (cell_text, "editable", &editable, NULL);
441   if (editable == FALSE)
442     return NULL;
443
444   cell_combo = GTK_CELL_RENDERER_COMBO (cell);
445   priv = cell_combo->priv;
446
447   if (priv->text_column < 0)
448     return NULL;
449
450   if (priv->has_entry)
451     {
452       combo = gtk_combo_box_entry_new ();
453
454       if (priv->model)
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),
457                                            priv->text_column);
458
459       g_object_get (cell_text, "text", &text, NULL);
460       if (text)
461         gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo))),
462                             text);
463       g_free (text);
464     }
465   else
466     {
467       cell = gtk_cell_renderer_text_new ();
468
469       combo = gtk_combo_box_new ();
470       if (priv->model)
471         gtk_combo_box_set_model (GTK_COMBO_BOX (combo), priv->model);
472
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,
476                                       NULL);
477
478       /* determine the current value */
479       if (priv->model)
480         {
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));
487         }
488     }
489
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);
494
495   gtk_widget_show (combo);
496
497   g_signal_connect (GTK_CELL_EDITABLE (combo), "editing-done",
498                     G_CALLBACK (gtk_cell_renderer_combo_editing_done),
499                     cell_combo);
500   g_signal_connect (GTK_CELL_EDITABLE (combo), "changed",
501                     G_CALLBACK (gtk_cell_renderer_combo_changed),
502                     cell_combo);
503   priv->focus_out_id = g_signal_connect (combo, "focus-out-event",
504                                          G_CALLBACK (gtk_cell_renderer_combo_focus_out_event),
505                                          cell_combo);
506
507   priv->combo = combo;
508
509   return GTK_CELL_EDITABLE (combo);
510 }