simplify change mode
[gcalctool.git] / gcalctool / calctool.h
blobbd0e7e1d5dea0aafa864f87d234f40eb7b780a29
2 /* $Header$
4 * Copyright (c) 1987-2007 Sun Microsystems, Inc. All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
22 #ifndef CALCTOOL_H
23 #define CALCTOOL_H
25 #include "config.h"
26 #include "mp.h"
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <locale.h>
32 #include <math.h>
33 #include <glib/gi18n.h>
34 #include <gtk/gtk.h>
36 #define FCLOSE (void) fclose /* To make lint happy. */
37 #define FPRINTF (void) fprintf
38 #define FPUTS (void) fputs
39 #define GETHOSTNAME (void) gethostname
40 #define MEMCPY (void) memcpy
41 #define MEMSET (void) memset
42 #define MKSTEMP (void) mkstemp
43 #define REWIND (void) rewind
44 #define SNPRINTF (void) snprintf
45 #define SPRINTF (void) sprintf
46 #define SSCANF (void) sscanf
47 #define STRCAT (void) strcat
48 #define STRCPY (void) strcpy
49 #define STRNCPY (void) strncpy
50 #define STRNCAT (void) strncat
51 #define UNLINK (void) unlink
53 /* Menu bar menu types. */
55 enum mb_type { M_ABOUT, M_ASCII, M_BASIC, M_ADV, M_CONTENTS, M_COPY, M_FIN,
56 M_PASTE, M_QUIT, M_REGS, M_SCI, M_EXP, M_TSEP, M_ZEROES,
57 M_LR_ARITH, M_OP_ARITH };
59 enum base_type { BIN, OCT, DEC, HEX }; /* Base definitions. */
61 enum item_type { BASEITEM, TTYPEITEM, NUMITEM,
62 HYPITEM, INVITEM, OPITEM, MODEITEM };
64 /* Calculator modes. */
65 enum mode_type { BASIC, ADVANCED, FINANCIAL, SCIENTIFIC };
67 enum num_type { ENG, FIX, SCI }; /* Number display mode. */
69 enum trig_type { DEG, GRAD, RAD }; /* Trigonometric types. */
71 enum trig_func {SIN=0, COS=1, TAN=2};
73 /* Abbreviations for the gcalctool keyboard */
75 enum
77 KEY_0,
78 KEY_1,
79 KEY_2,
80 KEY_3,
81 KEY_4,
82 KEY_5,
83 KEY_6,
84 KEY_7,
85 KEY_8,
86 KEY_9,
87 KEY_A,
88 KEY_B,
89 KEY_C,
90 KEY_D,
91 KEY_E,
92 KEY_F,
93 KEY_NUMERIC_POINT,
94 KEY_CALCULATE,
95 KEY_CLEAR,
96 KEY_CLEAR_ENTRY,
97 KEY_START_BLOCK,
98 KEY_END_BLOCK,
99 KEY_ADD,
100 KEY_SUBTRACT,
101 KEY_MULTIPLY,
102 KEY_DIVIDE,
103 KEY_BACKSPACE,
104 KEY_CHANGE_SIGN,
105 KEY_INTEGER,
106 KEY_FRACTION,
107 KEY_PERCENTAGE,
108 KEY_SQUARE,
109 KEY_SQUARE_ROOT,
110 KEY_RECIPROCAL,
111 KEY_E_POW_X,
112 KEY_10_POW_X,
113 KEY_X_POW_Y,
114 KEY_FACTORIAL,
115 KEY_RANDOM,
116 KEY_SINE,
117 KEY_COSINE,
118 KEY_TANGENT,
119 KEY_NATURAL_LOGARITHM,
120 KEY_LOGARITHM,
121 KEY_ABSOLUTE_VALUE,
122 KEY_MASK_16,
123 KEY_MASK_32,
124 KEY_MODULUS_DIVIDE,
125 KEY_EXPONENTIAL,
126 KEY_NOT,
127 KEY_OR,
128 KEY_AND,
129 KEY_XOR,
130 KEY_XNOR,
131 KEY_FINC_CTRM,
132 KEY_FINC_DDB,
133 KEY_FINC_FV,
134 KEY_FINC_PMT,
135 KEY_FINC_PV,
136 KEY_FINC_RATE,
137 KEY_FINC_SLN,
138 KEY_FINC_SYD,
139 KEY_FINC_TERM,
140 KEY_LEFT_SHIFT,
141 KEY_RIGHT_SHIFT,
142 KEY_STORE,
143 KEY_RECALL,
144 KEY_EXCHANGE,
145 KEY_SET_ACCURACY,
146 KEY_CONSTANT,
147 KEY_FUNCTION,
148 NKEYS
151 #define EQUAL(a, b) (strlen(a)==strlen(b)) & !strcmp(a, b)
153 #ifndef LINT_CAST
154 #ifdef lint
155 #define LINT_CAST(arg) (arg ? 0 : 0)
156 #else
157 #define LINT_CAST(arg) (arg)
158 #endif /*lint*/
159 #endif /*LINT_CAST*/
161 #define MAX_DIGITS 200 /* Maximum displayable number of digits. */
162 #define MAX_LOCALIZED (MAX_DIGITS * (1 + MB_LEN_MAX) + MB_LEN_MAX)
164 #ifndef MAXLINE
165 #define MAXLINE 512 /* Length of character strings. */
166 #endif /*MAXLINE*/
168 #define MAXACC 99 /* Max. number of digits after numeric point. */
169 #define MAXBASES 4 /* Maximum number of numeric bases. */
170 #define MAXCONFUN 10 /* Maximum number of constants/functions. */
171 #define MAXDISPMODES 3 /* Maximum number of display modes. */
172 #define MAXMODES 4 /* Maximum number of calculator modes. */
173 #define MAXREGS 10 /* Maximum number of memory registers. */
174 #define MAXSTACK 256 /* Parenthese stack size. */
175 #define MAXTRIGMODES 3 /* Maximum number of trig. modes. */
176 #define MAXSYNTAX 2 /* Number of syntaxes in calculator */
177 #define MAXBITCALC 2 /* Choices for bitcalculating */
179 #ifndef MIN
180 #define MIN(x,y) ((x) < (y) ? (x) : (y))
181 #endif /*MIN*/
183 #ifndef CFNAME
184 #define CFNAME ".gcalctoolcf"
185 #endif /*CFNAME*/
187 #ifndef RCNAME
188 #define RCNAME ".gcalctoolrc"
189 #endif /*RCNAME*/
191 #undef TRUE /* Boolean definitions. */
192 #define TRUE 1
194 #undef FALSE
195 #define FALSE 0
197 #define UNDO_HISTORY_LENGTH 16 /* Arithmetic mode undo history length */
199 #define MPMATH_ERR 20001
201 enum button_flags {
202 none = 0, /* No flags */
203 enter = (1 << 2), /* Expression is entered */
204 number = (1 << 3), /* Number button */
205 func = (1 << 6), /* Function */
206 bsp = (1 << 7), /* Backspace */
207 clear = (1 << 8), /* Clear display */
208 neg = (1 << 9), /* Negate display */
209 inv = (1 << 10), /* Reciprocial */
210 con = (1 << 11), /* Constant */
211 regrcl = (1 << 12), /* Recall register */
212 expnum = (1 << 13), /* Exponential number */
213 postfixop = (1 << 14), /* Unary postfix operation */
214 prefixop = (1 << 15), /* Unary prefix operation */
215 dpoint = (1 << 16) /* Decimal point */
218 enum shiftd {
219 left = 0,
220 right
223 enum syntax {
224 npa = 0, /* Non-precedence arithmetic */
225 exprs, /* Expression with arithmetic precedence */
228 struct button {
229 int id;
230 char *symname; /* Expression function name */
231 void (*func)(); /* Function to obey on button press. */
232 enum button_flags flags; /* Misc flags */
235 struct exprm_state { /* Expression mode state */
236 struct button button; /* Current button/character pressed. */
237 int value;
238 int ans[MP_SIZE]; /* Previously calculated answer */
239 char *expression; /* Expression entered by user */
242 /* Circular list of Arithmetic Precedence Mode states*/
243 struct exprm_state_history {
244 unsigned int begin;
245 unsigned int end;
246 unsigned int current;
247 struct exprm_state e[UNDO_HISTORY_LENGTH]; /* Expression mode state */
250 struct menu {
251 char *title; /* Menu title. */
252 int total; /* Number of menu entries. */
253 int index; /* Index into menu string array. */
254 int defval; /* Default menu item position (from 1). */
257 struct calcVars { /* Calctool variables and options. */
258 struct exprm_state_history h; /* History of expression mode states */
260 int current; /* Current button/character pressed. */
262 char *appname; /* Application name for resources. */
263 char con_names[MAXREGS][MAXLINE]; /* Selectable constant names. */
264 char display[MAXLINE]; /* Current calculator display. */
265 char *exp_posn; /* Position of the exponent sign. */
266 char fnum[MAX_DIGITS]; /* Scratchpad for fixed numbers. */
267 char fun_names[MAXREGS][MAXLINE]; /* Function names from .gcalctoolcf. */
268 char fun_vals[MAXREGS][MAXLINE]; /* Function defs from .gcalctoolcf. */
269 char *home; /* Pathname for users home directory. */
270 char *iconlabel; /* The calctool icon label. */
271 char op_item_text[5]; /* Operand item panel string. */
272 char opstr[5]; /* Operand string during pending op. */
273 char *progname; /* Name of this program. */
274 char pstr[5]; /* Current button text string. */
275 const char *radix; /* Locale specific radix string. */
276 char *shelf; /* PUT selection shelf contents. */
277 char snum[MAX_DIGITS]; /* Scratchpad for scientific numbers. */
278 const char *tsep; /* Locale specific thousands seperator. */
280 int MPcon_vals[MAXREGS][MP_SIZE]; /* Selectable constants. */
281 int MPdebug; /* If set, debug info. to stderr. */
282 int MPerrors; /* If set, output errors to stderr. */
283 int MPdisp_val[MP_SIZE]; /* Value of the current display. */
284 int MPexpr_val[MP_SIZE]; /* Value of the current expression. */
285 int MPlast_input[MP_SIZE]; /* Previous number input by user. */
286 int MPmvals[MAXREGS][MP_SIZE]; /* Memory register values. */
287 int *MPnumstack[MAXSTACK]; /* Numeric stack for parens. */
288 int MPresult[MP_SIZE]; /* Current calculator total value. */
289 int MPimresult[MP_SIZE]; /* Current intermediate result. */
290 int MPtresults[3][MP_SIZE]; /* Current trigonometric results. */
292 enum base_type base; /* Current base: BIN, OCT, DEC or HEX. */
293 enum mode_type modetype; /* Current calculator mode. */
294 enum num_type dtype; /* Number display mode. */
295 enum trig_type ttype; /* Trig. type (deg, grad or rad). */
297 enum syntax syntax; /* Calculation syntax mode */
299 int accuracy; /* Number of digits precision (Max 9). */
300 int cur_op; /* Current arithmetic operation. */
301 int doing_mi; /* Set if adjusting the "show zeroes" menu item. */
302 int error; /* Indicates some kind of display error. */
303 int ghost_zero; /* Flag to indicate display with "0", actually
304 having empty content. */
305 int math_error; /* Math error (used in expression mode) */
306 int hyperbolic; /* If set, trig functions will be hyperbolic. */
307 int iconic; /* Set if window is currently iconic. */
308 int inverse; /* If set, trig and log functions will be inversed. */
309 int key_exp; /* Set if entering exponent number. */
310 int ndisplay; /* Height of the numerical display. */
311 int new_input; /* New number input since last op. */
312 int noparens; /* Count of left brackets still to be matched. */
313 int numsptr; /* Pointer into the parenthese numeric stack. */
314 int old_cal_value; /* Previous calculation operator. */
315 int opsptr; /* Pointer into the parentheses operand stack. */
316 int opstack[MAXSTACK]; /* Stack containing parentheses input. */
317 int pointed; /* Whether a decimal point has been given. */
318 int rstate; /* Indicates if memory register frame is displayed. */
319 int show_paren; /* Set if we wish to show DISPLAYITEM during parens. */
320 int show_tsep; /* Set if the thousands seperator should be shown. */
321 int show_zeroes; /* Set if trailing zeroes should be shown. */
322 int started; /* Set just before window is displayed. */
323 int toclear; /* Indicates if display should be cleared. */
324 int warn_change_mode; /* Should we warn user when changing modes? */
325 int bitcalculating_mode; /* 0 = no, else yes */
328 typedef struct calcVars *Vars;
330 extern Vars v; /* Calctool variables and options. */
331 extern int basevals[]; /* Supported arithmetic bases. */
332 extern struct button buttons[]; /* Calculator button values. */
334 void doerr(char *);
336 #endif /*CALCTOOL_H*/