Fix spec for InitiateSystemShutdownExA, as pointed out by Stefan
[wine/multimedia.git] / windows / msgbox.c
blob3fb5922ce6c52aee6bace3b11a27bca6b432ffca
1 /*
2 * Message boxes
4 * Copyright 1995 Bernd Schmidt
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <string.h>
23 #include "windef.h"
24 #include "wingdi.h"
25 #include "wine/winbase16.h"
26 #include "wine/winuser16.h"
27 #include "winternl.h"
28 #include "dlgs.h"
29 #include "user.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
34 #define MSGBOX_IDICON 1088
35 #define MSGBOX_IDTEXT 100
36 #define IDS_ERROR 2
38 static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
40 HFONT hFont = 0, hPrevFont = 0;
41 RECT rect;
42 HWND hItem;
43 HDC hdc;
44 int i, buttons;
45 int bspace, bw, bh, theight, tleft, wwidth, wheight, bpos;
46 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
47 LPCWSTR lpszText;
48 WCHAR buf[256];
50 if (TWEAK_WineLook >= WIN95_LOOK) {
51 NONCLIENTMETRICSW nclm;
52 nclm.cbSize = sizeof(nclm);
53 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
54 hFont = CreateFontIndirectW (&nclm.lfMessageFont);
55 /* set button font */
56 for (i=1; i < 8; i++)
57 SendDlgItemMessageW (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
58 /* set text font */
59 SendDlgItemMessageW (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
61 if (HIWORD(lpmb->lpszCaption)) {
62 SetWindowTextW(hwnd, lpmb->lpszCaption);
63 } else {
64 UINT res_id = LOWORD(lpmb->lpszCaption);
65 if (res_id)
67 if (LoadStringW(lpmb->hInstance, res_id, buf, 256))
68 SetWindowTextW(hwnd, buf);
70 else
72 if (LoadStringW(GetModuleHandleA("user32.dll"), IDS_ERROR, buf, 256))
73 SetWindowTextW(hwnd, buf);
76 if (HIWORD(lpmb->lpszText)) {
77 lpszText = lpmb->lpszText;
78 } else {
79 lpszText = buf;
80 if (!LoadStringW(lpmb->hInstance, LOWORD(lpmb->lpszText), buf, 256))
81 *buf = 0; /* FIXME ?? */
83 SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
85 /* Hide not selected buttons */
86 switch(lpmb->dwStyle & MB_TYPEMASK) {
87 case MB_OK:
88 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
89 /* fall through */
90 case MB_OKCANCEL:
91 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
92 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
93 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
94 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
95 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
96 break;
97 case MB_ABORTRETRYIGNORE:
98 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
99 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
100 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
101 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
102 break;
103 case MB_YESNO:
104 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
105 /* fall through */
106 case MB_YESNOCANCEL:
107 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
108 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
109 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
110 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
111 break;
112 case MB_RETRYCANCEL:
113 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
114 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
115 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
116 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
117 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
118 break;
120 /* Set the icon */
121 switch(lpmb->dwStyle & MB_ICONMASK) {
122 case MB_ICONEXCLAMATION:
123 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
124 (WPARAM)LoadIconW(0, IDI_EXCLAMATIONW), 0);
125 break;
126 case MB_ICONQUESTION:
127 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
128 (WPARAM)LoadIconW(0, IDI_QUESTIONW), 0);
129 break;
130 case MB_ICONASTERISK:
131 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
132 (WPARAM)LoadIconW(0, IDI_ASTERISKW), 0);
133 break;
134 case MB_ICONHAND:
135 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
136 (WPARAM)LoadIconW(0, IDI_HANDW), 0);
137 break;
138 case MB_USERICON:
139 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
140 (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
141 break;
142 default:
143 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
144 * So wine should do the same.
146 break;
149 /* Position everything */
150 GetWindowRect(hwnd, &rect);
151 borheight = rect.bottom - rect.top;
152 borwidth = rect.right - rect.left;
153 GetClientRect(hwnd, &rect);
154 borheight -= rect.bottom - rect.top;
155 borwidth -= rect.right - rect.left;
157 /* Get the icon height */
158 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
159 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
160 if (!(lpmb->dwStyle & MB_ICONMASK))
162 rect.bottom = rect.top;
163 rect.right = rect.left;
165 iheight = rect.bottom - rect.top;
166 ileft = rect.left;
167 iwidth = rect.right - ileft;
169 hdc = GetDC(hwnd);
170 if (hFont)
171 hPrevFont = SelectObject(hdc, hFont);
173 /* Get the number of visible buttons and their size */
174 bh = bw = 1; /* Minimum button sizes */
175 for (buttons = 0, i = 1; i < 8; i++)
177 hItem = GetDlgItem(hwnd, i);
178 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
180 WCHAR buttonText[1024];
181 int w, h;
182 buttons++;
183 if (GetWindowTextW(hItem, buttonText, 1024))
185 DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
186 h = rect.bottom - rect.top;
187 w = rect.right - rect.left;
188 if (h > bh) bh = h;
189 if (w > bw) bw = w ;
193 bw = max(bw, bh * 2);
194 /* Button white space */
195 bh = bh * 2;
196 bw = bw * 2;
197 bspace = bw/3; /* Space between buttons */
199 /* Get the text size */
200 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
201 rect.top = rect.left = rect.bottom = 0;
202 DrawTextW( hdc, lpszText, -1, &rect,
203 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
204 /* Min text width corresponds to space for the buttons */
205 tleft = ileft;
206 if (iwidth) tleft += ileft + iwidth;
207 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
208 theight = rect.bottom;
210 if (hFont)
211 SelectObject(hdc, hPrevFont);
212 ReleaseDC(hItem, hdc);
214 tiheight = 16 + max(iheight, theight);
215 wwidth = tleft + twidth + ileft + borwidth;
216 wheight = 8 + tiheight + bh + borheight;
218 /* Resize the window */
219 SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
220 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
222 /* Position the icon */
223 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
224 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
226 /* Position the text */
227 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
228 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
230 /* Position the buttons */
231 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
232 for (buttons = i = 0; i < 7; i++) {
233 /* some arithmetic to get the right order for YesNoCancel windows */
234 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
235 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE) {
236 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
237 SetFocus(hItem);
238 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
240 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
241 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
242 bpos += bw + bspace;
246 /* handle modal MessageBoxes */
247 if (lpmb->dwStyle & (MB_TASKMODAL|MB_SYSTEMMODAL))
249 FIXME("%s modal msgbox ! Not modal yet.\n",
250 lpmb->dwStyle & MB_TASKMODAL ? "task" : "system");
251 /* Probably do EnumTaskWindows etc. here for TASKMODAL
252 * and work your way up to the top - I'm lazy (HWND_TOP) */
253 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
254 SWP_NOSIZE | SWP_NOMOVE);
255 if (lpmb->dwStyle & MB_TASKMODAL)
256 /* at least MB_TASKMODAL seems to imply a ShowWindow */
257 ShowWindow(hwnd, SW_SHOW);
259 if (lpmb->dwStyle & MB_APPLMODAL)
260 FIXME("app modal msgbox ! Not modal yet.\n");
262 return hFont;
266 /**************************************************************************
267 * MSGBOX_DlgProc
269 * Dialog procedure for message boxes.
271 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
272 WPARAM wParam, LPARAM lParam )
274 HFONT hFont;
276 switch(message) {
277 case WM_INITDIALOG:
279 LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
280 SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
281 hFont = MSGBOX_OnInit(hwnd, mbp);
282 SetPropA(hwnd, "WINE_MSGBOX_HFONT", (HANDLE)hFont);
283 SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", (HANDLE)mbp->lpfnMsgBoxCallback);
284 break;
287 case WM_COMMAND:
288 switch (LOWORD(wParam))
290 case IDOK:
291 case IDCANCEL:
292 case IDABORT:
293 case IDRETRY:
294 case IDIGNORE:
295 case IDYES:
296 case IDNO:
297 hFont = GetPropA(hwnd, "WINE_MSGBOX_HFONT");
298 EndDialog(hwnd, wParam);
299 if (hFont)
300 DeleteObject(hFont);
301 break;
303 break;
305 case WM_HELP:
307 MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
308 HELPINFO hi;
310 memcpy(&hi, (void *)lParam, sizeof(hi));
311 hi.dwContextId = GetWindowContextHelpId(hwnd);
313 if (callback)
314 callback(&hi);
315 else
316 SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
317 break;
320 default:
321 /* Ok. Ignore all the other messages */
322 TRACE("Message number 0x%04x is being ignored.\n", message);
323 break;
325 return 0;
329 /**************************************************************************
330 * MessageBoxA (USER32.@)
332 * NOTES
333 * The WARN is here to help debug erroneous MessageBoxes
334 * Use: -debugmsg warn+dialog,+relay
336 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
338 return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
342 /**************************************************************************
343 * MessageBoxW (USER32.@)
345 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
347 return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
351 /**************************************************************************
352 * MessageBoxExA (USER32.@)
354 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
355 UINT type, WORD langid )
357 MSGBOXPARAMSA msgbox;
359 msgbox.cbSize = sizeof(msgbox);
360 msgbox.hwndOwner = hWnd;
361 msgbox.hInstance = 0;
362 msgbox.lpszText = text;
363 msgbox.lpszCaption = title;
364 msgbox.dwStyle = type;
365 msgbox.lpszIcon = NULL;
366 msgbox.dwContextHelpId = 0;
367 msgbox.lpfnMsgBoxCallback = NULL;
368 msgbox.dwLanguageId = langid;
370 return MessageBoxIndirectA(&msgbox);
373 /**************************************************************************
374 * MessageBoxExW (USER32.@)
376 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
377 UINT type, WORD langid )
379 MSGBOXPARAMSW msgbox;
381 msgbox.cbSize = sizeof(msgbox);
382 msgbox.hwndOwner = hWnd;
383 msgbox.hInstance = 0;
384 msgbox.lpszText = text;
385 msgbox.lpszCaption = title;
386 msgbox.dwStyle = type;
387 msgbox.lpszIcon = NULL;
388 msgbox.dwContextHelpId = 0;
389 msgbox.lpfnMsgBoxCallback = NULL;
390 msgbox.dwLanguageId = langid;
392 return MessageBoxIndirectW(&msgbox);
395 /**************************************************************************
396 * MessageBoxIndirectA (USER32.@)
398 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
400 MSGBOXPARAMSW msgboxW;
401 UNICODE_STRING textW, captionW, iconW;
402 int ret;
404 if (HIWORD(msgbox->lpszText))
405 RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
406 else
407 textW.Buffer = (LPWSTR)msgbox->lpszText;
408 if (HIWORD(msgbox->lpszCaption))
409 RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
410 else
411 captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
412 if (HIWORD(msgbox->lpszIcon))
413 RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
414 else
415 iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
417 msgboxW.cbSize = sizeof(msgboxW);
418 msgboxW.hwndOwner = msgbox->hwndOwner;
419 msgboxW.hInstance = msgbox->hInstance;
420 msgboxW.lpszText = textW.Buffer;
421 msgboxW.lpszCaption = captionW.Buffer;
422 msgboxW.dwStyle = msgbox->dwStyle;
423 msgboxW.lpszIcon = iconW.Buffer;
424 msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
425 msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
426 msgboxW.dwLanguageId = msgbox->dwLanguageId;
428 ret = MessageBoxIndirectW(&msgboxW);
430 if (HIWORD(textW.Buffer)) RtlFreeUnicodeString(&textW);
431 if (HIWORD(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
432 if (HIWORD(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
433 return ret;
436 /**************************************************************************
437 * MessageBoxIndirectW (USER32.@)
439 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
441 LPVOID tmplate;
442 HRSRC hRes;
443 HMODULE hUser32;
444 static const WCHAR user32_res_nameW[] = { 'u','s','e','r','3','2','.','d','l','l',0 };
445 static const WCHAR msg_box_res_nameW[] = { 'M','S','G','B','O','X',0 };
447 hUser32 = GetModuleHandleW(user32_res_nameW);
448 if (!(hRes = FindResourceExW(hUser32, RT_DIALOGW, msg_box_res_nameW, msgbox->dwLanguageId)))
449 return 0;
450 if (!(tmplate = (LPVOID)LoadResource(hUser32, hRes)))
451 return 0;
453 return DialogBoxIndirectParamW(msgbox->hInstance, tmplate, msgbox->hwndOwner,
454 MSGBOX_DlgProc, (LPARAM)msgbox);