gcalctool r2157 - trunk/gcalctool
- From: rancell svn gnome org
- To: svn-commits-list gnome org
- Subject: gcalctool r2157 - trunk/gcalctool
- Date: Sat, 9 Aug 2008 06:27:20 +0000 (UTC)
Author: rancell
Date: Sat Aug 9 06:27:19 2008
New Revision: 2157
URL: http://svn.gnome.org/viewvc/gcalctool?rev=2157&view=rev
Log:
Streamline display code
Modified:
trunk/gcalctool/calctool.c
trunk/gcalctool/calctool.h
trunk/gcalctool/functions.c
trunk/gcalctool/functions.h
trunk/gcalctool/gtk.c
Modified: trunk/gcalctool/calctool.c
==============================================================================
--- trunk/gcalctool/calctool.c (original)
+++ trunk/gcalctool/calctool.c Sat Aug 9 06:27:19 2008
@@ -446,6 +446,26 @@
0
},
{
+ KEY_SET_BASE,
+ NULL,
+ 0
+},
+{
+ KEY_SET_NUMBERTYPE,
+ NULL,
+ 0
+},
+{
+ KEY_UNDO,
+ NULL,
+ 0
+},
+{
+ KEY_REDO,
+ NULL,
+ 0
+},
+{
KEY_CONSTANT,
NULL,
0
@@ -655,8 +675,6 @@
read_resources(); /* Read resources from merged database. */
ui_load();
- do_clear(); /* Initialise and clear display. */
-
ui_start(); /* Display the calculator. */
return(0);
Modified: trunk/gcalctool/calctool.h
==============================================================================
--- trunk/gcalctool/calctool.h (original)
+++ trunk/gcalctool/calctool.h Sat Aug 9 06:27:19 2008
@@ -112,6 +112,10 @@
KEY_SHIFT,
KEY_STORE, KEY_RECALL, KEY_EXCHANGE,
KEY_SET_ACCURACY,
+ KEY_SET_BASE,
+ KEY_SET_NUMBERTYPE,
+ KEY_UNDO,
+ KEY_REDO,
KEY_CONSTANT,
KEY_FUNCTION,
NKEYS
Modified: trunk/gcalctool/functions.c
==============================================================================
--- trunk/gcalctool/functions.c (original)
+++ trunk/gcalctool/functions.c Sat Aug 9 06:27:19 2008
@@ -77,21 +77,7 @@
}
-void
-perform_undo(void)
-{
- display_pop(&v->display);
-}
-
-
-void
-perform_redo(void)
-{
- display_unpop(&v->display);
-}
-
-
-void
+static void
do_accuracy(int value) /* Set display accuracy. */
{
v->accuracy = value;
@@ -99,7 +85,9 @@
ui_set_accuracy(v->accuracy);
ui_make_registers();
clear_undo_history();
- syntaxdep_show_display();
+
+ display_set_cursor(&v->display, -1);
+ display_refresh(&v->display);
}
@@ -137,24 +125,13 @@
display_set_string(&v->display, "Ans", -1);
}
- syntaxdep_show_display();
-}
-
-
-/* Clear the calculator display and re-initialise. */
-void
-do_clear(void)
-{
- display_clear(&v->display);
- if (v->error) {
- ui_set_display("", -1);
- }
- display_reset(&v->display);
+ display_set_cursor(&v->display, -1);
+ display_refresh(&v->display);
}
/* Change the current base setting. */
-void
+static void
do_base(enum base_type b)
{
int ret, MP[MP_SIZE];
@@ -198,11 +175,12 @@
ui_make_registers();
}
- syntaxdep_show_display();
+ display_set_cursor(&v->display, -1);
+ display_refresh(&v->display);
}
-void
+static void
do_numtype(enum num_type n) /* Set number display type. */
{
int ret, MP[MP_SIZE];
@@ -243,15 +221,11 @@
* TODO: remove hardcoding from reg ranges.
*/
-int
+void
do_sto_reg(int reg, int value[MP_SIZE])
{
- if ((reg >= 0) && (reg <= 10)) {
+ if ((reg >= 0) && (reg <= 10))
mp_set_from_mp(value, v->MPmvals[reg]);
- return(0);
- } else {
- return(-EINVAL);
- }
}
@@ -260,23 +234,11 @@
* TODO: remove hardcoding from reg ranges.
*/
-int
+void
do_rcl_reg(int reg, int value[MP_SIZE])
{
- if ((reg >= 0) && (reg <= 10)) {
+ if ((reg >= 0) && (reg <= 10))
mp_set_from_mp(v->MPmvals[reg], value);
- return(0);
- } else {
- return(-EINVAL);
- }
-}
-
-
-void
-syntaxdep_show_display(void)
-{
- display_set_cursor(&v->display, -1);
- display_refresh(&v->display);
}
@@ -316,6 +278,22 @@
do_accuracy(arg);
return;
+ case KEY_SET_BASE:
+ do_base(arg);
+ return;
+
+ case KEY_SET_NUMBERTYPE:
+ do_numtype(arg);
+ return;
+
+ case KEY_UNDO:
+ display_pop(&v->display);
+ return;
+
+ case KEY_REDO:
+ display_unpop(&v->display);
+ return;
+
case KEY_FUNCTION:
do_function(arg);
return;
@@ -360,10 +338,10 @@
/* TODO: Work out why two undo steps are required and why
* the cursor must be taken from the first undo */
if (display_is_result(&v->display)) {
- perform_undo();
+ display_pop(&v->display);
cursor = display_get_cursor(&v->display);
if (display_is_undo_step(&v->display)) {
- perform_undo();
+ display_pop(&v->display);
}
/* Do nothing */
Modified: trunk/gcalctool/functions.h
==============================================================================
--- trunk/gcalctool/functions.h (original)
+++ trunk/gcalctool/functions.h Sat Aug 9 06:27:19 2008
@@ -24,19 +24,11 @@
#include "calctool.h"
-void syntaxdep_show_display(void);
+void do_expression(int function, int arg, int cursor);
+// FIXME: Not functions
void make_exp(char *number, int t[MP_SIZE]);
-
-void perform_undo(void);
-void perform_redo(void);
-
-void do_base(enum base_type);
-void do_expression(int function, int arg, int cursor);
-void do_clear(void);
-void do_numtype(enum num_type);
-void do_accuracy(int);
-int do_rcl_reg(int reg, int value[MP_SIZE]);
-int do_sto_reg(int reg, int value[MP_SIZE]);
+void do_rcl_reg(int reg, int value[MP_SIZE]);
+void do_sto_reg(int reg, int value[MP_SIZE]);
#endif /*FUNCTIONS_H*/
Modified: trunk/gcalctool/gtk.c
==============================================================================
--- trunk/gcalctool/gtk.c (original)
+++ trunk/gcalctool/gtk.c Sat Aug 9 06:27:19 2008
@@ -490,8 +490,6 @@
char text[MAXLINE];
char *desc, *current, *tooltip;
- v->accuracy = accuracy;
-
SNPRINTF(text, MAXLINE, _("_Other (%d) ..."), accuracy);
widget = gtk_bin_get_child(GTK_BIN(GET_WIDGET("acc_item_other")));
gtk_label_set_markup_with_mnemonic(GTK_LABEL(widget), text);
@@ -614,7 +612,9 @@
menu = GET_WIDGET("show_thousands_separator_menu");
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu), visible);
- syntaxdep_show_display();
+ display_set_cursor(&v->display, -1);
+ display_refresh(&v->display);
+
ui_make_registers();
}
@@ -656,7 +656,9 @@
v->show_zeroes = visible;
set_boolean_resource(R_ZEROES, visible);
- syntaxdep_show_display();
+ display_set_cursor(&v->display, -1);
+ display_refresh(&v->display);
+
ui_make_registers();
menu = GET_WIDGET("show_trailing_zeroes_menu");
@@ -711,6 +713,70 @@
}
+gchar *
+ui_get_display(void)
+{
+ GtkTextIter start, end;
+ gtk_text_buffer_get_bounds(X->display_buffer, &start, &end);
+ return (gtk_text_buffer_get_text(X->display_buffer,
+ &start,
+ &end,
+ FALSE));
+}
+
+
+static int
+get_cursor(void)
+{
+ gint pos;
+ g_object_get(G_OBJECT(X->display_buffer), "cursor-position", &pos, NULL);
+
+ /* Convert the last position to -1 */
+ if (pos == gtk_text_buffer_get_char_count(X->display_buffer)) {
+ return (-1);
+ } else {
+ return (pos);
+ }
+}
+
+
+static void
+set_bit_panel(void)
+{
+ int bit_str_len, i;
+ int MP[MP_SIZE];
+ char bit_str[MAXLINE], label[MAXLINE];
+
+ if (display_is_usable_number(&v->display, MP) || !is_integer(MP)) {
+ gtk_widget_set_sensitive(X->bit_panel, FALSE);
+ return;
+ }
+ make_fixed(bit_str, MAXLINE, MP, BIN, MAXLINE, FALSE);
+ bit_str_len = strlen(bit_str);
+ if (bit_str_len <= MAXBITS) {
+ gtk_widget_set_sensitive(X->bit_panel, TRUE);
+
+ for (i = 0; i < MAXBITS; i++) {
+ if (i < bit_str_len) {
+ SNPRINTF(label, MAXLINE, " %c", bit_str[bit_str_len-i-1]);
+ } else {
+ SNPRINTF(label, MAXLINE, " 0");
+ }
+ gtk_label_set_text(GTK_LABEL(X->bits[MAXBITS - i - 1]), label);
+ }
+ } else {
+ gtk_widget_set_sensitive(X->bit_panel, FALSE);
+ }
+}
+
+
+static void do_button(int function, int arg)
+{
+ do_expression(function, arg, get_cursor());
+ set_bit_panel();
+}
+
+
void
ui_set_mode(enum mode_type mode)
{
@@ -724,13 +790,13 @@
ui_set_base(DEC);
ui_set_numeric_mode(FIX);
- ui_set_accuracy(DEFAULT_ACCURACY);
+ do_button(KEY_SET_ACCURACY, DEFAULT_ACCURACY);
ui_set_show_thousands_separator(FALSE);
ui_set_show_trailing_zeroes(FALSE);
ui_make_registers();
/* Reset display */
- do_clear();
+ display_reset(&v->display);
ui_set_statusbar("", "");
}
@@ -834,34 +900,6 @@
gtk_statusbar_push(GTK_STATUSBAR(X->statusbar), 0, text);
}
-static void
-set_bit_panel(void)
-{
- int bit_str_len, i;
- int MP[MP_SIZE];
- char bit_str[MAXLINE], label[MAXLINE];
-
- if (display_is_usable_number(&v->display, MP) || !is_integer(MP)) {
- gtk_widget_set_sensitive(X->bit_panel, FALSE);
- return;
- }
- make_fixed(bit_str, MAXLINE, MP, BIN, MAXLINE, FALSE);
- bit_str_len = strlen(bit_str);
- if (bit_str_len <= MAXBITS) {
- gtk_widget_set_sensitive(X->bit_panel, TRUE);
-
- for (i = 0; i < MAXBITS; i++) {
- if (i < bit_str_len) {
- SNPRINTF(label, MAXLINE, " %c", bit_str[bit_str_len-i-1]);
- } else {
- SNPRINTF(label, MAXLINE, " 0");
- }
- gtk_label_set_text(GTK_LABEL(X->bits[MAXBITS - i - 1]), label);
- }
- } else {
- gtk_widget_set_sensitive(X->bit_panel, FALSE);
- }
-}
static gboolean
redo_display(gpointer data)
@@ -1026,33 +1064,6 @@
}
-gchar *
-ui_get_display(void)
-{
- GtkTextIter start, end;
- gtk_text_buffer_get_bounds(X->display_buffer, &start, &end);
- return (gtk_text_buffer_get_text(X->display_buffer,
- &start,
- &end,
- FALSE));
-}
-
-
-static int
-get_cursor(void)
-{
- gint pos;
- g_object_get(G_OBJECT(X->display_buffer), "cursor-position", &pos, NULL);
-
- /* Convert the last position to -1 */
- if (pos == gtk_text_buffer_get_char_count(X->display_buffer)) {
- return (-1);
- } else {
- return (pos);
- }
-}
-
-
/*ARGSUSED*/
static void
about_cb(GtkWidget *widget)
@@ -1217,7 +1228,7 @@
disp_cb(GtkWidget *widget)
{
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
- do_numtype((enum num_type) g_object_get_data(G_OBJECT(widget), "numeric_mode"));
+ do_button(KEY_SET_NUMBERTYPE, (int)g_object_get_data(G_OBJECT(widget), "numeric_mode"));
}
@@ -1230,18 +1241,11 @@
base = (enum base_type) g_object_get_data(G_OBJECT(widget),
"base_mode");
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) {
- do_base(base);
+ do_button(KEY_SET_BASE, base);
}
}
-static void do_button(int function, int arg)
-{
- do_expression(function, arg, get_cursor());
- set_bit_panel();
-}
-
-
static void
help_display(void)
{
@@ -1899,34 +1903,34 @@
if (state == GDK_CONTROL_MASK && v->modetype == SCIENTIFIC) {
switch (event->keyval) {
case GDK_0:
- do_accuracy(0);
+ do_button(KEY_SET_ACCURACY, 0);
return (TRUE);
case GDK_1:
- do_accuracy(1);
+ do_button(KEY_SET_ACCURACY, 1);
return (TRUE);
case GDK_2:
- do_accuracy(2);
+ do_button(KEY_SET_ACCURACY, 2);
return (TRUE);
case GDK_3:
- do_accuracy(3);
+ do_button(KEY_SET_ACCURACY, 3);
return (TRUE);
case GDK_4:
- do_accuracy(4);
+ do_button(KEY_SET_ACCURACY, 4);
return (TRUE);
case GDK_5:
- do_accuracy(5);
+ do_button(KEY_SET_ACCURACY, 5);
return (TRUE);
case GDK_6:
- do_accuracy(6);
+ do_button(KEY_SET_ACCURACY, 6);
return (TRUE);
case GDK_7:
- do_accuracy(7);
+ do_button(KEY_SET_ACCURACY, 7);
return (TRUE);
case GDK_8:
- do_accuracy(8);
+ do_button(KEY_SET_ACCURACY, 8);
return (TRUE);
case GDK_9:
- do_accuracy(9);
+ do_button(KEY_SET_ACCURACY, 9);
return (TRUE);
}
}
@@ -2106,7 +2110,7 @@
static void
undo_cb(GtkWidget *widget)
{
- perform_undo();
+ do_button(KEY_UNDO, 0);
}
@@ -2114,7 +2118,7 @@
static void
redo_cb(GtkWidget *widget)
{
- perform_redo();
+ do_button(KEY_REDO, 0);
display_set_cursor(&v->display, -1);
display_refresh(&v->display);
}
@@ -2236,7 +2240,7 @@
int count;
count = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "accuracy"));
if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) {
- do_accuracy(count);
+ do_button(KEY_SET_ACCURACY, count);
}
}
@@ -2257,7 +2261,7 @@
static void
accuracy_default_cb(GtkWidget *widget)
{
- do_accuracy(DEFAULT_ACCURACY);
+ do_button(KEY_SET_ACCURACY, DEFAULT_ACCURACY);
}
@@ -2287,7 +2291,7 @@
int val;
if (response_id == GTK_RESPONSE_OK) {
val = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(X->precision_spin));
- ui_set_accuracy(val);
+ do_button(KEY_SET_ACCURACY, val);
}
gtk_widget_hide(dialog);
@@ -2783,9 +2787,6 @@
gtk_widget_show(X->kframe);
- /* Init expression mode.
- * This must be executed after do_base is called at init.
- */
reset_display();
gtk_main();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]