[CALC] Improve multi-precision support, and powers/roots. CORE-8486
[reactos.git] / base / applications / calc / winmain.c
1 #include "calc.h"
2
3 #include <winbase.h>
4 #include <wingdi.h>
5 #include <winreg.h>
6 #include <shellapi.h>
7 #include <commctrl.h>
8
9 #define HTMLHELP_PATH(_pt) TEXT("%systemroot%\\Help\\calc.chm::") TEXT(_pt)
10
11 #define MAKE_BITMASK4(_show_b16, _show_b10, _show_b8, _show_b2) \
12 (((_show_b2) << 0) | \
13 ((_show_b8) << 1) | \
14 ((_show_b10) << 2) | \
15 ((_show_b16) << 3))
16
17 #define MAKE_BITMASK5(_transl, _is_stats, _is_ctrl, _show_b16, _show_b10, _show_b8, _show_b2) \
18 (((_show_b2) << 0) | \
19 ((_show_b8) << 1) | \
20 ((_show_b10) << 2) | \
21 ((_show_b16) << 3) | \
22 ((_is_ctrl) << 5) | \
23 ((_is_stats) << 6) | \
24 ((_transl) << 7))
25
26 #define KEY_IS_UP 0x80000000
27 #define KEY_WAS_DOWN 0x40000000
28
29 #define BITMASK_IS_ASCII 0x80
30 #define BITMASK_IS_STATS 0x40
31 #define BITMASK_IS_CTRL 0x20
32 #define BITMASK_HEX_MASK 0x08
33 #define BITMASK_DEC_MASK 0x04
34 #define BITMASK_OCT_MASK 0x02
35 #define BITMASK_BIN_MASK 0x01
36
37 #define CALC_CLR_RED 0x000000FF
38 #define CALC_CLR_BLUE 0x00FF0000
39 #define CALC_CLR_PURP 0x00FF00FF
40
41 typedef struct {
42 CHAR key; // Virtual key identifier
43 WORD idc; // IDC for posting message
44 } key2code_t;
45
46 typedef struct {
47 WORD idc; // IDC for posting message
48 CHAR key; // Virtual key identifier
49 BYTE mask; // enable/disable into the various modes.
50 COLORREF col; // color used for drawing the text
51 } key3code_t;
52
53 #define CTRL_FLAG 0x100
54 #define ALT_FLAG 0x200
55
56 #define CTRL_A (0x0001+'A'-'A')
57 #define CTRL_C (0x0001+'C'-'A')
58 #define CTRL_D (0x0001+'D'-'A')
59 #define CTRL_L (0x0001+'L'-'A')
60 #define CTRL_M (0x0001+'M'-'A')
61 #define CTRL_P (0x0001+'P'-'A')
62 #define CTRL_R (0x0001+'R'-'A')
63 #define CTRL_S (0x0001+'S'-'A')
64 #define CTRL_T (0x0001+'T'-'A')
65 #define CTRL_V (0x0001+'V'-'A')
66 #define CTRL_Z (0x0001+'Z'-'A')
67
68 static const key3code_t key2code[] = {
69 /* CONTROL-ID Key asc sta ctl hex dec oct bin */
70 { IDC_BUTTON_STA, CTRL_S, MAKE_BITMASK5( 1, 0, 1, 1, 1, 1, 1), CALC_CLR_BLUE, },
71 { IDC_BUTTON_AVE, CTRL_A, MAKE_BITMASK5( 1, 1, 1, 1, 1, 1, 1), CALC_CLR_BLUE, },
72 { IDC_BUTTON_SUM, CTRL_T, MAKE_BITMASK5( 1, 1, 1, 1, 1, 1, 1), CALC_CLR_BLUE, },
73 { IDC_BUTTON_S, CTRL_D, MAKE_BITMASK5( 1, 1, 1, 1, 1, 1, 1), CALC_CLR_BLUE, },
74 { IDC_BUTTON_MS, CTRL_M, MAKE_BITMASK5( 1, 0, 1, 1, 1, 1, 1), CALC_CLR_RED, },
75 { IDC_BUTTON_MR, CTRL_R, MAKE_BITMASK5( 1, 0, 1, 1, 1, 1, 1), CALC_CLR_RED, },
76 { IDC_BUTTON_MP, CTRL_P, MAKE_BITMASK5( 1, 0, 1, 1, 1, 1, 1), CALC_CLR_RED, },
77 { IDC_BUTTON_MC, CTRL_L, MAKE_BITMASK5( 1, 0, 1, 1, 1, 1, 1), CALC_CLR_RED, },
78 { IDC_BUTTON_0, '0', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_BLUE, },
79 { IDC_BUTTON_1, '1', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_BLUE, },
80 { IDC_BUTTON_2, '2', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 0), CALC_CLR_BLUE, },
81 { IDC_BUTTON_3, '3', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 0), CALC_CLR_BLUE, },
82 { IDC_BUTTON_4, '4', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 0), CALC_CLR_BLUE, },
83 { IDC_BUTTON_5, '5', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 0), CALC_CLR_BLUE, },
84 { IDC_BUTTON_6, '6', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 0), CALC_CLR_BLUE, },
85 { IDC_BUTTON_7, '7', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 0), CALC_CLR_BLUE, },
86 { IDC_BUTTON_8, '8', MAKE_BITMASK5( 1, 0, 0, 1, 1, 0, 0), CALC_CLR_BLUE, },
87 { IDC_BUTTON_9, '9', MAKE_BITMASK5( 1, 0, 0, 1, 1, 0, 0), CALC_CLR_BLUE, },
88 { IDC_BUTTON_DOT, '.', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_BLUE, },
89 { IDC_BUTTON_DOT, ',', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), -1, },
90 { IDC_BUTTON_ADD, '+', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
91 { IDC_BUTTON_SUB, '-', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
92 { IDC_BUTTON_MULT, '*', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
93 { IDC_BUTTON_DIV, '/', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
94 { IDC_BUTTON_AND, '&', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
95 { IDC_BUTTON_OR, '|', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
96 { IDC_BUTTON_XOR, '^', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
97 { IDC_BUTTON_LSH, '<', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
98 { IDC_BUTTON_NOT, '~', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
99 { IDC_BUTTON_INT, ';', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_RED, },
100 { IDC_BUTTON_EQU, '=', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
101 { IDC_BUTTON_A, 'A', MAKE_BITMASK5( 1, 0, 0, 1, 0, 0, 0), CALC_CLR_BLUE, },
102 { IDC_BUTTON_B, 'B', MAKE_BITMASK5( 1, 0, 0, 1, 0, 0, 0), CALC_CLR_BLUE, },
103 { IDC_BUTTON_C, 'C', MAKE_BITMASK5( 1, 0, 0, 1, 0, 0, 0), CALC_CLR_BLUE, },
104 { IDC_BUTTON_D, 'D', MAKE_BITMASK5( 1, 0, 0, 1, 0, 0, 0), CALC_CLR_BLUE, },
105 { IDC_BUTTON_E, 'E', MAKE_BITMASK5( 1, 0, 0, 1, 0, 0, 0), CALC_CLR_BLUE, },
106 { IDC_BUTTON_F, 'F', MAKE_BITMASK5( 1, 0, 0, 1, 0, 0, 0), CALC_CLR_BLUE, },
107 { IDC_CHECK_HYP, 'H', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), -1, },
108 { IDC_CHECK_INV, 'I', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), -1, },
109 { IDC_BUTTON_LOG, 'L', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_PURP, },
110 { IDC_BUTTON_DMS, 'M', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_PURP, },
111 { IDC_BUTTON_LN, 'N', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_PURP, },
112 { IDC_BUTTON_PI, 'P', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_BLUE, },
113 { IDC_BUTTON_RX, 'R', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_PURP, },
114 { IDC_BUTTON_SIN, 'S', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_PURP, },
115 { IDC_BUTTON_COS, 'O', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_PURP, },
116 { IDC_BUTTON_TAN, 'T', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_PURP, },
117 { IDC_BUTTON_FE, 'V', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_PURP, },
118 { IDC_BUTTON_EXP, 'X', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_PURP, },
119 { IDC_BUTTON_XeY, 'Y', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_PURP, },
120 { IDC_BUTTON_SQRT, '@', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_BLUE, },
121 { IDC_BUTTON_Xe2, '@', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_PURP, },
122 { IDC_BUTTON_Xe3, '#', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_PURP, },
123 { IDC_BUTTON_NF, '!', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_PURP, },
124 { IDC_BUTTON_LEFTPAR, '(', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_PURP, },
125 { IDC_BUTTON_RIGHTPAR, ')', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_PURP, },
126 { IDC_BUTTON_MOD, '%', MAKE_BITMASK5( 1, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
127 { IDC_BUTTON_PERCENT, '%', MAKE_BITMASK5( 1, 0, 0, 0, 1, 0, 0), CALC_CLR_BLUE, },
128 /*----------------------------------------------------------------------*/
129 { IDC_BUTTON_DAT, VK_INSERT, MAKE_BITMASK5( 0, 1, 0, 1, 1, 1, 1), CALC_CLR_BLUE, },
130 { IDC_BUTTON_EQU, VK_RETURN, MAKE_BITMASK5( 0, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
131 { IDC_BUTTON_CANC, VK_ESCAPE, MAKE_BITMASK5( 0, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
132 { IDC_BUTTON_CE, VK_DELETE, MAKE_BITMASK5( 0, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
133 { IDC_BUTTON_BACK, VK_BACK, MAKE_BITMASK5( 0, 0, 0, 1, 1, 1, 1), CALC_CLR_RED, },
134 { IDC_RADIO_HEX, VK_F5, MAKE_BITMASK5( 0, 0, 0, 1, 1, 1, 1), -1, },
135 { IDC_RADIO_DEC, VK_F6, MAKE_BITMASK5( 0, 0, 0, 1, 1, 1, 1), -1, },
136 { IDC_RADIO_OCT, VK_F7, MAKE_BITMASK5( 0, 0, 0, 1, 1, 1, 1), -1, },
137 { IDC_RADIO_BIN, VK_F8, MAKE_BITMASK5( 0, 0, 0, 1, 1, 1, 1), -1, },
138 { IDC_BUTTON_SIGN, VK_F9, MAKE_BITMASK5( 0, 0, 0, 1, 1, 1, 1), CALC_CLR_BLUE, },
139 };
140
141 static const key2code_t key2code_base16[] = {
142 { VK_F2, IDC_RADIO_DWORD, },
143 { VK_F3, IDC_RADIO_WORD, },
144 { VK_F4, IDC_RADIO_BYTE, },
145 { VK_F12, IDC_RADIO_QWORD, },
146 };
147
148 static const key2code_t key2code_base10[] = {
149 { VK_F2, IDC_RADIO_DEG, },
150 { VK_F3, IDC_RADIO_RAD, },
151 { VK_F4, IDC_RADIO_GRAD, },
152 };
153
154 static const WORD operator_codes[] = {
155 /* CONTROL-ID operator */
156 (WORD)IDC_STATIC, // RPN_OPERATOR_PARENT
157 IDC_BUTTON_PERCENT, // RPN_OPERATOR_PERCENT
158 IDC_BUTTON_EQU, // RPN_OPERATOR_EQUAL
159 IDC_BUTTON_OR, // RPN_OPERATOR_OR
160 IDC_BUTTON_XOR, // RPN_OPERATOR_XOR
161 IDC_BUTTON_AND, // RPN_OPERATOR_AND
162 IDC_BUTTON_LSH, // RPN_OPERATOR_LSH
163 IDC_BUTTON_RSH, // RPN_OPERATOR_RSH
164 IDC_BUTTON_ADD, // RPN_OPERATOR_ADD
165 IDC_BUTTON_SUB, // RPN_OPERATOR_SUB
166 IDC_BUTTON_MULT, // RPN_OPERATOR_MULT
167 IDC_BUTTON_DIV, // RPN_OPERATOR_DIV
168 IDC_BUTTON_MOD, // RPN_OPERATOR_MOD
169 IDC_BUTTON_XeY, // RPN_OPERATOR_POW
170 IDC_BUTTON_XrY, // RPN_OPERATOR_SQR
171 };
172
173 typedef void (*rpn_callback1)(calc_number_t *);
174
175 typedef struct {
176 WORD idc;
177 BYTE range;
178 BYTE check_nan;
179 rpn_callback1 direct;
180 rpn_callback1 inverse;
181 rpn_callback1 hyperb;
182 rpn_callback1 inv_hyp;
183 } function_table_t;
184
185 static void run_fe(calc_number_t *number);
186 static void run_dat_sta(calc_number_t *number);
187 static void run_mp(calc_number_t *c);
188 static void run_mm(calc_number_t *c);
189 static void run_ms(calc_number_t *c);
190 static void run_mw(calc_number_t *c);
191 static void run_canc(calc_number_t *c);
192 static void run_rpar(calc_number_t *c);
193 static void run_lpar(calc_number_t *c);
194
195 static const function_table_t function_table[] = {
196 { IDC_BUTTON_SIN, MODIFIER_INV|MODIFIER_HYP, 1, rpn_sin, rpn_asin, rpn_sinh, rpn_asinh },
197 { IDC_BUTTON_COS, MODIFIER_INV|MODIFIER_HYP, 1, rpn_cos, rpn_acos, rpn_cosh, rpn_acosh },
198 { IDC_BUTTON_TAN, MODIFIER_INV|MODIFIER_HYP, 1, rpn_tan, rpn_atan, rpn_tanh, rpn_atanh },
199 { IDC_BUTTON_INT, MODIFIER_INV, 1, rpn_int, rpn_frac, NULL, NULL },
200 { IDC_BUTTON_RX, 0, 1, rpn_reci, NULL, NULL, NULL },
201 { IDC_BUTTON_NOT, 0, 1, rpn_not, NULL, NULL, NULL },
202 { IDC_BUTTON_PI, MODIFIER_INV, 0, rpn_pi, rpn_2pi, NULL, NULL },
203 { IDC_BUTTON_Xe2, MODIFIER_INV, 1, rpn_exp2, rpn_sqrt, NULL, NULL },
204 { IDC_BUTTON_Xe3, MODIFIER_INV, 1, rpn_exp3, rpn_cbrt, NULL, NULL },
205 { IDC_BUTTON_LN, MODIFIER_INV, 1, rpn_ln, rpn_exp, NULL, NULL },
206 { IDC_BUTTON_LOG, MODIFIER_INV, 1, rpn_log, rpn_exp10, NULL, NULL },
207 { IDC_BUTTON_NF, 0, 1, rpn_fact, NULL, NULL, NULL },
208 { IDC_BUTTON_AVE, MODIFIER_INV, 0, rpn_ave, rpn_ave2, NULL, NULL },
209 { IDC_BUTTON_SUM, MODIFIER_INV, 0, rpn_sum, rpn_sum2, NULL, NULL },
210 { IDC_BUTTON_S, MODIFIER_INV, 0, rpn_s_m1, rpn_s, NULL, NULL },
211 { IDC_BUTTON_SQRT, MODIFIER_INV, 1, rpn_sqrt, NULL, NULL, NULL },
212 { IDC_BUTTON_DMS, MODIFIER_INV, 1, rpn_dec2dms, rpn_dms2dec, NULL, NULL },
213 { IDC_BUTTON_FE, 0, 1, run_fe, NULL, NULL, NULL },
214 { IDC_BUTTON_DAT, 0, 1, run_dat_sta, NULL, NULL, NULL, },
215 { IDC_BUTTON_MP, MODIFIER_INV|NO_CHAIN, 1, run_mp, run_mm, NULL, NULL, },
216 { IDC_BUTTON_MS, MODIFIER_INV|NO_CHAIN, 1, run_ms, run_mw, NULL, NULL, },
217 { IDC_BUTTON_CANC, NO_CHAIN, 0, run_canc, NULL, NULL, NULL, },
218 { IDC_BUTTON_RIGHTPAR, NO_CHAIN, 1, run_rpar, NULL, NULL, NULL, },
219 { IDC_BUTTON_LEFTPAR, NO_CHAIN, 0, run_lpar, NULL, NULL, NULL, },
220 };
221
222 /*
223 */
224
225 calc_t calc;
226
227 static void load_config(void)
228 {
229 DWORD tmp;
230 HKEY hKey;
231
232 /* If no settings are found in the registry, then use the default options */
233 calc.layout = CALC_LAYOUT_STANDARD;
234 calc.usesep = FALSE;
235
236 /* Get the configuration based on what version of Windows that's being used */
237 if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Calc"), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
238 {
239 /* Try to load last selected layout */
240 tmp = sizeof(calc.layout);
241 if (RegQueryValueEx(hKey, TEXT("layout"), NULL, NULL, (LPBYTE)&calc.layout, &tmp) != ERROR_SUCCESS)
242 calc.layout = CALC_LAYOUT_STANDARD;
243
244 /* Try to load last selected formatting option */
245 tmp = sizeof(calc.usesep);
246 if (RegQueryValueEx(hKey, TEXT("UseSep"), NULL, NULL, (LPBYTE)&calc.usesep, &tmp) != ERROR_SUCCESS)
247 calc.usesep = FALSE;
248
249 /* close the key */
250 RegCloseKey(hKey);
251 }
252
253 /* memory is empty at startup */
254 calc.is_memory = FALSE;
255
256 /* empty these values */
257 calc.sDecimal[0] = TEXT('\0');
258 calc.sThousand[0] = TEXT('\0');
259
260 /* try to open the registry */
261 if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\International"), 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
262 {
263 /* get these values (ignore errors) */
264 tmp = sizeof(calc.sDecimal);
265 RegQueryValueEx(hKey, TEXT("sDecimal"), NULL, NULL, (LPBYTE)calc.sDecimal, &tmp);
266
267 tmp = sizeof(calc.sThousand);
268 RegQueryValueEx(hKey, TEXT("sThousand"), NULL, NULL, (LPBYTE)calc.sThousand, &tmp);
269
270 /* close the key */
271 RegCloseKey(hKey);
272 }
273 /* if something goes wrong, let's apply the defaults */
274 if (calc.sDecimal[0] == TEXT('\0'))
275 _tcscpy(calc.sDecimal, TEXT("."));
276
277 if (calc.sThousand[0] == TEXT('\0'))
278 _tcscpy(calc.sThousand, TEXT(","));
279
280 /* get the string lengths */
281 calc.sDecimal_len = _tcslen(calc.sDecimal);
282 calc.sThousand_len = _tcslen(calc.sThousand);
283 }
284
285 static void save_config(void)
286 {
287 HKEY hKey;
288 DWORD sepValue;
289
290 if (RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Microsoft\\Calc"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hKey, NULL) != ERROR_SUCCESS)
291 {
292 return;
293 }
294
295 sepValue = (calc.usesep) ? 1 : 0;
296
297 RegSetValueEx(hKey, TEXT("layout"), 0, REG_DWORD, (const BYTE*)&calc.layout, sizeof(calc.layout));
298 RegSetValueEx(hKey, TEXT("UseSep"), 0, REG_DWORD, (const BYTE*)&sepValue, sizeof(sepValue));
299
300 RegCloseKey(hKey);
301 }
302
303 static LRESULT post_key_press(LPARAM lParam, WORD idc)
304 {
305 HWND hCtlWnd = GetDlgItem(calc.hWnd,idc);
306 TCHAR ClassName[64];
307
308 /* check if the key is enabled! */
309 if (!IsWindowEnabled(hCtlWnd))
310 return 1;
311
312 if (!GetClassName(hCtlWnd, ClassName, SIZEOF(ClassName)))
313 return 1;
314
315 if (!_tcscmp(ClassName, TEXT("Button"))) {
316 DWORD dwStyle = GetWindowLongPtr(hCtlWnd, GWL_STYLE) & 0xF;
317
318 /* Set states for press/release, but only for push buttons */
319 if (dwStyle == BS_PUSHBUTTON || dwStyle == BS_DEFPUSHBUTTON || dwStyle == BS_OWNERDRAW) {
320 if (!(lParam & KEY_WAS_DOWN)) {
321 PostMessage(hCtlWnd, BM_SETSTATE, 1, 0);
322 } else
323 if ((lParam & KEY_IS_UP)) {
324 PostMessage(hCtlWnd, BM_SETSTATE, 0, 0);
325 PostMessage(hCtlWnd, BM_CLICK, 0, 0);
326 }
327 return 1;
328 }
329 }
330 /* default action: simple click event at key release */
331 if ((lParam & KEY_IS_UP)) {
332 PostMessage(hCtlWnd, BM_CLICK, 0, 0);
333 }
334 return 1;
335 }
336
337 static int vk2ascii(unsigned int vk)
338 {
339 unsigned short int s;
340 int scan;
341 BYTE state[256];
342 HKL layout=GetKeyboardLayout(0);
343
344 if(!GetKeyboardState(state))
345 return 0;
346
347 scan=MapVirtualKeyEx(vk, 0, layout);
348 s = 0;
349 if (ToAsciiEx(vk, scan, state, &s, 0, layout)>0) {
350 /* convert to upper case */
351 if (s >= 'a' && s <= 'z')
352 s = s - 'a' + 'A';
353 /* add check to CTRL key */
354 if (vk >= 'A' && vk <= 'Z' &&
355 s >= CTRL_A && s <= CTRL_Z)
356 s |= CTRL_FLAG;
357 else
358 if (GetAsyncKeyState(VK_MENU) < 0)
359 s |= ALT_FLAG;
360 return s;
361 }
362 return 0;
363 }
364
365 static int process_vk_key(WPARAM wParam, LPARAM lParam)
366 {
367 const key2code_t *k;
368 unsigned int x;
369 unsigned short int ch;
370
371 ch = vk2ascii(LOWORD(wParam));
372 if ((lParam & KEY_IS_UP)) {
373 /* Test for "copy" to clipboard */
374 if (ch == (CTRL_C|CTRL_FLAG)) {
375 SendMessage(calc.hWnd, WM_COMMAND, IDM_EDIT_COPY, 0);
376 return 1;
377 }
378 /* Test for "paste" from clipboard */
379 if (ch == (CTRL_V|CTRL_FLAG)) {
380 SendMessage(calc.hWnd, WM_COMMAND, IDM_EDIT_PASTE, 0);
381 return 1;
382 }
383 /* Test of help menu */
384 if (LOWORD(wParam) == VK_F1) {
385 SendMessage(calc.hWnd, WM_COMMAND, IDM_HELP_HELP, 0);
386 return 1;
387 }
388 }
389
390 for (x=0; x<SIZEOF(key2code); x++) {
391 int key = key2code[x].key;
392 if (key2code[x].mask & BITMASK_IS_CTRL)
393 key |= CTRL_FLAG;
394 if ((key == ch && (key2code[x].mask & BITMASK_IS_ASCII)) ||
395 (key == LOWORD(wParam) && !(key2code[x].mask & BITMASK_IS_ASCII))
396 ) {
397 if (GetDlgItem(calc.hWnd, key2code[x].idc) == NULL)
398 continue;
399 return post_key_press(lParam, key2code[x].idc);
400 }
401 }
402 if (calc.layout == CALC_LAYOUT_SCIENTIFIC) {
403 if (calc.base == IDC_RADIO_DEC) {
404 k = key2code_base10;
405 x = SIZEOF(key2code_base10);
406 } else {
407 k = key2code_base16;
408 x = SIZEOF(key2code_base16);
409 }
410 do {
411 if (k->key == LOWORD(wParam)) {
412 return post_key_press(lParam, k->idc);
413 }
414 k++;
415 } while (--x);
416 }
417 return 0;
418 }
419
420 #ifdef USE_KEYBOARD_HOOK
421 static LRESULT CALLBACK
422 KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
423 {
424 if(nCode<0 || calc.is_menu_on)
425 return CallNextHookEx(calc.hKeyboardHook,nCode,wParam,lParam);
426
427 if(nCode==HC_ACTION)
428 if (process_vk_key(wParam, lParam))
429 return;
430
431 return CallNextHookEx(calc.hKeyboardHook,nCode,wParam,lParam);
432 }
433 #endif
434
435 static void update_lcd_display(HWND hwnd)
436 {
437 /*
438 * multiply size of calc.buffer by 2 because it may
439 * happen that separator is used between each digit.
440 * Also added little additional space for dot and '\0'.
441 */
442 TCHAR *tmp = (TCHAR *)alloca(sizeof(calc.buffer)*2+2*sizeof(TCHAR));
443
444 if (calc.buffer[0] == TEXT('\0'))
445 _tcscpy(tmp, TEXT("0"));
446 else
447 _tcscpy(tmp, calc.buffer);
448 /* add final '.' in decimal mode (if it's missing) */
449 if (calc.base == IDC_RADIO_DEC) {
450 if (_tcschr(tmp, TEXT('.')) == NULL)
451 _tcscat(tmp, TEXT("."));
452 }
453 /* if separator mode is on, let's add an additional space */
454 if (calc.usesep && !calc.sci_in && !calc.sci_out && !calc.is_nan) {
455 /* go to the integer part of the string */
456 TCHAR *p = _tcschr(tmp, TEXT('.'));
457 TCHAR *e = _tcschr(tmp, TEXT('\0'));
458 int n=0, t;
459
460 if (p == NULL) p = e;
461 switch (calc.base) {
462 case IDC_RADIO_HEX:
463 case IDC_RADIO_BIN:
464 t = 4;
465 break;
466 default:
467 /* fall here for:
468 IDC_RADIO_DEC:
469 IDC_RADIO_OCT: */
470 t = 3;
471 break;
472 }
473 while (--p > tmp) {
474 if (++n == t && *(p-1) != TEXT('-')) {
475 memmove(p+1, p, (e-p+1)*sizeof(TCHAR));
476 e++;
477 *p = TEXT(' ');
478 n = 0;
479 }
480 }
481 /* if decimal mode, apply regional settings */
482 if (calc.base == IDC_RADIO_DEC) {
483 TCHAR *p = tmp;
484 TCHAR *e = _tcschr(tmp, TEXT('.'));
485
486 /* searching for thousands default separator */
487 while (p < e) {
488 if (*p == TEXT(' ')) {
489 memmove(p+calc.sThousand_len, p+1, _tcslen(p)*sizeof(TCHAR));
490 memcpy(p, calc.sThousand, calc.sThousand_len*sizeof(TCHAR));
491 p += calc.sThousand_len;
492 } else
493 p++;
494 }
495 /* update decimal point too. */
496 memmove(p+calc.sDecimal_len, p+1, _tcslen(p)*sizeof(TCHAR));
497 memcpy(p, calc.sDecimal, calc.sDecimal_len*sizeof(TCHAR));
498 }
499 } else {
500 TCHAR *p = _tcschr(tmp, TEXT('.'));
501
502 /* update decimal point when usesep is false */
503 if (p != NULL) {
504 memmove(p+calc.sDecimal_len, p+1, _tcslen(p)*sizeof(TCHAR));
505 memcpy(p, calc.sDecimal, calc.sDecimal_len*sizeof(TCHAR));
506 }
507 }
508 SendDlgItemMessage(hwnd, IDC_TEXT_OUTPUT, WM_SETTEXT, (WPARAM)0, (LPARAM)tmp);
509 }
510
511 static void update_parent_display(HWND hWnd)
512 {
513 TCHAR str[8];
514 int n = eval_parent_count();
515
516 if (!n)
517 str[0] = TEXT('\0');
518 else
519 _stprintf(str,TEXT("(=%d"), n);
520 SendDlgItemMessage(hWnd, IDC_TEXT_PARENT, WM_SETTEXT, 0, (LPARAM)str);
521 }
522
523 static void build_operand(HWND hwnd, DWORD idc)
524 {
525 unsigned int i = 0, n;
526
527 if (idc == IDC_BUTTON_DOT) {
528 /* if dot is the first char, it's added automatically */
529 if (calc.buffer == calc.ptr) {
530 *calc.ptr++ = TEXT('0');
531 *calc.ptr++ = TEXT('.');
532 *calc.ptr = TEXT('\0');
533 update_lcd_display(hwnd);
534 return;
535 }
536 /* if pressed dot and it's already in the string, then return */
537 if (_tcschr(calc.buffer, TEXT('.')) != NULL)
538 return;
539 }
540 if (idc != IDC_STATIC) {
541 while (idc != key2code[i].idc) i++;
542 }
543 n = calc.ptr - calc.buffer;
544 if (idc == IDC_BUTTON_0 && n == 0) {
545 /* no need to put the dot because it's handled by update_lcd_display() */
546 calc.buffer[0] = TEXT('0');
547 calc.buffer[1] = TEXT('\0');
548 update_lcd_display(hwnd);
549 return;
550 }
551 switch (calc.base) {
552 case IDC_RADIO_HEX:
553 if (n >= 16)
554 return;
555 break;
556 case IDC_RADIO_DEC:
557 if (n >= SIZEOF(calc.buffer)-1)
558 return;
559 if (calc.sci_in) {
560 if (idc != IDC_STATIC)
561 calc.esp = (calc.esp * 10 + (key2code[i].key-'0')) % LOCAL_EXP_SIZE;
562 if (calc.ptr == calc.buffer)
563 _stprintf(calc.ptr, TEXT("0.e%+d"), calc.esp);
564 else {
565 /* adds the dot at the end if the number has no decimal part */
566 if (!_tcschr(calc.buffer, TEXT('.')))
567 *calc.ptr++ = TEXT('.');
568 _stprintf(calc.ptr, TEXT("e%+d"), calc.esp);
569 }
570 update_lcd_display(hwnd);
571 return;
572 }
573 break;
574 case IDC_RADIO_OCT:
575 if (n >= 22)
576 return;
577 break;
578 case IDC_RADIO_BIN:
579 if (n >= 64)
580 return;
581 break;
582 }
583 calc.ptr += _stprintf(calc.ptr, TEXT("%C"), key2code[i].key);
584 update_lcd_display(hwnd);
585 }
586
587 static void prepare_rpn_result(calc_number_t *rpn, TCHAR *buffer, int size, int base)
588 {
589 if (calc.is_nan) {
590 rpn_zero(&calc.code);
591 LoadString(calc.hInstance, IDS_MATH_ERROR, buffer, size);
592 return;
593 }
594 prepare_rpn_result_2(rpn, buffer, size, base);
595 }
596
597 static void display_rpn_result(HWND hwnd, calc_number_t *rpn)
598 {
599 calc.sci_in = FALSE;
600 prepare_rpn_result(rpn, calc.buffer, SIZEOF(calc.buffer), calc.base);
601 calc.ptr = calc.buffer + _tcslen(calc.buffer);
602 update_lcd_display(hwnd);
603 calc.ptr = calc.buffer;
604 update_parent_display(hwnd);
605 }
606
607 static int get_modifiers(HWND hwnd)
608 {
609 int modifiers = 0;
610
611 if (SendDlgItemMessage(hwnd, IDC_CHECK_INV, BM_GETCHECK, 0, 0))
612 modifiers |= MODIFIER_INV;
613 if (SendDlgItemMessage(hwnd, IDC_CHECK_HYP, BM_GETCHECK, 0, 0))
614 modifiers |= MODIFIER_HYP;
615
616 return modifiers;
617 }
618
619 static void convert_text2number(calc_number_t *a)
620 {
621 /* if the screen output buffer is empty, then */
622 /* the operand is taken from the last input */
623 if (calc.buffer == calc.ptr) {
624 /* if pushed valued is ZERO then we should grab it */
625 if (!_tcscmp(calc.buffer, TEXT("0.")) ||
626 !_tcscmp(calc.buffer, TEXT("0")))
627 /* this zero is good for both integer and decimal */
628 rpn_zero(a);
629 else
630 rpn_copy(a, &calc.code);
631 return;
632 }
633 /* ZERO is the default value for all numeric bases */
634 rpn_zero(a);
635 convert_text2number_2(a);
636 }
637
638 static const struct _update_check_menus {
639 DWORD *sel;
640 WORD idm;
641 WORD idc;
642 } upd[] = {
643 { &calc.layout, IDM_VIEW_STANDARD, CALC_LAYOUT_STANDARD },
644 { &calc.layout, IDM_VIEW_SCIENTIFIC, CALC_LAYOUT_SCIENTIFIC },
645 { &calc.layout, IDM_VIEW_CONVERSION, CALC_LAYOUT_CONVERSION },
646 /*-----------------------------------------*/
647 { &calc.base, IDM_VIEW_HEX, IDC_RADIO_HEX, },
648 { &calc.base, IDM_VIEW_DEC, IDC_RADIO_DEC, },
649 { &calc.base, IDM_VIEW_OCT, IDC_RADIO_OCT, },
650 { &calc.base, IDM_VIEW_BIN, IDC_RADIO_BIN, },
651 /*-----------------------------------------*/
652 { &calc.degr, IDM_VIEW_DEG, IDC_RADIO_DEG, },
653 { &calc.degr, IDM_VIEW_RAD, IDC_RADIO_RAD, },
654 { &calc.degr, IDM_VIEW_GRAD, IDC_RADIO_GRAD, },
655 /*-----------------------------------------*/
656 { &calc.size, IDM_VIEW_QWORD, IDC_RADIO_QWORD, },
657 { &calc.size, IDM_VIEW_DWORD, IDC_RADIO_DWORD, },
658 { &calc.size, IDM_VIEW_WORD, IDC_RADIO_WORD, },
659 { &calc.size, IDM_VIEW_BYTE, IDC_RADIO_BYTE, },
660 };
661
662 static void update_menu(HWND hwnd)
663 {
664 HMENU hMenu = GetSubMenu(GetMenu(hwnd), 1);
665 unsigned int x;
666
667 /* Sets the state of the layout in the menu based on the configuration file */
668 if (calc.layout == CALC_LAYOUT_SCIENTIFIC)
669 {
670 CheckMenuRadioItem(GetMenu(hwnd),
671 IDM_VIEW_STANDARD,
672 IDM_VIEW_CONVERSION,
673 IDM_VIEW_SCIENTIFIC,
674 MF_BYCOMMAND);
675 }
676 else if (calc.layout == CALC_LAYOUT_CONVERSION)
677 {
678 CheckMenuRadioItem(GetMenu(hwnd),
679 IDM_VIEW_STANDARD,
680 IDM_VIEW_CONVERSION,
681 IDM_VIEW_CONVERSION,
682 MF_BYCOMMAND);
683 }
684 else
685 {
686 CheckMenuRadioItem(GetMenu(hwnd),
687 IDM_VIEW_STANDARD,
688 IDM_VIEW_CONVERSION,
689 IDM_VIEW_STANDARD,
690 MF_BYCOMMAND);
691 }
692
693 for (x=3; x<SIZEOF(upd); x++) {
694 if (*(upd[x].sel) != upd[x].idc) {
695 CheckMenuItem(hMenu, upd[x].idm, MF_BYCOMMAND|MF_UNCHECKED);
696 SendMessage((HWND)GetDlgItem(hwnd,upd[x].idc),BM_SETCHECK,FALSE,0L);
697 } else {
698 CheckMenuItem(hMenu, upd[x].idm, MF_BYCOMMAND|MF_CHECKED);
699 SendMessage((HWND)GetDlgItem(hwnd,upd[x].idc),BM_SETCHECK,TRUE,0L);
700 }
701 }
702 CheckMenuItem(hMenu, IDM_VIEW_GROUP, MF_BYCOMMAND|(calc.usesep ? MF_CHECKED : MF_UNCHECKED));
703 }
704
705 typedef struct {
706 WORD idc;
707 WORD mask;
708 } radio_config_t;
709
710 static const radio_config_t radio_setup[] = {
711 /* CONTROL-ID hex dec oct bin */
712 { IDC_RADIO_QWORD, MAKE_BITMASK4( 1, 0, 1, 1) },
713 { IDC_RADIO_DWORD, MAKE_BITMASK4( 1, 0, 1, 1) },
714 { IDC_RADIO_WORD, MAKE_BITMASK4( 1, 0, 1, 1) },
715 { IDC_RADIO_BYTE, MAKE_BITMASK4( 1, 0, 1, 1) },
716 { IDC_RADIO_DEG, MAKE_BITMASK4( 0, 1, 0, 0) },
717 { IDC_RADIO_RAD, MAKE_BITMASK4( 0, 1, 0, 0) },
718 { IDC_RADIO_GRAD, MAKE_BITMASK4( 0, 1, 0, 0) },
719 };
720
721 static void enable_allowed_controls(HWND hwnd, DWORD base)
722 {
723 BYTE mask;
724 int n;
725
726 switch (base) {
727 case IDC_RADIO_DEC:
728 mask = BITMASK_DEC_MASK;
729 break;
730 case IDC_RADIO_HEX:
731 mask = BITMASK_HEX_MASK;
732 break;
733 case IDC_RADIO_OCT:
734 mask = BITMASK_OCT_MASK;
735 break;
736 case IDC_RADIO_BIN:
737 mask = BITMASK_BIN_MASK;
738 break;
739 default:
740 return;
741 }
742 for (n=0; n<SIZEOF(key2code); n++) {
743 if (key2code[n].mask != 0) {
744 HWND hCtlWnd = GetDlgItem(hwnd, key2code[n].idc);
745 BOOL current;
746
747 if ((key2code[n].mask & BITMASK_IS_STATS))
748 current = IsWindow(calc.hStatWnd) ? TRUE : FALSE;
749 else
750 current = (key2code[n].mask & mask) ? TRUE : FALSE;
751 if (IsWindowEnabled(hCtlWnd) != current)
752 EnableWindow(hCtlWnd, current);
753 }
754 }
755 }
756
757 static void update_radio(HWND hwnd, unsigned int base)
758 {
759 HMENU hMenu;
760 LPCTSTR lpMenuId;
761 WORD mask;
762 int n;
763
764 switch (base) {
765 case IDC_RADIO_DEC:
766 lpMenuId = MAKEINTRESOURCE(IDR_MENU_SCIENTIFIC_1);
767 mask = BITMASK_DEC_MASK;
768 break;
769 case IDC_RADIO_HEX:
770 lpMenuId = MAKEINTRESOURCE(IDR_MENU_SCIENTIFIC_2);
771 mask = BITMASK_HEX_MASK;
772 break;
773 case IDC_RADIO_OCT:
774 lpMenuId = MAKEINTRESOURCE(IDR_MENU_SCIENTIFIC_2);
775 mask = BITMASK_OCT_MASK;
776 break;
777 case IDC_RADIO_BIN:
778 lpMenuId = MAKEINTRESOURCE(IDR_MENU_SCIENTIFIC_2);
779 mask = BITMASK_BIN_MASK;
780 break;
781 default:
782 return;
783 }
784
785 if (calc.base != base) {
786 convert_text2number(&calc.code);
787 convert_real_integer(base);
788 calc.base = base;
789 display_rpn_result(hwnd, &calc.code);
790
791 hMenu = GetMenu(hwnd);
792 DestroyMenu(hMenu);
793 hMenu = LoadMenu(calc.hInstance, lpMenuId);
794 SetMenu(hwnd, hMenu);
795 update_menu(hwnd);
796
797 for (n=0; n<SIZEOF(radio_setup); n++)
798 ShowWindow(GetDlgItem(hwnd, radio_setup[n].idc), (radio_setup[n].mask & mask) ? SW_SHOW : SW_HIDE);
799
800 enable_allowed_controls(hwnd, base);
801 }
802
803 SendDlgItemMessage(hwnd, calc.base, BM_SETCHECK, BST_CHECKED, 0);
804 if (base == IDC_RADIO_DEC)
805 SendDlgItemMessage(hwnd, calc.degr, BM_SETCHECK, BST_CHECKED, 0);
806 else
807 SendDlgItemMessage(hwnd, calc.size, BM_SETCHECK, BST_CHECKED, 0);
808 }
809
810 static void update_memory_flag(HWND hWnd, BOOL mem_flag)
811 {
812 calc.is_memory = mem_flag;
813 SendDlgItemMessage(hWnd, IDC_TEXT_MEMORY, WM_SETTEXT, 0, (LPARAM)(mem_flag ? TEXT("M") : TEXT("")));
814 }
815
816 static void update_n_stats_items(HWND hWnd, TCHAR *buffer)
817 {
818 unsigned int n = SendDlgItemMessage(hWnd, IDC_LIST_STAT, LB_GETCOUNT, 0, 0);
819
820 _stprintf(buffer, TEXT("n=%d"), n);
821 SendDlgItemMessage(hWnd, IDC_TEXT_NITEMS, WM_SETTEXT, 0, (LPARAM)buffer);
822 }
823
824 static void clean_stat_list(void)
825 {
826 statistic_t *p = calc.stat;
827
828 while (p != NULL) {
829 statistic_t *s = p;
830 p = (statistic_t *)(p->next);
831 rpn_free(&s->num);
832 free(s);
833 }
834 calc.stat = p;
835 }
836
837 static void delete_stat_item(int n)
838 {
839 statistic_t *p = calc.stat;
840 statistic_t *s;
841
842 if (n == 0) {
843 calc.stat = (statistic_t *)p->next;
844 rpn_free(&p->num);
845 free(p);
846 } else {
847 s = (statistic_t *)p->next;
848 while (--n) {
849 p = s;
850 s = (statistic_t *)p->next;
851 }
852 p->next = s->next;
853 rpn_free(&s->num);
854 free(s);
855 }
856 }
857
858 static char *ReadConversion(const char *formula)
859 {
860 int len = strlen(formula);
861 char *str = (char *)malloc(len+3);
862
863 if (str == NULL)
864 return NULL;
865
866 str[0] = '(';
867 memcpy(str+1, formula, len);
868 str[len+1] = ')';
869 str[len+2] = '\0';
870
871 _tcscpy(calc.source, (*calc.buffer == _T('\0')) ? _T("0") : calc.buffer);
872
873 /* clear display content before proceeding */
874 calc.ptr = calc.buffer;
875 calc.buffer[0] = TEXT('\0');
876
877 return str;
878 }
879
880 static INT_PTR CALLBACK DlgStatProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
881 {
882 TCHAR buffer[SIZEOF(calc.buffer)];
883 DWORD n;
884
885 switch (msg) {
886 case WM_INITDIALOG:
887 return TRUE;
888 case WM_COMMAND:
889 switch (LOWORD(wp)) {
890 case IDC_LIST_STAT:
891 if (HIWORD(wp) == CBN_DBLCLK)
892 SendMessage(hWnd, WM_COMMAND, (WPARAM)IDC_BUTTON_LOAD, 0);
893 return TRUE;
894 case IDC_BUTTON_RET:
895 SetFocus(GetDlgItem(GetParent(hWnd), IDC_BUTTON_FOCUS));
896 return TRUE;
897 case IDC_BUTTON_LOAD:
898 n = SendDlgItemMessage(hWnd, IDC_LIST_STAT, LB_GETCURSEL, 0, 0);
899 if (n == (DWORD)-1)
900 return TRUE;
901 PostMessage(GetParent(hWnd), WM_LOAD_STAT, (WPARAM)n, 0);
902 return TRUE;
903 case IDC_BUTTON_CD:
904 n = SendDlgItemMessage(hWnd, IDC_LIST_STAT, LB_GETCURSEL, 0, 0);
905 if (n == (DWORD)-1)
906 return TRUE;
907 SendDlgItemMessage(hWnd, IDC_LIST_STAT, LB_DELETESTRING, (WPARAM)n, 0);
908 update_n_stats_items(hWnd, buffer);
909 delete_stat_item(n);
910 return TRUE;
911 case IDC_BUTTON_CAD:
912 SendDlgItemMessage(hWnd, IDC_LIST_STAT, LB_RESETCONTENT, 0, 0);
913 clean_stat_list();
914 update_n_stats_items(hWnd, buffer);
915 return TRUE;
916 }
917 break;
918 case WM_CLOSE:
919 clean_stat_list();
920 DestroyWindow(hWnd);
921 return TRUE;
922 case WM_DESTROY:
923 PostMessage(GetParent(hWnd), WM_CLOSE_STATS, 0, 0);
924 return TRUE;
925 case WM_INSERT_STAT:
926 prepare_rpn_result(&(((statistic_t *)lp)->num),
927 buffer, SIZEOF(buffer),
928 ((statistic_t *)lp)->base);
929 SendDlgItemMessage(hWnd, IDC_LIST_STAT, LB_ADDSTRING, 0, (LPARAM)buffer);
930 update_n_stats_items(hWnd, buffer);
931 return TRUE;
932 }
933 return FALSE;
934 }
935
936 static BOOL idm_2_idc(int idm, WPARAM *pIdc)
937 {
938 int x;
939
940 for (x=0; x<SIZEOF(upd); x++) {
941 if (upd[x].idm == idm)
942 {
943 *pIdc = (WPARAM)(upd[x].idc);
944 return TRUE;
945 }
946 }
947 return FALSE;
948 }
949
950 static void CopyMemToClipboard(void *ptr)
951 {
952 if(OpenClipboard(NULL)) {
953 HGLOBAL clipbuffer;
954 TCHAR *buffer;
955
956 EmptyClipboard();
957 clipbuffer = GlobalAlloc(GMEM_DDESHARE, (_tcslen(ptr)+1)*sizeof(TCHAR));
958 buffer = (TCHAR *)GlobalLock(clipbuffer);
959 _tcscpy(buffer, ptr);
960 GlobalUnlock(clipbuffer);
961 #ifdef UNICODE
962 SetClipboardData(CF_UNICODETEXT,clipbuffer);
963 #else
964 SetClipboardData(CF_TEXT,clipbuffer);
965 #endif
966 CloseClipboard();
967 }
968 }
969
970 static void handle_copy_command(HWND hWnd)
971 {
972 TCHAR display[sizeof(calc.buffer)];
973
974 SendDlgItemMessage(hWnd, IDC_TEXT_OUTPUT, WM_GETTEXT, (WPARAM)SIZEOF(display), (LPARAM)display);
975 if (calc.base == IDC_RADIO_DEC && _tcschr(calc.buffer, _T('.')) == NULL)
976 display[_tcslen(display)-calc.sDecimal_len] = TEXT('\0');
977 CopyMemToClipboard(display);
978 }
979
980 static char *ReadClipboard(void)
981 {
982 char *buffer = NULL;
983
984 if (OpenClipboard(NULL)) {
985 HANDLE hData = GetClipboardData(CF_TEXT);
986 char *fromClipboard;
987
988 if (hData != NULL) {
989 fromClipboard = (char *)GlobalLock(hData);
990 if (strlen(fromClipboard))
991 buffer = _strupr(_strdup(fromClipboard));
992 GlobalUnlock( hData );
993 }
994 CloseClipboard();
995 }
996 return buffer;
997 }
998
999 static char *handle_sequence_input(HWND hwnd, sequence_t *seq)
1000 {
1001 char *ptr = seq->ptr;
1002 int ch, x;
1003
1004 ch = *ptr++;
1005 if (ch == '\\')
1006 PostMessage(hwnd, WM_COMMAND, (WPARAM)IDC_BUTTON_DAT, 0);
1007 else
1008 if (ch == ':') {
1009 ch = *ptr;
1010 if (ch != '\0')
1011 ptr++;
1012 switch (ch) {
1013 case 'C': PostMessage(hwnd, WM_COMMAND, (WPARAM)IDC_BUTTON_MC, 0); break;
1014 case 'E': PostMessage(hwnd, WM_COMMAND, (WPARAM)IDC_BUTTON_EXP,0); break;
1015 case 'M': PostMessage(hwnd, WM_COMMAND, (WPARAM)IDC_BUTTON_MS, 0); break;
1016 case 'P': PostMessage(hwnd, WM_COMMAND, (WPARAM)IDC_BUTTON_MP, 0); break;
1017 case 'Q': PostMessage(hwnd, WM_COMMAND, (WPARAM)IDC_BUTTON_CANC, 0); break;
1018 case 'R': PostMessage(hwnd, WM_COMMAND, (WPARAM)IDC_BUTTON_MR, 0); break;
1019 }
1020 } else
1021 if (ch == '$') {
1022 calc.ptr =
1023 _tcscpy(calc.buffer, calc.source) +
1024 _tcslen(calc.source);
1025 } else {
1026 for (x=0; x<SIZEOF(key2code); x++) {
1027 if (!(key2code[x].mask & BITMASK_IS_ASCII) ||
1028 (key2code[x].mask & BITMASK_IS_CTRL))
1029 continue;
1030 if (key2code[x].key == ch) {
1031 PostMessage(hwnd, WM_COMMAND, (WPARAM)key2code[x].idc, 0);
1032 break;
1033 }
1034 }
1035 }
1036 seq->ptr = ptr;
1037 if (*ptr != '\0')
1038 PostMessage(hwnd, seq->wm_msg, 0, 0);
1039 else {
1040 free(seq->data);
1041 seq->data = seq->ptr = ptr = NULL;
1042 }
1043 return ptr;
1044 }
1045
1046 static void run_dat_sta(calc_number_t *a)
1047 {
1048 statistic_t *s = (statistic_t *)malloc(sizeof(statistic_t));
1049 statistic_t *p = calc.stat;
1050
1051 rpn_alloc(&s->num);
1052 rpn_copy(&s->num, a);
1053 s->base = calc.base;
1054 s->next = NULL;
1055 if (p == NULL)
1056 calc.stat = s;
1057 else {
1058 while (p->next != NULL)
1059 p = (statistic_t *)(p->next);
1060 p->next = s;
1061 }
1062 PostMessage(calc.hStatWnd, WM_INSERT_STAT, 0, (LPARAM)s);
1063 }
1064
1065 static void run_mp(calc_number_t *c)
1066 {
1067 calc_node_t cn;
1068
1069 cn.number = *c;
1070 cn.base = calc.base;
1071 run_operator(&calc.memory, &calc.memory, &cn, RPN_OPERATOR_ADD);
1072 update_memory_flag(calc.hWnd, TRUE);
1073 }
1074
1075 static void run_mm(calc_number_t *c)
1076 {
1077 calc_node_t cn;
1078
1079 cn.number = *c;
1080 cn.base = calc.base;
1081 run_operator(&calc.memory, &calc.memory, &cn, RPN_OPERATOR_SUB);
1082 update_memory_flag(calc.hWnd, TRUE);
1083 }
1084
1085 static void run_ms(calc_number_t *c)
1086 {
1087 rpn_copy(&calc.memory.number, c);
1088 calc.memory.base = calc.base;
1089 update_memory_flag(calc.hWnd, rpn_is_zero(&calc.memory.number) ? FALSE : TRUE);
1090 }
1091
1092 static void run_mw(calc_number_t *c)
1093 {
1094 calc_number_t tmp;
1095
1096 rpn_copy(&tmp, &calc.memory.number);
1097 rpn_copy(&calc.memory.number, c);
1098 calc.memory.base = calc.base;
1099 if (calc.is_memory)
1100 rpn_copy(c, &tmp);
1101 update_memory_flag(calc.hWnd, rpn_is_zero(&calc.memory.number) ? FALSE : TRUE);
1102 }
1103
1104 static statistic_t *upload_stat_number(int n)
1105 {
1106 statistic_t *p = calc.stat;
1107
1108 if (p == NULL)
1109 return p;
1110
1111 while (n--) {
1112 p = (statistic_t *)(p->next);
1113 if (p == NULL)
1114 return p;
1115 }
1116
1117 #ifndef ENABLE_MULTI_PRECISION
1118 if (calc.base != p->base) {
1119 if (calc.base == IDC_RADIO_DEC)
1120 calc.code.f = (double)p->num.i;
1121 else {
1122 calc.code.i = (__int64)p->num.f;
1123 apply_int_mask(&calc.code);
1124 }
1125 } else
1126 #endif
1127 rpn_copy(&calc.code, &p->num);
1128
1129 calc.is_nan = FALSE;
1130
1131 return p;
1132 }
1133
1134 static void run_fe(calc_number_t *number)
1135 {
1136 calc.sci_out = ((calc.sci_out != FALSE) ? FALSE : TRUE);
1137 }
1138
1139 static void handle_context_menu(HWND hWnd, WPARAM wp, LPARAM lp)
1140 {
1141 TCHAR text[64];
1142 HMENU hMenu = CreatePopupMenu();
1143 BOOL idm;
1144
1145 LoadString(calc.hInstance, IDS_QUICKHELP, text, SIZEOF(text));
1146 AppendMenu(hMenu, MF_STRING | MF_ENABLED, IDM_HELP_HELP, text);
1147 idm = TrackPopupMenu( hMenu,
1148 TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD | TPM_RIGHTBUTTON,
1149 LOWORD(lp),
1150 HIWORD(lp),
1151 0,
1152 hWnd,
1153 NULL);
1154 DestroyMenu(hMenu);
1155 #ifndef DISABLE_HTMLHELP_SUPPORT
1156 if (idm) {
1157 HH_POPUP popup;
1158
1159 memset(&popup, 0, sizeof(popup));
1160 popup.cbStruct = sizeof(HH_POPUP);
1161 popup.clrForeground = 1;
1162 popup.clrBackground = -1;
1163 popup.pt.x = LOWORD(lp);
1164 popup.pt.y = HIWORD(lp);
1165 popup.rcMargins.top = -1;
1166 popup.rcMargins.bottom = -1;
1167 popup.rcMargins.left = -1;
1168 popup.rcMargins.right = -1;
1169 popup.idString = GetWindowLongPtr((HWND)wp, GWL_ID);
1170 HtmlHelp((HWND)wp, HTMLHELP_PATH("/popups.txt"), HH_DISPLAY_TEXT_POPUP, (DWORD_PTR)&popup);
1171 }
1172 #else
1173 (void)idm;
1174 #endif
1175 }
1176
1177 static void run_canc(calc_number_t *c)
1178 {
1179 flush_postfix();
1180 rpn_zero(c);
1181 /* clear also scientific display modes */
1182 calc.sci_out = FALSE;
1183 calc.sci_in = FALSE;
1184 /* clear state of inv and hyp flags */
1185 SendDlgItemMessage(calc.hWnd, IDC_CHECK_INV, BM_SETCHECK, 0, 0);
1186 SendDlgItemMessage(calc.hWnd, IDC_CHECK_HYP, BM_SETCHECK, 0, 0);
1187 }
1188
1189 static void run_rpar(calc_number_t *c)
1190 {
1191 exec_closeparent(c);
1192 }
1193
1194 static void run_lpar(calc_number_t *c)
1195 {
1196 exec_infix2postfix(c, RPN_OPERATOR_PARENT);
1197 }
1198
1199 static LRESULT CALLBACK SubclassButtonProc(HWND hWnd, WPARAM wp, LPARAM lp)
1200 {
1201 LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT)lp;
1202 DWORD dwStyle;
1203 UINT dwText;
1204 TCHAR text[64];
1205 int dx, dy, len;
1206 SIZE size;
1207 POINT pt;
1208
1209 if(dis->CtlType == ODT_BUTTON) {
1210 /*
1211 * little exception: 1/x has different color
1212 * in standard and scientific modes
1213 */
1214 if ((calc.layout == CALC_LAYOUT_STANDARD ||
1215 calc.layout == CALC_LAYOUT_CONVERSION) &&
1216 IDC_BUTTON_RX == dis->CtlID) {
1217 SetTextColor(dis->hDC, CALC_CLR_BLUE);
1218 } else
1219 for (dx=0; dx<SIZEOF(key2code); dx++) {
1220 if (key2code[dx].idc == dis->CtlID) {
1221 SetTextColor(dis->hDC, key2code[dx].col);
1222 break;
1223 }
1224 }
1225 /* button text to write */
1226 len = GetWindowText(dis->hwndItem, text, SIZEOF(text));
1227 /* default state: unpushed & enabled */
1228 dwStyle = 0;
1229 dwText = 0;
1230 if ((dis->itemState & ODS_DISABLED))
1231 dwText = DSS_DISABLED;
1232 if ((dis->itemState & ODS_SELECTED))
1233 dwStyle = DFCS_PUSHED;
1234
1235 DrawFrameControl(dis->hDC, &dis->rcItem, DFC_BUTTON, DFCS_BUTTONPUSH | dwStyle);
1236 GetTextExtentPoint32(dis->hDC, text, len, &size);
1237 dx = ((dis->rcItem.right-dis->rcItem.left) - size.cx) >> 1;
1238 dy = ((dis->rcItem.bottom-dis->rcItem.top) - size.cy) >> 1;
1239 if ((dwStyle & DFCS_PUSHED)) {
1240 dx++;
1241 dy++;
1242 }
1243 pt.x = dis->rcItem.left + dx;
1244 pt.y = dis->rcItem.top + dy;
1245 DrawState(dis->hDC, NULL, NULL, (LPARAM)text, 0, pt.x, pt.y, size.cx, size.cy, DST_TEXT | dwText);
1246 }
1247 return 1L;
1248 }
1249
1250 static INT_PTR CALLBACK DlgMainProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
1251 {
1252 unsigned int x;
1253 RECT rc;
1254 HMENU hMenu;
1255
1256 switch (msg) {
1257 case WM_DRAWITEM:
1258 return SubclassButtonProc(hWnd, wp, lp);
1259
1260 case WM_INITDIALOG:
1261 // For now, the Help dialog is disabled because of lacking of HTML Help support
1262 EnableMenuItem(GetMenu(hWnd), IDM_HELP_HELP, MF_BYCOMMAND | MF_GRAYED);
1263 calc.hWnd=hWnd;
1264
1265 #ifdef USE_KEYBOARD_HOOK
1266 calc.hKeyboardHook=SetWindowsHookEx(
1267 WH_KEYBOARD,
1268 KeyboardHookProc,
1269 NULL,
1270 GetCurrentThreadId()
1271 );
1272 #endif
1273 rpn_zero(&calc.code);
1274 calc.sci_out = FALSE;
1275 calc.base = IDC_RADIO_DEC;
1276 calc.size = IDC_RADIO_QWORD;
1277 calc.degr = IDC_RADIO_DEG;
1278 calc.ptr = calc.buffer;
1279 calc.is_nan = FALSE;
1280 enable_allowed_controls(hWnd, IDC_RADIO_DEC);
1281 update_radio(hWnd, IDC_RADIO_DEC);
1282 update_menu(hWnd);
1283 display_rpn_result(hWnd, &calc.code);
1284 update_memory_flag(hWnd, calc.is_memory);
1285 /* remove keyboard focus */
1286 SetFocus(GetDlgItem(hWnd, IDC_BUTTON_FOCUS));
1287 /* set our calc icon */
1288 SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(calc.hInstance, MAKEINTRESOURCE(IDI_CALC_BIG)));
1289 SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(calc.hInstance, MAKEINTRESOURCE(IDI_CALC_SMALL)));
1290
1291 /* Sets the state of the option to group digits */
1292 hMenu = GetSubMenu(GetMenu(hWnd), 1);
1293 CheckMenuItem(hMenu, IDM_VIEW_GROUP, (calc.usesep ? MF_CHECKED : MF_UNCHECKED));
1294
1295 /* update text for decimal button */
1296 SendDlgItemMessage(hWnd, IDC_BUTTON_DOT, WM_SETTEXT, (WPARAM)0, (LPARAM)calc.sDecimal);
1297 /* Fill combo box for conversion */
1298 if (calc.layout == CALC_LAYOUT_CONVERSION)
1299 ConvInit(hWnd);
1300 /* Restore the window at the same position it was */
1301 if (calc.x_coord >= 0 && calc.y_coord >= 0) {
1302 int w, h, sw, sh;
1303
1304 GetWindowRect(hWnd, &rc);
1305 w = rc.right-rc.left;
1306 h = rc.bottom-rc.top;
1307 sw = GetSystemMetrics(SM_CXSCREEN);
1308 sh = GetSystemMetrics(SM_CYSCREEN);
1309 if (calc.x_coord+w > sw) calc.x_coord = sw - w;
1310 if (calc.y_coord+h > sh) calc.y_coord = sh - h;
1311 MoveWindow(hWnd, calc.x_coord, calc.y_coord, w, h, FALSE);
1312 }
1313 break;
1314 case WM_CTLCOLORSTATIC:
1315 if ((HWND)lp == GetDlgItem(hWnd, IDC_TEXT_OUTPUT))
1316 return (LRESULT)GetStockObject(WHITE_BRUSH);
1317 break;
1318 case WM_HANDLE_CLIPBOARD:
1319 handle_sequence_input(hWnd, &calc.Clipboard);
1320 return TRUE;
1321 case WM_COMMAND:
1322 /*
1323 * if selection of category is changed, we must
1324 * updatethe content of the "from/to" combo boxes.
1325 */
1326 if (wp == MAKEWPARAM(IDC_COMBO_CATEGORY, CBN_SELCHANGE)) {
1327 ConvAdjust(hWnd, SendDlgItemMessage(hWnd, IDC_COMBO_CATEGORY, CB_GETCURSEL, 0, 0));
1328 return TRUE;
1329 }
1330 if (HIWORD(wp) != BN_CLICKED && HIWORD(wp) != BN_DBLCLK)
1331 break;
1332 /* avoid flicker if the user selects from keyboard */
1333 if (GetFocus() != GetDlgItem(hWnd, IDC_BUTTON_FOCUS))
1334 SetFocus(GetDlgItem(hWnd, IDC_BUTTON_FOCUS));
1335 switch (LOWORD(wp)) {
1336 case IDM_HELP_ABOUT:
1337 {
1338 TCHAR infotitle[100];
1339 TCHAR infotext[200];
1340 LoadString(calc.hInstance, IDS_CALC_NAME, infotitle, SIZEOF(infotitle));
1341 LoadString(calc.hInstance, IDS_AUTHOR, infotext, SIZEOF(infotext));
1342 ShellAbout(hWnd, infotitle, infotext, (HICON)LoadIcon(calc.hInstance, MAKEINTRESOURCE(IDI_CALC_BIG)));
1343 return TRUE;
1344 }
1345 case IDM_HELP_HELP:
1346 #ifndef DISABLE_HTMLHELP_SUPPORT
1347 HtmlHelp(hWnd, HTMLHELP_PATH("/general_information.htm"), HH_DISPLAY_TOPIC, (DWORD_PTR)NULL);
1348 #endif
1349 return TRUE;
1350 case IDM_VIEW_STANDARD:
1351 if (calc.layout != CALC_LAYOUT_STANDARD)
1352 {
1353 calc.layout = CALC_LAYOUT_STANDARD;
1354 calc.action = IDM_VIEW_STANDARD;
1355 DestroyWindow(hWnd);
1356 save_config();
1357
1358 CheckMenuRadioItem(GetMenu(hWnd),
1359 IDM_VIEW_STANDARD,
1360 IDM_VIEW_CONVERSION,
1361 IDM_VIEW_STANDARD,
1362 MF_BYCOMMAND);
1363 }
1364 return TRUE;
1365 case IDM_VIEW_SCIENTIFIC:
1366 if (calc.layout != CALC_LAYOUT_SCIENTIFIC)
1367 {
1368 calc.layout = CALC_LAYOUT_SCIENTIFIC;
1369 calc.action = IDM_VIEW_SCIENTIFIC;
1370 DestroyWindow(hWnd);
1371 save_config();
1372
1373 CheckMenuRadioItem(GetMenu(hWnd),
1374 IDM_VIEW_STANDARD,
1375 IDM_VIEW_CONVERSION,
1376 IDM_VIEW_SCIENTIFIC,
1377 MF_BYCOMMAND);
1378 }
1379 return TRUE;
1380 case IDM_VIEW_CONVERSION:
1381 if (calc.layout != CALC_LAYOUT_CONVERSION)
1382 {
1383 calc.layout = CALC_LAYOUT_CONVERSION;
1384 calc.action = IDM_VIEW_CONVERSION;
1385 DestroyWindow(hWnd);
1386 save_config();
1387
1388 CheckMenuRadioItem(GetMenu(hWnd),
1389 IDM_VIEW_STANDARD,
1390 IDM_VIEW_CONVERSION,
1391 IDM_VIEW_CONVERSION,
1392 MF_BYCOMMAND);
1393 }
1394 return TRUE;
1395 case IDM_VIEW_HEX:
1396 case IDM_VIEW_DEC:
1397 case IDM_VIEW_OCT:
1398 case IDM_VIEW_BIN:
1399 case IDM_VIEW_DEG:
1400 case IDM_VIEW_RAD:
1401 case IDM_VIEW_GRAD:
1402 case IDM_VIEW_QWORD:
1403 case IDM_VIEW_DWORD:
1404 case IDM_VIEW_WORD:
1405 case IDM_VIEW_BYTE:
1406 {
1407 WPARAM idc;
1408 if(idm_2_idc(LOWORD(wp), &idc))
1409 {
1410 SendMessage(hWnd, WM_COMMAND, idc, 0);
1411 return TRUE;
1412 }
1413 return FALSE;
1414 }
1415 case IDM_EDIT_COPY:
1416 handle_copy_command(hWnd);
1417 return TRUE;
1418 case IDM_EDIT_PASTE:
1419 if (calc.Clipboard.data != NULL)
1420 break;
1421 calc.Clipboard.data = ReadClipboard();
1422 if (calc.Clipboard.data != NULL) {
1423 /* clear the content of the display before pasting */
1424 PostMessage(hWnd, WM_COMMAND, IDC_BUTTON_CE, 0);
1425 calc.Clipboard.ptr = calc.Clipboard.data;
1426 calc.Clipboard.wm_msg = WM_HANDLE_CLIPBOARD;
1427 handle_sequence_input(hWnd, &calc.Clipboard);
1428 }
1429 return TRUE;
1430 case IDM_VIEW_GROUP:
1431 calc.usesep = (calc.usesep ? FALSE : TRUE);
1432 update_menu(hWnd);
1433 update_lcd_display(hWnd);
1434 save_config();
1435 return TRUE;
1436 case IDC_BUTTON_CONVERT:
1437 ConvExecute(hWnd);
1438 return TRUE;
1439 case IDC_BUTTON_CE: {
1440 calc_number_t tmp;
1441 rpn_zero(&tmp);
1442 display_rpn_result(hWnd, &tmp);
1443 }
1444 return TRUE;
1445 case IDC_RADIO_DEC:
1446 case IDC_RADIO_HEX:
1447 case IDC_RADIO_OCT:
1448 case IDC_RADIO_BIN:
1449 /* GNU WINDRES is bugged so I must always force radio update */
1450 /* (Fix for Win95/98) */
1451 #ifdef _MSC_VER
1452 if (calc.base == LOWORD(wp))
1453 break;
1454 #endif
1455 calc.is_nan = FALSE;
1456 update_radio(hWnd, LOWORD(wp));
1457 return TRUE;
1458 case IDC_RADIO_DEG:
1459 case IDC_RADIO_RAD:
1460 case IDC_RADIO_GRAD:
1461 /* GNU WINDRES is bugged so I must always force radio update */
1462 /* (Fix for Win95/98) */
1463 #ifdef _MSC_VER
1464 if (calc.degr == LOWORD(wp))
1465 break;
1466 #endif
1467 calc.degr = LOWORD(wp);
1468 calc.is_nan = FALSE;
1469 update_menu(hWnd);
1470 return TRUE;
1471 case IDC_RADIO_QWORD:
1472 case IDC_RADIO_DWORD:
1473 case IDC_RADIO_WORD:
1474 case IDC_RADIO_BYTE:
1475 /* GNU WINDRES is bugged so I must always force radio update */
1476 /* (Fix for Win95/98) */
1477 #ifdef _MSC_VER
1478 if (calc.size == LOWORD(wp))
1479 break;
1480 #endif
1481 calc.size = LOWORD(wp);
1482 calc.is_nan = FALSE;
1483 update_menu(hWnd);
1484 /*
1485 * update the content of the display
1486 */
1487 convert_text2number(&calc.code);
1488 apply_int_mask(&calc.code);
1489 display_rpn_result(hWnd, &calc.code);
1490 return TRUE;
1491 case IDC_BUTTON_1:
1492 case IDC_BUTTON_2:
1493 case IDC_BUTTON_3:
1494 case IDC_BUTTON_4:
1495 case IDC_BUTTON_5:
1496 case IDC_BUTTON_6:
1497 case IDC_BUTTON_7:
1498 case IDC_BUTTON_8:
1499 case IDC_BUTTON_9:
1500 case IDC_BUTTON_0:
1501 case IDC_BUTTON_DOT:
1502 case IDC_BUTTON_A:
1503 case IDC_BUTTON_B:
1504 case IDC_BUTTON_C:
1505 case IDC_BUTTON_D:
1506 case IDC_BUTTON_E:
1507 case IDC_BUTTON_F:
1508 calc.is_nan = FALSE;
1509 build_operand(hWnd, LOWORD(wp));
1510 return TRUE;
1511 case IDC_BUTTON_PERCENT:
1512 case IDC_BUTTON_ADD:
1513 case IDC_BUTTON_SUB:
1514 case IDC_BUTTON_MULT:
1515 case IDC_BUTTON_DIV:
1516 case IDC_BUTTON_MOD:
1517 case IDC_BUTTON_AND:
1518 case IDC_BUTTON_OR:
1519 case IDC_BUTTON_XOR:
1520 case IDC_BUTTON_LSH:
1521 case IDC_BUTTON_RSH:
1522 case IDC_BUTTON_EQU:
1523 case IDC_BUTTON_XeY:
1524 case IDC_BUTTON_XrY:
1525 if (calc.is_nan) break;
1526 /*
1527 * LSH and XeY buttons hold also the RSH and XrY functions with INV modifier,
1528 * but since they are two operand operators, they must be handled here.
1529 */
1530 if ((get_modifiers(hWnd) & MODIFIER_INV))
1531 {
1532 WPARAM IdcSim = IDC_STATIC;
1533
1534 switch (LOWORD(wp)) {
1535 case IDC_BUTTON_LSH: IdcSim = MAKEWPARAM(IDC_BUTTON_RSH, BN_CLICKED); break;
1536 case IDC_BUTTON_XeY: IdcSim = MAKEWPARAM(IDC_BUTTON_XrY, BN_CLICKED); break;
1537 }
1538
1539 if (IdcSim != IDC_STATIC)
1540 {
1541 PostMessage(hWnd, WM_COMMAND, IdcSim, 0);
1542 CheckDlgButton(hWnd, IDC_CHECK_INV, BST_UNCHECKED);
1543 }
1544 }
1545
1546 for (x=0; x<SIZEOF(operator_codes); x++) {
1547 if (LOWORD(wp) == operator_codes[x]) {
1548 convert_text2number(&calc.code);
1549
1550 if (calc.ptr == calc.buffer) {
1551 if (calc.last_operator != x) {
1552 if (x != RPN_OPERATOR_EQUAL)
1553 exec_change_infix();
1554 } else
1555 if (x == RPN_OPERATOR_EQUAL) {
1556 exec_infix2postfix(&calc.code, calc.prev_operator);
1557 rpn_copy(&calc.code, &calc.prev);
1558 } else
1559 break;
1560 }
1561
1562 /* if no change then quit silently, */
1563 /* without display updates */
1564 if (!exec_infix2postfix(&calc.code, x))
1565 break;
1566
1567 display_rpn_result(hWnd, &calc.code);
1568 break;
1569 }
1570 }
1571 return TRUE;
1572 case IDC_BUTTON_BACK:
1573 if (calc.sci_in) {
1574 if (calc.esp == 0) {
1575 TCHAR *ptr;
1576
1577 calc.sci_in = FALSE;
1578 ptr = _tcschr(calc.ptr, TEXT('e'));
1579 if (ptr)
1580 *ptr = TEXT('\0');
1581 update_lcd_display(hWnd);
1582 } else {
1583 calc.esp /= 10;
1584 build_operand(hWnd, IDC_STATIC);
1585 }
1586 } else
1587 if (calc.ptr != calc.buffer) {
1588 *--calc.ptr = TEXT('\0');
1589 if (!_tcscmp(calc.buffer, TEXT("-")) ||
1590 !_tcscmp(calc.buffer, TEXT("-0")) ||
1591 !_tcscmp(calc.buffer, TEXT("0"))) {
1592 calc.ptr = calc.buffer;
1593 calc.buffer[0] = TEXT('\0');
1594 }
1595 update_lcd_display(hWnd);
1596 }
1597 return TRUE;
1598 case IDC_BUTTON_MC:
1599 rpn_zero(&calc.memory.number);
1600 update_memory_flag(hWnd, FALSE);
1601 return TRUE;
1602 case IDC_BUTTON_MR:
1603 if (calc.is_memory) {
1604 calc.is_nan = FALSE;
1605 rpn_copy(&calc.code, &calc.memory.number);
1606 display_rpn_result(hWnd, &calc.code);
1607 }
1608 return TRUE;
1609 case IDC_BUTTON_EXP:
1610 if (calc.sci_in || calc.is_nan || calc.buffer == calc.ptr)
1611 break;
1612 calc.sci_in = TRUE;
1613 calc.esp = 0;
1614 build_operand(hWnd, IDC_STATIC);
1615 return TRUE;
1616 case IDC_BUTTON_SIGN:
1617 if (calc.sci_in) {
1618 calc.esp = 0-calc.esp;
1619 build_operand(hWnd, IDC_STATIC);
1620 } else {
1621 if (calc.is_nan || calc.buffer[0] == TEXT('\0'))
1622 break;
1623
1624 if (calc.buffer[0] == TEXT('-')) {
1625 /* make the number positive */
1626 memmove(calc.buffer, calc.buffer+1, sizeof(calc.buffer)-1);
1627 if (calc.buffer != calc.ptr)
1628 calc.ptr--;
1629 } else {
1630 /* if first char is '0' and no dot, it isn't valid */
1631 if (calc.buffer[0] == TEXT('0') &&
1632 calc.buffer[1] != TEXT('.'))
1633 break;
1634 /* make the number negative */
1635 memmove(calc.buffer+1, calc.buffer, sizeof(calc.buffer)-1);
1636 calc.buffer[0] = TEXT('-');
1637 if (calc.buffer != calc.ptr)
1638 calc.ptr++;
1639 }
1640 /* If the input buffer is empty, then
1641 we change also the sign of calc.code
1642 because it could be the result of a
1643 previous calculation. */
1644 if (calc.buffer == calc.ptr)
1645 rpn_sign(&calc.code);
1646 update_lcd_display(hWnd);
1647 }
1648 return TRUE;
1649 case IDC_BUTTON_RIGHTPAR:
1650 case IDC_BUTTON_LEFTPAR:
1651 case IDC_BUTTON_CANC:
1652 case IDC_BUTTON_MP:
1653 case IDC_BUTTON_DAT:
1654 case IDC_BUTTON_FE:
1655 case IDC_BUTTON_DMS:
1656 case IDC_BUTTON_SQRT:
1657 case IDC_BUTTON_S:
1658 case IDC_BUTTON_SUM:
1659 case IDC_BUTTON_AVE:
1660 case IDC_BUTTON_NF:
1661 case IDC_BUTTON_LN:
1662 case IDC_BUTTON_LOG:
1663 case IDC_BUTTON_Xe2:
1664 case IDC_BUTTON_Xe3:
1665 case IDC_BUTTON_PI:
1666 case IDC_BUTTON_NOT:
1667 case IDC_BUTTON_RX:
1668 case IDC_BUTTON_INT:
1669 case IDC_BUTTON_SIN:
1670 case IDC_BUTTON_COS:
1671 case IDC_BUTTON_TAN:
1672 case IDC_BUTTON_MS:
1673 for (x=0; x<SIZEOF(function_table); x++) {
1674 if (LOWORD(wp) == function_table[x].idc) {
1675 rpn_callback1 cb = NULL;
1676
1677 /* test if NaN state is important or not */
1678 if (calc.is_nan && function_table[x].check_nan) break;
1679 /* otherwise, it's cleared */
1680 calc.is_nan = FALSE;
1681
1682 switch (get_modifiers(hWnd) & function_table[x].range) {
1683 case 0:
1684 cb = function_table[x].direct;
1685 break;
1686 case MODIFIER_INV:
1687 cb = function_table[x].inverse;
1688 break;
1689 case MODIFIER_HYP:
1690 cb = function_table[x].hyperb;
1691 break;
1692 case MODIFIER_INV|MODIFIER_HYP:
1693 cb = function_table[x].inv_hyp;
1694 break;
1695 }
1696 if (cb != NULL) {
1697 convert_text2number(&calc.code);
1698 cb(&calc.code);
1699 display_rpn_result(hWnd, &calc.code);
1700 // if (!(function_table[x].range & NO_CHAIN))
1701 // exec_infix2postfix(&calc.code, RPN_OPERATOR_NONE);
1702 if (function_table[x].range & MODIFIER_INV)
1703 SendDlgItemMessage(hWnd, IDC_CHECK_INV, BM_SETCHECK, 0, 0);
1704 if (function_table[x].range & MODIFIER_HYP)
1705 SendDlgItemMessage(hWnd, IDC_CHECK_HYP, BM_SETCHECK, 0, 0);
1706 }
1707 }
1708 }
1709 return TRUE;
1710 case IDC_BUTTON_STA:
1711 if (IsWindow(calc.hStatWnd))
1712 break;
1713 calc.hStatWnd = CreateDialog(calc.hInstance,
1714 MAKEINTRESOURCE(IDD_DIALOG_STAT), hWnd, DlgStatProc);
1715 if (calc.hStatWnd != NULL) {
1716 enable_allowed_controls(hWnd, calc.base);
1717 SendMessage(calc.hStatWnd, WM_SETFOCUS, 0, 0);
1718 }
1719 return TRUE;
1720 }
1721 break;
1722 case WM_CLOSE_STATS:
1723 enable_allowed_controls(hWnd, calc.base);
1724 return TRUE;
1725 case WM_LOAD_STAT:
1726 if (upload_stat_number((int)LOWORD(wp)) != NULL)
1727 display_rpn_result(hWnd, &calc.code);
1728 return TRUE;
1729 case WM_START_CONV:
1730 x = LOWORD(lp);
1731 calc.Convert[x].data = ReadConversion(calc.Convert[x].data);
1732 if (calc.Convert[x].data != NULL) {
1733 calc.Convert[x].ptr = calc.Convert[x].data;
1734 PostMessage(hWnd, HIWORD(lp), 0, 0);
1735 }
1736 return TRUE;
1737 case WM_HANDLE_FROM:
1738 if (calc.is_nan)
1739 break;
1740 if (handle_sequence_input(hWnd, &calc.Convert[0]) == NULL)
1741 PostMessage(hWnd, WM_START_CONV, 0,
1742 MAKELPARAM(0x0001, WM_HANDLE_TO));
1743 return TRUE;
1744 case WM_HANDLE_TO:
1745 if (!calc.is_nan)
1746 handle_sequence_input(hWnd, &calc.Convert[1]);
1747 return TRUE;
1748 case WM_CLOSE:
1749 calc.action = IDC_STATIC;
1750 DestroyWindow(hWnd);
1751 return TRUE;
1752 case WM_DESTROY:
1753 /* Get (x,y) position of the calculator */
1754 GetWindowRect(hWnd, &rc);
1755 calc.x_coord = rc.left;
1756 calc.y_coord = rc.top;
1757 #ifdef USE_KEYBOARD_HOOK
1758 UnhookWindowsHookEx(calc.hKeyboardHook);
1759 #endif
1760 PostQuitMessage(0);
1761 return TRUE;
1762 case WM_CONTEXTMENU:
1763 if ((HWND)wp != hWnd)
1764 handle_context_menu(hWnd, wp, lp);
1765 return TRUE;
1766 case WM_ENTERMENULOOP:
1767 calc.is_menu_on = TRUE;
1768 /* Check if a valid format is available in the clipboard */
1769 EnableMenuItem(GetSubMenu(GetMenu(hWnd), 0),
1770 IDM_EDIT_PASTE,
1771 MF_BYCOMMAND|
1772 (IsClipboardFormatAvailable(CF_TEXT) ?
1773 MF_ENABLED : MF_GRAYED));
1774 break;
1775 case WM_EXITMENULOOP:
1776 calc.is_menu_on = FALSE;
1777 break;
1778 }
1779 return FALSE;
1780 }
1781
1782 #if defined(__GNUC__) && !defined(__REACTOS__)
1783 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
1784 #else
1785 int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
1786 #endif
1787 {
1788 MSG msg;
1789 DWORD dwLayout;
1790
1791 InitCommonControls();
1792
1793 calc.hInstance = hInstance;
1794
1795 calc.x_coord = -1;
1796 calc.y_coord = -1;
1797
1798 load_config();
1799 start_rpn_engine();
1800
1801 do {
1802 /* ignore hwnd: dialogs are already visible! */
1803 if (calc.layout == CALC_LAYOUT_SCIENTIFIC)
1804 dwLayout = IDD_DIALOG_SCIENTIFIC;
1805 else if (calc.layout == CALC_LAYOUT_CONVERSION)
1806 dwLayout = IDD_DIALOG_CONVERSION;
1807 else
1808 dwLayout = IDD_DIALOG_STANDARD;
1809
1810 /* This call will always fail if UNICODE for Win9x */
1811 if (NULL == CreateDialog(hInstance, MAKEINTRESOURCE(dwLayout), NULL, DlgMainProc))
1812 break;
1813
1814 while (GetMessage(&msg, NULL, 0, 0)) {
1815 #ifndef USE_KEYBOARD_HOOK
1816 if ((msg.message == WM_KEYUP ||
1817 msg.message == WM_KEYDOWN) &&
1818 !calc.is_menu_on)
1819 process_vk_key(msg.wParam, msg.lParam);
1820 #endif
1821 TranslateMessage(&msg);
1822 DispatchMessage(&msg);
1823 }
1824 } while (calc.action != IDC_STATIC);
1825
1826 stop_rpn_engine();
1827
1828 return 0;
1829 }