[gcalctool] Make preferences dialog a proper widget connected to MathEquation, add in base combobox



commit a35f8bd8a1599174b2a2a2f450b5fcc960221812
Author: Robert Ancell <robert ancell gmail com>
Date:   Mon Apr 12 09:37:31 2010 +1000

    Make preferences dialog a proper widget connected to MathEquation, add in base combobox

 data/preferences.ui  |  120 +++++++++-------
 src/get.h            |    1 +
 src/math-buttons.c   |    8 +-
 src/math-equation.c  |   68 +++++-----
 src/math-equation.h  |    8 +-
 src/ui-preferences.c |  377 ++++++++++++++++++++++++++++++++------------------
 src/ui-preferences.h |   28 +++--
 src/ui.c             |   25 +++-
 8 files changed, 390 insertions(+), 245 deletions(-)
---
diff --git a/data/preferences.ui b/data/preferences.ui
index dfdc109..ff7f736 100644
--- a/data/preferences.ui
+++ b/data/preferences.ui
@@ -7,56 +7,16 @@
       <!-- column-name label -->
       <column type="gchararray"/>
       <!-- column-name units -->
-      <column type="gchararray"/>
+      <column type="gint"/>
     </columns>
-    <data>
-      <row>
-        <col id="0" translatable="yes" comments="Preferences dialog: Angle unit combo box: Use degrees for trigonometric calculations">Degrees</col>
-        <col id="1">degrees</col>
-      </row>
-      <row>
-        <col id="0" translatable="yes" comments="Preferences dialog: Angle unit combo box: Use radians for trigonometric calculations">Radians</col>
-        <col id="1">radians</col>
-      </row>
-      <row>
-        <col id="0" translatable="yes" comments="Preferences dialog: Angle unit combo box: Use gradians for trigonometric calculations">Gradians</col>
-        <col id="1">gradians</col>
-      </row>
-    </data>
   </object>
   <object class="GtkListStore" id="display_format_model">
     <columns>
       <!-- column-name label -->
       <column type="gchararray"/>
       <!-- column-name format -->
-      <column type="gchararray"/>
+      <column type="gint"/>
     </columns>
-    <data>
-      <row>
-        <col id="0" translatable="yes" comments="Number display mode combo: Decimal, e.g. 1234">Decimal</col>
-        <col id="1">decimal</col>
-      </row>
-      <row>
-        <col id="0" translatable="yes" comments="Number display mode combo: Scientific, e.g. 1.234&#xD7;10^3">Scientific</col>
-        <col id="1">scientific</col>
-      </row>
-      <row>
-        <col id="0" translatable="yes" comments="Number display mode combo: Engineering, e.g. 1.234k">Engineering</col>
-        <col id="1">engineering</col>
-      </row>
-      <row>
-        <col id="0" translatable="yes" comments="Number display mode combo: Binary, e.g. 10011010010&#x2082;">Binary</col>
-        <col id="1">binary</col>
-      </row>
-      <row>
-        <col id="0" translatable="yes" comments="Number display mode combo: Octal, e.g. 2322&#x2088;">Octal</col>
-        <col id="1">octal</col>
-      </row>
-      <row>
-        <col id="0" translatable="yes" comments="Number display mode combo: Hexadecimal, e.g. 4D2&#x2081;&#x2086;">Hexadecimal</col>
-        <col id="1">hexadecimal</col>
-      </row>
-    </data>
   </object>
   <object class="GtkListStore" id="word_size_model">
     <columns>
@@ -96,10 +56,10 @@
         <property name="visible">True</property>
         <property name="spacing">5</property>
         <child>
-          <object class="GtkTable" id="table1">
+          <object class="GtkTable" id="preferences_table">
             <property name="visible">True</property>
             <property name="border_width">5</property>
-            <property name="n_rows">4</property>
+            <property name="n_rows">5</property>
             <property name="n_columns">2</property>
             <property name="column_spacing">6</property>
             <property name="row_spacing">12</property>
@@ -112,6 +72,8 @@
                 <property name="mnemonic_widget">angle_unit_combobox</property>
               </object>
               <packing>
+                <property name="top_attach">2</property>
+                <property name="bottom_attach">3</property>
                 <property name="x_options">GTK_FILL</property>
               </packing>
             </child>
@@ -124,8 +86,6 @@
                 <property name="mnemonic_widget">display_format_combobox</property>
               </object>
               <packing>
-                <property name="top_attach">1</property>
-                <property name="bottom_attach">2</property>
                 <property name="x_options">GTK_FILL</property>
               </packing>
             </child>
@@ -138,6 +98,8 @@
               <packing>
                 <property name="left_attach">1</property>
                 <property name="right_attach">2</property>
+                <property name="top_attach">2</property>
+                <property name="bottom_attach">3</property>
               </packing>
             </child>
             <child>
@@ -149,8 +111,6 @@
               <packing>
                 <property name="left_attach">1</property>
                 <property name="right_attach">2</property>
-                <property name="top_attach">1</property>
-                <property name="bottom_attach">2</property>
               </packing>
             </child>
             <child>
@@ -162,8 +122,8 @@
                 <property name="mnemonic_widget">word_size_combobox</property>
               </object>
               <packing>
