[gcalctool] Only use non-decimal bases in programming mode



commit 1c181e2c3f78ca793fff08b0a366d0b312d72fb0
Author: Robert Ancell <robert ancell gmail com>
Date:   Thu Apr 22 19:03:22 2010 +1000

    Only use non-decimal bases in programming mode

 data/gcalctool.schemas.in |    4 +-
 data/preferences.ui       |   26 ----------------
 src/gcalctool.c           |   29 ++++++------------
 src/math-buttons.c        |   32 +++++++++++++++++--
 src/math-buttons.h        |    4 ++
 src/math-display.c        |   12 -------
 src/math-equation.c       |   71 ++++++++++----------------------------------
 src/math-equation.h       |    5 +--
 src/math-preferences.c    |   16 +---------
 9 files changed, 63 insertions(+), 136 deletions(-)
---
diff --git a/data/gcalctool.schemas.in b/data/gcalctool.schemas.in
index 59515cf..958421a 100644
--- a/data/gcalctool.schemas.in
+++ b/data/gcalctool.schemas.in
@@ -37,7 +37,7 @@ To change the gconftool target use the - -config-source=blah option to gconftool
       <locale name="C">
         <short>Numeric Base</short>
         <long>
-          The numeric base for input and display.
+          The numeric base (used in programming mode)
         </long>
       </locale>
     </schema>
@@ -50,7 +50,7 @@ To change the gconftool target use the - -config-source=blah option to gconftool
       <locale name="C"> 
         <short>Display Mode</short> 
         <long> 
-          The initial display mode. Valid values are
+          The display format used in advanced mode.  One of:
             "ENG" (engineering), 
             "FIX" (fixed-point) and
             "SCI" (scientific)
diff --git a/data/preferences.ui b/data/preferences.ui
index 1c66df1..4d93aed 100644
--- a/data/preferences.ui
+++ b/data/preferences.ui
@@ -274,30 +274,4 @@
     <property name="step_increment">1</property>
     <property name="page_increment">1</property>
   </object>
-  <object class="GtkListStore" id="number_base_model">
-    <columns>
-      <!-- column-name label -->
-      <column type="gchararray"/>
-      <!-- column-name base -->
-      <column type="gint"/>
-    </columns>
-    <data>
-      <row>
-        <col id="0" translatable="yes">Binary</col>
-        <col id="1">2</col>
-      </row>
-      <row>
-        <col id="0" translatable="yes">Octal</col>
-        <col id="1">8</col>
-      </row>
-      <row>
-        <col id="0" translatable="yes">Decimal</col>
-        <col id="1">10</col>
-      </row>
-      <row>
-        <col id="0" translatable="yes">Hexadecimal</col>
-        <col id="1">16</col>
-      </row>
-    </data>
-  </object>
 </interface>
