gcalctool r2156 - trunk/gcalctool



Author: rancell
Date: Sat Aug  9 05:59:10 2008
New Revision: 2156
URL: http://svn.gnome.org/viewvc/gcalctool?rev=2156&view=rev

Log:
Streamline display code

Modified:
   trunk/gcalctool/calctool.c
   trunk/gcalctool/calctool.h
   trunk/gcalctool/display.c
   trunk/gcalctool/display.h
   trunk/gcalctool/functions.c
   trunk/gcalctool/gtk.c

Modified: trunk/gcalctool/calctool.c
==============================================================================
--- trunk/gcalctool/calctool.c	(original)
+++ trunk/gcalctool/calctool.c	Sat Aug  9 05:59:10 2008
@@ -602,7 +602,6 @@
     mpset(&acc, &size, &size);
 
     v->error       = 0;            /* No calculator error initially. */    
-    v->current    = KEY_CALCULATE;
 
     init_constant(0, "0.621");                 /* kms/hr <=> miles/hr. */
     init_constant(1, "1.4142135623");          /* square root of 2 */

Modified: trunk/gcalctool/calctool.h
==============================================================================
--- trunk/gcalctool/calctool.h	(original)
+++ trunk/gcalctool/calctool.h	Sat Aug  9 05:59:10 2008
@@ -171,8 +171,6 @@
 };
 
 struct calcVars {                      /* Calctool variables and options. */
-    int current;                       /* Current button/character pressed. */
-  
     char *appname;                     /* Application name for resources. */
     char *home;                        /* Pathname for users home directory. */
     char *progname;                    /* Name of this program. */

Modified: trunk/gcalctool/display.c
==============================================================================
--- trunk/gcalctool/display.c	(original)
+++ trunk/gcalctool/display.c	Sat Aug  9 05:59:10 2008
@@ -179,24 +179,24 @@
 
 
 void
-display_clear(GCDisplay *display, int initialise)
+display_clear(GCDisplay *display)
 {
-    display_set_string(display, "");
+    display_set_string(display, "", -1);
 }
 
 
 void
 display_reset(GCDisplay *display)
 {
-    v->error             = 0;         /* Currently no display error. */
+    v->error = 0;         /* Currently no display error. */
     mp_set_from_integer(0, v->MPresult);   /* No previous result yet. */
     mp_set_from_integer(0, v->MPdisp_val);         
     mp_set_from_integer(0, v->MPlast_input);
   
-    display_clear(display, TRUE);
+    display_clear(display);
 }
 
-const char *
+static const char *
 display_get_text(GCDisplay *display)
 {
     return get_state(display)->expression;
@@ -223,13 +223,14 @@
 }
 
 void
-display_set_string(GCDisplay *display, const char *value)
+display_set_string(GCDisplay *display, const char *value, int cursor)
 {
     struct exprm_state *e;
     
     e = get_state(display);
     free(e->expression);
     e->expression = strdup(value);
+    e->cursor = cursor;
 }
 
 void
@@ -323,7 +324,7 @@
     }
     update_undo_redo_button_sensitivity(display);
     
-    display_refresh(display, display_get_cursor(display));
+    display_refresh(display);
 }
 
 void
@@ -344,10 +345,11 @@
     return(display->h.current != display->h.begin);
 }
 
-int
-display_insert(GCDisplay *display, const char *text, int cursor)
+void
+display_insert(GCDisplay *display, const char *text)
 {
     char buf[MAX_DISPLAY], *currentText;
+    int cursor = display_get_cursor(display);
     
     if (cursor < 0) {
         SNPRINTF(buf, MAX_DISPLAY, "%s%s", display_get_text(display), text);
@@ -356,17 +358,17 @@
         SNPRINTF(buf, MAX_DISPLAY, "%.*s%s%s", cursor, currentText, text, currentText + cursor);
         cursor += strlen(text);
     }
-    display_set_string(display, buf);
-    
-    return cursor;
+    display_set_string(display, buf, cursor);
 }
 
