ReactOS  0.4.13-dev-79-gcd489d8
gui.c
Go to the documentation of this file.
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 
21 #include <windows.h>
22 #include <commctrl.h>
23 
24 #include "resource.h"
25 #include "winetest.h"
26 
27 /* Event object to signal successful window creation to main thread.
28  */
30 
31 /* Dialog handle
32  */
33 static HWND dialog;
34 
35 /* Progress data for the text* functions and for scaling.
36  */
37 static unsigned int progressMax, progressCurr;
38 static double progressScale;
39 
40 /* Progress group counter for the gui* functions.
41  */
42 static int progressGroup;
43 
45 
46 static int
47 MBdefault (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 
61 /* report (R_STATUS, fmt, ...) */
62 static int
64 {
65  char *str = vstrmake (NULL, ap);
66 
67  fputs (str, stderr);
68  fputc ('\n', stderr);
69  free (str);
70  return 0;
71 }
72 
73 static int
75 {
76  size_t len;
77  char *str = vstrmake (&len, ap);
78 
79  if (len > 128) str[129] = 0;
81  free (str);
82  return 0;
83 }
84 
85 /* report (R_PROGRESS, barnum, steps) */
86 static int
88 {
89  progressGroup = va_arg (ap, int);
90  progressMax = va_arg (ap, int);
91  progressCurr = 0;
92  return 0;
93 }
94 
95 static int
97 {
98  unsigned int max;
99  HWND pb;
100 
101  progressGroup = va_arg (ap, int);
102  progressMax = max = va_arg (ap, int);
103  progressCurr = 0;
104  if (max > 0xffff) {
105  progressScale = (double)0xffff / max;
106  max = 0xffff;
107  }
108  else progressScale = 1;
109  pb = GetDlgItem (dialog, IDC_PB0 + progressGroup * 2);
110  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, ...) */
116 static int
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 
128 static int
130 {
131  const int pgID = IDC_ST0 + progressGroup * 2;
132  char *str = vstrmake (NULL, ap);
133 
134  progressCurr++;
135  SetDlgItemText (dialog, pgID, str);
138  free (str);
139  return 0;
140 }
141 
142 /* report (R_DELTA, inc, fmt, ...) */
143 static int
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 
156 static int
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);
167  free (str);
168  return 0;
169 }
170 
171 /* report (R_TAG) */
172 static int
174 {
175  fputs ("Tag: ", stderr);
176  fputs (tag, stderr);
177  fputc ('\n', stderr);
178  return 0;
179 }
180 
181 static int
183 {
185  return 0;
186 }
187 
188 /* report (R_DIR, fmt, ...) */
189 static int
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 
201 static int
203 {
204  char *str = vstrmake (NULL, ap);
205 
207  free (str);
208  return 0;
209 }
210 
211 /* report (R_OUT, fmt, ...) */
212 static int
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 
224 static int
226 {
227  char *str = vstrmake (NULL, ap);
228 
230  free (str);
231  return 0;
232 }
233 
234 /* report (R_WARNING, fmt, ...) */
235 static int
237 {
238  fputs ("Warning: ", stderr);
239  textStatus (ap);
240  return 0;
241 }
242 
243 static int
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 
253 /* report (R_ERROR, fmt, ...) */
254 static int
256 {
257  fputs ("Error: ", stderr);
258  textStatus (ap);
259  return 0;
260 }
261 
262 static int
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, ...) */
273 static int
275 {
276  textError (ap);
277  exit (1);
278 }
279 
280 static int
282 {
283  guiError (ap);
284  exit (1);
285 }
286 
287 /* report (R_ASK, type, fmt, ...) */
288 static int
290 {
291  int uType = va_arg (ap, int);
292  int ret = MBdefault (uType);
293  char *str = vstrmake (NULL, ap);
294 
295  fprintf (stderr, "Question of type %d: %s\n"
296  "Returning default: %d\n", uType, str, ret);
297  free (str);
298  return ret;
299 }
300 
301 static int
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 
313 static BOOL CALLBACK
315 {
316  switch (msg) {
317  case WM_CHAR:
318  if (wParam == 8) break; /* backspace is OK */
320  !goodtagchar (wParam)) return TRUE;
321  break;
322  }
324 }
325 
326 static INT_PTR CALLBACK
328 {
329  int len;
330 
331  switch (msg) {
332  case WM_INITDIALOG:
335  return TRUE;
336  case WM_COMMAND:
337  switch (LOWORD (wParam)) {
338  case IDOK:
340  tag = xmalloc (len+1);
342  EndDialog (hwnd, IDOK);
343  return TRUE;
344  case IDABORT:
346  return TRUE;
347  }
348  }
349  return FALSE;
350 }
351 
352 int
353 guiAskTag (void)
354 {
355  return DialogBox (GetModuleHandle (NULL),
357  dialog, AskTagProc);
358 }
359 
360 /* Quiet functions */
361 static int
363 {
364  return 0;
365 }
366 
367 static int
369 {
370  exit (1);
371 }
372 
373 static int
375 {
376  return MBdefault (va_arg (ap, int));
377 }
378 
379 static INT_PTR CALLBACK
381 {
382  switch (msg) {
383  case WM_COMMAND:
384  switch (LOWORD (wParam)) {
385  case IDCANCEL:
387  return TRUE;
388  }
389  }
390  return FALSE;
391 }
392 
393 static INT_PTR CALLBACK
395 {
396  switch (msg) {
397  case WM_INITDIALOG:
398  SendMessage (hwnd, WM_SETICON, ICON_SMALL,
401  SendMessage (hwnd, WM_SETICON, ICON_BIG,
404  dialog = hwnd;
405  if (!SetEvent (initEvent)) {
406  report (R_STATUS, "Can't signal main thread: %d",
407  GetLastError ());
408  EndDialog (hwnd, 2);
409  }
410  return TRUE;
411  case WM_CLOSE:
412  EndDialog (hwnd, 3);
413  return TRUE;
414  case WM_COMMAND:
415  switch (LOWORD (wParam)) {
416  case IDHELP:
419  return TRUE;
420  case IDABORT:
421  report (R_WARNING, "Not implemented");
422  return TRUE;
423  }
424  }
425  return FALSE;
426 }
427 
428 static DWORD WINAPI
430 {
431  int ret;
432 
436  NULL, DlgProc);
437  switch (ret) {
438  case 0:
439  report (R_WARNING, "Invalid parent handle");
440  break;
441  case 1:
442  report (R_WARNING, "DialogBox failed: %d",
443  GetLastError ());
444  break;
445  case 3:
446  exit (0);
447  default:
448  report (R_STATUS, "Dialog exited: %d", ret);
449  }
450  return 0;
451 }
452 
453 int
454 report (enum report_type t, ...)
455 {
456  typedef int r_fun_t (va_list);
457 
458  va_list ap;
459  int ret = 0;
460  static r_fun_t * const text_funcs[] =
464  static r_fun_t * const GUI_funcs[] =
466  guiTag, guiDir, guiOut,
468  static r_fun_t * const quiet_funcs[] =
469  {qNoOp, qNoOp, qNoOp, qNoOp,
470  qNoOp, qNoOp, qNoOp,
471  qNoOp, qNoOp, qFatal, qAsk};
472  static r_fun_t * const * funcs = NULL;
473 
474  switch (t) {
475  case R_TEXTMODE:
476  funcs = text_funcs;
477  return 0;
478  case R_QUIET:
479  funcs = quiet_funcs;
480  return 0;
481  default:
482  break;
483  }
484 
485  if (!funcs) {
486  HANDLE DlgThread;
487  DWORD DlgThreadID;
488 
489  funcs = text_funcs;
491  if (!initEvent)
492  report (R_STATUS, "Can't create event object: %d",
493  GetLastError ());
494  else {
495  DlgThread = CreateThread (NULL, 0, DlgThreadProc,
496  NULL, 0, &DlgThreadID);
497  if (!DlgThread)
498  report (R_STATUS, "Can't create GUI thread: %d",
499  GetLastError ());
500  else {
502  switch (ret) {
503  case WAIT_OBJECT_0:
504  funcs = GUI_funcs;
505  break;
506  case WAIT_TIMEOUT:
507  report (R_STATUS, "GUI creation timed out");
508  break;
509  case WAIT_FAILED:
510  report (R_STATUS, "Wait for GUI failed: %d",
511  GetLastError ());
512  break;
513  default:
514  report (R_STATUS, "Wait returned %d",
515  ret);
516  break;
517  }
518  }
519  }
520  }
521 
522  va_start (ap, t);
523  if (t < sizeof text_funcs / sizeof text_funcs[0] &&
524  t < sizeof GUI_funcs / sizeof GUI_funcs[0]) ret = funcs[t](ap);
525  else report (R_WARNING, "unimplemented report type: %d", t);
526  va_end (ap);
527  return ret;
528 }
#define CreateEvent
Definition: winbase.h:3562
static unsigned int progressMax
Definition: gui.c:37
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
#define MAKEINTRESOURCE
Definition: winuser.h:591
#define LONG_PTR
Definition: treelist.c:79
#define max(a, b)
Definition: svc.c:63
#define TRUE
Definition: types.h:120
_Check_return_opt_ _CRTIMP int __cdecl fputc(_In_ int _Ch, _Inout_ FILE *_File)
#define IDOK
Definition: winuser.h:824
static int guiStatus(va_list ap)
Definition: gui.c:74
#define IDABORT
Definition: winuser.h:826
static int textFatal(va_list ap)
Definition: gui.c:274
static int qAsk(va_list ap)
Definition: gui.c:374
static int guiOut(va_list ap)
Definition: gui.c:225
static double progressScale
Definition: gui.c:38
#define WM_CHAR
Definition: winuser.h:1693
#define IDYES
Definition: winuser.h:829
GLuint GLenum matrix
Definition: glext.h:9407
static int textWarning(va_list ap)
Definition: gui.c:236
#define free
Definition: debug_ros.c:5
static int guiDir(va_list ap)
Definition: gui.c:202
#define MAKELPARAM(l, h)
Definition: winuser.h:3915
#define CALLBACK
Definition: compat.h:27
static int MBdefault(int uType)
Definition: gui.c:47
int report(enum report_type t,...)
Definition: gui.c:454
static int guiWarning(va_list ap)
Definition: gui.c:244
report_type
Definition: winetest.h:46
GLdouble GLdouble t
Definition: gl.h:2047
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:679
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
Definition: ecma_167.h:138
#define PBM_SETPOS
Definition: commctrl.h:2156
int goodtagchar(char c)
Definition: util.c:115
UINT_PTR WPARAM
Definition: windef.h:207
#define SendDlgItemMessage
Definition: winuser.h:5708
static int guiError(va_list ap)
Definition: gui.c:263
int32_t INT_PTR
Definition: typedefs.h:62
static HANDLE initEvent
Definition: gui.c:29
#define SetWindowLongPtr
Definition: treelist.c:70
BOOL WINAPI EndDialog(_In_ HWND, _In_ INT_PTR)
WPARAM wParam
Definition: combotst.c:138
#define MB_TYPEMASK
Definition: winuser.h:818
#define MB_ICONWARNING
Definition: winuser.h:780
LRESULT WINAPI CallWindowProcA(_In_ WNDPROC, _In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
#define IDC_SB
Definition: resource.h:40
#define IDHELP
Definition: resource_2.h:8
#define ICON_SMALL
Definition: tnclass.cpp:48
#define PBM_SETRANGE
Definition: commctrl.h:2155
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define DialogBox
Definition: winuser.h:5627
static WNDPROC DefEditProc
Definition: gui.c:44
#define va_end(ap)
Definition: acmsvcex.h:90
void * xmalloc(int size)
Definition: uimain.c:747
unsigned int BOOL
Definition: ntddk_ex.h:94
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
static INT_PTR CALLBACK AboutProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: gui.c:380
static int textOut(va_list ap)
Definition: gui.c:213
#define MAXTAGLEN
Definition: winetest.h:62
static int qFatal(va_list ap)
Definition: gui.c:368
static const char mbstate_t *static wchar_t const char mbstate_t *static const wchar_t int *static double
Definition: string.c:80
const WCHAR * str
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:112
smooth NULL
Definition: ftsmooth.c:416
#define MB_ICONQUESTION
Definition: winuser.h:783
LONG_PTR LPARAM
Definition: windef.h:208
char * va_list
Definition: acmsvcex.h:78
static int guiFatal(va_list ap)
Definition: gui.c:281
static int textDir(va_list ap)
Definition: gui.c:190
#define PBM_SETSTEP
Definition: commctrl.h:2158
#define IDC_TAG
Definition: resource.h:38
#define IDC_OUT
Definition: resource.h:37
static int guiAsk(va_list ap)
Definition: gui.c:302
static int guiProgress(va_list ap)
Definition: gui.c:96
static int progressGroup
Definition: gui.c:42
int WINAPI GetWindowTextLengthA(_In_ HWND)
#define WAIT_OBJECT_0
Definition: winbase.h:387
static int textProgress(va_list ap)
Definition: gui.c:87
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
#define WM_CLOSE
Definition: winuser.h:1603
#define MB_DEFMASK
Definition: winuser.h:814
GLfloat param
Definition: glext.h:5796
#define WINAPI
Definition: msvc.h:8
static struct __wine_debug_functions funcs
Definition: debug.c:59
unsigned long DWORD
Definition: ntddk_ex.h:95
#define WAIT_FAILED
Definition: winbase.h:394
static int textDelta(va_list ap)
Definition: gui.c:144
#define SetDlgItemText
Definition: winuser.h:5715
#define IDRETRY
Definition: winuser.h:827
#define WAIT_TIMEOUT
Definition: dderror.h:14
int ret
#define IDD_TAG
Definition: resource.h:25
#define MB_ICONERROR
Definition: winuser.h:781
GLenum GLsizei len
Definition: glext.h:6722
#define MessageBox
Definition: winuser.h:5688
static int textStatus(va_list ap)
Definition: gui.c:63
#define WM_COMMAND
Definition: winuser.h:1716
static int textTag(va_list ap)
Definition: gui.c:173
#define GWLP_WNDPROC
Definition: treelist.c:66
UINT WINAPI GetDlgItemTextA(HWND hDlg, int nIDDlgItem, LPSTR lpString, int nMaxCount)
Definition: dialog.c:2177
static int textAsk(va_list ap)
Definition: gui.c:289
LRESULT(CALLBACK * WNDPROC)(HWND, UINT, WPARAM, LPARAM)
Definition: winuser.h:2859
static int guiDelta(va_list ap)
Definition: gui.c:157
#define va_arg(ap, T)
Definition: acmsvcex.h:89
#define GetModuleHandle
Definition: winbase.h:3641
#define IDD_ABOUT
Definition: shresdef.h:298
static INT_PTR CALLBACK DlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: gui.c:394
#define IDC_DIR
Definition: resource.h:36
#define va_start(ap, A)
Definition: acmsvcex.h:91
unsigned int UINT
Definition: ndis.h:50
static int textStep(va_list ap)
Definition: gui.c:117
static INT_PTR CALLBACK AskTagProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: gui.c:327
#define MB_OK
Definition: winuser.h:784
static unsigned int progressCurr
Definition: gui.c:37
static int guiTag(va_list ap)
Definition: gui.c:182
#define IDD_STATUS
Definition: resource.h:22
#define IDC_PB0
Definition: resource.h:30
static HWND dialog
Definition: gui.c:33
#define IDC_ST0
Definition: resource.h:29
int guiAskTag(void)
Definition: gui.c:353
#define msg(x)
Definition: auth_time.c:54
#define MB_DEFBUTTON2
Definition: winuser.h:793
static int guiStep(va_list ap)
Definition: gui.c:129
void int int ULONGLONG int va_list * ap
Definition: winesup.h:32
#define SendMessage
Definition: winuser.h:5709
FILE * stderr
static int qNoOp(va_list ap)
Definition: gui.c:362
VOID WINAPI InitCommonControls(void)
Definition: commctrl.c:863
#define LoadIcon
Definition: winuser.h:5679
#define IDNO
Definition: winuser.h:830
#define IDI_WINE
Definition: resource.h:21
void exit(int exitcode)
Definition: _exit.c:33
#define IDCANCEL
Definition: winuser.h:825
static DWORD WINAPI DlgThreadProc(LPVOID param)
Definition: gui.c:429
#define INFINITE
Definition: serial.h:102
static int textError(va_list ap)
Definition: gui.c:255
#define WM_INITDIALOG
Definition: winuser.h:1715
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)
static BOOL CALLBACK EditTagProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
Definition: gui.c:314
#define IDIGNORE
Definition: winuser.h:828
#define ICON_BIG
Definition: tnclass.cpp:51
char * vstrmake(size_t *lenp, va_list ap)
Definition: util.c:74