[gcalctool] Show labels for bases and angles



commit af7b85732c52c2281d60679560e445d344bbdf96
Author: Robert Ancell <robert ancell gmail com>
Date:   Sun Apr 18 22:33:30 2010 +1000

    Show labels for bases and angles

 data/buttons-advanced.ui    |    3 +-
 data/buttons-programming.ui |    2 +-
 src/math-buttons.c          |  143 ++++++++++++++++++++++++++++++++----------
 src/math-equation.c         |   50 ++++++++--------
 src/math-equation.h         |    1 +
 5 files changed, 138 insertions(+), 61 deletions(-)
---
diff --git a/data/buttons-advanced.ui b/data/buttons-advanced.ui
index 4ad2fbc..55b189d 100644
--- a/data/buttons-advanced.ui
+++ b/data/buttons-advanced.ui
@@ -6,8 +6,9 @@
     <child>
       <object class="GtkVBox" id="button_panel">
         <property name="visible">True</property>
+        <property name="spacing">6</property>
         <child>
-          <object class="GtkLabel" id="display_label">
+          <object class="GtkLabel" id="angle_label">
             <property name="visible">True</property>
             <property name="xalign">1</property>
             <property name="label" comments="Example content">3.14159 radians = 180 degrees</property>
diff --git a/data/buttons-programming.ui b/data/buttons-programming.ui
index ece8d4f..b0ba77a 100644
--- a/data/buttons-programming.ui
+++ b/data/buttons-programming.ui
@@ -8,7 +8,7 @@
         <property name="visible">True</property>
         <property name="spacing">6</property>
         <child>
-          <object class="GtkLabel" id="display_label">
+          <object class="GtkLabel" id="base_label">
             <property name="visible">True</property>
             <property name="xalign">1</property>
             <property name="label" comments="Example content">FF&#x2081;&#x2086; 256&#x2081;&#x2080;</property>
diff --git a/src/math-buttons.c b/src/math-buttons.c
index a7ef1b6..e9d36fa 100644
--- a/src/math-buttons.c
+++ b/src/math-buttons.c
@@ -50,6 +50,8 @@ struct MathButtonsPrivate
     GList *superscript_toggles;
     GList *subscript_toggles;
 
+    GtkWidget *angle_label, *base_label;
+
     guint64 bits;
     GtkWidget *bit_panel;
     GtkWidget *bit_labels[MAXBITS];
@@ -424,6 +426,104 @@ load_finc_dialogs(MathButtons *buttons)
 }
 
 
