gcalctool r2256 - in trunk: . gcalctool



Author: rancell
Date: Sat Oct 11 03:50:39 2008
New Revision: 2256
URL: http://svn.gnome.org/viewvc/gcalctool?rev=2256&view=rev

Log:
Only update bit panel when display contains a solved number (Bug #548690)

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

Modified: trunk/gcalctool/display.c
==============================================================================
--- trunk/gcalctool/display.c	(original)
+++ trunk/gcalctool/display.c	Sat Oct 11 03:50:39 2008
@@ -205,6 +205,54 @@
     return get_state(display)->expression;
 }
 
+gboolean display_get_integer(GCDisplay *display, gint64 *value)
+{
+    const char *text;
+    char buf[MAX_DISPLAY];
+    gchar *endptr;
+    guint bases[] = {2, 8, 10, 16};
+
+    text = display_get_text(display);
+    if (text[0] == '\0') {
+        text = "0";
+    }
+    else if (display_is_result(display)) {
+        make_number(buf, MAX_DISPLAY, display_get_answer(display), v->base, FALSE);
+        text = buf;
+    }
+    
+    *value = g_ascii_strtoll(text, &endptr, bases[v->base]);
+    if(*endptr != '\0' || ((*value == G_MAXINT64 || *value == G_MININT64) && errno == ERANGE))
+        return FALSE;
+    return TRUE;
+}
+
+gboolean display_get_unsigned_integer(GCDisplay *display, guint64 *value)
+{
+    const char *text;
+    char buf[MAX_DISPLAY];
+    gchar *endptr;
+    guint bases[] = {2, 8, 10, 16};
+
+    text = display_get_text(display);
+    if (text[0] == '\0') {
+        text = "0";
+    }
+    else if (display_is_result(display)) {
+        make_number(buf, MAX_DISPLAY, display_get_answer(display), v->base, FALSE);
+        text = buf;
+    }
+    
+    /* strtoull() treats the string like a 2's complement number which is not what we want */
+    if(text[0] == '-')
+        return FALSE;
+
+    *value = g_ascii_strtoull(text, &endptr, bases[v->base]);
+    if(*endptr != '\0' || (*value == G_MAXUINT64 && errno == ERANGE))
+        return FALSE;
+    return TRUE;
+}
+
 int *display_get_answer(GCDisplay *display)
 {
     return get_state(display)->ans;

Modified: trunk/gcalctool/display.h
==============================================================================
--- trunk/gcalctool/display.h	(original)
+++ trunk/gcalctool/display.h	Sat Oct 11 03:50:39 2008
@@ -57,6 +57,8 @@
 void display_clear(GCDisplay *);
 void display_refresh(GCDisplay *);
 
+gboolean display_get_integer(GCDisplay *display, gint64 *value);
+gboolean display_get_unsigned_integer(GCDisplay *display, guint64 *value);
 int *display_get_answer(GCDisplay *);
 int display_get_cursor(GCDisplay *);
 

Modified: trunk/gcalctool/gtk.c
==============================================================================
--- trunk/gcalctool/gtk.c	(original)
+++ trunk/gcalctool/gtk.c	Sat Oct 11 03:50:39 2008
@@ -727,29 +727,23 @@
 static void
 set_bit_panel(void)
 {
-    int bit_str_len, i;
-    int MP[MP_SIZE];
-    char bit_str[MAXLINE], label[MAXLINE];
-
-    if (display_is_usable_number(&v->display, MP) || !is_integer(MP)) {
-        gtk_widget_set_sensitive(X->bit_panel, FALSE);
+    int i;
+    const gchar *label;
+    guint64 value;
+    
+    if (!display_get_unsigned_integer(&v->display, &value))
+    {
+        gtk_widget_set_sensitive(X->bit_panel, FALSE);        
         return;
     }
-    make_fixed(bit_str, MAXLINE, MP, BIN, MAXLINE, FALSE);
-    bit_str_len = strlen(bit_str);
-    if (bit_str_len <= MAXBITS) {
-        gtk_widget_set_sensitive(X->bit_panel, TRUE);
-        
-        for (i = 0; i < MAXBITS; i++) {
-            if (i < bit_str_len) {
-                SNPRINTF(label, MAXLINE, " %c", bit_str[bit_str_len-i-1]);
-            } else {
-                SNPRINTF(label, MAXLINE, " 0");
-            }
-            gtk_label_set_text(GTK_LABEL(X->bits[MAXBITS - i - 1]), label);
-        }
-    } else {
-        gtk_widget_set_sensitive(X->bit_panel, FALSE);
+    gtk_widget_set_sensitive(X->bit_panel, TRUE);
+
+    for (i = 0; i < MAXBITS; i++) {
+        if (value & (1LL << (MAXBITS-i-1)))
+            label = " 1";
+        else
+            label = " 0";
+        gtk_label_set_text(GTK_LABEL(X->bits[i]), label);
     }
 }
 
@@ -1649,34 +1643,32 @@
 static gboolean
 bit_toggle_cb(GtkWidget *event_box, GdkEventButton *event)
 {
-    double number;
-    unsigned long long lval;
-    int n, MP1[MP_SIZE], index, ret;
-
-    index = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(event_box),
-                                              "bit_index"));
-    n = MAXBITS - index - 1;
-
-    ret = display_is_usable_number(&v->display, display_get_answer(&v->display));
-    assert(!ret);
-    mp_set_from_mp(display_get_answer(&v->display), MP1);
-
-    number = mp_cast_to_double(MP1);
-    lval = (long long) number;
-
-    if (lval & (1LL << n)) {
-        lval &= ~(1LL << n);
-        gtk_label_set_text(GTK_LABEL(X->bits[index]), " 0");
-    } else {
-        lval |=  (1LL << n);
-        gtk_label_set_text(GTK_LABEL(X->bits[index]), " 1");
-    }
-    number = (double) lval;
-
-    mp_set_from_double(number, display_get_answer(&v->display));
-    display_set_string(&v->display, "Ans", -1);
+    guint64 value;
+    int index;
+    const gchar *text;
+    char buf[MAX_DISPLAY];
+    int MP[MP_SIZE];
+    
+    index = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(event_box), "bit_index"));
+    assert(display_get_unsigned_integer(&v->display, &value));
+    value ^= (1LL << (MAXBITS - index - 1));
+    
+    /* FIXME: Convert to since we don't support setting MP numbers from 64 bit integers */
+    SNPRINTF(buf, MAX_DISPLAY, "%llu", value);
+    MPstr_to_num(buf, 10, MP);
+
+    /* FIXME: Set as string as display_set_number doesn't store correctly */
+    make_number(buf, MAX_DISPLAY, MP, v->base, FALSE);
+    display_set_string(&v->display, buf, -1);
     display_refresh(&v->display);
 
+    text = gtk_label_get_text(GTK_LABEL(X->bits[index]));
+    if (strcmp(text, " 0") == 0)
+        text = " 1";
+    else
+        text = " 0";        
+    gtk_label_set_text(GTK_LABEL(X->bits[index]), text);
+
     return (TRUE);
 }
 

Modified: trunk/gcalctool/mp.h
==============================================================================
--- trunk/gcalctool/mp.h	(original)
+++ trunk/gcalctool/mp.h	Sat Oct 11 03:50:39 2008
@@ -85,6 +85,7 @@
 void   mp_set_from_float(float, int *);
 void   mp_set_from_double(double, int *);
 void   mp_set_from_integer(int, int *);
+void   mp_set_from_uint64(int, int *);
 void   mp_set_from_fraction(int, int, int *);
 float  mp_cast_to_float(const int *);
 double mp_cast_to_double(const int *);



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