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
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
);
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
;
61 static void MSGBOX_OnInit(HWND hwnd
, LPMSGBOXPARAMSW lpmb
)
68 int bspace
, bw
, bh
, theight
, tleft
, wwidth
, wheight
, wleft
, wtop
, bpos
;
69 int borheight
, borwidth
, iheight
, ileft
, iwidth
, twidth
, tiheight
;
70 NONCLIENTMETRICSW nclm
;
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
,
82 nclm
.cbSize
= sizeof(nclm
);
83 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS
, 0, &nclm
, 0);
85 if (!IS_INTRESOURCE(lpmb
->lpszCaption
)) {
86 SetWindowTextW(hwnd
, lpmb
->lpszCaption
);
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
) );
93 memcpy( buffer
, ptr
, len
* sizeof(WCHAR
) );
95 SetWindowTextW( hwnd
, buffer
);
96 HeapFree( GetProcessHeap(), 0, buffer
);
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
) );
105 memcpy( buffer
, ptr
, len
* sizeof(WCHAR
) );
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 */
123 switch(lpmb
->dwStyle
& MB_TYPEMASK
) {
125 NtUserDestroyWindow(GetDlgItem(hwnd
, IDCANCEL
));
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
));
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
));
147 NtUserDestroyWindow(GetDlgItem(hwnd
, IDCANCEL
));
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
));
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
));
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
);
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);
186 case MB_ICONQUESTION
:
187 SendDlgItemMessageW(hwnd
, MSGBOX_IDICON
, STM_SETICON
,
188 (WPARAM
)LoadIconW(0, (LPWSTR
)IDI_QUESTION
), 0);
190 case MB_ICONASTERISK
:
191 SendDlgItemMessageW(hwnd
, MSGBOX_IDICON
, STM_SETICON
,
192 (WPARAM
)LoadIconW(0, (LPWSTR
)IDI_ASTERISK
), 0);
195 SendDlgItemMessageW(hwnd
, MSGBOX_IDICON
, STM_SETICON
,
196 (WPARAM
)LoadIconW(0, (LPWSTR
)IDI_HAND
), 0);
199 SendDlgItemMessageW(hwnd
, MSGBOX_IDICON
, STM_SETICON
,
200 (WPARAM
)LoadIconW(lpmb
->hInstance
, lpmb
->lpszIcon
), 0);
203 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
204 * So wine should do the same.
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
;
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];
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
;
258 bw
= max(bw
, bh
* 2);
259 /* Button white space */
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 */
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
);
318 HeapFree( GetProcessHeap(), 0, buffer
);
322 /**************************************************************************
325 * Dialog procedure for message boxes.
327 static INT_PTR CALLBACK
MSGBOX_DlgProc( HWND hwnd
, UINT message
,
328 WPARAM wParam
, LPARAM lParam
)
333 LPMSGBOXPARAMSW mbp
= (LPMSGBOXPARAMSW
)lParam
;
334 SetWindowContextHelpId(hwnd
, mbp
->dwContextHelpId
);
335 MSGBOX_OnInit(hwnd
, mbp
);
336 SetPropA(hwnd
, "WINE_MSGBOX_HELPCALLBACK", mbp
->lpfnMsgBoxCallback
);
341 switch (LOWORD(wParam
))
352 EndDialog(hwnd
, wParam
);
355 FIXME("Help button not supported yet\n");
362 MSGBOXCALLBACK callback
= (MSGBOXCALLBACK
)GetPropA(hwnd
, "WINE_MSGBOX_HELPCALLBACK");
365 memcpy(&hi
, (void *)lParam
, sizeof(hi
));
366 hi
.dwContextId
= GetWindowContextHelpId(hwnd
);
371 SendMessageW(GetWindow(hwnd
, GW_OWNER
), WM_HELP
, 0, (LPARAM
)&hi
);
376 /* Ok. Ignore all the other messages */
377 TRACE("Message number 0x%04x is being ignored.\n", message
);
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
;
475 if (IS_INTRESOURCE(msgbox
->lpszText
))
476 textW
.Buffer
= (LPWSTR
)msgbox
->lpszText
;
478 RtlCreateUnicodeStringFromAsciiz(&textW
, msgbox
->lpszText
);
479 if (IS_INTRESOURCE(msgbox
->lpszCaption
))
480 captionW
.Buffer
= (LPWSTR
)msgbox
->lpszCaption
;
482 RtlCreateUnicodeStringFromAsciiz(&captionW
, msgbox
->lpszCaption
);
484 if (msgbox
->dwStyle
& MB_USERICON
)
486 if (IS_INTRESOURCE(msgbox
->lpszIcon
))
487 iconW
.Buffer
= (LPWSTR
)msgbox
->lpszIcon
;
489 RtlCreateUnicodeStringFromAsciiz(&iconW
, msgbox
->lpszIcon
);
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
);
513 /**************************************************************************
514 * MessageBoxIndirectW (USER32.@)
516 INT WINAPI
MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox
)
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
)))
530 if (!(tmplate
= LoadResource(user32_module
, hRes
)))
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
);