[Gnucash-changes] r14140 - gnucash/branches/deprecated-cleanup/src - Extract functions that can be applied to a GtkComboBoxEntry to require

David Hampton hampton at cvs.gnucash.org
Sat May 20 01:53:40 EDT 2006


Author: hampton
Date: 2006-05-20 01:53:39 -0400 (Sat, 20 May 2006)
New Revision: 14140
Trac: http://svn.gnucash.org/trac/changeset/14140

Added:
   gnucash/branches/deprecated-cleanup/src/core-utils/gnc-gtk-utils.c
   gnucash/branches/deprecated-cleanup/src/core-utils/gnc-gtk-utils.h
Modified:
   gnucash/branches/deprecated-cleanup/src/core-utils/Makefile.am
   gnucash/branches/deprecated-cleanup/src/gnome-utils/gnc-currency-edit.c
Log:
Extract functions that can be applied to a GtkComboBoxEntry to require
that all user typed entries exist in the embedded GtkTreeModel or else
they are rejected.


Modified: gnucash/branches/deprecated-cleanup/src/core-utils/Makefile.am
===================================================================
--- gnucash/branches/deprecated-cleanup/src/core-utils/Makefile.am	2006-05-20 05:52:51 UTC (rev 14139)
+++ gnucash/branches/deprecated-cleanup/src/core-utils/Makefile.am	2006-05-20 05:53:39 UTC (rev 14140)
@@ -7,7 +7,8 @@
   gnc-gdate-utils.c \
   gnc-gkeyfile-utils.c \
   gnc-glib-utils.c \
-  gnc-gobject-utils.c
+  gnc-gobject-utils.c \
+  gnc-gtk-utils.c
 
 libcore_utils_la_LIBADD = \
   ${top_builddir}/lib/glib26/libgncglib.la \
@@ -31,6 +32,7 @@
   gnc-gkeyfile-utils.h \
   gnc-glib-utils.h \
   gnc-gobject-utils.h \
+  gnc-gtk-utils.h \
   gw-core-utils.h
 
 EXTRA_DIST = ${gwmod_DATA}

