[gcalctool] Moved paste and insert character code operations out of gtk.c (Bug 500995)



commit 497a3fe51efbcd3c69e3d4445170fff0cbf0ba43
Author: Robert Ancell <robert ancell gmail com>
Date:   Mon May 4 09:34:20 2009 +1000

    Moved paste and insert character code operations out of gtk.c (Bug 500995)
---
 gcalctool/display.c   |   12 ++++++
 gcalctool/display.h   |    2 +
 gcalctool/functions.c |  103 +++++++++++++++++++++++++++++++++++++++++++++----
 gcalctool/functions.h |    2 +
 gcalctool/gtk.c       |   90 +++++-------------------------------------
 5 files changed, 122 insertions(+), 87 deletions(-)

diff --git a/gcalctool/display.c b/gcalctool/display.c
index a47e42b..344b648 100644
--- a/gcalctool/display.c
+++ b/gcalctool/display.c
@@ -440,6 +440,12 @@ display_insert(GCDisplay *display, int cursor, const char *text)
 }
 
 void
+display_insert_at_cursor(GCDisplay *display, const char *text)
+{
+    display_insert(display, display_get_cursor(display), text);
+}
+
+void
 display_insert_number(GCDisplay *display, int cursor, const MPNumber *value)
 {
     char text[MAX_DISPLAY];
@@ -447,6 +453,12 @@ display_insert_number(GCDisplay *display, int cursor, const MPNumber *value)
     display_insert(display, cursor, text);
 }
 
+void
+display_insert_number_at_cursor(GCDisplay *display, const MPNumber *value)
+{
+    display_insert_number(display, display_get_cursor(display), value);
+}
+
 
 void
 display_backspace(GCDisplay *display)
diff --git a/gcalctool/display.h b/gcalctool/display.h
index 18298b2..d956ff1 100644
--- a/gcalctool/display.h
+++ b/gcalctool/display.h
@@ -82,7 +82,9 @@ void display_unpop(GCDisplay *);
 gboolean display_is_undo_step(GCDisplay *display);
 
 void display_insert(GCDisplay *display, int, const char *);
+void display_insert_at_cursor(GCDisplay *display, const char *);
 void display_insert_number(GCDisplay *display, int, const MPNumber *);
+void display_insert_number_at_cursor(GCDisplay *display, const MPNumber *value);
 void display_backspace(GCDisplay *);
 void display_delete(GCDisplay *);
 void display_surround(GCDisplay *display, const char *, const char *);
diff --git a/gcalctool/functions.c b/gcalctool/functions.c
index 1cf58f9..3ce3885 100644
--- a/gcalctool/functions.c
+++ b/gcalctool/functions.c
@@ -141,7 +141,9 @@ static Function functions[NFUNCTIONS] = {
 { FN_UNDO,              NULL, 0 },
 { FN_REDO,              NULL, 0 },
 { FN_CONSTANT,          NULL, 0 },
-{ FN_FUNCTION,          NULL, 0 }
+{ FN_FUNCTION,          NULL, 0 },
+{ FN_PASTE,             NULL, 0 },
+{ FN_INSERT_CHARACTER,  NULL, 0 }
 };
 
 static void
@@ -151,8 +153,9 @@ clear_undo_history(void)
 }
 
 
+/* Set display accuracy. */
 static void
-do_accuracy(int value)     /* Set display accuracy. */
+do_accuracy(int value)
 {
     v->accuracy = value;
     set_int_resource(R_ACCURACY, v->accuracy);
@@ -163,8 +166,9 @@ do_accuracy(int value)     /* Set display accuracy. */
 }
 
 
+/* Perform a user defined function. */
 static void
-do_function(int index)      /* Perform a user defined function. */
+do_function(int index)
 {
     int ret;
 
@@ -181,8 +185,83 @@ do_function(int index)      /* Perform a user defined function. */
     }
 }
 
+
 static void
