Added support for anonymous structs/unions on compilers that implement it.
[wine.git] / windows / msgbox.c
blob6c7a5d0d49b3ca9233a0dbb64863d52bcdad9063
1 /*
2 * Message boxes
4 * Copyright 1995 Bernd Schmidt
5 */
7 #include <string.h>
9 #include "wine/winbase16.h"
10 #include "wine/winuser16.h"
11 #include "dlgs.h"
12 #include "heap.h"
13 #include "ldt.h"
14 #include "debugtools.h"
15 #include "debugstr.h"
16 #include "tweak.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;
33 if (TWEAK_WineLook >= WIN95_LOOK) {
34 NONCLIENTMETRICSA nclm;
35 INT i;
36 nclm.cbSize = sizeof(NONCLIENTMETRICSA);
37 SystemParametersInfoA (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
38 hFont = CreateFontIndirectA (&nclm.lfMessageFont);
39 /* set button font */
40 for (i=1; i < 8; i++)
41 SendDlgItemMessageA (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
42 /* set text font */
43 SendDlgItemMessageA (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
45 if (lpmb->lpszCaption) SetWindowTextA(hwnd, lpmb->lpszCaption);
46 SetWindowTextA(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpmb->lpszText);
47 /* Hide not selected buttons */
48 switch(lpmb->dwStyle & MB_TYPEMASK) {
49 case MB_OK:
50 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
51 /* fall through */
52 case MB_OKCANCEL:
53 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
54 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
55 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
56 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
57 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
58 break;
59 case MB_ABORTRETRYIGNORE:
60 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
61 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
62 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
63 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
64 break;
65 case MB_YESNO:
66 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
67 /* fall through */
68 case MB_YESNOCANCEL:
69 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
70 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
71 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
72 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
73 break;
74 case MB_RETRYCANCEL:
75 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
76 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
77 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
78 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
79 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
80 break;
82 /* Set the icon */
83 switch(lpmb->dwStyle & MB_ICONMASK) {
84 case MB_ICONEXCLAMATION:
85 SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
86 (WPARAM16)LoadIcon16(0, IDI_EXCLAMATION16), 0);
87 break;
88 case MB_ICONQUESTION:
89 SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
90 (WPARAM16)LoadIcon16(0, IDI_QUESTION16), 0);
91 break;
92 case MB_ICONASTERISK:
93 SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
94 (WPARAM16)LoadIcon16(0, IDI_ASTERISK16), 0);
95 break;
96 case MB_ICONHAND:
97 default:
98 SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
99 (WPARAM16)LoadIcon16(0, IDI_HAND16), 0);
100 break;
103 /* Position everything */
104 GetWindowRect(hwnd, &rect);
105 borheight = rect.bottom - rect.top;
106 borwidth = rect.right - rect.left;
107 GetClientRect(hwnd, &rect);
108 borheight -= rect.bottom - rect.top;
109 borwidth -= rect.right - rect.left;
111 /* Get the icon height */
112 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
113 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
114 iheight = rect.bottom - rect.top;
115 ileft = rect.left;
116 iwidth = rect.right - ileft;
118 hdc = GetDC(hwnd);
119 if (hFont)
120 hPrevFont = SelectObject(hdc, hFont);
122 /* Get the number of visible buttons and their size */
123 bh = bw = 1; /* Minimum button sizes */
124 for (buttons = 0, i = 1; i < 8; i++)
126 hItem = GetDlgItem(hwnd, i);
127 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE)
129 char buttonText[1024];
130 int w, h;
131 buttons++;
132 if (GetWindowTextA(hItem, buttonText, sizeof buttonText))
134 DrawTextA( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
135 h = rect.bottom - rect.top;
136 w = rect.right - rect.left;
137 if (h > bh) bh = h;
138 if (w > bw) bw = w ;
142 bw = MAX(bw, bh * 2);
143 /* Button white space */
144 bh = bh * 2;
145 bw = bw * 2;
146 bspace = bw/3; /* Space between buttons */
148 /* Get the text size */
149 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
150 rect.top = rect.left = rect.bottom = 0;
151 DrawTextA( hdc, lpmb->lpszText, -1, &rect,
152 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
153 /* Min text width corresponds to space for the buttons */
154 tleft = 2 * ileft + iwidth;
155 twidth = MAX((bw + bspace) * buttons + bspace - tleft, rect.right);
156 theight = rect.bottom;
158 if (hFont)
159 SelectObject(hdc, hPrevFont);
160 ReleaseDC(hItem, hdc);
162 tiheight = 16 + MAX(iheight, theight);
163 wwidth = tleft + twidth + ileft + borwidth;
164 wheight = 8 + tiheight + bh + borheight;
166 /* Resize the window */
167 SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
168 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
170 /* Position the icon */
171 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
172 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
174 /* Position the text */
175 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
176 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
178 /* Position the buttons */
179 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
180 for (buttons = i = 0; i < 7; i++) {
181 /* some arithmetic to get the right order for YesNoCancel windows */
182 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
183 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE) {
184 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
185 SetFocus(hItem);
186 SendMessageA( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
188 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
189 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
190 bpos += bw + bspace;
193 return hFont;
197 /**************************************************************************
198 * MSGBOX_DlgProc
200 * Dialog procedure for message boxes.
202 static LRESULT CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
203 WPARAM wParam, LPARAM lParam )
205 static HFONT hFont;
206 switch(message) {
207 case WM_INITDIALOG:
208 hFont = MSGBOX_OnInit(hwnd, (LPMSGBOXPARAMSA)lParam);
209 return 0;
211 case WM_COMMAND:
212 switch (wParam)
214 case IDOK:
215 case IDCANCEL:
216 case IDABORT:
217 case IDRETRY:
218 case IDIGNORE:
219 case IDYES:
220 case IDNO:
221 EndDialog(hwnd, wParam);
222 if (hFont)
223 DeleteObject(hFont);
224 break;
227 default:
228 /* Ok. Ignore all the other messages */
229 TRACE("Message number %i is being ignored.\n", message);
230 break;
232 return 0;
236 /**************************************************************************
237 * MessageBox16 (USER.1)
239 INT16 WINAPI MessageBox16( HWND16 hwnd, LPCSTR text, LPCSTR title, UINT16 type)
241 WARN("Messagebox\n");
242 return MessageBoxA( hwnd, text, title, type );
246 /**************************************************************************
247 * MessageBox32A (USER32.391)
249 * NOTES
250 * The WARN is here to help debug erroneous MessageBoxes
251 * Use: -debugmsg warn+dialog,+relay
253 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
255 LPVOID template;
256 HRSRC hRes;
257 MSGBOXPARAMSA mbox;
259 WARN("Messagebox\n");
261 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
262 return 0;
263 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
264 return 0;
266 if (!text) text="<WINE-NULL>";
267 if (!title)
268 title="Error";
269 mbox.lpszCaption = title;
270 mbox.lpszText = text;
271 mbox.dwStyle = type;
272 return DialogBoxIndirectParamA( GetWindowLongA(hWnd,GWL_HINSTANCE), template,
273 hWnd, (DLGPROC)MSGBOX_DlgProc, (LPARAM)&mbox );
277 /**************************************************************************
278 * MessageBox32W (USER32.396)
280 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title,
281 UINT type )
283 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
284 LPSTR textA = HEAP_strdupWtoA( GetProcessHeap(), 0, text );
285 INT ret;
287 WARN("Messagebox\n");
289 ret = MessageBoxA( hwnd, textA, titleA, type );
290 HeapFree( GetProcessHeap(), 0, titleA );
291 HeapFree( GetProcessHeap(), 0, textA );
292 return ret;
296 /**************************************************************************
297 * MessageBoxEx32A (USER32.392)
299 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
300 UINT type, WORD langid )
302 WARN("Messagebox\n");
303 /* ignore language id for now */
304 return MessageBoxA(hWnd,text,title,type);
307 /**************************************************************************
308 * MessageBoxEx32W (USER32.393)
310 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
311 UINT type, WORD langid )
313 WARN("Messagebox\n");
314 /* ignore language id for now */
315 return MessageBoxW(hWnd,text,title,type);
318 /**************************************************************************
319 * MessageBoxIndirect16 (USER.827)
321 INT16 WINAPI MessageBoxIndirect16( LPMSGBOXPARAMS16 msgbox )
323 LPVOID template;
324 HRSRC hRes;
325 MSGBOXPARAMSA msgbox32;
327 WARN("Messagebox\n");
329 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
330 return 0;
331 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
332 return 0;
334 msgbox32.cbSize = msgbox->cbSize;
335 msgbox32.hwndOwner = msgbox->hwndOwner;
336 msgbox32.hInstance = msgbox->hInstance;
337 msgbox32.lpszText = PTR_SEG_TO_LIN(msgbox->lpszText);
338 msgbox32.lpszCaption = PTR_SEG_TO_LIN(msgbox->lpszCaption);
339 msgbox32.dwStyle = msgbox->dwStyle;
340 msgbox32.lpszIcon = PTR_SEG_TO_LIN(msgbox->lpszIcon);
341 msgbox32.dwContextHelpId = msgbox->dwContextHelpId;
342 msgbox32.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
343 msgbox32.dwLanguageId = msgbox->dwLanguageId;
345 return DialogBoxIndirectParamA( msgbox32.hInstance, template,
346 msgbox32.hwndOwner, (DLGPROC)MSGBOX_DlgProc,
347 (LPARAM)&msgbox32 );
350 /**************************************************************************
351 * MessageBoxIndirect32A (USER32.394)
353 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
355 LPVOID template;
356 HRSRC hRes;
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 return DialogBoxIndirectParamA( msgbox->hInstance, template,
366 msgbox->hwndOwner, (DLGPROC)MSGBOX_DlgProc,
367 (LPARAM)msgbox );
370 /**************************************************************************
371 * MessageBoxIndirect32W (USER32.395)
373 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
375 MSGBOXPARAMSA msgboxa;
376 WARN("Messagebox\n");
378 memcpy(&msgboxa,msgbox,sizeof(msgboxa));
379 if (msgbox->lpszCaption)
380 lstrcpyWtoA((LPSTR)msgboxa.lpszCaption,msgbox->lpszCaption);
381 if (msgbox->lpszText)
382 lstrcpyWtoA((LPSTR)msgboxa.lpszText,msgbox->lpszText);
384 return MessageBoxIndirectA(&msgboxa);
388 /**************************************************************************
389 * FatalAppExit16 (KERNEL.137)
391 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
393 WARN("AppExit\n");
394 FatalAppExitA( action, str );
398 /**************************************************************************
399 * FatalAppExit32A (KERNEL32.108)
401 void WINAPI FatalAppExitA( UINT action, LPCSTR str )
403 WARN("AppExit\n");
404 MessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
405 ExitProcess(0);
409 /**************************************************************************
410 * FatalAppExit32W (KERNEL32.109)
412 void WINAPI FatalAppExitW( UINT action, LPCWSTR str )
414 WARN("AppExit\n");
415 MessageBoxW( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
416 ExitProcess(0);