gui.c 12.1 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 69 70 71 72
textStatus (va_list ap)
{
    char *str = vstrmake (NULL, ap);

    fputs (str, stderr);
    fputc ('\n', stderr);
    free (str);
    return 0;
}

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

    if (len > 128) str[129] = 0;
    SetDlgItemText (dialog, IDC_SB, str);
    free (str);
    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 112 113 114 115
    SendMessage (pb, PBM_SETRANGE, 0, MAKELPARAM (0, max));
    SendMessage (pb, PBM_SETSTEP, (WPARAM)1, 0);
    return 0;
}

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

    progressCurr++;
    fputs (str, stderr);
    fprintf (stderr, " (%d of %d)\n", progressCurr, progressMax);
    free (str);
    return 0;
}

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

/* report (R_DELTA, inc, fmt, ...) */
143
static int
144 145 146 147 148 149 150 151 152 153 154 155
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);
    free (str);
    return 0;
}

156
static int
157 158 159 160 161 162 163 164 165 166 167 168 169 170
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;
    SetDlgItemText (dialog, pgID, str);
    SendDlgItemMessage (dialog, pgID+1, PBM_SETPOS,
                        (WPARAM)(progressScale * progressCurr), 0);
    free (str);
    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 184 185 186 187
guiTag (va_list ap)
{
    SetDlgItemText (dialog, IDC_TAG, tag);
    return 0;
}

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

    fputs ("Temporary directory: ", stderr);
    fputs (str, stderr);
    fputc ('\n', stderr);
    free (str);
    return 0;
}

201
static int
202 203 204 205 206 207 208 209 210 211
guiDir (va_list ap)
{
    char *str = vstrmake (NULL, ap);

    SetDlgItemText (dialog, IDC_DIR, str);
    free (str);
    return 0;
}

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

    fputs ("Log file: ", stderr);
    fputs (str, stderr);
    fputc ('\n', stderr);
    free (str);
    return 0;
}

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

    SetDlgItemText (dialog, IDC_OUT, str);
    free (str);
    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
    MessageBox (dialog, str, "Warning", MB_ICONWARNING | MB_OK);
249
    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
    MessageBox (dialog, str, "Error", MB_ICONERROR | MB_OK);
268 269 270 271
    free (str);
    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
    free (str);
298
    return ret;
299 300
}

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

    free (str);
    return ret;
}

313
static BOOL CALLBACK
314 315 316 317 318 319 320 321 322 323 324 325
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);
}

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

    switch (msg) {
332 333 334 335
    case WM_INITDIALOG:
        DefEditProc = (WNDPROC)SetWindowLongPtr
            (GetDlgItem (hwnd, IDC_TAG), GWLP_WNDPROC, (LONG_PTR)EditTagProc);
        return TRUE;
336 337 338 339
    case WM_COMMAND:
        switch (LOWORD (wParam)) {
        case IDOK:
            len = GetWindowTextLengthA (GetDlgItem (hwnd, IDC_TAG));
340 341 342
            tag = xmalloc (len+1);
            GetDlgItemTextA (hwnd, IDC_TAG, tag, len+1);
            EndDialog (hwnd, IDOK);
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
            return TRUE;
        case IDABORT:
            EndDialog (hwnd, IDABORT);
            return TRUE;
        }
    }
    return FALSE;
}

int
guiAskTag (void)
{
    return DialogBox (GetModuleHandle (NULL),
                      MAKEINTRESOURCE (IDD_TAG),
                      dialog, AskTagProc);
}

360
/* Quiet functions */
361
static int
362 363 364 365 366
qNoOp (va_list ap)
{
    return 0;
}

367
static int
368 369 370 371 372
qFatal (va_list ap)
{
    exit (1);
}

373
static int
374 375 376 377 378
qAsk (va_list ap)
{
    return MBdefault (va_arg (ap, int));
}

379
static INT_PTR CALLBACK
380 381 382 383 384 385 386 387 388 389 390 391 392
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;
}

393
static INT_PTR CALLBACK
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
DlgProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg) {
    case WM_INITDIALOG:
        SendMessage (hwnd, WM_SETICON, ICON_SMALL,
                     (LPARAM)LoadIcon (GetModuleHandle (NULL),
                                       MAKEINTRESOURCE (IDI_WINE)));
        SendMessage (hwnd, WM_SETICON, ICON_BIG,
                     (LPARAM)LoadIcon (GetModuleHandle (NULL),
                                       MAKEINTRESOURCE (IDI_WINE)));
        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:
            DialogBox (GetModuleHandle (NULL),
                       MAKEINTRESOURCE (IDD_ABOUT), hwnd, AboutProc);
            return TRUE;
        case IDABORT:
            report (R_WARNING, "Not implemented");
            return TRUE;
        }
    }
    return FALSE;
}

428 429
static DWORD WINAPI
DlgThreadProc (LPVOID param)
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
{
    int ret;

    InitCommonControls ();
    ret = DialogBox (GetModuleHandle (NULL),
                     MAKEINTRESOURCE (IDD_STATUS),
                     NULL, DlgProc);
    switch (ret) {
    case 0:
        report (R_WARNING, "Invalid parent handle");
        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,
462
         textTag, textDir, textOut,
Ferenc Wagner's avatar
Ferenc Wagner committed
463
         textWarning, textError, textFatal, textAsk};
464 465
    static r_fun_t * const GUI_funcs[] =
        {guiStatus, guiProgress, guiStep, guiDelta,
466
         guiTag, guiDir, guiOut,
Ferenc Wagner's avatar
Ferenc Wagner committed
467
         guiWarning, guiError, guiFatal, guiAsk};
468 469
    static r_fun_t * const quiet_funcs[] =
        {qNoOp, qNoOp, qNoOp, qNoOp,
470
         qNoOp, qNoOp, qNoOp,
Ferenc Wagner's avatar
Ferenc Wagner committed
471
         qNoOp, qNoOp, qFatal, qAsk};
472 473
    static r_fun_t * const * funcs = NULL;

474 475 476 477 478 479 480 481
    switch (t) {
    case R_TEXTMODE:
        funcs = text_funcs;
        return 0;
    case R_QUIET:
        funcs = quiet_funcs;
        return 0;
    default:
482
        break;
483 484
    }

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
    if (!funcs) {
        HANDLE DlgThread;
        DWORD DlgThreadID;

        funcs = text_funcs;
        initEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
        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);
                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);
    if (t < sizeof text_funcs / sizeof text_funcs[0] &&
524
        t < sizeof GUI_funcs / sizeof GUI_funcs[0]) ret = funcs[t](ap);
525 526 527 528
    else report (R_WARNING, "unimplemented report type: %d", t);
    va_end (ap);
    return ret;
}