[gcalctool] Move register code into a GObject



commit c807a8fc54cc390e52c4fc78320c7c22d8f2e21c
Author: Robert Ancell <robert ancell gmail com>
Date:   Wed Apr 21 09:13:52 2010 +1000

    Move register code into a GObject

 src/Makefile.am     |    4 +-
 src/gcalctool.c     |    2 -
 src/math-buttons.c  |   13 +++--
 src/math-equation.c |   24 ++++++--
 src/math-equation.h |    3 +
 src/math-window.c   |    1 +
 src/mp-equation.c   |    4 +-
 src/mp-equation.h   |    4 +-
 src/register.c      |  144 ---------------------------------------------------
 src/register.h      |   29 ----------
 src/unittest.c      |    2 +-
 11 files changed, 37 insertions(+), 193 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 8f99012..4255bd7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,6 +19,8 @@ gcalctool_SOURCES = \
 	math-equation.h \
 	math-preferences.c \
 	math-preferences.h \
+	math-variables.c \
+	math-variables.h \
 	math-window.c \
 	math-window.h \
 	mp.c \
@@ -36,8 +38,6 @@ gcalctool_SOURCES = \
 	mp-equation-parser.h \
 	financial.c \
 	financial.h \
-	register.c \
-	register.h \
 	unittest.c \
 	unittest.h
 
diff --git a/src/gcalctool.c b/src/gcalctool.c
index 359bb14..af39452 100644
--- a/src/gcalctool.c
+++ b/src/gcalctool.c
@@ -25,7 +25,6 @@
 #include "currency.h"
 #include "unittest.h"
 #include "math-window.h"
-#include "register.h"
 #include "mp-equation.h"
 
 static GConfClient *client = NULL;
@@ -351,7 +350,6 @@ main(int argc, char **argv)
     /* Seed random number generator. */
     srand48((long) time((time_t *) 0));
 
-    register_init();
     get_options(argc, argv);
 
     client = gconf_client_get_default();
diff --git a/src/math-buttons.c b/src/math-buttons.c
index 74fe3bd..fd36fbf 100644
--- a/src/math-buttons.c
+++ b/src/math-buttons.c
@@ -19,7 +19,6 @@
 #include <glib/gi18n.h>
 
 #include "math-buttons.h"
-#include "register.h"
 #include "financial.h"
 #include "currency.h"
 
@@ -1063,7 +1062,7 @@ store_cb(GtkWidget *widget, MathButtons *buttons)
     gtk_menu_set_reserve_toggle_size(GTK_MENU(menu), FALSE);
     set_tint(menu, &buttons->priv->color_memory, 1);
 
-    names = register_get_names();
+    names = math_variables_get_names(math_equation_get_variables(buttons->priv->equation));
     if (names[0] == NULL) {
         item = gtk_menu_item_new_with_label(/* Text shown in store menu when no variables defined */
                                             _("No variables defined"));
@@ -1071,7 +1070,9 @@ store_cb(GtkWidget *widget, MathButtons *buttons)
         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
     }  
     for (i = 0; names[i]; i++) {
-        item = make_register_menu_item(buttons, names[i], register_get_value(names[i]), TRUE, G_CALLBACK(store_menu_cb));
+        MPNumber *value;
+        value = math_variables_get_value(math_equation_get_variables(buttons->priv->equation), names[i]);
+        item = make_register_menu_item(buttons, names[i], value, TRUE, G_CALLBACK(store_menu_cb));
         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
     }
 
@@ -1104,7 +1105,7 @@ recall_cb(GtkWidget *widget, MathButtons *buttons)
     gtk_menu_set_reserve_toggle_size(GTK_MENU(menu), FALSE);
     set_tint(menu, &buttons->priv->color_memory, 1);
 
-    names = register_get_names();
+    names = math_variables_get_names(math_equation_get_variables(buttons->priv->equation));
     if (names[0] == NULL) {
         item = gtk_menu_item_new_with_label(/* Text shown in recall menu when no variables defined */
                                             _("No variables defined"));
@@ -1112,7 +1113,9 @@ recall_cb(GtkWidget *widget, MathButtons *buttons)
         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
     }  
     for (i = 0; names[i]; i++) {
-        item = make_register_menu_item(buttons, names[i], register_get_value(names[i]), TRUE, G_CALLBACK(recall_menu_cb));
+        MPNumber *value;
+        value = math_variables_get_value(math_equation_get_variables(buttons->priv->equation), names[i]);
+        item = make_register_menu_item(buttons, names[i], value, TRUE, G_CALLBACK(recall_menu_cb));
         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
     }
 
diff --git a/src/math-equation.c b/src/math-equation.c
index 39f915c..c698cee 100644
--- a/src/math-equation.c
+++ b/src/math-equation.c
@@ -31,7 +31,6 @@
 
 #include "mp.h"
 #include "mp-equation.h"
-#include "register.h"
 #include "currency.h"
 
 
@@ -93,6 +92,8 @@ struct MathEquationPrivate
 
     gboolean in_delete;
 
+    MathVariables *variables;
+
     // FIXME: Replace with GtkClipboard
     GdkAtom clipboard_atom;   /* ??? */ // 
     GdkAtom primary_atom;     /* ??? */ // FIXME: Is this middle click?
@@ -109,6 +110,13 @@ math_equation_new()
 }
 
 
+MathVariables *
+math_equation_get_variables(MathEquation *equation)
+{
+    return equation->priv->variables;
+}
+
+
 static void
 get_ans_offsets(MathEquation *equation, gint *start, gint *end)
 {
@@ -692,7 +700,7 @@ math_equation_store(MathEquation *equation, const gchar *name)
     if (!math_equation_get_number(equation, &t))
         math_equation_set_status(equation, _("No sane value to store"));
     else
-        register_set_value(name, &t);
+        math_variables_set_value(equation->priv->variables, name, &t);
 }
 
 