+static void
+display_changed_cb(MathEquation *equation, GParamSpec *spec, MathButtons *buttons)
+{
+    MPNumber x;
+    gboolean is_number;
+
+    is_number = math_equation_get_number(equation, &x);
+
+    if (buttons->priv->angle_label && is_number) {
+        MPNumber pi;
+        char *label, ans_string[1024], conv_string[1024];
+
+        mp_get_pi(&pi);
+        mp_cast_to_string(&x, 10, 10, 2, false, ans_string, 1024);
+        switch (math_equation_get_angle_units(equation)) {
+        default:
+        case MP_DEGREES:
+            mp_multiply(&x, &pi, &x);
+            mp_divide_integer(&x, 180, &x);
+            mp_cast_to_string(&x, 10, 10, 2, false, conv_string, 1024);
+            label = g_strdup_printf("%s degrees = %s radians", ans_string, conv_string);
+            break;
+        case MP_RADIANS:
+            mp_multiply_integer(&x, 180, &x);
+            mp_divide(&x, &pi, &x);
+            mp_cast_to_string(&x, 10, 10, 2, false, conv_string, 1024);
+            label = g_strdup_printf("%s radians = %s degrees", ans_string, conv_string);
+            break;
+        case MP_GRADIANS:
+            mp_multiply(&x, &pi, &x);
+            mp_divide_integer(&x, 200, &x);
+            mp_cast_to_string(&x, 10, 10, 2, false, conv_string, 1024);
+            label = g_strdup_printf("%s gradians = %s radians", ans_string, conv_string);
+            break;
+        }
+
+        gtk_label_set_text(GTK_LABEL(buttons->priv->angle_label), label);
+        g_free(label);
+    }
+  
+    if (buttons->priv->base_label) {
+        GString *label;
+        gint base;
+        gchar text[1024];
+
+        base = math_equation_get_base(equation);      
+        label = g_string_new("");
+        if (base != 8) {
+            mp_cast_to_string(&x, 0, 8, 2, true, text, 1024);
+            if (label->len != 0)
+                g_string_append(label, " ");
+            g_string_append(label, text);
+        }
+        if (base != 10) {
+            mp_cast_to_string(&x, 0, 10, 2, true, text, 1024);
+            if (label->len != 0)
+                g_string_append(label, " ");
+            g_string_append(label, text);
+        }
+        if (base != 16) {
+            mp_cast_to_string(&x, 0, 16, 2, true, text, 1024);
+            if (label->len != 0)
+                g_string_append(label, " ");
+            g_string_append(label, text);
+        }
+
+        gtk_label_set_text(GTK_LABEL(buttons->priv->base_label), label->str);
+        g_string_free(label, TRUE);
+    }
+
+    if (buttons->priv->bit_panel) {
+        gboolean enabled = is_number;
+        int i;
+
+        if (enabled) {
+            MPNumber max;
+
+            mp_set_from_unsigned_integer(G_MAXUINT64, &max);
+            if (mp_is_negative(&x) || mp_is_greater_than(&x, &max))
+                enabled = FALSE;
+            else
+                buttons->priv->bits = mp_cast_to_unsigned_int(&x);
+        }
+
+        gtk_widget_set_sensitive(buttons->priv->bit_panel, enabled);
+        for (i = 0; i < MAXBITS; i++) {
+            const gchar *label;
+
+            if (buttons->priv->bits & (1LL << (MAXBITS-i-1)))
+                label = " 1";
+            else
+                label = " 0";
+            gtk_label_set_text(GTK_LABEL(buttons->priv->bit_labels[i]), label);
+        }
+    }
+}
+
+
 static GtkWidget *
 load_mode(MathButtons *buttons, ButtonMode mode)
 {
@@ -549,8 +649,13 @@ load_mode(MathButtons *buttons, ButtonMode mode)
         if (math_equation_get_number_mode(buttons->priv->equation) == SUBSCRIPT)
             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
     }
+  
+    if (mode == ADVANCED) {
+        buttons->priv->angle_label = GET_WIDGET(builder, "angle_label");
+    }
 
     if (mode == PROGRAMMING) {
+        buttons->priv->base_label = GET_WIDGET(builder, "base_label");      
         buttons->priv->character_code_dialog = GET_WIDGET(builder, "character_code_dialog");
         buttons->priv->character_code_entry = GET_WIDGET(builder, "character_code_entry");
 
@@ -581,6 +686,8 @@ load_mode(MathButtons *buttons, ButtonMode mode)
     }
 
     gtk_builder_connect_signals(builder, buttons);
+
+    display_changed_cb(buttons->priv->equation, NULL, buttons);
   
     return *panel;
 }
@@ -1260,40 +1367,6 @@ set_subscript_cb(GtkWidget *widget, MathButtons *buttons)
 
 
 static void
