user32/tests: Allow WM_MOUSEACTIVATE in the mouse hover test for compatibility with...
[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 "wine/winbase16.h"
29 #include "wine/winuser16.h"
30 #include "winternl.h"
31 #include "dlgs.h"
32 #include "user_private.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
36 WINE_DECLARE_DEBUG_CHANNEL(msgbox);
37
38 #define MSGBOX_IDICON 1088
39 #define MSGBOX_IDTEXT 100
40 #define IDS_ERROR     2
41
42 struct ThreadWindows
43 {
44     UINT numHandles;
45     UINT numAllocs;
46     HWND *handles;
47 };
48
49 static BOOL CALLBACK MSGBOX_EnumProc(HWND hwnd, LPARAM lParam)
50 {
51     struct ThreadWindows *threadWindows = (struct ThreadWindows *)lParam;
52
53     if (!EnableWindow(hwnd, FALSE))
54     {
55         if(threadWindows->numHandles >= threadWindows->numAllocs)
56         {
57             threadWindows->handles = HeapReAlloc(GetProcessHeap(), 0, threadWindows->handles,
58                                                  (threadWindows->numAllocs*2)*sizeof(HWND));
59             threadWindows->numAllocs *= 2;
60         }
61         threadWindows->handles[threadWindows->numHandles++]=hwnd;
62     }
63    return TRUE;
64 }
65
66 static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
67 {
68     HFONT hFont = 0, hPrevFont = 0;
69     RECT rect;
70     HWND hItem;
71     HDC hdc;
72     int i, buttons;
73     int bspace, bw, bh, theight, tleft, wwidth, wheight, wleft, wtop, bpos;
74     int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
75     NONCLIENTMETRICSW nclm;
76     HMONITOR monitor = 0;
77     MONITORINFO mon_info;
78     LPCWSTR lpszText;
79     WCHAR buf[256];
80
81     nclm.cbSize = sizeof(nclm);
82     SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
83     hFont = CreateFontIndirectW (&nclm.lfMessageFont);
84     /* set button font */
85     for (i=1; i < 8; i++)
86         SendDlgItemMessageW (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
87     /* set text font */
88     SendDlgItemMessageW (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
89
90     if (HIWORD(lpmb->lpszCaption)) {
91        SetWindowTextW(hwnd, lpmb->lpszCaption);
92     } else {
93        UINT res_id = LOWORD(lpmb->lpszCaption);
94        if (res_id)
95        {
96            if (LoadStringW(lpmb->hInstance, res_id, buf, 256))
97                SetWindowTextW(hwnd, buf);
98        }
99        else
100        {
101            if (LoadStringW(user32_module, IDS_ERROR, buf, 256))
102                SetWindowTextW(hwnd, buf);
103        }
104     }
105     if (HIWORD(lpmb->lpszText)) {
106        lpszText = lpmb->lpszText;
107     } else {
108        lpszText = buf;
109        if (!LoadStringW(lpmb->hInstance, LOWORD(lpmb->lpszText), buf, 256))
110           *buf = 0;     /* FIXME ?? */
111     }
112     
113     TRACE_(msgbox)("%s\n", debugstr_w(lpszText));
114     SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
115
116     /* Hide not selected buttons */
117     switch(lpmb->dwStyle & MB_TYPEMASK) {
118     case MB_OK:
119         ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
120         /* fall through */
121     case MB_OKCANCEL:
122         ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
123         ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
124         ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
125         ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
126         ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
127         break;
128     case MB_ABORTRETRYIGNORE:
129         ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
130         ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
131         ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
132         ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
133         break;
134     case MB_YESNO:
135         ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
136         /* fall through */
137     case MB_YESNOCANCEL:
138         ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
139         ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
140         ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
141         ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
142         break;
143     case MB_RETRYCANCEL:
144         ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
145         ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
146         ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
147         ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
148         ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
149         break;
150     }
151     /* Set the icon */
152     switch(lpmb->dwStyle & MB_ICONMASK) {
153     case MB_ICONEXCLAMATION:
154         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
155                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
156         break;
157     case MB_ICONQUESTION:
158         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
159                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
160         break;
161     case MB_ICONASTERISK:
162         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
163                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
164         break;
165     case MB_ICONHAND:
166       SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
167                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
168       break;
169     case MB_USERICON:
170       SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
171                           (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
172       break;
173     default:
174         /* By default, Windows 95/98/NT do not associate an icon to message boxes.
175          * So wine should do the same.
176          */
177         break;
178     }
179
180     /* Position everything */
181     GetWindowRect(hwnd, &rect);
182     borheight = rect.bottom - rect.top;
183     borwidth  = rect.right - rect.left;
184     GetClientRect(hwnd, &rect);
185     borheight -= rect.bottom - rect.top;
186     borwidth  -= rect.right - rect.left;
187
188     /* Get the icon height */
189     GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
190     MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
191     if (!(lpmb->dwStyle & MB_ICONMASK))
192     {
193         rect.bottom = rect.top;
194         rect.right = rect.left;
195     }
196     iheight = rect.bottom - rect.top;
197     ileft = rect.left;
198     iwidth = rect.right - ileft;
199
200     hdc = GetDC(hwnd);
201     if (hFont)
202         hPrevFont = SelectObject(hdc, hFont);
203
204     /* Get the number of visible buttons and their size */
205     bh = bw = 1; /* Minimum button sizes */
206     for (buttons = 0, i = 1; i < 8; i++)
207     {
208         hItem = GetDlgItem(hwnd, i);
209         if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
210         {
211             WCHAR buttonText[1024];
212             int w, h;
213             buttons++;
214             if (GetWindowTextW(hItem, buttonText, 1024))
215             {
216                 DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
217                 h = rect.bottom - rect.top;
218                 w = rect.right - rect.left;
219                 if (h > bh) bh = h;
220                 if (w > bw)  bw = w ;
221             }
222         }
223     }
224     bw = max(bw, bh * 2);
225     /* Button white space */
226     bh = bh * 2;
227     bw = bw * 2;
228     bspace = bw/3; /* Space between buttons */
229
230     /* Get the text size */
231     GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
232     rect.top = rect.left = rect.bottom = 0;
233     DrawTextW( hdc, lpszText, -1, &rect,
234                DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
235     /* Min text width corresponds to space for the buttons */
236     tleft = ileft;
237     if (iwidth) tleft += ileft + iwidth;
238     twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
239     theight = rect.bottom;
240
241     if (hFont)
242         SelectObject(hdc, hPrevFont);
243     ReleaseDC(hwnd, hdc);
244
245     tiheight = 16 + max(iheight, theight);
246     wwidth  = tleft + twidth + ileft + borwidth;
247     wheight = 8 + tiheight + bh + borheight;
248
249     /* Message boxes are always desktop centered, so query desktop size and center window */
250     monitor = MonitorFromWindow(lpmb->hwndOwner ? lpmb->hwndOwner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY);
251     mon_info.cbSize = sizeof(mon_info);
252     GetMonitorInfoW(monitor, &mon_info);
253     wleft = (mon_info.rcWork.left + mon_info.rcWork.right - wwidth) / 2;
254     wtop = (mon_info.rcWork.top + mon_info.rcWork.bottom - wheight) / 2;
255
256     /* Resize and center the window */
257     SetWindowPos(hwnd, 0, wleft, wtop, wwidth, wheight,
258                  SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
259
260     /* Position the icon */
261     SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
262                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
263
264     /* Position the text */
265     SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
266                  SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
267
268     /* Position the buttons */
269     bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
270     for (buttons = i = 0; i < 7; i++) {
271         /* some arithmetic to get the right order for YesNoCancel windows */
272         hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
273         if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE) {
274             if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
275                 SetFocus(hItem);
276                 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
277             }
278             SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
279                          SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
280             bpos += bw + bspace;
281         }
282     }
283
284     /*handle modal message boxes*/
285     if (((lpmb->dwStyle & MB_TASKMODAL) && (lpmb->hwndOwner==NULL)) || (lpmb->dwStyle & MB_SYSTEMMODAL))
286         SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
287
288     return hFont;
289 }
290
291
292 /**************************************************************************
293  *           MSGBOX_DlgProc
294  *
295  * Dialog procedure for message boxes.
296  */
297 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
298                                         WPARAM wParam, LPARAM lParam )
299 {
300   HFONT hFont;
301
302   switch(message) {
303    case WM_INITDIALOG:
304    {
305        LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
306        SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
307        hFont = MSGBOX_OnInit(hwnd, mbp);
308        SetPropA(hwnd, "WINE_MSGBOX_HFONT", (HANDLE)hFont);
309        SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", (HANDLE)mbp->lpfnMsgBoxCallback);
310        break;
311    }
312
313    case WM_COMMAND:
314     switch (LOWORD(wParam))
315     {
316      case IDOK:
317      case IDCANCEL:
318      case IDABORT:
319      case IDRETRY:
320      case IDIGNORE:
321      case IDYES:
322      case IDNO:
323       hFont = GetPropA(hwnd, "WINE_MSGBOX_HFONT");
324       EndDialog(hwnd, wParam);
325       if (hFont)
326           DeleteObject(hFont);
327       break;
328     }
329     break;
330
331     case WM_HELP:
332     {
333         MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
334         HELPINFO hi;
335
336         memcpy(&hi, (void *)lParam, sizeof(hi));
337         hi.dwContextId = GetWindowContextHelpId(hwnd);
338
339         if (callback)
340             callback(&hi);
341         else
342             SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
343         break;
344    }
345
346    default:
347      /* Ok. Ignore all the other messages */
348      TRACE("Message number 0x%04x is being ignored.\n", message);
349     break;
350   }
351   return 0;
352 }
353
354
355 /**************************************************************************
356  *              MessageBoxA (USER32.@)
357  */
358 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
359 {
360     return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
361 }
362
363
364 /**************************************************************************
365  *              MessageBoxW (USER32.@)
366  */
367 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
368 {
369     return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
370 }
371
372
373 /**************************************************************************
374  *              MessageBoxExA (USER32.@)
375  */
376 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
377                               UINT type, WORD langid )
378 {
379     MSGBOXPARAMSA msgbox;
380
381     msgbox.cbSize = sizeof(msgbox);
382     msgbox.hwndOwner = hWnd;
383     msgbox.hInstance = 0;
384     msgbox.lpszText = text;
385     msgbox.lpszCaption = title;
386     msgbox.dwStyle = type;
387     msgbox.lpszIcon = NULL;
388     msgbox.dwContextHelpId = 0;
389     msgbox.lpfnMsgBoxCallback = NULL;
390     msgbox.dwLanguageId = langid;
391
392     return MessageBoxIndirectA(&msgbox);
393 }
394
395 /**************************************************************************
396  *              MessageBoxExW (USER32.@)
397  */
398 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
399                               UINT type, WORD langid )
400 {
401     MSGBOXPARAMSW msgbox;
402
403     msgbox.cbSize = sizeof(msgbox);
404     msgbox.hwndOwner = hWnd;
405     msgbox.hInstance = 0;
406     msgbox.lpszText = text;
407     msgbox.lpszCaption = title;
408     msgbox.dwStyle = type;
409     msgbox.lpszIcon = NULL;
410     msgbox.dwContextHelpId = 0;
411     msgbox.lpfnMsgBoxCallback = NULL;
412     msgbox.dwLanguageId = langid;
413
414     return MessageBoxIndirectW(&msgbox);
415 }
416
417 /**************************************************************************
418  *              MessageBoxIndirectA (USER32.@)
419  */
420 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
421 {
422     MSGBOXPARAMSW msgboxW;
423     UNICODE_STRING textW, captionW, iconW;
424     int ret;
425
426     if (HIWORD(msgbox->lpszText))
427         RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
428     else
429         textW.Buffer = (LPWSTR)msgbox->lpszText;
430     if (HIWORD(msgbox->lpszCaption))
431         RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
432     else
433         captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
434
435     if (msgbox->dwStyle & MB_USERICON)
436     {
437         if (HIWORD(msgbox->lpszIcon))
438             RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
439         else
440             iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
441     }
442     else
443         iconW.Buffer = NULL;
444
445     msgboxW.cbSize = sizeof(msgboxW);
446     msgboxW.hwndOwner = msgbox->hwndOwner;
447     msgboxW.hInstance = msgbox->hInstance;
448     msgboxW.lpszText = textW.Buffer;
449     msgboxW.lpszCaption = captionW.Buffer;
450     msgboxW.dwStyle = msgbox->dwStyle;
451     msgboxW.lpszIcon = iconW.Buffer;
452     msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
453     msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
454     msgboxW.dwLanguageId = msgbox->dwLanguageId;
455
456     ret = MessageBoxIndirectW(&msgboxW);
457
458     if (HIWORD(textW.Buffer)) RtlFreeUnicodeString(&textW);
459     if (HIWORD(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
460     if (HIWORD(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
461     return ret;
462 }
463
464 /**************************************************************************
465  *              MessageBoxIndirectW (USER32.@)
466  */
467 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
468 {
469     LPVOID tmplate;
470     HRSRC hRes;
471     int ret;
472     UINT i;
473     struct ThreadWindows threadWindows;
474     static const WCHAR msg_box_res_nameW[] = { 'M','S','G','B','O','X',0 };
475
476     if (!(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG,
477                                  msg_box_res_nameW, msgbox->dwLanguageId)))
478         return 0;
479     if (!(tmplate = (LPVOID)LoadResource(user32_module, hRes)))
480         return 0;
481
482     if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
483     {
484         threadWindows.numHandles = 0;
485         threadWindows.numAllocs = 10;
486         threadWindows.handles = HeapAlloc(GetProcessHeap(), 0, 10*sizeof(HWND));
487         EnumThreadWindows(GetCurrentThreadId(), MSGBOX_EnumProc, (LPARAM)&threadWindows);
488     }
489
490     ret=DialogBoxIndirectParamW(msgbox->hInstance, tmplate,
491                                 msgbox->hwndOwner, MSGBOX_DlgProc, (LPARAM)msgbox);
492
493     if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
494     {
495         for (i = 0; i < threadWindows.numHandles; i++)
496             EnableWindow(threadWindows.handles[i], TRUE);
497         HeapFree(GetProcessHeap(), 0, threadWindows.handles);
498     }
499     return ret;
500 }