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