-do_shift(int count)     /* Perform bitwise shift on display value. */
+do_paste(const char *text)
+{
+    const char *input;
+    char c, *output, *clean_text;
+
+    /* Copy input to modify, no operation can make the clean string longer than
+     * the original string */
+    clean_text = strdup(text);
+    
+    output = clean_text;
+    for (input = text; *input; input++) {
+        /* If the clipboard buffer contains any occurances of the "thousands
+         * separator", remove them.
+         */
+        if (v->tsep[0] != '\0' && strncmp(input, v->tsep, strlen(v->tsep)) == 0) {
+            input += strlen(v->tsep) - 1;
+            continue;
+        }
+        
+        /* Replace radix with "." */
+        else if (strncmp(input, v->radix, strlen(v->radix)) == 0) {
+            input += strlen(v->radix) - 1;
+            c = '.';
+        }
+
+        /* Replace tabs with spaces */        
+        else if (*input == '\t') {
+            c = ' ';
+        }
+        
+        /* Terminate on newlines */        
+        else if (*input == '\r' || *input == '\n') {
+            c = '\0';
+        }
+        
+        /* If an "A", "B", "C", "D" or "F" character is encountered, it 
+         * will be converted to its lowercase equivalent. If an "E" is 
+         * found,  and the next character is a "-" or a "+", then it 
+         * remains as an upper case "E" (it's assumed to be a possible 
+         * exponential number), otherwise its converted to a lower case 
+         * "e". See bugs #455889 and #469245 for more details.
+         */
+        else if (*input >= 'A' && *input <= 'F') {
+            c = *input;
+            if (*input == 'E') {
+                if (*(input+1) != '-' && *(input+1) != '+')
+                    c = tolower(*input);
+            }
+            else
+                c = tolower(*input);
+        }
+        
+        else
+            c = *input;
+        
+        *output++ = c;
+    }
+    *output++ = '\0';
+
+    display_insert_at_cursor(&v->display, clean_text);
+}
+
+
+static void
+do_insert_character(const char *text)
+{
+    MPNumber value;
+    mp_set_from_integer(text[0], &value);
+    display_set_number(&v->display, &value);
+}
+
+
+/* Perform bitwise shift on display value. */
+static void
+do_shift(int count)
 {
     MPNumber MPval;
 
@@ -352,6 +431,14 @@ do_expression(int function, int arg, int cursor)
         case FN_FUNCTION:
             do_function(arg);
             return;
+        
+        case FN_PASTE:
+            do_paste((const char *)arg); // FIXME: Probably not 64 bit safe
+            return;
+        
+        case FN_INSERT_CHARACTER:
+            do_insert_character((const char *)arg); // FIXME: Probably not 64 bit safe
+            return;        
 
         case FN_STORE:
             do_sto(arg);
@@ -363,11 +450,11 @@ do_expression(int function, int arg, int cursor)
 
         case FN_RECALL:
             SNPRINTF(buf, MAXLINE, "R%d", arg);
-            display_insert(&v->display, display_get_cursor(&v->display), buf);
+            display_insert_at_cursor(&v->display, buf);
             break;
 
         case FN_CONSTANT:
-            display_insert_number(&v->display, display_get_cursor(&v->display), constant_get_value(arg));
+            display_insert_number_at_cursor(&v->display, constant_get_value(arg));
             break;
 
         case FN_BACKSPACE:
@@ -466,9 +553,9 @@ do_expression(int function, int arg, int cursor)
             } else {
                 if (functions[function].flags & FUNC) {
                     SNPRINTF(buf, MAXLINE, "%s(", functions[function].symname);
-                    display_insert(&v->display, display_get_cursor(&v->display), buf);
+                    display_insert_at_cursor(&v->display, buf);
                 } else {
-                    display_insert(&v->display, display_get_cursor(&v->display), functions[function].symname);
+                    display_insert_at_cursor(&v->display, functions[function].symname);
                 }
             }
             break;
diff --git a/gcalctool/functions.h b/gcalctool/functions.h
index fea15ce..49f545e 100644
--- a/gcalctool/functions.h
+++ b/gcalctool/functions.h
@@ -85,6 +85,8 @@ enum
     FN_REDO,
     FN_CONSTANT,
     FN_FUNCTION,
+    FN_PASTE,
+    FN_INSERT_CHARACTER,
     NFUNCTIONS
 };
 
