strmbase: Add support for rendering algorithms to quality control.
[wine] / dlls / user32 / msgbox.c
1 /*
2  * Message boxes
3  *
4  * Copyright 1995 Bernd Schmidt
5  * Copyright 2004 Ivan Leo Puoti, Juan Lang
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23 #include <string.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winternl.h"
29 #include "dlgs.h"
30 #include "user_private.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
34 WINE_DECLARE_DEBUG_CHANNEL(msgbox);
35
36 #define MSGBOX_IDICON 1088
37 #define MSGBOX_IDTEXT 100
38 #define IDS_ERROR     2
39
40 struct ThreadWindows
41 {
42     UINT numHandles;
43     UINT numAllocs;
44     HWND *handles;
45 };
46
47 static BOOL CALLBACK MSGBOX_EnumProc(HWND hwnd, LPARAM lParam)
48 {
49     struct ThreadWindows *threadWindows = (struct ThreadWindows *)lParam;
50
51     if (!EnableWindow(hwnd, FALSE))
52     {
53         if(threadWindows->numHandles >= threadWindows->numAllocs)
54         {
55             threadWindows->handles = HeapReAlloc(GetProcessHeap(), 0, threadWindows->handles,
56                                                  (threadWindows->numAllocs*2)*sizeof(HWND));
57             threadWindows->numAllocs *= 2;
58         }
59         threadWindows->handles[threadWindows->numHandles++]=hwnd;
60     }
61    return TRUE;
62 }
63
64 static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
65 {
66     HFONT hFont = 0, hPrevFont = 0;
67     RECT rect;
68     HWND hItem;
69     HDC hdc;
70     int i, buttons;
71     int bspace, bw, bh, theight, tleft, wwidth, wheight, wleft, wtop, bpos;
72     int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
73     NONCLIENTMETRICSW nclm;
74     HMONITOR monitor = 0;
75     MONITORINFO mon_info;
76     LPCWSTR lpszText;
77     WCHAR *buffer = NULL;
78     const WCHAR *ptr;
79
80     /* Index the order the buttons need to appear to an ID* constant */
81     static const int buttonOrder[10] = { 6, 7, 1, 3, 4, 2, 5, 10, 11, 9 };
82
83     nclm.cbSize = sizeof(nclm);
84     SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
85     hFont = CreateFontIndirectW (&nclm.lfMessageFont);
86     /* set button font */
87     for (i=1; i < 12; i++)
88         /* No button 8 (Close) */
89         if (i != 8) {
90             SendDlgItemMessageW (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
91         }
92     /* set text font */
93     SendDlgItemMessageW (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
94
95     if (!IS_INTRESOURCE(lpmb->lpszCaption)) {
96        SetWindowTextW(hwnd, lpmb->lpszCaption);
97     } else {
98         UINT len = LoadStringW( lpmb->hInstance, LOWORD(lpmb->lpszCaption), (LPWSTR)&ptr, 0 );
99         if (!len) len = LoadStringW( user32_module, IDS_ERROR, (LPWSTR)&ptr, 0 );
100         buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
101         if (buffer)
102         {
103             memcpy( buffer, ptr, len * sizeof(WCHAR) );
104             buffer[len] = 0;
105             SetWindowTextW( hwnd, buffer );
106             HeapFree( GetProcessHeap(), 0, buffer );
107             buffer = NULL;
108         }
109     }
110     if (IS_INTRESOURCE(lpmb->lpszText)) {
111         UINT len = LoadStringW( lpmb->hInstance, LOWORD(lpmb->lpszText), (LPWSTR)&ptr, 0 );
112         lpszText = buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
113         if (buffer)
114         {
115             memcpy( buffer, ptr, len * sizeof(WCHAR) );
116             buffer[len] = 0;
117         }
118     } else {
119        lpszText = lpmb->lpszText;
120     }
121
122     TRACE_(msgbox)("%s\n", debugstr_w(lpszText));
123     SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
124
125     /* Remove not selected buttons */
126     switch(lpmb->dwStyle & MB_TYPEMASK) {
127     case MB_OK:
128         DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
129         /* fall through */
130     case MB_OKCANCEL:
131         DestroyWindow(GetDlgItem(hwnd, IDABORT));
132         DestroyWindow(GetDlgItem(hwnd, IDRETRY));
133         DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
134         DestroyWindow(GetDlgItem(hwnd, IDYES));
135         DestroyWindow(GetDlgItem(hwnd, IDNO));
136         DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
137         DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
138         break;
139     case MB_ABORTRETRYIGNORE:
140         DestroyWindow(GetDlgItem(hwnd, IDOK));
141         DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
142         DestroyWindow(GetDlgItem(hwnd, IDYES));
143         DestroyWindow(GetDlgItem(hwnd, IDNO));
144         DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
145         DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
146         break;
147     case MB_YESNO:
148         DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
149         /* fall through */
150     case MB_YESNOCANCEL:
151         DestroyWindow(GetDlgItem(hwnd, IDOK));
152         DestroyWindow(GetDlgItem(hwnd, IDABORT));
153         DestroyWindow(GetDlgItem(hwnd, IDRETRY));
154         DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
155         DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
156         DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
157         break;
158     case MB_RETRYCANCEL:
159         DestroyWindow(GetDlgItem(hwnd, IDOK));
160         DestroyWindow(GetDlgItem(hwnd, IDABORT));
161         DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
162         DestroyWindow(GetDlgItem(hwnd, IDYES));
163         DestroyWindow(GetDlgItem(hwnd, IDNO));
164         DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
165         DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
166         break;
167     case MB_CANCELTRYCONTINUE:
168         DestroyWindow(GetDlgItem(hwnd, IDOK));
169         DestroyWindow(GetDlgItem(hwnd, IDABORT));
170         DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
171         DestroyWindow(GetDlgItem(hwnd, IDYES));
172         DestroyWindow(GetDlgItem(hwnd, IDNO));
173         DestroyWindow(GetDlgItem(hwnd, IDRETRY));
174     }
175     /* Set the icon */
176     switch(lpmb->dwStyle & MB_ICONMASK) {
177     case MB_ICONEXCLAMATION:
178         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
179                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
180         break;
181     case MB_ICONQUESTION:
182         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
183                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
184         break;
185     case MB_ICONASTERISK:
186         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
187                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
188         break;
189     case MB_ICONHAND:
190       SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
191                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
192       break;
193     case MB_USERICON:
194       SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
195                           (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
196       break;
197     default:
198         /* By default, Windows 95/98/NT do not associate an icon to message boxes.
199          * So wine should do the same.
200          */
201         break;
202     }
203
204     /* Remove Help button unless MB_HELP supplied */
205     if (!(lpmb->dwStyle & MB_HELP)) {
206         DestroyWindow(GetDlgItem(hwnd, IDHELP));
207     }
208
209     /* Position everything */
210     GetWindowRect(hwnd, &rect);
211     borheight = rect.bottom - rect.top;
212     borwidth  = rect.right - rect.left;
213     GetClientRect(hwnd, &rect);
214     borheight -= rect.bottom - rect.top;
215     borwidth  -= rect.right - rect.left;
216
217     /* Get the icon height */
218     GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
219     MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
220     if (!(lpmb->dwStyle & MB_ICONMASK))
221     {
222         rect.bottom = rect.top;
223         rect.right = rect.left;
224     }
225     iheight = rect.bottom - rect.top;
226     ileft = rect.left;
227     iwidth = rect.right - ileft;
228
229     hdc = GetDC(hwnd);
230     if (hFont)
231         hPrevFont = SelectObject(hdc, hFont);
232
233     /* Get the number of visible buttons and their size */
234     bh = bw = 1; /* Minimum button sizes */
235     for (buttons = 0, i = 1; i < 12; i++)
236     {
237         if (i == 8) continue; /* No CLOSE button */
238         hItem = GetDlgItem(hwnd, i);
239         if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
240         {
241             WCHAR buttonText[1024];
242             int w, h;
243             buttons++;
244             if (GetWindowTextW(hItem, buttonText, 1024))
245             {
246                 DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
247                 h = rect.bottom - rect.top;
248                 w = rect.right - rect.left;
249                 if (h > bh) bh = h;
250                 if (w > bw)  bw = w ;
251             }
252         }
253     }
254     bw = max(bw, bh * 2);
255     /* Button white space */
256     bh = bh * 2;
257     bw = bw * 2;
258     bspace = bw/3; /* Space between buttons */
259
260     /* Get the text size */
261     GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
262     rect.top = rect.left = rect.bottom = 0;
263     DrawTextW( hdc, lpszText, -1, &rect,
264                DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
265     /* Min text width corresponds to space for the buttons */
266     tleft = ileft;
267     if (iwidth) tleft += ileft + iwidth;
268     twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
269     theight = rect.bottom;
270
271     if (hFont)
272         SelectObject(hdc, hPrevFont);
273     ReleaseDC(hwnd, hdc);
274
275     tiheight = 16 + max(iheight, theight);
276     wwidth  = tleft + twidth + ileft + borwidth;
277     wheight = 8 + tiheight + bh + borheight;
278
279     /* Message boxes are always desktop centered, so query desktop size and center window */
280     monitor = MonitorFromWindow(lpmb->hwndOwner ? lpmb->hwndOwner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY);
281     mon_info.cbSize = sizeof(mon_info);
282     GetMonitorInfoW(monitor, &mon_info);
283     wleft = (mon_info.rcWork.left + mon_info.rcWork.right - wwidth) / 2;
284     wtop = (mon_info.rcWork.top + mon_info.rcWork.bottom - wheight) / 2;
285
286     /* Resize and center the window */
287     SetWindowPos(hwnd, 0, wleft, wtop, wwidth, wheight,
288                  SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
289
290     /* Position the icon */
291     SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
292                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
293
294     /* Position the text */
295     SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
296                  SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
297
298     /* Position the buttons */
299     bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
300     for (buttons = i = 0; i < (sizeof(buttonOrder) / sizeof(buttonOrder[0])); i++) {
301
302         /* Convert the button order to ID* value to order for the buttons */
303         hItem = GetDlgItem(hwnd, buttonOrder[i]);
304         if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE) {
305             if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
306                 SetFocus(hItem);
307                 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
308             }
309             SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
310                          SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
311             bpos += bw + bspace;
312         }
313     }
314
315     /*handle modal message boxes*/
316     if (((lpmb->dwStyle & MB_TASKMODAL) && (lpmb->hwndOwner==NULL)) || (lpmb->dwStyle & MB_SYSTEMMODAL))
317         SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
318
319     HeapFree( GetProcessHeap(), 0, buffer );
320     return hFont;
321 }
322
323
324 /**************************************************************************
325  *           MSGBOX_DlgProc
326  *
327  * Dialog procedure for message boxes.
328  */
329 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
330                                         WPARAM wParam, LPARAM lParam )
331 {
332   HFONT hFont;
333
334   switch(message) {
335    case WM_INITDIALOG:
336    {
337        LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
338        SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
339        hFont = MSGBOX_OnInit(hwnd, mbp);
340        SetPropA(hwnd, "WINE_MSGBOX_HFONT", hFont);
341        SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", mbp->lpfnMsgBoxCallback);
342        break;
343    }
344
345    case WM_COMMAND:
346     switch (LOWORD(wParam))
347     {
348      case IDOK:
349      case IDCANCEL:
350      case IDABORT:
351      case IDRETRY:
352      case IDIGNORE:
353      case IDYES:
354      case IDNO:
355      case IDTRYAGAIN:
356      case IDCONTINUE:
357       hFont = GetPropA(hwnd, "WINE_MSGBOX_HFONT");
358       EndDialog(hwnd, wParam);
359       if (hFont)
360           DeleteObject(hFont);
361       break;
362      case IDHELP:
363       FIXME("Help button not supported yet\n");
364       break;
365     }
366     break;
367
368     case WM_HELP:
369     {
370         MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
371         HELPINFO hi;
372
373         memcpy(&hi, (void *)lParam, sizeof(hi));
374         hi.dwContextId = GetWindowContextHelpId(hwnd);
375
376         if (callback)
377             callback(&hi);
378         else
379             SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
380         break;
381    }
382
383    default:
384      /* Ok. Ignore all the other messages */
385      TRACE("Message number 0x%04x is being ignored.\n", message);
386     break;
387   }
388   return 0;
389 }
390
391
392 /**************************************************************************
393  *              MessageBoxA (USER32.@)
394  */
395 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
396 {
397     return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
398 }
399
400
401 /**************************************************************************
402  *              MessageBoxW (USER32.@)
403  */
404 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
405 {
406     return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
407 }
408
409
410 /**************************************************************************
411  *              MessageBoxExA (USER32.@)
412  */
413 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
414                               UINT type, WORD langid )
415 {
416     MSGBOXPARAMSA msgbox;
417
418     msgbox.cbSize = sizeof(msgbox);
419     msgbox.hwndOwner = hWnd;
420     msgbox.hInstance = 0;
421     msgbox.lpszText = text;
422     msgbox.lpszCaption = title;
423     msgbox.dwStyle = type;
424     msgbox.lpszIcon = NULL;
425     msgbox.dwContextHelpId = 0;
426     msgbox.lpfnMsgBoxCallback = NULL;
427     msgbox.dwLanguageId = langid;
428
429     return MessageBoxIndirectA(&msgbox);
430 }
431
432 /**************************************************************************
433  *              MessageBoxExW (USER32.@)
434  */
435 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
436                               UINT type, WORD langid )
437 {
438     MSGBOXPARAMSW msgbox;
439
440     msgbox.cbSize = sizeof(msgbox);
441     msgbox.hwndOwner = hWnd;
442     msgbox.hInstance = 0;
443     msgbox.lpszText = text;
444     msgbox.lpszCaption = title;
445     msgbox.dwStyle = type;
446     msgbox.lpszIcon = NULL;
447     msgbox.dwContextHelpId = 0;
448     msgbox.lpfnMsgBoxCallback = NULL;
449     msgbox.dwLanguageId = langid;
450
451     return MessageBoxIndirectW(&msgbox);
452 }
453
454 /**************************************************************************
455  *              MessageBoxIndirectA (USER32.@)
456  */
457 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
458 {
459     MSGBOXPARAMSW msgboxW;
460     UNICODE_STRING textW, captionW, iconW;
461     int ret;
462
463     if (IS_INTRESOURCE(msgbox->lpszText))
464         textW.Buffer = (LPWSTR)msgbox->lpszText;
465     else
466         RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
467     if (IS_INTRESOURCE(msgbox->lpszCaption))
468         captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
469     else
470         RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
471
472     if (msgbox->dwStyle & MB_USERICON)
473     {
474         if (IS_INTRESOURCE(msgbox->lpszIcon))
475             iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
476         else
477             RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
478     }
479     else
480         iconW.Buffer = NULL;
481
482     msgboxW.cbSize = sizeof(msgboxW);
483     msgboxW.hwndOwner = msgbox->hwndOwner;
484     msgboxW.hInstance = msgbox->hInstance;
485     msgboxW.lpszText = textW.Buffer;
486     msgboxW.lpszCaption = captionW.Buffer;
487     msgboxW.dwStyle = msgbox->dwStyle;
488     msgboxW.lpszIcon = iconW.Buffer;
489     msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
490     msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
491     msgboxW.dwLanguageId = msgbox->dwLanguageId;
492
493     ret = MessageBoxIndirectW(&msgboxW);
494
495     if (!IS_INTRESOURCE(textW.Buffer)) RtlFreeUnicodeString(&textW);
496     if (!IS_INTRESOURCE(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
497     if (!IS_INTRESOURCE(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
498     return ret;
499 }
500
501 /**************************************************************************
502  *              MessageBoxIndirectW (USER32.@)
503  */
504 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
505 {
506     LPVOID tmplate;
507     HRSRC hRes;
508     int ret;
509     UINT i;
510     struct ThreadWindows threadWindows;
511     static const WCHAR msg_box_res_nameW[] = { 'M','S','G','B','O','X',0 };
512
513     if (!(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG,
514                                  msg_box_res_nameW, msgbox->dwLanguageId)))
515     {
516         if (!msgbox->dwLanguageId ||
517             !(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG, msg_box_res_nameW, LANG_NEUTRAL)))
518             return 0;
519     }
520     if (!(tmplate = LoadResource(user32_module, hRes)))
521         return 0;
522
523     if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
524     {
525         threadWindows.numHandles = 0;
526         threadWindows.numAllocs = 10;
527         threadWindows.handles = HeapAlloc(GetProcessHeap(), 0, 10*sizeof(HWND));
528         EnumThreadWindows(GetCurrentThreadId(), MSGBOX_EnumProc, (LPARAM)&threadWindows);
529     }
530
531     ret=DialogBoxIndirectParamW(msgbox->hInstance, tmplate,
532                                 msgbox->hwndOwner, MSGBOX_DlgProc, (LPARAM)msgbox);
533
534     if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
535     {
536         for (i = 0; i < threadWindows.numHandles; i++)
537             EnableWindow(threadWindows.handles[i], TRUE);
538         HeapFree(GetProcessHeap(), 0, threadWindows.handles);
539     }
540     return ret;
541 }