gui.c 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * GUI support
 *
 * Copyright 2004 Ferenc Wagner
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
19 20 21 22 23
 */

#include <windows.h>
#include <commctrl.h>

24
#include "resource.h"
25 26 27 28
#include "winetest.h"

/* Event object to signal successful window creation to main thread.
 */
29
static HANDLE initEvent;
30 31 32

/* Dialog handle
 */
33
static HWND dialog;
34 35 36

/* Progress data for the text* functions and for scaling.
 */
37 38
static unsigned int progressMax, progressCurr;
static double progressScale;
39 40 41

/* Progress group counter for the gui* functions.
 */
42
static int progressGroup;
43

44
static WNDPROC DefEditProc;
45

46
static int
47 48 49 50 51 52 53 54 55 56 57 58 59 60
MBdefault (int uType)
{
    static const int matrix[][4] = {{IDOK,    0,        0,        0},
                                    {IDOK,    IDCANCEL, 0,        0},
                                    {IDABORT, IDRETRY,  IDIGNORE, 0},
                                    {IDYES,   IDNO,     IDCANCEL, 0},
                                    {IDYES,   IDNO,     0,        0},
                                    {IDRETRY, IDCANCEL, 0,        0}};
    int type = uType & MB_TYPEMASK;
    int def  = (uType & MB_DEFMASK) / MB_DEFBUTTON2;

    return matrix[type][def];
}

61
/* report (R_STATUS, fmt, ...) */
62
static int
63 64 65 66 67 68
textStatus (va_list ap)
{
    char *str = vstrmake (NULL, ap);

    fputs (str, stderr);
    fputc ('\n', stderr);
69
    heap_free (str);
70 71 72
    return 0;
}

73
static int
74 75 76 77 78 79
guiStatus (va_list ap)
{
    size_t len;
    char *str = vstrmake (&len, ap);

    if (len > 128) str[129] = 0;
80
    SetDlgItemTextA (dialog, IDC_SB, str);
81
    heap_free (str);
82 83 84
    return 0;
}

85
/* report (R_PROGRESS, barnum, steps) */
86
static int
87 88
textProgress (va_list ap)
{
Ferenc Wagner's avatar
Ferenc Wagner committed
89
    progressGroup = va_arg (ap, int);
90 91 92 93 94
    progressMax = va_arg (ap, int);
    progressCurr = 0;
    return 0;
}

95
static int
96 97
guiProgress (va_list ap)
{
98 99
    unsigned int max;
    HWND pb;
100

101 102
    progressGroup = va_arg (ap, int);
    progressMax = max = va_arg (ap, int);
103 104 105 106 107 108
    progressCurr = 0;
    if (max > 0xffff) {
        progressScale = (double)0xffff / max;
        max = 0xffff;
    }
    else progressScale = 1;
109
    pb = GetDlgItem (dialog, IDC_PB0 + progressGroup * 2);
110 111
    SendMessageA(pb, PBM_SETRANGE, 0, MAKELPARAM (0, max));
    SendMessageA(pb, PBM_SETSTEP, 1, 0);
112 113 114 115
    return 0;
}

/* report (R_STEP, fmt, ...) */
116
static int
117 118 119 120 121 122 123
textStep (va_list ap)
{
    char *str = vstrmake (NULL, ap);

    progressCurr++;
    fputs (str, stderr);
    fprintf (stderr, " (%d of %d)\n", progressCurr, progressMax);
124
    heap_free (str);
125 126 127
    return 0;
}

128
static int
129 130 131 132 133 134
guiStep (va_list ap)
{
    const int pgID = IDC_ST0 + progressGroup * 2;
    char *str = vstrmake (NULL, ap);
    
    progressCurr++;
135 136
    SetDlgItemTextA (dialog, pgID, str);
    SendDlgItemMessageA(dialog, pgID+1, PBM_SETPOS,
137
                        progressScale * progressCurr, 0);
138
    heap_free (str);
139 140 141 142
    return 0;
}

