* Changes in the autotools stuff affecting a lot of platform dependent
[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 <gtk/gtkliststore.h>
33 #include "modest-combo-box.h"
34
35 /* 'private'/'protected' functions */
36 static void modest_combo_box_class_init (ModestComboBoxClass *klass);
37 static void modest_combo_box_init       (ModestComboBox *obj);
38 static void modest_combo_box_finalize   (GObject *obj);
39 /* list my signals  */
40 enum {
41         /* MY_SIGNAL_1, */
42         /* MY_SIGNAL_2, */
43         LAST_SIGNAL
44 };
45
46
47 enum {
48         COLUMN_ID,
49         COLUMN_DISPLAY_NAME,
50         COLUMN_NUM
51 };
52
53 typedef struct _ModestComboBoxPrivate ModestComboBoxPrivate;
54 struct _ModestComboBoxPrivate {
55         GEqualFunc id_equal_func;
56         
57 };
58 #define MODEST_COMBO_BOX_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
59                                               MODEST_TYPE_COMBO_BOX, \
60                                               ModestComboBoxPrivate))
61 /* globals */
62 static GObjectClass *parent_class = NULL;
63
64 /* uncomment the following if you have defined any signals */
65 /* static guint signals[LAST_SIGNAL] = {0}; */
66
67 GType
68 modest_combo_box_get_type (void)
69 {
70         static GType my_type = 0;
71         if (!my_type) {
72                 static const GTypeInfo my_info = {
73                         sizeof(ModestComboBoxClass),
74                         NULL,           /* base init */
75                         NULL,           /* base finalize */
76                         (GClassInitFunc) modest_combo_box_class_init,
77                         NULL,           /* class finalize */
78                         NULL,           /* class data */
79                         sizeof(ModestComboBox),
80                         1,              /* n_preallocs */
81                         (GInstanceInitFunc) modest_combo_box_init,
82                         NULL
83                 };
84                 my_type = g_type_register_static (GTK_TYPE_COMBO_BOX,
85                                                   "ModestComboBox",
86                                                   &my_info, 0);
87         }
88         return my_type;
89 }
90
91 static void
92 modest_combo_box_class_init (ModestComboBoxClass *klass)
93 {
94         GObjectClass *gobject_class;
95         gobject_class = (GObjectClass*) klass;
96
97         parent_class            = g_type_class_peek_parent (klass);
98         gobject_class->finalize = modest_combo_box_finalize;
99
100         g_type_class_add_private (gobject_class, sizeof(ModestComboBoxPrivate));
101
102         /* signal definitions go here, e.g.: */
103 /*      signals[MY_SIGNAL_1] = */
104 /*              g_signal_new ("my_signal_1",....); */
105 /*      signals[MY_SIGNAL_2] = */
106 /*              g_signal_new ("my_signal_2",....); */
107 /*      etc. */
108 }
109
110 static void
111 modest_combo_box_init (ModestComboBox *obj)
112 {
113 /* uncomment the following if you init any of the private data */
114 /*      ModestComboBoxPrivate *priv = MODEST_COMBO_BOX_GET_PRIVATE(obj); */
115
116 /*      initialize this object, eg.: */
117 /*      priv->frobnicate_mode = FALSE; */
118 }
119
120 static void
121 modest_combo_box_finalize (GObject *obj)
122 {
123 /*      free/unref instance resources here */
124         G_OBJECT_CLASS(parent_class)->finalize (obj);
125 }
126
127 static GtkTreeModel*
128 get_model (ModestPairList *pairs)
129 {
130         GtkTreeIter iter;
131         GtkListStore *store;
132         GSList *cursor;
133         
134         store = gtk_list_store_new (2,
135                                     G_TYPE_POINTER, /* the id */
136                                     G_TYPE_STRING); /* the display name */
137         cursor = pairs;
138         while (cursor) {
139                 ModestPair *pair = (ModestPair*)cursor->data;
140                 gtk_list_store_insert_with_values (store, &iter, G_MAXINT,
141                                                    COLUMN_ID,           pair->first,
142                                                    COLUMN_DISPLAY_NAME, pair->second,
143                                                    -1);
144                 cursor = cursor->next;
145         }
146         
147         return GTK_TREE_MODEL (store);
148 }
149
150 void
151 modest_combo_box_set_pair_list (ModestComboBox *combo, ModestPairList *pairs)
152 {
153         GtkTreeModel *model;
154
155         model = get_model (pairs);
156
157         gtk_combo_box_set_model (GTK_COMBO_BOX(combo), model);
158         g_object_unref (model);
159         
160         gtk_combo_box_set_active (GTK_COMBO_BOX(combo), 0);
161 }
162
163
164
165 GtkWidget*
166 modest_combo_box_new (ModestPairList *pairs, GEqualFunc id_equal_func)
167 {
168         GtkTreeModel *model;
169         GtkCellRenderer *renderer;
170         GObject *obj;
171         ModestComboBoxPrivate *priv;
172
173         obj  = G_OBJECT(g_object_new(MODEST_TYPE_COMBO_BOX, NULL));
174         priv = MODEST_COMBO_BOX_GET_PRIVATE(obj);
175         
176         model = get_model (pairs);
177         if (model) {
178                 gtk_combo_box_set_model (GTK_COMBO_BOX(obj), model);
179                 g_object_unref (model);
180                 gtk_cell_layout_clear (GTK_CELL_LAYOUT(obj));
181                 
182                 renderer = gtk_cell_renderer_text_new ();
183                 g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
184                 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(obj),
185                                             renderer, TRUE);  
186                 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(obj),
187                                                 renderer, "text",
188                                                 COLUMN_DISPLAY_NAME, NULL); 
189         }
190
191         gtk_combo_box_set_active (GTK_COMBO_BOX(obj), 0);
192
193         if (id_equal_func)
194                 priv->id_equal_func = id_equal_func;
195         else
196                 priv->id_equal_func = g_direct_equal; /* compare the ptr values */
197         
198         return GTK_WIDGET(obj);
199 }
200
201
202
203 static void
204 get_active (ModestComboBox *self, GValue *val, gint column)
205 {
206         GtkTreeIter iter;
207         g_return_if_fail (self);
208
209         if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX(self), &iter)) {
210                 GtkTreeModel *model;
211                 
212                 model = gtk_combo_box_get_model (GTK_COMBO_BOX(self));
213                 gtk_tree_model_get_value (model, &iter, column, val);
214         }
215 }
216
217 gpointer
218 modest_combo_box_get_active_id (ModestComboBox *self)
219 {
220         GValue val = {0,};
221
222         g_return_val_if_fail (self, NULL);
223         
224         /* Do not unset the GValue */
225         get_active (self, &val, COLUMN_ID);
226
227         return g_value_get_pointer (&val);
228 }
229
230
231 void
232 modest_combo_box_set_active_id (ModestComboBox *self, gpointer id)
233 {
234         GtkTreeModel *model;
235         GtkTreeIter iter;
236         ModestComboBoxPrivate *priv;
237         gboolean found = FALSE;
238         
239         g_return_if_fail (self);
240
241         priv = MODEST_COMBO_BOX_GET_PRIVATE(self);
242         
243         model = gtk_combo_box_get_model (GTK_COMBO_BOX(self));
244         if (!gtk_tree_model_get_iter_first (model, &iter))
245                 return; /* empty list */
246
247         do {
248                 gpointer row_id;
249                 gtk_tree_model_get (model, &iter, COLUMN_ID, &row_id, -1);
250                 if ((priv->id_equal_func)(id, row_id)) {
251                         gtk_combo_box_set_active_iter (GTK_COMBO_BOX(self), &iter);
252                         found = TRUE;
253                 }
254         } while (!found && gtk_tree_model_iter_next (model, &iter));
255
256         if (!found)
257                 g_printerr ("modest: could not set the active id\n"); 
258 }
259
260
261
262 const gchar*
263 modest_combo_box_get_active_display_name (ModestComboBox *self)
264 {
265         const GValue val = {0,};
266         const gchar *retval;
267
268         g_return_val_if_fail (self, NULL);
269
270         /* Do not unset the GValue */
271         get_active (self, (GValue *)&val, COLUMN_DISPLAY_NAME);
272         retval = g_value_get_string (&val);
273
274         return retval;
275 }