-                <property name="top_attach">3</property>
-                <property name="bottom_attach">4</property>
+                <property name="top_attach">4</property>
+                <property name="bottom_attach">5</property>
                 <property name="x_options">GTK_FILL</property>
               </packing>
             </child>
@@ -176,8 +136,8 @@
               <packing>
                 <property name="left_attach">1</property>
                 <property name="right_attach">2</property>
-                <property name="top_attach">3</property>
-                <property name="bottom_attach">4</property>
+                <property name="top_attach">4</property>
+                <property name="bottom_attach">5</property>
               </packing>
             </child>
             <child>
@@ -269,8 +229,34 @@
               </object>
               <packing>
                 <property name="right_attach">2</property>
-                <property name="top_attach">2</property>
-                <property name="bottom_attach">3</property>
+                <property name="top_attach">1</property>
+                <property name="bottom_attach">2</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkLabel" id="label1">
+                <property name="visible">True</property>
+                <property name="xalign">0</property>
+                <property name="label" translatable="yes" comments="Preferences dialog: label for word size combo box">Number _base:</property>
+                <property name="use_underline">True</property>
+                <property name="mnemonic_widget">number_base_combobox</property>
+              </object>
+              <packing>
+                <property name="top_attach">3</property>
+                <property name="bottom_attach">4</property>
+                <property name="x_options">GTK_FILL</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkComboBox" id="number_base_combobox">
+                <property name="visible">True</property>
+                <property name="model">number_base_model</property>
+              </object>
+              <packing>
+                <property name="left_attach">1</property>
+                <property name="right_attach">2</property>
+                <property name="top_attach">3</property>
+                <property name="bottom_attach">4</property>
               </packing>
             </child>
           </object>
@@ -314,4 +300,30 @@
     <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/get.h b/src/get.h
index 5d5044f..46c46aa 100644
--- a/src/get.h
+++ b/src/get.h
@@ -27,6 +27,7 @@
 #define R_ZEROES   "showzeroes"
 #define R_TSEP     "showthousands"
 #define R_WORDLEN  "wordlen"
+#define R_BASE     "base"
 
 void resources_init();
 
diff --git a/src/math-buttons.c b/src/math-buttons.c
index a551a93..9b92c0c 100644
--- a/src/math-buttons.c
+++ b/src/math-buttons.c
@@ -292,10 +292,10 @@ load_mode(MathButtons *buttons, ButtonMode mode)
     gint i;
     gchar *name;
     const gchar *builder_file;
-    gchar *objects[] = { "button_panel", "character_code_dialog", "currency_dialog",
-                         "ctrm_dialog", "ddb_dialog", "fv_dialog", "gpm_dialog",
-                         "pmt_dialog", "pv_dialog", "rate_dialog", "sln_dialog",
-                         "syd_dialog", "term_dialog", "adjustment1", "adjustment2", NULL };
+    static gchar *objects[] = { "button_panel", "character_code_dialog", "currency_dialog",
+                                "ctrm_dialog", "ddb_dialog", "fv_dialog", "gpm_dialog",
+                                "pmt_dialog", "pv_dialog", "rate_dialog", "sln_dialog",
+                                "syd_dialog", "term_dialog", "adjustment1", "adjustment2", NULL };
     GtkWidget *widget, **panel;
     GError *error = NULL;
 
diff --git a/src/math-equation.c b/src/math-equation.c
index 3c84a6e..048ca5f 100644
--- a/src/math-equation.c
+++ b/src/math-equation.c
@@ -42,11 +42,11 @@ enum {
     PROP_EQUATION,
     PROP_NUMBER_MODE,
     PROP_ACCURACY,
-    PROP_SHOW_THOUSANDS_SEPARATOR,
+    PROP_SHOW_THOUSANDS_SEPARATORS,
     PROP_SHOW_TRAILING_ZEROES,
-    PROP_FORMAT,
+    PROP_DISPLAY_FORMAT,
     PROP_WORD_SIZE,
-    PROP_ANGLE_UNIT,
+    PROP_ANGLE_UNITS,
     PROP_BASE
 };
 
@@ -75,7 +75,7 @@ struct MathEquationPrivate
     int accuracy;             /* Number of digits to show */
     int base;                 /* Number base */
     int word_size;            /* Word size in bits */
-    MPAngleUnit angle_unit;   /* Units for trigonometric functions */
+    MPAngleUnit angle_units;  /* Units for trigonometric functions */
     NumberMode number_mode;   /* ??? */
     gboolean can_super_minus; /* TRUE if entering minus can generate a superscript minus */
 
@@ -205,18 +205,18 @@ math_equation_get_accuracy(MathEquation *equation)
 
 
 void
-math_equation_set_show_thousands_separator(MathEquation *equation, gboolean visible)
+math_equation_set_show_thousands_separators(MathEquation *equation, gboolean visible)
 {
     if (equation->priv->show_tsep && visible || !equation->priv->show_tsep && !visible)
         return;
     equation->priv->show_tsep = visible;
     display_refresh(equation);
-    g_object_notify(G_OBJECT(equation), "show-thousands-separator");
+    g_object_notify(G_OBJECT(equation), "show-thousands-separators");
 }
 
 
 gboolean
