Display an error message if loading a Winelib app failed (suggested by
[wine/multimedia.git] / windows / msgbox.c
blob2266a160e46d9144979e1ddbfa246e3eb5e0d098
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 nclm.cbSize = sizeof(NONCLIENTMETRICSA);
39 SystemParametersInfoA (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
40 hFont = CreateFontIndirectA (&nclm.lfMessageFont);
41 /* set button font */
42 for (i=1; i < 8; i++)
43 SendDlgItemMessageA (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
44 /* set text font */
45 SendDlgItemMessageA (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
47 if (HIWORD(lpmb->lpszCaption)) {
48 SetWindowTextA(hwnd, lpmb->lpszCaption);
49 } else {
50 if (LoadStringA(lpmb->hInstance, LOWORD(lpmb->lpszCaption), buf, sizeof(buf)))
51 SetWindowTextA(hwnd, buf);
53 if (HIWORD(lpmb->lpszText)) {
54 lpszText = lpmb->lpszText;
55 } else {
56 lpszText = buf;
57 if (!LoadStringA(lpmb->hInstance, LOWORD(lpmb->lpszText), buf, sizeof(buf)))
58 *buf = 0; /* FIXME ?? */
60 SetWindowTextA(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
62 /* Hide not selected buttons */
63 switch(lpmb->dwStyle & MB_TYPEMASK) {
64 case MB_OK:
65 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
66 /* fall through */
67 case MB_OKCANCEL:
68 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
69 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
70 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
71 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
72 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
73 break;
74 case MB_ABORTRETRYIGNORE:
75 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
76 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
77 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
78 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
79 break;
80 case MB_YESNO:
81 ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
82 /* fall through */
83 case MB_YESNOCANCEL:
84 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
85 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
86 ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
87 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
88 break;
89 case MB_RETRYCANCEL:
90 ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
91 ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
92 ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
93 ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
94 ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
95 break;
97 /* Set the icon */
98 switch(lpmb->dwStyle & MB_ICONMASK) {
99 case MB_ICONEXCLAMATION:
100 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_EXCLAMATIONA), 0);
101 break;
102 case MB_ICONQUESTION:
103 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_QUESTIONA), 0);
104 break;
105 case MB_ICONASTERISK:
106 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_ASTERISKA), 0);
107 break;
108 case MB_ICONHAND:
109 SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_HANDA), 0);
110 break;
111 default:
112 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
113 * So wine should do the same.
115 break;
118 /* Position everything */
119 GetWindowRect(hwnd, &rect);
120 borheight = rect.bottom - rect.top;
121 borwidth = rect.right - rect.left;
122 GetClientRect(hwnd, &rect);
123 borheight -= rect.bottom - rect.top;
124 borwidth -= rect.right - rect.left;
126 /* Get the icon height */
127 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
128 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
129 iheight = rect.bottom - rect.top;
130 ileft = rect.left;
131 iwidth = rect.right - ileft;
133 hdc = GetDC(hwnd);
134 if (hFont)
135 hPrevFont = SelectObject(hdc, hFont);
137 /* Get the number of visible buttons and their size */
138 bh = bw = 1; /* Minimum button sizes */
139 for (buttons = 0, i = 1; i < 8; i++)
141 hItem = GetDlgItem(hwnd, i);
142 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE)
144 char buttonText[1024];
145 int w, h;
146 buttons++;
147 if (GetWindowTextA(hItem, buttonText, sizeof buttonText))
149 DrawTextA( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
150 h = rect.bottom - rect.top;
151 w = rect.right - rect.left;
152 if (h > bh) bh = h;
153 if (w > bw) bw = w ;
157 bw = max(bw, bh * 2);
158 /* Button white space */
159 bh = bh * 2;
160 bw = bw * 2;
161 bspace = bw/3; /* Space between buttons */
163 /* Get the text size */
164 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
165 rect.top = rect.left = rect.bottom = 0;
166 DrawTextA( hdc, lpszText, -1, &rect,
167 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
168 /* Min text width corresponds to space for the buttons */
169 tleft = 2 * ileft + iwidth;
170 twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
171 theight = rect.bottom;
173 if (hFont)
174 SelectObject(hdc, hPrevFont);
175 ReleaseDC(hItem, hdc);
177 tiheight = 16 + max(iheight, theight);
178 wwidth = tleft + twidth + ileft + borwidth;
179 wheight = 8 + tiheight + bh + borheight;
181 /* Resize the window */
182 SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
183 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
185 /* Position the icon */
186 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
187 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
189 /* Position the text */
190 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
191 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
193 /* Position the buttons */
194 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
195 for (buttons = i = 0; i < 7; i++) {
196 /* some arithmetic to get the right order for YesNoCancel windows */
197 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
198 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE) {
199 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
200 SetFocus(hItem);
201 SendMessageA( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
203 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
204 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
205 bpos += bw + bspace;
208 return hFont;
212 /**************************************************************************
213 * MSGBOX_DlgProc
215 * Dialog procedure for message boxes.
217 static LRESULT CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
218 WPARAM wParam, LPARAM lParam )
220 static HFONT hFont;
221 switch(message) {
222 case WM_INITDIALOG:
223 hFont = MSGBOX_OnInit(hwnd, (LPMSGBOXPARAMSA)lParam);
224 return 0;
226 case WM_COMMAND:
227 switch (wParam)
229 case IDOK:
230 case IDCANCEL:
231 case IDABORT:
232 case IDRETRY:
233 case IDIGNORE:
234 case IDYES:
235 case IDNO:
236 EndDialog(hwnd, wParam);
237 if (hFont)
238 DeleteObject(hFont);
239 break;
242 default:
243 /* Ok. Ignore all the other messages */
244 TRACE("Message number %i is being ignored.\n", message);
245 break;
247 return 0;
251 /**************************************************************************
252 * MessageBox16 (USER.1)
254 INT16 WINAPI MessageBox16( HWND16 hwnd, LPCSTR text, LPCSTR title, UINT16 type)
256 WARN("Messagebox\n");
257 return MessageBoxA( hwnd, text, title, type );
261 /**************************************************************************
262 * MessageBoxA (USER32.391)
264 * NOTES
265 * The WARN is here to help debug erroneous MessageBoxes
266 * Use: -debugmsg warn+dialog,+relay
268 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
270 LPVOID template;
271 HRSRC hRes;
272 MSGBOXPARAMSA mbox;
274 WARN("Messagebox\n");
276 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
277 return 0;
278 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
279 return 0;
281 if (!text) text="<WINE-NULL>";
282 if (!title)
283 title="Error";
284 mbox.lpszCaption = title;
285 mbox.lpszText = text;
286 mbox.dwStyle = type;
287 return DialogBoxIndirectParamA( GetWindowLongA(hWnd,GWL_HINSTANCE), template,
288 hWnd, (DLGPROC)MSGBOX_DlgProc, (LPARAM)&mbox );
292 /**************************************************************************
293 * MessageBoxW (USER32.396)
295 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title,
296 UINT type )
298 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
299 LPSTR textA = HEAP_strdupWtoA( GetProcessHeap(), 0, text );
300 INT ret;
302 WARN("Messagebox\n");
304 ret = MessageBoxA( hwnd, textA, titleA, type );
305 HeapFree( GetProcessHeap(), 0, titleA );
306 HeapFree( GetProcessHeap(), 0, textA );
307 return ret;
311 /**************************************************************************
312 * MessageBoxExA (USER32.392)
314 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
315 UINT type, WORD langid )
317 WARN("Messagebox\n");
318 /* ignore language id for now */
319 return MessageBoxA(hWnd,text,title,type);
322 /**************************************************************************
323 * MessageBoxExW (USER32.393)
325 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
326 UINT type, WORD langid )
328 WARN("Messagebox\n");
329 /* ignore language id for now */
330 return MessageBoxW(hWnd,text,title,type);
333 /**************************************************************************
334 * MessageBoxIndirect16 (USER.827)
336 INT16 WINAPI MessageBoxIndirect16( LPMSGBOXPARAMS16 msgbox )
338 LPVOID template;
339 HRSRC hRes;
340 MSGBOXPARAMSA msgbox32;
342 WARN("Messagebox\n");
344 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
345 return 0;
346 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
347 return 0;
349 msgbox32.cbSize = msgbox->cbSize;
350 msgbox32.hwndOwner = msgbox->hwndOwner;
351 msgbox32.hInstance = msgbox->hInstance;
352 msgbox32.lpszText = PTR_SEG_TO_LIN(msgbox->lpszText);
353 msgbox32.lpszCaption = PTR_SEG_TO_LIN(msgbox->lpszCaption);
354 msgbox32.dwStyle = msgbox->dwStyle;
355 msgbox32.lpszIcon = PTR_SEG_TO_LIN(msgbox->lpszIcon);
356 msgbox32.dwContextHelpId = msgbox->dwContextHelpId;
357 msgbox32.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
358 msgbox32.dwLanguageId = msgbox->dwLanguageId;
360 return DialogBoxIndirectParamA( msgbox32.hInstance, template,
361 msgbox32.hwndOwner, (DLGPROC)MSGBOX_DlgProc,
362 (LPARAM)&msgbox32 );
365 /**************************************************************************
366 * MessageBoxIndirectA (USER32.394)
368 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
370 LPVOID template;
371 HRSRC hRes;
373 WARN("Messagebox\n");
375 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
376 return 0;
377 if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
378 return 0;
380 return DialogBoxIndirectParamA( msgbox->hInstance, template,
381 msgbox->hwndOwner, (DLGPROC)MSGBOX_DlgProc,
382 (LPARAM)msgbox );
385 /**************************************************************************
386 * MessageBoxIndirectW (USER32.395)
388 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
390 MSGBOXPARAMSA msgboxa;
391 memcpy(&msgboxa,msgbox,sizeof(msgboxa));
392 msgboxa.lpszCaption = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszCaption );
393 msgboxa.lpszText = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszText );
394 msgboxa.lpszIcon = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszIcon );
395 return MessageBoxIndirectA(&msgboxa);