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
28 #include "wine/winbase16.h"
29 #include "wine/winuser16.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
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
;
67 static HFONT
MSGBOX_OnInit(HWND hwnd
, LPMSGBOXPARAMSW lpmb
)
69 HFONT hFont
= 0, hPrevFont
= 0;
74 int bspace
, bw
, bh
, theight
, tleft
, wwidth
, wheight
, bpos
;
75 int borheight
, borwidth
, iheight
, ileft
, iwidth
, twidth
, tiheight
;
76 NONCLIENTMETRICSW nclm
;
80 nclm
.cbSize
= sizeof(nclm
);
81 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS
, 0, &nclm
, 0);
82 hFont
= CreateFontIndirectW (&nclm
.lfMessageFont
);
85 SendDlgItemMessageW (hwnd
, i
, WM_SETFONT
, (WPARAM
)hFont
, 0);
87 SendDlgItemMessageW (hwnd
, MSGBOX_IDTEXT
, WM_SETFONT
, (WPARAM
)hFont
, 0);
89 if (HIWORD(lpmb
->lpszCaption
)) {
90 SetWindowTextW(hwnd
, lpmb
->lpszCaption
);
92 UINT res_id
= LOWORD(lpmb
->lpszCaption
);
95 if (LoadStringW(lpmb
->hInstance
, res_id
, buf
, 256))
96 SetWindowTextW(hwnd
, buf
);
100 if (LoadStringW(user32_module
, IDS_ERROR
, buf
, 256))
101 SetWindowTextW(hwnd
, buf
);
104 if (HIWORD(lpmb
->lpszText
)) {
105 lpszText
= lpmb
->lpszText
;
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
) {
118 ShowWindow(GetDlgItem(hwnd
, IDCANCEL
), SW_HIDE
);
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
);
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
);
134 ShowWindow(GetDlgItem(hwnd
, IDCANCEL
), SW_HIDE
);
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
);
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
);
151 switch(lpmb
->dwStyle
& MB_ICONMASK
) {
152 case MB_ICONEXCLAMATION
:
153 SendDlgItemMessageW(hwnd
, stc1
, STM_SETICON
,
154 (WPARAM
)LoadIconW(0, (LPWSTR
)IDI_EXCLAMATION
), 0);
156 case MB_ICONQUESTION
:
157 SendDlgItemMessageW(hwnd
, stc1
, STM_SETICON
,
158 (WPARAM
)LoadIconW(0, (LPWSTR
)IDI_QUESTION
), 0);
160 case MB_ICONASTERISK
:
161 SendDlgItemMessageW(hwnd
, stc1
, STM_SETICON
,
162 (WPARAM
)LoadIconW(0, (LPWSTR
)IDI_ASTERISK
), 0);
165 SendDlgItemMessageW(hwnd
, stc1
, STM_SETICON
,
166 (WPARAM
)LoadIconW(0, (LPWSTR
)IDI_HAND
), 0);
169 SendDlgItemMessageW(hwnd
, stc1
, STM_SETICON
,
170 (WPARAM
)LoadIconW(lpmb
->hInstance
, lpmb
->lpszIcon
), 0);
173 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
174 * So wine should do the same.
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
;
197 iwidth
= rect
.right
- ileft
;
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];
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
;
223 bw
= max(bw
, bh
* 2);
224 /* Button white space */
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 */
236 if (iwidth
) tleft
+= ileft
+ iwidth
;
237 twidth
= max((bw
+ bspace
) * buttons
+ bspace
- tleft
, rect
.right
);
238 theight
= rect
.bottom
;
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)) {
268 SendMessageW( hItem
, BM_SETSTYLE
, BS_DEFPUSHBUTTON
, TRUE
);
270 SetWindowPos(hItem
, 0, bpos
, tiheight
, bw
, bh
,
271 SWP_NOZORDER
|SWP_NOACTIVATE
|SWP_NOREDRAW
);
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
);
284 /**************************************************************************
287 * Dialog procedure for message boxes.
289 static INT_PTR CALLBACK
MSGBOX_DlgProc( HWND hwnd
, UINT message
,
290 WPARAM wParam
, LPARAM lParam
)
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
);
306 switch (LOWORD(wParam
))
315 hFont
= GetPropA(hwnd
, "WINE_MSGBOX_HFONT");
316 EndDialog(hwnd
, wParam
);
325 MSGBOXCALLBACK callback
= (MSGBOXCALLBACK
)GetPropA(hwnd
, "WINE_MSGBOX_HELPCALLBACK");
328 memcpy(&hi
, (void *)lParam
, sizeof(hi
));
329 hi
.dwContextId
= GetWindowContextHelpId(hwnd
);
334 SendMessageW(GetWindow(hwnd
, GW_OWNER
), WM_HELP
, 0, (LPARAM
)&hi
);
339 /* Ok. Ignore all the other messages */
340 TRACE("Message number 0x%04x is being ignored.\n", message
);
347 /**************************************************************************
348 * MessageBoxA (USER32.@)
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
;
422 if (HIWORD(msgbox
->lpszText
))
423 RtlCreateUnicodeStringFromAsciiz(&textW
, msgbox
->lpszText
);
425 textW
.Buffer
= (LPWSTR
)msgbox
->lpszText
;
426 if (HIWORD(msgbox
->lpszCaption
))
427 RtlCreateUnicodeStringFromAsciiz(&captionW
, msgbox
->lpszCaption
);
429 captionW
.Buffer
= (LPWSTR
)msgbox
->lpszCaption
;
431 if (msgbox
->dwStyle
& MB_USERICON
)
433 if (HIWORD(msgbox
->lpszIcon
))
434 RtlCreateUnicodeStringFromAsciiz(&iconW
, msgbox
->lpszIcon
);
436 iconW
.Buffer
= (LPWSTR
)msgbox
->lpszIcon
;
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
);
460 /**************************************************************************
461 * MessageBoxIndirectW (USER32.@)
463 INT WINAPI
MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox
)
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
)))
475 if (!(tmplate
= (LPVOID
)LoadResource(user32_module
, hRes
)))
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
);