/* report (R_DELTA, inc, fmt, ...) */
143
static int
144 145 146 147 148 149 150 151
textDelta (va_list ap)
{
    const int inc = va_arg (ap, int);
    char *str = vstrmake (NULL, ap);

    progressCurr += inc;
    fputs (str, stderr);
    fprintf (stderr, " (%d of %d)\n", progressCurr, progressMax);
152
    heap_free (str);
153 154 155
    return 0;
}

156
static int
157 158 159 160 161 162 163
guiDelta (va_list ap)
{
    const int inc = va_arg (ap, int);
    const int pgID = IDC_ST0 + progressGroup * 2;
    char *str = vstrmake (NULL, ap);

    progressCurr += inc;
164 165
    SetDlgItemTextA (dialog, pgID, str);
    SendDlgItemMessageA(dialog, pgID+1, PBM_SETPOS,
166
                        progressScale * progressCurr, 0);
167
    heap_free (str);
168 169 170
    return 0;
}

171
/* report (R_TAG) */
172
static int
173 174 175 176 177 178 179 180
textTag (va_list ap)
{
    fputs ("Tag: ", stderr);
    fputs (tag, stderr);
    fputc ('\n', stderr);
    return 0;
}

181
static int
182 183
guiTag (va_list ap)
{
184
    SetDlgItemTextA (dialog, IDC_TAG, tag);
185 186 187
    return 0;
}

188
/* report (R_DIR, fmt, ...) */
189
static int
190 191 192 193 194 195 196
textDir (va_list ap)
{
    char *str = vstrmake (NULL, ap);

    fputs ("Temporary directory: ", stderr);
    fputs (str, stderr);
    fputc ('\n', stderr);
197
    heap_free (str);
198 199 200
    return 0;
}

201
static int
202 203 204 205
guiDir (va_list ap)
{
    char *str = vstrmake (NULL, ap);

206
    SetDlgItemTextA (dialog, IDC_DIR, str);
207
    heap_free (str);
208 209 210 211
    return 0;
}

/* report (R_OUT, fmt, ...) */
212
static int
213 214 215 216 217 218 219
textOut (va_list ap)
{
    char *str = vstrmake (NULL, ap);

    fputs ("Log file: ", stderr);
    fputs (str, stderr);
    fputc ('\n', stderr);
220
    heap_free (str);
221 222 223
    return 0;
}

224
static int
225 226 227 228
guiOut (va_list ap)
{
    char *str = vstrmake (NULL, ap);

229
    SetDlgItemTextA (dialog, IDC_OUT, str);
230
    heap_free (str);
231 232 233
    return 0;
}

234
/* report (R_WARNING, fmt, ...) */
235
static int
236
textWarning (va_list ap)
237
{
238 239 240
    fputs ("Warning: ", stderr);
    textStatus (ap);
    return 0;
241 242
}

243
static int
244
guiWarning (va_list ap)
245 246 247
{
    char *str = vstrmake (NULL, ap);

248
    MessageBoxA (dialog, str, "Warning", MB_ICONWARNING | MB_OK);
249
    heap_free (str);
250
    return 0;
251 252
}

253
/* report (R_ERROR, fmt, ...) */
254
static int
255
textError (va_list ap)
256
{
257 258
    fputs ("Error: ", stderr);
    textStatus (ap);
259 260 261
    return 0;
}

262
static int
263
guiError (va_list ap)
264 265 266
{
    char *str = vstrmake (NULL, ap);

267
    MessageBoxA (dialog, str, "Error", MB_ICONERROR | MB_OK);
268
    heap_free (str);
269 270 271
    return 0;
}

272
/* report (R_FATAL, fmt, ...) */
273
static int
274 275 276 277 278 279
textFatal (va_list ap)
{
    textError (ap);
    exit (1);
}

280
static int
281 282 283 284 285 286
guiFatal (va_list ap)
{
    guiError (ap);
    exit (1);
}

287
/* report (R_ASK, type, fmt, ...) */
288
static int
289 290 291
textAsk (va_list ap)
{
    int uType = va_arg (ap, int);
292
    int ret = MBdefault (uType);
293 294
    char *str = vstrmake (NULL, ap);

295 296
    fprintf (stderr, "Question of type %d: %s\n"
             "Returning default: %d\n", uType, str, ret);
297
    heap_free (str);
298
    return ret;
299 300
}