Added: gnucash/branches/deprecated-cleanup/src/core-utils/gnc-gtk-utils.c
===================================================================
--- gnucash/branches/deprecated-cleanup/src/core-utils/gnc-gtk-utils.c	2006-05-20 05:52:51 UTC (rev 14139)
+++ gnucash/branches/deprecated-cleanup/src/core-utils/gnc-gtk-utils.c	2006-05-20 05:53:39 UTC (rev 14140)
@@ -0,0 +1,191 @@
+/********************************************************************\
+ * gnc-gtk-utils.c -- utility functions based on glib functions     *
+ * Copyright (C) 2006 David Hampton <hampton at employees.org>         *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+#include "config.h"
+
+#include "gnc-gtk-utils.h"
+
+#define LAST_INDEX "last_index"
+#define CHANGED_ID "changed_id"
+
+
+/** Find an entry in the GtkComboBoxEntry by its text value, and set
+ *  the widget to that value.  This function also records the index of
+ *  that text value for use when the user leaves the widget.
+ *
+ *  @param cbe A pointer to a GtkComboBoxEntry widget.
+ *
+ *  @param text The entry text to find in the model of the combo box
+ *  entry. */
+void
+gnc_cbe_set_by_string(GtkComboBoxEntry *cbe,
+		      const gchar *text)
+{
+  GtkTreeModel *model;
+  GtkTreeIter iter;
+  const gchar *tree_string;
+  gint column, index, id;
+
+  model = gtk_combo_box_get_model(GTK_COMBO_BOX(cbe));
+  if (!gtk_tree_model_get_iter_first(model, &iter)) {
+    /* empty tree */
+    return;
+  }
+
+  column = gtk_combo_box_entry_get_text_column(cbe);
+  do {
+    gtk_tree_model_get(model, &iter, column, &tree_string, -1);
+    if (g_utf8_collate(text, tree_string) != 0)
+      continue;
+
+    /* Found a matching string */
+    id = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(cbe), CHANGED_ID));
+    g_signal_handler_block(cbe, id);
+    gtk_combo_box_set_active_iter(GTK_COMBO_BOX(cbe), &iter);
+    g_signal_handler_unblock(cbe, id);
+
+    index = gtk_combo_box_get_active(GTK_COMBO_BOX(cbe));
+    g_object_set_data(G_OBJECT(cbe), LAST_INDEX, GINT_TO_POINTER(index));
+    return;
+  } while (gtk_tree_model_iter_next(model, &iter));
+}
+
+
+/**  The GtkComboBoxEntry widget has changed its value.  If the widget
+ *   now points to another valid entry string then record the index of
+ *   that string for use when the user leaves the widget.
+ *
+ *   @param widget Unused.
+ *
+ *   @param cbe A pointer to a GtkComboBoxEntry widget. */
+static void
+gnc_cbe_changed_cb (GtkComboBox *widget,
+		    GtkComboBoxEntry *cbe)
+{
+  gint index;
+
+  index = gtk_combo_box_get_active(widget);
+  if (index == -1)
+    return;
+  g_object_set_data(G_OBJECT(cbe), LAST_INDEX, GINT_TO_POINTER(index));
+}
+
+
+/**  The completion attached to currency edit widget has selected a
+ *   match.  This function extracts the completed string from the
+ *   completion code's temporary model, and uses that to set the index
+ *   of that currency name for use when the user leaves the widget.
+ *   This should always point to a valid currency name since the user
+ *   made the selection from a list of currency names.
+ *
+ *   @param completion Unused.
+ *
+ *   @param comp_model A temporary model used by completion code that
+ *   contains only the current matches.
+ *
+ *   @param comp_iter The iter in the completion's temporary model
+ *   that represents the user selected match.
+ *
+ *   @param cbe A pointer to a currency entry widget. */
+static gboolean
+gnc_cbe_match_selected_cb (GtkEntryCompletion *completion,
+			   GtkTreeModel       *comp_model,
+			   GtkTreeIter        *comp_iter,
+			   GtkComboBoxEntry   *cbe)
+{
+  gint column;
+  gchar *text;
+
+  column = gtk_combo_box_entry_get_text_column(cbe);
+  gtk_tree_model_get(comp_model, comp_iter, column, &text, -1);
+  gnc_cbe_set_by_string(cbe, text);
+  return FALSE;
+}
+
+
+/**  The focus left the currency edit widget, so reset the widget to
+ *   its last known good value.  If the widget value contained a valid
+ *   currency then this is a noop.  Otherwise the widget will be reset
+ *   to the last user selected currency.  This latter state will occur
+ *   if the user has typed characters directly into the widget but not
+ *   selected a completion.
+ *
+ *   @param entry Unused.
+ *
+ *   @param event Unused.
+ *
+ *   @param cbe A pointer to a currency entry widget. */
+static gboolean
+gnc_cbe_focus_out_cb (GtkEntry *entry,
+		      GdkEventFocus *event,
+		      GtkComboBoxEntry *cbe)
+{
+  const gchar *text;
+  gint index;
+
+  /* Make a final attempt to match the current text. */
+  text = gtk_entry_get_text(entry);
+  gnc_cbe_set_by_string(cbe, text);
+
+  /* Get the last known index (which may have just been set). */
+  index = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(cbe), LAST_INDEX));
+  gtk_combo_box_set_active(GTK_COMBO_BOX(cbe), index);
+  return FALSE;
+}
+
+void
+gnc_cbe_require_list_item (GtkComboBoxEntry *cbe)
+{
+  GtkEntry *entry;
+  GtkEntryCompletion *completion;
+  GtkTreeModel *model;
+  GtkTreeIter iter;
+  gint index, id;
+
+  entry = GTK_ENTRY(gtk_bin_get_child(GTK_BIN(cbe)));
+  completion = gtk_entry_get_completion(entry);
+
+  /* If an item in the combo box isn't already selected, then force
+   * select the first item. Take care, the combo box may not have been
+   * filled yet.  */
+  index = gtk_combo_box_get_active(GTK_COMBO_BOX(cbe));
+  if (index == -1) {
+    model = gtk_entry_completion_get_model(completion);
+    if (gtk_tree_model_get_iter_first(model, &iter)) {
+      gtk_combo_box_set_active(GTK_COMBO_BOX(cbe), 0);
+      index = 0;
+    }
+  }
+  g_object_set_data(G_OBJECT(cbe), LAST_INDEX, GINT_TO_POINTER(index));
+
+  /* Now the signals to make sure the user can't leave the
+     widget without a valid currency. */
+  id = g_signal_connect(cbe, "changed",
+			G_CALLBACK(gnc_cbe_changed_cb), cbe);
+  g_signal_connect(completion, "match_selected",
+		   G_CALLBACK(gnc_cbe_match_selected_cb), cbe);
+  g_signal_connect(entry, "focus-out-event",
+		   G_CALLBACK(gnc_cbe_focus_out_cb), cbe);
+
+  g_object_set_data(G_OBJECT(cbe), CHANGED_ID, GINT_TO_POINTER(id));
+}

