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 "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dialog
);
34 WINE_DECLARE_DEBUG_CHANNEL(msgbox
);
36 #define MSGBOX_IDICON 1088
37 #define MSGBOX_IDTEXT 100
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
;
64 static HFONT
MSGBOX_OnInit(HWND hwnd
, LPMSGBOXPARAMSW lpmb
)
66 HFONT hFont
= 0, hPrevFont
= 0;
71 int bspace
, bw
, bh
, theight
, tleft
, wwidth
, wheight
, wleft
, wtop
, bpos
;
72 int borheight
, borwidth
, iheight
, ileft
, iwidth
, twidth
, tiheight
;
73 NONCLIENTMETRICSW nclm
;
80 /* Index the order the buttons need to appear to an ID* constant */
81 static const int buttonOrder
[10] = { 6, 7, 1, 3, 4, 2, 5, 10, 11, 9 };
83 nclm
.cbSize
= sizeof(nclm
);
84 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS
, 0, &nclm
, 0);
85 hFont
= CreateFontIndirectW (&nclm
.lfMessageFont
);
87 for (i
=1; i
< 12; i
++)
88 /* No button 8 (Close) */
90 SendDlgItemMessageW (hwnd
, i
, WM_SETFONT
, (WPARAM
)hFont
, 0);
93 SendDlgItemMessageW (hwnd
, MSGBOX_IDTEXT
, WM_SETFONT
, (WPARAM
)hFont
, 0);
95 if (!IS_INTRESOURCE(lpmb
->lpszCaption
)) {
96 SetWindowTextW(hwnd
, lpmb
->lpszCaption
);
98 UINT len
= LoadStringW( lpmb
->hInstance
, LOWORD(lpmb
->lpszCaption
), (LPWSTR
)&ptr
, 0 );
99 if (!len
) len
= LoadStringW( user32_module
, IDS_ERROR
, (LPWSTR
)&ptr
, 0 );
100 buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
103 memcpy( buffer
, ptr
, len
* sizeof(WCHAR
) );
105 SetWindowTextW( hwnd
, buffer
);
106 HeapFree( GetProcessHeap(), 0, buffer
);
110 if (IS_INTRESOURCE(lpmb
->lpszText
)) {
111 UINT len
= LoadStringW( lpmb
->hInstance
, LOWORD(lpmb
->lpszText
), (LPWSTR
)&ptr
, 0 );
112 lpszText
= buffer
= HeapAlloc( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
115 memcpy( buffer
, ptr
, len
* sizeof(WCHAR
) );
119 lpszText
= lpmb
->lpszText
;
122 TRACE_(msgbox
)("%s\n", debugstr_w(lpszText
));
123 SetWindowTextW(GetDlgItem(hwnd
, MSGBOX_IDTEXT
), lpszText
);
125 /* Remove not selected buttons */
126 switch(lpmb
->dwStyle
& MB_TYPEMASK
) {
128 DestroyWindow(GetDlgItem(hwnd
, IDCANCEL
));
131 DestroyWindow(GetDlgItem(hwnd
, IDABORT
));
132 DestroyWindow(GetDlgItem(hwnd
, IDRETRY
));
133 DestroyWindow(GetDlgItem(hwnd
, IDIGNORE
));
134 DestroyWindow(GetDlgItem(hwnd
, IDYES
));
135 DestroyWindow(GetDlgItem(hwnd
, IDNO
));
136 DestroyWindow(GetDlgItem(hwnd
, IDTRYAGAIN
));
137 DestroyWindow(GetDlgItem(hwnd
, IDCONTINUE
));
139 case MB_ABORTRETRYIGNORE
:
140 DestroyWindow(GetDlgItem(hwnd
, IDOK
));
141 DestroyWindow(GetDlgItem(hwnd
, IDCANCEL
));
142 DestroyWindow(GetDlgItem(hwnd
, IDYES
));
143 DestroyWindow(GetDlgItem(hwnd
, IDNO
));
144 DestroyWindow(GetDlgItem(hwnd
, IDCONTINUE
));
145 DestroyWindow(GetDlgItem(hwnd
, IDTRYAGAIN
));
148 DestroyWindow(GetDlgItem(hwnd
, IDCANCEL
));
151 DestroyWindow(GetDlgItem(hwnd
, IDOK
));
152 DestroyWindow(GetDlgItem(hwnd
, IDABORT
));
153 DestroyWindow(GetDlgItem(hwnd
, IDRETRY
));
154 DestroyWindow(GetDlgItem(hwnd
, IDIGNORE
));
155 DestroyWindow(GetDlgItem(hwnd
, IDCONTINUE
));
156 DestroyWindow(GetDlgItem(hwnd
, IDTRYAGAIN
));
159 DestroyWindow(GetDlgItem(hwnd
, IDOK
));
160 DestroyWindow(GetDlgItem(hwnd
, IDABORT
));
161 DestroyWindow(GetDlgItem(hwnd
, IDIGNORE
));
162 DestroyWindow(GetDlgItem(hwnd
, IDYES
));
163 DestroyWindow(GetDlgItem(hwnd
, IDNO
));
164 DestroyWindow(GetDlgItem(hwnd
, IDCONTINUE
));
165 DestroyWindow(GetDlgItem(hwnd
, IDTRYAGAIN
));
167 case MB_CANCELTRYCONTINUE
:
168 DestroyWindow(GetDlgItem(hwnd
, IDOK
));
169 DestroyWindow(GetDlgItem(hwnd
, IDABORT
));
170 DestroyWindow(GetDlgItem(hwnd
, IDIGNORE
));
171 DestroyWindow(GetDlgItem(hwnd
, IDYES
));
172 DestroyWindow(GetDlgItem(hwnd
, IDNO
));
173 DestroyWindow(GetDlgItem(hwnd
, IDRETRY
));
176 switch(lpmb
->dwStyle
& MB_ICONMASK
) {
177 case MB_ICONEXCLAMATION
:
178 SendDlgItemMessageW(hwnd
, stc1
, STM_SETICON
,
179 (WPARAM
)LoadIconW(0, (LPWSTR
)IDI_EXCLAMATION
), 0);
181 case MB_ICONQUESTION
:
182 SendDlgItemMessageW(hwnd
, stc1
, STM_SETICON
,
183 (WPARAM
)LoadIconW(0, (LPWSTR
)IDI_QUESTION
), 0);
185 case MB_ICONASTERISK
:
186 SendDlgItemMessageW(hwnd
, stc1
, STM_SETICON
,
187 (WPARAM
)LoadIconW(0, (LPWSTR
)IDI_ASTERISK
), 0);
190 SendDlgItemMessageW(hwnd
, stc1
, STM_SETICON
,
191 (WPARAM
)LoadIconW(0, (LPWSTR
)IDI_HAND
), 0);
194 SendDlgItemMessageW(hwnd
, stc1
, STM_SETICON
,
195 (WPARAM
)LoadIconW(lpmb
->hInstance
, lpmb
->lpszIcon
), 0);
198 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
199 * So wine should do the same.
204 /* Remove Help button unless MB_HELP supplied */
205 if (!(lpmb
->dwStyle
& MB_HELP
)) {
206 DestroyWindow(GetDlgItem(hwnd
, IDHELP
));
209 /* Position everything */
210 GetWindowRect(hwnd
, &rect
);
211 borheight
= rect
.bottom
- rect
.top
;
212 borwidth
= rect
.right
- rect
.left
;
213 GetClientRect(hwnd
, &rect
);
214 borheight
-= rect
.bottom
- rect
.top
;
215 borwidth
-= rect
.right
- rect
.left
;
217 /* Get the icon height */
218 GetWindowRect(GetDlgItem(hwnd
, MSGBOX_IDICON
), &rect
);
219 MapWindowPoints(0, hwnd
, (LPPOINT
)&rect
, 2);
220 if (!(lpmb
->dwStyle
& MB_ICONMASK
))
222 rect
.bottom
= rect
.top
;
223 rect
.right
= rect
.left
;
225 iheight
= rect
.bottom
- rect
.top
;
227 iwidth
= rect
.right
- ileft
;
231 hPrevFont
= SelectObject(hdc
, hFont
);
233 /* Get the number of visible buttons and their size */
234 bh
= bw
= 1; /* Minimum button sizes */
235 for (buttons
= 0, i
= 1; i
< 12; i
++)
237 if (i
== 8) continue; /* No CLOSE button */
238 hItem
= GetDlgItem(hwnd
, i
);
239 if (GetWindowLongW(hItem
, GWL_STYLE
) & WS_VISIBLE
)
241 WCHAR buttonText
[1024];
244 if (GetWindowTextW(hItem
, buttonText
, 1024))
246 DrawTextW( hdc
, buttonText
, -1, &rect
, DT_LEFT
| DT_EXPANDTABS
| DT_CALCRECT
);
247 h
= rect
.bottom
- rect
.top
;
248 w
= rect
.right
- rect
.left
;
254 bw
= max(bw
, bh
* 2);
255 /* Button white space */
258 bspace
= bw
/3; /* Space between buttons */
260 /* Get the text size */
261 GetClientRect(GetDlgItem(hwnd
, MSGBOX_IDTEXT
), &rect
);
262 rect
.top
= rect
.left
= rect
.bottom
= 0;
263 DrawTextW( hdc
, lpszText
, -1, &rect
,
264 DT_LEFT
| DT_EXPANDTABS
| DT_WORDBREAK
| DT_CALCRECT
);
265 /* Min text width corresponds to space for the buttons */
267 if (iwidth
) tleft
+= ileft
+ iwidth
;
268 twidth
= max((bw
+ bspace
) * buttons
+ bspace
- tleft
, rect
.right
);
269 theight
= rect
.bottom
;
272 SelectObject(hdc
, hPrevFont
);
273 ReleaseDC(hwnd
, hdc
);
275 tiheight
= 16 + max(iheight
, theight
);
276 wwidth
= tleft
+ twidth
+ ileft
+ borwidth
;
277 wheight
= 8 + tiheight
+ bh
+ borheight
;
279 /* Message boxes are always desktop centered, so query desktop size and center window */
280 monitor
= MonitorFromWindow(lpmb
->hwndOwner
? lpmb
->hwndOwner
: GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY
);
281 mon_info
.cbSize
= sizeof(mon_info
);
282 GetMonitorInfoW(monitor
, &mon_info
);
283 wleft
= (mon_info
.rcWork
.left
+ mon_info
.rcWork
.right
- wwidth
) / 2;
284 wtop
= (mon_info
.rcWork
.top
+ mon_info
.rcWork
.bottom
- wheight
) / 2;
286 /* Resize and center the window */
287 SetWindowPos(hwnd
, 0, wleft
, wtop
, wwidth
, wheight
,
288 SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_NOREDRAW
);
290 /* Position the icon */
291 SetWindowPos(GetDlgItem(hwnd
, MSGBOX_IDICON
), 0, ileft
, (tiheight
- iheight
) / 2, 0, 0,
292 SWP_NOSIZE
| SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_NOREDRAW
);
294 /* Position the text */
295 SetWindowPos(GetDlgItem(hwnd
, MSGBOX_IDTEXT
), 0, tleft
, (tiheight
- theight
) / 2, twidth
, theight
,
296 SWP_NOZORDER
| SWP_NOACTIVATE
| SWP_NOREDRAW
);
298 /* Position the buttons */
299 bpos
= (wwidth
- (bw
+ bspace
) * buttons
+ bspace
) / 2;
300 for (buttons
= i
= 0; i
< (sizeof(buttonOrder
) / sizeof(buttonOrder
[0])); i
++) {
302 /* Convert the button order to ID* value to order for the buttons */
303 hItem
= GetDlgItem(hwnd
, buttonOrder
[i
]);
304 if (GetWindowLongW(hItem
, GWL_STYLE
) & WS_VISIBLE
) {
305 if (buttons
++ == ((lpmb
->dwStyle
& MB_DEFMASK
) >> 8)) {
307 SendMessageW( hItem
, BM_SETSTYLE
, BS_DEFPUSHBUTTON
, TRUE
);
309 SetWindowPos(hItem
, 0, bpos
, tiheight
, bw
, bh
,
310 SWP_NOZORDER
|SWP_NOACTIVATE
|SWP_NOREDRAW
);
315 /*handle modal message boxes*/
316 if (((lpmb
->dwStyle
& MB_TASKMODAL
) && (lpmb
->hwndOwner
==NULL
)) || (lpmb
->dwStyle
& MB_SYSTEMMODAL
))
317 SetWindowPos(hwnd
, HWND_TOP
, 0, 0, 0, 0, SWP_NOSIZE
| SWP_NOMOVE
);
319 HeapFree( GetProcessHeap(), 0, buffer
);
324 /**************************************************************************
327 * Dialog procedure for message boxes.
329 static INT_PTR CALLBACK
MSGBOX_DlgProc( HWND hwnd
, UINT message
,
330 WPARAM wParam
, LPARAM lParam
)
337 LPMSGBOXPARAMSW mbp
= (LPMSGBOXPARAMSW
)lParam
;
338 SetWindowContextHelpId(hwnd
, mbp
->dwContextHelpId
);
339 hFont
= MSGBOX_OnInit(hwnd
, mbp
);
340 SetPropA(hwnd
, "WINE_MSGBOX_HFONT", hFont
);
341 SetPropA(hwnd
, "WINE_MSGBOX_HELPCALLBACK", mbp
->lpfnMsgBoxCallback
);
346 switch (LOWORD(wParam
))
357 hFont
= GetPropA(hwnd
, "WINE_MSGBOX_HFONT");
358 EndDialog(hwnd
, wParam
);
363 FIXME("Help button not supported yet\n");
370 MSGBOXCALLBACK callback
= (MSGBOXCALLBACK
)GetPropA(hwnd
, "WINE_MSGBOX_HELPCALLBACK");
373 memcpy(&hi
, (void *)lParam
, sizeof(hi
));
374 hi
.dwContextId
= GetWindowContextHelpId(hwnd
);
379 SendMessageW(GetWindow(hwnd
, GW_OWNER
), WM_HELP
, 0, (LPARAM
)&hi
);
384 /* Ok. Ignore all the other messages */
385 TRACE("Message number 0x%04x is being ignored.\n", message
);
392 /**************************************************************************
393 * MessageBoxA (USER32.@)
395 INT WINAPI
MessageBoxA(HWND hWnd
, LPCSTR text
, LPCSTR title
, UINT type
)
397 return MessageBoxExA(hWnd
, text
, title
, type
, LANG_NEUTRAL
);
401 /**************************************************************************
402 * MessageBoxW (USER32.@)
404 INT WINAPI
MessageBoxW( HWND hwnd
, LPCWSTR text
, LPCWSTR title
, UINT type
)
406 return MessageBoxExW(hwnd
, text
, title
, type
, LANG_NEUTRAL
);
410 /**************************************************************************
411 * MessageBoxExA (USER32.@)
413 INT WINAPI
MessageBoxExA( HWND hWnd
, LPCSTR text
, LPCSTR title
,
414 UINT type
, WORD langid
)
416 MSGBOXPARAMSA msgbox
;
418 msgbox
.cbSize
= sizeof(msgbox
);
419 msgbox
.hwndOwner
= hWnd
;
420 msgbox
.hInstance
= 0;
421 msgbox
.lpszText
= text
;
422 msgbox
.lpszCaption
= title
;
423 msgbox
.dwStyle
= type
;
424 msgbox
.lpszIcon
= NULL
;
425 msgbox
.dwContextHelpId
= 0;
426 msgbox
.lpfnMsgBoxCallback
= NULL
;
427 msgbox
.dwLanguageId
= langid
;
429 return MessageBoxIndirectA(&msgbox
);
432 /**************************************************************************
433 * MessageBoxExW (USER32.@)
435 INT WINAPI
MessageBoxExW( HWND hWnd
, LPCWSTR text
, LPCWSTR title
,
436 UINT type
, WORD langid
)
438 MSGBOXPARAMSW msgbox
;
440 msgbox
.cbSize
= sizeof(msgbox
);
441 msgbox
.hwndOwner
= hWnd
;
442 msgbox
.hInstance
= 0;
443 msgbox
.lpszText
= text
;
444 msgbox
.lpszCaption
= title
;
445 msgbox
.dwStyle
= type
;
446 msgbox
.lpszIcon
= NULL
;
447 msgbox
.dwContextHelpId
= 0;
448 msgbox
.lpfnMsgBoxCallback
= NULL
;
449 msgbox
.dwLanguageId
= langid
;
451 return MessageBoxIndirectW(&msgbox
);
454 /**************************************************************************
455 * MessageBoxIndirectA (USER32.@)
457 INT WINAPI
MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox
)
459 MSGBOXPARAMSW msgboxW
;
460 UNICODE_STRING textW
, captionW
, iconW
;
463 if (IS_INTRESOURCE(msgbox
->lpszText
))
464 textW
.Buffer
= (LPWSTR
)msgbox
->lpszText
;
466 RtlCreateUnicodeStringFromAsciiz(&textW
, msgbox
->lpszText
);
467 if (IS_INTRESOURCE(msgbox
->lpszCaption
))
468 captionW
.Buffer
= (LPWSTR
)msgbox
->lpszCaption
;
470 RtlCreateUnicodeStringFromAsciiz(&captionW
, msgbox
->lpszCaption
);
472 if (msgbox
->dwStyle
& MB_USERICON
)
474 if (IS_INTRESOURCE(msgbox
->lpszIcon
))
475 iconW
.Buffer
= (LPWSTR
)msgbox
->lpszIcon
;
477 RtlCreateUnicodeStringFromAsciiz(&iconW
, msgbox
->lpszIcon
);
482 msgboxW
.cbSize
= sizeof(msgboxW
);
483 msgboxW
.hwndOwner
= msgbox
->hwndOwner
;
484 msgboxW
.hInstance
= msgbox
->hInstance
;
485 msgboxW
.lpszText
= textW
.Buffer
;
486 msgboxW
.lpszCaption
= captionW
.Buffer
;
487 msgboxW
.dwStyle
= msgbox
->dwStyle
;
488 msgboxW
.lpszIcon
= iconW
.Buffer
;
489 msgboxW
.dwContextHelpId
= msgbox
->dwContextHelpId
;
490 msgboxW
.lpfnMsgBoxCallback
= msgbox
->lpfnMsgBoxCallback
;
491 msgboxW
.dwLanguageId
= msgbox
->dwLanguageId
;
493 ret
= MessageBoxIndirectW(&msgboxW
);
495 if (!IS_INTRESOURCE(textW
.Buffer
)) RtlFreeUnicodeString(&textW
);
496 if (!IS_INTRESOURCE(captionW
.Buffer
)) RtlFreeUnicodeString(&captionW
);
497 if (!IS_INTRESOURCE(iconW
.Buffer
)) RtlFreeUnicodeString(&iconW
);
501 /**************************************************************************
502 * MessageBoxIndirectW (USER32.@)
504 INT WINAPI
MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox
)
510 struct ThreadWindows threadWindows
;
511 static const WCHAR msg_box_res_nameW
[] = { 'M','S','G','B','O','X',0 };
513 if (!(hRes
= FindResourceExW(user32_module
, (LPWSTR
)RT_DIALOG
,
514 msg_box_res_nameW
, msgbox
->dwLanguageId
)))
516 if (!msgbox
->dwLanguageId
||
517 !(hRes
= FindResourceExW(user32_module
, (LPWSTR
)RT_DIALOG
, msg_box_res_nameW
, LANG_NEUTRAL
)))
520 if (!(tmplate
= LoadResource(user32_module
, hRes
)))
523 if ((msgbox
->dwStyle
& MB_TASKMODAL
) && (msgbox
->hwndOwner
==NULL
))
525 threadWindows
.numHandles
= 0;
526 threadWindows
.numAllocs
= 10;
527 threadWindows
.handles
= HeapAlloc(GetProcessHeap(), 0, 10*sizeof(HWND
));
528 EnumThreadWindows(GetCurrentThreadId(), MSGBOX_EnumProc
, (LPARAM
)&threadWindows
);
531 ret
=DialogBoxIndirectParamW(msgbox
->hInstance
, tmplate
,
532 msgbox
->hwndOwner
, MSGBOX_DlgProc
, (LPARAM
)msgbox
);
534 if ((msgbox
->dwStyle
& MB_TASKMODAL
) && (msgbox
->hwndOwner
==NULL
))
536 for (i
= 0; i
< threadWindows
.numHandles
; i
++)
537 EnableWindow(threadWindows
.handles
[i
], TRUE
);
538 HeapFree(GetProcessHeap(), 0, threadWindows
.handles
);