gcalctool r2087 - in trunk: . gcalctool



Author: rancell
Date: Tue Apr 29 10:33:22 2008
New Revision: 2087
URL: http://svn.gnome.org/viewvc/gcalctool?rev=2087&view=rev

Log:
Modify cursor when adding/removing thousands separators (Bug #527669).

Modified:
   trunk/ChangeLog
   trunk/gcalctool/display.c
   trunk/gcalctool/display.h
   trunk/gcalctool/gtk.c

Modified: trunk/gcalctool/display.c
==============================================================================
--- trunk/gcalctool/display.c	(original)
+++ trunk/gcalctool/display.c	Tue Apr 29 10:33:22 2008
@@ -43,25 +43,36 @@
  * currently in the decimal numeric base, use the "right" radix character.
  */
 
+/* Add in the thousand separators characters if required */
 void
-localize_expression(char *dest, const char *src, int dest_length)
+localize_expression(char *dest, const char *src, int dest_length, int *cursor)
 {
     GString *clean, *output;
     const char *c, *d;
-    int digit_count = -1;
+    int digit_count = -1, read_cursor, new_cursor;
     gboolean after_radix = FALSE;
-
+    
     /* Only modify if valid */
     if (v->error || v->base != DEC) {
         STRNCPY(dest, src, dest_length - 1);
         return;
     }
+    
+    if (cursor) {
+        new_cursor = *cursor;
+    } else {
+        new_cursor = -1;
+    }
 
     /* Remove separators if not supported */
     clean = g_string_sized_new(strlen(src));
-    for (c = src; *c; c++) {
+    for (c = src, read_cursor = 1; *c; c++, read_cursor++) {
         if (strncmp(c, v->tsep, strlen(v->tsep)) == 0) {
             c += strlen(v->tsep) - 1;
+            if (new_cursor >= read_cursor) {
+                new_cursor--;
+            }
+            read_cursor--;
         }
         else {
             g_string_append_c(clean, *c);
@@ -76,14 +87,15 @@
 
     /* Scan expression looking for numbers and inserting separators */
     output = g_string_sized_new(dest_length);
-    for (c = clean->str; *c; c++) {
+    for (c = clean->str, read_cursor = 1; *c; c++, read_cursor++) {
         /* Insert separators between digits */
         if (*c >= '0' && *c <= '9') {
             /* Read ahead to find the number of digits */
             if (digit_count < 0) {
                 digit_count = 1;
-                for (d = c + 1; *d >= '0' && *d <= '9'; d++)
+                for (d = c + 1; *d >= '0' && *d <= '9'; d++) {
                     digit_count++;
+                }
             }
             
             g_string_append_c(output, *c);
@@ -91,6 +103,10 @@
             /* Insert separator after nth digit */
             if (!after_radix && digit_count > 1 && digit_count % v->tsep_count == 1) {
                 g_string_append(output, v->tsep);
+                if (new_cursor > read_cursor) {
+                    new_cursor++;
+                }
+                read_cursor++;
             }
             digit_count--;
         }
@@ -111,7 +127,11 @@
     
     STRNCPY(dest, output->str, dest_length - 1);
     g_string_free(output, TRUE);
-    g_string_free(clean, TRUE);    
+    g_string_free(clean, TRUE);
+    
+    if (cursor != NULL && *cursor != -1) {
+        *cursor = new_cursor;
+    }
 }
 
 
@@ -567,7 +587,7 @@
         
             /* Substitute answer register */
             make_number(ans, MAX_LOCALIZED, e->ans, v->base, TRUE);
-            localize_expression(localized, ans, MAX_LOCALIZED);
+            localize_expression(localized, ans, MAX_LOCALIZED, &cursor);
             str = str_replace(str, "Ans", localized);
 
             /* Replace registers with values. */

Modified: trunk/gcalctool/display.h
==============================================================================
--- trunk/gcalctool/display.h	(original)
+++ trunk/gcalctool/display.h	Tue Apr 29 10:33:22 2008
@@ -25,7 +25,7 @@
 #include "calctool.h"
 
 void display_reset();
-void localize_expression(char *, const char *, int);
+void localize_expression(char *, const char *, int, int *);
 void display_clear(int);
 void paren_disp(int);
 void display_refresh(int);

Modified: trunk/gcalctool/gtk.c
==============================================================================
--- trunk/gcalctool/gtk.c	(original)
+++ trunk/gcalctool/gtk.c	Tue Apr 29 10:33:22 2008
@@ -957,8 +957,8 @@
     if (str == NULL || str[0] == '\0') {
         str = " ";
     } else {
-        if (v->ltr.noparens == 0) {
-            localize_expression(localized, str, MAX_LOCALIZED);
+        if (v->syntax == EXPRS || (v->syntax == NPA && v->ltr.noparens == 0)) {
+            localize_expression(localized, str, MAX_LOCALIZED, &cursor);
             str = localized;
         }
     }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]