[Gnucash-changes] Convert from the deprecated GtkCombo widget to a GtkComboBox widget.

David Hampton hampton at cvs.gnucash.org
Sat Sep 17 12:38:57 EDT 2005


Log Message:
-----------
Convert from the deprecated GtkCombo widget to a GtkComboBox widget.

Tags:
----
gnucash-gnome2-dev

Modified Files:
--------------
    gnucash:
        ChangeLog
    gnucash/src/gnome:
        dialog-price-editor.c
    gnucash/src/gnome-utils:
        gnc-currency-edit.c
        gnc-currency-edit.h

Revision Data
-------------
Index: ChangeLog
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/ChangeLog,v
retrieving revision 1.1487.2.286
retrieving revision 1.1487.2.287
diff -LChangeLog -LChangeLog -u -r1.1487.2.286 -r1.1487.2.287
--- ChangeLog
+++ ChangeLog
@@ -1,3 +1,10 @@
+2005-09-17  David Hampton  <hampton at employees.org>
+
+	* src/gnome/dialog-price-editor.c:
+	* src/gnome-utils/gnc-currency-edit.[ch]: Convert from the
+	deprecated GtkCombo widget to a GtkComboBox widget.  Add some
+	documentation.
+
 2005-09-16  David Hampton  <hampton at employees.org>
 
 	* src/gnome/gnc-plugin-page-account-tree.c:
Index: dialog-price-editor.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/gnome/dialog-price-editor.c,v
retrieving revision 1.30.4.6
retrieving revision 1.30.4.7
diff -Lsrc/gnome/dialog-price-editor.c -Lsrc/gnome/dialog-price-editor.c -u -r1.30.4.6 -r1.30.4.7
--- src/gnome/dialog-price-editor.c
+++ src/gnome/dialog-price-editor.c
@@ -373,9 +373,8 @@
   pedit_dialog->currency_edit = w;
   gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
   gtk_widget_show (w);