-math_equation_get_show_thousands_separator(MathEquation *equation)
+math_equation_get_show_thousands_separators(MathEquation *equation)
 {
     return equation->priv->show_tsep;
 }
@@ -241,7 +241,7 @@ math_equation_get_show_trailing_zeroes(MathEquation *equation)
 
 
 void
-math_equation_set_format(MathEquation *equation, DisplayFormat format)
+math_equation_set_display_format(MathEquation *equation, DisplayFormat format)
 {
     if (equation->priv->format == format)
         return;
@@ -252,7 +252,7 @@ math_equation_set_format(MathEquation *equation, DisplayFormat format)
 
 
 DisplayFormat
-math_equation_get_format(MathEquation *equation)
+math_equation_get_display_format(MathEquation *equation)
 {
     return equation->priv->format;
 }
@@ -276,19 +276,19 @@ math_equation_get_word_size(MathEquation *equation)
 
 
 void
-math_equation_set_angle_unit(MathEquation *equation, MPAngleUnit angle_unit)
+math_equation_set_angle_units(MathEquation *equation, MPAngleUnit angle_units)
 {
-    if (equation->priv->angle_unit == angle_unit)
+    if (equation->priv->angle_units == angle_units)
         return;
-    equation->priv->angle_unit = angle_unit;
-    g_object_notify(G_OBJECT(equation), "angle-unit");  
+    equation->priv->angle_units = angle_units;
+    g_object_notify(G_OBJECT(equation), "angle-units");
 }
 
 
 MPAngleUnit
-math_equation_get_angle_unit(MathEquation *equation)
+math_equation_get_angle_units(MathEquation *equation)
 {
-    return equation->priv->angle_unit;
+    return equation->priv->angle_units;
 }
 
 
@@ -727,7 +727,7 @@ parse(MathEquation *equation, const char *text, MPNumber *z, char **error_token)
 
     memset(&options, 0, sizeof(options));
     options.wordlen = equation->priv->word_size;
-    options.angle_units = equation->priv->angle_unit;
+    options.angle_units = equation->priv->angle_units;
     options.variable_is_defined = variable_is_defined;
     options.get_variable = get_variable;
     options.set_variable = set_variable;
@@ -1077,20 +1077,20 @@ math_equation_set_property(GObject      *object,
     case PROP_ACCURACY:
       math_equation_set_accuracy(self, g_value_get_int(value));
       break;
-    case PROP_SHOW_THOUSANDS_SEPARATOR:
-      math_equation_set_show_thousands_separator(self, g_value_get_boolean(value));
+    case PROP_SHOW_THOUSANDS_SEPARATORS:
+      math_equation_set_show_thousands_separators(self, g_value_get_boolean(value));
       break;
     case PROP_SHOW_TRAILING_ZEROES:
       math_equation_set_show_trailing_zeroes(self, g_value_get_boolean(value));
       break;
-    case PROP_FORMAT:
-      math_equation_set_format(self, g_value_get_int(value));
+    case PROP_DISPLAY_FORMAT:
+      math_equation_set_display_format(self, g_value_get_int(value));
       break;
     case PROP_WORD_SIZE:
       math_equation_set_word_size(self, g_value_get_int(value));
       break;
-    case PROP_ANGLE_UNIT:
-      math_equation_set_angle_unit(self, g_value_get_int(value));
+    case PROP_ANGLE_UNITS:
+      math_equation_set_angle_units(self, g_value_get_int(value));
       break;
     case PROP_BASE:
       math_equation_set_base(self, g_value_get_int(value));
@@ -1133,20 +1133,20 @@ math_equation_get_property(GObject    *object,
     case PROP_ACCURACY:
       g_value_set_int(value, self->priv->accuracy);
       break;
-    case PROP_SHOW_THOUSANDS_SEPARATOR:
+    case PROP_SHOW_THOUSANDS_SEPARATORS:
       g_value_set_boolean(value, self->priv->show_tsep);
       break;
     case PROP_SHOW_TRAILING_ZEROES:
       g_value_set_boolean(value, self->priv->show_zeroes);
       break;
-    case PROP_FORMAT:
+    case PROP_DISPLAY_FORMAT:
       g_value_set_enum(value, self->priv->format);
       break;
     case PROP_WORD_SIZE:
       g_value_set_int(value, self->priv->word_size);
       break;
-    case PROP_ANGLE_UNIT:
-      g_value_set_enum(value, self->priv->angle_unit);
+    case PROP_ANGLE_UNITS:
+      g_value_set_enum(value, self->priv->angle_units);
       break;
     case PROP_BASE:
       g_value_set_int(value, self->priv->base);
@@ -1233,7 +1233,7 @@ math_equation_class_init (MathEquationClass *klass)
                                                      0, 20, 9,
                                                      G_PARAM_READWRITE));
     g_object_class_install_property(object_class,
-                                    PROP_SHOW_THOUSANDS_SEPARATOR,
+                                    PROP_SHOW_THOUSANDS_SEPARATORS,
                                     g_param_spec_boolean("show-thousands-separators",
                                                          "show-thousands-separators",
                                                          "Show thousands separators",
@@ -1247,9 +1247,9 @@ math_equation_class_init (MathEquationClass *klass)
                                                          FALSE,
                                                          G_PARAM_READWRITE));
     g_object_class_install_property(object_class,
-                                    PROP_FORMAT,
-                                    g_param_spec_enum("format",
-                                                      "format",
+                                    PROP_DISPLAY_FORMAT,
+                                    g_param_spec_enum("display-format",
+                                                      "display-format",
                                                       "Display format",
                                                       display_format_type,
                                                       DEC,
@@ -1262,9 +1262,9 @@ math_equation_class_init (MathEquationClass *klass)
                                                      8, 64, 64,
                                                      G_PARAM_READWRITE));
     g_object_class_install_property(object_class,
-                                    PROP_ANGLE_UNIT,
-                                    g_param_spec_enum("angle-unit",
-                                                      "angle-unit",
+                                    PROP_ANGLE_UNITS,
+                                    g_param_spec_enum("angle-units",
+                                                      "angle-units",
                                                       "Angle units",
                                                       angle_unit_type,
                                                       MP_DEGREES,
@@ -1354,7 +1354,7 @@ math_equation_init(MathEquation *equation)
     equation->priv->base = 10;
     equation->priv->accuracy = 9;
     equation->priv->word_size = 32;
-    equation->priv->angle_unit = MP_DEGREES;
+    equation->priv->angle_units = MP_DEGREES;
 
     mp_set_from_integer(0, &equation->priv->state.ans);
     equation->priv->state.ans_start = -1;
diff --git a/src/math-equation.h b/src/math-equation.h
index 5c14212..e98ed03 100644
--- a/src/math-equation.h
+++ b/src/math-equation.h
@@ -78,14 +78,14 @@ NumberMode math_equation_get_number_mode(MathEquation *equation);
 void math_equation_set_accuracy(MathEquation *equation, int accuracy);
 int math_equation_get_accuracy(MathEquation *equation);
 
-void math_equation_set_show_thousands_separator(MathEquation *equation, gboolean visible);
-gboolean math_equation_get_show_thousands_separator(MathEquation *equation);
+void math_equation_set_show_thousands_separators(MathEquation *equation, gboolean visible);
+gboolean math_equation_get_show_thousands_separators(MathEquation *equation);
 
 void math_equation_set_show_trailing_zeroes(MathEquation *equation, gboolean visible);
 gboolean math_equation_get_show_trailing_zeroes(MathEquation *equation);
 
-void math_equation_set_format(MathEquation *equation, DisplayFormat format);
-DisplayFormat math_equation_get_format(MathEquation *equation);
+void math_equation_set_display_format(MathEquation *equation, DisplayFormat format);
+DisplayFormat math_equation_get_display_format(MathEquation *equation);
 
 void math_equation_set_word_size(MathEquation *equation, int word_size);
 int math_equation_get_word_size(MathEquation *equation);
diff --git a/src/ui-preferences.c b/src/ui-preferences.c
index a70cf5a..424a930 100644
--- a/src/ui-preferences.c
+++ b/src/ui-preferences.c
@@ -20,7 +20,19 @@
 #include <gtk/gtk.h>
 
 #include "ui-preferences.h"
-#include "get.h"
+
+G_DEFINE_TYPE (PreferencesDialog, ui_preferences, GTK_TYPE_DIALOG);
+
+enum {
+    PROP_0,
+    PROP_EQUATION
+};
+
+struct PreferencesDialogPrivate
+{
+    MathEquation *equation;
+    GtkBuilder *ui;
+};
 
 #define UI_DIALOGS_FILE  UI_DIR "/preferences.ui"
 #define GET_WIDGET(ui, name) \
@@ -28,29 +40,23 @@
 
 
 PreferencesDialog *
-ui_preferences_dialog_new(GCalctoolUI *ui)
-{
-    PreferencesDialog *dialog;
-  
-    dialog = g_malloc0(sizeof(PreferencesDialog));
-    dialog->ui = ui;
-    return dialog;
+ui_preferences_dialog_new(MathEquation *equation)
+{  
+    return g_object_new (ui_preferences_get_type(), "equation", equation, NULL);
 }
 
 
-G_MODULE_EXPORT
-void
-preferences_response_cb(GtkWidget *widget, gint id, PreferencesDialog *dialog)
+static void
+preferences_response_cb(GtkWidget *widget, gint id)
 {
-    gtk_widget_hide(dialog->dialog);
+    gtk_widget_hide(widget);
 }
 
 
-G_MODULE_EXPORT
-gboolean
-preferences_dialog_delete_cb(GtkWidget *widget, GdkEvent *event, PreferencesDialog *dialog)
+static gboolean
+preferences_dialog_delete_cb(GtkWidget *widget, GdkEvent *event)
 {
-    preferences_response_cb(widget, 0, dialog);
+    preferences_response_cb(widget, 0);
     return TRUE;
 }
 
@@ -59,29 +65,14 @@ G_MODULE_EXPORT
 void
 angle_unit_combobox_changed_cb(GtkWidget *combo, PreferencesDialog *dialog)
 {
-    int i;
-    const gchar *value;
+    MPAngleUnit value;
     GtkTreeModel *model;
     GtkTreeIter iter;
-    struct
-    {
-        const gchar *value;
-        MPAngleUnit units;
-    } unit_map[] =
-    {
-        {"degrees",     MP_DEGREES},
-        {"radians" ,    MP_RADIANS},
-        {"gradians",    MP_GRADIANS},
-        {NULL,          MP_DEGREES}
-    };
 
     model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
     gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter);
     gtk_tree_model_get(model, &iter, 1, &value, -1);
-    for (i = 0; unit_map[i].value != NULL && strcmp(unit_map[i].value, value) != 0; i++);
-    math_equation_set_angle_unit(ui_get_equation(dialog->ui), unit_map[i].units);
-
-    set_resource(R_TRIG, value);
+    math_equation_set_angle_units(dialog->priv->equation, value);
 }
 
 
@@ -89,32 +80,14 @@ G_MODULE_EXPORT
 void
 display_format_combobox_changed_cb(GtkWidget *combo, PreferencesDialog *dialog)
 {
-    int i;
-    const gchar *value;
+    DisplayFormat value;
     GtkTreeModel *model;
     GtkTreeIter iter;
-    struct
-    {
-        const gchar *value;
-        DisplayFormat format;
-    } mode_map[] =
-    {
-        {"decimal",     DEC},
-        {"binary" ,     BIN},
-        {"octal",       OCT},
-        {"hexadecimal", HEX},
-        {"scientific",  SCI},
-        {"engineering", ENG},
-        {NULL,          DEC}
-    };
 
     model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
     gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter);
     gtk_tree_model_get(model, &iter, 1, &value, -1);
-    for (i = 0; mode_map[i].value != NULL && strcmp(mode_map[i].value, value) != 0; i++);
-    math_equation_set_format(ui_get_equation(dialog->ui), mode_map[i].format);
-
-    set_resource(R_DISPLAY, value);
+    math_equation_set_display_format(dialog->priv->equation, value);
 }
 
 
@@ -129,9 +102,7 @@ word_size_combobox_changed_cb(GtkWidget *combo, PreferencesDialog *dialog)
     model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
     gtk_combo_box_get_active_iter(GTK_COMBO_BOX(combo), &iter);
     gtk_tree_model_get(model, &iter, 1, &value, -1);
-    math_equation_set_word_size(ui_get_equation(dialog->ui), value);
-
-    set_int_resource(R_WORDLEN, value);
+    math_equation_set_word_size(dialog->priv->equation, value);
 }
 
 
@@ -142,9 +113,7 @@ decimal_places_spin_change_value_cb(GtkWidget *spin, PreferencesDialog *dialog)
     gint value = 0;
 
     value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
-    math_equation_set_accuracy(ui_get_equation(dialog->ui), value);
-
-    set_int_resource(R_ACCURACY, value);
+    math_equation_set_accuracy(dialog->priv->equation, value);
 }
 
 
@@ -155,8 +124,7 @@ thousands_separator_check_toggled_cb(GtkWidget *check, PreferencesDialog *dialog
     gboolean value;
 
     value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check));
-    math_equation_set_show_thousands_separator(ui_get_equation(dialog->ui), value);
-    set_boolean_resource(R_TSEP, value);
+    math_equation_set_show_thousands_separators(dialog->priv->equation, value);
 }
 
 
