rpcrt4: Store all active connections in RpcServerProtseq.
[wine.git] / dlls / user32 / msgbox.c
blobd3d11d6ceb137b4bf3afede36a5a2c7edbaaba1f
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 "resources.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
35 WINE_DECLARE_DEBUG_CHANNEL(msgbox);
37 struct ThreadWindows
39 UINT numHandles;
40 UINT numAllocs;
41 HWND *handles;
44 static BOOL CALLBACK MSGBOX_EnumProc(HWND hwnd, LPARAM lParam)
46 struct ThreadWindows *threadWindows = (struct ThreadWindows *)lParam;
48 if (!EnableWindow(hwnd, FALSE))
50 if(threadWindows->numHandles >= threadWindows->numAllocs)
52 threadWindows->handles = HeapReAlloc(GetProcessHeap(), 0, threadWindows->handles,
53 (threadWindows->numAllocs*2)*sizeof(HWND));
54 threadWindows->numAllocs *= 2;
56 threadWindows->handles[threadWindows->numHandles++]=hwnd;
58 return TRUE;
61 static void MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
63 HFONT hPrevFont;
64 RECT rect;
65 HWND hItem;
66 HDC hdc;
67 int i, buttons;
68 int bspace, bw, bh, theight, tleft, wwidth, wheight, wleft, wtop, bpos;
69 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
70 NONCLIENTMETRICSW nclm;
71 HMONITOR monitor;
72 MONITORINFO mon_info;
73 LPCWSTR lpszText;
74 WCHAR *buffer = NULL;
75 const WCHAR *ptr;
77 /* Index the order the buttons need to appear to an ID* constant */
78 static const int buttonOrder[10] = { IDYES, IDNO, IDOK, IDABORT, IDRETRY,
79 IDCANCEL, IDIGNORE, IDTRYAGAIN,
80 IDCONTINUE, IDHELP };
82 nclm.cbSize = sizeof(nclm);
83 SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
85 if (!IS_INTRESOURCE(lpmb->lpszCaption)) {
86 SetWindowTextW(hwnd, lpmb->lpszCaption);
87 } else {
88 UINT len = LoadStringW( lpmb->hInstance, LOWORD(lpmb->lpszCaption), (LPWSTR)&ptr, 0 );
89 if (!len) len = LoadStringW( user32_module, IDS_ERROR, (LPWSTR)&ptr, 0 );
90 buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
91 if (buffer)
93 memcpy( buffer, ptr, len * sizeof(WCHAR) );
94 buffer[len] = 0;
95 SetWindowTextW( hwnd, buffer );
96 HeapFree( GetProcessHeap(), 0, buffer );
97 buffer = NULL;
100 if (IS_INTRESOURCE(lpmb->lpszText)) {
101 UINT len = LoadStringW( lpmb->hInstance, LOWORD(lpmb->lpszText), (LPWSTR)&ptr, 0 );
102 lpszText = buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
103 if (buffer)
105 memcpy( buffer, ptr, len * sizeof(WCHAR) );
106 buffer[len] = 0;
108 } else {
109 lpszText = lpmb->lpszText;
112 TRACE_(msgbox)("%s\n", debugstr_w(lpszText));
113 SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
115 /* Remove not selected buttons and assign the WS_GROUP style to the first button */
116 hItem = 0;
117 switch(lpmb->dwStyle & MB_TYPEMASK) {
118 case MB_OK:
119 DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
120 /* fall through */
121 case MB_OKCANCEL:
122 hItem = GetDlgItem(hwnd, IDOK);
123 DestroyWindow(GetDlgItem(hwnd, IDABORT));
124 DestroyWindow(GetDlgItem(hwnd, IDRETRY));
125 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
126 DestroyWindow(GetDlgItem(hwnd, IDYES));
127 DestroyWindow(GetDlgItem(hwnd, IDNO));
128 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
129 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
130 break;
131 case MB_ABORTRETRYIGNORE:
132 hItem = GetDlgItem(hwnd, IDABORT);
133 DestroyWindow(GetDlgItem(hwnd, IDOK));
134 DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
135 DestroyWindow(GetDlgItem(hwnd, IDYES));
136 DestroyWindow(GetDlgItem(hwnd, IDNO));
137 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
138 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
139 break;
140 case MB_YESNO:
141 DestroyWindow(GetDlgItem(hwnd, IDCANCEL));
142 /* fall through */
143 case MB_YESNOCANCEL:
144 hItem = GetDlgItem(hwnd, IDYES);
145 DestroyWindow(GetDlgItem(hwnd, IDOK));
146 DestroyWindow(GetDlgItem(hwnd, IDABORT));
147 DestroyWindow(GetDlgItem(hwnd, IDRETRY));
148 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
149 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
150 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
151 break;
152 case MB_RETRYCANCEL:
153 hItem = GetDlgItem(hwnd, IDRETRY);
154 DestroyWindow(GetDlgItem(hwnd, IDOK));
155 DestroyWindow(GetDlgItem(hwnd, IDABORT));
156 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
157 DestroyWindow(GetDlgItem(hwnd, IDYES));
158 DestroyWindow(GetDlgItem(hwnd, IDNO));
159 DestroyWindow(GetDlgItem(hwnd, IDCONTINUE));
160 DestroyWindow(GetDlgItem(hwnd, IDTRYAGAIN));
161 break;
162 case MB_CANCELTRYCONTINUE:
163 hItem = GetDlgItem(hwnd, IDCANCEL);
164 DestroyWindow(GetDlgItem(hwnd, IDOK));
165 DestroyWindow(GetDlgItem(hwnd, IDABORT));
166 DestroyWindow(GetDlgItem(hwnd, IDIGNORE));
167 DestroyWindow(GetDlgItem(hwnd, IDYES));
168 DestroyWindow(GetDlgItem(hwnd, IDNO));
169 DestroyWindow(GetDlgItem(hwnd, IDRETRY));
172 if (hItem) SetWindowLongW(hItem, GWL_STYLE, GetWindowLongW(hItem, GWL_STYLE) | WS_GROUP);
174 /* Set the icon */
175 switch(lpmb->dwStyle & MB_ICONMASK) {
176 case MB_ICONEXCLAMATION:
177 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
178 (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
179 break;
180 case MB_ICONQUESTION:
181 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
182 (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
183 break;
184 case MB_ICONASTERISK:
185 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
186 (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
187 break;
188 case MB_ICONHAND:
189 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
190 (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
191 break;
192 case MB_USERICON:
193 SendDlgItemMessageW(hwnd, MSGBOX_IDICON, STM_SETICON,
194 (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
195 break;
196 default:
197 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
198 * So wine should do the same.
200 break;
203 /* Remove Help button unless MB_HELP supplied */
204 if (!(lpmb->dwStyle & MB_HELP)) {
205 DestroyWindow(GetDlgItem(hwnd, IDHELP));
208 /* Position everything */
209 GetWindowRect(hwnd, &rect);
210 borheight = rect.bottom - rect.top;
211 borwidth = rect.right - rect.left;
212 GetClientRect(hwnd, &rect);
213 borheight -= rect.bottom - rect.top;
214 borwidth -= rect.right - rect.left;
216 /* Get the icon height */
217 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
218 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
219 if (!(lpmb->dwStyle & MB_ICONMASK))
221 rect.bottom = rect.top;
222 rect.right = rect.left;
224 iheight = rect.bottom - rect.top;
225 ileft = rect.left;
226 iwidth = rect.right - ileft;
228 hdc = GetDC(hwnd);
229 hPrevFont = SelectObject( hdc, (HFONT)SendMessageW( hwnd, WM_GETFONT, 0, 0 ));
231 /* Get the number of visible buttons and their size */
232 bh = bw = 1; /* Minimum button sizes */
233 for (buttons = 0, i = IDOK; i <= IDCONTINUE; i++)
235 if (i == IDCLOSE) continue; /* No CLOSE button */
236 hItem = GetDlgItem(hwnd, i);
237 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
239 WCHAR buttonText[1024];
240 int w, h;
241 buttons++;
242 if (GetWindowTextW(hItem, buttonText, 1024))
244 DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
245 h = rect.bottom - rect.top;
246 w = rect.right - rect.left;
247 if (h > bh) bh = h;
248 if (w > bw) bw = w ;
252 bw = max(bw, bh * 2);
253 /* Button white space */
254 bh = bh * 2;
255 bw = bw * 2;
256 bspace = bw/3; /* Space between buttons */
258 /* Get the text size */
259 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
260 rect.top = rect.left = rect.bottom = 0;
261 DrawTextW(hdc, lpszText, -1, &rect,
262 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT | DT_NOPREFIX);
263 /* Min text width corresponds to space for the buttons */
264 tleft = ileft;
265 if (iwidth) tleft += ileft + iwidth;
266 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
267 theight = rect.bottom;
269 SelectObject(hdc, hPrevFont);
270 ReleaseDC(hwnd, hdc);
272 tiheight = 16 + max(iheight, theight);
273 wwidth = tleft + twidth + ileft + borwidth;
274 wheight = 8 + tiheight + bh + borheight;
276 /* Message boxes are always desktop centered, so query desktop size and center window */
277 monitor = MonitorFromWindow(lpmb->hwndOwner ? lpmb->hwndOwner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY);
278 mon_info.cbSize = sizeof(mon_info);
279 GetMonitorInfoW(monitor, &mon_info);
280 wleft = (mon_info.rcWork.left + mon_info.rcWork.right - wwidth) / 2;
281 wtop = (mon_info.rcWork.top + mon_info.rcWork.bottom - wheight) / 2;
283 /* Resize and center the window */
284 SetWindowPos(hwnd, 0, wleft, wtop, wwidth, wheight,
285 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
287 /* Position the icon */
288 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
289 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
291 /* Position the text */
292 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
293 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
295 /* Position the buttons */
296 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
297 for (buttons = i = 0; i < (sizeof(buttonOrder) / sizeof(buttonOrder[0])); i++) {
299 /* Convert the button order to ID* value to order for the buttons */
300 hItem = GetDlgItem(hwnd, buttonOrder[i]);
301 if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE) {
302 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
303 SetFocus(hItem);
304 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
306 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
307 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
308 bpos += bw + bspace;
312 /*handle modal message boxes*/
313 if (((lpmb->dwStyle & MB_TASKMODAL) && (lpmb->hwndOwner==NULL)) || (lpmb->dwStyle & MB_SYSTEMMODAL))
314 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
316 HeapFree( GetProcessHeap(), 0, buffer );
320 /**************************************************************************
321 * MSGBOX_DlgProc
323 * Dialog procedure for message boxes.
325 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
326 WPARAM wParam, LPARAM lParam )
328 switch(message) {
329 case WM_INITDIALOG:
331 LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
332 SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
333 MSGBOX_OnInit(hwnd, mbp);
334 SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", mbp->lpfnMsgBoxCallback);
335 break;
338 case WM_COMMAND:
339 switch (LOWORD(wParam))
341 case IDOK:
342 case IDCANCEL:
343 case IDABORT:
344 case IDRETRY:
345 case IDIGNORE:
346 case IDYES:
347 case IDNO:
348 case IDTRYAGAIN:
349 case IDCONTINUE:
350 EndDialog(hwnd, wParam);
351 break;
352 case IDHELP:
353 FIXME("Help button not supported yet\n");
354 break;
356 break;
358 case WM_HELP:
360 MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
361 HELPINFO hi;
363 memcpy(&hi, (void *)lParam, sizeof(hi));
364 hi.dwContextId = GetWindowContextHelpId(hwnd);
366 if (callback)
367 callback(&hi);
368 else
369 SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
370 break;
373 default:
374 /* Ok. Ignore all the other messages */
375 TRACE("Message number 0x%04x is being ignored.\n", message);
376 break;
378 return 0;
382 /**************************************************************************
383 * MessageBoxA (USER32.@)
385 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
387 return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
391 /**************************************************************************
392 * MessageBoxW (USER32.@)
394 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
396 return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
400 /**************************************************************************
401 * MessageBoxExA (USER32.@)
403 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
404 UINT type, WORD langid )
406 MSGBOXPARAMSA msgbox;
408 msgbox.cbSize = sizeof(msgbox);
409 msgbox.hwndOwner = hWnd;
410 msgbox.hInstance = 0;
411 msgbox.lpszText = text;
412 msgbox.lpszCaption = title;
413 msgbox.dwStyle = type;
414 msgbox.lpszIcon = NULL;
415 msgbox.dwContextHelpId = 0;
416 msgbox.lpfnMsgBoxCallback = NULL;
417 msgbox.dwLanguageId = langid;
419 return MessageBoxIndirectA(&msgbox);
422 /**************************************************************************
423 * MessageBoxExW (USER32.@)
425 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
426 UINT type, WORD langid )
428 MSGBOXPARAMSW msgbox;
430 msgbox.cbSize = sizeof(msgbox);
431 msgbox.hwndOwner = hWnd;
432 msgbox.hInstance = 0;
433 msgbox.lpszText = text;
434 msgbox.lpszCaption = title;
435 msgbox.dwStyle = type;
436 msgbox.lpszIcon = NULL;
437 msgbox.dwContextHelpId = 0;
438 msgbox.lpfnMsgBoxCallback = NULL;
439 msgbox.dwLanguageId = langid;
441 return MessageBoxIndirectW(&msgbox);
444 /**************************************************************************
445 * MessageBoxTimeoutA (USER32.@)
447 INT WINAPI MessageBoxTimeoutA( HWND hWnd, LPCSTR text, LPCSTR title,
448 UINT type, WORD langid, DWORD timeout )
450 FIXME("timeout not supported (%u)\n", timeout);
451 return MessageBoxExA( hWnd, text, title, type, langid );
454 /**************************************************************************
455 * MessageBoxTimeoutW (USER32.@)
457 INT WINAPI MessageBoxTimeoutW( HWND hWnd, LPCWSTR text, LPCWSTR title,
458 UINT type, WORD langid, DWORD timeout )
460 FIXME("timeout not supported (%u)\n", timeout);
461 return MessageBoxExW( hWnd, text, title, type, langid );
464 /**************************************************************************
465 * MessageBoxIndirectA (USER32.@)
467 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
469 MSGBOXPARAMSW msgboxW;
470 UNICODE_STRING textW, captionW, iconW;
471 int ret;
473 if (IS_INTRESOURCE(msgbox->lpszText))
474 textW.Buffer = (LPWSTR)msgbox->lpszText;
475 else
476 RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
477 if (IS_INTRESOURCE(msgbox->lpszCaption))
478 captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
479 else
480 RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
482 if (msgbox->dwStyle & MB_USERICON)
484 if (IS_INTRESOURCE(msgbox->lpszIcon))
485 iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
486 else
487 RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
489 else
490 iconW.Buffer = NULL;
492 msgboxW.cbSize = sizeof(msgboxW);
493 msgboxW.hwndOwner = msgbox->hwndOwner;
494 msgboxW.hInstance = msgbox->hInstance;
495 msgboxW.lpszText = textW.Buffer;
496 msgboxW.lpszCaption = captionW.Buffer;
497 msgboxW.dwStyle = msgbox->dwStyle;
498 msgboxW.lpszIcon = iconW.Buffer;
499 msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
500 msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
501 msgboxW.dwLanguageId = msgbox->dwLanguageId;
503 ret = MessageBoxIndirectW(&msgboxW);
505 if (!IS_INTRESOURCE(textW.Buffer)) RtlFreeUnicodeString(&textW);
506 if (!IS_INTRESOURCE(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
507 if (!IS_INTRESOURCE(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
508 return ret;
511 /**************************************************************************
512 * MessageBoxIndirectW (USER32.@)
514 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
516 LPVOID tmplate;
517 HRSRC hRes;
518 int ret;
519 UINT i;
520 struct ThreadWindows threadWindows;
521 static const WCHAR msg_box_res_nameW[] = { 'M','S','G','B','O','X',0 };
523 if (!(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG,
524 msg_box_res_nameW, msgbox->dwLanguageId)))
526 if (!msgbox->dwLanguageId ||
527 !(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG, msg_box_res_nameW, LANG_NEUTRAL)))
528 return 0;
530 if (!(tmplate = LoadResource(user32_module, hRes)))
531 return 0;
533 if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
535 threadWindows.numHandles = 0;
536 threadWindows.numAllocs = 10;
537 threadWindows.handles = HeapAlloc(GetProcessHeap(), 0, 10*sizeof(HWND));
538 EnumThreadWindows(GetCurrentThreadId(), MSGBOX_EnumProc, (LPARAM)&threadWindows);
541 ret=DialogBoxIndirectParamW(msgbox->hInstance, tmplate,
542 msgbox->hwndOwner, MSGBOX_DlgProc, (LPARAM)msgbox);
544 if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
546 for (i = 0; i < threadWindows.numHandles; i++)
547 EnableWindow(threadWindows.handles[i], TRUE);
548 HeapFree(GetProcessHeap(), 0, threadWindows.handles);
550 return ret;