]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrenderercombo.c
Updated Norwegian bokmål 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   canceled = _gtk_combo_box_editing_canceled (GTK_COMBO_BOX (combo));
336   gtk_cell_renderer_stop_editing (GTK_CELL_RENDERER (data), canceled);
337   if (canceled)
338     {
339       priv->combo = NULL;
340       return;
341     }
342
343   if (GTK_IS_COMBO_BOX_ENTRY (combo))
344     {
345       entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo)));
346       new_text = g_strdup (gtk_entry_get_text (entry));
347     }
348   else 
349     {
350       model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo));
351
352       if (model
353           && gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter))
354         gtk_tree_model_get (model, &iter, cell->text_column, &new_text, -1);
355     }
356
357   path = g_object_get_data (G_OBJECT (combo), GTK_CELL_RENDERER_COMBO_PATH);
358   g_signal_emit_by_name (cell, "edited", path, new_text);
359
360   priv->combo = NULL;
361
362   g_free (new_text);
363 }
364
365 static gboolean
366 gtk_cell_renderer_combo_focus_out_event (GtkWidget *widget,
367                                          GdkEvent  *event,
368                                          gpointer   data)
369 {
370   
371   gtk_cell_renderer_combo_editing_done (GTK_CELL_EDITABLE (widget), data);
372
373   return FALSE;
374 }
375
376 typedef struct 
377 {
378   GtkCellRendererCombo *cell;
379   gboolean found;
380   GtkTreeIter iter;
381 } SearchData;
382
383 static gboolean 
384 find_text (GtkTreeModel *model, 
385            GtkTreePath  *path, 
386            GtkTreeIter  *iter, 
387            gpointer      data)
388 {
389   SearchData *search_data = (SearchData *)data;
390   gchar *text;
391   
392   gtk_tree_model_get (model, iter, search_data->cell->text_column, &text, -1);
393   if (text && GTK_CELL_RENDERER_TEXT (search_data->cell)->text &&
394       strcmp (text, GTK_CELL_RENDERER_TEXT (search_data->cell)->text) == 0)
395     {
396       search_data->iter = *iter;
397       search_data->found = TRUE;
398     }
399
400   g_free (text);
401   
402   return search_data->found;
403 }
404
405 static GtkCellEditable *
406 gtk_cell_renderer_combo_start_editing (GtkCellRenderer     *cell,
407                                        GdkEvent            *event,
408                                        GtkWidget           *widget,
409                                        const gchar         *path,
410                                        GdkRectangle        *background_area,
411                                        GdkRectangle        *cell_area,
412                                        GtkCellRendererState flags)
413 {
414   GtkCellRendererCombo *cell_combo;
415   GtkCellRendererText *cell_text;
416   GtkWidget *combo;
417   SearchData search_data;
418   GtkCellRendererComboPrivate *priv;
419
420   cell_text = GTK_CELL_RENDERER_TEXT (cell);
421   if (cell_text->editable == FALSE)
422     return NULL;
423
424   cell_combo = GTK_CELL_RENDERER_COMBO (cell);
425   if (cell_combo->text_column < 0)
426     return NULL;
427
428   priv = GTK_CELL_RENDERER_COMBO_GET_PRIVATE (cell_combo);
429
430   if (cell_combo->has_entry) 
431     {
432       combo = gtk_combo_box_entry_new ();
433
434       if (cell_combo->model)
435         gtk_combo_box_set_model (GTK_COMBO_BOX (combo), cell_combo->model);
436       gtk_combo_box_entry_set_text_column (GTK_COMBO_BOX_ENTRY (combo),
437                                            cell_combo->text_column);
438
439       if (cell_text->text)
440         gtk_entry_set_text (GTK_ENTRY (GTK_BIN (combo)->child), 
441                             cell_text->text);
442     }
443   else
444     {
445       cell = gtk_cell_renderer_text_new ();
446
447       combo = gtk_combo_box_new ();
448       if (cell_combo->model)
449         gtk_combo_box_set_model (GTK_COMBO_BOX (combo), cell_combo->model);
450
451       gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
452       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), 
453                                       cell, "text", cell_combo->text_column, 
454                                       NULL);
455
456       /* determine the current value */
457       if (cell_combo->model)
458         {
459           search_data.cell = cell_combo;
460           search_data.found = FALSE;
461           gtk_tree_model_foreach (cell_combo->model, find_text, &search_data);
462           if (search_data.found)
463             gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combo),
464                                            &(search_data.iter));
465         }
466     }
467
468   g_object_set (combo, "has-frame", FALSE, NULL);
469   g_object_set_data_full (G_OBJECT (combo),
470                           I_(GTK_CELL_RENDERER_COMBO_PATH),
471                           g_strdup (path), g_free);
472
473   gtk_widget_show (combo);
474
475   g_signal_connect (GTK_CELL_EDITABLE (combo), "editing-done",
476                     G_CALLBACK (gtk_cell_renderer_combo_editing_done),
477                     cell_combo);
478   g_signal_connect (GTK_CELL_EDITABLE (combo), "changed",
479                     G_CALLBACK (gtk_cell_renderer_combo_changed),
480                     cell_combo);
481   cell_combo->focus_out_id = 
482     g_signal_connect (combo, "focus-out-event",
483                       G_CALLBACK (gtk_cell_renderer_combo_focus_out_event),
484                       cell_combo);
485
486   priv->combo = combo;
487
488   return GTK_CELL_EDITABLE (combo);
489 }
490
491 #define __GTK_CELL_RENDERER_COMBO_C__
492 #include "gtkaliasdef.c"