From 5394ef9c1b12052f3b7310e6bb64757660d6de4d Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Tue, 29 Apr 2008 10:33:22 +0000 Subject: [PATCH] Modify cursor when adding/removing thousands separators (Bug #527669). svn path=/trunk/; revision=2087 --- ChangeLog | 2 ++ gcalctool/display.c | 36 ++++++++++++++++++++++++++++-------- gcalctool/display.h | 2 +- gcalctool/gtk.c | 4 ++-- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1f829278..318d26da 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,8 @@ gcalctool change history. * Restructure display.[ch] to have consistent function names and work towards routing all display code through one point to support Unicode more easily (Bug #530532). + * gcalctool/display.[ch]: + gcalctool/gtk.c: Modify cursor when adding/removing thousands separators (Bug #527669). 2008-04-20 Robert Ancell diff --git a/gcalctool/display.c b/gcalctool/display.c index 4de5c72e..d09fccb0 100644 --- a/gcalctool/display.c +++ b/gcalctool/display.c @@ -43,25 +43,36 @@ static double max_fix[MAXBASES] = { * 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 @@ localize_expression(char *dest, const char *src, int dest_length) /* 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 @@ localize_expression(char *dest, const char *src, int dest_length) /* 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 @@ localize_expression(char *dest, const char *src, int dest_length) 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 @@ display_refresh(int cursor) /* 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. */ diff --git a/gcalctool/display.h b/gcalctool/display.h index 71571617..55714422 100644 --- a/gcalctool/display.h +++ b/gcalctool/display.h @@ -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); diff --git a/gcalctool/gtk.c b/gcalctool/gtk.c index 07767570..36f23287 100644 --- a/gcalctool/gtk.c +++ b/gcalctool/gtk.c @@ -957,8 +957,8 @@ ui_set_display(char *str, int cursor) 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; } } -- 2.11.4.GIT