offsets array is the size of the wine data format so there is no need
[wine.git] / windows / msgbox.c
blob6d235c657f9a58a621da6949f3156e3eabe39207
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 <stdarg.h>
22 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "wine/winbase16.h"
28 #include "wine/winuser16.h"
29 #include "winreg.h"
30 #include "winternl.h"
31 #include "dlgs.h"
32 #include "user.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
36 WINE_DECLARE_DEBUG_CHANNEL(msgbox);
38 #define MSGBOX_IDICON 1088
39 #define MSGBOX_IDTEXT 100
40 #define IDS_ERROR 2
42 static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
44 HFONT hFont = 0, hPrevFont = 0;
45 RECT rect;
46 HWND hItem;
47 HDC hdc;
48 int i, buttons;
49 int bspace, bw, bh, theight, tleft, wwidth, wheight, bpos;
50 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
51 NONCLIENTMETRICSW nclm;
52 LPCWSTR lpszText;
53 WCHAR buf[256];
55 nclm.cbSize = sizeof(nclm);
56 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
57 hFont = CreateFontIndirectW (&nclm.lfMessageFont);
58 /* set button font */
59 for (i=1; i < 8; i++)
60 SendDlgItemMessageW (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
61 /* set text font */
62 SendDlgItemMessageW (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
64 if (HIWORD(lpmb->lpszCaption)) {
65 SetWindowTextW(hwnd, lpmb->lpszCaption);
66 } else {
67 UINT res_id = LOWORD(lpmb->lpszCaption);
68 if (res_id)
70 if (LoadStringW(lpmb->hInstance, res_id, buf, 256))
71 SetWindowTextW(hwnd, buf);
73 else
75 if (LoadStringW(user32_module, IDS_ERROR, buf, 256))
76 SetWindowTextW(hwnd, buf);
79 if (HIWORD(lpmb->lpszText)) {
80 lpszText = lpmb->lpszText;
81 } else {
82 lpszText = buf;
83 if (!LoadStringW(lpmb->hInstance, LOWORD(lpmb->lpszText), buf, 256))
84 *buf = 0; /* FIXME ?? */
87 TRACE_(msgbox)("%s\n", debugstr_w(lpszText));
88 SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
90 /* Hide not selected buttons */
91 switch(lpmb->dwStyle & MB_TYPEMASK) {
92 case MB_OK:
93 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
94 /* fall through */
95 case MB_OKCANCEL:
96 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
97 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
98 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
99 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
100 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
101 break;
102 case MB_ABORTRETRYIGNORE:
103 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
104 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
105 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
106 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
107 break;
108 case MB_YESNO:
109 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
110 /* fall through */
111 case MB_YESNOCANCEL:
112 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
113 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
114 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
115 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
116 break;
117 case MB_RETRYCANCEL:
118 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
119 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
120 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
121 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
122 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
123 break;
125 /* Set the icon */
126 switch(lpmb->dwStyle & MB_ICONMASK) {
127 case MB_ICONEXCLAMATION:
128 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
129 (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
130 break;
131 case MB_ICONQUESTION:
132 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
133 (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
134 break;
135 case MB_ICONASTERISK:
136 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
137 (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
138 break;
139 case MB_ICONHAND:
140 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
141 (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
142 break;
143 case MB_USERICON:
144 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
145 (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
146 break;
147 default:
148 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
149 * So wine should do the same.
151 break;
154 /* Position everything */
155 GetWindowRect(hwnd, &rect);
156 borheight = rect.bottom - rect.top;
157 borwidth = rect.right - rect.left;
158 GetClientRect(hwnd, &rect);
159 borheight -= rect.bottom - rect.top;
160 borwidth -= rect.right - rect.left;
162 /* Get the icon height */
163 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
164 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
165 if (!(lpmb->dwStyle & MB_ICONMASK))
167 rect.bottom = rect.top;
168 rect.right = rect.left;
170 iheight = rect.bottom - rect.top;
171 ileft = rect.left;
172 iwidth = rect.right - ileft;
174 hdc = GetDC(hwnd);
175 if (hFont)
176 hPrevFont = SelectObject(hdc, hFont);
178 /* Get the number of visible buttons and their size */
179 bh = bw = 1; /* Minimum button sizes */
180 for (buttons = 0, i = 1; i < 8; i++)
182 hItem = GetDlgItem(hwnd, i);
183 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
185 WCHAR buttonText[1024];
186 int w, h;
187 buttons++;
188 if (GetWindowTextW(hItem, buttonText, 1024))
190 DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
191 h = rect.bottom - rect.top;
192 w = rect.right - rect.left;
193 if (h > bh) bh = h;
194 if (w > bw) bw = w ;
198 bw = max(bw, bh * 2);
199 /* Button white space */
200 bh = bh * 2;
201 bw = bw * 2;
202 bspace = bw/3; /* Space between buttons */
204 /* Get the text size */
205 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
206 rect.top = rect.left = rect.bottom = 0;
207 DrawTextW( hdc, lpszText, -1, &rect,
208 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
209 /* Min text width corresponds to space for the buttons */
210 tleft = ileft;
211 if (iwidth) tleft += ileft + iwidth;
212 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
213 theight = rect.bottom;
215 if (hFont)
216 SelectObject(hdc, hPrevFont);
217 ReleaseDC(hItem, hdc);
219 tiheight = 16 + max(iheight, theight);
220 wwidth = tleft + twidth + ileft + borwidth;
221 wheight = 8 + tiheight + bh + borheight;
223 /* Resize the window */
224 SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
225 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
227 /* Position the icon */
228 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
229 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
231 /* Position the text */
232 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
233 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
235 /* Position the buttons */
236 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
237 for (buttons = i = 0; i < 7; i++) {
238 /* some arithmetic to get the right order for YesNoCancel windows */
239 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
240 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE) {
241 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
242 SetFocus(hItem);
243 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
245 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
246 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
247 bpos += bw + bspace;
251 /* handle modal MessageBoxes */
252 if (lpmb->dwStyle & (MB_TASKMODAL|MB_SYSTEMMODAL))
254 FIXME("%s modal msgbox ! Not modal yet.\n",
255 lpmb->dwStyle & MB_TASKMODAL ? "task" : "system");
256 /* Probably do EnumTaskWindows etc. here for TASKMODAL
257 * and work your way up to the top - I'm lazy (HWND_TOP) */
258 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
259 SWP_NOSIZE | SWP_NOMOVE);
260 if (lpmb->dwStyle & MB_TASKMODAL)
261 /* at least MB_TASKMODAL seems to imply a ShowWindow */
262 ShowWindow(hwnd, SW_SHOW);
264 if (lpmb->dwStyle & MB_APPLMODAL)
265 FIXME("app modal msgbox ! Not modal yet.\n");
267 return hFont;
271 /**************************************************************************
272 * MSGBOX_DlgProc
274 * Dialog procedure for message boxes.
276 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
277 WPARAM wParam, LPARAM lParam )
279 HFONT hFont;
281 switch(message) {
282 case WM_INITDIALOG:
284 LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
285 SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
286 hFont = MSGBOX_OnInit(hwnd, mbp);
287 SetPropA(hwnd, "WINE_MSGBOX_HFONT", (HANDLE)hFont);
288 SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", (HANDLE)mbp->lpfnMsgBoxCallback);
289 break;
292 case WM_COMMAND:
293 switch (LOWORD(wParam))
295 case IDOK:
296 case IDCANCEL:
297 case IDABORT:
298 case IDRETRY:
299 case IDIGNORE:
300 case IDYES:
301 case IDNO:
302 hFont = GetPropA(hwnd, "WINE_MSGBOX_HFONT");
303 EndDialog(hwnd, wParam);
304 if (hFont)
305 DeleteObject(hFont);
306 break;
308 break;
310 case WM_HELP:
312 MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
313 HELPINFO hi;
315 memcpy(&hi, (void *)lParam, sizeof(hi));
316 hi.dwContextId = GetWindowContextHelpId(hwnd);
318 if (callback)
319 callback(&hi);
320 else
321 SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
322 break;
325 default:
326 /* Ok. Ignore all the other messages */
327 TRACE("Message number 0x%04x is being ignored.\n", message);
328 break;
330 return 0;
334 /**************************************************************************
335 * MessageBoxA (USER32.@)
337 * NOTES
338 * The WARN is here to help debug erroneous MessageBoxes
339 * Use: WINEDEBUG=warn+dialog,+relay
341 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
343 return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
347 /**************************************************************************
348 * MessageBoxW (USER32.@)
350 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
352 return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
356 /**************************************************************************
357 * MessageBoxExA (USER32.@)
359 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
360 UINT type, WORD langid )
362 MSGBOXPARAMSA msgbox;
364 msgbox.cbSize = sizeof(msgbox);
365 msgbox.hwndOwner = hWnd;
366 msgbox.hInstance = 0;
367 msgbox.lpszText = text;
368 msgbox.lpszCaption = title;
369 msgbox.dwStyle = type;
370 msgbox.lpszIcon = NULL;
371 msgbox.dwContextHelpId = 0;
372 msgbox.lpfnMsgBoxCallback = NULL;
373 msgbox.dwLanguageId = langid;
375 return MessageBoxIndirectA(&msgbox);
378 /**************************************************************************
379 * MessageBoxExW (USER32.@)
381 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
382 UINT type, WORD langid )
384 MSGBOXPARAMSW msgbox;
386 msgbox.cbSize = sizeof(msgbox);
387 msgbox.hwndOwner = hWnd;
388 msgbox.hInstance = 0;
389 msgbox.lpszText = text;
390 msgbox.lpszCaption = title;
391 msgbox.dwStyle = type;
392 msgbox.lpszIcon = NULL;
393 msgbox.dwContextHelpId = 0;
394 msgbox.lpfnMsgBoxCallback = NULL;
395 msgbox.dwLanguageId = langid;
397 return MessageBoxIndirectW(&msgbox);
400 /**************************************************************************
401 * MessageBoxIndirectA (USER32.@)
403 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
405 MSGBOXPARAMSW msgboxW;
406 UNICODE_STRING textW, captionW, iconW;
407 int ret;
409 if (HIWORD(msgbox->lpszText))
410 RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
411 else
412 textW.Buffer = (LPWSTR)msgbox->lpszText;
413 if (HIWORD(msgbox->lpszCaption))
414 RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
415 else
416 captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
417 if (HIWORD(msgbox->lpszIcon))
418 RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
419 else
420 iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
422 msgboxW.cbSize = sizeof(msgboxW);
423 msgboxW.hwndOwner = msgbox->hwndOwner;
424 msgboxW.hInstance = msgbox->hInstance;
425 msgboxW.lpszText = textW.Buffer;
426 msgboxW.lpszCaption = captionW.Buffer;
427 msgboxW.dwStyle = msgbox->dwStyle;
428 msgboxW.lpszIcon = iconW.Buffer;
429 msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
430 msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
431 msgboxW.dwLanguageId = msgbox->dwLanguageId;
433 ret = MessageBoxIndirectW(&msgboxW);
435 if (HIWORD(textW.Buffer)) RtlFreeUnicodeString(&textW);
436 if (HIWORD(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
437 if (HIWORD(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
438 return ret;
441 /**************************************************************************
442 * MessageBoxIndirectW (USER32.@)
444 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
446 LPVOID tmplate;
447 HRSRC hRes;
448 static const WCHAR msg_box_res_nameW[] = { 'M','S','G','B','O','X',0 };
450 if (!(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG,
451 msg_box_res_nameW, msgbox->dwLanguageId)))
452 return 0;
453 if (!(tmplate = (LPVOID)LoadResource(user32_module, hRes)))
454 return 0;
456 return DialogBoxIndirectParamW(msgbox->hInstance, tmplate, msgbox->hwndOwner,
457 MSGBOX_DlgProc, (LPARAM)msgbox);