-  g_signal_connect (G_OBJECT (GTK_COMBO(w)->entry), "changed",
+  g_signal_connect (G_OBJECT (GTK_COMBO_BOX(w)), "changed",
                     G_CALLBACK (currency_changed_cb), pedit_dialog);
-  gtk_entry_set_activates_default(GTK_ENTRY(GTK_COMBO(w)->entry), TRUE);
 
   box = glade_xml_get_widget (xml, "date_box");
   w = gnc_date_edit_new (time (NULL), FALSE, FALSE);
Index: gnc-currency-edit.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/gnome-utils/gnc-currency-edit.h,v
retrieving revision 1.2.6.2
retrieving revision 1.2.6.3
diff -Lsrc/gnome-utils/gnc-currency-edit.h -Lsrc/gnome-utils/gnc-currency-edit.h -u -r1.2.6.2 -r1.2.6.3
--- src/gnome-utils/gnc-currency-edit.h
+++ src/gnome-utils/gnc-currency-edit.h
@@ -24,8 +24,32 @@
  * Boston, MA  02111-1307,  USA       gnu at gnu.org
  *
  */
-/*
-  @NOTATION@
+
+/** @addtogroup GUI
+    @{ */
+/** @addtogroup GncCurrencyEdit
+ * @{ */
+/** @file gnc-currency-edit.h
+ *  @brief Currency selection widget.
+ *  @author Dave Peticolas <dave at krondo.com>
+ *  @author David Hampton <hampton at employees.org>
+ *
+ *  This widget is a GtkComboBox that is wrapped with support
+ *  functions for building/selecting from a list of ISO4217 currency
+ *  names.  All data is maintained within the widget itself, which
+ *  makes the name/item lookup functions somewhat complicated.  The
+ *  alternative coding would be to keep an auxiliary list of strings
+ *  attacked to the widget for lookup purposes, but that would be 100%
+ *  redundant information.
+ *
+ *  When the GtkComboCellEntry widget supports completion, this Gnucash
+ *  widget should be modified so that it is based upon that widget.
+ *  That would give users the capability to select a currency by typing
+ *  its ISO 4217 code (e.g. USD, GBP, etc).  Moving to that widget
+ *  today, however, would cause more problems that its worth.  There is
+ *  currently no way to get access to the embedded GtkEntry widget, and
+ *  therefore no way to implement completion in gnucash or prevent the
+ *  user from typing in random data.
  */
 
 #ifndef GNC_CURRENCY_EDIT_H
@@ -35,30 +59,64 @@
 
 #include "gnc-commodity.h"
 
+/** @name Basic Object Implementation */
+/** @{ */
+
 #define GNC_TYPE_CURRENCY_EDIT	    (gnc_currency_edit_get_type())
 #define GNC_CURRENCY_EDIT(o)	    (G_TYPE_CHECK_INSTANCE_CAST ((o), GNC_TYPE_CURRENCY_EDIT, GNCCurrencyEdit))
 #define GNC_CURRENCY_EDIT_CLASS(k)  (G_TYPE_CHECK_CLASS_CAST ((k), GNC_TYPE_CURRENCY_EDIT, GNCCurrencyEditClass))
 #define GNC_IS_CURRENCY_EDIT(o)	    (G_TYPE_CHECK_INSTANCE_TYPE ((o), GNC_TYPE_CURRENCY_EDIT))
 
 typedef struct {
-        GtkCombo combo;
+        GtkComboBox combobox;
 } GNCCurrencyEdit;
 
 typedef struct {
-        GtkComboClass parent_class;
+        GtkComboBoxClass combobox;
 } GNCCurrencyEditClass;
 
-GType		gnc_currency_edit_get_type	(void);
+/** Return the GType for the GNCCurrencyEdit currency selection widget.
+ *
+ *  @return A GType value.
+ */
+GType gnc_currency_edit_get_type (void);
+
+
+/** Create a new GNCCurrencyEdit widget which can be used to provide
+ *  an easy way to enter ISO currency codes.
+ * 
+ *  @return A GNCCurrencyEdit widget.
+ */
+GtkWidget *gnc_currency_edit_new (void);
+/** @} */
 
-GtkWidget      *gnc_currency_edit_new		(void);
 
-void		gnc_currency_edit_set_currency	(GNCCurrencyEdit *gce,
-						 const gnc_commodity *currency);
+/** @name Get/Set Functions */
 
-gnc_commodity  *gnc_currency_edit_get_currency	(GNCCurrencyEdit *gce);
+
+/** Set the widget to display a certain currency name.
+ *
+ *  @param gce The currency editor widget to set.
+ *
+ *  @param currency The currency to set as the displayed/selected
+ *  value of the widget.
+ */
+void gnc_currency_edit_set_currency (GNCCurrencyEdit *gce, const gnc_commodity *currency);
+
+
+/** Retrieve the displayed currency of the widget.
+ *
+ *  @param gce The currency editor widget whose values should be retrieved.
+ *
+ *  @return A pointer to the selected currency (a gnc_commodity
+ *  structure).
+ */
+gnc_commodity *gnc_currency_edit_get_currency (GNCCurrencyEdit *gce);
 
 #endif
 
+/** @} */
+
 /*
   Local Variables:
   c-basic-offset: 8
Index: gnc-currency-edit.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/gnome-utils/gnc-currency-edit.c,v
retrieving revision 1.4.4.2
retrieving revision 1.4.4.3
diff -Lsrc/gnome-utils/gnc-currency-edit.c -Lsrc/gnome-utils/gnc-currency-edit.c -u -r1.4.4.2 -r1.4.4.3
--- src/gnome-utils/gnc-currency-edit.c
+++ src/gnome-utils/gnc-currency-edit.c
@@ -22,14 +22,37 @@
  * Boston, MA  02111-1307,  USA       gnu at gnu.org
  *
  */
-/*
-  @NOTATION@
- */
 
-/*
- * Currency editor widget
+/** @addtogroup GUI
+    @{ */
+/** @addtogroup GncCurrencyEdit
+ * @{ */
+/** @file gnc-currency-edit.c
+ *  @brief Currency selection widget.
+ *  @author Dave Peticolas <dave at krondo.com>
+ *  @author David Hampton <hampton at employees.org>
+ *
+ *  This widget is a GtkComboBox that is wrapped with support
+ *  functions for building/selecting from a list of ISO4217 currency
+ *  names.  All data is maintained within the widget itself, which
+ *  makes the name/item lookup functions somewhat complicated.  The
+ *  alternative coding would be to keep an auxiliary list of strings
+ *  attacked to the widget for lookup purposes, but that would be 100%
+ *  redundant information.
  *
- * Authors: Dave Peticolas <dave at krondo.com>
+ *  This function currently builds a new GtkListStore for each widget
+ *  created.  It could be optimized to build a single list store and
+ *  share across all extant version of the widget, or even build the
+ *  list store once and maintain for the life of the application.
+ *
+ *  When the GtkComboCellEntry widget supports completion, this Gnucash
+ *  widget should be modified so that it is based upon that widget.
+ *  That would give users the capability to select a currency by typing
+ *  its ISO 4217 code (e.g. USD, GBP, etc).  Moving to that widget
+ *  today, however, would cause more problems that its worth.  There is
+ *  currently no way to get access to the embedded GtkEntry widget, and
+ *  therefore no way to implement completion in gnucash or prevent the
+ *  user from typing in random data.
  */
 
 #include <config.h>
@@ -43,17 +66,15 @@
 #include "gnc-ui-util.h"
 #include "messages.h"
 
-#define MNEMONIC_MAXLEN 10
-
 static void gnc_currency_edit_init         (GNCCurrencyEdit      *gce);
 static void gnc_currency_edit_class_init   (GNCCurrencyEditClass *class);
 
-static GtkComboClass *parent_class;
+static GtkComboBoxClass *parent_class;
 
-/**
- * gnc_currency_edit_get_type:
- *
- * Returns the GType for the GNCCurrencyEdit widget
+/** @name Basic Object Implementation */
+/** @{ */
+
+/*  Return the GType for the GNCCurrencyEdit currency selection widget.
  */
 GType
 gnc_currency_edit_get_type (void)
@@ -74,7 +95,7 @@
 			NULL
 		};
 