-int
-display_backspace(GCDisplay *display, int cursor)
+void
+display_backspace(GCDisplay *display)
 {
     char buf[MAX_DISPLAY] = "", buf2[MAX_DISPLAY], *t;
     struct exprm_state *e = get_state(display);
-    int i, MP_reg[MP_SIZE];
+    int i, MP_reg[MP_SIZE], cursor;
+    
+    cursor = display_get_cursor(display);
 
     /* If cursor is at end of the line then delete the last character preserving accuracy */
     if (cursor < 0) {
@@ -383,8 +385,8 @@
                     make_number(buf2, MAX_DISPLAY, MP_reg, v->base, FALSE);
                     /* Remove "Rx" postfix and replace with backspaced number */
                     SNPRINTF(buf, MAX_DISPLAY, "%.*s%s", strlen(e->expression) - 2, e->expression - 3, buf2);
-                    display_set_string(display, buf);
-                    return cursor - 1;
+                    display_set_string(display, buf, cursor - 1);
+                    return;
                 }
             }
         }
@@ -394,48 +396,45 @@
         t = ui_get_display();
         SNPRINTF(buf, MAX_DISPLAY, "%.*s%s", cursor - 1, t, t + cursor);
     } else {
-        return cursor; /* At the start of the line */
+        return; /* At the start of the line */
     }
 
-    display_set_string(display, buf);
-    return cursor - 1;
+    display_set_string(display, buf, cursor - 1);
 }
 
-int
-display_delete(GCDisplay *display, int cursor)
+void
+display_delete(GCDisplay *display)
 {
     char buf[MAX_DISPLAY] = "", *text;
+    int cursor = display_get_cursor(display);    
     
     if (cursor >= 0) {
         text = ui_get_display();
         SNPRINTF(buf, MAX_DISPLAY, "%.*s%s", cursor, text, text + cursor + 1);
-        display_set_string(display, buf);
+        display_set_string(display, buf, cursor);
     }
-
-    return cursor;
 }
 
-int
-display_surround(GCDisplay *display, const char *prefix, const char *suffix, int cursor)
+void
+display_surround(GCDisplay *display, const char *prefix, const char *suffix)
 {
     char buffer[MAX_DISPLAY];
     
     SNPRINTF(buffer, MAX_DISPLAY, "%s%s%s", prefix, display_get_text(display), suffix);
-    display_set_string(display, buffer);
-    
-    return cursor;
+    display_set_string(display, buffer, -1);
 }
 
 /* In arithmetic precedence mode this routine should be called to redraw 
  * the display.
  */
 void
