6c9435929214a60dd2c56b7d3afa3bcff9ab383c
[modest] / src / widgets / modest-combo-box.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
16  *
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.
28  */
29
30 #include <gtk/gtkcelllayout.h>
31 #include <gtk/gtkcellrenderertext.h>
32 #include "modest-combo-box.h"
33
34 /* 'private'/'protected' functions */
35 static void modest_combo_box_class_init (ModestComboBoxClass *klass);
36 static void modest_combo_box_init       (ModestComboBox *obj);
37 static void modest_combo_box_finalize   (GObject *obj);
38 /* list my signals  */
39 enum {
40         /* MY_SIGNAL_1, */
41         /* MY_SIGNAL_2, */
42         LAST_SIGNAL
43 };
44
45
46 enum {
47         COLUMN_ID,
48         COLUMN_DISPLAY_NAME,
49         COLUMN_NUM
50 };
51
52 typedef struct _ModestComboBoxPrivate ModestComboBoxPrivate;
53 struct _ModestComboBoxPrivate {
54         /* my private members go here, eg. */
55         /* gboolean frobnicate_mode; */
56 };
57 #define MODEST_COMBO_BOX_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
58                                               MODEST_TYPE_COMBO_BOX, \
59                                               ModestComboBoxPrivate))
60 /* globals */
61 static GObjectClass *parent_class = NULL;
62
63 /* uncomment the following if you have defined any signals */
64 /* static guint signals[LAST_SIGNAL] = {0}; */
65
66 GType
67 modest_combo_box_get_type (void)
68 {
69         static GType my_type = 0;
70         if (!my_type) {
71                 static const GTypeInfo my_info = {
72                         sizeof(ModestComboBoxClass),
73                         NULL,           /* base init */
74                         NULL,           /* base finalize */
75                         (GClassInitFunc) modest_combo_box_class_init,
76                         NULL,           /* class finalize */
77                         NULL,           /* class data */
78                         sizeof(ModestComboBox),
79                         1,              /* n_preallocs */
80                         (GInstanceInitFunc) modest_combo_box_init,
81                         NULL
82                 };
83                 my_type = g_type_register_static (GTK_TYPE_COMBO_BOX,
84                                                   "ModestComboBox",
85                                                   &my_info, 0);
86         }
87         return my_type;
88 }
89
90 static void
91 modest_combo_box_class_init (ModestComboBoxClass *klass)
92 {
93         GObjectClass *gobject_class;
94         gobject_class = (GObjectClass*) klass;
95
96         parent_class            = g_type_class_peek_parent (klass);
97         gobject_class->finalize = modest_combo_box_finalize;
98
99         g_type_class_add_private (gobject_class, sizeof(ModestComboBoxPrivate));
100
101         /* signal definitions go here, e.g.: */
102 /*      signals[MY_SIGNAL_1] = */
103 /*              g_signal_new ("my_signal_1",....); */
104 /*      signals[MY_SIGNAL_2] = */
105 /*              g_signal_new ("my_signal_2",....); */
106 /*      etc. */
107 }
108
109 static void
110 modest_combo_box_init (ModestComboBox *obj)
111 {
112 /* uncomment the following if you init any of the private data */
113 /*      ModestComboBoxPrivate *priv = MODEST_COMBO_BOX_GET_PRIVATE(obj); */
114
115 /*      initialize this object, eg.: */
116 /*      priv->frobnicate_mode = FALSE; */
117 }
118
119 static void
120 modest_combo_box_finalize (GObject *obj)
121 {
122 /*      free/unref instance resources here */
123         G_OBJECT_CLASS(parent_class)->finalize (obj);
124 }
125
126 static GtkTreeModel*
127 get_model (const GSList *pairs)
128 {
129         GtkTreeIter iter;
130         GtkListStore *store;
131         const GSList *cursor;
132         
133         store = gtk_list_store_new (2,
134                                     G_TYPE_POINTER, /* the id */
135                                     G_TYPE_STRING); /* the display name */
136         cursor = pairs;
137         while (cursor) {
138                 ModestPair *pair = (ModestPair*)cursor->data;
139                 gtk_list_store_insert_with_values (store, &iter, G_MAXINT,
140                                                    COLUMN_ID,           pair->first,
141                                                    COLUMN_DISPLAY_NAME, pair->second,
142                                                    -1);
143                 cursor = cursor->next;
144         }
145         
146         return GTK_TREE_MODEL (store);
147 }
148
149
150 GtkWidget*
151 modest_combo_box_new (const GSList *pairs)
152 {
153         GtkTreeModel *model;
154         GtkCellRenderer *renderer;
155         GObject *obj;
156
157         obj  = G_OBJECT(g_object_new(MODEST_TYPE_COMBO_BOX, NULL));
158                 
159         model = get_model (pairs);
160
161         if (model) {
162                 gtk_combo_box_set_model (GTK_COMBO_BOX(obj), model);
163                 g_object_unref (model);
164                 gtk_cell_layout_clear (GTK_CELL_LAYOUT(obj));
165                 
166                 renderer = gtk_cell_renderer_text_new ();
167                 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(obj),
168                                             renderer, FALSE);  
169                 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(obj),
170                                                 renderer, "text",
171                                                 COLUMN_DISPLAY_NAME, NULL); 
172         }
173         
174         return GTK_WIDGET(obj);
175 }
176
177
178
179 static void
180 get_active (ModestComboBox *self, GValue *val, gint column)
181 {
182         GtkTreeIter iter;
183         
184         g_return_if_fail (self);
185
186         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX(self), &iter)) {
187                 GtkTreeModel *model;
188                 
189                 model = gtk_combo_box_get_model (GTK_COMBO_BOX(self));
190                 gtk_tree_model_get_value (model, &iter, column, val);
191         }
192 }
193
194 gpointer
195 modest_combo_box_get_active_id (ModestComboBox *self)
196 {
197         g_return_val_if_fail (self, NULL);
198         GValue val = {0,};
199         gpointer retval;
200
201         /* Do not unset the GValue */
202         get_active (self, &val, COLUMN_ID);
203         retval = g_value_peek_pointer (&val);
204
205         return retval;
206 }
207
208 gpointer
209 modest_combo_box_get_active_display_name (ModestComboBox *self)
210 {
211         g_return_val_if_fail (self, NULL);
212         GValue val = {0,};
213         gpointer retval;
214
215         /* Do not unset the GValue */
216         get_active (self, &val, COLUMN_DISPLAY_NAME);
217         retval = g_value_peek_pointer (&val);
218
219         return retval;
220 }