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