-display_changed_cb(MathEquation *equation, GParamSpec *spec, MathButtons *buttons)
-{
-    gboolean enabled;
-    MPNumber x;
-    int i;
-
-    if (!buttons->priv->bit_panel)
-       return;
-
-    enabled = math_equation_get_number(equation, &x);
-    if (enabled) {
-        MPNumber max;
-
-        mp_set_from_unsigned_integer(G_MAXUINT64, &max);
-        if (mp_is_negative(&x) || mp_is_greater_than(&x, &max))
-            enabled = FALSE;
-        else
-            buttons->priv->bits = mp_cast_to_unsigned_int(&x);
-    }
-
-    gtk_widget_set_sensitive(buttons->priv->bit_panel, enabled);
-    for (i = 0; i < MAXBITS; i++) {
-        const gchar *label;
-
-        if (buttons->priv->bits & (1LL << (MAXBITS-i-1)))
-            label = " 1";
-        else
-            label = " 0";
-        gtk_label_set_text(GTK_LABEL(buttons->priv->bit_labels[i]), label);
-    }
-}
-
-
-static void
 number_mode_changed_cb(MathEquation *equation, GParamSpec *spec, MathButtons *buttons)
 {
     GList *i;
@@ -1328,6 +1401,8 @@ math_buttons_set_property (GObject      *object,
         math_buttons_set_mode(self, self->priv->mode);
         g_signal_connect(self->priv->equation, "notify::number-mode", G_CALLBACK(number_mode_changed_cb), self);
         g_signal_connect(self->priv->equation, "notify::display", G_CALLBACK(display_changed_cb), self);
+        g_signal_connect(self->priv->equation, "notify::angle-units", G_CALLBACK(display_changed_cb), self);
+        g_signal_connect(self->priv->equation, "notify::number-format", G_CALLBACK(display_changed_cb), self);
         number_mode_changed_cb(self->priv->equation, NULL, self);
         display_changed_cb(self->priv->equation, NULL, self);
         break;
diff --git a/src/math-equation.c b/src/math-equation.c
index c8ccaaf..b2e4046 100644
--- a/src/math-equation.c
+++ b/src/math-equation.c
@@ -397,6 +397,23 @@ math_equation_get_number_format(MathEquation *equation)
 }
 
 
+gint
+math_equation_get_base(MathEquation *equation)
+{
+    switch(equation->priv->format)
+    {
+    case BIN:
+      return 2;
+    case OCT:
+      return 8;
+    case HEX:
+      return 16;
+    default:
+      return 10;
+    }
+}
+
+
 void
 math_equation_set_word_size(MathEquation *equation, int word_size)
 {
@@ -500,23 +517,6 @@ math_equation_get_equation(MathEquation *equation)
 }
 
 
-static int
-get_base(MathEquation *equation)
-{
-    switch(equation->priv->format)
-    {
-    case BIN:
-      return 2;
-    case OCT:
-      return 8;
-    case HEX:
-      return 16;
-    default:
-      return 10;
-    }
-}
-
-
 gboolean
 math_equation_get_number(MathEquation *equation, MPNumber *z)
 {
@@ -524,7 +524,7 @@ math_equation_get_number(MathEquation *equation, MPNumber *z)
     gboolean result;
 
     text = math_equation_get_display(equation);
-    result = !mp_set_from_string(text, get_base(equation), z);
+    result = !mp_set_from_string(text, math_equation_get_base(equation), z);
     g_free (text);
 
     return result;
@@ -866,7 +866,7 @@ parse(MathEquation *equation, const char *text, MPNumber *z, char **error_token)
     MPEquationOptions options;
 
     memset(&options, 0, sizeof(options));
-    options.base = get_base(equation);
+    options.base = math_equation_get_base(equation);
     options.wordlen = equation->priv->word_size;
     options.angle_units = equation->priv->angle_units;
     options.variable_is_defined = variable_is_defined;
@@ -1146,22 +1146,22 @@ display_make_number(MathEquation *equation, char *target, int target_len, const
 {
     switch(equation->priv->format) {
     case DEC:
-        mp_cast_to_string(x, get_base(equation), 10, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
+        mp_cast_to_string(x, math_equation_get_base(equation), 10, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
         break;
     case BIN:
-        mp_cast_to_string(x, get_base(equation), 2, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
+        mp_cast_to_string(x, math_equation_get_base(equation), 2, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
         break;
     case OCT:
-        mp_cast_to_string(x, get_base(equation), 8, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
+        mp_cast_to_string(x, math_equation_get_base(equation), 8, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
         break;
     case HEX:
-        mp_cast_to_string(x, get_base(equation), 16, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
+        mp_cast_to_string(x, math_equation_get_base(equation), 16, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
         break;
     case SCI:
-        make_eng_sci(equation, target, target_len, x, get_base(equation), 10);
+        make_eng_sci(equation, target, target_len, x, math_equation_get_base(equation), 10);
         break;
     case ENG:
-        make_eng_sci(equation, target, target_len, x, get_base(equation), 10);
+        make_eng_sci(equation, target, target_len, x, math_equation_get_base(equation), 10);
         break;
     }
 }
diff --git a/src/math-equation.h b/src/math-equation.h
index c94d85d..7952cbf 100644
--- a/src/math-equation.h
+++ b/src/math-equation.h
@@ -86,6 +86,7 @@ gboolean math_equation_get_show_trailing_zeroes(MathEquation *equation);
 
 void math_equation_set_number_format(MathEquation *equation, DisplayFormat format);
 DisplayFormat math_equation_get_number_format(MathEquation *equation);
+gint math_equation_get_base(MathEquation *equation);
 
 void math_equation_set_word_size(MathEquation *equation, int word_size);
 int math_equation_get_word_size(MathEquation *equation);



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