Added to project. Currently incomplete but will update weekly.
[wine.git] / windows / msgbox.c
blobf43f0b1297661a03f8244a5447b823468104b46c
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 "ldt.h"
16 #include "debugtools.h"
17 #include "tweak.h"
19 DEFAULT_DEBUG_CHANNEL(dialog)
21 #define MSGBOX_IDICON 1088
22 #define MSGBOX_IDTEXT 100
24 static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSA lpmb)
26 static HFONT hFont = 0, hPrevFont = 0;
27 RECT rect;
28 HWND hItem;
29 HDC hdc;
30 int i, buttons;
31 int bspace, bw, bh, theight, tleft, wwidth, wheight, bpos;
32 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
33 LPCSTR lpszText;
34 char buf[256];
36 if (TWEAK_WineLook >= WIN95_LOOK) {
37 NONCLIENTMETRICSA nclm;
38 INT i;
39 nclm.cbSize = sizeof(NONCLIENTMETRICSA);
40 SystemParametersInfoA (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
41 hFont = CreateFontIndirectA (&nclm.lfMessageFont);
42 /* set button font */
43 for (i=1; i < 8; i++)
44 SendDlgItemMessageA (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
45 /* set text font */
46 SendDlgItemMessageA (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
48 if (HIWORD(lpmb->lpszCaption)) {
49 SetWindowTextA(hwnd, lpmb->lpszCaption);
50 } else {
51 if (LoadStringA(lpmb->hInstance, LOWORD(lpmb->lpszCaption), buf, sizeof(buf)))
52 SetWindowTextA(hwnd, buf);
54 if (HIWORD(lpmb->lpszText)) {
55 lpszText = lpmb->lpszText;
56 } else {
57 lpszText = buf;
58 if (!LoadStringA(lpmb->hInstance, LOWORD(lpmb->lpszText), buf, sizeof(buf)))
59 *buf = 0; /* FIXME ?? */
61 SetWindowTextA(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
63 /* Hide not selected buttons */
64 switch(lpmb->dwStyle & MB_TYPEMASK) {
65 case MB_OK:
66 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
67 /* fall through */
68 case MB_OKCANCEL:
69 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
70 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
71 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
72 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
73 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
74 break;
75 case MB_ABORTRETRYIGNORE:
76 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
77 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
78 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
79 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
80 break;
81 case MB_YESNO:
82 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
83 /* fall through */
84 case MB_YESNOCANCEL:
85 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
86 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
87 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
88 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
89 break;
90 case MB_RETRYCANCEL:
91 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
92 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
93 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
94 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
95 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
96 break;
98 /* Set the icon */
99 switch(lpmb->dwStyle & MB_ICONMASK) {
100 case MB_ICONEXCLAMATION:
101 SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
102 (WPARAM16)LoadIcon16(0, IDI_EXCLAMATION16), 0);
103 break;
104 case MB_ICONQUESTION:
105 SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
106 (WPARAM16)LoadIcon16(0, IDI_QUESTION16), 0);
107 break;
108 case MB_ICONASTERISK:
109 SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
110 (WPARAM16)LoadIcon16(0, IDI_ASTERISK16), 0);
111 break;
112 case MB_ICONHAND:
113 default:
114 SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
115 (WPARAM16)LoadIcon16(0, IDI_HAND16), 0);
116 break;
119 /* Position everything */
120 GetWindowRect(hwnd, &rect);
121 borheight = rect.bottom - rect.top;
122 borwidth = rect.right - rect.left;
123 GetClientRect(hwnd, &rect);
124 borheight -= rect.bottom - rect.top;
125 borwidth -= rect.right - rect.left;
127 /* Get the icon height */
128 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
129 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
130 iheight = rect.bottom - rect.top;
131 ileft = rect.left;
132 iwidth = rect.right - ileft;
134 hdc = GetDC(hwnd);
135 if (hFont)
136 hPrevFont = SelectObject(hdc, hFont);
138 /* Get the number of visible buttons and their size */
139 bh = bw = 1; /* Minimum button sizes */
140 for (buttons = 0, i = 1; i < 8; i++)
142 hItem = GetDlgItem(hwnd, i);
143 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE)
145 char buttonText[1024];
146 int w, h;
147 buttons++;
148 if (GetWindowTextA(hItem, buttonText, sizeof buttonText))
150 DrawTextA( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
151 h = rect.bottom - rect.top;
152 w = rect.right - rect.left;
153 if (h > bh) bh = h;
154 if (w > bw) bw = w ;
158 bw = max(bw, bh * 2);
159 /* Button white space */
160 bh = bh * 2;
161 bw = bw * 2;
162 bspace = bw/3; /* Space between buttons */
164 /* Get the text size */
165 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
166 rect.top = rect.left = rect.bottom = 0;
167 DrawTextA( hdc, lpszText, -1, &rect,
168 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
169 /* Min text width corresponds to space for the buttons */
170 tleft = 2 * ileft + iwidth;
171 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
172 theight = rect.bottom;
174 if (hFont)
175 SelectObject(hdc, hPrevFont);
176 ReleaseDC(hItem, hdc);
178 tiheight = 16 + max(iheight, theight);
179 wwidth = tleft + twidth + ileft + borwidth;
180 wheight = 8 + tiheight + bh + borheight;
182 /* Resize the window */
183 SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
184 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
186 /* Position the icon */
187 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
188 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
190 /* Position the text */
191 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
192 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
194 /* Position the buttons */
195 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
196 for (buttons = i = 0; i < 7; i++) {
197 /* some arithmetic to get the right order for YesNoCancel windows */
198 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
199 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE) {
200 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
201 SetFocus(hItem);
202 SendMessageA( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
204 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
205 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
206 bpos += bw + bspace;
209 return hFont;
213 /**************************************************************************
214 * MSGBOX_DlgProc
216 * Dialog procedure for message boxes.
218 static LRESULT CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
219 WPARAM wParam, LPARAM lParam )
221 static HFONT hFont;
222 switch(message) {
223 case WM_INITDIALOG:
224 hFont = MSGBOX_OnInit(hwnd, (LPMSGBOXPARAMSA)lParam);
225 return 0;
227 case WM_COMMAND:
228 switch (wParam)
230 case IDOK:
231 case IDCANCEL:
232 case IDABORT:
233 case IDRETRY:
234 case IDIGNORE:
235 case IDYES:
236 case IDNO:
237 EndDialog(hwnd, wParam);
238 if (hFont)
239 DeleteObject(hFont);
240 break;
243 default:
244 /* Ok. Ignore all the other messages */
245 TRACE("Message number %i is being ignored.\n", message);
246 break;
248 return 0;
252 /**************************************************************************
253 * MessageBox16 (USER.1)
255 INT16 WINAPI MessageBox16( HWND16 hwnd, LPCSTR text, LPCSTR title, UINT16 type)
257 WARN("Messagebox\n");
258 return MessageBoxA( hwnd, text, title, type );
262 /**************************************************************************
263 * MessageBoxA (USER32.391)
265 * NOTES
266 * The WARN is here to help debug erroneous MessageBoxes
267 * Use: -debugmsg warn+dialog,+relay
269 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
271 LPVOID template;
272 HRSRC hRes;
273 MSGBOXPARAMSA mbox;
275 WARN("Messagebox\n");
277 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
278 return 0;
279 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
280 return 0;
282 if (!text) text="<WINE-NULL>";
283 if (!title)
284 title="Error";
285 mbox.lpszCaption = title;
286 mbox.lpszText = text;
287 mbox.dwStyle = type;
288 return DialogBoxIndirectParamA( GetWindowLongA(hWnd,GWL_HINSTANCE), template,
289 hWnd, (DLGPROC)MSGBOX_DlgProc, (LPARAM)&mbox );
293 /**************************************************************************
294 * MessageBoxW (USER32.396)
296 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title,
297 UINT type )
299 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
300 LPSTR textA = HEAP_strdupWtoA( GetProcessHeap(), 0, text );
301 INT ret;
303 WARN("Messagebox\n");
305 ret = MessageBoxA( hwnd, textA, titleA, type );
306 HeapFree( GetProcessHeap(), 0, titleA );
307 HeapFree( GetProcessHeap(), 0, textA );
308 return ret;
312 /**************************************************************************
313 * MessageBoxExA (USER32.392)
315 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
316 UINT type, WORD langid )
318 WARN("Messagebox\n");
319 /* ignore language id for now */
320 return MessageBoxA(hWnd,text,title,type);
323 /**************************************************************************
324 * MessageBoxExW (USER32.393)
326 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
327 UINT type, WORD langid )
329 WARN("Messagebox\n");
330 /* ignore language id for now */
331 return MessageBoxW(hWnd,text,title,type);
334 /**************************************************************************
335 * MessageBoxIndirect16 (USER.827)
337 INT16 WINAPI MessageBoxIndirect16( LPMSGBOXPARAMS16 msgbox )
339 LPVOID template;
340 HRSRC hRes;
341 MSGBOXPARAMSA msgbox32;
343 WARN("Messagebox\n");
345 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
346 return 0;
347 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
348 return 0;
350 msgbox32.cbSize = msgbox->cbSize;
351 msgbox32.hwndOwner = msgbox->hwndOwner;
352 msgbox32.hInstance = msgbox->hInstance;
353 msgbox32.lpszText = PTR_SEG_TO_LIN(msgbox->lpszText);
354 msgbox32.lpszCaption = PTR_SEG_TO_LIN(msgbox->lpszCaption);
355 msgbox32.dwStyle = msgbox->dwStyle;
356 msgbox32.lpszIcon = PTR_SEG_TO_LIN(msgbox->lpszIcon);
357 msgbox32.dwContextHelpId = msgbox->dwContextHelpId;
358 msgbox32.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
359 msgbox32.dwLanguageId = msgbox->dwLanguageId;
361 return DialogBoxIndirectParamA( msgbox32.hInstance, template,
362 msgbox32.hwndOwner, (DLGPROC)MSGBOX_DlgProc,
363 (LPARAM)&msgbox32 );
366 /**************************************************************************
367 * MessageBoxIndirectA (USER32.394)
369 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
371 LPVOID template;
372 HRSRC hRes;
374 WARN("Messagebox\n");
376 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
377 return 0;
378 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
379 return 0;
381 return DialogBoxIndirectParamA( msgbox->hInstance, template,
382 msgbox->hwndOwner, (DLGPROC)MSGBOX_DlgProc,
383 (LPARAM)msgbox );
386 /**************************************************************************
387 * MessageBoxIndirectW (USER32.395)
389 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
391 MSGBOXPARAMSA msgboxa;
392 WARN("Messagebox\n");
394 memcpy(&msgboxa,msgbox,sizeof(msgboxa));
395 if (msgbox->lpszCaption)
396 lstrcpyWtoA((LPSTR)msgboxa.lpszCaption,msgbox->lpszCaption);
397 if (msgbox->lpszText)
398 lstrcpyWtoA((LPSTR)msgboxa.lpszText,msgbox->lpszText);
400 return MessageBoxIndirectA(&msgboxa);