Check for overflows with ClrUsed.
[wine.git] / windows / msgbox.c
blobfb5ce9bf0d54f3296c9228676c6bc5ab30f3c5bd
1 /*
2 * Message boxes
4 * Copyright 1995 Bernd Schmidt
5 * Copyright 2004 Ivan Leo Puoti, Juan Lang
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdarg.h>
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "wine/winbase16.h"
29 #include "wine/winuser16.h"
30 #include "winreg.h"
31 #include "winternl.h"
32 #include "dlgs.h"
33 #include "user_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
37 WINE_DECLARE_DEBUG_CHANNEL(msgbox);
39 #define MSGBOX_IDICON 1088
40 #define MSGBOX_IDTEXT 100
41 #define IDS_ERROR 2
43 struct ThreadWindows
45 UINT numHandles;
46 UINT numAllocs;
47 HWND *handles;
50 BOOL CALLBACK MSGBOX_EnumProc(HWND hwnd, LPARAM lParam)
52 struct ThreadWindows *threadWindows = (struct ThreadWindows *)lParam;
54 if (!EnableWindow(hwnd, FALSE))
56 if(threadWindows->numHandles >= threadWindows->numAllocs)
58 threadWindows->handles = HeapReAlloc(GetProcessHeap(), 0, threadWindows->handles,
59 (threadWindows->numAllocs*2)*sizeof(HWND));
60 threadWindows->numAllocs *= 2;
62 threadWindows->handles[threadWindows->numHandles++]=hwnd;
64 return TRUE;
67 static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
69 HFONT hFont = 0, hPrevFont = 0;
70 RECT rect;
71 HWND hItem;
72 HDC hdc;
73 int i, buttons;
74 int bspace, bw, bh, theight, tleft, wwidth, wheight, bpos;
75 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
76 NONCLIENTMETRICSW nclm;
77 LPCWSTR lpszText;
78 WCHAR buf[256];
80 nclm.cbSize = sizeof(nclm);
81 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
82 hFont = CreateFontIndirectW (&nclm.lfMessageFont);
83 /* set button font */
84 for (i=1; i < 8; i++)
85 SendDlgItemMessageW (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
86 /* set text font */
87 SendDlgItemMessageW (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
89 if (HIWORD(lpmb->lpszCaption)) {
90 SetWindowTextW(hwnd, lpmb->lpszCaption);
91 } else {
92 UINT res_id = LOWORD(lpmb->lpszCaption);
93 if (res_id)
95 if (LoadStringW(lpmb->hInstance, res_id, buf, 256))
96 SetWindowTextW(hwnd, buf);
98 else
100 if (LoadStringW(user32_module, IDS_ERROR, buf, 256))
101 SetWindowTextW(hwnd, buf);
104 if (HIWORD(lpmb->lpszText)) {
105 lpszText = lpmb->lpszText;
106 } else {
107 lpszText = buf;
108 if (!LoadStringW(lpmb->hInstance, LOWORD(lpmb->lpszText), buf, 256))
109 *buf = 0; /* FIXME ?? */
112 TRACE_(msgbox)("%s\n", debugstr_w(lpszText));
113 SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
115 /* Hide not selected buttons */
116 switch(lpmb->dwStyle & MB_TYPEMASK) {
117 case MB_OK:
118 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
119 /* fall through */
120 case MB_OKCANCEL:
121 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
122 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
123 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
124 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
125 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
126 break;
127 case MB_ABORTRETRYIGNORE:
128 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
129 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
130 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
131 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
132 break;
133 case MB_YESNO:
134 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
135 /* fall through */
136 case MB_YESNOCANCEL:
137 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
138 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
139 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
140 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
141 break;
142 case MB_RETRYCANCEL:
143 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
144 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
145 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
146 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
147 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
148 break;
150 /* Set the icon */
151 switch(lpmb->dwStyle & MB_ICONMASK) {
152 case MB_ICONEXCLAMATION:
153 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
154 (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
155 break;
156 case MB_ICONQUESTION:
157 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
158 (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
159 break;
160 case MB_ICONASTERISK:
161 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
162 (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
163 break;
164 case MB_ICONHAND:
165 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
166 (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
167 break;
168 case MB_USERICON:
169 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
170 (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
171 break;
172 default:
173 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
174 * So wine should do the same.
176 break;
179 /* Position everything */
180 GetWindowRect(hwnd, &rect);
181 borheight = rect.bottom - rect.top;
182 borwidth = rect.right - rect.left;
183 GetClientRect(hwnd, &rect);
184 borheight -= rect.bottom - rect.top;
185 borwidth -= rect.right - rect.left;
187 /* Get the icon height */
188 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
189 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
190 if (!(lpmb->dwStyle & MB_ICONMASK))
192 rect.bottom = rect.top;
193 rect.right = rect.left;
195 iheight = rect.bottom - rect.top;
196 ileft = rect.left;
197 iwidth = rect.right - ileft;
199 hdc = GetDC(hwnd);
200 if (hFont)
201 hPrevFont = SelectObject(hdc, hFont);
203 /* Get the number of visible buttons and their size */
204 bh = bw = 1; /* Minimum button sizes */
205 for (buttons = 0, i = 1; i < 8; i++)
207 hItem = GetDlgItem(hwnd, i);
208 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
210 WCHAR buttonText[1024];
211 int w, h;
212 buttons++;
213 if (GetWindowTextW(hItem, buttonText, 1024))
215 DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
216 h = rect.bottom - rect.top;
217 w = rect.right - rect.left;
218 if (h > bh) bh = h;
219 if (w > bw) bw = w ;
223 bw = max(bw, bh * 2);
224 /* Button white space */
225 bh = bh * 2;
226 bw = bw * 2;
227 bspace = bw/3; /* Space between buttons */
229 /* Get the text size */
230 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
231 rect.top = rect.left = rect.bottom = 0;
232 DrawTextW( hdc, lpszText, -1, &rect,
233 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
234 /* Min text width corresponds to space for the buttons */
235 tleft = ileft;
236 if (iwidth) tleft += ileft + iwidth;
237 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
238 theight = rect.bottom;
240 if (hFont)
241 SelectObject(hdc, hPrevFont);
242 ReleaseDC(hItem, hdc);
244 tiheight = 16 + max(iheight, theight);
245 wwidth = tleft + twidth + ileft + borwidth;
246 wheight = 8 + tiheight + bh + borheight;
248 /* Resize the window */
249 SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
250 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
252 /* Position the icon */
253 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
254 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
256 /* Position the text */
257 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
258 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
260 /* Position the buttons */
261 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
262 for (buttons = i = 0; i < 7; i++) {
263 /* some arithmetic to get the right order for YesNoCancel windows */
264 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
265 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE) {
266 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
267 SetFocus(hItem);
268 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
270 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
271 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
272 bpos += bw + bspace;
276 /*handle modal message boxes*/
277 if (((lpmb->dwStyle & MB_TASKMODAL) && (lpmb->hwndOwner==NULL)) || (lpmb->dwStyle & MB_SYSTEMMODAL))
278 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
280 return hFont;
284 /**************************************************************************
285 * MSGBOX_DlgProc
287 * Dialog procedure for message boxes.
289 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
290 WPARAM wParam, LPARAM lParam )
292 HFONT hFont;
294 switch(message) {
295 case WM_INITDIALOG:
297 LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
298 SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
299 hFont = MSGBOX_OnInit(hwnd, mbp);
300 SetPropA(hwnd, "WINE_MSGBOX_HFONT", (HANDLE)hFont);
301 SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", (HANDLE)mbp->lpfnMsgBoxCallback);
302 break;
305 case WM_COMMAND:
306 switch (LOWORD(wParam))
308 case IDOK:
309 case IDCANCEL:
310 case IDABORT:
311 case IDRETRY:
312 case IDIGNORE:
313 case IDYES:
314 case IDNO:
315 hFont = GetPropA(hwnd, "WINE_MSGBOX_HFONT");
316 EndDialog(hwnd, wParam);
317 if (hFont)
318 DeleteObject(hFont);
319 break;
321 break;
323 case WM_HELP:
325 MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
326 HELPINFO hi;
328 memcpy(&hi, (void *)lParam, sizeof(hi));
329 hi.dwContextId = GetWindowContextHelpId(hwnd);
331 if (callback)
332 callback(&hi);
333 else
334 SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
335 break;
338 default:
339 /* Ok. Ignore all the other messages */
340 TRACE("Message number 0x%04x is being ignored.\n", message);
341 break;
343 return 0;
347 /**************************************************************************
348 * MessageBoxA (USER32.@)
350 * NOTES
351 * The WARN is here to help debug erroneous MessageBoxes
352 * Use: WINEDEBUG=warn+dialog,+relay
354 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
356 return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
360 /**************************************************************************
361 * MessageBoxW (USER32.@)
363 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
365 return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
369 /**************************************************************************
370 * MessageBoxExA (USER32.@)
372 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
373 UINT type, WORD langid )
375 MSGBOXPARAMSA msgbox;
377 msgbox.cbSize = sizeof(msgbox);
378 msgbox.hwndOwner = hWnd;
379 msgbox.hInstance = 0;
380 msgbox.lpszText = text;
381 msgbox.lpszCaption = title;
382 msgbox.dwStyle = type;
383 msgbox.lpszIcon = NULL;
384 msgbox.dwContextHelpId = 0;
385 msgbox.lpfnMsgBoxCallback = NULL;
386 msgbox.dwLanguageId = langid;
388 return MessageBoxIndirectA(&msgbox);
391 /**************************************************************************
392 * MessageBoxExW (USER32.@)
394 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
395 UINT type, WORD langid )
397 MSGBOXPARAMSW msgbox;
399 msgbox.cbSize = sizeof(msgbox);
400 msgbox.hwndOwner = hWnd;
401 msgbox.hInstance = 0;
402 msgbox.lpszText = text;
403 msgbox.lpszCaption = title;
404 msgbox.dwStyle = type;
405 msgbox.lpszIcon = NULL;
406 msgbox.dwContextHelpId = 0;
407 msgbox.lpfnMsgBoxCallback = NULL;
408 msgbox.dwLanguageId = langid;
410 return MessageBoxIndirectW(&msgbox);
413 /**************************************************************************
414 * MessageBoxIndirectA (USER32.@)
416 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
418 MSGBOXPARAMSW msgboxW;
419 UNICODE_STRING textW, captionW, iconW;
420 int ret;
422 if (HIWORD(msgbox->lpszText))
423 RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
424 else
425 textW.Buffer = (LPWSTR)msgbox->lpszText;
426 if (HIWORD(msgbox->lpszCaption))
427 RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
428 else
429 captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
431 if (msgbox->dwStyle & MB_USERICON)
433 if (HIWORD(msgbox->lpszIcon))
434 RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
435 else
436 iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
438 else
439 iconW.Buffer = NULL;
441 msgboxW.cbSize = sizeof(msgboxW);
442 msgboxW.hwndOwner = msgbox->hwndOwner;
443 msgboxW.hInstance = msgbox->hInstance;
444 msgboxW.lpszText = textW.Buffer;
445 msgboxW.lpszCaption = captionW.Buffer;
446 msgboxW.dwStyle = msgbox->dwStyle;
447 msgboxW.lpszIcon = iconW.Buffer;
448 msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
449 msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
450 msgboxW.dwLanguageId = msgbox->dwLanguageId;
452 ret = MessageBoxIndirectW(&msgboxW);
454 if (HIWORD(textW.Buffer)) RtlFreeUnicodeString(&textW);
455 if (HIWORD(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
456 if (HIWORD(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
457 return ret;
460 /**************************************************************************
461 * MessageBoxIndirectW (USER32.@)
463 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
465 LPVOID tmplate;
466 HRSRC hRes;
467 int ret;
468 UINT i;
469 struct ThreadWindows threadWindows;
470 static const WCHAR msg_box_res_nameW[] = { 'M','S','G','B','O','X',0 };
472 if (!(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG,
473 msg_box_res_nameW, msgbox->dwLanguageId)))
474 return 0;
475 if (!(tmplate = (LPVOID)LoadResource(user32_module, hRes)))
476 return 0;
478 if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
480 threadWindows.numHandles = 0;
481 threadWindows.numAllocs = 10;
482 threadWindows.handles = HeapAlloc(GetProcessHeap(), 0, 10*sizeof(HWND));
483 EnumThreadWindows(GetCurrentThreadId(), MSGBOX_EnumProc, (LPARAM)&threadWindows);
486 ret=DialogBoxIndirectParamW(msgbox->hInstance, tmplate,
487 msgbox->hwndOwner, MSGBOX_DlgProc, (LPARAM)msgbox);
489 if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
491 for (i = 0; i < threadWindows.numHandles; i++)
492 EnableWindow(threadWindows.handles[i], TRUE);
493 HeapFree(GetProcessHeap(), 0, threadWindows.handles);
495 return ret;