@@ -167,106 +135,173 @@ trailing_zeroes_check_toggled_cb(GtkWidget *check, PreferencesDialog *dialog)
     gboolean value;
 
     value = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check));
-    math_equation_set_show_trailing_zeroes(ui_get_equation(dialog->ui), value);
-    set_boolean_resource(R_ZEROES, value);
+    math_equation_set_show_trailing_zeroes(dialog->priv->equation, value);
 }
 
 
 static void
-set_combo_box_from_config(PreferencesDialog *dialog, const gchar *name, const gchar *key_name, GType key_type)
+set_combo_box_from_int(GtkWidget *combo, int value)
 {
-    GtkWidget *combo;
     GtkTreeModel *model;
-    gchar *str_key_value = NULL;
-    int int_key_value;
     GtkTreeIter iter;
     gboolean valid;
 
-    combo = GET_WIDGET(dialog->dialog_ui, name);
     model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo));
     valid = gtk_tree_model_get_iter_first(model, &iter);
 
-    switch (key_type)
-    {
-    case G_TYPE_STRING:
-        str_key_value = get_resource(key_name);
-        if (!str_key_value)
-            valid = FALSE;
-        break;
-    case G_TYPE_INT:
-        if (!get_int_resource(key_name, &int_key_value))
-            valid = FALSE;
-        break;
-    default:
-        break;
-    }
-
     while (valid) {
-        gchar *str_value;
-        gint int_value;
+        gint v;
         gboolean matched = FALSE;
 
-        switch (key_type)
-        {
-        case G_TYPE_STRING:
-            gtk_tree_model_get(model, &iter, 1, &str_value, -1);
-            matched = strcmp(str_value, str_key_value) == 0;
+        gtk_tree_model_get(model, &iter, 1, &v, -1);
+        if (v == value)
             break;
-        case G_TYPE_INT:
-            gtk_tree_model_get(model, &iter, 1, &int_value, -1);
-            matched = int_value == int_key_value;
-            break;
-        default:
-            break;
-        }
-
-        if (matched)
-            break;
-
         valid = gtk_tree_model_iter_next(model, &iter);
     }
     if (!valid)
         valid = gtk_tree_model_get_iter_first(model, &iter);
 
     gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo), &iter);
