[gcalctool] Killed calctool.h and associated global variables



commit 82b3506fb6e830692a3eaa5d6c1a35c3da6cf9c8
Author: Robert Ancell <robert ancell gmail com>
Date:   Tue Apr 6 09:52:46 2010 +1000

    Killed calctool.h and associated global variables

 src/Makefile.am      |    2 +-
 src/calctool.c       |   77 +++++--------------
 src/calctool.h       |   62 ---------------
 src/financial.c      |    5 +-
 src/get.c            |   14 ++--
 src/get.h            |    2 -
 src/math-buttons.c   |   16 ++--
 src/math-buttons.h   |    2 -
 src/math-display.c   |  100 ++++++++++++-------------
 src/math-display.h   |    5 -
 src/math-equation.c  |  201 +++++++++++++++++++++++++++++++++++++++++---------
 src/math-equation.h  |   17 ++++-
 src/ui-preferences.c |    1 +
 src/ui.c             |    1 +
 14 files changed, 271 insertions(+), 234 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 6b490ca..7e063ac 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,13 +2,13 @@ bin_PROGRAMS = gcalctool gcalccmd
 
 INCLUDES = \
 	-DUI_DIR=\""$(datadir)/gcalctool"\" \
+	-DVERSION=\""$(VERSION)"\" \
 	-DLOCALE_DIR=\""$(localedir)"\" \
     -DGETTEXT_PACKAGE=\"$(GETTEXT_PACKAGE)\" \
 	$(GCALCTOOL_CFLAGS)
 
 gcalctool_SOURCES = \
 	calctool.c \
-	calctool.h \
 	currency.c \
 	currency.h \
 	get.c \
diff --git a/src/calctool.c b/src/calctool.c
index 61438c3..67df778 100644
--- a/src/calctool.c
+++ b/src/calctool.c
@@ -23,7 +23,6 @@
 #include <sys/types.h>
 #include <glib-object.h>
 
-#include "calctool.h"
 #include "currency.h"
 #include "unittest.h"
 #include "get.h"
@@ -31,26 +30,22 @@
 #include "register.h"
 #include "mp-equation.h"
 
-/* Calctool variables and options. */
-static CalculatorVariables calc_state;
-CalculatorVariables *v;
-
-GCalctoolUI *X;
 
 static void
-version()
+version(const gchar *progname)
 {
     /* NOTE: Is not translated so can be easily parsed */
-    fprintf(stderr, "%1$s %2$s\n", v->progname, VERSION);
+    fprintf(stderr, "%1$s %2$s\n", progname, VERSION);
 }
 
+
 static void
 solve(const char *equation)
 {
     MPEquationOptions options;
     MPErrorCode error;
     MPNumber result;
-    char result_str[MAXLINE];
+    char result_str[1024];
 
     memset(&options, 0, sizeof(options));
     options.wordlen = 32;
@@ -66,19 +61,20 @@ solve(const char *equation)
         exit(1);
     }
     else {
-        mp_cast_to_string(&result, 10, 9, 1, result_str, MAXLINE);
+        mp_cast_to_string(&result, 10, 9, 1, result_str, 1024);
         printf("%s\n", result_str);
         exit(0);
     }
 }
 
+
 static void
-usage(int show_gtk)
+usage(const gchar *progname, int show_gtk)
 {
     fprintf(stderr,
             /* Description on how to use gcalctool displayed on command-line */
             _("Usage:\n"
-              "  %s â?? Perform mathematical calculations"), v->progname);
+              "  %s â?? Perform mathematical calculations"), progname);
 
     fprintf(stderr,
             "\n\n");
@@ -116,28 +112,31 @@ usage(int show_gtk)
             "\n\n");
 }
 
+
 void
 get_options(int argc, char *argv[])
 {
     int i;
-    char *arg;
+    char *progname, *arg;
+
+    progname = g_path_get_basename(argv[0]);
 
     for (i = 1; i < argc; i++) {
         arg = argv[i];
 
         if (strcmp(arg, "-v") == 0 ||
             strcmp(arg, "--version") == 0) {
-            version();
+            version(progname);
             exit(0);
         }
         else if (strcmp(arg, "-h") == 0 ||
                  strcmp(arg, "-?") == 0 ||
                  strcmp(arg, "--help") == 0) {
-            usage(FALSE);
+            usage(progname, FALSE);
             exit(0);
         }
         else if (strcmp(arg, "--help-all") == 0) {
-            usage(TRUE);
+            usage(progname, TRUE);
             exit(0);
         }
         else if (strcmp(arg, "-s") == 0 ||
@@ -162,68 +161,34 @@ get_options(int argc, char *argv[])
                     /* Error printed to stderr when user provides an unknown command-line argument */
                     _("Unknown argument '%s'"), arg);
             fprintf(stderr, "\n");
-            usage(FALSE);
+            usage(progname, FALSE);
             exit(1);
         }
     }
 }
 
 