diff --git a/src/gcalctool.c b/src/gcalctool.c
index af39452..1eed178 100644
--- a/src/gcalctool.c
+++ b/src/gcalctool.c
@@ -182,14 +182,8 @@ number_format_to_string(DisplayFormat format)
     switch(format)
     {
     default:
-    case DEC:
-        return "DEC";
-    case BIN:
-        return "BIN";
-    case OCT:
-        return "OCT";
-    case HEX:
-        return "HEX";
+    case FIX:
+        return "FIX";
     case SCI:
         return "SCI";
     case ENG:      
@@ -202,20 +196,14 @@ static DisplayFormat
 string_to_number_format(const gchar *name)
 {
     if (name == NULL)
-        return DEC;
-
-    if (strcmp(name, "BIN") == 0)
-        return BIN;
-    else if (strcmp(name, "OCT") == 0)
-        return OCT;
-    else if (strcmp(name, "HEX") == 0)
-        return HEX;
-    else if (strcmp(name, "SCI") == 0)
+        return FIX;
+
+    if (strcmp(name, "SCI") == 0)
         return SCI;
     else if (strcmp(name, "ENG") == 0)
         return ENG;
     else
-        return DEC;
+        return FIX;
 }
 
 
@@ -301,6 +289,7 @@ quit_cb(MathWindow *window)
     gconf_client_set_string(client, "/apps/gcalctool/result_format", number_format_to_string(math_equation_get_number_format(equation)), NULL);
     gconf_client_set_string(client, "/apps/gcalctool/angle_units", angle_unit_to_string(math_equation_get_angle_units(equation)), NULL);
     gconf_client_set_string(client, "/apps/gcalctool/button_layout", button_mode_to_string(math_buttons_get_mode(buttons)), NULL);
+    gconf_client_set_int(client, "/apps/gcalctool/base", math_buttons_get_programming_base(buttons), NULL);
 
     currency_free_resources();
     gtk_main_quit();
@@ -337,7 +326,7 @@ int
 main(int argc, char **argv)
 {
     MathEquation *equation;
-    int accuracy = 9, word_size = 64;
+    int accuracy = 9, word_size = 64, base = 10;
     gboolean show_tsep = FALSE, show_zeroes = FALSE;
     gchar *number_format, *angle_units, *button_mode;
   
@@ -360,6 +349,7 @@ main(int argc, char **argv)
     get_int("/apps/gcalctool/wordlen", &word_size);
     get_bool("/apps/gcalctool/showthousands", &show_tsep);
     get_bool("/apps/gcalctool/showzeroes", &show_zeroes);
+    get_int("/apps/gcalctool/base", &base);
     number_format = gconf_client_get_string(client, "/apps/gcalctool/result_format", NULL);
     angle_units = gconf_client_get_string(client, "/apps/gcalctool/angle_units", NULL);
     button_mode = gconf_client_get_string(client, "/apps/gcalctool/button_layout", NULL);
@@ -377,6 +367,7 @@ main(int argc, char **argv)
 
     window = math_window_new(equation);
     g_signal_connect(G_OBJECT(window), "quit", G_CALLBACK(quit_cb), NULL);
+    math_buttons_set_programming_base(math_window_get_buttons(window), base);
     math_buttons_set_mode(math_window_get_buttons(window), string_to_button_mode(button_mode)); // FIXME: We load the basic buttons even if we immediately switch to the next type
     g_free(button_mode);
 
diff --git a/src/math-buttons.c b/src/math-buttons.c
index fd36fbf..1bad5fc 100644
--- a/src/math-buttons.c
+++ b/src/math-buttons.c
@@ -37,6 +37,8 @@ struct MathButtonsPrivate
     MathEquation *equation;
 
     ButtonMode mode;
+    gint programming_base;
+  
     GtkBuilder *basic_ui, *advanced_ui, *financial_ui, *programming_ui;
 
     GdkColor color_numbers, color_action, color_operator, color_function, color_memory, color_group;
@@ -610,17 +612,19 @@ base_changed_cb(MathEquation *equation, GParamSpec *spec, MathButtons *buttons)
     GtkTreeModel *model;
     GtkTreeIter iter;
     gboolean valid;
-    gint value;
+  
+    if (buttons->priv->mode != PROGRAMMING)
+        return;
 
     model = gtk_combo_box_get_model(GTK_COMBO_BOX(buttons->priv->base_combo));
     valid = gtk_tree_model_get_iter_first(model, &iter);
-    value = math_equation_get_base(buttons->priv->equation);
+    buttons->priv->programming_base = math_equation_get_base(buttons->priv->equation);
 
     while (valid) {
         gint v;
 
         gtk_tree_model_get(model, &iter, 1, &v, -1);
-        if (v == value)
+        if (v == buttons->priv->programming_base)
             break;
         valid = gtk_tree_model_iter_next(model, &iter);
     }
@@ -892,6 +896,11 @@ math_buttons_set_mode(MathButtons *buttons, ButtonMode mode)
 
     old_mode = buttons->priv->mode;
     buttons->priv->mode = mode;
+  
+    if (mode == PROGRAMMING)
+        math_equation_set_base(buttons->priv->equation, buttons->priv->programming_base);
+    else
+        math_equation_set_base(buttons->priv->equation, 10);
 
     load_buttons(buttons);
 
@@ -906,6 +915,20 @@ math_buttons_get_mode(MathButtons *buttons)
 }
 
 
+void
+math_buttons_set_programming_base(MathButtons *buttons, gint base)
+{
+    buttons->priv->programming_base = base;
+}
+
+
+gint
+math_buttons_get_programming_base(MathButtons *buttons)
+{
+    return buttons->priv->programming_base;
+}
+
+
 G_MODULE_EXPORT
 void
 exponent_cb(GtkWidget *widget, MathButtons *buttons)
@@ -1568,8 +1591,8 @@ math_buttons_set_property (GObject      *object,
     case PROP_EQUATION:
         self->priv->equation = g_value_get_object (value);
         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::number-mode", G_CALLBACK(number_mode_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);
@@ -1651,6 +1674,7 @@ static void
 math_buttons_init (MathButtons *buttons)
 {
     buttons->priv = G_TYPE_INSTANCE_GET_PRIVATE (buttons, math_buttons_get_type(), MathButtonsPrivate);
+    buttons->priv->programming_base = 10;
     gdk_color_parse("#0000FF", &buttons->priv->color_numbers);
     gdk_color_parse("#00FF00", &buttons->priv->color_action);
     gdk_color_parse("#FF0000", &buttons->priv->color_operator);
diff --git a/src/math-buttons.h b/src/math-buttons.h
index 1257014..a7f7c08 100644
--- a/src/math-buttons.h
+++ b/src/math-buttons.h
@@ -55,4 +55,8 @@ void math_buttons_set_mode(MathButtons *buttons, ButtonMode mode);
 
 ButtonMode math_buttons_get_mode(MathButtons *buttons);
 
+void math_buttons_set_programming_base(MathButtons *buttons, gint base);
+
+gint math_buttons_get_programming_base(MathButtons *buttons);
+
 #endif /* MATH_BUTTONS_H */
diff --git a/src/math-display.c b/src/math-display.c
index acdfc26..5d5fe8c 100644
--- a/src/math-display.c
+++ b/src/math-display.c
@@ -103,27 +103,15 @@ display_key_press_cb(GtkWidget *widget, GdkEventKey *event, MathDisplay *display
     if (state == GDK_CONTROL_MASK) {
         switch(event->keyval)
         {
-        case GDK_b:
-            math_equation_set_number_format(display->priv->equation, BIN);
-            return TRUE;
-        case GDK_d:
-            math_equation_set_number_format(display->priv->equation, DEC);
-            return TRUE;
         case GDK_e:
             math_equation_insert_exponent(display->priv->equation);
             return TRUE;
         case GDK_f:
             math_equation_factorize(display->priv->equation);
             return TRUE;
-        case GDK_h:
-            math_equation_set_number_format(display->priv->equation, HEX);
-            return TRUE;
         case GDK_i:
             math_equation_insert(display->priv->equation, "�¹");
             return TRUE;
-        case GDK_o:
-            math_equation_set_number_format(display->priv->equation, OCT);
-            return TRUE;
         case GDK_p:
             math_equation_insert(display->priv->equation, "Ï?");
             return TRUE;
diff --git a/src/math-equation.c b/src/math-equation.c
index f180c94..57ad414 100644
--- a/src/math-equation.c
+++ b/src/math-equation.c
@@ -75,6 +75,7 @@ struct MathEquationPrivate
     gint accuracy;            /* Number of digits to show */
     gint word_size;           /* Word size in bits */
     MPAngleUnit angle_units;  /* Units for trigonometric functions */
+    gint base;                /* Numeric base */
     NumberMode number_mode;   /* ??? */
     gboolean can_super_minus; /* TRUE if entering minus can generate a superscript minus */
 
@@ -391,17 +392,12 @@ math_equation_get_show_trailing_zeroes(MathEquation *equation)
 void
 math_equation_set_number_format(MathEquation *equation, DisplayFormat format)
 {
-    gint base;
-
     if (equation->priv->format == format)
         return;
 
-    base = math_equation_get_base(equation);
     equation->priv->format = format;
     reformat_display(equation);
     g_object_notify(G_OBJECT(equation), "number-format");
-    if (base != math_equation_get_base(equation))
-        g_object_notify(G_OBJECT(equation), "base");
 }
 
 
@@ -415,43 +411,19 @@ math_equation_get_number_format(MathEquation *equation)
 void
 math_equation_set_base(MathEquation *equation, gint base)
 {
-    if (math_equation_get_base(equation) == base)
+    if (equation->priv->base == base)
         return;
   
-    switch(base) {
-    case 2:
-        math_equation_set_number_format(equation, BIN);
-        break;
-    case 8:
-        math_equation_set_number_format(equation, OCT);
-        break;
-    case 10:
-        math_equation_set_number_format(equation, DEC);
-        break;
-    case 16:
-        math_equation_set_number_format(equation, HEX);
-        break;
-    default:
-        g_warning("Attempt to set non-supported base %d", base);
-        return;
-    }
+    equation->priv->base = base;
+    reformat_display(equation);
+    g_object_notify(G_OBJECT(equation), "base");
 }
 
 
 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;
-    }
+    return equation->priv->base;
 }
 
 
@@ -565,7 +537,7 @@ math_equation_get_number(MathEquation *equation, MPNumber *z)
     gboolean result;
 
     text = math_equation_get_display(equation);
-    result = !mp_set_from_string(text, math_equation_get_base(equation), z);
+    result = !mp_set_from_string(text, equation->priv->base, z);
     g_free (text);
 
     return result;
@@ -909,7 +881,7 @@ parse(MathEquation *equation, const char *text, MPNumber *z, char **error_token)
     MPEquationOptions options;
 
     memset(&options, 0, sizeof(options));
-    options.base = math_equation_get_base(equation);
+    options.base = equation->priv->base;
     options.wordlen = equation->priv->word_size;
     options.angle_units = equation->priv->angle_units;
     options.variable_is_defined = variable_is_defined;
@@ -1122,23 +1094,14 @@ void
 display_make_number(MathEquation *equation, char *target, int target_len, const MPNumber *x)
 {
     switch(equation->priv->format) {
-    case DEC:
-        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, math_equation_get_base(equation), 2, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
-        break;
-    case OCT:
-        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, math_equation_get_base(equation), 16, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
+    case FIX:
+        mp_cast_to_string(x, equation->priv->base, equation->priv->base, equation->priv->accuracy, !equation->priv->show_zeroes, target, target_len);
         break;
     case SCI:
-        mp_cast_to_exponential_string(x, math_equation_get_base(equation), 10, equation->priv->accuracy, !equation->priv->show_zeroes, false, target, target_len);
+        mp_cast_to_exponential_string(x, equation->priv->base, equation->priv->base, equation->priv->accuracy, !equation->priv->show_zeroes, false, target, target_len);
         break;
     case ENG:
-        mp_cast_to_exponential_string(x, math_equation_get_base(equation), 10, equation->priv->accuracy, !equation->priv->show_zeroes, true, target, target_len);
+        mp_cast_to_exponential_string(x, equation->priv->base, equation->priv->base, equation->priv->accuracy, !equation->priv->show_zeroes, true, target, target_len);
         break;
     }
 }
@@ -1260,10 +1223,7 @@ math_equation_class_init (MathEquationClass *klass)
     };
     static GEnumValue number_format_values[] =
     {
-      {DEC, "decimal",     "decimal"},
-      {BIN, "binary",      "binary"},
-      {OCT, "octal",       "octal"},
-      {HEX, "hexadecimal", "hexadecimal"},
+      {FIX, "fixed-point", "fixed-point"},
       {SCI, "scientific",  "scientific"},
       {ENG, "engineering", "engineering"},
       {0, NULL, NULL}
@@ -1342,7 +1302,7 @@ math_equation_class_init (MathEquationClass *klass)
                                                       "number-format",
                                                       "Display format",
                                                       number_format_type,
-                                                      DEC,
+                                                      FIX,
                                                       G_PARAM_READWRITE));
     g_object_class_install_property(object_class,
                                     PROP_BASE,
@@ -1514,10 +1474,11 @@ math_equation_init(MathEquation *equation)
     equation->priv->state.status = g_strdup("");
     equation->priv->show_zeroes = FALSE;
     equation->priv->show_tsep = FALSE;
-    equation->priv->format = DEC;
+    equation->priv->format = FIX;
     equation->priv->accuracy = 9;
     equation->priv->word_size = 32;
     equation->priv->angle_units = MP_DEGREES;
+    equation->priv->base = 10;
 
     mp_set_from_integer(0, &equation->priv->state.ans);
 }
diff --git a/src/math-equation.h b/src/math-equation.h
index a4b2a13..14d7c8e 100644
--- a/src/math-equation.h
+++ b/src/math-equation.h
@@ -44,10 +44,7 @@ typedef struct
 
 /* Number display mode. */
 typedef enum {
-  DEC,
-  BIN,
-  OCT,
-  HEX,
+  FIX,
   SCI,
   ENG
 } DisplayFormat;
diff --git a/src/math-preferences.c b/src/math-preferences.c
index 88b06d1..4441ffc 100644
--- a/src/math-preferences.c
+++ b/src/math-preferences.c
@@ -263,8 +263,8 @@ create_gui(MathPreferencesDialog *dialog)
     model = gtk_combo_box_get_model(GTK_COMBO_BOX(widget));
     gtk_list_store_append(GTK_LIST_STORE(model), &iter);
     gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
-                       /* Number display mode combo: Decimal, e.g. 1234 */
-                       _("Decimal"), 1, DEC, -1);
+                       /* Number display mode combo: Fixed, e.g. 1234 */
+                       _("Fixed"), 1, FIX, -1);
     gtk_list_store_append(GTK_LIST_STORE(model), &iter);
     gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
                        /* Number display mode combo: Scientific, e.g. 1.234Ã?10^3 */
@@ -273,18 +273,6 @@ create_gui(MathPreferencesDialog *dialog)
     gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
                        /* Number display mode combo: Engineering, e.g. 1.234k */
                        _("Engineering"), 1, ENG, -1);
-    gtk_list_store_append(GTK_LIST_STORE(model), &iter);
-    gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
-                       /* Number display mode combo: Binary, e.g. 10011010010â?? */
-                       _("Binary"), 1, BIN, -1);
-    gtk_list_store_append(GTK_LIST_STORE(model), &iter);
-    gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
-                       /* Number display mode combo: Octal, e.g. 2322â?? */
-                       _("Octal"), 1, OCT, -1);
-    gtk_list_store_append(GTK_LIST_STORE(model), &iter);
-    gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
-                       /* Number display mode combo: Hexadecimal, e.g. 4D2â??â?? */
-                       _("Hexadecimal"), 1, HEX, -1);
     renderer = gtk_cell_renderer_text_new();
     gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(widget), renderer, TRUE);
     gtk_cell_layout_add_attribute(GTK_CELL_LAYOUT(widget), renderer, "text", 0);



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