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