301
static int
302 303 304 305
guiAsk (va_list ap)
{
    int uType = va_arg (ap, int);
    char *str = vstrmake (NULL, ap);
306
    int ret = MessageBoxA (dialog, str, "Question", MB_ICONQUESTION | uType);
307

308
    heap_free (str);
309 310 311
    return ret;
}

312
static BOOL CALLBACK
313 314 315 316 317 318 319 320 321 322 323 324
EditTagProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg) {
    case WM_CHAR:
        if (wParam == 8) break; /* backspace is OK */
        if (GetWindowTextLengthA (hwnd) == MAXTAGLEN ||
            !goodtagchar (wParam)) return TRUE;
        break;
    }
    return CallWindowProcA (DefEditProc, hwnd, msg, wParam, lParam);
}

325
static INT_PTR CALLBACK
326 327 328 329 330
AskTagProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int len;

    switch (msg) {
331
    case WM_INITDIALOG:
332
        DefEditProc = (WNDPROC)SetWindowLongPtrA
333 334
            (GetDlgItem (hwnd, IDC_TAG), GWLP_WNDPROC, (LONG_PTR)EditTagProc);
        return TRUE;
335 336 337 338
    case WM_COMMAND:
        switch (LOWORD (wParam)) {
        case IDOK:
            len = GetWindowTextLengthA (GetDlgItem (hwnd, IDC_TAG));
339 340 341 342
	    if(!len) {
               report (R_WARNING, "You must enter a tag to continue");
               return FALSE;
            }
343
            tag = heap_alloc (len+1);
344 345
            GetDlgItemTextA (hwnd, IDC_TAG, tag, len+1);
            EndDialog (hwnd, IDOK);
346 347 348 349 350 351 352 353 354 355 356 357
            return TRUE;
        case IDABORT:
            EndDialog (hwnd, IDABORT);
            return TRUE;
        }
    }
    return FALSE;
}

int
guiAskTag (void)
{
358
    return DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_TAG), dialog, AskTagProc);
359 360
}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
static INT_PTR CALLBACK
AskEmailProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int len;

    switch (msg)
    {
    case WM_COMMAND:
        switch (LOWORD (wParam)) {
        case IDOK:
	    len = GetWindowTextLengthA (GetDlgItem (hwnd, IDC_EMAIL));
	    if(!len) {
               report (R_WARNING, "You must enter an email address to continue");
               return FALSE;
            }
            email = heap_alloc (len+1);
            GetDlgItemTextA (hwnd, IDC_EMAIL, email, len+1);
            EndDialog (hwnd, IDOK);
            return TRUE;
        case IDABORT:
            EndDialog (hwnd, IDABORT);
            return TRUE;
        }
    }
    return FALSE;
}

int
guiAskEmail (void)
{
391
    return DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_EMAIL), dialog, AskEmailProc);
392 393
}

394
/* Quiet functions */
395
static int
396 397 398 399 400
qNoOp (va_list ap)
{
    return 0;
}

401
static int
402 403 404 405 406
qAsk (va_list ap)
{
    return MBdefault (va_arg (ap, int));
}

407
static INT_PTR CALLBACK
408 409 410 411 412 413 414 415 416 417 418 419 420
AboutProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg) {
    case WM_COMMAND:
        switch (LOWORD (wParam)) {
        case IDCANCEL:
            EndDialog (hwnd, IDCANCEL);
            return TRUE;
        }
    }
    return FALSE;
}

421
static INT_PTR CALLBACK
422 423 424 425
DlgProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg) {
    case WM_INITDIALOG:
426 427 428 429 430 431
        SendMessageA(hwnd, WM_SETICON, ICON_SMALL,
                     (LPARAM)LoadImageA( GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDI_WINE), IMAGE_ICON,
                                         GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
                                         LR_SHARED ));
        SendMessageA(hwnd, WM_SETICON, ICON_BIG,
                     (LPARAM)LoadIconA( GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDI_WINE)));