@@ -819,8 +827,9 @@ math_equation_insert_subtract(MathEquation *equation)
 
 
 static int
-variable_is_defined(const char *name)
+variable_is_defined(const char *name, void *data)
 {
+    MathEquation *equation = data;
     char *c, *lower_name;
 
     lower_name = strdup(name);
@@ -834,7 +843,7 @@ variable_is_defined(const char *name)
     }
     g_free (lower_name);
 
-    return register_get_value(name) != NULL;
+    return math_variables_get_value(equation->priv->variables, name) != NULL;
 }
 
 
@@ -855,7 +864,7 @@ get_variable(const char *name, MPNumber *z, void *data)
     else if (strcmp(lower_name, "ans") == 0)
         mp_set_from_mp(&equation->priv->state.ans, z);
     else {
-        t = register_get_value(name);
+        t = math_variables_get_value(equation->priv->variables, name);
         if (t)
             mp_set_from_mp(t, z);
         else
@@ -871,8 +880,9 @@ get_variable(const char *name, MPNumber *z, void *data)
 static void
 set_variable(const char *name, const MPNumber *x, void *data)
 {
+    MathEquation *equation = data;
     /* FIXME: Don't allow writing to built-in variables, e.g. ans, rand, sin, ... */
-    register_set_value(name, x);
+    math_variables_set_value(equation->priv->variables, name, x);
 }
 
 
@@ -1560,6 +1570,8 @@ math_equation_init(MathEquation *equation)
     equation->priv->tsep = tsep ? g_locale_to_utf8(tsep, -1, NULL, NULL, NULL) : g_strdup(",");
 
     equation->priv->tsep_count = 3;
+  
+    equation->priv->variables = math_variables_new();
 
     // Use GtkClipboad instead
     equation->priv->primary_atom = gdk_atom_intern("PRIMARY", FALSE);
diff --git a/src/math-equation.h b/src/math-equation.h
index 3d32ed2..a4b2a13 100644
--- a/src/math-equation.h
+++ b/src/math-equation.h
@@ -23,6 +23,7 @@
 #include <glib-object.h>
 #include <gtk/gtk.h>
 #include "mp.h"
+#include "math-variables.h"
 
 G_BEGIN_DECLS
 
@@ -60,6 +61,8 @@ typedef enum {
 GType math_equation_get_type();
 MathEquation *math_equation_new();
 
+MathVariables *math_equation_get_variables(MathEquation *equation);
+
 const gchar *math_equation_get_digit_text(MathEquation *equation, guint digit);
 const gchar *math_equation_get_numeric_point_text(MathEquation *equation);
 
diff --git a/src/math-window.c b/src/math-window.c
index acbe1a9..18662b6 100644
--- a/src/math-window.c
+++ b/src/math-window.c
@@ -285,6 +285,7 @@ button_mode_changed_cb(MathButtons *buttons, GParamSpec *spec, MathWindow *windo
     {
     case BASIC:
       menu = window->priv->mode_basic_menu_item;
+      //FIXME: Should it revert to decimal mode? math_equation_set_number_format(window->priv->equation, DEC);
       break;
 
     case ADVANCED:
diff --git a/src/mp-equation.c b/src/mp-equation.c
index d443fb4..ceac748 100644
--- a/src/mp-equation.c
+++ b/src/mp-equation.c
@@ -33,7 +33,7 @@ variable_is_defined(MPEquationParserState *state, const char *name)
     if (strcmp(name, "e") == 0 || strcmp(name, "i") == 0 || strcmp(name, "Ï?") == 0)
         return 1;
     if (state->options->variable_is_defined)
-        return state->options->variable_is_defined(name);
+        return state->options->variable_is_defined(name, state->options->callback_data);
     return 0;
 }
 
@@ -147,7 +147,7 @@ function_is_defined(MPEquationParserState *state, const char *name)
     g_free (lower_name);
 
     if (state->options->function_is_defined)
-        return state->options->function_is_defined(name);
+        return state->options->function_is_defined(name, state->options->callback_data);
     return 0;
 }
 
diff --git a/src/mp-equation.h b/src/mp-equation.h
index 0424654..60e6690 100644
--- a/src/mp-equation.h
+++ b/src/mp-equation.h
@@ -51,7 +51,7 @@ typedef struct {
     void *callback_data;
   
     /* Function to check if a variable is defined */
-    int (*variable_is_defined)(const char *name);
+    int (*variable_is_defined)(const char *name, void *data);
 
     /* Function to get variable values */
     int (*get_variable)(const char *name, MPNumber *z, void *data);
@@ -60,7 +60,7 @@ typedef struct {
     void (*set_variable)(const char *name, const MPNumber *x, void *data);
 
     /* Function to check if a function is defined */
-    int (*function_is_defined)(const char *name);
+    int (*function_is_defined)(const char *name, void *data);
 
     /* Function to solve functions */
     int (*get_function)(const char *name, const MPNumber *x, MPNumber *z, void *data);
diff --git a/src/unittest.c b/src/unittest.c
index f91a5dd..ac57e21 100644
--- a/src/unittest.c
+++ b/src/unittest.c
@@ -129,7 +129,7 @@ test_conversions()
 
 
 int
-variable_is_defined(const char *name)
+variable_is_defined(const char *name, void *data)
 {
     return strcmp (name, "x") == 0 || strcmp (name, "y") == 0;
 }



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