1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "modest-combo-box.h"
33 /* 'private'/'protected' functions */
34 static void modest_combo_box_class_init (ModestComboBoxClass *klass);
35 static void modest_combo_box_init (ModestComboBox *obj);
36 static void modest_combo_box_finalize (GObject *obj);
51 typedef struct _ModestComboBoxPrivate ModestComboBoxPrivate;
52 struct _ModestComboBoxPrivate {
53 /* my private members go here, eg. */
54 /* gboolean frobnicate_mode; */
56 #define MODEST_COMBO_BOX_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
57 MODEST_TYPE_COMBO_BOX, \
58 ModestComboBoxPrivate))
60 static GObjectClass *parent_class = NULL;
62 /* uncomment the following if you have defined any signals */
63 /* static guint signals[LAST_SIGNAL] = {0}; */
66 modest_combo_box_get_type (void)
68 static GType my_type = 0;
70 static const GTypeInfo my_info = {
71 sizeof(ModestComboBoxClass),
73 NULL, /* base finalize */
74 (GClassInitFunc) modest_combo_box_class_init,
75 NULL, /* class finalize */
76 NULL, /* class data */
77 sizeof(ModestComboBox),
79 (GInstanceInitFunc) modest_combo_box_init,
82 my_type = g_type_register_static (G_TYPE_OBJECT,
90 modest_combo_box_class_init (ModestComboBoxClass *klass)
92 GObjectClass *gobject_class;
93 gobject_class = (GObjectClass*) klass;
95 parent_class = g_type_class_peek_parent (klass);
96 gobject_class->finalize = modest_combo_box_finalize;
98 g_type_class_add_private (gobject_class, sizeof(ModestComboBoxPrivate));
100 /* signal definitions go here, e.g.: */
101 /* signals[MY_SIGNAL_1] = */
102 /* g_signal_new ("my_signal_1",....); */
103 /* signals[MY_SIGNAL_2] = */
104 /* g_signal_new ("my_signal_2",....); */
109 modest_combo_box_init (ModestComboBox *obj)
111 /* uncomment the following if you init any of the private data */
112 /* ModestComboBoxPrivate *priv = MODEST_COMBO_BOX_GET_PRIVATE(obj); */
114 /* initialize this object, eg.: */
115 /* priv->frobnicate_mode = FALSE; */
119 modest_combo_box_finalize (GObject *obj)
121 /* free/unref instance resources here */
122 G_OBJECT_CLASS(parent_class)->finalize (obj);
126 get_model (const GSList *pairs)
130 const GSList *cursor;
132 store = gtk_list_store_new (2,
133 G_TYPE_POINTER, /* the id */
134 G_TYPE_STRING); /* the display name */
137 ModestPair *pair = (ModestPair*)cursor->data;
138 gtk_list_store_insert_with_values (store, &iter, G_MAXINT,
139 COLUMN_ID, pair->first,
140 COLUMN_DISPLAY_NAME, pair->second,
142 cursor = cursor->next;
145 return GTK_TREE_MODEL (store);
150 modest_combo_box_new (const GSList *pairs)
153 GtkCellRenderer *renderer;
156 obj = G_OBJECT(g_object_new(MODEST_TYPE_COMBO_BOX, NULL));
158 model = get_model (pairs);
161 gtk_combo_box_set_model (GTK_COMBO_BOX(obj), model);
162 g_object_unref (model);
163 gtk_cell_layout_clear (GTK_CELL_LAYOUT(obj));
165 renderer = gtk_cell_renderer_text_new ();
166 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(obj),
168 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(obj),
170 COLUMN_DISPLAY_NAME, NULL);
173 return GTK_WIDGET(obj);
179 modest_combo_box_get_active_id (ModestComboBox *self)
184 g_return_val_if_fail (self, NULL);
186 if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX(self), &iter))
187 retval = NULL; /* nothing found */
192 model = gtk_combo_box_get_model (GTK_COMBO_BOX(self));
194 g_value_init (&val, G_TYPE_POINTER);
195 gtk_tree_model_get_value (model, &iter, COLUMN_ID, &val);
197 retval = g_value_get_pointer (&val);
198 g_value_unset (&val);