432 433 434 435 436 437 438 439 440 441 442 443 444
        dialog = hwnd;
        if (!SetEvent (initEvent)) {
            report (R_STATUS, "Can't signal main thread: %d",
                    GetLastError ());
            EndDialog (hwnd, 2);
        }
        return TRUE;
    case WM_CLOSE:
        EndDialog (hwnd, 3);
        return TRUE;
    case WM_COMMAND:
        switch (LOWORD (wParam)) {
        case IDHELP:
445
            DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_ABOUT), hwnd, AboutProc);
446 447
            return TRUE;
        case IDABORT:
448 449
            report (R_STATUS, "Aborting, please wait...");
            aborting = TRUE;
450 451 452 453 454 455
            return TRUE;
        }
    }
    return FALSE;
}

456 457
static DWORD WINAPI
DlgThreadProc (LPVOID param)
458 459 460 461
{
    int ret;

    InitCommonControls ();
462
    ret = DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_STATUS), NULL, DlgProc);
463 464
    switch (ret) {
    case 0:
465
        report (R_FATAL, "Cannot display dialog");
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
        break;
    case 1:
        report (R_WARNING, "DialogBox failed: %d",
                GetLastError ());
        break;
    case 3:
        exit (0);
    default:
        report (R_STATUS, "Dialog exited: %d", ret);
    }
    return 0;
}

int
report (enum report_type t, ...)
{
    typedef int r_fun_t (va_list);

    va_list ap;
    int ret = 0;
    static r_fun_t * const text_funcs[] =
        {textStatus, textProgress, textStep, textDelta,
488
         textTag, textDir, textOut,
Ferenc Wagner's avatar
Ferenc Wagner committed
489
         textWarning, textError, textFatal, textAsk};
490 491
    static r_fun_t * const GUI_funcs[] =
        {guiStatus, guiProgress, guiStep, guiDelta,
492
         guiTag, guiDir, guiOut,
Ferenc Wagner's avatar
Ferenc Wagner committed
493
         guiWarning, guiError, guiFatal, guiAsk};
494 495
    static r_fun_t * const quiet_funcs[] =
        {qNoOp, qNoOp, qNoOp, qNoOp,
496
         qNoOp, qNoOp, qNoOp,
497
         qNoOp, textError, textFatal, qAsk};
498 499
    static r_fun_t * const * funcs = NULL;

500 501 502 503 504 505 506 507
    switch (t) {
    case R_TEXTMODE:
        funcs = text_funcs;
        return 0;
    case R_QUIET:
        funcs = quiet_funcs;
        return 0;
    default:
508
        break;
509 510
    }

511 512 513 514 515
    if (!funcs) {
        HANDLE DlgThread;
        DWORD DlgThreadID;

        funcs = text_funcs;
516
        initEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
517 518 519 520 521 522 523 524 525 526 527
        if (!initEvent)
            report (R_STATUS, "Can't create event object: %d",
                    GetLastError ());
        else {
            DlgThread = CreateThread (NULL, 0, DlgThreadProc,
                                      NULL, 0, &DlgThreadID);
            if (!DlgThread)
                report (R_STATUS, "Can't create GUI thread: %d",
                        GetLastError ());
            else {
                DWORD ret = WaitForSingleObject (initEvent, INFINITE);
528
                CloseHandle(DlgThread);
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
                switch (ret) {
                case WAIT_OBJECT_0:
                    funcs = GUI_funcs;
                    break;
                case WAIT_TIMEOUT:
                    report (R_STATUS, "GUI creation timed out");
                    break;
                case WAIT_FAILED:
                    report (R_STATUS, "Wait for GUI failed: %d",
                            GetLastError ());
                    break;
                default:
                    report (R_STATUS, "Wait returned %d",
                            ret);
                    break;
                }
            }
        }
    }
        
    va_start (ap, t);
550
    if (t < sizeof text_funcs / sizeof text_funcs[0]) ret = funcs[t](ap);
551 552 553 554
    else report (R_WARNING, "unimplemented report type: %d", t);
    va_end (ap);
    return ret;
}