widl: Add support for protected attribute.
[wine.git] / dlls / user32 / msgbox.c
blob4a14a868c7332deea49af40f597a6fda7ea8820e
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winternl.h"
29 #include "dlgs.h"
30 #include "user_private.h"
31 #include "resources.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
35 WINE_DECLARE_DEBUG_CHANNEL(msgbox);
37 struct ThreadWindows
39 UINT numHandles;
40 UINT numAllocs;
41 HWND *handles;
44 static BOOL CALLBACK MSGBOX_EnumProc(HWND hwnd, LPARAM lParam)
46 struct ThreadWindows *threadWindows = (struct ThreadWindows *)lParam;
48 if (!EnableWindow(hwnd, FALSE))
50 if(threadWindows->numHandles >= threadWindows->numAllocs)
52 threadWindows->handles = HeapReAlloc(GetProcessHeap(), 0, threadWindows->handles,
53 (threadWindows->numAllocs*2)*sizeof(HWND));
54 threadWindows->numAllocs *= 2;
56 threadWindows->handles[threadWindows->numHandles++]=hwnd;
58 return TRUE;
61 static void MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
63 HFONT hPrevFont;
64 RECT rect;
65 HWND hItem;
66 HDC hdc;
67 int i, buttons;
68 int bspace, bw, bh, theight, tleft, wwidth, wheight, wleft, wtop, bpos;
69 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
70 NONCLIENTMETRICSW nclm;
71 HMONITOR monitor;
72 MONITORINFO mon_info;
73 LPCWSTR lpszText;
74 WCHAR *buffer = NULL;
75 const WCHAR *ptr;
77 /* Index the order the buttons need to appear to an ID* constant */
78 static const int buttonOrder[10] = { IDYES, IDNO, IDOK, IDABORT, IDRETRY,
79 IDCANCEL, IDIGNORE, IDTRYAGAIN,
80 IDCONTINUE, IDHELP };
82 nclm.cbSize = sizeof(nclm);
83 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
85 if (!IS_INTRESOURCE(lpmb->lpszCaption)) {
86 SetWindowTextW(hwnd, lpmb->lpszCaption);
87 } else {
88 UINT len = LoadStringW( lpmb->hInstance, LOWORD(lpmb->lpszCaption), (LPWSTR)&ptr, 0 );
89 if (!len) len = LoadStringW( user32_module, IDS_ERROR, (LPWSTR)&ptr, 0 );
90 buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
91 if (buffer)
93 memcpy( buffer, ptr, len * sizeof(WCHAR) );
94 buffer[len] = 0;
95 SetWindowTextW( hwnd, buffer );
96 HeapFree( GetProcessHeap(), 0, buffer );
97 buffer = NULL;
100 if (IS_INTRESOURCE(lpmb->lpszText)) {
101 UINT len = LoadStringW( lpmb->hInstance, LOWORD(lpmb->lpszText), (LPWSTR)&ptr, 0 );
102 lpszText = buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
103 if (buffer)
105 memcpy( buffer, ptr, len * sizeof(WCHAR) );
106 buffer[len] = 0;
108 } else {
109 lpszText = lpmb->lpszText;
112 /* handle modal message boxes */
113 if (((lpmb->dwStyle & MB_TASKMODAL) && (lpmb->hwndOwner==NULL)))
114 NtUserSetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE );
115 else if (lpmb->dwStyle & MB_SYSTEMMODAL)
116 NtUserSetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED );
118 TRACE_(msgbox)("%s\n", debugstr_w(lpszText));
119 SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
121 /* Remove not selected buttons and assign the WS_GROUP style to the first button */
122 hItem = 0;
123 switch(lpmb->dwStyle & MB_TYPEMASK) {
124 case MB_OK:
125 NtUserDestroyWindow(GetDlgItem(hwnd, IDCANCEL));
126 /* fall through */
127 case MB_OKCANCEL:
128 hItem = GetDlgItem(hwnd, IDOK);
129 NtUserDestroyWindow(GetDlgItem(hwnd, IDABORT));
130 NtUserDestroyWindow(GetDlgItem(hwnd, IDRETRY));
131 NtUserDestroyWindow(GetDlgItem(hwnd, IDIGNORE));
132 NtUserDestroyWindow(GetDlgItem(hwnd, IDYES));
133 NtUserDestroyWindow(GetDlgItem(hwnd, IDNO));
134 NtUserDestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
135 NtUserDestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
136 break;
137 case MB_ABORTRETRYIGNORE:
138 hItem = GetDlgItem(hwnd, IDABORT);
139 NtUserDestroyWindow(GetDlgItem(hwnd, IDOK));
140 NtUserDestroyWindow(GetDlgItem(hwnd, IDCANCEL));
141 NtUserDestroyWindow(GetDlgItem(hwnd, IDYES));
142 NtUserDestroyWindow(GetDlgItem(hwnd, IDNO));
143 NtUserDestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
144 NtUserDestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
145 break;
146 case MB_YESNO:
147 NtUserDestroyWindow(GetDlgItem(hwnd, IDCANCEL));
148 /* fall through */
149 case MB_YESNOCANCEL:
150 hItem = GetDlgItem(hwnd, IDYES);
151 NtUserDestroyWindow(GetDlgItem(hwnd, IDOK));
152 NtUserDestroyWindow(GetDlgItem(hwnd, IDABORT));
153 NtUserDestroyWindow(GetDlgItem(hwnd, IDRETRY));
154 NtUserDestroyWindow(GetDlgItem(hwnd, IDIGNORE));
155 NtUserDestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
156 NtUserDestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
157 break;
158 case MB_RETRYCANCEL:
159 hItem = GetDlgItem(hwnd, IDRETRY);
160 NtUserDestroyWindow(GetDlgItem(hwnd, IDOK));
161 NtUserDestroyWindow(GetDlgItem(hwnd, IDABORT));
162 NtUserDestroyWindow(GetDlgItem(hwnd, IDIGNORE));
163 NtUserDestroyWindow(GetDlgItem(hwnd, IDYES));
164 NtUserDestroyWindow(GetDlgItem(hwnd, IDNO));
165 NtUserDestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
166 NtUserDestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
167 break;
168 case MB_CANCELTRYCONTINUE:
169 hItem = GetDlgItem(hwnd, IDCANCEL);
170 NtUserDestroyWindow(GetDlgItem(hwnd, IDOK));
171 NtUserDestroyWindow(GetDlgItem(hwnd, IDABORT));
172 NtUserDestroyWindow(GetDlgItem(hwnd, IDIGNORE));
173 NtUserDestroyWindow(GetDlgItem(hwnd, IDYES));
174 NtUserDestroyWindow(GetDlgItem(hwnd, IDNO));
175 NtUserDestroyWindow(GetDlgItem(hwnd, IDRETRY));
178 if (hItem) SetWindowLongW(hItem, GWL_STYLE, GetWindowLongW(hItem, GWL_STYLE) | WS_GROUP);
180 /* Set the icon */
181 switch(lpmb->dwStyle & MB_ICONMASK) {
182 case MB_ICONEXCLAMATION:
183 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
184 (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
185 break;
186 case MB_ICONQUESTION:
187 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
188 (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
189 break;
190 case MB_ICONASTERISK:
191 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
192 (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
193 break;
194 case MB_ICONHAND:
195 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
196 (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
197 break;
198 case MB_USERICON:
199 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
200 (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
201 break;
202 default:
203 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
204 * So wine should do the same.
206 break;
209 /* Remove Help button unless MB_HELP supplied */
210 if (!(lpmb->dwStyle & MB_HELP)) {
211 NtUserDestroyWindow(GetDlgItem(hwnd, IDHELP));
214 /* Position everything */
215 GetWindowRect(hwnd, &rect);
216 borheight = rect.bottom - rect.top;
217 borwidth = rect.right - rect.left;
218 GetClientRect(hwnd, &rect);
219 borheight -= rect.bottom - rect.top;
220 borwidth -= rect.right - rect.left;
222 /* Get the icon height */
223 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
224 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
225 if (!(lpmb->dwStyle & MB_ICONMASK))
227 rect.bottom = rect.top;
228 rect.right = rect.left;
230 iheight = rect.bottom - rect.top;
231 ileft = rect.left;
232 iwidth = rect.right - ileft;
234 hdc = NtUserGetDC(hwnd);
235 hPrevFont = SelectObject( hdc, (HFONT)SendMessageW( hwnd, WM_GETFONT, 0, 0 ));
237 /* Get the number of visible buttons and their size */
238 bh = bw = 1; /* Minimum button sizes */
239 for (buttons = 0, i = IDOK; i <= IDCONTINUE; i++)
241 if (i == IDCLOSE) continue; /* No CLOSE button */
242 hItem = GetDlgItem(hwnd, i);
243 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
245 WCHAR buttonText[1024];
246 int w, h;
247 buttons++;
248 if (GetWindowTextW(hItem, buttonText, 1024))
250 DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
251 h = rect.bottom - rect.top;
252 w = rect.right - rect.left;
253 if (h > bh) bh = h;
254 if (w > bw) bw = w ;
258 bw = max(bw, bh * 2);
259 /* Button white space */
260 bh = bh * 2;
261 bw = bw * 2;
262 bspace = bw/3; /* Space between buttons */
264 /* Get the text size */
265 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
266 rect.top = rect.left = rect.bottom = 0;
267 DrawTextW(hdc, lpszText, -1, &rect,
268 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT | DT_NOPREFIX);
269 /* Min text width corresponds to space for the buttons */
270 tleft = ileft;
271 if (iwidth) tleft += ileft + iwidth;
272 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
273 theight = rect.bottom;
275 SelectObject(hdc, hPrevFont);
276 NtUserReleaseDC( hwnd, hdc );
278 tiheight = 16 + max(iheight, theight);
279 wwidth = tleft + twidth + ileft + borwidth;
280 wheight = 8 + tiheight + bh + borheight;
282 /* Message boxes are always desktop centered, so query desktop size and center window */
283 monitor = MonitorFromWindow(lpmb->hwndOwner ? lpmb->hwndOwner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY);
284 mon_info.cbSize = sizeof(mon_info);
285 GetMonitorInfoW(monitor, &mon_info);
286 wleft = (mon_info.rcWork.left + mon_info.rcWork.right - wwidth) / 2;
287 wtop = (mon_info.rcWork.top + mon_info.rcWork.bottom - wheight) / 2;
289 /* Resize and center the window */
290 NtUserSetWindowPos( hwnd, 0, wleft, wtop, wwidth, wheight,
291 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW );
293 /* Position the icon */
294 NtUserSetWindowPos( GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
295 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW );
297 /* Position the text */
298 NtUserSetWindowPos( GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
299 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW );
301 /* Position the buttons */
302 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
303 for (buttons = i = 0; i < ARRAY_SIZE(buttonOrder); i++) {
305 /* Convert the button order to ID* value to order for the buttons */
306 hItem = GetDlgItem(hwnd, buttonOrder[i]);
307 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE) {
308 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
309 NtUserSetFocus(hItem);
310 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
312 NtUserSetWindowPos( hItem, 0, bpos, tiheight, bw, bh,
313 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW );
314 bpos += bw + bspace;
318 HeapFree( GetProcessHeap(), 0, buffer );
322 /**************************************************************************
323 * MSGBOX_DlgProc
325 * Dialog procedure for message boxes.
327 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
328 WPARAM wParam, LPARAM lParam )
330 switch(message) {
331 case WM_INITDIALOG:
333 LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
334 SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
335 MSGBOX_OnInit(hwnd, mbp);
336 SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", mbp->lpfnMsgBoxCallback);
337 break;
340 case WM_COMMAND:
341 switch (LOWORD(wParam))
343 case IDOK:
344 case IDCANCEL:
345 case IDABORT:
346 case IDRETRY:
347 case IDIGNORE:
348 case IDYES:
349 case IDNO:
350 case IDTRYAGAIN:
351 case IDCONTINUE:
352 EndDialog(hwnd, wParam);
353 break;
354 case IDHELP:
355 FIXME("Help button not supported yet\n");
356 break;
358 break;
360 case WM_HELP:
362 MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
363 HELPINFO hi;
365 memcpy(&hi, (void *)lParam, sizeof(hi));
366 hi.dwContextId = GetWindowContextHelpId(hwnd);
368 if (callback)
369 callback(&hi);
370 else
371 SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
372 break;
375 default:
376 /* Ok. Ignore all the other messages */
377 TRACE("Message number 0x%04x is being ignored.\n", message);
378 break;
380 return 0;
384 /**************************************************************************
385 * MessageBoxA (USER32.@)
387 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
389 return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
393 /**************************************************************************
394 * MessageBoxW (USER32.@)
396 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
398 return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
402 /**************************************************************************
403 * MessageBoxExA (USER32.@)
405 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
406 UINT type, WORD langid )
408 MSGBOXPARAMSA msgbox;
410 msgbox.cbSize = sizeof(msgbox);
411 msgbox.hwndOwner = hWnd;
412 msgbox.hInstance = 0;
413 msgbox.lpszText = text;
414 msgbox.lpszCaption = title;
415 msgbox.dwStyle = type;
416 msgbox.lpszIcon = NULL;
417 msgbox.dwContextHelpId = 0;
418 msgbox.lpfnMsgBoxCallback = NULL;
419 msgbox.dwLanguageId = langid;
421 return MessageBoxIndirectA(&msgbox);
424 /**************************************************************************
425 * MessageBoxExW (USER32.@)
427 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
428 UINT type, WORD langid )
430 MSGBOXPARAMSW msgbox;
432 msgbox.cbSize = sizeof(msgbox);
433 msgbox.hwndOwner = hWnd;
434 msgbox.hInstance = 0;
435 msgbox.lpszText = text;
436 msgbox.lpszCaption = title;
437 msgbox.dwStyle = type;
438 msgbox.lpszIcon = NULL;
439 msgbox.dwContextHelpId = 0;
440 msgbox.lpfnMsgBoxCallback = NULL;
441 msgbox.dwLanguageId = langid;
443 return MessageBoxIndirectW(&msgbox);
446 /**************************************************************************
447 * MessageBoxTimeoutA (USER32.@)
449 INT WINAPI MessageBoxTimeoutA( HWND hWnd, LPCSTR text, LPCSTR title,
450 UINT type, WORD langid, DWORD timeout )
452 FIXME("timeout not supported (%lu)\n", timeout);
453 return MessageBoxExA( hWnd, text, title, type, langid );
456 /**************************************************************************
457 * MessageBoxTimeoutW (USER32.@)
459 INT WINAPI MessageBoxTimeoutW( HWND hWnd, LPCWSTR text, LPCWSTR title,
460 UINT type, WORD langid, DWORD timeout )
462 FIXME("timeout not supported (%lu)\n", timeout);
463 return MessageBoxExW( hWnd, text, title, type, langid );
466 /**************************************************************************
467 * MessageBoxIndirectA (USER32.@)
469 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
471 MSGBOXPARAMSW msgboxW;
472 UNICODE_STRING textW, captionW, iconW;
473 int ret;
475 if (IS_INTRESOURCE(msgbox->lpszText))
476 textW.Buffer = (LPWSTR)msgbox->lpszText;
477 else
478 RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
479 if (IS_INTRESOURCE(msgbox->lpszCaption))
480 captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
481 else
482 RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
484 if (msgbox->dwStyle & MB_USERICON)
486 if (IS_INTRESOURCE(msgbox->lpszIcon))
487 iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
488 else
489 RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
491 else
492 iconW.Buffer = NULL;
494 msgboxW.cbSize = sizeof(msgboxW);
495 msgboxW.hwndOwner = msgbox->hwndOwner;
496 msgboxW.hInstance = msgbox->hInstance;
497 msgboxW.lpszText = textW.Buffer;
498 msgboxW.lpszCaption = captionW.Buffer;
499 msgboxW.dwStyle = msgbox->dwStyle;
500 msgboxW.lpszIcon = iconW.Buffer;
501 msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
502 msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
503 msgboxW.dwLanguageId = msgbox->dwLanguageId;
505 ret = MessageBoxIndirectW(&msgboxW);
507 if (!IS_INTRESOURCE(textW.Buffer)) RtlFreeUnicodeString(&textW);
508 if (!IS_INTRESOURCE(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
509 if (!IS_INTRESOURCE(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
510 return ret;
513 /**************************************************************************
514 * MessageBoxIndirectW (USER32.@)
516 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
518 LPVOID tmplate;
519 HRSRC hRes;
520 int ret;
521 UINT i;
522 struct ThreadWindows threadWindows;
524 if (!(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG, L"MSGBOX", msgbox->dwLanguageId)))
526 if (!msgbox->dwLanguageId ||
527 !(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG, L"MSGBOX", LANG_NEUTRAL)))
528 return 0;
530 if (!(tmplate = LoadResource(user32_module, hRes)))
531 return 0;
533 if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
535 threadWindows.numHandles = 0;
536 threadWindows.numAllocs = 10;
537 threadWindows.handles = HeapAlloc(GetProcessHeap(), 0, 10*sizeof(HWND));
538 EnumThreadWindows(GetCurrentThreadId(), MSGBOX_EnumProc, (LPARAM)&threadWindows);
541 ret=DialogBoxIndirectParamW(msgbox->hInstance, tmplate,
542 msgbox->hwndOwner, MSGBOX_DlgProc, (LPARAM)msgbox);
544 if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
546 for (i = 0; i < threadWindows.numHandles; i++)
547 EnableWindow(threadWindows.handles[i], TRUE);
548 HeapFree(GetProcessHeap(), 0, threadWindows.handles);
550 return ret;