x3daudio1_5: Add stub dll.
[wine.git] / dlls / user32 / msgbox.c
blob7fb17bd4fcb8502044b80d6b2155fbbadff7377e
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 "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
34 WINE_DECLARE_DEBUG_CHANNEL(msgbox);
36 #define MSGBOX_IDICON stc1
37 #define MSGBOX_IDTEXT 100
38 #define IDS_ERROR 2
40 struct ThreadWindows
42 UINT numHandles;
43 UINT numAllocs;
44 HWND *handles;
47 static BOOL CALLBACK MSGBOX_EnumProc(HWND hwnd, LPARAM lParam)
49 struct ThreadWindows *threadWindows = (struct ThreadWindows *)lParam;
51 if (!EnableWindow(hwnd, FALSE))
53 if(threadWindows->numHandles >= threadWindows->numAllocs)
55 threadWindows->handles = HeapReAlloc(GetProcessHeap(), 0, threadWindows->handles,
56 (threadWindows->numAllocs*2)*sizeof(HWND));
57 threadWindows->numAllocs *= 2;
59 threadWindows->handles[threadWindows->numHandles++]=hwnd;
61 return TRUE;
64 static void MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
66 HFONT hPrevFont;
67 RECT rect;
68 HWND hItem;
69 HDC hdc;
70 int i, buttons;
71 int bspace, bw, bh, theight, tleft, wwidth, wheight, wleft, wtop, bpos;
72 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
73 NONCLIENTMETRICSW nclm;
74 HMONITOR monitor = 0;
75 MONITORINFO mon_info;
76 LPCWSTR lpszText;
77 WCHAR *buffer = NULL;
78 const WCHAR *ptr;
80 /* Index the order the buttons need to appear to an ID* constant */
81 static const int buttonOrder[10] = { IDYES, IDNO, IDOK, IDABORT, IDRETRY,
82 IDCANCEL, IDIGNORE, IDTRYAGAIN,
83 IDCONTINUE, IDHELP };
85 nclm.cbSize = sizeof(nclm);
86 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
88 if (!IS_INTRESOURCE(lpmb->lpszCaption)) {
89 SetWindowTextW(hwnd, lpmb->lpszCaption);
90 } else {
91 UINT len = LoadStringW( lpmb->hInstance, LOWORD(lpmb->lpszCaption), (LPWSTR)&ptr, 0 );
92 if (!len) len = LoadStringW( user32_module, IDS_ERROR, (LPWSTR)&ptr, 0 );
93 buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
94 if (buffer)
96 memcpy( buffer, ptr, len * sizeof(WCHAR) );
97 buffer[len] = 0;
98 SetWindowTextW( hwnd, buffer );
99 HeapFree( GetProcessHeap(), 0, buffer );
100 buffer = NULL;
103 if (IS_INTRESOURCE(lpmb->lpszText)) {
104 UINT len = LoadStringW( lpmb->hInstance, LOWORD(lpmb->lpszText), (LPWSTR)&ptr, 0 );
105 lpszText = buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
106 if (buffer)
108 memcpy( buffer, ptr, len * sizeof(WCHAR) );
109 buffer[len] = 0;
111 } else {
112 lpszText = lpmb->lpszText;
115 TRACE_(msgbox)("%s\n", debugstr_w(lpszText));
116 SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
118 /* Remove not selected buttons */
119 switch(lpmb->dwStyle & MB_TYPEMASK) {
120 case MB_OK:
121 DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
122 /* fall through */
123 case MB_OKCANCEL:
124 DestroyWindow(GetDlgItem(hwnd, IDABORT));
125 DestroyWindow(GetDlgItem(hwnd, IDRETRY));
126 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
127 DestroyWindow(GetDlgItem(hwnd, IDYES));
128 DestroyWindow(GetDlgItem(hwnd, IDNO));
129 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
130 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
131 break;
132 case MB_ABORTRETRYIGNORE:
133 DestroyWindow(GetDlgItem(hwnd, IDOK));
134 DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
135 DestroyWindow(GetDlgItem(hwnd, IDYES));
136 DestroyWindow(GetDlgItem(hwnd, IDNO));
137 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
138 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
139 break;
140 case MB_YESNO:
141 DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
142 /* fall through */
143 case MB_YESNOCANCEL:
144 DestroyWindow(GetDlgItem(hwnd, IDOK));
145 DestroyWindow(GetDlgItem(hwnd, IDABORT));
146 DestroyWindow(GetDlgItem(hwnd, IDRETRY));
147 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
148 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
149 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
150 break;
151 case MB_RETRYCANCEL:
152 DestroyWindow(GetDlgItem(hwnd, IDOK));
153 DestroyWindow(GetDlgItem(hwnd, IDABORT));
154 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
155 DestroyWindow(GetDlgItem(hwnd, IDYES));
156 DestroyWindow(GetDlgItem(hwnd, IDNO));
157 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
158 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
159 break;
160 case MB_CANCELTRYCONTINUE:
161 DestroyWindow(GetDlgItem(hwnd, IDOK));
162 DestroyWindow(GetDlgItem(hwnd, IDABORT));
163 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
164 DestroyWindow(GetDlgItem(hwnd, IDYES));
165 DestroyWindow(GetDlgItem(hwnd, IDNO));
166 DestroyWindow(GetDlgItem(hwnd, IDRETRY));
168 /* Set the icon */
169 switch(lpmb->dwStyle & MB_ICONMASK) {
170 case MB_ICONEXCLAMATION:
171 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
172 (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
173 break;
174 case MB_ICONQUESTION:
175 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
176 (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
177 break;
178 case MB_ICONASTERISK:
179 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
180 (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
181 break;
182 case MB_ICONHAND:
183 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
184 (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
185 break;
186 case MB_USERICON:
187 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
188 (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
189 break;
190 default:
191 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
192 * So wine should do the same.
194 break;
197 /* Remove Help button unless MB_HELP supplied */
198 if (!(lpmb->dwStyle & MB_HELP)) {
199 DestroyWindow(GetDlgItem(hwnd, IDHELP));
202 /* Position everything */
203 GetWindowRect(hwnd, &rect);
204 borheight = rect.bottom - rect.top;
205 borwidth = rect.right - rect.left;
206 GetClientRect(hwnd, &rect);
207 borheight -= rect.bottom - rect.top;
208 borwidth -= rect.right - rect.left;
210 /* Get the icon height */
211 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
212 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
213 if (!(lpmb->dwStyle & MB_ICONMASK))
215 rect.bottom = rect.top;
216 rect.right = rect.left;
218 iheight = rect.bottom - rect.top;
219 ileft = rect.left;
220 iwidth = rect.right - ileft;
222 hdc = GetDC(hwnd);
223 hPrevFont = SelectObject( hdc, (HFONT)SendMessageW( hwnd, WM_GETFONT, 0, 0 ));
225 /* Get the number of visible buttons and their size */
226 bh = bw = 1; /* Minimum button sizes */
227 for (buttons = 0, i = IDOK; i <= IDCONTINUE; i++)
229 if (i == IDCLOSE) continue; /* No CLOSE button */
230 hItem = GetDlgItem(hwnd, i);
231 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
233 WCHAR buttonText[1024];
234 int w, h;
235 buttons++;
236 if (GetWindowTextW(hItem, buttonText, 1024))
238 DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
239 h = rect.bottom - rect.top;
240 w = rect.right - rect.left;
241 if (h > bh) bh = h;
242 if (w > bw) bw = w ;
246 bw = max(bw, bh * 2);
247 /* Button white space */
248 bh = bh * 2;
249 bw = bw * 2;
250 bspace = bw/3; /* Space between buttons */
252 /* Get the text size */
253 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
254 rect.top = rect.left = rect.bottom = 0;
255 DrawTextW( hdc, lpszText, -1, &rect,
256 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
257 /* Min text width corresponds to space for the buttons */
258 tleft = ileft;
259 if (iwidth) tleft += ileft + iwidth;
260 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
261 theight = rect.bottom;
263 SelectObject(hdc, hPrevFont);
264 ReleaseDC(hwnd, hdc);
266 tiheight = 16 + max(iheight, theight);
267 wwidth = tleft + twidth + ileft + borwidth;
268 wheight = 8 + tiheight + bh + borheight;
270 /* Message boxes are always desktop centered, so query desktop size and center window */
271 monitor = MonitorFromWindow(lpmb->hwndOwner ? lpmb->hwndOwner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY);
272 mon_info.cbSize = sizeof(mon_info);
273 GetMonitorInfoW(monitor, &mon_info);
274 wleft = (mon_info.rcWork.left + mon_info.rcWork.right - wwidth) / 2;
275 wtop = (mon_info.rcWork.top + mon_info.rcWork.bottom - wheight) / 2;
277 /* Resize and center the window */
278 SetWindowPos(hwnd, 0, wleft, wtop, wwidth, wheight,
279 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
281 /* Position the icon */
282 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
283 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
285 /* Position the text */
286 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
287 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
289 /* Position the buttons */
290 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
291 for (buttons = i = 0; i < (sizeof(buttonOrder) / sizeof(buttonOrder[0])); i++) {
293 /* Convert the button order to ID* value to order for the buttons */
294 hItem = GetDlgItem(hwnd, buttonOrder[i]);
295 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE) {
296 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
297 SetFocus(hItem);
298 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
300 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
301 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
302 bpos += bw + bspace;
306 /*handle modal message boxes*/
307 if (((lpmb->dwStyle & MB_TASKMODAL) && (lpmb->hwndOwner==NULL)) || (lpmb->dwStyle & MB_SYSTEMMODAL))
308 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
310 HeapFree( GetProcessHeap(), 0, buffer );
314 /**************************************************************************
315 * MSGBOX_DlgProc
317 * Dialog procedure for message boxes.
319 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
320 WPARAM wParam, LPARAM lParam )
322 switch(message) {
323 case WM_INITDIALOG:
325 LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
326 SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
327 MSGBOX_OnInit(hwnd, mbp);
328 SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", mbp->lpfnMsgBoxCallback);
329 break;
332 case WM_COMMAND:
333 switch (LOWORD(wParam))
335 case IDOK:
336 case IDCANCEL:
337 case IDABORT:
338 case IDRETRY:
339 case IDIGNORE:
340 case IDYES:
341 case IDNO:
342 case IDTRYAGAIN:
343 case IDCONTINUE:
344 EndDialog(hwnd, wParam);
345 break;
346 case IDHELP:
347 FIXME("Help button not supported yet\n");
348 break;
350 break;
352 case WM_HELP:
354 MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
355 HELPINFO hi;
357 memcpy(&hi, (void *)lParam, sizeof(hi));
358 hi.dwContextId = GetWindowContextHelpId(hwnd);
360 if (callback)
361 callback(&hi);
362 else
363 SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
364 break;
367 default:
368 /* Ok. Ignore all the other messages */
369 TRACE("Message number 0x%04x is being ignored.\n", message);
370 break;
372 return 0;
376 /**************************************************************************
377 * MessageBoxA (USER32.@)
379 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
381 return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
385 /**************************************************************************
386 * MessageBoxW (USER32.@)
388 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
390 return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
394 /**************************************************************************
395 * MessageBoxExA (USER32.@)
397 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
398 UINT type, WORD langid )
400 MSGBOXPARAMSA msgbox;
402 msgbox.cbSize = sizeof(msgbox);
403 msgbox.hwndOwner = hWnd;
404 msgbox.hInstance = 0;
405 msgbox.lpszText = text;
406 msgbox.lpszCaption = title;
407 msgbox.dwStyle = type;
408 msgbox.lpszIcon = NULL;
409 msgbox.dwContextHelpId = 0;
410 msgbox.lpfnMsgBoxCallback = NULL;
411 msgbox.dwLanguageId = langid;
413 return MessageBoxIndirectA(&msgbox);
416 /**************************************************************************
417 * MessageBoxExW (USER32.@)
419 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
420 UINT type, WORD langid )
422 MSGBOXPARAMSW msgbox;
424 msgbox.cbSize = sizeof(msgbox);
425 msgbox.hwndOwner = hWnd;
426 msgbox.hInstance = 0;
427 msgbox.lpszText = text;
428 msgbox.lpszCaption = title;
429 msgbox.dwStyle = type;
430 msgbox.lpszIcon = NULL;
431 msgbox.dwContextHelpId = 0;
432 msgbox.lpfnMsgBoxCallback = NULL;
433 msgbox.dwLanguageId = langid;
435 return MessageBoxIndirectW(&msgbox);
438 /**************************************************************************
439 * MessageBoxTimeoutA (USER32.@)
441 INT WINAPI MessageBoxTimeoutA( HWND hWnd, LPCSTR text, LPCSTR title,
442 UINT type, WORD langid, DWORD timeout )
444 FIXME("timeout not supported (%u)\n", timeout);
445 return MessageBoxExA( hWnd, text, title, type, langid );
448 /**************************************************************************
449 * MessageBoxTimeoutW (USER32.@)
451 INT WINAPI MessageBoxTimeoutW( HWND hWnd, LPCWSTR text, LPCWSTR title,
452 UINT type, WORD langid, DWORD timeout )
454 FIXME("timeout not supported (%u)\n", timeout);
455 return MessageBoxExW( hWnd, text, title, type, langid );
458 /**************************************************************************
459 * MessageBoxIndirectA (USER32.@)
461 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
463 MSGBOXPARAMSW msgboxW;
464 UNICODE_STRING textW, captionW, iconW;
465 int ret;
467 if (IS_INTRESOURCE(msgbox->lpszText))
468 textW.Buffer = (LPWSTR)msgbox->lpszText;
469 else
470 RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
471 if (IS_INTRESOURCE(msgbox->lpszCaption))
472 captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
473 else
474 RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
476 if (msgbox->dwStyle & MB_USERICON)
478 if (IS_INTRESOURCE(msgbox->lpszIcon))
479 iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
480 else
481 RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
483 else
484 iconW.Buffer = NULL;
486 msgboxW.cbSize = sizeof(msgboxW);
487 msgboxW.hwndOwner = msgbox->hwndOwner;
488 msgboxW.hInstance = msgbox->hInstance;
489 msgboxW.lpszText = textW.Buffer;
490 msgboxW.lpszCaption = captionW.Buffer;
491 msgboxW.dwStyle = msgbox->dwStyle;
492 msgboxW.lpszIcon = iconW.Buffer;
493 msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
494 msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
495 msgboxW.dwLanguageId = msgbox->dwLanguageId;
497 ret = MessageBoxIndirectW(&msgboxW);
499 if (!IS_INTRESOURCE(textW.Buffer)) RtlFreeUnicodeString(&textW);
500 if (!IS_INTRESOURCE(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
501 if (!IS_INTRESOURCE(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
502 return ret;
505 /**************************************************************************
506 * MessageBoxIndirectW (USER32.@)
508 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
510 LPVOID tmplate;
511 HRSRC hRes;
512 int ret;
513 UINT i;
514 struct ThreadWindows threadWindows;
515 static const WCHAR msg_box_res_nameW[] = { 'M','S','G','B','O','X',0 };
517 if (!(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG,
518 msg_box_res_nameW, msgbox->dwLanguageId)))
520 if (!msgbox->dwLanguageId ||
521 !(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG, msg_box_res_nameW, LANG_NEUTRAL)))
522 return 0;
524 if (!(tmplate = LoadResource(user32_module, hRes)))
525 return 0;
527 if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
529 threadWindows.numHandles = 0;
530 threadWindows.numAllocs = 10;
531 threadWindows.handles = HeapAlloc(GetProcessHeap(), 0, 10*sizeof(HWND));
532 EnumThreadWindows(GetCurrentThreadId(), MSGBOX_EnumProc, (LPARAM)&threadWindows);
535 ret=DialogBoxIndirectParamW(msgbox->hInstance, tmplate,
536 msgbox->hwndOwner, MSGBOX_DlgProc, (LPARAM)msgbox);
538 if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
540 for (i = 0; i < threadWindows.numHandles; i++)
541 EnableWindow(threadWindows.handles[i], TRUE);
542 HeapFree(GetProcessHeap(), 0, threadWindows.handles);
544 return ret;