+}
 
-    g_free(str_key_value);
+
+static void
+accuracy_cb(MathEquation *equation, GParamSpec *spec, PreferencesDialog *dialog)
+{
+    gtk_spin_button_set_value(GTK_SPIN_BUTTON(gtk_builder_get_object(dialog->priv->ui, "decimal_places_spin")),
+                              math_equation_get_accuracy(equation));
 }
 
 
-void
-ui_preferences_show(PreferencesDialog *dialog)
-{  
+static void
+show_thousands_separators_cb(MathEquation *equation, GParamSpec *spec, PreferencesDialog *dialog)
+{
+    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(dialog->priv->ui, "thousands_separator_check")),
+                                 math_equation_get_show_thousands_separators(equation));
+}
+
+
+static void
+show_trailing_zeroes_cb(MathEquation *equation, GParamSpec *spec, PreferencesDialog *dialog)
+{
+    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(dialog->priv->ui, "trailing_zeroes_check")),
+                                 math_equation_get_show_trailing_zeroes(equation));
+}
+
+
+static void
+display_format_cb(MathEquation *equation, GParamSpec *spec, PreferencesDialog *dialog)
+{
+    set_combo_box_from_int(GET_WIDGET(dialog->priv->ui, "display_format_combobox"), math_equation_get_display_format(equation));
+}
+
+
+static void
+word_size_cb(MathEquation *equation, GParamSpec *spec, PreferencesDialog *dialog)
+{
+    set_combo_box_from_int(GET_WIDGET(dialog->priv->ui, "word_size_combobox"), math_equation_get_word_size(equation));
+}
+
+
+static void
+angle_unit_cb(MathEquation *equation, GParamSpec *spec, PreferencesDialog *dialog)
+{
+    set_combo_box_from_int(GET_WIDGET(dialog->priv->ui, "angle_unit_combobox"), math_equation_get_angle_units(equation));  
+}
+
+
+static void
+base_cb(MathEquation *equation, GParamSpec *spec, PreferencesDialog *dialog)
+{
+    set_combo_box_from_int(GET_WIDGET(dialog->priv->ui, "number_base_combobox"), math_equation_get_base(equation));
+}
+
+
+static void
+create_gui(PreferencesDialog *dialog)
+{
     GtkWidget *widget;
+    GtkTreeModel *model;
+    GtkTreeIter iter;
     GtkCellRenderer *renderer;
     gchar *string, **tokens;
-    int value;
-  
-    if (dialog->dialog_ui) {
-        gtk_window_present(GTK_WINDOW(dialog->dialog));
-        return;
-    }
+    GError *error = NULL;
+    static gchar *objects[] = { "preferences_table", "angle_unit_model", "display_format_model",
+                                "word_size_model", "decimal_places_adjustment", "number_base_model", NULL };
 
     // FIXME: Handle errors
-    dialog->dialog_ui = gtk_builder_new();
-    gtk_builder_add_from_file(dialog->dialog_ui, UI_DIALOGS_FILE, NULL);
-
-    dialog->dialog = GET_WIDGET(dialog->dialog_ui, "preferences_dialog");
-    //FIXME: gtk_window_set_transient_for(GTK_WINDOW(dialog->dialog), GTK_WINDOW(dialog->ui->main_window));
-
-    /* Configuration dialog */
+    dialog->priv->ui = gtk_builder_new();
+    gtk_builder_add_objects_from_file(dialog->priv->ui, UI_DIALOGS_FILE, objects, &error);
+    if (error)
+        g_warning("Error loading preferences UI: %s", error->message);
+    g_clear_error(&error);
+
+    gtk_window_set_title(GTK_WINDOW(dialog),
+                         /* Title of preferences dialog */
+                         _("Preferences")); 
+    gtk_container_set_border_width(GTK_CONTAINER(dialog), 8);
+    gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
+    gtk_dialog_add_button(GTK_DIALOG(dialog),
+                          /* Label on close button in preferences dialog */
+                          _("_Close"), 0);
+    g_signal_connect(dialog, "response", G_CALLBACK(preferences_response_cb), NULL);
+    g_signal_connect(dialog, "delete-event", G_CALLBACK(preferences_dialog_delete_cb), NULL);
+    gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), GET_WIDGET(dialog->priv->ui, "preferences_table"), TRUE, TRUE, 0);
+
+    widget = GET_WIDGET(dialog->priv->ui, "angle_unit_combobox");
+    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,
+                       /* Preferences dialog: Angle unit combo box: Use degrees for trigonometric calculations */
+                       _("Degrees"), 1, MP_DEGREES, -1);
+    gtk_list_store_append(GTK_LIST_STORE(model), &iter);
+    gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
+                       /* Preferences dialog: Angle unit combo box: Use radians for trigonometric calculations */
+                       _("Radians"), 1, MP_RADIANS, -1);
+    gtk_list_store_append(GTK_LIST_STORE(model), &iter);
+    gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0,
+                       /* Preferences dialog: Angle unit combo box: Use gradians for trigonometric calculations */
+                       _("Gradians"), 1, MP_GRADIANS, -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);
 
