[gcalctool] Convert number in display if it has a base and a base button is pressed



commit e06fad43ccff136db33f41eba6ebebd8974cf2a3
Author: Robert Ancell <robert ancell gmail com>
Date:   Mon Mar 1 11:34:55 2010 +1100

    Convert number in display if it has a base and a base button is pressed

 NEWS          |    5 +++--
 src/display.c |   26 ++++++++++++++++++++++++++
 src/display.h |    1 +
 src/gtk.c     |    4 ++++
 4 files changed, 34 insertions(+), 2 deletions(-)
---
diff --git a/NEWS b/NEWS
index e34eccf..e87fedd 100644
--- a/NEWS
+++ b/NEWS
@@ -6,8 +6,9 @@
 
 Overview of changes in gcalctool 5.29.92
 
-    * Add shortcuts to change base (Ctrl+B, Ctrl+O, Ctrl+D, Ctrl+H), and change
-      the displayed number base on results.
+    * Add shortcuts to base buttons (Ctrl+B, Ctrl+O, Ctrl+D, Ctrl+H)
+
+    * Convert number in display if it has a base and a base button is pressed
 
     * Make scientific notation button behave the same as keyboard shortcut (i.e.
       go into superscript mode).
diff --git a/src/display.c b/src/display.c
index a71d78e..e22d164 100644
--- a/src/display.c
+++ b/src/display.c
@@ -558,6 +558,32 @@ display_is_usable_number(GCDisplay *display, MPNumber *z)
 }
 
 
+gboolean
+display_is_number_with_base(GCDisplay *display)
+{
+    MPNumber t;
+    const char *text;
+    const char *sub_digits[] = { "â??", "â??", "â??", "â??", "â??", "â??", "â??", "â??", "â??", "â??", NULL };
+    int i;
+
+    if (display_is_empty(display))
+        return FALSE;
+
+    if (display_is_result(display))
+        return (display->format == BIN || display->format == OCT || display->format == HEX);
+
+    /* See if it has a subscript suffix */
+    text = get_text(display);
+    text += strlen (text);
+    for (i = 0; sub_digits[i] != NULL; i++) {
+        if (strcmp (text - strlen (sub_digits[i]), sub_digits[i]) == 0)
+            return mp_set_from_string(get_text(display), &t) == 0;
+    }
+
+    return FALSE;
+}
+
+
 void
 display_init(GCDisplay *display)
 {
diff --git a/src/display.h b/src/display.h
index e639b01..ac5d425 100644
--- a/src/display.h
+++ b/src/display.h
@@ -112,6 +112,7 @@ void display_delete(GCDisplay *, int, int);
 gboolean display_is_empty(GCDisplay *);
 gboolean display_is_result(GCDisplay *);
 gboolean display_is_usable_number(GCDisplay *display, MPNumber *);
+gboolean display_is_number_with_base(GCDisplay *display);
 
 void display_make_number(GCDisplay *display, char *target, int target_len, const MPNumber *x);
 
diff --git a/src/gtk.c b/src/gtk.c
index 2ce6a5b..e93aea1 100644
--- a/src/gtk.c
+++ b/src/gtk.c
@@ -1148,6 +1148,10 @@ digit_cb(GtkWidget *widget, GdkEventButton *event)
 static void
 do_base(gint base)
 {
+    /* If has a number already in a base, then solve and convert it */
+    if (display_is_number_with_base(&v->display))
+        do_button(FN_CALCULATE, NULL);
+
     if (display_is_result(&v->display)) {
         if (base == 2)
             display_convert (&v->display, BIN);



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