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