-    widget = GET_WIDGET(dialog->dialog_ui, "angle_unit_combobox");
+    widget = GET_WIDGET(dialog->priv->ui, "display_format_combobox");
+    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);
+    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 */
+                       _("Scientific"), 1, SCI, -1);
+    gtk_list_store_append(GTK_LIST_STORE(model), &iter);
+    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);
 
-    widget = GET_WIDGET(dialog->dialog_ui, "display_format_combobox");
+    widget = GET_WIDGET(dialog->priv->ui, "number_base_combobox");
     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);
 
-    widget = GET_WIDGET(dialog->dialog_ui, "word_size_combobox");
+    widget = GET_WIDGET(dialog->priv->ui, "word_size_combobox");
     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);
@@ -274,7 +309,7 @@ ui_preferences_show(PreferencesDialog *dialog)
     /* Label used in preferences dialog.  The %d is replaced by a spinbutton */
     string = _("Show %d decimal _places");
     tokens = g_strsplit(string, "%d", 2);
-    widget = GET_WIDGET(dialog->dialog_ui, "decimal_places_label1");
+    widget = GET_WIDGET(dialog->priv->ui, "decimal_places_label1");
     if (tokens[0])
         string = g_strstrip(tokens[0]);
     else
@@ -284,7 +319,7 @@ ui_preferences_show(PreferencesDialog *dialog)
     else
         gtk_widget_hide(widget);
 
