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