commdlg: Let the color picker respond properly to keystrokes.
[wine.git] / dlls / user / msgbox.c
blobadd32112010ece2a5c77581d4ef0af25219f9c98
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <stdarg.h>
23 #include <string.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "wine/winbase16.h"
29 #include "wine/winuser16.h"
30 #include "winreg.h"
31 #include "winternl.h"
32 #include "dlgs.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
41 #define IDS_ERROR 2
43 struct ThreadWindows
45 UINT numHandles;
46 UINT numAllocs;
47 HWND *handles;
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;
64 return TRUE;
67 static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
69 HFONT hFont = 0, hPrevFont = 0;
70 RECT rect;
71 HWND hItem;
72 HDC hdc;
73 int i, buttons;
74 int bspace, bw, bh, theight, tleft, wwidth, wheight, bpos;
75 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
76 NONCLIENTMETRICSW nclm;
77 LPCWSTR lpszText;
78 WCHAR buf[256];
80 nclm.cbSize = sizeof(nclm);
81 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
82 hFont = CreateFontIndirectW (&nclm.lfMessageFont);
83 /* set button font */
84 for (i=1; i < 8; i++)
85 SendDlgItemMessageW (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
86 /* set text font */
87 SendDlgItemMessageW (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
89 if (HIWORD(lpmb->lpszCaption)) {
90 SetWindowTextW(hwnd, lpmb->lpszCaption);
91 } else {
92 UINT res_id = LOWORD(lpmb->lpszCaption);
93 if (res_id)
95 if (LoadStringW(lpmb->hInstance, res_id, buf, 256))
96 SetWindowTextW(hwnd, buf);
98 else
100 if (LoadStringW(user32_module, IDS_ERROR, buf, 256))
101 SetWindowTextW(hwnd, buf);
104 if (HIWORD(lpmb->lpszText)) {
105 lpszText = lpmb->lpszText;
106 } else {
107 lpszText = buf;
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) {
117 case MB_OK:
118 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
119 /* fall through */
120 case MB_OKCANCEL:
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);
126 break;
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);
132 break;
133 case MB_YESNO:
134 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
135 /* fall through */
136 case MB_YESNOCANCEL:
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);
141 break;
142 case MB_RETRYCANCEL:
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);
148 break;
150 /* Set the icon */
151 switch(lpmb->dwStyle & MB_ICONMASK) {
152 case MB_ICONEXCLAMATION:
153 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
154 (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
155 break;
156 case MB_ICONQUESTION:
157 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
158 (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
159 break;
160 case MB_ICONASTERISK:
161 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
162 (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
163 break;
164 case MB_ICONHAND:
165 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
166 (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
167 break;
168 case MB_USERICON:
169 SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
170 (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
171 break;
172 default:
173 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
174 * So wine should do the same.
176 break;
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;
196 ileft = rect.left;
197 iwidth = rect.right - ileft;
199 hdc = GetDC(hwnd);
200 if (hFont)
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];
211 int w, h;
212 buttons++;
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;
218 if (h > bh) bh = h;
219 if (w > bw) bw = w ;
223 bw = max(bw, bh * 2);
224 /* Button white space */
225 bh = bh * 2;
226 bw = bw * 2;
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 */
235 tleft = ileft;
236 if (iwidth) tleft += ileft + iwidth;
237 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
238 theight = rect.bottom;
240 if (hFont)
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)) {
267 SetFocus(hItem);
268 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
270 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
271 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
272 bpos += bw + bspace;
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);
280 return hFont;
284 /**************************************************************************
285 * MSGBOX_DlgProc
287 * Dialog procedure for message boxes.
289 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
290 WPARAM wParam, LPARAM lParam )
292 HFONT hFont;
294 switch(message) {
295 case WM_INITDIALOG:
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);
302 break;
305 case WM_COMMAND:
306 switch (LOWORD(wParam))
308 case IDOK:
309 case IDCANCEL:
310 case IDABORT:
311 case IDRETRY:
312 case IDIGNORE:
313 case IDYES:
314 case IDNO:
315 hFont = GetPropA(hwnd, "WINE_MSGBOX_HFONT");
316 EndDialog(hwnd, wParam);
317 if (hFont)
318 DeleteObject(hFont);
319 break;
321 break;
323 case WM_HELP:
325 MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
326 HELPINFO hi;
328 memcpy(&hi, (void *)lParam, sizeof(hi));
329 hi.dwContextId = GetWindowContextHelpId(hwnd);
331 if (callback)
332 callback(&hi);
333 else
334 SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
335 break;
338 default:
339 /* Ok. Ignore all the other messages */
340 TRACE("Message number 0x%04x is being ignored.\n", message);
341 break;
343 return 0;
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;
416 int ret;
418 if (HIWORD(msgbox->lpszText))
419 RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
420 else
421 textW.Buffer = (LPWSTR)msgbox->lpszText;
422 if (HIWORD(msgbox->lpszCaption))
423 RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
424 else
425 captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
427 if (msgbox->dwStyle & MB_USERICON)
429 if (HIWORD(msgbox->lpszIcon))
430 RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
431 else
432 iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
434 else
435 iconW.Buffer = NULL;
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);
453 return ret;
456 /**************************************************************************
457 * MessageBoxIndirectW (USER32.@)
459 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
461 LPVOID tmplate;
462 HRSRC hRes;
463 int ret;
464 UINT i;
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)))
470 return 0;
471 if (!(tmplate = (LPVOID)LoadResource(user32_module, hRes)))
472 return 0;
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);
491 return ret;