]> Pileus Git - ~andy/gtk/blob - gtk/gtkcomboboxentry.c
GtkToolPalette: Change gtk_tool_palette_get_drop_group() return.
[~andy/gtk] / gtk / gtkcomboboxentry.c
1 /* gtkcomboboxentry.c
2  * Copyright (C) 2002, 2003  Kristian Rietveld <kris@gtk.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 #include "gtkcomboboxentry.h"
23 #include "gtkcelllayout.h"
24
25 #include "gtkentry.h"
26 #include "gtkcellrenderertext.h"
27
28 #include "gtkprivate.h"
29 #include "gtkintl.h"
30 #include "gtkbuildable.h"
31 #include "gtkalias.h"
32
33 #define GTK_COMBO_BOX_ENTRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_COMBO_BOX_ENTRY, GtkComboBoxEntryPrivate))
34
35 struct _GtkComboBoxEntryPrivate
36 {
37   GtkCellRenderer *text_renderer;
38   gint text_column;
39 };
40
41 static void gtk_combo_box_entry_set_property     (GObject               *object,
42                                                   guint                  prop_id,
43                                                   const GValue          *value,
44                                                   GParamSpec            *pspec);
45 static void gtk_combo_box_entry_get_property     (GObject               *object,
46                                                   guint                  prop_id,
47                                                   GValue                *value,
48                                                   GParamSpec            *pspec);
49 static void gtk_combo_box_entry_add              (GtkContainer          *container,
50                                                   GtkWidget             *child);
51 static void gtk_combo_box_entry_remove           (GtkContainer          *container,
52                                                   GtkWidget             *child);
53
54 static gchar *gtk_combo_box_entry_get_active_text (GtkComboBox *combo_box);
55 static void gtk_combo_box_entry_active_changed   (GtkComboBox           *combo_box,
56                                                   gpointer               user_data);
57 static void gtk_combo_box_entry_contents_changed (GtkEntry              *entry,
58                                                   gpointer               user_data);
59 static gboolean gtk_combo_box_entry_mnemonic_activate (GtkWidget        *entry,
60                                                        gboolean          group_cycling);
61 static void gtk_combo_box_entry_grab_focus       (GtkWidget *widget);
62 static void has_frame_changed                    (GtkComboBoxEntry      *entry_box,
63                                                   GParamSpec            *pspec,
64                                                   gpointer               data);
65 static void gtk_combo_box_entry_buildable_interface_init     (GtkBuildableIface *iface);
66 static GObject * gtk_combo_box_entry_buildable_get_internal_child (GtkBuildable *buildable,
67                                                      GtkBuilder   *builder,
68                                                      const gchar  *childname);
69
70 enum
71 {
72   PROP_0,
73   PROP_TEXT_COLUMN
74 };
75
76 G_DEFINE_TYPE_WITH_CODE (GtkComboBoxEntry, gtk_combo_box_entry, GTK_TYPE_COMBO_BOX,
77                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
78                                                 gtk_combo_box_entry_buildable_interface_init))
79
80 static void
81 gtk_combo_box_entry_class_init (GtkComboBoxEntryClass *klass)
82 {
83   GObjectClass *object_class;
84   GtkWidgetClass *widget_class;
85   GtkContainerClass *container_class;
86   GtkComboBoxClass *combo_class;
87   
88   object_class = (GObjectClass *)klass;
89   object_class->set_property = gtk_combo_box_entry_set_property;
90   object_class->get_property = gtk_combo_box_entry_get_property;
91
92   widget_class = (GtkWidgetClass *)klass;
93   widget_class->mnemonic_activate = gtk_combo_box_entry_mnemonic_activate;
94   widget_class->grab_focus = gtk_combo_box_entry_grab_focus;
95
96   container_class = (GtkContainerClass *)klass;
97   container_class->add = gtk_combo_box_entry_add;
98   container_class->remove = gtk_combo_box_entry_remove;
99
100   combo_class = (GtkComboBoxClass *)klass;
101   combo_class->get_active_text = gtk_combo_box_entry_get_active_text;
102   
103   g_object_class_install_property (object_class,
104                                    PROP_TEXT_COLUMN,
105                                    g_param_spec_int ("text-column",
106                                                      P_("Text Column"),
107                                                      P_("A column in the data source model to get the strings from"),
108                                                      -1,
109                                                      G_MAXINT,
110                                                      -1,
111                                                      GTK_PARAM_READWRITE));
112
113   g_type_class_add_private ((GObjectClass *) klass,
114                             sizeof (GtkComboBoxEntryPrivate));
115 }
116
117 static void
118 gtk_combo_box_entry_init (GtkComboBoxEntry *entry_box)
119 {
120   GtkWidget *entry;
121
122   entry_box->priv = GTK_COMBO_BOX_ENTRY_GET_PRIVATE (entry_box);
123   entry_box->priv->text_column = -1;
124
125   entry = gtk_entry_new ();
126   gtk_widget_show (entry);
127   gtk_container_add (GTK_CONTAINER (entry_box), entry);
128
129   entry_box->priv->text_renderer = gtk_cell_renderer_text_new ();
130   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (entry_box),
131                               entry_box->priv->text_renderer, TRUE);
132
133   gtk_combo_box_set_active (GTK_COMBO_BOX (entry_box), -1);
134
135   g_signal_connect (entry_box, "changed",
136                     G_CALLBACK (gtk_combo_box_entry_active_changed), NULL);
137   g_signal_connect (entry_box, "notify::has-frame", G_CALLBACK (has_frame_changed), NULL);
138 }
139
140 static void
141 gtk_combo_box_entry_buildable_interface_init (GtkBuildableIface *iface)
142 {
143   iface->get_internal_child = gtk_combo_box_entry_buildable_get_internal_child;
144 }
145
146 static GObject *
147 gtk_combo_box_entry_buildable_get_internal_child (GtkBuildable *buildable,
148                                                   GtkBuilder   *builder,
149                                                   const gchar  *childname)
150 {
151     if (strcmp (childname, "entry") == 0)
152       return G_OBJECT (gtk_bin_get_child (GTK_BIN (buildable)));
153
154     return NULL;
155 }
156
157 static void
158 gtk_combo_box_entry_set_property (GObject      *object,
159                                   guint         prop_id,
160                                   const GValue *value,
161                                   GParamSpec   *pspec)
162 {
163   GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (object);
164
165   switch (prop_id)
166     {
167       case PROP_TEXT_COLUMN:
168         gtk_combo_box_entry_set_text_column (entry_box,
169                                              g_value_get_int (value));
170         break;
171
172       default:
173         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174         break;
175     }
176 }
177
178 static void
179 gtk_combo_box_entry_get_property (GObject    *object,
180                                   guint       prop_id,
181                                   GValue     *value,
182                                   GParamSpec *pspec)
183 {
184   GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (object);
185
186   switch (prop_id)
187     {
188       case PROP_TEXT_COLUMN:
189         g_value_set_int (value, entry_box->priv->text_column);
190         break;
191
192       default:
193         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194         break;
195     }
196 }
197
198 static void
199 gtk_combo_box_entry_add (GtkContainer *container,
200                          GtkWidget    *child)
201 {
202   GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (container);
203
204   if (!GTK_IS_ENTRY (child))
205     {
206       g_warning ("Attempting to add a widget with type %s to a GtkComboBoxEntry "
207                  "(need an instance of GtkEntry or of a subclass)",
208                  G_OBJECT_TYPE_NAME (child));
209       return;
210     }
211
212   GTK_CONTAINER_CLASS (gtk_combo_box_entry_parent_class)->add (container, child);
213
214   /* this flag is a hack to tell the entry to fill its allocation.
215    */
216   GTK_ENTRY (child)->is_cell_renderer = TRUE;
217
218   g_signal_connect (child, "changed",
219                     G_CALLBACK (gtk_combo_box_entry_contents_changed),
220                     entry_box);
221   has_frame_changed (entry_box, NULL, NULL);
222 }
223
224 static void
225 gtk_combo_box_entry_remove (GtkContainer *container,
226                             GtkWidget    *child)
227 {
228   if (child && child == GTK_BIN (container)->child)
229     {
230       g_signal_handlers_disconnect_by_func (child,
231                                             gtk_combo_box_entry_contents_changed,
232                                             container);
233       GTK_ENTRY (child)->is_cell_renderer = FALSE;
234     }
235
236   GTK_CONTAINER_CLASS (gtk_combo_box_entry_parent_class)->remove (container, child);
237 }
238
239 static void
240 gtk_combo_box_entry_active_changed (GtkComboBox *combo_box,
241                                     gpointer     user_data)
242 {
243   GtkComboBoxEntry *entry_box = GTK_COMBO_BOX_ENTRY (combo_box);
244   GtkTreeModel *model;
245   GtkTreeIter iter;
246   gchar *str = NULL;
247
248   if (gtk_combo_box_get_active_iter (combo_box, &iter))
249     {
250       GtkEntry *entry = GTK_ENTRY (GTK_BIN (combo_box)->child);
251
252       if (entry)
253         {
254           g_signal_handlers_block_by_func (entry,
255                                            gtk_combo_box_entry_contents_changed,
256                                            combo_box);
257
258           model = gtk_combo_box_get_model (combo_box);
259
260           gtk_tree_model_get (model, &iter, 
261                               entry_box->priv->text_column, &str, 
262                               -1);
263           gtk_entry_set_text (entry, str);
264           g_free (str);
265
266           g_signal_handlers_unblock_by_func (entry,
267                                              gtk_combo_box_entry_contents_changed,
268                                              combo_box);
269         }
270     }
271 }
272
273 static void 
274 has_frame_changed (GtkComboBoxEntry *entry_box,
275                    GParamSpec       *pspec,
276                    gpointer          data)
277 {
278   if (GTK_BIN (entry_box)->child)
279     {
280       gboolean has_frame;
281   
282       g_object_get (entry_box, "has-frame", &has_frame, NULL);
283
284       gtk_entry_set_has_frame (GTK_ENTRY (GTK_BIN (entry_box)->child), has_frame);
285     }
286 }
287
288 static void
289 gtk_combo_box_entry_contents_changed (GtkEntry *entry,
290                                       gpointer  user_data)
291 {
292   GtkComboBox *combo_box = GTK_COMBO_BOX (user_data);
293
294   /*
295    *  Fixes regression reported in bug #574059. The old functionality relied on
296    *  bug #572478.  As a bugfix, we now emit the "changed" signal ourselves
297    *  when the selection was already set to -1. 
298    */
299   if (gtk_combo_box_get_active(combo_box) == -1)
300     g_signal_emit_by_name (combo_box, "changed");
301   else 
302     gtk_combo_box_set_active (combo_box, -1);
303 }
304
305 /* public API */
306
307 /**
308  * gtk_combo_box_entry_new:
309  *
310  * Creates a new #GtkComboBoxEntry which has a #GtkEntry as child. After
311  * construction, you should set a model using gtk_combo_box_set_model() and a
312  * text column using gtk_combo_box_entry_set_text_column().
313  *
314  * Return value: A new #GtkComboBoxEntry.
315  *
316  * Since: 2.4
317  */
318 GtkWidget *
319 gtk_combo_box_entry_new (void)
320 {
321   return g_object_new (gtk_combo_box_entry_get_type (), NULL);
322 }
323
324 /**
325  * gtk_combo_box_entry_new_with_model:
326  * @model: A #GtkTreeModel.
327  * @text_column: A column in @model to get the strings from.
328  *
329  * Creates a new #GtkComboBoxEntry which has a #GtkEntry as child and a list
330  * of strings as popup. You can get the #GtkEntry from a #GtkComboBoxEntry
331  * using GTK_ENTRY (GTK_BIN (combo_box_entry)->child). To add and remove
332  * strings from the list, just modify @model using its data manipulation
333  * API.
334  *
335  * Return value: A new #GtkComboBoxEntry.
336  *
337  * Since: 2.4
338  */
339 GtkWidget *
340 gtk_combo_box_entry_new_with_model (GtkTreeModel *model,
341                                     gint          text_column)
342 {
343   GtkWidget *ret;
344
345   g_return_val_if_fail (GTK_IS_TREE_MODEL (model), NULL);
346   g_return_val_if_fail (text_column >= 0, NULL);
347   g_return_val_if_fail (text_column < gtk_tree_model_get_n_columns (model), NULL);
348
349   ret = g_object_new (gtk_combo_box_entry_get_type (),
350                       "model", model,
351                       "text-column", text_column,
352                       NULL);
353
354   return ret;
355 }
356
357 /**
358  * gtk_combo_box_entry_set_text_column:
359  * @entry_box: A #GtkComboBoxEntry.
360  * @text_column: A column in @model to get the strings from.
361  *
362  * Sets the model column which @entry_box should use to get strings from
363  * to be @text_column.
364  *
365  * Since: 2.4
366  */
367 void
368 gtk_combo_box_entry_set_text_column (GtkComboBoxEntry *entry_box,
369                                      gint              text_column)
370 {
371   GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX (entry_box));
372
373   g_return_if_fail (text_column >= 0);
374   g_return_if_fail (model == NULL || text_column < gtk_tree_model_get_n_columns (model));
375
376   entry_box->priv->text_column = text_column;
377
378   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (entry_box),
379                                   entry_box->priv->text_renderer,
380                                   "text", text_column,
381                                   NULL);
382 }
383
384 /**
385  * gtk_combo_box_entry_get_text_column:
386  * @entry_box: A #GtkComboBoxEntry.
387  *
388  * Returns the column which @entry_box is using to get the strings from.
389  *
390  * Return value: A column in the data source model of @entry_box.
391  *
392  * Since: 2.4
393  */
394 gint
395 gtk_combo_box_entry_get_text_column (GtkComboBoxEntry *entry_box)
396 {
397   g_return_val_if_fail (GTK_IS_COMBO_BOX_ENTRY (entry_box), 0);
398
399   return entry_box->priv->text_column;
400 }
401
402 static gboolean
403 gtk_combo_box_entry_mnemonic_activate (GtkWidget *widget,
404                                        gboolean   group_cycling)
405 {
406   GtkBin *entry_box = GTK_BIN (widget);
407
408   if (entry_box->child)
409     gtk_widget_grab_focus (entry_box->child);
410
411   return TRUE;
412 }
413
414 static void
415 gtk_combo_box_entry_grab_focus (GtkWidget *widget)
416 {
417   GtkBin *entry_box = GTK_BIN (widget);
418
419   if (entry_box->child)
420     gtk_widget_grab_focus (entry_box->child);
421 }
422
423
424
425 /* convenience API for simple text combos */
426
427 /**
428  * gtk_combo_box_entry_new_text:
429  *
430  * Convenience function which constructs a new editable text combo box, which 
431  * is a #GtkComboBoxEntry just displaying strings. If you use this function to
432  * create a text combo box, you should only manipulate its data source with
433  * the following convenience functions: gtk_combo_box_append_text(),
434  * gtk_combo_box_insert_text(), gtk_combo_box_prepend_text() and
435  * gtk_combo_box_remove_text().
436  *
437  * Return value: A new text #GtkComboBoxEntry.
438  *
439  * Since: 2.4
440  */
441 GtkWidget *
442 gtk_combo_box_entry_new_text (void)
443 {
444   GtkWidget *entry_box;
445   GtkListStore *store;
446
447   store = gtk_list_store_new (1, G_TYPE_STRING);
448   entry_box = gtk_combo_box_entry_new_with_model (GTK_TREE_MODEL (store), 0);
449   g_object_unref (store);
450
451   return entry_box;
452 }
453
454 static gchar *
455 gtk_combo_box_entry_get_active_text (GtkComboBox *combo_box)
456 {
457   GtkBin *combo = GTK_BIN (combo_box);
458
459   if (combo->child)
460     return g_strdup (gtk_entry_get_text (GTK_ENTRY (combo->child)));
461
462   return NULL;
463 }
464
465 #define __GTK_COMBO_BOX_ENTRY_C__
466 #include "gtkaliasdef.c"