gui.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  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
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include <windows.h>
  21. #include <commctrl.h>
  22. #include "resource.h"
  23. #include "winetest.h"
  24. /* Event object to signal successful window creation to main thread.
  25. */
  26. static HANDLE initEvent;
  27. /* Dialog handle
  28. */
  29. static HWND dialog;
  30. /* Progress data for the text* functions and for scaling.
  31. */
  32. static unsigned int progressMax, progressCurr;
  33. static double progressScale;
  34. /* Progress group counter for the gui* functions.
  35. */
  36. static int progressGroup;
  37. static WNDPROC DefEditProc;
  38. static int
  39. MBdefault (int uType)
  40. {
  41. static const int matrix[][4] = {{IDOK, 0, 0, 0},
  42. {IDOK, IDCANCEL, 0, 0},
  43. {IDABORT, IDRETRY, IDIGNORE, 0},
  44. {IDYES, IDNO, IDCANCEL, 0},
  45. {IDYES, IDNO, 0, 0},
  46. {IDRETRY, IDCANCEL, 0, 0}};
  47. int type = uType & MB_TYPEMASK;
  48. int def = (uType & MB_DEFMASK) / MB_DEFBUTTON2;
  49. return matrix[type][def];
  50. }
  51. /* report (R_STATUS, fmt, ...) */
  52. static int
  53. textStatus (va_list ap)
  54. {
  55. char *str = vstrmake (NULL, ap);
  56. fputs (str, stderr);
  57. fputc ('\n', stderr);
  58. heap_free (str);
  59. return 0;
  60. }
  61. static int
  62. guiStatus (va_list ap)
  63. {
  64. size_t len;
  65. char *str = vstrmake (&len, ap);
  66. if (len > 128) str[129] = 0;
  67. SetDlgItemTextA (dialog, IDC_SB, str);
  68. heap_free (str);
  69. return 0;
  70. }
  71. /* report (R_PROGRESS, barnum, steps) */
  72. static int
  73. textProgress (va_list ap)
  74. {
  75. progressGroup = va_arg (ap, int);
  76. progressMax = va_arg (ap, int);
  77. progressCurr = 0;
  78. return 0;
  79. }
  80. static int
  81. guiProgress (va_list ap)
  82. {
  83. unsigned int max;
  84. HWND pb;
  85. progressGroup = va_arg (ap, int);
  86. progressMax = max = va_arg (ap, int);
  87. progressCurr = 0;
  88. if (max > 0xffff) {
  89. progressScale = (double)0xffff / max;
  90. max = 0xffff;
  91. }
  92. else progressScale = 1;
  93. pb = GetDlgItem (dialog, IDC_PB0 + progressGroup * 2);
  94. SendMessageA(pb, PBM_SETRANGE, 0, MAKELPARAM (0, max));
  95. SendMessageA(pb, PBM_SETSTEP, 1, 0);
  96. return 0;
  97. }
  98. /* report (R_STEP, fmt, ...) */
  99. static int
  100. textStep (va_list ap)
  101. {
  102. char *str = vstrmake (NULL, ap);
  103. progressCurr++;
  104. fputs (str, stderr);
  105. fprintf (stderr, " (%d of %d)\n", progressCurr, progressMax);
  106. heap_free (str);
  107. return 0;
  108. }
  109. static int
  110. guiStep (va_list ap)
  111. {
  112. const int pgID = IDC_ST0 + progressGroup * 2;
  113. char *str = vstrmake (NULL, ap);
  114. progressCurr++;
  115. SetDlgItemTextA (dialog, pgID, str);
  116. SendDlgItemMessageA(dialog, pgID+1, PBM_SETPOS,
  117. progressScale * progressCurr, 0);
  118. heap_free (str);
  119. return 0;
  120. }
  121. /* report (R_DELTA, inc, fmt, ...) */
  122. static int
  123. textDelta (va_list ap)
  124. {
  125. const int inc = va_arg (ap, int);
  126. char *str = vstrmake (NULL, ap);
  127. progressCurr += inc;
  128. fputs (str, stderr);
  129. fprintf (stderr, " (%d of %d)\n", progressCurr, progressMax);
  130. heap_free (str);
  131. return 0;
  132. }
  133. static int
  134. guiDelta (va_list ap)
  135. {
  136. const int inc = va_arg (ap, int);
  137. const int pgID = IDC_ST0 + progressGroup * 2;
  138. char *str = vstrmake (NULL, ap);
  139. progressCurr += inc;
  140. SetDlgItemTextA (dialog, pgID, str);
  141. SendDlgItemMessageA(dialog, pgID+1, PBM_SETPOS,
  142. progressScale * progressCurr, 0);
  143. heap_free (str);
  144. return 0;
  145. }
  146. /* report (R_TAG) */
  147. static int
  148. textTag (va_list ap)
  149. {
  150. fputs ("Tag: ", stderr);
  151. fputs (tag, stderr);
  152. fputc ('\n', stderr);
  153. return 0;
  154. }
  155. static int
  156. guiTag (va_list ap)
  157. {
  158. SetDlgItemTextA (dialog, IDC_TAG, tag);
  159. return 0;
  160. }
  161. /* report (R_DIR, fmt, ...) */
  162. static int
  163. textDir (va_list ap)
  164. {
  165. char *str = vstrmake (NULL, ap);
  166. fputs ("Temporary directory: ", stderr);
  167. fputs (str, stderr);
  168. fputc ('\n', stderr);
  169. heap_free (str);
  170. return 0;
  171. }
  172. static int
  173. guiDir (va_list ap)
  174. {
  175. char *str = vstrmake (NULL, ap);
  176. SetDlgItemTextA (dialog, IDC_DIR, str);
  177. heap_free (str);
  178. return 0;
  179. }
  180. /* report (R_OUT, fmt, ...) */
  181. static int
  182. textOut (va_list ap)
  183. {
  184. char *str = vstrmake (NULL, ap);
  185. fputs ("Log file: ", stderr);
  186. fputs (str, stderr);
  187. fputc ('\n', stderr);
  188. heap_free (str);
  189. return 0;
  190. }
  191. static int
  192. guiOut (va_list ap)
  193. {
  194. char *str = vstrmake (NULL, ap);
  195. SetDlgItemTextA (dialog, IDC_OUT, str);
  196. heap_free (str);
  197. return 0;
  198. }
  199. /* report (R_WARNING, fmt, ...) */
  200. static int
  201. textWarning (va_list ap)
  202. {
  203. fputs ("Warning: ", stderr);
  204. textStatus (ap);
  205. return 0;
  206. }
  207. static int
  208. guiWarning (va_list ap)
  209. {
  210. char *str = vstrmake (NULL, ap);
  211. MessageBoxA (dialog, str, "Warning", MB_ICONWARNING | MB_OK);
  212. heap_free (str);
  213. return 0;
  214. }
  215. /* report (R_ERROR, fmt, ...) */
  216. static int
  217. textError (va_list ap)
  218. {
  219. fputs ("Error: ", stderr);
  220. textStatus (ap);
  221. return 0;
  222. }
  223. static int
  224. guiError (va_list ap)
  225. {
  226. char *str = vstrmake (NULL, ap);
  227. MessageBoxA (dialog, str, "Error", MB_ICONERROR | MB_OK);
  228. heap_free (str);
  229. return 0;
  230. }
  231. /* report (R_FATAL, fmt, ...) */
  232. static int
  233. textFatal (va_list ap)
  234. {
  235. textError (ap);
  236. exit (1);
  237. return 0;
  238. }
  239. static int
  240. guiFatal (va_list ap)
  241. {
  242. guiError (ap);
  243. exit (1);
  244. return 0;
  245. }
  246. /* report (R_ASK, type, fmt, ...) */
  247. static int
  248. textAsk (va_list ap)
  249. {
  250. int uType = va_arg (ap, int);
  251. int ret = MBdefault (uType);
  252. char *str = vstrmake (NULL, ap);
  253. fprintf (stderr, "Question of type %d: %s\n"
  254. "Returning default: %d\n", uType, str, ret);
  255. heap_free (str);
  256. return ret;
  257. }
  258. static int
  259. guiAsk (va_list ap)
  260. {
  261. int uType = va_arg (ap, int);
  262. char *str = vstrmake (NULL, ap);
  263. int ret = MessageBoxA (dialog, str, "Question", MB_ICONQUESTION | uType);
  264. heap_free (str);
  265. return ret;
  266. }
  267. static BOOL CALLBACK
  268. EditTagProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  269. {
  270. switch (msg) {
  271. case WM_CHAR:
  272. if (wParam == 8) break; /* backspace is OK */
  273. if (GetWindowTextLengthA (hwnd) == MAXTAGLEN ||
  274. !goodtagchar (wParam)) return TRUE;
  275. break;
  276. }
  277. return CallWindowProcA (DefEditProc, hwnd, msg, wParam, lParam);
  278. }
  279. static INT_PTR CALLBACK
  280. AskTagProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  281. {
  282. int len;
  283. switch (msg) {
  284. case WM_INITDIALOG:
  285. DefEditProc = (WNDPROC)SetWindowLongPtrA
  286. (GetDlgItem (hwnd, IDC_TAG), GWLP_WNDPROC, (LONG_PTR)EditTagProc);
  287. return TRUE;
  288. case WM_COMMAND:
  289. switch (LOWORD (wParam)) {
  290. case IDOK:
  291. len = GetWindowTextLengthA (GetDlgItem (hwnd, IDC_TAG));
  292. if(!len) {
  293. report (R_WARNING, "You must enter a tag to continue");
  294. return FALSE;
  295. }
  296. tag = heap_alloc (len+1);
  297. GetDlgItemTextA (hwnd, IDC_TAG, tag, len+1);
  298. EndDialog (hwnd, IDOK);
  299. return TRUE;
  300. case IDABORT:
  301. EndDialog (hwnd, IDABORT);
  302. return TRUE;
  303. }
  304. }
  305. return FALSE;
  306. }
  307. int
  308. guiAskTag (void)
  309. {
  310. return DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_TAG), dialog, AskTagProc);
  311. }
  312. static INT_PTR CALLBACK
  313. AskEmailProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  314. {
  315. int len;
  316. switch (msg)
  317. {
  318. case WM_COMMAND:
  319. switch (LOWORD (wParam)) {
  320. case IDOK:
  321. len = GetWindowTextLengthA (GetDlgItem (hwnd, IDC_EMAIL));
  322. if(!len) {
  323. report (R_WARNING, "You must enter an email address to continue");
  324. return FALSE;
  325. }
  326. email = heap_alloc (len+1);
  327. GetDlgItemTextA (hwnd, IDC_EMAIL, email, len+1);
  328. EndDialog (hwnd, IDOK);
  329. return TRUE;
  330. case IDABORT:
  331. EndDialog (hwnd, IDABORT);
  332. return TRUE;
  333. }
  334. }
  335. return FALSE;
  336. }
  337. int
  338. guiAskEmail (void)
  339. {
  340. return DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_EMAIL), dialog, AskEmailProc);
  341. }
  342. /* Quiet functions */
  343. static int
  344. qNoOp (va_list ap)
  345. {
  346. return 0;
  347. }
  348. static int
  349. qAsk (va_list ap)
  350. {
  351. return MBdefault (va_arg (ap, int));
  352. }
  353. static INT_PTR CALLBACK
  354. AboutProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  355. {
  356. switch (msg) {
  357. case WM_COMMAND:
  358. switch (LOWORD (wParam)) {
  359. case IDCANCEL:
  360. EndDialog (hwnd, IDCANCEL);
  361. return TRUE;
  362. }
  363. }
  364. return FALSE;
  365. }
  366. static INT_PTR CALLBACK
  367. DlgProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  368. {
  369. switch (msg) {
  370. case WM_INITDIALOG:
  371. SendMessageA(hwnd, WM_SETICON, ICON_SMALL,
  372. (LPARAM)LoadImageA( GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDI_WINE), IMAGE_ICON,
  373. GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON),
  374. LR_SHARED ));
  375. SendMessageA(hwnd, WM_SETICON, ICON_BIG,
  376. (LPARAM)LoadIconA( GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDI_WINE)));
  377. dialog = hwnd;
  378. if (!SetEvent (initEvent)) {
  379. report (R_STATUS, "Can't signal main thread: %d",
  380. GetLastError ());
  381. EndDialog (hwnd, 2);
  382. }
  383. return TRUE;
  384. case WM_CLOSE:
  385. EndDialog (hwnd, 3);
  386. return TRUE;
  387. case WM_COMMAND:
  388. switch (LOWORD (wParam)) {
  389. case IDHELP:
  390. DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_ABOUT), hwnd, AboutProc);
  391. return TRUE;
  392. case IDABORT:
  393. report (R_STATUS, "Aborting, please wait...");
  394. aborting = TRUE;
  395. return TRUE;
  396. }
  397. }
  398. return FALSE;
  399. }
  400. static DWORD WINAPI
  401. DlgThreadProc (LPVOID param)
  402. {
  403. int ret;
  404. InitCommonControls ();
  405. ret = DialogBoxA(GetModuleHandleA(NULL), MAKEINTRESOURCEA(IDD_STATUS), NULL, DlgProc);
  406. switch (ret) {
  407. case 0:
  408. report (R_FATAL, "Cannot display dialog");
  409. break;
  410. case 1:
  411. report (R_WARNING, "DialogBox failed: %d",
  412. GetLastError ());
  413. break;
  414. case 3:
  415. exit (0);
  416. default:
  417. report (R_STATUS, "Dialog exited: %d", ret);
  418. }
  419. return 0;
  420. }
  421. int WINAPIV report (enum report_type t, ...)
  422. {
  423. typedef int r_fun_t (va_list);
  424. va_list ap;
  425. int ret = 0;
  426. static r_fun_t * const text_funcs[] =
  427. {textStatus, textProgress, textStep, textDelta,
  428. textTag, textDir, textOut,
  429. textWarning, textError, textFatal, textAsk};
  430. static r_fun_t * const GUI_funcs[] =
  431. {guiStatus, guiProgress, guiStep, guiDelta,
  432. guiTag, guiDir, guiOut,
  433. guiWarning, guiError, guiFatal, guiAsk};
  434. static r_fun_t * const quiet_funcs[] =
  435. {qNoOp, qNoOp, qNoOp, qNoOp,
  436. qNoOp, qNoOp, qNoOp,
  437. qNoOp, textError, textFatal, qAsk};
  438. static r_fun_t * const * funcs = NULL;
  439. switch (t) {
  440. case R_TEXTMODE:
  441. funcs = text_funcs;
  442. return 0;
  443. case R_QUIET:
  444. funcs = quiet_funcs;
  445. return 0;
  446. default:
  447. break;
  448. }
  449. if (!funcs) {
  450. HANDLE DlgThread;
  451. DWORD DlgThreadID;
  452. funcs = text_funcs;
  453. initEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
  454. if (!initEvent)
  455. report (R_STATUS, "Can't create event object: %d",
  456. GetLastError ());
  457. else {
  458. DlgThread = CreateThread (NULL, 0, DlgThreadProc,
  459. NULL, 0, &DlgThreadID);
  460. if (!DlgThread)
  461. report (R_STATUS, "Can't create GUI thread: %d",
  462. GetLastError ());
  463. else {
  464. DWORD ret = WaitForSingleObject (initEvent, INFINITE);
  465. CloseHandle(DlgThread);
  466. switch (ret) {
  467. case WAIT_OBJECT_0:
  468. funcs = GUI_funcs;
  469. break;
  470. case WAIT_TIMEOUT:
  471. report (R_STATUS, "GUI creation timed out");
  472. break;
  473. case WAIT_FAILED:
  474. report (R_STATUS, "Wait for GUI failed: %d",
  475. GetLastError ());
  476. break;
  477. default:
  478. report (R_STATUS, "Wait returned %d",
  479. ret);
  480. break;
  481. }
  482. }
  483. }
  484. }
  485. va_start (ap, t);
  486. if (t < ARRAY_SIZE(text_funcs)) ret = funcs[t](ap);
  487. else report (R_WARNING, "unimplemented report type: %d", t);
  488. va_end (ap);
  489. return ret;
  490. }