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