source: trunk/src/user32/msgbox.c

Last change on this file was 21916, checked in by dmik, 14 years ago

Merge branch gcc-kmk to trunk.

File size: 13.6 KB
Line 
1/* $Id: msgbox.c,v 1.8 2003-07-31 15:56:43 sandervl Exp $ */
2/*
3 * Message boxes (based on Wine code)
4 *
5 * Copyright 1995 Bernd Schmidt
6 *
7 * Corel WINE version: 20000317
8 *
9 */
10#include <win/winbase.h>
11#include <win/winuser.h>
12#include <string.h>
13#include <dlgs.h>
14#include <misc.h>
15#include <heapstring.h>
16
17#define DBG_LOCALLOG DBG_msgbox
18#include "dbglocal.h"
19
20#define MSGBOX_IDICON 1088
21#define MSGBOX_IDTEXT 100
22
23static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSA lpmb)
24{
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];
34 NONCLIENTMETRICSA nclm;
35 BOOL hasIcon = TRUE;
36
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);
45
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);
51 }
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 ?? */
58 }
59 SetWindowTextA(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
60
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;
95 }
96 /* Set the icon */
97 switch(lpmb->dwStyle & MB_ICONMASK) {
98 case MB_ICONEXCLAMATION:
99 SendDlgItemMessageA(hwnd, stc1, STM_SETICON,
100 (WPARAM)LoadIconA(0, IDI_EXCLAMATIONA), 0);
101 break;
102 case MB_ICONQUESTION:
103 SendDlgItemMessageA(hwnd, stc1, STM_SETICON,
104 (WPARAM)LoadIconA(0, IDI_QUESTIONA), 0);
105 break;
106 case MB_ICONASTERISK:
107 SendDlgItemMessageA(hwnd, stc1, STM_SETICON,
108 (WPARAM)LoadIconA(0, IDI_ASTERISKA), 0);
109 break;
110 case MB_ICONHAND:
111 SendDlgItemMessageA(hwnd, stc1, STM_SETICON,
112 (WPARAM)LoadIconA(0, IDI_HANDA), 0);
113 break;
114 default:
115 /* By default, Windows 95/98/NT do not associate an icon to message boxes.
116 * So wine should do the same.
117 */
118 hasIcon = FALSE;
119 ShowWindow(GetDlgItem(hwnd,stc1),SW_HIDE);
120 break;
121 }
122
123 /* Position everything */
124 GetWindowRect(hwnd, &rect);
125 borheight = rect.bottom - rect.top;
126 borwidth = rect.right - rect.left;
127 GetClientRect(hwnd, &rect);
128 borheight -= rect.bottom - rect.top;
129 borwidth -= rect.right - rect.left;
130
131 /* Get the icon height */
132 if (hasIcon)
133 {
134 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
135 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
136 iheight = rect.bottom - rect.top;
137 ileft = rect.left;
138 iwidth = rect.right - ileft;
139 } else
140 {
141 iheight = 0;
142 ileft = 0;
143 iwidth = 0;
144 }
145
146 hdc = GetDC(hwnd);
147 if (hFont)
148 hPrevFont = SelectObject(hdc, hFont);
149
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++)
153 {
154 hItem = GetDlgItem(hwnd, i);
155 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE)
156 {
157 char buttonText[1024];
158 int w, h;
159 buttons++;
160 if (GetWindowTextA(hItem, buttonText, sizeof buttonText))
161 {
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 ;
167 }
168 }
169 }
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 */
175
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;
185
186 if (hFont)
187 SelectObject(hdc, hPrevFont);
188#ifdef __WIN32OS2__
189 ReleaseDC(hwnd, hdc);
190#else
191 ReleaseDC(hItem, hdc);
192#endif
193
194 tiheight = 16 + MAX(iheight, theight);
195 wwidth = tleft + twidth + ileft + borwidth;
196 wheight = 8 + tiheight + bh + borheight;
197
198 /* Resize the window */
199 SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
200 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
201
202 /* Position the icon */
203 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
204 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
205
206 /* Position the text */
207 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
208 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
209
210 /* Position the buttons */
211 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
212 for (buttons = i = 0; i < 7; i++) {
213 /* some arithmetic to get the right order for YesNoCancel windows */
214 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
215 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE) {
216 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
217 SetFocus(hItem);
218 SendMessageA( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
219 }
220 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
221 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
222 bpos += bw + bspace;
223 }
224 }
225 return hFont;
226}
227
228
229/**************************************************************************
230 * MSGBOX_DlgProc
231 *
232 * Dialog procedure for message boxes.
233 */
234static LRESULT CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
235 WPARAM wParam, LPARAM lParam )
236{
237 static HFONT hFont;
238 switch(message) {
239 case WM_INITDIALOG:
240 hFont = MSGBOX_OnInit(hwnd, (LPMSGBOXPARAMSA)lParam);
241 return 0;
242
243 case WM_COMMAND:
244 switch (wParam)
245 {
246 case IDOK:
247 case IDCANCEL:
248 case IDABORT:
249 case IDRETRY:
250 case IDIGNORE:
251 case IDYES:
252 case IDNO:
253 EndDialog(hwnd, wParam);
254 if (hFont)
255 DeleteObject(hFont);
256 break;
257 }
258
259 default:
260 /* Ok. Ignore all the other messages */
261 //TRACE("Message number %i is being ignored.\n", message);
262 break;
263 }
264 return 0;
265}
266
267/**************************************************************************
268 * MessageBox32A (USER32.391)
269 *
270 * NOTES
271 * The WARN is here to help debug erroneous MessageBoxes
272 * Use: -debugmsg warn+dialog,+relay
273 */
274INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
275{
276 LPVOID lpTemplate;
277 HRSRC hRes;
278 MSGBOXPARAMSA mbox;
279
280 dprintf(("MessageBoxA %x %s %s %x", hWnd, text, title, type));
281
282 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
283 return 0;
284 if(!(lpTemplate = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
285 return 0;
286
287 if (!text) text="<ODIN-NULL>";
288 if (!title)
289 title="Error";
290 mbox.lpszCaption = title;
291 mbox.lpszText = text;
292 mbox.dwStyle = type;
293 return DialogBoxIndirectParamA( GetWindowLongA(hWnd,GWL_HINSTANCE), lpTemplate,
294 hWnd, (DLGPROC)MSGBOX_DlgProc, (LPARAM)&mbox );
295}
296
297
298/**************************************************************************
299 * MessageBox32W (USER32.396)
300 */
301INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title,
302 UINT type )
303{
304 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
305 LPSTR textA = HEAP_strdupWtoA( GetProcessHeap(), 0, text );
306 INT ret;
307
308 ret = MessageBoxA( hwnd, textA, titleA, type );
309 HeapFree( GetProcessHeap(), 0, titleA );
310 HeapFree( GetProcessHeap(), 0, textA );
311 return ret;
312}
313
314
315/**************************************************************************
316 * MessageBoxEx32A (USER32.392)
317 */
318INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
319 UINT type, WORD langid )
320{
321 /* ignore language id for now */
322 return MessageBoxA(hWnd,text,title,type);
323}
324
325/**************************************************************************
326 * MessageBoxEx32W (USER32.393)
327 */
328INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
329 UINT type, WORD langid )
330{
331 /* ignore language id for now */
332 return MessageBoxW(hWnd,text,title,type);
333}
334
335
336/**************************************************************************
337 * MessageBoxIndirect32A (USER32.394)
338 */
339INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
340{
341 LPVOID lpTemplate;
342 HRSRC hRes;
343
344 dprintf(("MessageBoxIndirectA %x", msgbox));
345
346 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
347 return 0;
348 if(!(lpTemplate = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
349 return 0;
350
351 return DialogBoxIndirectParamA( msgbox->hInstance, lpTemplate,
352 msgbox->hwndOwner, (DLGPROC)MSGBOX_DlgProc,
353 (LPARAM)msgbox );
354}
355
356/**************************************************************************
357 * MessageBoxIndirect32W (USER32.395)
358 */
359INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
360{
361 MSGBOXPARAMSA msgboxa;
362#ifdef __WIN32OS2__
363 int ret;
364#endif
365
366 memcpy(&msgboxa,msgbox,sizeof(msgboxa));
367#ifdef __WIN32OS2__
368 if (msgbox->lpszCaption)
369 msgboxa.lpszCaption = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszCaption );
370 if (msgbox->lpszText)
371 msgboxa.lpszText = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszText );
372 if( msgbox->lpszIcon && HIWORD( msgbox->lpszIcon ))
373 msgboxa.lpszIcon = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszIcon );
374
375 ret = MessageBoxIndirectA(&msgboxa);
376
377 if (msgbox->lpszCaption)
378 HeapFree( GetProcessHeap(), 0, ( LPVOID )msgboxa.lpszCaption );
379 if (msgbox->lpszText)
380 HeapFree( GetProcessHeap(), 0, ( LPVOID )msgboxa.lpszText );
381 if( msgbox->lpszIcon && HIWORD( msgbox->lpszIcon ))
382 HeapFree( GetProcessHeap(), 0, ( LPVOID )msgboxa.lpszIcon );
383
384 return ret;
385#else
386 if (msgbox->lpszCaption)
387 lstrcpyWtoA((LPSTR)msgboxa.lpszCaption,msgbox->lpszCaption);
388 if (msgbox->lpszText)
389 lstrcpyWtoA((LPSTR)msgboxa.lpszText,msgbox->lpszText);
390
391 return MessageBoxIndirectA(&msgboxa);
392#endif
393
394}
395
396#ifndef __WIN32OS2__
397/**************************************************************************
398 * FatalAppExit32A (KERNEL32.108)
399 */
400void WINAPI FatalAppExitA( UINT action, LPCSTR str )
401{
402 //WARN("AppExit\n");
403 MessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
404 ExitProcess(0);
405}
406
407
408/**************************************************************************
409 * FatalAppExit32W (KERNEL32.109)
410 */
411void WINAPI FatalAppExitW( UINT action, LPCWSTR str )
412{
413 //WARN("AppExit\n");
414 MessageBoxW( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
415 ExitProcess(0);
416}
417#endif
418
419/*****************************************************************************
420 * Name : BOOL WIN32API SysErrorBox
421 * Purpose : Unknown
422 * Parameters: Unknown
423 * Variables :
424 * Result :
425 * Remark : HARDERR like ?
426 * Status : UNTESTED UNKNOWN STUB
427 *
428 * Author : Patrick Haller [Wed, 1998/06/16 11:55]
429 *****************************************************************************/
430
431BOOL WIN32API SysErrorBox(DWORD x1,
432 DWORD x2,
433 DWORD x3)
434{
435 dprintf(("USER32: SysErrorBox(%08xh,%08xh,%08xh) not implemented.\n",
436 x1,
437 x2,
438 x3));
439
440 return (FALSE); /* default */
441}
Note: See TracBrowser for help on using the repository browser.