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 static 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(hwnd
, 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.@)
350 INT WINAPI
MessageBoxA(HWND hWnd
, LPCSTR text
, LPCSTR title
, UINT type
)
352 return MessageBoxExA(hWnd
, text
, title
, type
, LANG_NEUTRAL
);
356 /**************************************************************************
357 * MessageBoxW (USER32.@)
359 INT WINAPI
MessageBoxW( HWND hwnd
, LPCWSTR text
, LPCWSTR title
, UINT type
)
361 return MessageBoxExW(hwnd
, text
, title
, type
, LANG_NEUTRAL
);
365 /**************************************************************************
366 * MessageBoxExA (USER32.@)
368 INT WINAPI
MessageBoxExA( HWND hWnd
, LPCSTR text
, LPCSTR title
,
369 UINT type
, WORD langid
)
371 MSGBOXPARAMSA msgbox
;
373 msgbox
.cbSize
= sizeof(msgbox
);
374 msgbox
.hwndOwner
= hWnd
;
375 msgbox
.hInstance
= 0;
376 msgbox
.lpszText
= text
;
377 msgbox
.lpszCaption
= title
;
378 msgbox
.dwStyle
= type
;
379 msgbox
.lpszIcon
= NULL
;
380 msgbox
.dwContextHelpId
= 0;
381 msgbox
.lpfnMsgBoxCallback
= NULL
;
382 msgbox
.dwLanguageId
= langid
;
384 return MessageBoxIndirectA(&msgbox
);
387 /**************************************************************************
388 * MessageBoxExW (USER32.@)
390 INT WINAPI
MessageBoxExW( HWND hWnd
, LPCWSTR text
, LPCWSTR title
,
391 UINT type
, WORD langid
)
393 MSGBOXPARAMSW msgbox
;
395 msgbox
.cbSize
= sizeof(msgbox
);
396 msgbox
.hwndOwner
= hWnd
;
397 msgbox
.hInstance
= 0;
398 msgbox
.lpszText
= text
;
399 msgbox
.lpszCaption
= title
;
400 msgbox
.dwStyle
= type
;
401 msgbox
.lpszIcon
= NULL
;
402 msgbox
.dwContextHelpId
= 0;
403 msgbox
.lpfnMsgBoxCallback
= NULL
;
404 msgbox
.dwLanguageId
= langid
;
406 return MessageBoxIndirectW(&msgbox
);
409 /**************************************************************************
410 * MessageBoxIndirectA (USER32.@)
412 INT WINAPI
MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox
)
414 MSGBOXPARAMSW msgboxW
;
415 UNICODE_STRING textW
, captionW
, iconW
;
418 if (HIWORD(msgbox
->lpszText
))
419 RtlCreateUnicodeStringFromAsciiz(&textW
, msgbox
->lpszText
);
421 textW
.Buffer
= (LPWSTR
)msgbox
->lpszText
;
422 if (HIWORD(msgbox
->lpszCaption
))
423 RtlCreateUnicodeStringFromAsciiz(&captionW
, msgbox
->lpszCaption
);
425 captionW
.Buffer
= (LPWSTR
)msgbox
->lpszCaption
;
427 if (msgbox
->dwStyle
& MB_USERICON
)
429 if (HIWORD(msgbox
->lpszIcon
))
430 RtlCreateUnicodeStringFromAsciiz(&iconW
, msgbox
->lpszIcon
);
432 iconW
.Buffer
= (LPWSTR
)msgbox
->lpszIcon
;
437 msgboxW
.cbSize
= sizeof(msgboxW
);
438 msgboxW
.hwndOwner
= msgbox
->hwndOwner
;
439 msgboxW
.hInstance
= msgbox
->hInstance
;
440 msgboxW
.lpszText
= textW
.Buffer
;
441 msgboxW
.lpszCaption
= captionW
.Buffer
;
442 msgboxW
.dwStyle
= msgbox
->dwStyle
;
443 msgboxW
.lpszIcon
= iconW
.Buffer
;
444 msgboxW
.dwContextHelpId
= msgbox
->dwContextHelpId
;
445 msgboxW
.lpfnMsgBoxCallback
= msgbox
->lpfnMsgBoxCallback
;
446 msgboxW
.dwLanguageId
= msgbox
->dwLanguageId
;
448 ret
= MessageBoxIndirectW(&msgboxW
);
450 if (HIWORD(textW
.Buffer
)) RtlFreeUnicodeString(&textW
);
451 if (HIWORD(captionW
.Buffer
)) RtlFreeUnicodeString(&captionW
);
452 if (HIWORD(iconW
.Buffer
)) RtlFreeUnicodeString(&iconW
);
456 /**************************************************************************
457 * MessageBoxIndirectW (USER32.@)
459 INT WINAPI
MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox
)
465 struct ThreadWindows threadWindows
;
466 static const WCHAR msg_box_res_nameW
[] = { 'M','S','G','B','O','X',0 };
468 if (!(hRes
= FindResourceExW(user32_module
, (LPWSTR
)RT_DIALOG
,
469 msg_box_res_nameW
, msgbox
->dwLanguageId
)))
471 if (!(tmplate
= (LPVOID
)LoadResource(user32_module
, hRes
)))
474 if ((msgbox
->dwStyle
& MB_TASKMODAL
) && (msgbox
->hwndOwner
==NULL
))
476 threadWindows
.numHandles
= 0;
477 threadWindows
.numAllocs
= 10;
478 threadWindows
.handles
= HeapAlloc(GetProcessHeap(), 0, 10*sizeof(HWND
));
479 EnumThreadWindows(GetCurrentThreadId(), MSGBOX_EnumProc
, (LPARAM
)&threadWindows
);
482 ret
=DialogBoxIndirectParamW(msgbox
->hInstance
, tmplate
,
483 msgbox
->hwndOwner
, MSGBOX_DlgProc
, (LPARAM
)msgbox
);
485 if ((msgbox
->dwStyle
& MB_TASKMODAL
) && (msgbox
->hwndOwner
==NULL
))
487 for (i
= 0; i
< threadWindows
.numHandles
; i
++)
488 EnableWindow(threadWindows
.handles
[i
], TRUE
);
489 HeapFree(GetProcessHeap(), 0, threadWindows
.handles
);