blob: 2957dbfd1d57d2686450a5197988eaa49c55acde [file] [log] [blame]
Ferenc Wagner354f6622004-01-15 01:48:05 +00001/*
2 * GUI support
3 *
4 * Copyright 2004 Ferenc Wagner
5 *
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.
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Ferenc Wagner354f6622004-01-15 01:48:05 +000019 */
20
21#include <windows.h>
22#include <commctrl.h>
23
Ferenc Wagner24624f62004-06-15 22:45:15 +000024#include "resource.h"
Ferenc Wagner354f6622004-01-15 01:48:05 +000025#include "winetest.h"
26
27/* Event object to signal successful window creation to main thread.
28 */
Mike McCormackae511352005-06-02 15:11:32 +000029static HANDLE initEvent;
Ferenc Wagner354f6622004-01-15 01:48:05 +000030
31/* Dialog handle
32 */
Mike McCormackae511352005-06-02 15:11:32 +000033static HWND dialog;
Ferenc Wagner354f6622004-01-15 01:48:05 +000034
35/* Progress data for the text* functions and for scaling.
36 */
Mike McCormackae511352005-06-02 15:11:32 +000037static unsigned int progressMax, progressCurr;
38static double progressScale;
Ferenc Wagner354f6622004-01-15 01:48:05 +000039
40/* Progress group counter for the gui* functions.
41 */
Mike McCormackae511352005-06-02 15:11:32 +000042static int progressGroup;
Ferenc Wagner354f6622004-01-15 01:48:05 +000043
Mike McCormackae511352005-06-02 15:11:32 +000044static WNDPROC DefEditProc;
Ferenc Wagnerff67da42005-04-18 09:54:24 +000045
Mike McCormackae511352005-06-02 15:11:32 +000046static int
Ferenc Wagnerefc67252004-02-19 04:12:42 +000047MBdefault (int uType)
48{
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;
57
58 return matrix[type][def];
59}
60
Ferenc Wagner354f6622004-01-15 01:48:05 +000061/* report (R_STATUS, fmt, ...) */
Mike McCormackae511352005-06-02 15:11:32 +000062static int
Ferenc Wagner354f6622004-01-15 01:48:05 +000063textStatus (va_list ap)
64{
65 char *str = vstrmake (NULL, ap);
66
67 fputs (str, stderr);
68 fputc ('\n', stderr);
69 free (str);
70 return 0;
71}
72
Mike McCormackae511352005-06-02 15:11:32 +000073static int
Ferenc Wagner354f6622004-01-15 01:48:05 +000074guiStatus (va_list ap)
75{
76 size_t len;
77 char *str = vstrmake (&len, ap);
78
79 if (len > 128) str[129] = 0;
80 SetDlgItemText (dialog, IDC_SB, str);
81 free (str);
82 return 0;
83}
84
Ferenc Wagnerfeaad962004-02-21 04:01:56 +000085/* report (R_PROGRESS, barnum, steps) */
Mike McCormackae511352005-06-02 15:11:32 +000086static int
Ferenc Wagner354f6622004-01-15 01:48:05 +000087textProgress (va_list ap)
88{
Ferenc Wagner83877702004-03-19 01:54:10 +000089 progressGroup = va_arg (ap, int);
Ferenc Wagner354f6622004-01-15 01:48:05 +000090 progressMax = va_arg (ap, int);
91 progressCurr = 0;
92 return 0;
93}
94
Mike McCormackae511352005-06-02 15:11:32 +000095static int
Ferenc Wagner354f6622004-01-15 01:48:05 +000096guiProgress (va_list ap)
97{
Ferenc Wagnerfeaad962004-02-21 04:01:56 +000098 unsigned int max;
99 HWND pb;
Ferenc Wagner354f6622004-01-15 01:48:05 +0000100
Ferenc Wagnerfeaad962004-02-21 04:01:56 +0000101 progressGroup = va_arg (ap, int);
102 progressMax = max = va_arg (ap, int);
Ferenc Wagner354f6622004-01-15 01:48:05 +0000103 progressCurr = 0;
104 if (max > 0xffff) {
105 progressScale = (double)0xffff / max;
106 max = 0xffff;
107 }
108 else progressScale = 1;
Ferenc Wagnerfeaad962004-02-21 04:01:56 +0000109 pb = GetDlgItem (dialog, IDC_PB0 + progressGroup * 2);
Ferenc Wagner354f6622004-01-15 01:48:05 +0000110 SendMessage (pb, PBM_SETRANGE, 0, MAKELPARAM (0, max));
111 SendMessage (pb, PBM_SETSTEP, (WPARAM)1, 0);
112 return 0;
113}
114
115/* report (R_STEP, fmt, ...) */
Mike McCormackae511352005-06-02 15:11:32 +0000116static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000117textStep (va_list ap)
118{
119 char *str = vstrmake (NULL, ap);
120
121 progressCurr++;
122 fputs (str, stderr);
123 fprintf (stderr, " (%d of %d)\n", progressCurr, progressMax);
124 free (str);
125 return 0;
126}
127
Mike McCormackae511352005-06-02 15:11:32 +0000128static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000129guiStep (va_list ap)
130{
131 const int pgID = IDC_ST0 + progressGroup * 2;
132 char *str = vstrmake (NULL, ap);
133
134 progressCurr++;
135 SetDlgItemText (dialog, pgID, str);
136 SendDlgItemMessage (dialog, pgID+1, PBM_SETPOS,
137 (WPARAM)(progressScale * progressCurr), 0);
138 free (str);
139 return 0;
140}
141
142/* report (R_DELTA, inc, fmt, ...) */
Mike McCormackae511352005-06-02 15:11:32 +0000143static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000144textDelta (va_list ap)
145{
146 const int inc = va_arg (ap, int);
147 char *str = vstrmake (NULL, ap);
148
149 progressCurr += inc;
150 fputs (str, stderr);
151 fprintf (stderr, " (%d of %d)\n", progressCurr, progressMax);
152 free (str);
153 return 0;
154}
155
Mike McCormackae511352005-06-02 15:11:32 +0000156static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000157guiDelta (va_list ap)
158{
159 const int inc = va_arg (ap, int);
160 const int pgID = IDC_ST0 + progressGroup * 2;
161 char *str = vstrmake (NULL, ap);
162
163 progressCurr += inc;
164 SetDlgItemText (dialog, pgID, str);
165 SendDlgItemMessage (dialog, pgID+1, PBM_SETPOS,
166 (WPARAM)(progressScale * progressCurr), 0);
167 free (str);
168 return 0;
169}
170
Ferenc Wagner30dcc6b2005-03-31 15:22:41 +0000171/* report (R_TAG) */
Mike McCormackae511352005-06-02 15:11:32 +0000172static int
Ferenc Wagner30dcc6b2005-03-31 15:22:41 +0000173textTag (va_list ap)
174{
175 fputs ("Tag: ", stderr);
176 fputs (tag, stderr);
177 fputc ('\n', stderr);
178 return 0;
179}
180
Mike McCormackae511352005-06-02 15:11:32 +0000181static int
Ferenc Wagner30dcc6b2005-03-31 15:22:41 +0000182guiTag (va_list ap)
183{
184 SetDlgItemText (dialog, IDC_TAG, tag);
185 return 0;
186}
187
Ferenc Wagner354f6622004-01-15 01:48:05 +0000188/* report (R_DIR, fmt, ...) */
Mike McCormackae511352005-06-02 15:11:32 +0000189static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000190textDir (va_list ap)
191{
192 char *str = vstrmake (NULL, ap);
193
194 fputs ("Temporary directory: ", stderr);
195 fputs (str, stderr);
196 fputc ('\n', stderr);
197 free (str);
198 return 0;
199}
200
Mike McCormackae511352005-06-02 15:11:32 +0000201static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000202guiDir (va_list ap)
203{
204 char *str = vstrmake (NULL, ap);
205
206 SetDlgItemText (dialog, IDC_DIR, str);
207 free (str);
208 return 0;
209}
210
211/* report (R_OUT, fmt, ...) */
Mike McCormackae511352005-06-02 15:11:32 +0000212static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000213textOut (va_list ap)
214{
215 char *str = vstrmake (NULL, ap);
216
217 fputs ("Log file: ", stderr);
218 fputs (str, stderr);
219 fputc ('\n', stderr);
220 free (str);
221 return 0;
222}
223
Mike McCormackae511352005-06-02 15:11:32 +0000224static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000225guiOut (va_list ap)
226{
227 char *str = vstrmake (NULL, ap);
228
229 SetDlgItemText (dialog, IDC_OUT, str);
230 free (str);
231 return 0;
232}
233
Ferenc Wagner354f6622004-01-15 01:48:05 +0000234/* report (R_WARNING, fmt, ...) */
Mike McCormackae511352005-06-02 15:11:32 +0000235static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000236textWarning (va_list ap)
237{
Ferenc Wagner354f6622004-01-15 01:48:05 +0000238 fputs ("Warning: ", stderr);
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000239 textStatus (ap);
Ferenc Wagner354f6622004-01-15 01:48:05 +0000240 return 0;
241}
242
Mike McCormackae511352005-06-02 15:11:32 +0000243static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000244guiWarning (va_list ap)
245{
246 char *str = vstrmake (NULL, ap);
247
248 MessageBox (dialog, str, "Warning", MB_ICONWARNING | MB_OK);
249 free (str);
250 return 0;
251}
252
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000253/* report (R_ERROR, fmt, ...) */
Mike McCormackae511352005-06-02 15:11:32 +0000254static int
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000255textError (va_list ap)
256{
257 fputs ("Error: ", stderr);
258 textStatus (ap);
259 return 0;
260}
261
Mike McCormackae511352005-06-02 15:11:32 +0000262static int
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000263guiError (va_list ap)
264{
265 char *str = vstrmake (NULL, ap);
266
267 MessageBox (dialog, str, "Error", MB_ICONERROR | MB_OK);
268 free (str);
269 return 0;
270}
271
272/* report (R_FATAL, fmt, ...) */
Mike McCormackae511352005-06-02 15:11:32 +0000273static int
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000274textFatal (va_list ap)
275{
276 textError (ap);
277 exit (1);
278}
279
Mike McCormackae511352005-06-02 15:11:32 +0000280static int
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000281guiFatal (va_list ap)
282{
283 guiError (ap);
284 exit (1);
285}
286
Ferenc Wagner354f6622004-01-15 01:48:05 +0000287/* report (R_ASK, type, fmt, ...) */
Mike McCormackae511352005-06-02 15:11:32 +0000288static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000289textAsk (va_list ap)
290{
291 int uType = va_arg (ap, int);
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000292 int ret = MBdefault (uType);
Ferenc Wagner354f6622004-01-15 01:48:05 +0000293 char *str = vstrmake (NULL, ap);
294
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000295 fprintf (stderr, "Question of type %d: %s\n"
296 "Returning default: %d\n", uType, str, ret);
Ferenc Wagner354f6622004-01-15 01:48:05 +0000297 free (str);
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000298 return ret;
Ferenc Wagner354f6622004-01-15 01:48:05 +0000299}
300
Mike McCormackae511352005-06-02 15:11:32 +0000301static int
Ferenc Wagner354f6622004-01-15 01:48:05 +0000302guiAsk (va_list ap)
303{
304 int uType = va_arg (ap, int);
305 char *str = vstrmake (NULL, ap);
306 int ret = MessageBox (dialog, str, "Question",
307 MB_ICONQUESTION | uType);
308
309 free (str);
310 return ret;
311}
312
Mike McCormackae511352005-06-02 15:11:32 +0000313static BOOL CALLBACK
Ferenc Wagnerff67da42005-04-18 09:54:24 +0000314EditTagProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
315{
316 switch (msg) {
317 case WM_CHAR:
318 if (wParam == 8) break; /* backspace is OK */
319 if (GetWindowTextLengthA (hwnd) == MAXTAGLEN ||
320 !goodtagchar (wParam)) return TRUE;
321 break;
322 }
323 return CallWindowProcA (DefEditProc, hwnd, msg, wParam, lParam);
324}
325
Mike McCormack5ea2a172006-06-06 14:21:13 +0900326static INT_PTR CALLBACK
Ferenc Wagner30dcc6b2005-03-31 15:22:41 +0000327AskTagProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
328{
329 int len;
330
331 switch (msg) {
Ferenc Wagnerff67da42005-04-18 09:54:24 +0000332 case WM_INITDIALOG:
333 DefEditProc = (WNDPROC)SetWindowLongPtr
334 (GetDlgItem (hwnd, IDC_TAG), GWLP_WNDPROC, (LONG_PTR)EditTagProc);
335 return TRUE;
Ferenc Wagner30dcc6b2005-03-31 15:22:41 +0000336 case WM_COMMAND:
337 switch (LOWORD (wParam)) {
338 case IDOK:
339 len = GetWindowTextLengthA (GetDlgItem (hwnd, IDC_TAG));
Jeff Zaroyko454a2412008-09-04 18:06:46 +1000340 if(!len) {
341 report (R_WARNING, "You must enter a tag to continue");
342 return FALSE;
343 }
Ferenc Wagnerff67da42005-04-18 09:54:24 +0000344 tag = xmalloc (len+1);
345 GetDlgItemTextA (hwnd, IDC_TAG, tag, len+1);
346 EndDialog (hwnd, IDOK);
Ferenc Wagner30dcc6b2005-03-31 15:22:41 +0000347 return TRUE;
348 case IDABORT:
349 EndDialog (hwnd, IDABORT);
350 return TRUE;
351 }
352 }
353 return FALSE;
354}
355
356int
357guiAskTag (void)
358{
359 return DialogBox (GetModuleHandle (NULL),
360 MAKEINTRESOURCE (IDD_TAG),
361 dialog, AskTagProc);
362}
363
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000364/* Quiet functions */
Mike McCormackae511352005-06-02 15:11:32 +0000365static int
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000366qNoOp (va_list ap)
367{
368 return 0;
369}
370
Mike McCormackae511352005-06-02 15:11:32 +0000371static int
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000372qFatal (va_list ap)
373{
374 exit (1);
375}
376
Mike McCormackae511352005-06-02 15:11:32 +0000377static int
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000378qAsk (va_list ap)
379{
380 return MBdefault (va_arg (ap, int));
381}
382
Mike McCormack5ea2a172006-06-06 14:21:13 +0900383static INT_PTR CALLBACK
Ferenc Wagner354f6622004-01-15 01:48:05 +0000384AboutProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
385{
386 switch (msg) {
387 case WM_COMMAND:
388 switch (LOWORD (wParam)) {
389 case IDCANCEL:
390 EndDialog (hwnd, IDCANCEL);
391 return TRUE;
392 }
393 }
394 return FALSE;
395}
396
Mike McCormack5ea2a172006-06-06 14:21:13 +0900397static INT_PTR CALLBACK
Ferenc Wagner354f6622004-01-15 01:48:05 +0000398DlgProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
399{
400 switch (msg) {
401 case WM_INITDIALOG:
402 SendMessage (hwnd, WM_SETICON, ICON_SMALL,
403 (LPARAM)LoadIcon (GetModuleHandle (NULL),
404 MAKEINTRESOURCE (IDI_WINE)));
405 SendMessage (hwnd, WM_SETICON, ICON_BIG,
406 (LPARAM)LoadIcon (GetModuleHandle (NULL),
407 MAKEINTRESOURCE (IDI_WINE)));
408 dialog = hwnd;
409 if (!SetEvent (initEvent)) {
410 report (R_STATUS, "Can't signal main thread: %d",
411 GetLastError ());
412 EndDialog (hwnd, 2);
413 }
414 return TRUE;
415 case WM_CLOSE:
416 EndDialog (hwnd, 3);
417 return TRUE;
418 case WM_COMMAND:
419 switch (LOWORD (wParam)) {
420 case IDHELP:
421 DialogBox (GetModuleHandle (NULL),
422 MAKEINTRESOURCE (IDD_ABOUT), hwnd, AboutProc);
423 return TRUE;
424 case IDABORT:
425 report (R_WARNING, "Not implemented");
426 return TRUE;
427 }
428 }
429 return FALSE;
430}
431
Mike McCormackae511352005-06-02 15:11:32 +0000432static DWORD WINAPI
433DlgThreadProc (LPVOID param)
Ferenc Wagner354f6622004-01-15 01:48:05 +0000434{
435 int ret;
436
437 InitCommonControls ();
438 ret = DialogBox (GetModuleHandle (NULL),
439 MAKEINTRESOURCE (IDD_STATUS),
440 NULL, DlgProc);
441 switch (ret) {
442 case 0:
443 report (R_WARNING, "Invalid parent handle");
444 break;
445 case 1:
446 report (R_WARNING, "DialogBox failed: %d",
447 GetLastError ());
448 break;
449 case 3:
450 exit (0);
451 default:
452 report (R_STATUS, "Dialog exited: %d", ret);
453 }
454 return 0;
455}
456
457int
458report (enum report_type t, ...)
459{
460 typedef int r_fun_t (va_list);
461
462 va_list ap;
463 int ret = 0;
464 static r_fun_t * const text_funcs[] =
465 {textStatus, textProgress, textStep, textDelta,
Ferenc Wagner30dcc6b2005-03-31 15:22:41 +0000466 textTag, textDir, textOut,
Ferenc Wagner83877702004-03-19 01:54:10 +0000467 textWarning, textError, textFatal, textAsk};
Ferenc Wagner354f6622004-01-15 01:48:05 +0000468 static r_fun_t * const GUI_funcs[] =
469 {guiStatus, guiProgress, guiStep, guiDelta,
Ferenc Wagner30dcc6b2005-03-31 15:22:41 +0000470 guiTag, guiDir, guiOut,
Ferenc Wagner83877702004-03-19 01:54:10 +0000471 guiWarning, guiError, guiFatal, guiAsk};
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000472 static r_fun_t * const quiet_funcs[] =
473 {qNoOp, qNoOp, qNoOp, qNoOp,
Ferenc Wagner30dcc6b2005-03-31 15:22:41 +0000474 qNoOp, qNoOp, qNoOp,
Ferenc Wagner83877702004-03-19 01:54:10 +0000475 qNoOp, qNoOp, qFatal, qAsk};
Ferenc Wagner354f6622004-01-15 01:48:05 +0000476 static r_fun_t * const * funcs = NULL;
477
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000478 switch (t) {
479 case R_TEXTMODE:
480 funcs = text_funcs;
481 return 0;
482 case R_QUIET:
483 funcs = quiet_funcs;
484 return 0;
485 default:
Gerald Pfeifer3061e972004-02-20 01:09:31 +0000486 break;
Ferenc Wagnerefc67252004-02-19 04:12:42 +0000487 }
488
Ferenc Wagner354f6622004-01-15 01:48:05 +0000489 if (!funcs) {
490 HANDLE DlgThread;
491 DWORD DlgThreadID;
492
493 funcs = text_funcs;
494 initEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
495 if (!initEvent)
496 report (R_STATUS, "Can't create event object: %d",
497 GetLastError ());
498 else {
499 DlgThread = CreateThread (NULL, 0, DlgThreadProc,
500 NULL, 0, &DlgThreadID);
501 if (!DlgThread)
502 report (R_STATUS, "Can't create GUI thread: %d",
503 GetLastError ());
504 else {
505 DWORD ret = WaitForSingleObject (initEvent, INFINITE);
506 switch (ret) {
507 case WAIT_OBJECT_0:
508 funcs = GUI_funcs;
509 break;
510 case WAIT_TIMEOUT:
511 report (R_STATUS, "GUI creation timed out");
512 break;
513 case WAIT_FAILED:
514 report (R_STATUS, "Wait for GUI failed: %d",
515 GetLastError ());
516 break;
517 default:
518 report (R_STATUS, "Wait returned %d",
519 ret);
520 break;
521 }
522 }
523 }
524 }
525
526 va_start (ap, t);
527 if (t < sizeof text_funcs / sizeof text_funcs[0] &&
Ferenc Wagnerf0422092005-04-11 12:46:28 +0000528 t < sizeof GUI_funcs / sizeof GUI_funcs[0]) ret = funcs[t](ap);
Ferenc Wagner354f6622004-01-15 01:48:05 +0000529 else report (R_WARNING, "unimplemented report type: %d", t);
530 va_end (ap);
531 return ret;
532}