* added ModestComboBox, convenience class for GtkComboBoxen
authorDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Fri, 11 Aug 2006 12:21:23 +0000 (12:21 +0000)
committerDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Fri, 11 Aug 2006 12:21:23 +0000 (12:21 +0000)
  with a "Friendly Name", referring to some key.

pmo-trunk-r472

src/widgets/Makefile.am
src/widgets/modest-combo-box.c [new file with mode: 0644]
src/widgets/modest-combo-box.h [new file with mode: 0644]

index 86840cb..af48426 100644 (file)
@@ -1,6 +1,6 @@
 #
 # Makefile.am
-# Time-stamp: <2006-08-08 12:22:31 (djcb)>
+# Time-stamp: <2006-08-11 00:24:01 (djcb)>
 
 INCLUDES=\
        $(MODEST_GSTUFF_CFLAGS) \
@@ -23,7 +23,9 @@ libmodest_widgets_la_SOURCES=    \
        modest-msg-view.c        \
        modest-msg-view.h        \
        modest-toolbar.h         \
-       modest-toolbar.c        
+       modest-toolbar.c         \
+       modest-combo-box.c       \
+       modest-combo-box.h
  
 LDADD = \
        $(MODEST_GSTUFF_LIBS) \
diff --git a/src/widgets/modest-combo-box.c b/src/widgets/modest-combo-box.c
new file mode 100644 (file)
index 0000000..3514f38
--- /dev/null
@@ -0,0 +1,168 @@
+/* modest-combo-box.c */
+
+/* insert (c)/licensing information) */
+
+#include "modest-combo-box.h"
+/* include other impl specific header files */
+
+/* 'private'/'protected' functions */
+static void modest_combo_box_class_init (ModestComboBoxClass *klass);
+static void modest_combo_box_init       (ModestComboBox *obj);
+static void modest_combo_box_finalize   (GObject *obj);
+/* list my signals  */
+enum {
+       /* MY_SIGNAL_1, */
+       /* MY_SIGNAL_2, */
+       LAST_SIGNAL
+};
+
+enum {
+       COLUMN_ID,
+       COLUMN_DISPLAY_NAME,
+       COLUMN_NUM
+};
+
+
+typedef struct _ModestComboBoxPrivate ModestComboBoxPrivate;
+struct _ModestComboBoxPrivate {
+       /* my private members go here, eg. */
+       /* gboolean frobnicate_mode; */
+};
+#define MODEST_COMBO_BOX_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
+                                              MODEST_TYPE_COMBO_BOX, \
+                                              ModestComboBoxPrivate))
+/* globals */
+static GtkComboBoxClass *parent_class = NULL;
+
+/* uncomment the following if you have defined any signals */
+/* static guint signals[LAST_SIGNAL] = {0}; */
+
+GType
+modest_combo_box_get_type (void)
+{
+       static GType my_type = 0;
+       if (!my_type) {
+               static const GTypeInfo my_info = {
+                       sizeof(ModestComboBoxClass),
+                       NULL,           /* base init */
+                       NULL,           /* base finalize */
+                       (GClassInitFunc) modest_combo_box_class_init,
+                       NULL,           /* class finalize */
+                       NULL,           /* class data */
+                       sizeof(ModestComboBox),
+                       1,              /* n_preallocs */
+                       (GInstanceInitFunc) modest_combo_box_init,
+                       NULL
+               };
+               my_type = g_type_register_static (GTK_TYPE_COMBO_BOX,
+                                                 "ModestComboBox",
+                                                 &my_info, 0);
+       }
+       return my_type;
+}
+
+static void
+modest_combo_box_class_init (ModestComboBoxClass *klass)
+{
+       GObjectClass *gobject_class;
+       gobject_class = (GObjectClass*) klass;
+
+       parent_class            = g_type_class_peek_parent (klass);
+       gobject_class->finalize = modest_combo_box_finalize;
+
+       g_type_class_add_private (gobject_class, sizeof(ModestComboBoxPrivate));
+
+       /* signal definitions go here, e.g.: */
+/*     signals[MY_SIGNAL_1] = */
+/*             g_signal_new ("my_signal_1",....); */
+/*     signals[MY_SIGNAL_2] = */
+/*             g_signal_new ("my_signal_2",....); */
+/*     etc. */
+}
+
+static void
+modest_combo_box_init (ModestComboBox *obj)
+{
+/* uncomment the following if you init any of the private data */
+/*     ModestComboBoxPrivate *priv = MODEST_COMBO_BOX_GET_PRIVATE(obj); */
+
+/*     initialize this object, eg.: */
+/*     priv->frobnicate_mode = FALSE; */
+}
+
+static void
+modest_combo_box_finalize (GObject *obj)
+{
+/*     free/unref instance resources here */
+       G_OBJECT_CLASS(parent_class)->finalize (obj);
+}
+
+
+static GtkTreeModel*
+get_model (ModestComboBoxLemma *lemmas)
+{
+       GtkTreeIter iter;
+       GtkListStore *store;
+       ModestComboBoxLemma *lemma;
+       
+       if (!lemmas)
+               return NULL; /* not an error */
+       
+       store = gtk_list_store_new (2,
+                                   G_TYPE_STRING,   /* the display name */
+                                   G_TYPE_POINTER); /* the id */
+       
+       for (lemma = lemmas; lemma; ++lemma)
+               gtk_list_store_insert_with_values (store, &iter, G_MAXINT,
+                                                  /* FIXME: g_strdup?*/
+                                                  COLUMN_DISPLAY_NAME, lemma->display_name, 
+                                                  COLUMN_ID,           lemma->id,
+                                                  -1);
+       return GTK_TREE_MODEL (store);
+}
+
+
+GtkWidget*
+modest_combo_box_new (ModestComboBoxLemma *lemmas)
+{
+       GObject *obj;
+       GtkTreeModel *model;
+       
+       obj = g_object_new(MODEST_TYPE_COMBO_BOX, NULL);
+       
+       model = get_model (lemmas);
+
+       gtk_combo_box_set_model (GTK_COMBO_BOX(obj), model);
+       g_object_unref (model);
+
+       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(obj),
+                                       gtk_cell_renderer_text_new(),
+                                       "text", COLUMN_DISPLAY_NAME,
+                                       NULL);
+       return GTK_WIDGET(obj);
+}
+
+gpointer
+modest_combo_box_get_active_id (ModestComboBox *self)
+{
+       GtkTreeIter iter;
+       gpointer retval;
+       
+       g_return_val_if_fail (self, NULL);
+
+       if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX(self), &iter))
+               retval = NULL; /* nothing found */
+       else {
+               GtkTreeModel *model;
+               GValue val;
+               
+               model = gtk_combo_box_get_model (GTK_COMBO_BOX(self));
+
+               g_value_init (&val, G_TYPE_POINTER);
+               gtk_tree_model_get_value (model, &iter, COLUMN_ID, &val);
+
+               retval = g_value_get_pointer (&val);
+               g_value_unset (&val);
+       }
+       return retval;
+}
diff --git a/src/widgets/modest-combo-box.h b/src/widgets/modest-combo-box.h
new file mode 100644 (file)
index 0000000..f43e0eb
--- /dev/null
@@ -0,0 +1,76 @@
+/* modest-combo-box.h */
+/* insert (c)/licensing information) */
+
+#ifndef __MODEST_COMBO_BOX_H__
+#define __MODEST_COMBO_BOX_H__
+
+#include <gtk/gtk.h>
+/* other include files */
+
+G_BEGIN_DECLS
+
+/* convenience macros */
+#define MODEST_TYPE_COMBO_BOX             (modest_combo_box_get_type())
+#define MODEST_COMBO_BOX(obj)             (G_TYPE_CHECK_INSTANCE_CAST((obj),MODEST_TYPE_COMBO_BOX,ModestComboBox))
+#define MODEST_COMBO_BOX_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST((klass),MODEST_TYPE_COMBO_BOX,GtkComboBox))
+#define MODEST_IS_COMBO_BOX(obj)          (G_TYPE_CHECK_INSTANCE_TYPE((obj),MODEST_TYPE_COMBO_BOX))
+#define MODEST_IS_COMBO_BOX_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE((klass),MODEST_TYPE_COMBO_BOX))
+#define MODEST_COMBO_BOX_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj),MODEST_TYPE_COMBO_BOX,ModestComboBoxClass))
+
+typedef struct _ModestComboBox      ModestComboBox;
+typedef struct _ModestComboBoxClass ModestComboBoxClass;
+
+struct _ModestComboBox {
+        GtkComboBox parent;
+       /* insert public members, if any */
+};
+
+struct _ModestComboBoxClass {
+       GtkComboBoxClass parent_class;
+       /* insert signal callback declarations, eg. */
+       /* void (* my_event) (ModestComboBox* obj); */
+};
+
+
+struct _ModestComboBoxLemma {
+       const gchar *display_name;
+       gpointer id;
+};
+typedef struct _ModestComboBoxLemma ModestComboBoxLemma;
+
+/**
+ * modest_combo_box_get_type
+ *
+ * Returns: the id of the ModestComboBox type
+ */
+GType        modest_combo_box_get_type    (void) G_GNUC_CONST;
+
+/**
+ * modest_combo_box_new
+ * @lemmas: a ptr to a NULL terminated list of ModestComboBox lemmas,
+ * each corresponding to a display_name, and the corresponding value
+ * create a new modest combo box,
+ *
+ * create a new modest combo box
+ * 
+ * Returns: a new ModestComboBox instance, or NULL in case of failure
+ */
+GtkWidget*   modest_combo_box_new         (ModestComboBoxLemma *lemmas);
+
+
+/**
+ * modest_combo_box_get_active_id
+ * @self: a valid ModestComboBox instance 
+ * 
+ * get the id for the currently active lemma, or NULL if there's nothing chosen
+ * 
+ * Returns: the id or NULL if there's nothing chosen.
+ */
+gpointer   modest_combo_box_get_active_id       (ModestComboBox *self);
+
+
+
+G_END_DECLS
+
+#endif /* __MODEST_COMBO_BOX_H__ */
+