123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- /*
- * 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
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
- */
- #include <windows.h>
- #include <commctrl.h>
- #include "resource.h"
- #include "winetest.h"
- /* Event object to signal successful window creation to main thread.
- */
- static HANDLE initEvent;
- /* Dialog handle
- */
- static HWND dialog;
- /* Progress data for the text* functions and for scaling.
- */
- static unsigned int progressMax, progressCurr;
- static double progressScale;
- /* Progress group counter for the gui* functions.
- */
- static int progressGroup;
- static WNDPROC DefEditProc;
- static int
- 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];
- }
- /* report (R_STATUS, fmt, ...) */
- static int
- textStatus (__ms_va_list ap)
- {
- char *str = vstrmake (NULL, ap);
- fputs (str, stderr);
- fputc ('\n', stderr);
- heap_free (str);
- return 0;
- }
- static int
- guiStatus (__ms_va_list ap)
- {
- size_t len;
- char *str = vstrmake (&len, ap);
- if (len > 128) str[129] = 0;
- SetDlgItemTextA (dialog, IDC_SB, str);
- heap_free (str);
- return 0;
- }
- /* report (R_PROGRESS, barnum, steps) */
- static int
- textProgress (__ms_va_list ap)
- {
- progressGroup = va_arg (ap, int);
- progressMax = va_arg (ap, int);
- progressCurr = 0;
- return 0;
- }
- static int
- guiProgress (__ms_va_list ap)
- {
- unsigned int max;
- HWND pb;
- progressGroup = va_arg (ap, int);
- progressMax = max = va_arg (ap, int);
- progressCurr = 0;
- if (max > 0xffff) {
- progressScale = (double)0xffff / max;
- max = 0xffff;
- }
- else progressScale = 1;
- pb = GetDlgItem (dialog, IDC_PB0 + progressGroup * 2);
- SendMessageA(pb, PBM_SETRANGE, 0, MAKELPARAM (0, max));
- SendMessageA(pb, PBM_SETSTEP, 1, 0);
- return 0;
- }
- /* report (R_STEP, fmt, ...) */
- static int
- textStep (__ms_va_list ap)
- {
- char *str = vstrmake (NULL, ap);
- progressCurr++;
- fputs (str, stderr);
- fprintf (stderr, " (%d of %d)\n", progressCurr, progressMax);
- heap_free (str);
- return 0;
- }
- static int
- guiStep (__ms_va_list ap)
- {
- const int pgID = IDC_ST0 + progressGroup * 2;
- char *str = vstrmake (NULL, ap);
-
- progressCurr++;
- SetDlgItemTextA (dialog, pgID, str);
- SendDlgItemMessageA(dialog, pgID+1, PBM_SETPOS,
- progressScale * progressCurr, 0);
- heap_free (str);
- return 0;
- }
- /* report (R_DELTA, inc, fmt, ...) */
- static int
- textDelta (__ms_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);
- heap_free (str);
- return 0;
- }
- static int
- guiDelta (__ms_va_list ap)
- {
- const int inc = va_arg (ap, int);
- const int pgID = IDC_ST0 + progressGroup * 2;
- char *str = vstrmake (NULL, ap);
- progressCurr += inc;
- SetDlgItemTextA (dialog, pgID, str);
- SendDlgItemMessageA(dialog, pgID+1, PBM_SETPOS,
- progressScale * progressCurr, 0);
- heap_free (str);
- return 0;
- }
- /* report (R_TAG) */
- static int
- textTag (__ms_va_list ap)
- {
- fputs ("Tag: ", stderr);
- fputs (tag, stderr);
- fputc ('\n', stderr);
- return 0;
- }
- static int
- guiTag (__ms_va_list ap)
- {
- SetDlgItemTextA (dialog, IDC_TAG, tag);
- return 0;
- }
- /* report (R_DIR, fmt, ...) */
- static int
- textDir (__ms_va_list ap)
- {
- char *str = vstrmake (NULL, ap);
- fputs ("Temporary directory: ", stderr);
- fputs (str, stderr);
- fputc ('\n', stderr);
- heap_free (str);
- return 0;
- }
- static int
- guiDir (__ms_va_list ap)
- {
- char *str = vstrmake (NULL, ap);
- SetDlgItemTextA (dialog, IDC_DIR, str);
- heap_free (str);
- return 0;
- }
- /* report (R_OUT, fmt, ...) */
- static int
- textOut (__ms_va_list ap)
- {
- char *str = vstrmake (NULL, ap);
- fputs ("Log file: ", stderr);
- fputs (str, stderr);
- fputc ('\n', stderr);
- heap_free (str);
- return 0;
- }
- static int
- guiOut (__ms_va_list ap)
- {
- char *str = vstrmake (NULL, ap);
- SetDlgItemTextA (dialog, IDC_OUT, str);
- heap_free (str);
- return 0;
- }
- /* report (R_WARNING, fmt, ...) */
- static int
- textWarning (__ms_va_list ap)
- {
- fputs ("Warning: ", stderr);
- textStatus (ap);
- return 0;
- }
- static int
- guiWarning (__ms_va_list ap)
- {
- char *str = vstrmake (NULL, ap);
- MessageBoxA (dialog, str, "Warning", MB_ICONWARNING | MB_OK);
- heap_free (str);
- return 0;
- }
- /* report (R_ERROR, fmt, ...) */
- static int
- textError (__ms_va_list ap)
- {
- fputs ("Error: ", stderr);
- textStatus (ap);
- return 0;
- }
- static int
- guiError (__ms_va_list ap)
- {
- char *str = vstrmake (NULL, ap);
- MessageBoxA (dialog, str, "Error", MB_ICONERROR | MB_OK);
- heap_free (str);
- return 0;
- }
- /* report (R_FATAL, fmt, ...) */
- static int
- textFatal (__ms_va_list ap)
- {
- textError (ap);
- exit (1);
- return 0;
- }
- static int
- guiFatal (__ms_va_list ap)
- {
- guiError (ap);
- exit (1);
- return 0;
- }
- /* report (R_ASK, type, fmt, ...) */
- static int
- textAsk (__ms_va_list ap)
- {
- int uType = va_arg (ap, int);
- int ret = MBdefault (uType);
- char *str = vstrmake (NULL, ap);
- fprintf (stderr, "Question of type %d: %s\n"
- "Returning default: %d\n", uType, str, ret);
- heap_free (str);
- return ret;
- }
- static int
- guiAsk (__ms_va_list ap)
- {
- int uType = va_arg (ap, int);
- char *str = vstrmake (NULL, ap);
- int ret = MessageBoxA (dialog, str, "Question", MB_ICONQUESTION | uType);
- heap_free (str);
- return ret;
- }
- static BOOL CALLBACK
- 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);
- }
- static INT_PTR CALLBACK
- AskTagProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- int len;
- switch (msg) {
- case WM_INITDIALOG:
- DefEditProc = (WNDPROC)SetWindowLongPtrA
- (GetDlgItem (hwnd, IDC_TAG), GWLP_WNDPROC, (LONG_PTR)EditTagProc);
- return TRUE;
- case WM_COMMAND:
- switch (LOWORD (wParam)) {
- case IDOK:
- len = GetWindowTextLengthA (GetDlgItem (hwnd, IDC_TAG));
- if(!len) {
- report (R_WARNING, "You must enter a tag to continue");
- return FALSE;
- }
- tag = heap_alloc (len+1);
- GetDlgItemTextA (hwnd, IDC_TAG, tag, len+1);
- EndDialog (hwnd, IDOK);
- return TRUE;
- case IDABORT:
- EndDialog (hwnd, IDABORT);
- return TRUE;
- }
- }
- return FALSE;
- }
- int
- guiAskTag (void)
- {
- return DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_TAG), dialog, AskTagProc);
- }
- 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)
- {
- return DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_EMAIL), dialog, AskEmailProc);
- }
- /* Quiet functions */
- static int
- qNoOp (__ms_va_list ap)
- {
- return 0;
- }
- static int
- qAsk (__ms_va_list ap)
- {
- return MBdefault (va_arg (ap, int));
- }
- static INT_PTR CALLBACK
- 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;
- }
- static INT_PTR CALLBACK
- DlgProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch (msg) {
- case WM_INITDIALOG:
- 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)));
- 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:
- DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_ABOUT), hwnd, AboutProc);
- return TRUE;
- case IDABORT:
- report (R_STATUS, "Aborting, please wait...");
- aborting = TRUE;
- return TRUE;
- }
- }
- return FALSE;
- }
- static DWORD WINAPI
- DlgThreadProc (LPVOID param)
- {
- int ret;
- InitCommonControls ();
- ret = DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_STATUS), NULL, DlgProc);
- switch (ret) {
- case 0:
- report (R_FATAL, "Cannot display dialog");
- 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 WINAPIV report (enum report_type t, ...)
- {
- typedef int r_fun_t (__ms_va_list);
- __ms_va_list ap;
- int ret = 0;
- static r_fun_t * const text_funcs[] =
- {textStatus, textProgress, textStep, textDelta,
- textTag, textDir, textOut,
- textWarning, textError, textFatal, textAsk};
- static r_fun_t * const GUI_funcs[] =
- {guiStatus, guiProgress, guiStep, guiDelta,
- guiTag, guiDir, guiOut,
- guiWarning, guiError, guiFatal, guiAsk};
- static r_fun_t * const quiet_funcs[] =
- {qNoOp, qNoOp, qNoOp, qNoOp,
- qNoOp, qNoOp, qNoOp,
- qNoOp, textError, textFatal, qAsk};
- static r_fun_t * const * funcs = NULL;
- switch (t) {
- case R_TEXTMODE:
- funcs = text_funcs;
- return 0;
- case R_QUIET:
- funcs = quiet_funcs;
- return 0;
- default:
- break;
- }
- if (!funcs) {
- HANDLE DlgThread;
- DWORD DlgThreadID;
- funcs = text_funcs;
- initEvent = CreateEventA(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);
- CloseHandle(DlgThread);
- 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;
- }
- }
- }
- }
- __ms_va_start (ap, t);
- if (t < ARRAY_SIZE(text_funcs)) ret = funcs[t](ap);
- else report (R_WARNING, "unimplemented report type: %d", t);
- __ms_va_end (ap);
- return ret;
- }
|