Portability fix.
[wine/wine64.git] / windows / msgbox.c
blob7db5fc367b16cba21b20c6747bde4a400c0f1f20
1 /*
2 * Message boxes
4 * Copyright 1995 Bernd Schmidt
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <string.h>
23 #include "windef.h"
24 #include "wingdi.h"
25 #include "wine/winbase16.h"
26 #include "wine/winuser16.h"
27 #include "dlgs.h"
28 #include "heap.h"
29 #include "user.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
34 #define MSGBOX_IDICON 1088
35 #define MSGBOX_IDTEXT 100
37 static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSA lpmb)
39 static HFONT hFont = 0, hPrevFont = 0;
40 RECT rect;
41 HWND hItem;
42 HDC hdc;
43 int i, buttons;
44 int bspace, bw, bh, theight, tleft, wwidth, wheight, bpos;
45 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
46 LPCSTR lpszText;
47 char buf[256];
49 if (TWEAK_WineLook >= WIN95_LOOK) {
50 NONCLIENTMETRICSA nclm;
51 nclm.cbSize = sizeof(NONCLIENTMETRICSA);
52 SystemParametersInfoA (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
53 hFont = CreateFontIndirectA (&nclm.lfMessageFont);
54 /* set button font */
55 for (i=1; i < 8; i++)
56 SendDlgItemMessageA (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
57 /* set text font */
58 SendDlgItemMessageA (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
60 if (HIWORD(lpmb->lpszCaption)) {
61 SetWindowTextA(hwnd, lpmb->lpszCaption);
62 } else {
63 if (LoadStringA(lpmb->hInstance, LOWORD(lpmb->lpszCaption), buf, sizeof(buf)))
64 SetWindowTextA(hwnd, buf);
66 if (HIWORD(lpmb->lpszText)) {
67 lpszText = lpmb->lpszText;
68 } else {
69 lpszText = buf;
70 if (!LoadStringA(lpmb->hInstance, LOWORD(lpmb->lpszText), buf, sizeof(buf)))
71 *buf = 0; /* FIXME ?? */
73 SetWindowTextA(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
75 /* Hide not selected buttons */
76 switch(lpmb->dwStyle & MB_TYPEMASK) {
77 case MB_OK:
78 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
79 /* fall through */
80 case MB_OKCANCEL:
81 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
82 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
83 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
84 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
85 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
86 break;
87 case MB_ABORTRETRYIGNORE:
88 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
89 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
90 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
91 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
92 break;
93 case MB_YESNO:
94 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
95 /* fall through */
96 case MB_YESNOCANCEL:
97 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
98 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
99 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
100 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
101 break;
102 case MB_RETRYCANCEL:
103 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
104 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
105 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
106 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
107 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
108 break;
110 /* Set the icon */
111 switch(lpmb->dwStyle & MB_ICONMASK) {
112 case MB_ICONEXCLAMATION:
113 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_EXCLAMATIONA), 0);
114 break;
115 case MB_ICONQUESTION:
116 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_QUESTIONA), 0);
117 break;
118 case MB_ICONASTERISK:
119 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_ASTERISKA), 0);
120 break;
121 case MB_ICONHAND:
122 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_HANDA), 0);
123 break;
124 default:
125 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
126 * So wine should do the same.
128 break;
131 /* Position everything */
132 GetWindowRect(hwnd, &rect);
133 borheight = rect.bottom - rect.top;
134 borwidth = rect.right - rect.left;
135 GetClientRect(hwnd, &rect);
136 borheight -= rect.bottom - rect.top;
137 borwidth -= rect.right - rect.left;
139 /* Get the icon height */
140 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
141 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
142 iheight = rect.bottom - rect.top;
143 ileft = rect.left;
144 iwidth = rect.right - ileft;
146 hdc = GetDC(hwnd);
147 if (hFont)
148 hPrevFont = SelectObject(hdc, hFont);
150 /* Get the number of visible buttons and their size */
151 bh = bw = 1; /* Minimum button sizes */
152 for (buttons = 0, i = 1; i < 8; i++)
154 hItem = GetDlgItem(hwnd, i);
155 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE)
157 char buttonText[1024];
158 int w, h;
159 buttons++;
160 if (GetWindowTextA(hItem, buttonText, sizeof buttonText))
162 DrawTextA( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
163 h = rect.bottom - rect.top;
164 w = rect.right - rect.left;
165 if (h > bh) bh = h;
166 if (w > bw) bw = w ;
170 bw = max(bw, bh * 2);
171 /* Button white space */
172 bh = bh * 2;
173 bw = bw * 2;
174 bspace = bw/3; /* Space between buttons */
176 /* Get the text size */
177 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
178 rect.top = rect.left = rect.bottom = 0;
179 DrawTextA( hdc, lpszText, -1, &rect,
180 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
181 /* Min text width corresponds to space for the buttons */
182 tleft = 2 * ileft + iwidth;
183 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
184 theight = rect.bottom;
186 if (hFont)
187 SelectObject(hdc, hPrevFont);
188 ReleaseDC(hItem, hdc);
190 tiheight = 16 + max(iheight, theight);
191 wwidth = tleft + twidth + ileft + borwidth;
192 wheight = 8 + tiheight + bh + borheight;
194 /* Resize the window */
195 SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
196 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
198 /* Position the icon */
199 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
200 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
202 /* Position the text */
203 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
204 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
206 /* Position the buttons */
207 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
208 for (buttons = i = 0; i < 7; i++) {
209 /* some arithmetic to get the right order for YesNoCancel windows */
210 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
211 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE) {
212 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
213 SetFocus(hItem);
214 SendMessageA( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
216 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
217 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
218 bpos += bw + bspace;
222 /* handle modal MessageBoxes */
223 if (lpmb->dwStyle & (MB_TASKMODAL|MB_SYSTEMMODAL))
225 FIXME("%s modal msgbox ! Not modal yet.\n",
226 lpmb->dwStyle & MB_TASKMODAL ? "task" : "system");
227 /* Probably do EnumTaskWindows etc. here for TASKMODAL
228 * and work your way up to the top - I'm lazy (HWND_TOP) */
229 SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
230 SWP_NOSIZE | SWP_NOMOVE);
231 if (lpmb->dwStyle & MB_TASKMODAL)
232 /* at least MB_TASKMODAL seems to imply a ShowWindow */
233 ShowWindow(hwnd, SW_SHOW);
235 if (lpmb->dwStyle & MB_APPLMODAL)
236 FIXME("app modal msgbox ! Not modal yet.\n");
238 return hFont;
242 /**************************************************************************
243 * MSGBOX_DlgProc
245 * Dialog procedure for message boxes.
247 static LRESULT CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
248 WPARAM wParam, LPARAM lParam )
250 static HFONT hFont;
251 switch(message) {
252 case WM_INITDIALOG:
253 hFont = MSGBOX_OnInit(hwnd, (LPMSGBOXPARAMSA)lParam);
254 return 0;
256 case WM_COMMAND:
257 switch (wParam)
259 case IDOK:
260 case IDCANCEL:
261 case IDABORT:
262 case IDRETRY:
263 case IDIGNORE:
264 case IDYES:
265 case IDNO:
266 EndDialog(hwnd, wParam);
267 if (hFont)
268 DeleteObject(hFont);
269 break;
272 default:
273 /* Ok. Ignore all the other messages */
274 TRACE("Message number 0x%04x is being ignored.\n", message);
275 break;
277 return 0;
281 /**************************************************************************
282 * MessageBoxA (USER32.@)
284 * NOTES
285 * The WARN is here to help debug erroneous MessageBoxes
286 * Use: -debugmsg warn+dialog,+relay
288 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
290 LPVOID template;
291 HRSRC hRes;
292 MSGBOXPARAMSA mbox;
294 WARN("Messagebox\n");
296 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
297 return 0;
298 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
299 return 0;
301 if (!text) text="<WINE-NULL>";
302 if (!title)
303 title="Error";
304 mbox.lpszCaption = title;
305 mbox.lpszText = text;
306 mbox.dwStyle = type;
307 return DialogBoxIndirectParamA( GetWindowLongA(hWnd,GWL_HINSTANCE), template,
308 hWnd, (DLGPROC)MSGBOX_DlgProc, (LPARAM)&mbox );
312 /**************************************************************************
313 * MessageBoxW (USER32.@)
315 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title,
316 UINT type )
318 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
319 LPSTR textA = HEAP_strdupWtoA( GetProcessHeap(), 0, text );
320 INT ret;
322 WARN("Messagebox\n");
324 ret = MessageBoxA( hwnd, textA, titleA, type );
325 HeapFree( GetProcessHeap(), 0, titleA );
326 HeapFree( GetProcessHeap(), 0, textA );
327 return ret;
331 /**************************************************************************
332 * MessageBoxExA (USER32.@)
334 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
335 UINT type, WORD langid )
337 WARN("Messagebox\n");
338 /* ignore language id for now */
339 return MessageBoxA(hWnd,text,title,type);
342 /**************************************************************************
343 * MessageBoxExW (USER32.@)
345 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
346 UINT type, WORD langid )
348 WARN("Messagebox\n");
349 /* ignore language id for now */
350 return MessageBoxW(hWnd,text,title,type);
353 /**************************************************************************
354 * MessageBoxIndirectA (USER32.@)
356 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
358 LPVOID template;
359 HRSRC hRes;
361 WARN("Messagebox\n");
363 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
364 return 0;
365 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
366 return 0;
368 return DialogBoxIndirectParamA( msgbox->hInstance, template,
369 msgbox->hwndOwner, (DLGPROC)MSGBOX_DlgProc,
370 (LPARAM)msgbox );
373 /**************************************************************************
374 * MessageBoxIndirectW (USER32.@)
376 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
378 MSGBOXPARAMSA msgboxa;
379 memcpy(&msgboxa,msgbox,sizeof(msgboxa));
380 msgboxa.lpszCaption = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszCaption );
381 msgboxa.lpszText = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszText );
382 msgboxa.lpszIcon = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszIcon );
383 return MessageBoxIndirectA(&msgboxa);