* Added some unit tests for ModestTextUtils
[modest] / src / widgets / modest-combo-box.c
index 3514f38..7d4e6e8 100644 (file)
@@ -1,9 +1,36 @@
-/* modest-combo-box.c */
-
-/* insert (c)/licensing information) */
-
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <gtk/gtkcelllayout.h>
+#include <gtk/gtkcellrenderertext.h>
+#include <gtk/gtkliststore.h>
 #include "modest-combo-box.h"
-/* include other impl specific header files */
 
 /* 'private'/'protected' functions */
 static void modest_combo_box_class_init (ModestComboBoxClass *klass);
@@ -16,13 +43,13 @@ enum {
        LAST_SIGNAL
 };
 
+
 enum {
        COLUMN_ID,
        COLUMN_DISPLAY_NAME,
        COLUMN_NUM
 };
 
-
 typedef struct _ModestComboBoxPrivate ModestComboBoxPrivate;
 struct _ModestComboBoxPrivate {
        /* my private members go here, eg. */
@@ -32,7 +59,7 @@ struct _ModestComboBoxPrivate {
                                               MODEST_TYPE_COMBO_BOX, \
                                               ModestComboBoxPrivate))
 /* globals */
-static GtkComboBoxClass *parent_class = NULL;
+static GObjectClass *parent_class = NULL;
 
 /* uncomment the following if you have defined any signals */
 /* static guint signals[LAST_SIGNAL] = {0}; */
@@ -97,72 +124,98 @@ modest_combo_box_finalize (GObject *obj)
        G_OBJECT_CLASS(parent_class)->finalize (obj);
 }
 
-
 static GtkTreeModel*
-get_model (ModestComboBoxLemma *lemmas)
+get_model (const GSList *pairs)
 {
        GtkTreeIter iter;
        GtkListStore *store;
-       ModestComboBoxLemma *lemma;
-       
-       if (!lemmas)
-               return NULL; /* not an error */
+       const GSList *cursor;
        
        store = gtk_list_store_new (2,
-                                   G_TYPE_STRING,   /* the display name */
-                                   G_TYPE_POINTER); /* the id */
-       
-       for (lemma = lemmas; lemma; ++lemma)
+                                   G_TYPE_POINTER, /* the id */
+                                   G_TYPE_STRING); /* the display name */
+       cursor = pairs;
+       while (cursor) {
+               ModestPair *pair = (ModestPair*)cursor->data;
                gtk_list_store_insert_with_values (store, &iter, G_MAXINT,
-                                                  /* FIXME: g_strdup?*/
-                                                  COLUMN_DISPLAY_NAME, lemma->display_name, 
-                                                  COLUMN_ID,           lemma->id,
+                                                  COLUMN_ID,           pair->first,
+                                                  COLUMN_DISPLAY_NAME, pair->second,
                                                   -1);
+               cursor = cursor->next;
+       }
+       
        return GTK_TREE_MODEL (store);
 }
 
 
 GtkWidget*
-modest_combo_box_new (ModestComboBoxLemma *lemmas)
+modest_combo_box_new (const GSList *pairs)
 {
-       GObject *obj;
        GtkTreeModel *model;
-       
-       obj = g_object_new(MODEST_TYPE_COMBO_BOX, NULL);
-       
-       model = get_model (lemmas);
+       GtkCellRenderer *renderer;
+       GObject *obj;
 
-       gtk_combo_box_set_model (GTK_COMBO_BOX(obj), model);
-       g_object_unref (model);
+       obj  = G_OBJECT(g_object_new(MODEST_TYPE_COMBO_BOX, NULL));
+               
+       model = get_model (pairs);
 
-       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(obj),
-                                       gtk_cell_renderer_text_new(),
-                                       "text", COLUMN_DISPLAY_NAME,
-                                       NULL);
+       if (model) {
+               gtk_combo_box_set_model (GTK_COMBO_BOX(obj), model);
+               g_object_unref (model);
+               gtk_cell_layout_clear (GTK_CELL_LAYOUT(obj));
+               
+               renderer = gtk_cell_renderer_text_new ();
+               gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(obj),
+                                           renderer, FALSE);  
+               gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(obj),
+                                               renderer, "text",
+                                               COLUMN_DISPLAY_NAME, NULL); 
+       }
+       
        return GTK_WIDGET(obj);
 }
 
-gpointer
-modest_combo_box_get_active_id (ModestComboBox *self)
+
+
+static void
+get_active (ModestComboBox *self, GValue *val, gint column)
 {
        GtkTreeIter iter;
-       gpointer retval;
        
-       g_return_val_if_fail (self, NULL);
+       g_return_if_fail (self);
 
-       if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX(self), &iter))
-               retval = NULL; /* nothing found */
-       else {
+       if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX(self), &iter)) {
                GtkTreeModel *model;
-               GValue val;
                
                model = gtk_combo_box_get_model (GTK_COMBO_BOX(self));
+               gtk_tree_model_get_value (model, &iter, column, val);
+       }
+}
 
-               g_value_init (&val, G_TYPE_POINTER);
-               gtk_tree_model_get_value (model, &iter, COLUMN_ID, &val);
+gpointer
+modest_combo_box_get_active_id (ModestComboBox *self)
+{
+       g_return_val_if_fail (self, NULL);
+       GValue val = {0,};
+       gpointer retval;
+
+       /* Do not unset the GValue */
+       get_active (self, &val, COLUMN_ID);
+       retval = g_value_peek_pointer (&val);
+
+       return retval;
+}
+
+gpointer
+modest_combo_box_get_active_display_name (ModestComboBox *self)
+{
+       g_return_val_if_fail (self, NULL);
+       GValue val = {0,};
+       gpointer retval;
+
+       /* Do not unset the GValue */
+       get_active (self, &val, COLUMN_DISPLAY_NAME);
+       retval = g_value_peek_pointer (&val);
 
-               retval = g_value_get_pointer (&val);
-               g_value_unset (&val);
-       }
        return retval;
 }