-    widget = GET_WIDGET(dialog->dialog_ui, "decimal_places_label2");
+    widget = GET_WIDGET(dialog->priv->ui, "decimal_places_label2");
     if (tokens[0] && tokens[1])
         string = g_strstrip(tokens[1]);
     else
@@ -296,23 +331,91 @@ ui_preferences_show(PreferencesDialog *dialog)
 
     g_strfreev(tokens);
 
-    set_combo_box_from_config(dialog, "angle_unit_combobox", R_TRIG, G_TYPE_STRING);
-    set_combo_box_from_config(dialog, "display_format_combobox", R_DISPLAY, G_TYPE_STRING);
-    set_combo_box_from_config(dialog, "word_size_combobox", R_WORDLEN, G_TYPE_INT);
+    gtk_builder_connect_signals(dialog->priv->ui, dialog);
+
+    g_signal_connect(dialog->priv->equation, "notify::accuracy", G_CALLBACK(accuracy_cb), dialog);
+    g_signal_connect(dialog->priv->equation, "notify::show-thousands-separators", G_CALLBACK(show_thousands_separators_cb), dialog);
+    g_signal_connect(dialog->priv->equation, "notify::show-trailing_zeroes", G_CALLBACK(show_trailing_zeroes_cb), dialog);
+    g_signal_connect(dialog->priv->equation, "notify::display_format", G_CALLBACK(display_format_cb), dialog);
+    g_signal_connect(dialog->priv->equation, "notify::word-size", G_CALLBACK(word_size_cb), dialog);
+    g_signal_connect(dialog->priv->equation, "notify::angle-unit", G_CALLBACK(angle_unit_cb), dialog);
+    g_signal_connect(dialog->priv->equation, "notify::base", G_CALLBACK(base_cb), dialog);
+
+    accuracy_cb(dialog->priv->equation, NULL, dialog);
+    show_thousands_separators_cb(dialog->priv->equation, NULL, dialog);
+    show_trailing_zeroes_cb(dialog->priv->equation, NULL, dialog);
+    display_format_cb(dialog->priv->equation, NULL, dialog);
+    word_size_cb(dialog->priv->equation, NULL, dialog);
+    angle_unit_cb(dialog->priv->equation, NULL, dialog);
+    base_cb(dialog->priv->equation, NULL, dialog);
+}
+
+
+static void
+ui_preferences_set_property(GObject      *object,
+                            guint         prop_id,
+                            const GValue *value,
+                            GParamSpec   *pspec)
+{
+    PreferencesDialog *self;
+
+    self = UI_PREFERENCES (object);
 
-    if (!get_int_resource(R_ACCURACY, &value))
-        value = 9;
-    gtk_spin_button_set_value(GTK_SPIN_BUTTON(gtk_builder_get_object(dialog->dialog_ui, "decimal_places_spin")), value);
+    switch (prop_id) {
+    case PROP_EQUATION:
+        self->priv->equation = g_value_get_object (value);
+        create_gui(self);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        break;
+    }
+}
 
-    if (!get_boolean_resource(R_TSEP, &value))
-        value = FALSE;
-    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(dialog->dialog_ui, "thousands_separator_check")), value);
 
