Block SIGINT and SIGALRM in signal handlers.
[wine/multimedia.git] / windows / msgbox.c
blobf4616f25cc53bceca14dbb5f8e4a868813018e3f
1 /*
2 * Message boxes
4 * Copyright 1995 Bernd Schmidt
5 */
7 #include <string.h>
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "wine/winbase16.h"
12 #include "wine/winuser16.h"
13 #include "dlgs.h"
14 #include "heap.h"
15 #include "user.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(dialog);
20 #define MSGBOX_IDICON 1088
21 #define MSGBOX_IDTEXT 100
23 static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSA lpmb)
25 static HFONT hFont = 0, hPrevFont = 0;
26 RECT rect;
27 HWND hItem;
28 HDC hdc;
29 int i, buttons;
30 int bspace, bw, bh, theight, tleft, wwidth, wheight, bpos;
31 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
32 LPCSTR lpszText;
33 char buf[256];
35 if (TWEAK_WineLook >= WIN95_LOOK) {
36 NONCLIENTMETRICSA nclm;
37 nclm.cbSize = sizeof(NONCLIENTMETRICSA);
38 SystemParametersInfoA (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
39 hFont = CreateFontIndirectA (&nclm.lfMessageFont);
40 /* set button font */
41 for (i=1; i < 8; i++)
42 SendDlgItemMessageA (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
43 /* set text font */
44 SendDlgItemMessageA (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
46 if (HIWORD(lpmb->lpszCaption)) {
47 SetWindowTextA(hwnd, lpmb->lpszCaption);
48 } else {
49 if (LoadStringA(lpmb->hInstance, LOWORD(lpmb->lpszCaption), buf, sizeof(buf)))
50 SetWindowTextA(hwnd, buf);
52 if (HIWORD(lpmb->lpszText)) {
53 lpszText = lpmb->lpszText;
54 } else {
55 lpszText = buf;
56 if (!LoadStringA(lpmb->hInstance, LOWORD(lpmb->lpszText), buf, sizeof(buf)))
57 *buf = 0; /* FIXME ?? */
59 SetWindowTextA(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
61 /* Hide not selected buttons */
62 switch(lpmb->dwStyle & MB_TYPEMASK) {
63 case MB_OK:
64 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
65 /* fall through */
66 case MB_OKCANCEL:
67 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
68 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
69 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
70 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
71 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
72 break;
73 case MB_ABORTRETRYIGNORE:
74 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
75 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
76 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
77 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
78 break;
79 case MB_YESNO:
80 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
81 /* fall through */
82 case MB_YESNOCANCEL:
83 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
84 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
85 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
86 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
87 break;
88 case MB_RETRYCANCEL:
89 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
90 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
91 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
92 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
93 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
94 break;
96 /* Set the icon */
97 switch(lpmb->dwStyle & MB_ICONMASK) {
98 case MB_ICONEXCLAMATION:
99 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_EXCLAMATIONA), 0);
100 break;
101 case MB_ICONQUESTION:
102 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_QUESTIONA), 0);
103 break;
104 case MB_ICONASTERISK:
105 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_ASTERISKA), 0);
106 break;
107 case MB_ICONHAND:
108 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_HANDA), 0);
109 break;
110 default:
111 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
112 * So wine should do the same.
114 break;
117 /* Position everything */
118 GetWindowRect(hwnd, &rect);
119 borheight = rect.bottom - rect.top;
120 borwidth = rect.right - rect.left;
121 GetClientRect(hwnd, &rect);
122 borheight -= rect.bottom - rect.top;
123 borwidth -= rect.right - rect.left;
125 /* Get the icon height */
126 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
127 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
128 iheight = rect.bottom - rect.top;
129 ileft = rect.left;
130 iwidth = rect.right - ileft;
132 hdc = GetDC(hwnd);
133 if (hFont)
134 hPrevFont = SelectObject(hdc, hFont);
136 /* Get the number of visible buttons and their size */
137 bh = bw = 1; /* Minimum button sizes */
138 for (buttons = 0, i = 1; i < 8; i++)
140 hItem = GetDlgItem(hwnd, i);
141 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE)
143 char buttonText[1024];
144 int w, h;
145 buttons++;
146 if (GetWindowTextA(hItem, buttonText, sizeof buttonText))
148 DrawTextA( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
149 h = rect.bottom - rect.top;
150 w = rect.right - rect.left;
151 if (h > bh) bh = h;
152 if (w > bw) bw = w ;
156 bw = max(bw, bh * 2);
157 /* Button white space */
158 bh = bh * 2;
159 bw = bw * 2;
160 bspace = bw/3; /* Space between buttons */
162 /* Get the text size */
163 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
164 rect.top = rect.left = rect.bottom = 0;
165 DrawTextA( hdc, lpszText, -1, &rect,
166 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
167 /* Min text width corresponds to space for the buttons */
168 tleft = 2 * ileft + iwidth;
169 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
170 theight = rect.bottom;
172 if (hFont)
173 SelectObject(hdc, hPrevFont);
174 ReleaseDC(hItem, hdc);
176 tiheight = 16 + max(iheight, theight);
177 wwidth = tleft + twidth + ileft + borwidth;
178 wheight = 8 + tiheight + bh + borheight;
180 /* Resize the window */
181 SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
182 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
184 /* Position the icon */
185 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
186 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
188 /* Position the text */
189 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
190 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
192 /* Position the buttons */
193 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
194 for (buttons = i = 0; i < 7; i++) {
195 /* some arithmetic to get the right order for YesNoCancel windows */
196 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
197 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE) {
198 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
199 SetFocus(hItem);
200 SendMessageA( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
202 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
203 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
204 bpos += bw + bspace;
208 /* handle modal MessageBoxes */
209 if (lpmb->dwStyle & (MB_TASKMODAL|MB_SYSTEMMODAL))
211 FIXME("%s modal msgbox ! Not modal yet.\n",
212 lpmb->dwStyle & MB_TASKMODAL ? "task" : "system");
213 /* Probably do EnumTaskWindows etc. here for TASKMODAL
214 * and work your way up to the top - I'm lazy (HWND_TOP) */
215 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
216 SWP_NOSIZE | SWP_NOMOVE);
217 if (lpmb->dwStyle & MB_TASKMODAL)
218 /* at least MB_TASKMODAL seems to imply a ShowWindow */
219 ShowWindow(hwnd, SW_SHOW);
221 if (lpmb->dwStyle & MB_APPLMODAL)
222 FIXME("app modal msgbox ! Not modal yet.\n");
224 return hFont;
228 /**************************************************************************
229 * MSGBOX_DlgProc
231 * Dialog procedure for message boxes.
233 static LRESULT CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
234 WPARAM wParam, LPARAM lParam )
236 static HFONT hFont;
237 switch(message) {
238 case WM_INITDIALOG:
239 hFont = MSGBOX_OnInit(hwnd, (LPMSGBOXPARAMSA)lParam);
240 return 0;
242 case WM_COMMAND:
243 switch (wParam)
245 case IDOK:
246 case IDCANCEL:
247 case IDABORT:
248 case IDRETRY:
249 case IDIGNORE:
250 case IDYES:
251 case IDNO:
252 EndDialog(hwnd, wParam);
253 if (hFont)
254 DeleteObject(hFont);
255 break;
258 default:
259 /* Ok. Ignore all the other messages */
260 TRACE("Message number 0x%04x is being ignored.\n", message);
261 break;
263 return 0;
267 /**************************************************************************
268 * MessageBox (USER.1)
270 INT16 WINAPI MessageBox16( HWND16 hwnd, LPCSTR text, LPCSTR title, UINT16 type)
272 WARN("Messagebox\n");
273 return MessageBoxA( hwnd, text, title, type );
277 /**************************************************************************
278 * MessageBoxA (USER32.@)
280 * NOTES
281 * The WARN is here to help debug erroneous MessageBoxes
282 * Use: -debugmsg warn+dialog,+relay
284 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
286 LPVOID template;
287 HRSRC hRes;
288 MSGBOXPARAMSA mbox;
290 WARN("Messagebox\n");
292 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
293 return 0;
294 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
295 return 0;
297 if (!text) text="<WINE-NULL>";
298 if (!title)
299 title="Error";
300 mbox.lpszCaption = title;
301 mbox.lpszText = text;
302 mbox.dwStyle = type;
303 return DialogBoxIndirectParamA( GetWindowLongA(hWnd,GWL_HINSTANCE), template,
304 hWnd, (DLGPROC)MSGBOX_DlgProc, (LPARAM)&mbox );
308 /**************************************************************************
309 * MessageBoxW (USER32.@)
311 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title,
312 UINT type )
314 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
315 LPSTR textA = HEAP_strdupWtoA( GetProcessHeap(), 0, text );
316 INT ret;
318 WARN("Messagebox\n");
320 ret = MessageBoxA( hwnd, textA, titleA, type );
321 HeapFree( GetProcessHeap(), 0, titleA );
322 HeapFree( GetProcessHeap(), 0, textA );
323 return ret;
327 /**************************************************************************
328 * MessageBoxExA (USER32.@)
330 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
331 UINT type, WORD langid )
333 WARN("Messagebox\n");
334 /* ignore language id for now */
335 return MessageBoxA(hWnd,text,title,type);
338 /**************************************************************************
339 * MessageBoxExW (USER32.@)
341 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
342 UINT type, WORD langid )
344 WARN("Messagebox\n");
345 /* ignore language id for now */
346 return MessageBoxW(hWnd,text,title,type);
349 /**************************************************************************
350 * MessageBoxIndirect (USER.827)
352 INT16 WINAPI MessageBoxIndirect16( LPMSGBOXPARAMS16 msgbox )
354 LPVOID template;
355 HRSRC hRes;
356 MSGBOXPARAMSA msgbox32;
358 WARN("Messagebox\n");
360 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
361 return 0;
362 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
363 return 0;
365 msgbox32.cbSize = msgbox->cbSize;
366 msgbox32.hwndOwner = msgbox->hwndOwner;
367 msgbox32.hInstance = msgbox->hInstance;
368 msgbox32.lpszText = MapSL(msgbox->lpszText);
369 msgbox32.lpszCaption = MapSL(msgbox->lpszCaption);
370 msgbox32.dwStyle = msgbox->dwStyle;
371 msgbox32.lpszIcon = MapSL(msgbox->lpszIcon);
372 msgbox32.dwContextHelpId = msgbox->dwContextHelpId;
373 msgbox32.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
374 msgbox32.dwLanguageId = msgbox->dwLanguageId;
376 return DialogBoxIndirectParamA( msgbox32.hInstance, template,
377 msgbox32.hwndOwner, (DLGPROC)MSGBOX_DlgProc,
378 (LPARAM)&msgbox32 );
381 /**************************************************************************
382 * MessageBoxIndirectA (USER32.@)
384 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
386 LPVOID template;
387 HRSRC hRes;
389 WARN("Messagebox\n");
391 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
392 return 0;
393 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
394 return 0;
396 return DialogBoxIndirectParamA( msgbox->hInstance, template,
397 msgbox->hwndOwner, (DLGPROC)MSGBOX_DlgProc,
398 (LPARAM)msgbox );
401 /**************************************************************************
402 * MessageBoxIndirectW (USER32.@)
404 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
406 MSGBOXPARAMSA msgboxa;
407 memcpy(&msgboxa,msgbox,sizeof(msgboxa));
408 msgboxa.lpszCaption = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszCaption );
409 msgboxa.lpszText = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszText );
410 msgboxa.lpszIcon = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszIcon );
411 return MessageBoxIndirectA(&msgboxa);