3514f38b3a4bc9989133351223ba146f89be6b37
[modest] / src / widgets / modest-combo-box.c
1 /* modest-combo-box.c */
2
3 /* insert (c)/licensing information) */
4
5 #include "modest-combo-box.h"
6 /* include other impl specific header files */
7
8 /* 'private'/'protected' functions */
9 static void modest_combo_box_class_init (ModestComboBoxClass *klass);
10 static void modest_combo_box_init       (ModestComboBox *obj);
11 static void modest_combo_box_finalize   (GObject *obj);
12 /* list my signals  */
13 enum {
14         /* MY_SIGNAL_1, */
15         /* MY_SIGNAL_2, */
16         LAST_SIGNAL
17 };
18
19 enum {
20         COLUMN_ID,
21         COLUMN_DISPLAY_NAME,
22         COLUMN_NUM
23 };
24
25
26 typedef struct _ModestComboBoxPrivate ModestComboBoxPrivate;
27 struct _ModestComboBoxPrivate {
28         /* my private members go here, eg. */
29         /* gboolean frobnicate_mode; */
30 };
31 #define MODEST_COMBO_BOX_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
32                                               MODEST_TYPE_COMBO_BOX, \
33                                               ModestComboBoxPrivate))
34 /* globals */
35 static GtkComboBoxClass *parent_class = NULL;
36
37 /* uncomment the following if you have defined any signals */
38 /* static guint signals[LAST_SIGNAL] = {0}; */
39
40 GType
41 modest_combo_box_get_type (void)
42 {
43         static GType my_type = 0;
44         if (!my_type) {
45                 static const GTypeInfo my_info = {
46                         sizeof(ModestComboBoxClass),
47                         NULL,           /* base init */
48                         NULL,           /* base finalize */
49                         (GClassInitFunc) modest_combo_box_class_init,
50                         NULL,           /* class finalize */
51                         NULL,           /* class data */
52                         sizeof(ModestComboBox),
53                         1,              /* n_preallocs */
54                         (GInstanceInitFunc) modest_combo_box_init,
55                         NULL
56                 };
57                 my_type = g_type_register_static (GTK_TYPE_COMBO_BOX,
58                                                   "ModestComboBox",
59                                                   &my_info, 0);
60         }
61         return my_type;
62 }
63
64 static void
65 modest_combo_box_class_init (ModestComboBoxClass *klass)
66 {
67         GObjectClass *gobject_class;
68         gobject_class = (GObjectClass*) klass;
69
70         parent_class            = g_type_class_peek_parent (klass);
71         gobject_class->finalize = modest_combo_box_finalize;
72
73         g_type_class_add_private (gobject_class, sizeof(ModestComboBoxPrivate));
74
75         /* signal definitions go here, e.g.: */
76 /*      signals[MY_SIGNAL_1] = */
77 /*              g_signal_new ("my_signal_1",....); */
78 /*      signals[MY_SIGNAL_2] = */
79 /*              g_signal_new ("my_signal_2",....); */
80 /*      etc. */
81 }
82
83 static void
84 modest_combo_box_init (ModestComboBox *obj)
85 {
86 /* uncomment the following if you init any of the private data */
87 /*      ModestComboBoxPrivate *priv = MODEST_COMBO_BOX_GET_PRIVATE(obj); */
88
89 /*      initialize this object, eg.: */
90 /*      priv->frobnicate_mode = FALSE; */
91 }
92
93 static void
94 modest_combo_box_finalize (GObject *obj)
95 {
96 /*      free/unref instance resources here */
97         G_OBJECT_CLASS(parent_class)->finalize (obj);
98 }
99
100
101 static GtkTreeModel*
102 get_model (ModestComboBoxLemma *lemmas)
103 {
104         GtkTreeIter iter;
105         GtkListStore *store;
106         ModestComboBoxLemma *lemma;
107         
108         if (!lemmas)
109                 return NULL; /* not an error */
110         
111         store = gtk_list_store_new (2,
112                                     G_TYPE_STRING,   /* the display name */
113                                     G_TYPE_POINTER); /* the id */
114         
115         for (lemma = lemmas; lemma; ++lemma)
116                 gtk_list_store_insert_with_values (store, &iter, G_MAXINT,
117                                                    /* FIXME: g_strdup?*/
118                                                    COLUMN_DISPLAY_NAME, lemma->display_name, 
119                                                    COLUMN_ID,           lemma->id,
120                                                    -1);
121         return GTK_TREE_MODEL (store);
122 }
123
124
125 GtkWidget*
126 modest_combo_box_new (ModestComboBoxLemma *lemmas)
127 {
128         GObject *obj;
129         GtkTreeModel *model;
130         
131         obj = g_object_new(MODEST_TYPE_COMBO_BOX, NULL);
132         
133         model = get_model (lemmas);
134
135         gtk_combo_box_set_model (GTK_COMBO_BOX(obj), model);
136         g_object_unref (model);
137
138         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(obj),
139                                         gtk_cell_renderer_text_new(),
140                                         "text", COLUMN_DISPLAY_NAME,
141                                         NULL);
142         return GTK_WIDGET(obj);
143 }
144
145 gpointer
146 modest_combo_box_get_active_id (ModestComboBox *self)
147 {
148         GtkTreeIter iter;
149         gpointer retval;
150         
151         g_return_val_if_fail (self, NULL);
152
153         if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX(self), &iter))
154                 retval = NULL; /* nothing found */
155         else {
156                 GtkTreeModel *model;
157                 GValue val;
158                 
159                 model = gtk_combo_box_get_model (GTK_COMBO_BOX(self));
160
161                 g_value_init (&val, G_TYPE_POINTER);
162                 gtk_tree_model_get_value (model, &iter, COLUMN_ID, &val);
163
164                 retval = g_value_get_pointer (&val);
165                 g_value_unset (&val);
166         }
167         return retval;
168 }