Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / programs / winetest / gui.c
bloba8bcf05d5e2e9633199d9604f97e94d2b914c084
1 /*
2 * GUI support
4 * Copyright 2004 Ferenc Wagner
6 * This library 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.1 of the License, or (at your option) any later version.
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.
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 St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <windows.h>
22 #include <commctrl.h>
24 #include "resource.h"
25 #include "winetest.h"
27 /* Event object to signal successful window creation to main thread.
29 static HANDLE initEvent;
31 /* Dialog handle
33 static HWND dialog;
35 /* Progress data for the text* functions and for scaling.
37 static unsigned int progressMax, progressCurr;
38 static double progressScale;
40 /* Progress group counter for the gui* functions.
42 static int progressGroup;
44 static WNDPROC DefEditProc;
46 static int
47 MBdefault (int uType)
49 static const int matrix[][4] = {{IDOK, 0, 0, 0},
50 {IDOK, IDCANCEL, 0, 0},
51 {IDABORT, IDRETRY, IDIGNORE, 0},
52 {IDYES, IDNO, IDCANCEL, 0},
53 {IDYES, IDNO, 0, 0},
54 {IDRETRY, IDCANCEL, 0, 0}};
55 int type = uType & MB_TYPEMASK;
56 int def = (uType & MB_DEFMASK) / MB_DEFBUTTON2;
58 return matrix[type][def];
61 /* report (R_STATUS, fmt, ...) */
62 static int
63 textStatus (va_list ap)
65 char *str = vstrmake (NULL, ap);
67 fputs (str, stderr);
68 fputc ('\n', stderr);
69 heap_free (str);
70 return 0;
73 static int
74 guiStatus (va_list ap)
76 size_t len;
77 char *str = vstrmake (&len, ap);
79 if (len > 128) str[129] = 0;
80 SetDlgItemTextA (dialog, IDC_SB, str);
81 heap_free (str);
82 return 0;
85 /* report (R_PROGRESS, barnum, steps) */
86 static int
87 textProgress (va_list ap)
89 progressGroup = va_arg (ap, int);
90 progressMax = va_arg (ap, int);
91 progressCurr = 0;
92 return 0;
95 static int
96 guiProgress (va_list ap)
98 unsigned int max;
99 HWND pb;
101 progressGroup = va_arg (ap, int);
102 progressMax = max = va_arg (ap, int);
103 progressCurr = 0;
104 if (max > 0xffff) {
105 progressScale = (double)0xffff / max;
106 max = 0xffff;
108 else progressScale = 1;
109 pb = GetDlgItem (dialog, IDC_PB0 + progressGroup * 2);
110 SendMessageA(pb, PBM_SETRANGE, 0, MAKELPARAM (0, max));
111 SendMessageA(pb, PBM_SETSTEP, 1, 0);
112 return 0;
115 /* report (R_STEP, fmt, ...) */
116 static int
117 textStep (va_list ap)
119 char *str = vstrmake (NULL, ap);
121 progressCurr++;
122 fputs (str, stderr);
123 fprintf (stderr, " (%d of %d)\n", progressCurr, progressMax);
124 heap_free (str);
125 return 0;
128 static int
129 guiStep (va_list ap)
131 const int pgID = IDC_ST0 + progressGroup * 2;
132 char *str = vstrmake (NULL, ap);
134 progressCurr++;
135 SetDlgItemTextA (dialog, pgID, str);
136 SendDlgItemMessageA(dialog, pgID+1, PBM_SETPOS,
137 progressScale * progressCurr, 0);
138 heap_free (str);
139 return 0;
142 /* report (R_DELTA, inc, fmt, ...) */
143 static int
144 textDelta (va_list ap)
146 const int inc = va_arg (ap, int);
147 char *str = vstrmake (NULL, ap);
149 progressCurr += inc;
150 fputs (str, stderr);
151 fprintf (stderr, " (%d of %d)\n", progressCurr, progressMax);
152 heap_free (str);
153 return 0;
156 static int
157 guiDelta (va_list ap)
159 const int inc = va_arg (ap, int);
160 const int pgID = IDC_ST0 + progressGroup * 2;
161 char *str = vstrmake (NULL, ap);
163 progressCurr += inc;
164 SetDlgItemTextA (dialog, pgID, str);
165 SendDlgItemMessageA(dialog, pgID+1, PBM_SETPOS,
166 progressScale * progressCurr, 0);
167 heap_free (str);
168 return 0;
171 /* report (R_TAG) */
172 static int
173 textTag (va_list ap)
175 fputs ("Tag: ", stderr);
176 fputs (tag, stderr);
177 fputc ('\n', stderr);
178 return 0;
181 static int
182 guiTag (va_list ap)
184 SetDlgItemTextA (dialog, IDC_TAG, tag);
185 return 0;
188 /* report (R_DIR, fmt, ...) */
189 static int
190 textDir (va_list ap)
192 char *str = vstrmake (NULL, ap);
194 fputs ("Temporary directory: ", stderr);
195 fputs (str, stderr);
196 fputc ('\n', stderr);
197 heap_free (str);
198 return 0;
201 static int
202 guiDir (va_list ap)
204 char *str = vstrmake (NULL, ap);
206 SetDlgItemTextA (dialog, IDC_DIR, str);
207 heap_free (str);
208 return 0;
211 /* report (R_OUT, fmt, ...) */
212 static int
213 textOut (va_list ap)
215 char *str = vstrmake (NULL, ap);
217 fputs ("Log file: ", stderr);
218 fputs (str, stderr);
219 fputc ('\n', stderr);
220 heap_free (str);
221 return 0;
224 static int
225 guiOut (va_list ap)
227 char *str = vstrmake (NULL, ap);
229 SetDlgItemTextA (dialog, IDC_OUT, str);
230 heap_free (str);
231 return 0;
234 /* report (R_WARNING, fmt, ...) */
235 static int
236 textWarning (va_list ap)
238 fputs ("Warning: ", stderr);
239 textStatus (ap);
240 return 0;
243 static int
244 guiWarning (va_list ap)
246 char *str = vstrmake (NULL, ap);
248 MessageBoxA (dialog, str, "Warning", MB_ICONWARNING | MB_OK);
249 heap_free (str);
250 return 0;
253 /* report (R_ERROR, fmt, ...) */
254 static int
255 textError (va_list ap)
257 fputs ("Error: ", stderr);
258 textStatus (ap);
259 return 0;
262 static int
263 guiError (va_list ap)
265 char *str = vstrmake (NULL, ap);
267 MessageBoxA (dialog, str, "Error", MB_ICONERROR | MB_OK);
268 heap_free (str);
269 return 0;
272 /* report (R_FATAL, fmt, ...) */
273 static int
274 textFatal (va_list ap)
276 textError (ap);
277 exit (1);
280 static int
281 guiFatal (va_list ap)
283 guiError (ap);
284 exit (1);
287 /* report (R_ASK, type, fmt, ...) */
288 static int
289 textAsk (va_list ap)
291 int uType = va_arg (ap, int);
292 int ret = MBdefault (uType);
293 char *str = vstrmake (NULL, ap);
295 fprintf (stderr, "Question of type %d: %s\n"
296 "Returning default: %d\n", uType, str, ret);
297 heap_free (str);
298 return ret;
301 static int
302 guiAsk (va_list ap)
304 int uType = va_arg (ap, int);
305 char *str = vstrmake (NULL, ap);
306 int ret = MessageBoxA (dialog, str, "Question", MB_ICONQUESTION | uType);
308 heap_free (str);
309 return ret;
312 static BOOL CALLBACK
313 EditTagProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
315 switch (msg) {
316 case WM_CHAR:
317 if (wParam == 8) break; /* backspace is OK */
318 if (GetWindowTextLengthA (hwnd) == MAXTAGLEN ||
319 !goodtagchar (wParam)) return TRUE;
320 break;
322 return CallWindowProcA (DefEditProc, hwnd, msg, wParam, lParam);
325 static INT_PTR CALLBACK
326 AskTagProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
328 int len;
330 switch (msg) {
331 case WM_INITDIALOG:
332 DefEditProc = (WNDPROC)SetWindowLongPtrA
333 (GetDlgItem (hwnd, IDC_TAG), GWLP_WNDPROC, (LONG_PTR)EditTagProc);
334 return TRUE;
335 case WM_COMMAND:
336 switch (LOWORD (wParam)) {
337 case IDOK:
338 len = GetWindowTextLengthA (GetDlgItem (hwnd, IDC_TAG));
339 if(!len) {
340 report (R_WARNING, "You must enter a tag to continue");
341 return FALSE;
343 tag = heap_alloc (len+1);
344 GetDlgItemTextA (hwnd, IDC_TAG, tag, len+1);
345 EndDialog (hwnd, IDOK);
346 return TRUE;
347 case IDABORT:
348 EndDialog (hwnd, IDABORT);
349 return TRUE;
352 return FALSE;
356 guiAskTag (void)
358 return DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_TAG), dialog, AskTagProc);
361 static INT_PTR CALLBACK
362 AskEmailProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
364 int len;
366 switch (msg)
368 case WM_COMMAND:
369 switch (LOWORD (wParam)) {
370 case IDOK:
371 len = GetWindowTextLengthA (GetDlgItem (hwnd, IDC_EMAIL));
372 if(!len) {
373 report (R_WARNING, "You must enter an email address to continue");
374 return FALSE;
376 email = heap_alloc (len+1);
377 GetDlgItemTextA (hwnd, IDC_EMAIL, email, len+1);
378 EndDialog (hwnd, IDOK);
379 return TRUE;
380 case IDABORT:
381 EndDialog (hwnd, IDABORT);
382 return TRUE;
385 return FALSE;
389 guiAskEmail (void)
391 return DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_EMAIL), dialog, AskEmailProc);
394 /* Quiet functions */
395 static int
396 qNoOp (va_list ap)
398 return 0;
401 static int
402 qAsk (va_list ap)
404 return MBdefault (va_arg (ap, int));
407 static INT_PTR CALLBACK
408 AboutProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
410 switch (msg) {
411 case WM_COMMAND:
412 switch (LOWORD (wParam)) {
413 case IDCANCEL:
414 EndDialog (hwnd, IDCANCEL);
415 return TRUE;
418 return FALSE;
421 static INT_PTR CALLBACK
422 DlgProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
424 switch (msg) {
425 case WM_INITDIALOG:
426 SendMessageA(hwnd, WM_SETICON, ICON_SMALL,
427 (LPARAM)LoadImageA( GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDI_WINE), IMAGE_ICON,
428 GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
429 LR_SHARED ));
430 SendMessageA(hwnd, WM_SETICON, ICON_BIG,
431 (LPARAM)LoadIconA( GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDI_WINE)));
432 dialog = hwnd;
433 if (!SetEvent (initEvent)) {
434 report (R_STATUS, "Can't signal main thread: %d",
435 GetLastError ());
436 EndDialog (hwnd, 2);
438 return TRUE;
439 case WM_CLOSE:
440 EndDialog (hwnd, 3);
441 return TRUE;
442 case WM_COMMAND:
443 switch (LOWORD (wParam)) {
444 case IDHELP:
445 DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_ABOUT), hwnd, AboutProc);
446 return TRUE;
447 case IDABORT:
448 report (R_STATUS, "Aborting, please wait...");
449 aborting = TRUE;
450 return TRUE;
453 return FALSE;
456 static DWORD WINAPI
457 DlgThreadProc (LPVOID param)
459 int ret;
461 InitCommonControls ();
462 ret = DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_STATUS), NULL, DlgProc);
463 switch (ret) {
464 case 0:
465 report (R_FATAL, "Cannot display dialog");
466 break;
467 case 1:
468 report (R_WARNING, "DialogBox failed: %d",
469 GetLastError ());
470 break;
471 case 3:
472 exit (0);
473 default:
474 report (R_STATUS, "Dialog exited: %d", ret);
476 return 0;
480 report (enum report_type t, ...)
482 typedef int r_fun_t (va_list);
484 va_list ap;
485 int ret = 0;
486 static r_fun_t * const text_funcs[] =
487 {textStatus, textProgress, textStep, textDelta,
488 textTag, textDir, textOut,
489 textWarning, textError, textFatal, textAsk};
490 static r_fun_t * const GUI_funcs[] =
491 {guiStatus, guiProgress, guiStep, guiDelta,
492 guiTag, guiDir, guiOut,
493 guiWarning, guiError, guiFatal, guiAsk};
494 static r_fun_t * const quiet_funcs[] =
495 {qNoOp, qNoOp, qNoOp, qNoOp,
496 qNoOp, qNoOp, qNoOp,
497 qNoOp, textError, textFatal, qAsk};
498 static r_fun_t * const * funcs = NULL;
500 switch (t) {
501 case R_TEXTMODE:
502 funcs = text_funcs;
503 return 0;
504 case R_QUIET:
505 funcs = quiet_funcs;
506 return 0;
507 default:
508 break;
511 if (!funcs) {
512 HANDLE DlgThread;
513 DWORD DlgThreadID;
515 funcs = text_funcs;
516 initEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
517 if (!initEvent)
518 report (R_STATUS, "Can't create event object: %d",
519 GetLastError ());
520 else {
521 DlgThread = CreateThread (NULL, 0, DlgThreadProc,
522 NULL, 0, &DlgThreadID);
523 if (!DlgThread)
524 report (R_STATUS, "Can't create GUI thread: %d",
525 GetLastError ());
526 else {
527 DWORD ret = WaitForSingleObject (initEvent, INFINITE);
528 CloseHandle(DlgThread);
529 switch (ret) {
530 case WAIT_OBJECT_0:
531 funcs = GUI_funcs;
532 break;
533 case WAIT_TIMEOUT:
534 report (R_STATUS, "GUI creation timed out");
535 break;
536 case WAIT_FAILED:
537 report (R_STATUS, "Wait for GUI failed: %d",
538 GetLastError ());
539 break;
540 default:
541 report (R_STATUS, "Wait returned %d",
542 ret);
543 break;
549 va_start (ap, t);
550 if (t < sizeof text_funcs / sizeof text_funcs[0] &&
551 t < sizeof GUI_funcs / sizeof GUI_funcs[0]) ret = funcs[t](ap);
552 else report (R_WARNING, "unimplemented report type: %d", t);
553 va_end (ap);
554 return ret;