Added: gnucash/branches/deprecated-cleanup/src/core-utils/gnc-gtk-utils.h
===================================================================
--- gnucash/branches/deprecated-cleanup/src/core-utils/gnc-gtk-utils.h	2006-05-20 05:52:51 UTC (rev 14139)
+++ gnucash/branches/deprecated-cleanup/src/core-utils/gnc-gtk-utils.h	2006-05-20 05:53:39 UTC (rev 14140)
@@ -0,0 +1,54 @@
+/********************************************************************\
+ * gnc-gtk-utils.c -- utility functions based on glib functions     *
+ * Copyright (C) 2006 David Hampton <hampton at employees.org>         *
+ *                                                                  *
+ * This program is free software; you can redistribute it and/or    *
+ * modify it under the terms of the GNU General Public License as   *
+ * published by the Free Software Foundation; either version 2 of   *
+ * the License, or (at your option) any later version.              *
+ *                                                                  *
+ * This program is distributed in the hope that it will be useful,  *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
+ * GNU General Public License for more details.                     *
+ *                                                                  *
+ * You should have received a copy of the GNU General Public License*
+ * along with this program; if not, contact:                        *
+ *                                                                  *
+ * Free Software Foundation           Voice:  +1-617-542-5942       *
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org                   *
+ *                                                                  *
+\********************************************************************/
+
+/** @addtogroup Gtk
+    @{ */
+/** @addtogroup Gtk Gtk Utilities
+
+    The API in this file is designed to provide support functions that
+    wrap the base gtk functions and make them easier to use.
+
+    @{ */
+/** @file gnc-gtk-utils.h
+ *  @brief gtk helper routines.
+ *  @author Copyright (C) 2006 David Hampton <hampton at employees.org>
+ */
+
+#ifndef GNC_GTK_UTILS_H
+#define GNC_GTK_UTILS_H
+
+#include <gtk/gtk.h>
+
+/** @name gtk Miscellaneous Functions
+ @{ 
+*/
+
+void gnc_cbe_set_by_string(GtkComboBoxEntry *cbe, const gchar *text);
+void gnc_cbe_require_list_item (GtkComboBoxEntry *cbe);
+
+
+/** @} */
+
+#endif /* GNC_GTK_UTILS_H */
+/** @} */
+/** @} */

Modified: gnucash/branches/deprecated-cleanup/src/gnome-utils/gnc-currency-edit.c
===================================================================
--- gnucash/branches/deprecated-cleanup/src/gnome-utils/gnc-currency-edit.c	2006-05-20 05:52:51 UTC (rev 14139)
+++ gnucash/branches/deprecated-cleanup/src/gnome-utils/gnc-currency-edit.c	2006-05-20 05:53:39 UTC (rev 14140)
@@ -64,6 +64,7 @@
 
 #include "gnc-currency-edit.h"
 #include "gnc-commodity.h"
+#include "gnc-gtk-utils.h"
 #include "gnc-ui-util.h"
 
 static void gnc_currency_edit_init         (GNCCurrencyEdit      *gce);
@@ -74,8 +75,7 @@
 /** The instance private data for a content plugin. */
 typedef struct _GNCCurrencyEditPrivate
 {
-	gint last_index;	/**< Last valid GtkListStore index  */
-	gulong changed_id;	/**< Signal handler id */
+	gint dummy;
 } GNCCurrencyEditPrivate;
 
 #define GET_PRIVATE(o)  \
@@ -139,135 +139,9 @@
 static void
 gnc_currency_edit_init (GNCCurrencyEdit *gce)
 {
-	GNCCurrencyEditPrivate *priv;
-
-	priv = GET_PRIVATE(gce);
-	priv->last_index = -1;
-	priv->changed_id = 0;
 }
 
 