-display_refresh(GCDisplay *display, int cursor)
+display_refresh(GCDisplay *display)
 {
     int i, MP_reg[MP_SIZE];
     char localized[MAX_LOCALIZED], *str, reg[3], *t;
     struct exprm_state *e;
     char x[MAX_LOCALIZED], xx[MAX_LOCALIZED], ans[MAX_LOCALIZED];
+    int cursor = display_get_cursor(display);
 
     e = get_state(display);
     if (display_is_empty(display)) {

Modified: trunk/gcalctool/display.h
==============================================================================
--- trunk/gcalctool/display.h	(original)
+++ trunk/gcalctool/display.h	Sat Aug  9 05:59:10 2008
@@ -53,15 +53,14 @@
 
 void display_reset(GCDisplay *);
 void localize_expression(char *, const char *, int, int *);
-void display_clear(GCDisplay *, int);
-void display_refresh(GCDisplay *, int);
+void display_clear(GCDisplay *);
+void display_refresh(GCDisplay *);
 
-const char *display_get_text(GCDisplay *);
 int *display_get_answer(GCDisplay *);
 int display_get_cursor(GCDisplay *);
 
 void display_set_number(GCDisplay *, int *);
-void display_set_string(GCDisplay *, const char *);
+void display_set_string(GCDisplay *, const char *, int);
 void display_set_cursor(GCDisplay *, int);
 void display_set_error(GCDisplay *, const char *);
 
@@ -71,10 +70,10 @@
 void display_unpop(GCDisplay *);
 gboolean display_is_undo_step(GCDisplay *display);
 
-int display_insert(GCDisplay *, const char *, int);
-int display_backspace(GCDisplay *, int cursor);
-int display_delete(GCDisplay *, int);
-int display_surround(GCDisplay *, const char *, const char *, int);
+void display_insert(GCDisplay *, const char *);
+void display_backspace(GCDisplay *);
+void display_delete(GCDisplay *);
+void display_surround(GCDisplay *, const char *, const char *);
 
 gboolean display_is_empty(GCDisplay *);
 gboolean display_is_result(GCDisplay *);

Modified: trunk/gcalctool/functions.c
==============================================================================
--- trunk/gcalctool/functions.c	(original)
+++ trunk/gcalctool/functions.c	Sat Aug  9 05:59:10 2008
@@ -104,19 +104,6 @@
 
 
 static void
-exp_negate(void)
-{
-    display_surround(&v->display, "-(", ")", 0); // FIXME: Cursor
-}
-
-
-static void
-exp_inv(void)
-{
-    display_surround(&v->display, "1/(", ")", 0); // FIXME: Cursor
-}
-
-static void
 do_function(int index)      /* Perform a user defined function. */
 {
     char *str;
@@ -147,7 +134,7 @@
     }
     else {
         calc_shift(MPval, display_get_answer(&v->display), count);
-        display_set_string(&v->display, "Ans");
+        display_set_string(&v->display, "Ans", -1);
     }
 
     syntaxdep_show_display();
@@ -158,7 +145,7 @@
 void
 do_clear(void)
 {
-    display_clear(&v->display, TRUE);
+    display_clear(&v->display);
     if (v->error) {
         ui_set_display("", -1);
     }
@@ -179,7 +166,7 @@
                          "gtk-dialog-error");
     } else {
         mp_set_from_mp(MP, display_get_answer(&v->display));
-        display_set_string(&v->display, "Ans");
+        display_set_string(&v->display, "Ans", -1);
     }
     v->base = b;
     set_resource(R_BASE, Rbstr[(int) v->base]);
@@ -187,7 +174,8 @@
     ui_make_registers();
     clear_undo_history();
 
-    display_refresh(&v->display, -1);
+    display_set_cursor(&v->display, -1);
+    display_refresh(&v->display);
 }
 
 
@@ -205,8 +193,8 @@
         mp_set_from_mp(v->MPmvals[index], MPtemp);
         mp_set_from_mp(MPexpr, v->MPmvals[index]);
         mp_set_from_mp(MPtemp, display_get_answer(&v->display));
-        display_set_string(&v->display, "Ans");
-        display_refresh(&v->display, -1);
+        display_set_string(&v->display, "Ans", -1);
+        display_refresh(&v->display);
         ui_make_registers();
     }
 
@@ -228,12 +216,13 @@
                          "gtk-dialog-error");
     } else {
         mp_set_from_mp(MP, display_get_answer(&v->display));
-        display_set_string(&v->display, "Ans");
+        display_set_string(&v->display, "Ans", -1);
         ui_make_registers();
     }
     clear_undo_history();
 
-    display_refresh(&v->display, -1);
+    display_set_cursor(&v->display, -1);
+    display_refresh(&v->display);
 }
 
 
@@ -286,7 +275,8 @@
 void
 syntaxdep_show_display(void)
 {
-    display_refresh(&v->display, -1);
+    display_set_cursor(&v->display, -1);
+    display_refresh(&v->display);
 }
 
 
