[Gnucash-changes] r13939 - gnucash/trunk - Bug#340875: factor utf8-clean decimal-point insertion and string handling out of the pricecell, and call from the formulacell.

Joshua Sled jsled at cvs.gnucash.org
Sun May 7 14:01:35 EDT 2006


Author: jsled
Date: 2006-05-07 14:01:33 -0400 (Sun, 07 May 2006)
New Revision: 13939
Trac: http://svn.gnucash.org/trac/changeset/13939

Modified:
   gnucash/trunk/ChangeLog
   gnucash/trunk/src/register/register-gnome/formulacell-gnome.c
   gnucash/trunk/src/register/register-gnome/pricecell-gnome.c
   gnucash/trunk/src/register/register-gnome/pricecell-gnome.h
Log:
Bug#340875: factor utf8-clean decimal-point insertion and string handling out of the pricecell, and call from the formulacell.


Modified: gnucash/trunk/ChangeLog
===================================================================
--- gnucash/trunk/ChangeLog	2006-05-07 05:36:07 UTC (rev 13938)
+++ gnucash/trunk/ChangeLog	2006-05-07 18:01:33 UTC (rev 13939)
@@ -1,3 +1,14 @@
+2006-05-07  Joshua Sled  <jsled at asynchronous.org>
+
+	* src/register/register-gnome/formulacell-gnome.c
+	(gnc_formula_cell_direct_update): Call new factored-out logic.
+	Fix Bug#340875 regarding same.
+
+	* src/register/register-gnome/pricecell-gnome.c
+	(gnc_basic_cell_insert_decimal): Factor utf8-safe string copying +
+	decimal-point insertion out.
+	(gnc_price_cell_direct_update): Call new factored-out logic.
+
 2006-05-06  David Hampton  <hampton at employees.org>
 
 	* src/gnome-utils/gnc-tree-view-account.c: Rebuild the column

Modified: gnucash/trunk/src/register/register-gnome/formulacell-gnome.c
===================================================================
--- gnucash/trunk/src/register/register-gnome/formulacell-gnome.c	2006-05-07 05:36:07 UTC (rev 13938)
+++ gnucash/trunk/src/register/register-gnome/formulacell-gnome.c	2006-05-07 18:01:33 UTC (rev 13939)
@@ -38,8 +38,9 @@
 
 #include "formulacell.h"
 #include "formulacell-gnome.h"
+#include "pricecell-gnome.h"
 
-static QofLogModule log_module = GNC_MOD_REGISTER;
+//static QofLogModule log_module = GNC_MOD_REGISTER;
 
 static
 gboolean
@@ -51,13 +52,8 @@
 {
     FormulaCell *cell = (FormulaCell *)bcell;
     GdkEventKey *event = gui_data;
-    char decimal_point;
     struct lconv *lc;
-    GString *newval_gs;
     gboolean is_return;
-    int i;
-    const gchar *c;
-    gunichar uc;
 
     if (event->type != GDK_KEY_PRESS)
         return FALSE;
@@ -66,7 +62,11 @@
 
     is_return = FALSE;
 
-    DEBUG( "direct update" );
+    /* FIXME!! This code is almost identical (except for GDK_KP_Enter
+     * handling) to pricecell-gnome.c:gnc_price_cell_direct_update.  I write
+     * this after fixing a bug where one copy was kept up to date, and the
+     * other not.  So, fix this.
+     */
     switch (event->keyval)
     {
         case GDK_Return:
@@ -93,44 +93,14 @@
             return FALSE;
     }
 
-    if (cell->print_info.monetary)
-        decimal_point = lc->mon_decimal_point[0];
-    else
-        decimal_point = lc->decimal_point[0];
+    gnc_basic_cell_insert_decimal(bcell,
+                                  cell->print_info.monetary
+                                  ? lc->mon_decimal_point[0]
+                                  : lc->decimal_point[0],
+                                  cursor_position,
+                                  start_selection,
+                                  end_selection);
 
-    newval_gs = g_string_new ("");
-    c = bcell->value;
-    i = 0;
-    
-    /* copy original value up to cursor position */
-    while (*c && (i < *cursor_position))
-    {
-        uc = g_utf8_get_char (c);
-        g_string_append_unichar (newval_gs, uc);
-        c = g_utf8_next_char (c);
-        i++;
-    }
-    
-    /* insert the decimal_point at cursor position */
-    g_string_append_c (newval_gs, decimal_point);
-	i++;
-    c = g_utf8_next_char (c);
-    
-    /* copy rest of original value */
-    while (*c)
-    {
-        uc = g_utf8_get_char (c);
-        g_string_append_unichar (newval_gs, uc);
-        c = g_utf8_next_char (c);
-    }
-
-    /* update the cursor position */
-    (*cursor_position)++;
-
-    gnc_basic_cell_set_value_internal( bcell, newval_gs->str );
-
-    g_string_free (newval_gs, TRUE);
-
     return TRUE;
 }
 