diff --git a/gcalctool/gtk.c b/gcalctool/gtk.c
index 90706d1..e2738d4 100644
--- a/gcalctool/gtk.c
+++ b/gcalctool/gtk.c
@@ -1292,14 +1292,12 @@ G_MODULE_EXPORT
 void
 aframe_response_cb(GtkWidget *dialog, gint response_id)
 {
-    char *ch;
-
-    if (response_id == GTK_RESPONSE_OK) {
-        MPNumber value;
-        ch = (char *) gtk_entry_get_text(GTK_ENTRY(X.aframe_ch));
-        mp_set_from_integer(ch[0], &value);
-        display_set_number(&v->display, &value);
-    }
+    const gchar *text;
+    
+    text = gtk_entry_get_text(GTK_ENTRY(X.aframe_ch));
+    
+    if (response_id == GTK_RESPONSE_OK)
+        do_button(FN_INSERT_CHARACTER, GPOINTER_TO_INT(text));
     
     gtk_widget_hide(dialog);
 }
@@ -2099,76 +2097,10 @@ copy_cb(GtkWidget *widget)
 
 
 static void
-get_proc(GtkClipboard *clipboard, const gchar *buffer, gpointer data)
+on_paste(GtkClipboard *clipboard, const gchar *text, gpointer data)
 {
-    gchar *dstp, *end_buffer, *srcp, *text, c;
-
-    if (buffer == NULL) {
-        return;
-    }
-
-    end_buffer = (gchar *) (buffer + strlen(buffer));
-    text = malloc(strlen(buffer)+1);
-
-    dstp = text;
-    for (srcp = (gchar *) buffer; srcp < end_buffer; srcp++) {
-        /* If the clipboard buffer contains any occurances of the "thousands
-         * separator", remove them.
-         */
-        if (v->tsep[0] != '\0' && strncmp(srcp, v->tsep, strlen(v->tsep)) == 0) {
-            srcp += strlen(v->tsep) - 1;
-            continue;
-        }
-        
-        /* Replace radix with "." */
-        else if (strncmp(srcp, v->radix, strlen(v->radix)) == 0) {
-            c = '.';
-        }
-
-        /* Replace tabs with spaces */        
-        else if (*srcp == '\t') {
-            c = ' ';
-        }
-        
-        /* Terminate on newlines */        
-        else if (*srcp == '\r' || *srcp == '\n') {
-            c = '\0';
-        }
-        
-        /* If an "A", "B", "C", "D" or "F" character is encountered, it 
-         * will be converted to its lowercase equivalent. If an "E" is 
-         * found,  and the next character is a "-" or a "+", then it 
-         * remains as an upper case "E" (it's assumed to be a possible 
-         * exponential number), otherwise its converted to a lower case 
-         * "e". See bugs #455889 and #469245 for more details.
-         */
-        else if (*srcp >= 'A' && *srcp <= 'F') {
-            if (*srcp == 'E') {
-                c = *srcp;
-                if (srcp < (end_buffer-1)) {
-                    if (*(srcp+1) != '-' &&
-                        *(srcp+1) != '+') {
-                        c = tolower(*srcp);
-                    }
-                }
-            }
-            else
-                c = tolower(*srcp);
-        }
-        
-        else
-            c = *srcp;
-        
-        *dstp++ = c;
-    }
-    *dstp++ = '\0';
-
-    if (display_is_result(&v->display))
-        display_set_string(&v->display, (char *)text, -1);
-    else
-        display_insert(&v->display, get_cursor(), (char *)text);        
-    display_set_cursor(&v->display, -1);
-    free(text);
+    if (text != NULL)
+        do_button(FN_PASTE, GPOINTER_TO_INT(text));
 }
 
 
@@ -2178,7 +2110,7 @@ mouse_button_cb(GtkWidget *widget, GdkEventButton *event)
 {
     if (event->button == 2) {
         gtk_clipboard_request_text(gtk_clipboard_get(X.primary_atom),
-                                   get_proc, NULL);
+                                   on_paste, NULL);
     }
 
     return (FALSE);
@@ -2190,7 +2122,7 @@ void
 paste_cb(GtkWidget *widget)
 {
     gtk_clipboard_request_text(gtk_clipboard_get(X.clipboard_atom),
-                               get_proc, NULL);
+                               on_paste, NULL);
 }
 
 



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