* 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"
/* 'private'/'protected' functions */
typedef struct _ModestComboBoxPrivate ModestComboBoxPrivate;
struct _ModestComboBoxPrivate {
- /* my private members go here, eg. */
- /* gboolean frobnicate_mode; */
+ GEqualFunc id_equal_func;
+
};
#define MODEST_COMBO_BOX_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \
MODEST_TYPE_COMBO_BOX, \
(GInstanceInitFunc) modest_combo_box_init,
NULL
};
- my_type = g_type_register_static (G_TYPE_OBJECT,
+ my_type = g_type_register_static (GTK_TYPE_COMBO_BOX,
"ModestComboBox",
&my_info, 0);
}
}
static GtkTreeModel*
-get_model (const GSList *pairs)
+get_model (ModestPairList *pairs)
{
GtkTreeIter iter;
GtkListStore *store;
- const GSList *cursor;
+ GSList *cursor;
store = gtk_list_store_new (2,
G_TYPE_POINTER, /* the id */
return GTK_TREE_MODEL (store);
}
+void
+modest_combo_box_set_pair_list (ModestComboBox *combo, ModestPairList *pairs)
+{
+ GtkTreeModel *model;
+
+ model = get_model (pairs);
+
+ gtk_combo_box_set_model (GTK_COMBO_BOX(combo), model);
+ g_object_unref (model);
+
+ gtk_combo_box_set_active (GTK_COMBO_BOX(combo), 0);
+}
+
+
GtkWidget*
-modest_combo_box_new (const GSList *pairs)
+modest_combo_box_new (ModestPairList *pairs, GEqualFunc id_equal_func)
{
GtkTreeModel *model;
GtkCellRenderer *renderer;
GObject *obj;
+ ModestComboBoxPrivate *priv;
obj = G_OBJECT(g_object_new(MODEST_TYPE_COMBO_BOX, NULL));
-
+ priv = MODEST_COMBO_BOX_GET_PRIVATE(obj);
+
model = get_model (pairs);
-
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 ();
+ g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
gtk_cell_layout_pack_start (GTK_CELL_LAYOUT(obj),
- renderer, FALSE);
+ renderer, TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT(obj),
renderer, "text",
COLUMN_DISPLAY_NAME, NULL);
}
+
+ gtk_combo_box_set_active (GTK_COMBO_BOX(obj), 0);
+
+ if (id_equal_func)
+ priv->id_equal_func = id_equal_func;
+ else
+ priv->id_equal_func = g_direct_equal; /* compare the ptr values */
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);
+ }
+}
+
+gpointer
+modest_combo_box_get_active_id (ModestComboBox *self)
+{
+ GValue val = {0,};
- g_value_init (&val, G_TYPE_POINTER);
- gtk_tree_model_get_value (model, &iter, COLUMN_ID, &val);
+ g_return_val_if_fail (self, NULL);
+
+ /* Do not unset the GValue */
+ get_active (self, &val, COLUMN_ID);
+
+ return g_value_get_pointer (&val);
+}
+
+
+void
+modest_combo_box_set_active_id (ModestComboBox *self, gpointer id)
+{
+ GtkTreeModel *model;
+ GtkTreeIter iter;
+ ModestComboBoxPrivate *priv;
+ gboolean found = FALSE;
+
+ g_return_if_fail (self);
+
+ priv = MODEST_COMBO_BOX_GET_PRIVATE(self);
+
+ model = gtk_combo_box_get_model (GTK_COMBO_BOX(self));
+ if (!gtk_tree_model_get_iter_first (model, &iter))
+ return; /* empty list */
+
+ do {
+ gpointer row_id;
+ gtk_tree_model_get (model, &iter, COLUMN_ID, &row_id, -1);
+ if ((priv->id_equal_func)(id, row_id)) {
+ gtk_combo_box_set_active_iter (GTK_COMBO_BOX(self), &iter);
+ found = TRUE;
+ }
+ } while (!found && gtk_tree_model_iter_next (model, &iter));
+
+ if (!found)
+ g_printerr ("modest: could not set the active id\n");
+}
+
+
+
+const gchar*
+modest_combo_box_get_active_display_name (ModestComboBox *self)
+{
+ const GValue val = {0,};
+ const gchar *retval;
+
+ g_return_val_if_fail (self, NULL);
+
+ /* Do not unset the GValue */
+ get_active (self, (GValue *)&val, COLUMN_DISPLAY_NAME);
+ retval = g_value_get_string (&val);
- retval = g_value_get_pointer (&val);
- g_value_unset (&val);
- }
return retval;
}