Modified: gnucash/trunk/src/register/register-gnome/pricecell-gnome.c
===================================================================
--- gnucash/trunk/src/register/register-gnome/pricecell-gnome.c	2006-05-07 05:36:07 UTC (rev 13938)
+++ gnucash/trunk/src/register/register-gnome/pricecell-gnome.c	2006-05-07 18:01:33 UTC (rev 13939)
@@ -46,12 +46,8 @@
 {
     PriceCell *cell = (PriceCell *) bcell;
     GdkEventKey *event = gui_data;
-    char decimal_point;
     struct lconv *lc;
-    GString *newval_gs;
     gboolean is_return;
-    gint start, end;
-    gchar *buf;
 
     if (event->type != GDK_KEY_PRESS)
 	return FALSE;
@@ -114,39 +110,16 @@
     }
 
     /* This  point is only reached when the KP_Decimal key is pressed. */
-    if (cell->print_info.monetary)
-	decimal_point = lc->mon_decimal_point[0];
-    else
-	decimal_point = lc->decimal_point[0];
+    gnc_basic_cell_insert_decimal(bcell,
+                                  cell->print_info.monetary
+                                  ? lc->mon_decimal_point[0]
+                                  : lc->decimal_point[0],
+                                  cursor_position,
+                                  start_selection,
+                                  end_selection);
 
-    /* allocate space for newval_ptr : oldval + one letter ( the
-       decimal_point ) */
-    newval_gs = g_string_new("");
-
-    start = MIN(*start_selection, *end_selection);
-    end = MAX(*start_selection, *end_selection);
-
-    /* length in bytes, not chars. do not use g_utf8_strlen. */
-    buf = malloc(strlen(bcell->value)+1);
-    memset(buf, 0, strlen(bcell->value)+1);
-    g_utf8_strncpy(buf, bcell->value, start);
-    g_string_append(newval_gs, buf);
-    free(buf);
-
-    g_string_append_unichar(newval_gs, decimal_point);
-
-    buf = g_utf8_offset_to_pointer(bcell->value, end);
-    g_string_append(newval_gs, buf);
-
-    /* update the cursor position */
-    *cursor_position = start + 1;
-
-    gnc_basic_cell_set_value_internal (bcell, newval_gs->str);
-
-    g_string_free (newval_gs, TRUE);
+    cell->need_to_parse = TRUE;
     
-    cell->need_to_parse = TRUE;
-
     return TRUE;
 }
 
@@ -161,3 +134,42 @@
 
   return cell;
 }
+
+void
+gnc_basic_cell_insert_decimal(BasicCell *bcell,
+                              char decimal_point,
+                              int *cursor_position,
+                              int *start_selection,
+                              int *end_selection)
+{
+  GString *newval_gs;
+  gint start, end;
+  gchar *buf;
+
+  /* allocate space for newval_ptr : oldval + one letter ( the
+     decimal_point ) */
+  newval_gs = g_string_new("");
+
+  start = MIN(*start_selection, *end_selection);
+  end = MAX(*start_selection, *end_selection);
+
+  /* length in bytes, not chars. do not use g_utf8_strlen. */
+  buf = malloc(strlen(bcell->value)+1);
+  memset(buf, 0, strlen(bcell->value)+1);
+  g_utf8_strncpy(buf, bcell->value, start);
+  g_string_append(newval_gs, buf);
+  free(buf);
+
+  g_string_append_unichar(newval_gs, decimal_point);
+
+  buf = g_utf8_offset_to_pointer(bcell->value, end);
+  g_string_append(newval_gs, buf);
+
+  /* update the cursor position */
+  *cursor_position = start + 1;
+
+  gnc_basic_cell_set_value_internal (bcell, newval_gs->str);
+
+  g_string_free (newval_gs, TRUE);
+}
+

Modified: gnucash/trunk/src/register/register-gnome/pricecell-gnome.h
===================================================================
--- gnucash/trunk/src/register/register-gnome/pricecell-gnome.h	2006-05-07 05:36:07 UTC (rev 13938)
+++ gnucash/trunk/src/register/register-gnome/pricecell-gnome.h	2006-05-07 18:01:33 UTC (rev 13939)
@@ -28,4 +28,11 @@
 
 BasicCell * gnc_price_cell_gnome_new (void);
 
+/* fixme: find a better home for this. */
+void gnc_basic_cell_insert_decimal(BasicCell *cell,
+                                   char decimal_point,
+                                   int *cursor_position,
+                                   int *start_selection,
+                                   int *end_selection);
+
 #endif



More information about the gnucash-changes mailing list