-		currency_edit_type = g_type_register_static (GTK_TYPE_COMBO, 
+		currency_edit_type = g_type_register_static (GTK_TYPE_COMBO_BOX, 
 							     "GNCCurrencyEdit",
 							     &currency_edit_info, 0);
 	}
@@ -82,43 +103,67 @@
 	return currency_edit_type;
 }
 
+
+/** Initialize the GncCurrencyEdit class object.
+ *
+ *  @internal
+ *
+ *  @param klass A pointer to the newly created class object.
+ */
 static void
 gnc_currency_edit_class_init (GNCCurrencyEditClass *klass)
 {
 	parent_class = g_type_class_peek_parent (klass);
 }
 
+
+/** Initialize a GncCurrencyEdit object.  This function is currently a
+ *  noop.
+ *
+ *  @internal
+ *
+ *  @param gce A pointer to the newly created object.
+ */
 static void
 gnc_currency_edit_init (GNCCurrencyEdit *gce)
 {
-        gtk_combo_set_use_arrows_always(GTK_COMBO(gce), TRUE);
-        gtk_combo_set_value_in_list(GTK_COMBO(gce), FALSE, TRUE);
-        gtk_combo_disable_activate(GTK_COMBO(gce));
 }
 
+
+/** This auxiliary function adds a single currency name to the combo
+ *  box.  It is called as an iterator function when running a list of
+ *  currencies.
+ *
+ *  @internal
+ *
+ *  @param commodity The currency to add to the selection widget.
+ *
+ *  @param gce A pointer to the selection widget.
+ */
 static void
-add_item(GNCCurrencyEdit *gce, gnc_commodity *commodity)
+add_item(gnc_commodity *commodity, GNCCurrencyEdit *gce)
 {
-        GtkWidget *item;
-        GtkWidget *label;
         const char *string;
 
-        item = gtk_list_item_new();
-
-        string = gnc_commodity_get_printname (commodity);
-
-        label = gtk_label_new(string);
-        gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
-
-        gtk_container_add(GTK_CONTAINER(item), label);
-
-        gtk_widget_show_all(item);
-
-	gtk_combo_set_item_string(GTK_COMBO(gce), GTK_ITEM (item), string);
-
-        gtk_container_add(GTK_CONTAINER(GTK_COMBO(gce)->list), item);
+        string = gnc_commodity_get_printname(commodity);
+	gtk_combo_box_append_text(GTK_COMBO_BOX(gce), string);
 }
 
+
+/** This auxiliary function adds a single currency name to the combo
+ *  box.  It is called as an iterator function when running a list of
+ *  currencies.
+ *
+ *  @internal
+ *
+ *  @param a A pointer to the first currency to compare.
+ *
+ *  @param b A pointer to the second currency to compare.
+ *
+ *  @return This function returns -1 if the first currency should be
+ *  ordered before the second, 0 if the currencies have the same name,
+ *  and +1 if the second currency should be ordered before the first.
+ */
 static int
 currency_compare(gconstpointer a, gconstpointer b)
 {
@@ -126,91 +171,146 @@
                        gnc_commodity_get_printname (b));
 }
 
+
+/** This auxiliary function adds all the currency names to a combo
+ *  box.
+ *
+ *  @internal
+ *
+ *  @param gce A pointer to the widget that should be filled with
+ *  currency names.
+ */
 static void
 fill_currencies(GNCCurrencyEdit *gce)
 {
         GList *currencies;
-        GList *node;
 
         currencies = gnc_commodity_table_get_commodities
                 (gnc_get_current_commodities (), GNC_COMMODITY_NS_ISO);
-
         currencies = g_list_sort(currencies, currency_compare);
-
-        for (node = currencies; node != NULL; node = node->next)
-                add_item(gce, node->data);
-
+	g_list_foreach(currencies, (GFunc)add_item, gce);
         g_list_free(currencies);
 }
 