@@ -306,14 +296,14 @@
     /* Starting a number after a calculation clears the display */
     if (display_is_result(&v->display)) {
         if (buttons[function].flags & NUMBER) {
-            display_set_string(&v->display, "");
+            display_clear(&v->display);
         }
     }
 
     switch (buttons[function].id) {
         case KEY_CLEAR:
         case KEY_CLEAR_ENTRY:
-            display_clear(&v->display, FALSE);
+            display_clear(&v->display);
             ui_set_error_state(FALSE);
             MPstr_to_num("0", DEC, ans);
             break;
@@ -336,35 +326,32 @@
 
         case KEY_EXCHANGE:
             do_exchange(arg);
-            cursor = -1;
             return;
 
         case KEY_RECALL:
             SNPRINTF(buf, MAXLINE, "R%d", arg);
-            cursor = display_insert(&v->display, buf, cursor);
+            display_insert(&v->display, buf);
             break;
 
         case KEY_CONSTANT:
             make_number(buf, MAXLINE, v->MPcon_vals[arg], v->base, FALSE);
-            cursor = display_insert(&v->display, buf, cursor);
+            display_insert(&v->display, buf);
             break;
 
         case KEY_BACKSPACE:
-            cursor = display_backspace(&v->display, cursor);
+            display_backspace(&v->display);
             break;
         
         case KEY_DELETE:
-            cursor = display_delete(&v->display, cursor);
+            display_delete(&v->display);
             break;
 
         case KEY_CHANGE_SIGN:
-            exp_negate();
-            cursor = -1;
+            display_surround(&v->display, "-(", ")");
             break;
 
         case KEY_RECIPROCAL:
-            exp_inv();
-            cursor = -1;
+            display_surround(&v->display, "1/(", ")");
             break;
 
         case KEY_CALCULATE:
@@ -393,8 +380,7 @@
                 switch (result) {
                     case 0:
                         mp_set_from_mp(MPval, ans);
-                        display_set_string(&v->display, "Ans");
-                        cursor = -1;
+                        display_set_string(&v->display, "Ans", -1);
                         break;
 
                     case -PARSER_ERR_INVALID_BASE:
@@ -427,23 +413,23 @@
             break;
 
         case KEY_NUMERIC_POINT:
-            cursor = display_insert(&v->display, v->radix, cursor);
+            display_insert(&v->display, v->radix);
             break;
 
         default:
             /* If display is a number then perform functions on that number */
             if (buttons[function].flags & (PREFIXOP | FUNC) && display_is_result(&v->display)) {
                 SNPRINTF(buf, MAXLINE, "%s(", buttons[function].symname);
-                display_surround(&v->display, buf, ")", 0); // FIXME: Cursor
+                display_surround(&v->display, buf, ")");
             } else {
                 if (buttons[function].flags & FUNC) {
                     SNPRINTF(buf, MAXLINE, "%s(", buttons[function].symname);
-                    cursor = display_insert(&v->display, buf, cursor);
+                    display_insert(&v->display, buf);
                 } else {
-                    cursor = display_insert(&v->display, buttons[function].symname, cursor);
+                    display_insert(&v->display, buttons[function].symname);
                 }
             }
             break;
     }
-    display_refresh(&v->display, cursor);
+    display_refresh(&v->display);
 }

Modified: trunk/gcalctool/gtk.c
==============================================================================
--- trunk/gcalctool/gtk.c	(original)
+++ trunk/gcalctool/gtk.c	Sat Aug  9 05:59:10 2008
@@ -478,7 +478,7 @@
 
     ans = display_get_answer(&v->display);
     MPstr_to_num("0", DEC, ans);
-    display_clear(&v->display, FALSE);
+    display_clear(&v->display);
     display_set_number(&v->display, ans);
 }
 
@@ -524,7 +524,8 @@
     set_int_resource(R_ACCURACY, accuracy);
     
     ui_make_registers();
-    display_refresh(&v->display, -1);
+    display_set_cursor(&v->display, -1);
+    display_refresh(&v->display);
     
     /* Hide the manual dialog */
     gtk_widget_hide(X->spframe);
@@ -1645,8 +1646,8 @@
     number = (double) lval;
 
     mp_set_from_double(number, display_get_answer(&v->display));
-    display_set_string(&v->display, "Ans");
-    display_refresh(&v->display, -1);
+    display_set_string(&v->display, "Ans", -1);
+    display_refresh(&v->display);
 
     return (TRUE);
 }
@@ -2063,8 +2064,10 @@
     }
     *dstp++ = '\0';
 
-    display_insert(&v->display, (char *) text, get_cursor()); // FIXME: Move out of gtk.c
-    display_refresh(&v->display, -1);
+    display_set_cursor(&v->display, get_cursor()); // FIXME: Move out of gtk.c
+    display_insert(&v->display, (char *) text);
+    display_set_cursor(&v->display, -1);
+    display_refresh(&v->display);
     free(text);
 }
 
@@ -2112,7 +2115,8 @@
 redo_cb(GtkWidget *widget)
 {
     perform_redo();
-    display_refresh(&v->display, -1);
+    display_set_cursor(&v->display, -1);
+    display_refresh(&v->display);
 }
 
 



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