-/** Find an entry in the GtkComboBoxEntry by its text value, and set
- *  the widget to that value.  This function also records the index of
- *  that text value for use when the user leaves the widget.
- *
- *  @param gce A pointer to a currency entry widget.
- *
- *  @param text The entry text to find in the model of the combo box
- *  entry. */
-static void
-gce_set_by_string(GNCCurrencyEdit *gce,
-		  const gchar *text)
-{
-	GNCCurrencyEditPrivate *priv;
-	GtkTreeModel *model;
-	GtkTreeIter iter;
-	GValue value = { 0 };
-	const gchar *tree_string;
-	gint result = 1;
-
-	priv = GET_PRIVATE(gce);
-	model = gtk_combo_box_get_model(GTK_COMBO_BOX(gce));
-	if (!gtk_tree_model_get_iter_first(model, &iter)) {
-		/* empty tree */
-		return;
-	}
-
-	do {
-		gtk_tree_model_get_value(model, &iter, 0, &value);
-		tree_string = g_value_get_string(&value);
-		result = strcmp(text, tree_string);
-		g_value_unset(&value);
-		if (result != 0)
-			continue;
-
-		/* Found a matching string */
-		g_signal_handler_block(gce, priv->changed_id);
-		gtk_combo_box_set_active_iter(GTK_COMBO_BOX(gce), &iter);
-		g_signal_handler_unblock(gce, priv->changed_id);
-		priv->last_index = gtk_combo_box_get_active(GTK_COMBO_BOX(gce));
-		return;
-	} while (gtk_tree_model_iter_next(model, &iter));
-}
-
-
-/**  The currency edit widget has changed its value.  If the widget
- *   now points to another valid currency name then record the index
- *   of that currency name for use when the user leaves the widget.
- *
- *   @param widget Unused.
- *
- *   @param gce A pointer to a currency entry widget. */
-static void
-gnc_changed_cb (GtkComboBox *widget,
-		GNCCurrencyEdit *gce)
-{
-	GNCCurrencyEditPrivate *priv;
-	gint current;
-
-	priv = GET_PRIVATE(gce);
-	current = gtk_combo_box_get_active(widget);
-	if (current == -1)
-		return;
-	priv->last_index = current;
-}
-
-
-/**  The completion attached to currency edit widget has selected a
- *   match.  This function extracts the completed string from the
- *   completion code's temporary model, and uses that to set the index
- *   of that currency name for use when the user leaves the widget.
- *   This should always point to a valid currency name since the user
- *   made the selection from a list of currency names.
- *
- *   @param completion Unused.
- *
- *   @param comp_model A temporary model used by completion code that
- *   contains only the current matches.
- *
- *   @param comp_iter The iter in the completion's temporary model
- *   that represents the user selected match.
- *
- *   @param gce A pointer to a currency entry widget. */
-static gboolean
-gnc_match_selected_cb (GtkEntryCompletion *completion,
-		       GtkTreeModel       *comp_model,
-		       GtkTreeIter        *comp_iter,
-		       GNCCurrencyEdit    *gce)
-{
-	gchar *text;
-
-	gtk_tree_model_get(comp_model, comp_iter, 0, &text, -1);
-	gce_set_by_string(gce, text);
-	return FALSE;
-}
-
-
-/**  The focus left the currency edit widget, so reset the widget to
- *   its last known good value.  If the widget value contained a valid
- *   currency then this is a noop.  Otherwise the widget will be reset
- *   to the last user selected currency.  This latter state will occur
- *   if the user has typed characters directly into the widget but not
- *   selected a completion.
- *
- *   @param entry Unused.
- *
- *   @param event Unused.
- *
- *   @param gce A pointer to a currency entry widget. */
-static gboolean
-gce_focus_out_cb (GtkEntry *entry,
-		  GdkEventFocus *event,
-		  GNCCurrencyEdit *gce)
-{
-	GNCCurrencyEditPrivate *priv;
-
-	priv = GET_PRIVATE(gce);
-	gtk_combo_box_set_active(GTK_COMBO_BOX(gce), priv->last_index);
-	return FALSE;
-}
-
-
 /** 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.
@@ -316,7 +190,6 @@
 GtkWidget *
 gnc_currency_edit_new (void)
 {
-	GNCCurrencyEditPrivate *priv;
 	GNCCurrencyEdit *gce;
 	GtkListStore *store;
 	GtkEntry *entry;
@@ -339,14 +212,7 @@
 
 	/* Now the signals to make sure the user can't leave the
 	   widget without a valid currency. */
-	priv = GET_PRIVATE(gce);
-	priv->changed_id =
-		g_signal_connect(gce, "changed",
-				 G_CALLBACK(gnc_changed_cb), gce);
-	g_signal_connect(completion, "match_selected",
-			 G_CALLBACK(gnc_match_selected_cb), gce);
-	g_signal_connect(entry, "focus-out-event",
-			 G_CALLBACK(gce_focus_out_cb), gce);
+	gnc_cbe_require_list_item(GTK_COMBO_BOX_ENTRY(gce));
 
 	/* Fill in all the data. */
 	fill_currencies (gce);
@@ -378,7 +244,7 @@
         g_return_if_fail(currency != NULL);
 	
 	printname = gnc_commodity_get_printname(currency);
-	gce_set_by_string(gce, printname);
+	gnc_cbe_set_by_string(GTK_COMBO_BOX_ENTRY(gce), printname);
 }
 
 



More information about the gnucash-changes mailing list