dwrite: Partially implement GetGlyphImageFormats().
[wine.git] / dlls / user32 / msgbox.c
blob0839f97dcb990aa50fc532a822923bea570eab53
1 /*
2 * Message boxes
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
22 #include <stdarg.h>
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winternl.h"
29 #include "dlgs.h"
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 stc1
37 #define MSGBOX_IDTEXT 100
38 #define IDS_ERROR 2
40 struct ThreadWindows
42 UINT numHandles;
43 UINT numAllocs;
44 HWND *handles;
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;
61 return TRUE;
64 static void MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
66 HFONT hPrevFont;
67 RECT rect;
68 HWND hItem;
69 HDC hdc;
70 int i, buttons;
71 int bspace, bw, bh, theight, tleft, wwidth, wheight, wleft, wtop, bpos;
72 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
73 NONCLIENTMETRICSW nclm;
74 HMONITOR monitor;
75 MONITORINFO mon_info;
76 LPCWSTR lpszText;
77 WCHAR *buffer = NULL;
78 const WCHAR *ptr;
80 /* Index the order the buttons need to appear to an ID* constant */
81 static const int buttonOrder[10] = { IDYES, IDNO, IDOK, IDABORT, IDRETRY,
82 IDCANCEL, IDIGNORE, IDTRYAGAIN,
83 IDCONTINUE, IDHELP };
85 nclm.cbSize = sizeof(nclm);
86 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
88 if (!IS_INTRESOURCE(lpmb->lpszCaption)) {
89 SetWindowTextW(hwnd, lpmb->lpszCaption);
90 } else {
91 UINT len = LoadStringW( lpmb->hInstance, LOWORD(lpmb->lpszCaption), (LPWSTR)&ptr, 0 );
92 if (!len) len = LoadStringW( user32_module, IDS_ERROR, (LPWSTR)&ptr, 0 );
93 buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
94 if (buffer)
96 memcpy( buffer, ptr, len * sizeof(WCHAR) );
97 buffer[len] = 0;
98 SetWindowTextW( hwnd, buffer );
99 HeapFree( GetProcessHeap(), 0, buffer );
100 buffer = NULL;
103 if (IS_INTRESOURCE(lpmb->lpszText)) {
104 UINT len = LoadStringW( lpmb->hInstance, LOWORD(lpmb->lpszText), (LPWSTR)&ptr, 0 );
105 lpszText = buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
106 if (buffer)
108 memcpy( buffer, ptr, len * sizeof(WCHAR) );
109 buffer[len] = 0;
111 } else {
112 lpszText = lpmb->lpszText;
115 TRACE_(msgbox)("%s\n", debugstr_w(lpszText));
116 SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
118 /* Remove not selected buttons and assign the WS_GROUP style to the first button */
119 hItem = 0;
120 switch(lpmb->dwStyle & MB_TYPEMASK) {
121 case MB_OK:
122 DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
123 /* fall through */
124 case MB_OKCANCEL:
125 hItem = GetDlgItem(hwnd, IDOK);
126 DestroyWindow(GetDlgItem(hwnd, IDABORT));
127 DestroyWindow(GetDlgItem(hwnd, IDRETRY));
128 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
129 DestroyWindow(GetDlgItem(hwnd, IDYES));
130 DestroyWindow(GetDlgItem(hwnd, IDNO));
131 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
132 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
133 break;
134 case MB_ABORTRETRYIGNORE:
135 hItem = GetDlgItem(hwnd, IDABORT);
136 DestroyWindow(GetDlgItem(hwnd, IDOK));
137 DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
138 DestroyWindow(GetDlgItem(hwnd, IDYES));
139 DestroyWindow(GetDlgItem(hwnd, IDNO));
140 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
141 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
142 break;
143 case MB_YESNO:
144 DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
145 /* fall through */
146 case MB_YESNOCANCEL:
147 hItem = GetDlgItem(hwnd, IDYES);
148 DestroyWindow(GetDlgItem(hwnd, IDOK));
149 DestroyWindow(GetDlgItem(hwnd, IDABORT));
150 DestroyWindow(GetDlgItem(hwnd, IDRETRY));
151 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
152 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
153 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
154 break;
155 case MB_RETRYCANCEL:
156 hItem = GetDlgItem(hwnd, IDRETRY);
157 DestroyWindow(GetDlgItem(hwnd, IDOK));
158 DestroyWindow(GetDlgItem(hwnd, IDABORT));
159 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
160 DestroyWindow(GetDlgItem(hwnd, IDYES));
161 DestroyWindow(GetDlgItem(hwnd, IDNO));
162 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
163 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
164 break;
165 case MB_CANCELTRYCONTINUE:
166 hItem = GetDlgItem(hwnd, IDCANCEL);
167 DestroyWindow(GetDlgItem(hwnd, IDOK));
168 DestroyWindow(GetDlgItem(hwnd, IDABORT));
169 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
170 DestroyWindow(GetDlgItem(hwnd, IDYES));
171 DestroyWindow(GetDlgItem(hwnd, IDNO));
172 DestroyWindow(GetDlgItem(hwnd, IDRETRY));
175 if (hItem) SetWindowLongW(hItem, GWL_STYLE, GetWindowLongW(hItem, GWL_STYLE) | WS_GROUP);
177 /* Set the icon */
178 switch(lpmb->dwStyle & MB_ICONMASK) {
179 case MB_ICONEXCLAMATION:
180 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
181 (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
182 break;
183 case MB_ICONQUESTION:
184 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
185 (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
186 break;
187 case MB_ICONASTERISK:
188 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
189 (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
190 break;
191 case MB_ICONHAND:
192 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
193 (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
194 break;
195 case MB_USERICON:
196 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
197 (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
198 break;
199 default:
200 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
201 * So wine should do the same.
203 break;
206 /* Remove Help button unless MB_HELP supplied */
207 if (!(lpmb->dwStyle & MB_HELP)) {
208 DestroyWindow(GetDlgItem(hwnd, IDHELP));
211 /* Position everything */
212 GetWindowRect(hwnd, &rect);
213 borheight = rect.bottom - rect.top;
214 borwidth = rect.right - rect.left;
215 GetClientRect(hwnd, &rect);
216 borheight -= rect.bottom - rect.top;
217 borwidth -= rect.right - rect.left;
219 /* Get the icon height */
220 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
221 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
222 if (!(lpmb->dwStyle & MB_ICONMASK))
224 rect.bottom = rect.top;
225 rect.right = rect.left;
227 iheight = rect.bottom - rect.top;
228 ileft = rect.left;
229 iwidth = rect.right - ileft;
231 hdc = GetDC(hwnd);
232 hPrevFont = SelectObject( hdc, (HFONT)SendMessageW( hwnd, WM_GETFONT, 0, 0 ));
234 /* Get the number of visible buttons and their size */
235 bh = bw = 1; /* Minimum button sizes */
236 for (buttons = 0, i = IDOK; i <= IDCONTINUE; i++)
238 if (i == IDCLOSE) continue; /* No CLOSE button */
239 hItem = GetDlgItem(hwnd, i);
240 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
242 WCHAR buttonText[1024];
243 int w, h;
244 buttons++;
245 if (GetWindowTextW(hItem, buttonText, 1024))
247 DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
248 h = rect.bottom - rect.top;
249 w = rect.right - rect.left;
250 if (h > bh) bh = h;
251 if (w > bw) bw = w ;
255 bw = max(bw, bh * 2);
256 /* Button white space */
257 bh = bh * 2;
258 bw = bw * 2;
259 bspace = bw/3; /* Space between buttons */
261 /* Get the text size */
262 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
263 rect.top = rect.left = rect.bottom = 0;
264 DrawTextW(hdc, lpszText, -1, &rect,
265 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT | DT_NOPREFIX);
266 /* Min text width corresponds to space for the buttons */
267 tleft = ileft;
268 if (iwidth) tleft += ileft + iwidth;
269 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
270 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)) {
306 SetFocus(hItem);
307 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
309 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
310 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
311 bpos += bw + bspace;
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 );
323 /**************************************************************************
324 * MSGBOX_DlgProc
326 * Dialog procedure for message boxes.
328 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
329 WPARAM wParam, LPARAM lParam )
331 switch(message) {
332 case WM_INITDIALOG:
334 LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
335 SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
336 MSGBOX_OnInit(hwnd, mbp);
337 SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", mbp->lpfnMsgBoxCallback);
338 break;
341 case WM_COMMAND:
342 switch (LOWORD(wParam))
344 case IDOK:
345 case IDCANCEL:
346 case IDABORT:
347 case IDRETRY:
348 case IDIGNORE:
349 case IDYES:
350 case IDNO:
351 case IDTRYAGAIN:
352 case IDCONTINUE:
353 EndDialog(hwnd, wParam);
354 break;
355 case IDHELP:
356 FIXME("Help button not supported yet\n");
357 break;
359 break;
361 case WM_HELP:
363 MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
364 HELPINFO hi;
366 memcpy(&hi, (void *)lParam, sizeof(hi));
367 hi.dwContextId = GetWindowContextHelpId(hwnd);
369 if (callback)
370 callback(&hi);
371 else
372 SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
373 break;
376 default:
377 /* Ok. Ignore all the other messages */
378 TRACE("Message number 0x%04x is being ignored.\n", message);
379 break;
381 return 0;
385 /**************************************************************************
386 * MessageBoxA (USER32.@)
388 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
390 return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
394 /**************************************************************************
395 * MessageBoxW (USER32.@)
397 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
399 return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
403 /**************************************************************************
404 * MessageBoxExA (USER32.@)
406 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
407 UINT type, WORD langid )
409 MSGBOXPARAMSA msgbox;
411 msgbox.cbSize = sizeof(msgbox);
412 msgbox.hwndOwner = hWnd;
413 msgbox.hInstance = 0;
414 msgbox.lpszText = text;
415 msgbox.lpszCaption = title;
416 msgbox.dwStyle = type;
417 msgbox.lpszIcon = NULL;
418 msgbox.dwContextHelpId = 0;
419 msgbox.lpfnMsgBoxCallback = NULL;
420 msgbox.dwLanguageId = langid;
422 return MessageBoxIndirectA(&msgbox);
425 /**************************************************************************
426 * MessageBoxExW (USER32.@)
428 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
429 UINT type, WORD langid )
431 MSGBOXPARAMSW msgbox;
433 msgbox.cbSize = sizeof(msgbox);
434 msgbox.hwndOwner = hWnd;
435 msgbox.hInstance = 0;
436 msgbox.lpszText = text;
437 msgbox.lpszCaption = title;
438 msgbox.dwStyle = type;
439 msgbox.lpszIcon = NULL;
440 msgbox.dwContextHelpId = 0;
441 msgbox.lpfnMsgBoxCallback = NULL;
442 msgbox.dwLanguageId = langid;
444 return MessageBoxIndirectW(&msgbox);
447 /**************************************************************************
448 * MessageBoxTimeoutA (USER32.@)
450 INT WINAPI MessageBoxTimeoutA( HWND hWnd, LPCSTR text, LPCSTR title,
451 UINT type, WORD langid, DWORD timeout )
453 FIXME("timeout not supported (%u)\n", timeout);
454 return MessageBoxExA( hWnd, text, title, type, langid );
457 /**************************************************************************
458 * MessageBoxTimeoutW (USER32.@)
460 INT WINAPI MessageBoxTimeoutW( HWND hWnd, LPCWSTR text, LPCWSTR title,
461 UINT type, WORD langid, DWORD timeout )
463 FIXME("timeout not supported (%u)\n", timeout);
464 return MessageBoxExW( hWnd, text, title, type, langid );
467 /**************************************************************************
468 * MessageBoxIndirectA (USER32.@)
470 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
472 MSGBOXPARAMSW msgboxW;
473 UNICODE_STRING textW, captionW, iconW;
474 int ret;
476 if (IS_INTRESOURCE(msgbox->lpszText))
477 textW.Buffer = (LPWSTR)msgbox->lpszText;
478 else
479 RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
480 if (IS_INTRESOURCE(msgbox->lpszCaption))
481 captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
482 else
483 RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
485 if (msgbox->dwStyle & MB_USERICON)
487 if (IS_INTRESOURCE(msgbox->lpszIcon))
488 iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
489 else
490 RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
492 else
493 iconW.Buffer = NULL;
495 msgboxW.cbSize = sizeof(msgboxW);
496 msgboxW.hwndOwner = msgbox->hwndOwner;
497 msgboxW.hInstance = msgbox->hInstance;
498 msgboxW.lpszText = textW.Buffer;
499 msgboxW.lpszCaption = captionW.Buffer;
500 msgboxW.dwStyle = msgbox->dwStyle;
501 msgboxW.lpszIcon = iconW.Buffer;
502 msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
503 msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
504 msgboxW.dwLanguageId = msgbox->dwLanguageId;
506 ret = MessageBoxIndirectW(&msgboxW);
508 if (!IS_INTRESOURCE(textW.Buffer)) RtlFreeUnicodeString(&textW);
509 if (!IS_INTRESOURCE(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
510 if (!IS_INTRESOURCE(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
511 return ret;
514 /**************************************************************************
515 * MessageBoxIndirectW (USER32.@)
517 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
519 LPVOID tmplate;
520 HRSRC hRes;
521 int ret;
522 UINT i;
523 struct ThreadWindows threadWindows;
524 static const WCHAR msg_box_res_nameW[] = { 'M','S','G','B','O','X',0 };
526 if (!(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG,
527 msg_box_res_nameW, msgbox->dwLanguageId)))
529 if (!msgbox->dwLanguageId ||
530 !(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG, msg_box_res_nameW, LANG_NEUTRAL)))
531 return 0;
533 if (!(tmplate = LoadResource(user32_module, hRes)))
534 return 0;
536 if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
538 threadWindows.numHandles = 0;
539 threadWindows.numAllocs = 10;
540 threadWindows.handles = HeapAlloc(GetProcessHeap(), 0, 10*sizeof(HWND));
541 EnumThreadWindows(GetCurrentThreadId(), MSGBOX_EnumProc, (LPARAM)&threadWindows);
544 ret=DialogBoxIndirectParamW(msgbox->hInstance, tmplate,
545 msgbox->hwndOwner, MSGBOX_DlgProc, (LPARAM)msgbox);
547 if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
549 for (i = 0; i < threadWindows.numHandles; i++)
550 EnableWindow(threadWindows.handles[i], TRUE);
551 HeapFree(GetProcessHeap(), 0, threadWindows.handles);
553 return ret;