-/**
- * gnc_currency_edit_new:
- *
- * Creates a new GNCCurrencyEdit widget which can be used to provide
- * an easy way to enter ISO currency codes.
+
+/*  Create a new GNCCurrencyEdit widget which can be used to provide
+ *  an easy way to enter ISO currency codes.
  * 
- * Returns a GNCCurrencyEdit widget.
+ *  @return A GNCCurrencyEdit widget.
  */
 GtkWidget *
 gnc_currency_edit_new (void)
 {
 	GNCCurrencyEdit *gce;
+	GtkListStore *store;
+	GtkCellRenderer *cell;
 
-	gce = g_object_new (GNC_TYPE_CURRENCY_EDIT, NULL, NULL);
+	store = gtk_list_store_new (1, G_TYPE_STRING);
+	gce = g_object_new (GNC_TYPE_CURRENCY_EDIT, "model", store, NULL);
+	g_object_unref (store);
+
+	cell = gtk_cell_renderer_text_new ();
+	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (gce), cell, TRUE);
+	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (gce), cell,
+					"text", 0,
+					NULL);
 
 	fill_currencies (gce);
 
 	return GTK_WIDGET (gce);
 }
 
-/**
- * gnc_currency_edit_set_curreny:
- * @gce: the currency editor widget
- * @currency: the currency to select
+/** @} */
+
+/** @name Get/Set Functions */
+
+/*  Set the widget to display a certain currency name.
  *
- * Sets the currency value of the widget to a particular currency.
+ *  @param gce The currency editor widget to set.
  *
- * Returns nothing.
+ *  @param currency The currency to set as the displayed/selected
+ *  value of the widget.
  */
 void
 gnc_currency_edit_set_currency (GNCCurrencyEdit *gce,
                                 const gnc_commodity *currency)
 {
+	GtkTreeModel *model;
+	GtkTreeIter iter;
+	const gchar *printname, *tree_string;
+	GValue value = { 0 };
+	gint result = 1;
+
         g_return_if_fail(gce != NULL);
         g_return_if_fail(GNC_IS_CURRENCY_EDIT(gce));
         g_return_if_fail(currency != NULL);
+	
+	model = gtk_combo_box_get_model(GTK_COMBO_BOX(gce));
+	if (!gtk_tree_model_get_iter_first(model, &iter)) {
+		/* empty tree */
+		return;
+	}
 
-        gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(gce)->entry),
-                           gnc_commodity_get_printname (currency));
+	printname = gnc_commodity_get_printname(currency);
+	do {
+		gtk_tree_model_get_value(model, &iter, 0, &value);
+		tree_string = g_value_get_string(&value);
+		result = strcmp(printname, tree_string);
+		g_value_unset(&value);
+
+		if (result == 0) {
+		  gtk_combo_box_set_active_iter(GTK_COMBO_BOX(gce), &iter);
+		  return;
+		}
+	} while (gtk_tree_model_iter_next(model, &iter));
 }
 
-/**
- * gnc_currency_edit_get_curreny:
- * @gce: the currency editor widget
+
+/*  Retrieve the displayed currency of the widget.
+ *
+ *  @param gce The currency editor widget whose values should be retrieved.
  *
- * Returns the selected currency.
+ *  @return A pointer to the selected currency (a gnc_commodity
+ *  structure).
  */
 gnc_commodity *
 gnc_currency_edit_get_currency (GNCCurrencyEdit *gce)
 {
 	gnc_commodity *commodity;
-        char *mnemonic, *name;
+        const char *fullname;
+	char *mnemonic, *name;
+	GtkTreeModel *model;
+	GtkTreeIter iter;
+	GValue value = { 0 };
 
         g_return_val_if_fail(gce != NULL, NULL);
         g_return_val_if_fail(GNC_IS_CURRENCY_EDIT(gce), NULL);
 
-        mnemonic = g_strdup(gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(gce)->entry)));
+	if (gtk_combo_box_get_active_iter(GTK_COMBO_BOX(gce), &iter)) {
+		model = gtk_combo_box_get_model(GTK_COMBO_BOX(gce));
+		gtk_tree_model_get_value(model, &iter, 0, &value);
+		fullname = g_value_get_string(&value);
+		mnemonic = g_strdup(fullname);
+		g_value_unset(&value);
+
+		name = index(mnemonic, ' ');
+		if (name != NULL)
+			*name = '\0';
+		commodity = gnc_commodity_table_lookup (gnc_get_current_commodities (),
+							GNC_COMMODITY_NS_ISO,
+							mnemonic);
+		g_free(mnemonic);
+	} else {
+		g_warning("Combo box returned 'inactive'. Using locale default currency.");
+		commodity = gnc_locale_default_currency();
+	}
+
 
-	name = index(mnemonic, ' ');
-	if (name != NULL)
-		*name = '\0';
-        commodity = gnc_commodity_table_lookup (gnc_get_current_commodities (),
-						GNC_COMMODITY_NS_ISO,
-						mnemonic);
-	g_free(mnemonic);
 	return commodity;
 }
 
+/** @} */
+
 /*
   Local Variables:
   c-basic-offset: 8


More information about the gnucash-changes mailing list