-    if (!get_boolean_resource(R_ZEROES, &value))
-        value = FALSE;
-    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(gtk_builder_get_object(dialog->dialog_ui, "trailing_zeroes_check")), value);
+static void
+ui_preferences_get_property(GObject    *object,
+                            guint       prop_id,
+                            GValue     *value,
+                            GParamSpec *pspec)
+{
+    PreferencesDialog *self;
 
-    gtk_builder_connect_signals(dialog->dialog_ui, dialog);
+    self = UI_PREFERENCES (object);
 
-    gtk_window_present(GTK_WINDOW(dialog->dialog));
+    switch (prop_id) {
+    case PROP_EQUATION:
+        g_value_set_object (value, self->priv->equation);
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        break;
+    }
+}
+
+
+static void
+ui_preferences_class_init (PreferencesDialogClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+    object_class->get_property = ui_preferences_get_property;
+    object_class->set_property = ui_preferences_set_property;
+
+    g_type_class_add_private (klass, sizeof (PreferencesDialogPrivate));
+
+    g_object_class_install_property(object_class,
+                                    PROP_EQUATION,
+                                    g_param_spec_object("equation",
+                                                        "equation",
+                                                        "Equation being configured",
+                                                        math_equation_get_type(),
+                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+}
+
+
+static void
+ui_preferences_init(PreferencesDialog *dialog)
+{
+    dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog, ui_preferences_get_type(), PreferencesDialogPrivate);
 }
diff --git a/src/ui-preferences.h b/src/ui-preferences.h
index f84c465..f6b62fc 100644
--- a/src/ui-preferences.h
+++ b/src/ui-preferences.h
@@ -19,19 +19,29 @@
 #ifndef UI_PREFERENCES_H
 #define UI_PREFERENCES_H
 
-typedef struct PreferencesDialog PreferencesDialog;
+#include <glib-object.h>
+#include <gtk/gtk.h>
+#include "math-equation.h"
 
-#include "ui.h"
+G_BEGIN_DECLS
 
-struct PreferencesDialog
+#define UI_PREFERENCES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ui_preferences_get_type(), PreferencesDialog))
+
+typedef struct PreferencesDialogPrivate PreferencesDialogPrivate;
+
+typedef struct
+{
+    GtkDialog                 parent_instance;
+    PreferencesDialogPrivate *priv;
+} PreferencesDialog;
+
+typedef struct
 {
-    GCalctoolUI *ui;
-    GtkBuilder *dialog_ui;
-    GtkWidget *dialog;
-};
+    GtkDialogClass parent_class;
+} PreferencesDialogClass;
 
-PreferencesDialog *ui_preferences_dialog_new(GCalctoolUI *ui);
+GType ui_preferences_get_type();
 
-void ui_preferences_show(PreferencesDialog *dialog);
+PreferencesDialog *ui_preferences_dialog_new(MathEquation *equation);
 
 #endif /* UI_PREFERENCES_H */
diff --git a/src/ui.c b/src/ui.c
index 44a2409..49bb325 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -192,7 +192,11 @@ G_MODULE_EXPORT
 void
 show_preferences_cb(GtkMenuItem *menu, GCalctoolUI *ui)
 {
-    ui_preferences_show(ui->priv->preferences_dialog);
+    if (!ui->priv->preferences_dialog) {
+        ui->priv->preferences_dialog = ui_preferences_dialog_new(ui->priv->equation);
+        gtk_window_set_transient_for(GTK_WINDOW(ui->priv->preferences_dialog), GTK_WINDOW(ui->priv->main_window));
+    }
+    gtk_window_present(GTK_WINDOW(ui->priv->preferences_dialog));
 }
 
 
@@ -318,10 +322,27 @@ ui_init(GCalctoolUI *ui)
     GtkWidget *scrolled_window;
     GError *error = NULL;
     int i;
+    int accuracy = 9, base = 10, word_size = 64;
+    gchar *angle_units;
+    gboolean show_tsep = FALSE, show_zeroes = FALSE;
+    gchar *display_format, *angle_unit;
 
     ui->priv = G_TYPE_INSTANCE_GET_PRIVATE (ui, ui_get_type(), GCalctoolUIPrivate);
   
     ui->priv->equation = math_equation_new();
+    get_int_resource(R_ACCURACY, &accuracy);
+    get_int_resource(R_BASE, &accuracy);
+    get_int_resource(R_WORDLEN, &word_size);
+    get_boolean_resource(R_TSEP, &show_tsep);  
+    get_boolean_resource(R_ZEROES, &show_zeroes);
+    display_format = get_resource(R_DISPLAY);
+    angle_units = get_resource(R_TRIG);
+  
+    math_equation_set_accuracy(ui->priv->equation, accuracy);
+    g_free(display_format);
+    g_free(angle_units);
+  
+    // FIXME: Save these on quit
 
     ui->priv->ui = gtk_builder_new();
     gtk_builder_add_from_file(ui->priv->ui, UI_FILE, &error);
@@ -361,6 +382,4 @@ ui_init(GCalctoolUI *ui)
     ui->priv->buttons = math_buttons_new(ui->priv->equation);
     gtk_box_pack_start(GTK_BOX(GET_WIDGET(ui->priv->ui, "window_vbox")), GTK_WIDGET(ui->priv->buttons), TRUE, TRUE, 0);
     gtk_widget_show(GTK_WIDGET(ui->priv->buttons));
-
-    ui->priv->preferences_dialog = ui_preferences_dialog_new(ui);
 }



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