-static void
-init_state(void)
-{
-    /* Translators: Digits localized for the given language */
-    const char *digit_values = _("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F");
-    const char *default_digits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
-    gchar **digits;
-    gboolean use_default_digits = FALSE;
-    int i;
-
-    digits = g_strsplit(digit_values, ",", -1);
-    for (i = 0; i < 16; i++) {
-        if (use_default_digits || digits[i] == NULL) {
-            use_default_digits = TRUE;
-            v->digits[i] = strdup(default_digits[i]);
-        }
-        else
-            v->digits[i] = strdup(digits[i]);
-    }
-    g_strfreev(digits);
-
-    v->radix         = get_radix();    /* Locale specific radix string. */
-    v->tsep          = get_tsep();     /* Locale specific thousands separator. */
-    v->tsep_count    = get_tsep_count();
-}
-
-
 int
 main(int argc, char **argv)
 {
-    memset(&calc_state, 0, sizeof(calc_state));
-    v = &calc_state;
-
+    GCalctoolUI *ui;
+  
     g_type_init();
 
     bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
     bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
     textdomain(GETTEXT_PACKAGE);
 
-    v->progname = g_path_get_basename(argv[0]);
-
     /* Seed random number generator. */
     srand48((long) time((time_t *) 0));
 
     resources_init();
-
-    init_state();
     register_init();
     ui_gtk_init(&argc, &argv);
-
     get_options(argc, argv);
-
-    X = ui_new();
-    ui_start(X);
-
+    ui = ui_new();
+    ui_start(ui);
+  
     currency_free_resources();
 
     return(0);
diff --git a/src/financial.c b/src/financial.c
index faf0105..43f0878 100644
--- a/src/financial.c
+++ b/src/financial.c
@@ -17,12 +17,11 @@
  *  02111-1307, USA.
  */
 
+#include <glib/gi18n.h>
+
 #include "financial.h"
-#include "calctool.h"
 #include "mp.h"
 
-#include <libintl.h>
-
 static void
 calc_ctrm(MathEquation *equation, MPNumber *t, MPNumber *pint, MPNumber *fv, MPNumber *pv)
 {
diff --git a/src/get.c b/src/get.c
index 25b70bb..cb575a6 100644
--- a/src/get.c
+++ b/src/get.c
@@ -34,7 +34,7 @@
 #include "register.h"
 #include "mp.h"
 
-#define EQUAL(a, b)    (strlen(a)==strlen(b)) & !strcmp(a, b)
+#define MAXLINE 1024
 
 /* Various string values read/written as X resources. */
 
@@ -45,7 +45,7 @@ char *
 get_resource(const char *key)
 {
     char key_name[MAXLINE];
-    SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
+    snprintf(key_name, MAXLINE, "/apps/gcalctool/%s", key);
     return gconf_client_get_string(client, key_name, NULL);
 }
 
@@ -54,7 +54,7 @@ void
 set_resource(const char *key, const char *value)
 {
     char key_name[MAXLINE];
-    SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
+    snprintf(key_name, MAXLINE, "/apps/gcalctool/%s", key);
     gconf_client_set_string(client, key_name, value, NULL);
 }
 
@@ -63,7 +63,7 @@ void
 set_int_resource(const char *key, int value)
 {
     char key_name[MAXLINE];
-    SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
+    snprintf(key_name, MAXLINE, "/apps/gcalctool/%s", key);
     gconf_client_set_int(client, key_name, value, NULL);
 }
 
@@ -72,7 +72,7 @@ void
 set_boolean_resource(const char *key, int value)
 {
     char key_name[MAXLINE];
-    SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
+    snprintf(key_name, MAXLINE, "/apps/gcalctool/%s", key);
     gconf_client_set_bool(client, key_name, value, NULL);
 }
 
@@ -90,7 +90,7 @@ get_int_resource(const char *key, int *intval)
     GError *error = NULL;
     gint v;
 
-    SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
+    snprintf(key_name, MAXLINE, "/apps/gcalctool/%s", key);
     v = gconf_client_get_int(client, key_name, &error);
     if (error)
         return FALSE;
@@ -107,7 +107,7 @@ get_boolean_resource(const char *key, int *boolval)
     GError *error = NULL;
     gboolean v;
 
-    SNPRINTF(key_name, MAXLINE, "/apps/gcalctool/%s", key);
+    snprintf(key_name, MAXLINE, "/apps/gcalctool/%s", key);
     v = gconf_client_get_bool(client, key_name, &error);
     if (error)
         return FALSE;
diff --git a/src/get.h b/src/get.h
index 96f2fd1..5d5044f 100644
--- a/src/get.h
+++ b/src/get.h
@@ -20,8 +20,6 @@
 #ifndef GET_H
 #define GET_H
 
-#include "calctool.h"
-
 #define R_ACCURACY "accuracy"
 #define R_DISPLAY  "result_format"
 #define R_MODE     "button_layout"
diff --git a/src/math-buttons.c b/src/math-buttons.c
index 213a936..9b42017 100644
--- a/src/math-buttons.c
+++ b/src/math-buttons.c
@@ -342,12 +342,12 @@ load_mode(MathButtons *buttons, ButtonMode mode)
         name = g_strdup_printf("calc_%d_button", i);
         button = GET_WIDGET(builder, name);     
         if (button)
-            gtk_button_set_label(GTK_BUTTON(button), math_display_get_digit_text(buttons->priv->display, i));
+            gtk_button_set_label(GTK_BUTTON(button), math_equation_get_digit_text(math_display_get_equation(buttons->priv->display), i));
         g_free(name);
     }
     widget = GET_WIDGET(builder, "calc_numeric_point_button");
     if (widget)
-        gtk_button_set_label(GTK_BUTTON(widget), math_display_get_numeric_point_text(buttons->priv->display));
+        gtk_button_set_label(GTK_BUTTON(widget), math_equation_get_numeric_point_text(math_display_get_equation(buttons->priv->display)));
 
     /* Connect super and subscript */
     for (i = 0; i < 10; i++) {
@@ -1085,18 +1085,19 @@ set_subscript_cb(GtkWidget *widget, MathButtons *buttons)
 }
 
 
-// FIXME: Watch for display changes from MathEngine
-void
-math_buttons_set_bitfield(MathButtons *buttons, int enabled, guint64 bits)
+static void
+bitfield_changed_cb(MathEquation *equation, MathButtons *buttons)
 {
     int i;
-    const gchar *label;  
+    const gchar *label;
+    guint64 bits;
 
     if (!buttons->priv->bit_panel)
        return;
 
-    gtk_widget_set_sensitive(buttons->priv->bit_panel, enabled);
+    gtk_widget_set_sensitive(buttons->priv->bit_panel, math_equation_get_bitfield_enabled(equation));
 
+    bits = math_equation_get_bitfield(equation);
     for (i = 0; i < MAXBITS; i++) {
         if (bits & (1LL << (MAXBITS-i-1)))
             label = " 1";
@@ -1140,6 +1141,7 @@ math_buttons_set_property (GObject      *object,
     case PROP_DISPLAY:
         self->priv->display = g_value_get_object (value);
         g_signal_connect(self->priv->display, "number-mode-changed", G_CALLBACK(number_mode_changed_cb), self);
+        g_signal_connect(math_display_get_equation(self->priv->display), "bitfield-changed", G_CALLBACK(bitfield_changed_cb), self);
         break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
diff --git a/src/math-buttons.h b/src/math-buttons.h
index 91927d3..be05e2b 100644
--- a/src/math-buttons.h
+++ b/src/math-buttons.h
@@ -51,8 +51,6 @@ GType math_buttons_get_type();
 
 MathButtons *math_buttons_new(MathDisplay *display);
 
-void math_buttons_set_bitfield(MathButtons *buttons, int enabled, guint64 bits);
-
 void math_buttons_set_mode(MathButtons *buttons, ButtonMode mode);
 
 ButtonMode math_buttons_get_mode(MathButtons *buttons);
diff --git a/src/math-display.c b/src/math-display.c
index bcfa0cf..13984dd 100644
--- a/src/math-display.c
+++ b/src/math-display.c
@@ -17,10 +17,11 @@
  */
 
 #include <string.h>
+#include <glib/gi18n.h>
 #include <gdk/gdkkeysyms.h>
 
 #include "math-display.h"
-#include "calctool.h" // FIXME: TEMP
+#include "ui.h" // FIXME: TEMP
 
 enum {
     PROP_0,
@@ -79,18 +80,6 @@ math_display_get_text(MathDisplay *display)
 }
 
 
-const gchar *math_display_get_digit_text(MathDisplay *display, guint digit)
-{
-    return v->digits[digit];
-}
-
-
-const gchar *math_display_get_numeric_point_text(MathDisplay *display)
-{
-    return v->radix;
-}
-
-
 void
 math_display_set_number_mode(MathDisplay *display, NumberMode mode)
 {
@@ -184,7 +173,7 @@ math_display_insert_digit(MathDisplay *display, unsigned int digit)
     static const char *superscript_digits[] = {"�", "¹", "²", "³", "�", "�", "�", "�", "�", "�", NULL};
 
     if (display->priv->number_mode == NORMAL || digit >= 10)
-        math_display_insert(display, v->digits[digit]);
+        math_display_insert(display, math_equation_get_digit_text(display->priv->equation, digit));
     else if (display->priv->number_mode == SUPERSCRIPT)
         math_display_insert(display, superscript_digits[digit]);
     else if (display->priv->number_mode == SUBSCRIPT)
@@ -195,7 +184,7 @@ math_display_insert_digit(MathDisplay *display, unsigned int digit)
 void
 math_display_insert_numeric_point(MathDisplay *display)
 {
-    math_display_insert(display, v->radix);
+    math_display_insert(display, math_equation_get_numeric_point_text(display->priv->equation));
 }
 
 
@@ -264,30 +253,6 @@ math_display_toggle_bit(MathDisplay *display, guint bit)
 
 
 void
-math_display_set(MathDisplay *display, char *str, int cursor)
-{
-    GtkTextIter iter;
-    GtkAdjustment *adj;
-
-    gtk_text_buffer_set_text(display->priv->display_buffer, str, -1);
-
-    if (cursor < 0)
-        gtk_text_buffer_get_end_iter(display->priv->display_buffer, &iter);
-    else
-        gtk_text_buffer_get_iter_at_offset(display->priv->display_buffer, &iter, cursor);
-    gtk_text_buffer_place_cursor(display->priv->display_buffer, &iter);
-    gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(display->priv->display_item), &iter, 0.0, TRUE, 1.0, 0.0);
-}
-
-
-void
-math_display_set_status(MathDisplay *display, const gchar *message)
-{
-    gtk_text_buffer_set_text(display->priv->info_buffer, message, -1);
-}
-
-
-void
 math_display_copy(MathDisplay *display)
 {
     gchar *string = NULL;
@@ -308,7 +273,7 @@ math_display_copy(MathDisplay *display)
 
 
 static gboolean
-check_for_localized_numeric_point(int keyval)
+check_for_localized_numeric_point(MathDisplay *display, int keyval)
 {
     gchar outbuf[10]; /* Minumum size 6. */
     gunichar ch;
@@ -318,7 +283,7 @@ check_for_localized_numeric_point(int keyval)
 
     outbuf[g_unichar_to_utf8(ch, outbuf)] = '\0';
 
-    return (strcmp(outbuf, v->radix) == 0);
+    return (strcmp(outbuf, math_equation_get_numeric_point_text(display->priv->equation)) == 0);
 }
 
 
@@ -340,7 +305,7 @@ main_window_key_press_cb(GtkWidget *widget, GdkEventKey *event, GCalctoolUI *ui)
     // FIXME: Convert event to character
     // FIXME: Or safer to intercept characters as they enter the text input (handles input methods)
 
-    if (check_for_localized_numeric_point(event->keyval) == TRUE) {
+    if (check_for_localized_numeric_point(display, event->keyval) == TRUE) {
         event->state = 0;
         event->keyval = GDK_KP_Decimal;
     }
@@ -505,7 +470,7 @@ main_window_key_press_cb(GtkWidget *widget, GdkEventKey *event, GCalctoolUI *ui)
         }
     }
     if (strcmp(event->string, ".") == 0) {
-        math_display_insert(display, v->radix);
+        math_display_insert_numeric_point(display);
         return TRUE;
     }
 
@@ -640,10 +605,39 @@ math_display_set_base(MathDisplay *display, gint base)
 
 
 static void
-math_display_set_property (GObject      *object,
-                           guint         prop_id,
-                           const GValue *value,
-                           GParamSpec   *pspec)
+display_changed_cb(MathEquation *equation, MathDisplay *display)
+{
+    GtkTextIter iter;
+    GtkAdjustment *adj;
+    const gchar *str;
+    gint cursor;
+  
+    str = math_equation_get_text(equation);
+    cursor = math_equation_get_cursor(equation);
+
+    gtk_text_buffer_set_text(display->priv->display_buffer, str, -1);
+
+    if (cursor < 0)
+        gtk_text_buffer_get_end_iter(display->priv->display_buffer, &iter);
+    else
+        gtk_text_buffer_get_iter_at_offset(display->priv->display_buffer, &iter, cursor);
+    gtk_text_buffer_place_cursor(display->priv->display_buffer, &iter);
+    gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(display->priv->display_item), &iter, 0.0, TRUE, 1.0, 0.0);
+}
+
+
+static void
+status_changed_cb(MathEquation *equation, MathDisplay *display)
+{
+    gtk_text_buffer_set_text(display->priv->info_buffer, math_equation_get_status(equation), -1);
+}
+
+
+static void
+math_display_set_property(GObject      *object,
+                          guint         prop_id,
+                          const GValue *value,
+                          GParamSpec   *pspec)
 {
     MathDisplay *self;
 
@@ -652,6 +646,9 @@ math_display_set_property (GObject      *object,
     switch (prop_id) {
     case PROP_EQUATION:
         self->priv->equation = g_value_get_object (value);
+        g_signal_connect(self->priv->equation, "display-changed", G_CALLBACK(display_changed_cb), self);      
+        g_signal_connect(self->priv->equation, "status-changed", G_CALLBACK(status_changed_cb), self);
+        status_changed_cb(self->priv->equation, self);
         break;
     default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -661,10 +658,10 @@ math_display_set_property (GObject      *object,
 
 
 static void
-math_display_get_property (GObject    *object,
-                           guint       prop_id,
-                           GValue     *value,
-                           GParamSpec *pspec)
+math_display_get_property(GObject    *object,
+                          guint       prop_id,
+                          GValue     *value,
+                          GParamSpec *pspec)
 {
     MathDisplay *self;
 
@@ -729,6 +726,7 @@ math_display_init(MathDisplay *display)
     PangoFontDescription *font_desc;
 
     display->priv = G_TYPE_INSTANCE_GET_PRIVATE (display, math_display_get_type(), MathDisplayPrivate);
+
     display->priv->primary_atom = gdk_atom_intern("PRIMARY", FALSE);
     display->priv->clipboard_atom = gdk_atom_intern("CLIPBOARD", FALSE);
 
diff --git a/src/math-display.h b/src/math-display.h
index d1872ee..22bba61 100644
--- a/src/math-display.h
+++ b/src/math-display.h
@@ -56,13 +56,8 @@ MathEquation *math_display_get_equation(MathDisplay *display);
 
 void math_display_set_base(MathDisplay *display, gint base);
 void math_display_set_number_mode(MathDisplay *display, NumberMode mode);
-void math_display_set(MathDisplay *display, gchar *, gint); // FIXME: Make obsolete by Math model
-void math_display_set_status(MathDisplay *display, const gchar *message);
 gchar *math_display_get_text(MathDisplay *display);
 
-const gchar *math_display_get_digit_text(MathDisplay *display, guint digit);
-const gchar *math_display_get_numeric_point_text(MathDisplay *display);
-
 void math_display_copy(MathDisplay *display);
 void math_display_paste(MathDisplay *display);
 void math_display_store(MathDisplay *display, const gchar *name);
diff --git a/src/math-equation.c b/src/math-equation.c
index 612ed72..17d33d9 100644
--- a/src/math-equation.c
+++ b/src/math-equation.c
@@ -32,12 +32,31 @@
 #include "mp-equation.h"
 #include "register.h"
 #include "currency.h"
-#include "calctool.h"
+#include "get.h"
 
 
+enum {
+    STATUS_CHANGED,
+    BITFIELD_CHANGED,
+    DISPLAY_CHANGED,
+    LAST_SIGNAL
+};
+static guint signals[LAST_SIGNAL] = { 0, };
+
+#define MAX_DIGITS 200
+
 struct MathEquationPrivate
 {
-    gint fixme;
+    gchar *status;
+    guint64 bitfield;
+    gboolean bitfield_enabled;
+    char localized[MAX_DIGITS];
+    gint cursor;
+
+    const char *digits[16];   /* Localized digit values */
+    const char *radix;        /* Locale specific radix string. */
+    const char *tsep;         /* Locale specific thousands separator. */
+    int tsep_count;           /* Number of digits between separator. */
 };
 
 G_DEFINE_TYPE (MathEquation, math_equation, G_TYPE_OBJECT);
@@ -49,6 +68,73 @@ math_equation_new()
 }
 
 
+const gchar *
+math_equation_get_digit_text(MathEquation *equation, guint digit)
+{
+    return equation->priv->digits[digit];
+}
+
+
+const gchar *
+math_equation_get_numeric_point_text(MathEquation *equation)
+{
+    return equation->priv->radix;
+}
+
+
+void
+math_equation_set_status(MathEquation *equation, const gchar *status)
+{
+    g_free(equation->priv->status);
+    equation->priv->status = g_strdup(status);
+    g_signal_emit(equation, signals[STATUS_CHANGED], 0);
+}
+
+
+const gchar *
+math_equation_get_status(MathEquation *equation)
+{
+    return equation->priv->status;
+}
+
+
+static void
+math_equation_set_bitfield(MathEquation *equation, gboolean enabled, guint64 bitfield)
+{
+    equation->priv->bitfield_enabled = enabled;
+    equation->priv->bitfield = bitfield;
+    g_signal_emit(equation, signals[BITFIELD_CHANGED], 0);
+}
+
+
+gboolean
+math_equation_get_bitfield_enabled(MathEquation *equation)
+{
+    return equation->priv->bitfield_enabled;
+}
+
+
+guint64
+math_equation_get_bitfield(MathEquation *equation)
+{
+    return equation->priv->bitfield;
+}
+
+
+const gchar *
+math_equation_get_text(MathEquation *equation)
+{
+    return equation->priv->localized;
+}
+
+
+gint
+math_equation_get_cursor(MathEquation *equation)
+{
+    return equation->priv->cursor;
+}
+
+
 static MathEquationState *
 get_state(MathEquation *display)
 {
@@ -92,8 +178,8 @@ localize_expression(MathEquation *display, char *dest, const char *src, int dest
 
             /* Insert separator after nth digit */
             if (display->show_tsep && display->format == DEC &&
-                !after_radix && digit_count > 1 && digit_count % v->tsep_count == 1) {
-                g_string_append(output, v->tsep);
+                !after_radix && digit_count > 1 && digit_count % display->priv->tsep_count == 1) {
+                g_string_append(output, display->priv->tsep);
                 if (new_cursor > read_cursor) {
                     new_cursor++;
                 }
@@ -105,7 +191,7 @@ localize_expression(MathEquation *display, char *dest, const char *src, int dest
         else if (*c == '.') {
             digit_count = -1;
             after_radix = TRUE;
-            g_string_append(output, v->radix);
+            g_string_append(output, display->priv->radix);
             // FIXME: Handle cursor if radix is more than one character?
         }
         /* Reset when encountering other characters (e.g. '+') */
@@ -116,7 +202,7 @@ localize_expression(MathEquation *display, char *dest, const char *src, int dest
         }
     }
 
-    STRNCPY(dest, output->str, dest_length - 1);
+    strncpy(dest, output->str, dest_length - 1);
     g_string_free(output, TRUE);
 
     if (cursor != NULL && *cursor != -1) {
@@ -219,7 +305,7 @@ display_set_number(MathEquation *display, const MPNumber *x)
     display_set_string(display, text, -1);
 
     enabled = display_get_unsigned_integer(display, &bit_value);
-    math_buttons_set_bitfield(ui_get_buttons(X), enabled, bit_value);
+    math_equation_set_bitfield(display, enabled, bit_value);
 }
 
 
@@ -247,8 +333,8 @@ display_make_text(MathEquation *display, char *localized, int length, int *curso
 
     /* Substitute answer register */
     if (display_is_result(display)) {
-        char temp[MAX_LOCALIZED];
-        display_make_number(display, temp, MAX_LOCALIZED, &e->ans);
+        char temp[MAX_DIGITS];
+        display_make_number(display, temp, MAX_DIGITS, &e->ans);
         str = strdup(temp);
     }
     else
@@ -260,14 +346,11 @@ display_make_text(MathEquation *display, char *localized, int length, int *curso
 
 
 static void
-display_refresh(MathEquation *display)
+display_refresh(MathEquation *equation)
 {
-    char localized[MAX_LOCALIZED];
-    int cursor;
-
-    cursor = display_get_cursor(display);
-    display_make_text(display, localized, MAX_LOCALIZED, &cursor);
-    math_display_set(ui_get_display(X), localized, cursor);
+    equation->priv->cursor = display_get_cursor(equation);
+    display_make_text(equation, equation->priv->localized, MAX_DIGITS, &equation->priv->cursor);
+    g_signal_emit(equation, signals[DISPLAY_CHANGED], 0);
 }
 
 
@@ -302,7 +385,7 @@ display_set_cursor(MathEquation *display, int cursor)
 void
 display_set_error(MathEquation *display, const char *message)
 {
-    math_display_set_status(ui_get_display(X), message);
+    math_equation_set_status(display, message);
 }
 
 
@@ -381,9 +464,9 @@ display_pop(MathEquation *display)
 {
     if (display->h.current != display->h.begin) {
         display->h.current = ((display->h.current - 1) % UNDO_HISTORY_LENGTH);
-        math_display_set_status(ui_get_display(X), "");
+        math_equation_set_status(display, "");
     } else {
-        math_display_set_status(ui_get_display(X), _("No undo history"));
+        math_equation_set_status(display, _("No undo history"));
     }
 
     display_refresh(display);
@@ -395,9 +478,9 @@ display_unpop(MathEquation *display)
 {
     if (display->h.current != display->h.end) {
         display->h.current = ((display->h.current + 1) % UNDO_HISTORY_LENGTH);
-        math_display_set_status(ui_get_display(X), "");
+        math_equation_set_status(display, "");
     } else {
-        math_display_set_status(ui_get_display(X), _("No redo steps"));
+        math_equation_set_status(display, _("No redo steps"));
     }
     get_state(display)->cursor = -1;
     display_refresh(display);
@@ -426,7 +509,7 @@ display_insert(MathEquation *display, int cursor_start, int cursor_end, const ch
     }
 
     if (cursor_start < 0) {
-        SNPRINTF(buf, MAX_DISPLAY, "%s%s", get_text(display), text);
+        snprintf(buf, MAX_DISPLAY, "%s%s", get_text(display), text);
         display_set_string(display, buf, -1);
     } else {
         GString *new_text;
@@ -442,7 +525,7 @@ display_insert(MathEquation *display, int cursor_start, int cursor_end, const ch
         }
 
         cursor = 0;
-        for (c = math_display_get_text(ui_get_display(X)); *c; c = g_utf8_next_char(c), cursor++) {
+        for (c = display->priv->localized; *c; c = g_utf8_next_char(c), cursor++) {
             gboolean use = TRUE;
 
             /* Ignore selected part */
@@ -450,7 +533,7 @@ display_insert(MathEquation *display, int cursor_start, int cursor_end, const ch
                 use = FALSE;
 
             /* Ignore thousands separators (if one exists) */
-            if (v->tsep[0] != '\0' && strncmp(c, v->tsep, strlen(v->tsep)) == 0)
+            if (display->priv->tsep[0] != '\0' && strncmp(c, display->priv->tsep, strlen(display->priv->tsep)) == 0)
                 use = FALSE;
 
             /* Copy existing text */
@@ -496,7 +579,7 @@ display_backspace(MathEquation *display, int cursor_start, int cursor_end)
     /* If cursor is at end of the line then delete the last character preserving accuracy */
     if (cursor_start < 0) {
         int len;
-        len = g_utf8_strlen(math_display_get_text(ui_get_display(X)), -1);
+        len = g_utf8_strlen(display->priv->localized, -1);
         display_insert(display, len - 1, len, "");
     } else if (cursor_start != cursor_end) {
         display_insert(display, cursor_start, cursor_end, "");
@@ -828,14 +911,14 @@ do_paste(MathEquation *display, int cursor_start, int cursor_end, const char *te
         /* If the clipboard buffer contains any occurances of the "thousands
          * separator", remove them.
          */
-        if (v->tsep[0] != '\0' && strncmp(input, v->tsep, strlen(v->tsep)) == 0) {
-            input += strlen(v->tsep) - 1;
+        if (display->priv->tsep[0] != '\0' && strncmp(input, display->priv->tsep, strlen(display->priv->tsep)) == 0) {
+            input += strlen(display->priv->tsep) - 1;
             continue;
         }
 
         /* Replace radix with "." */
-        else if (strncmp(input, v->radix, strlen(v->radix)) == 0) {
-            input += strlen(v->radix) - 1;
+        else if (strncmp(input, display->priv->radix, strlen(display->priv->radix)) == 0) {
+            input += strlen(display->priv->radix) - 1;
             c = '.';
         }
 
@@ -905,7 +988,7 @@ do_shift(MathEquation *display, int count)
     if (!display_is_usable_number(display, &z)) {
         /* Translators: This message is displayed in the status bar when a bit
            shift operation is performed and the display does not contain a number */
-        math_display_set_status(ui_get_display(X), _("No sane value to bitwise shift"));
+        math_equation_set_status(display, _("No sane value to bitwise shift"));
     }
     else {
         mp_shift(&z, count, display_get_answer(display));
@@ -921,7 +1004,7 @@ do_factorize(MathEquation *equation)
 
     if (!display_is_usable_number(equation, &value)) {
         /* Translators: Error displayed when trying to factorize a non-integer value */
-        math_display_set_status(ui_get_display(X), _("Need an integer to factorize"));
+        math_equation_set_status(equation, _("Need an integer to factorize"));
         return;
     }
     display_clear(equation);
@@ -947,7 +1030,7 @@ do_sto(MathEquation *display, const char *name)
     MPNumber t;
 
     if (!display_is_usable_number(display, &t))
-        math_display_set_status(ui_get_display(X), _("No sane value to store"));
+        math_equation_set_status(display, _("No sane value to store"));
     else
         register_set_value(name, &t);
 }
@@ -978,7 +1061,7 @@ display_do_function(MathEquation *display, int function, gpointer arg, int curso
     display_set_cursor(display, cursor_start);
     ans = display_get_answer(display);
 
-    math_display_set_status(ui_get_display(X), "");
+    math_equation_set_status(display, "");
 
     switch (function) {
         case FN_CLEAR:
@@ -1025,7 +1108,7 @@ display_do_function(MathEquation *display, int function, gpointer arg, int curso
                 bit_value ^= (1LL << (63 - GPOINTER_TO_INT (arg)));
 
                 /* FIXME: Convert to string since we don't support setting MP numbers from 64 bit integers */
-                SNPRINTF(buf, MAX_DISPLAY, "%" G_GUINT64_FORMAT, bit_value);
+                snprintf(buf, MAX_DISPLAY, "%" G_GUINT64_FORMAT, bit_value);
                 mp_set_from_string(buf, &MP);
                 display_set_number(display, &MP);
             }
@@ -1098,7 +1181,7 @@ display_do_function(MathEquation *display, int function, gpointer arg, int curso
                         break;
                 }
                 if (message)
-                    math_display_set_status(ui_get_display(X), message);
+                    math_equation_set_status(display, message);
             }
             break;
 
@@ -1112,7 +1195,7 @@ display_do_function(MathEquation *display, int function, gpointer arg, int curso
     }
 
     enabled = display_get_unsigned_integer(display, &bit_value);
-    math_buttons_set_bitfield(ui_get_buttons(X), enabled, bit_value);
+    math_equation_set_bitfield(display, enabled, bit_value);
 }
 
 
@@ -1122,16 +1205,62 @@ math_equation_class_init (MathEquationClass *klass)
     GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
     g_type_class_add_private (klass, sizeof (MathEquationPrivate));
+
+    signals[STATUS_CHANGED] =
+        g_signal_new ("status-changed",
+                      G_TYPE_FROM_CLASS (klass),
+                      G_SIGNAL_RUN_LAST,
+                      G_STRUCT_OFFSET (MathEquationClass, status_changed),
+                      NULL, NULL,
+                      g_cclosure_marshal_VOID__VOID,
+                      G_TYPE_NONE, 0);
+    signals[BITFIELD_CHANGED] =
+        g_signal_new ("bitfield-changed",
+                      G_TYPE_FROM_CLASS (klass),
+                      G_SIGNAL_RUN_LAST,
+                      G_STRUCT_OFFSET (MathEquationClass, bitfield_changed),
+                      NULL, NULL,
+                      g_cclosure_marshal_VOID__VOID,
+                      G_TYPE_NONE, 0);
+    signals[DISPLAY_CHANGED] =
+        g_signal_new ("display-changed",
+                      G_TYPE_FROM_CLASS (klass),
+                      G_SIGNAL_RUN_LAST,
+                      G_STRUCT_OFFSET (MathEquationClass, display_changed),
+                      NULL, NULL,
+                      g_cclosure_marshal_VOID__VOID,
+                      G_TYPE_NONE, 0);
 }
 
 
 static void
 math_equation_init(MathEquation *equation)
 {
+    /* Translators: Digits localized for the given language */
+    const char *digit_values = _("0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F");
+    const char *default_digits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
+    gchar **digits;
+    gboolean use_default_digits = FALSE;
     int i;
 
     equation->priv = G_TYPE_INSTANCE_GET_PRIVATE (equation, math_equation_get_type(), MathEquationPrivate);
 
+    digits = g_strsplit(digit_values, ",", -1);
+    for (i = 0; i < 16; i++) {
+        if (use_default_digits || digits[i] == NULL) {
+            use_default_digits = TRUE;
+            equation->priv->digits[i] = strdup(default_digits[i]);
+        }
+        else
+            equation->priv->digits[i] = strdup(digits[i]);
+    }
+    g_strfreev(digits);
+
+    equation->priv->radix = get_radix();   /* Locale specific radix string. */
+    equation->priv->tsep = get_tsep();     /* Locale specific thousands separator. */
+    equation->priv->tsep_count = get_tsep_count();
+
+    equation->priv->status = g_strdup("");
     equation->show_zeroes = FALSE;
     equation->show_tsep = FALSE;
     equation->format = DEC;
diff --git a/src/math-equation.h b/src/math-equation.h
index 9eb9a44..d14b644 100644
--- a/src/math-equation.h
+++ b/src/math-equation.h
@@ -21,6 +21,7 @@
 #define DISPLAY_H
 
 #include <glib-object.h>
+#include "mp.h"
 
 G_BEGIN_DECLS
 
@@ -28,8 +29,6 @@ G_BEGIN_DECLS
 
 typedef struct MathEquationPrivate MathEquationPrivate;
 
-#include "mp.h"
-
 #define UNDO_HISTORY_LENGTH 16  /* Arithmetic mode undo history length */
 #define MAX_DISPLAY 512
 
@@ -68,6 +67,10 @@ typedef struct
 typedef struct
 {
     GObjectClass parent_class;
+
+    void (*status_changed)(MathEquation *display);  
+    void (*bitfield_changed)(MathEquation *display);
+    void (*display_changed)(MathEquation *display);
 } MathEquationClass;
 
 /* Available functions */
@@ -92,6 +95,16 @@ enum
 GType math_equation_get_type();
 MathEquation *math_equation_new();
 
+const gchar *math_equation_get_digit_text(MathEquation *equation, guint digit);
+const gchar *math_equation_get_numeric_point_text(MathEquation *equation);
+
+void math_equation_set_status(MathEquation *equation, const gchar *status);
+const gchar *math_equation_get_status(MathEquation *equation);
+gboolean math_equation_get_bitfield_enabled(MathEquation *equation);
+guint64 math_equation_get_bitfield(MathEquation *equation);
+const gchar *math_equation_get_text(MathEquation *equation);
+gint math_equation_get_cursor(MathEquation *equation);
+
 void display_set_accuracy(MathEquation *display, int accuracy);
 void display_set_show_thousands_separator(MathEquation *display, gboolean visible);
 void display_set_show_trailing_zeroes(MathEquation *display, gboolean visible);
diff --git a/src/ui-preferences.c b/src/ui-preferences.c
index f732c1f..dfbb81a 100644
--- a/src/ui-preferences.c
+++ b/src/ui-preferences.c
@@ -16,6 +16,7 @@
  *  02111-1307, USA.
  */
 
+#include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
 #include "ui-preferences.h"
diff --git a/src/ui.c b/src/ui.c
index c8cb834..2ede3f5 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -17,6 +17,7 @@
  *  02111-1307, USA.
  */
 
+#include <glib/gi18n.h>
 #include <gtk/gtk.h>
 
 #include "ui.h"



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