source: trunk/src/user32/msgbox.c@ 2912

Last change on this file since 2912 was 2804, checked in by sandervl, 26 years ago

Added new logging feature

File size: 11.2 KB
Line 
1/* $Id: msgbox.c,v 1.2 2000-02-16 14:34:24 sandervl Exp $ */
2/*
3 * Message boxes (based on Wine code)
4 *
5 * Copyright 1995 Bernd Schmidt
6 *
7 *
8 */
9#include <win\winbase.h>
10#include <win\winuser.h>
11#include <string.h>
12#include <dlgs.h>
13#include <misc.h>
14#include <heapstring.h>
15
16#define DBG_LOCALLOG DBG_msgbox
17#include "dbglocal.h"
18
19#define MSGBOX_IDICON 1088
20#define MSGBOX_IDTEXT 100
21
22static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSA lpmb)
23{
24 static HFONT hFont = 0, hPrevFont = 0;
25 RECT rect;
26 HWND hItem;
27 HDC hdc;
28 int i, buttons;
29 int bspace, bw, bh, theight, tleft, wwidth, wheight, bpos;
30 int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
31 LPCSTR lpszText;
32 char buf[256];
33
34//// if (TWEAK_WineLook >= WIN95_LOOK) {
35 NONCLIENTMETRICSA nclm;
36//// INT i;
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 default:
112 SendDlgItemMessageA(hwnd, stc1, STM_SETICON,
113 (WPARAM)LoadIconA(0, IDI_HANDA), 0);
114 break;
115 }
116
117 /* Position everything */
118 GetWindowRect(hwnd, &rect);
119 borheight = rect.bottom - rect.top;
120 borwidth = rect.right - rect.left;
121 GetClientRect(hwnd, &rect);
122 borheight -= rect.bottom - rect.top;
123 borwidth -= rect.right - rect.left;
124
125 /* Get the icon height */
126 GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
127 MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
128 iheight = rect.bottom - rect.top;
129 ileft = rect.left;
130 iwidth = rect.right - ileft;
131
132 hdc = GetDC(hwnd);
133 if (hFont)
134 hPrevFont = SelectObject(hdc, hFont);
135
136 /* Get the number of visible buttons and their size */
137 bh = bw = 1; /* Minimum button sizes */
138 for (buttons = 0, i = 1; i < 8; i++)
139 {
140 hItem = GetDlgItem(hwnd, i);
141 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE)
142 {
143 char buttonText[1024];
144 int w, h;
145 buttons++;
146 if (GetWindowTextA(hItem, buttonText, sizeof buttonText))
147 {
148 DrawTextA( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
149 h = rect.bottom - rect.top;
150 w = rect.right - rect.left;
151 if (h > bh) bh = h;
152 if (w > bw) bw = w ;
153 }
154 }
155 }
156 bw = MAX(bw, bh * 2);
157 /* Button white space */
158 bh = bh * 2;
159 bw = bw * 2;
160 bspace = bw/3; /* Space between buttons */
161
162 /* Get the text size */
163 GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
164 rect.top = rect.left = rect.bottom = 0;
165 DrawTextA( hdc, lpszText, -1, &rect,
166 DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
167 /* Min text width corresponds to space for the buttons */
168 tleft = 2 * ileft + iwidth;
169 twidth = MAX((bw + bspace) * buttons + bspace - tleft, rect.right);
170 theight = rect.bottom;
171
172 if (hFont)
173 SelectObject(hdc, hPrevFont);
174 ReleaseDC(hItem, hdc);
175
176 tiheight = 16 + MAX(iheight, theight);
177 wwidth = tleft + twidth + ileft + borwidth;
178 wheight = 8 + tiheight + bh + borheight;
179
180 /* Resize the window */
181 SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
182 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
183
184 /* Position the icon */
185 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
186 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
187
188 /* Position the text */
189 SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
190 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
191
192 /* Position the buttons */
193 bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
194 for (buttons = i = 0; i < 7; i++) {
195 /* some arithmetic to get the right order for YesNoCancel windows */
196 hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
197 if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE) {
198 if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
199 SetFocus(hItem);
200 SendMessageA( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
201 }
202 SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
203 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
204 bpos += bw + bspace;
205 }
206 }
207 return hFont;
208}
209
210
211/**************************************************************************
212 * MSGBOX_DlgProc
213 *
214 * Dialog procedure for message boxes.
215 */
216static LRESULT CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
217 WPARAM wParam, LPARAM lParam )
218{
219 static HFONT hFont;
220 switch(message) {
221 case WM_INITDIALOG:
222 hFont = MSGBOX_OnInit(hwnd, (LPMSGBOXPARAMSA)lParam);
223 return 0;
224
225 case WM_COMMAND:
226 switch (wParam)
227 {
228 case IDOK:
229 case IDCANCEL:
230 case IDABORT:
231 case IDRETRY:
232 case IDIGNORE:
233 case IDYES:
234 case IDNO:
235 EndDialog(hwnd, wParam);
236 if (hFont)
237 DeleteObject(hFont);
238 break;
239 }
240
241 default:
242 /* Ok. Ignore all the other messages */
243 //TRACE("Message number %i is being ignored.\n", message);
244 break;
245 }
246 return 0;
247}
248
249/**************************************************************************
250 * MessageBox32A (USER32.391)
251 *
252 * NOTES
253 * The WARN is here to help debug erroneous MessageBoxes
254 * Use: -debugmsg warn+dialog,+relay
255 */
256INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
257{
258 LPVOID lpTemplate;
259 HRSRC hRes;
260 MSGBOXPARAMSA mbox;
261
262 dprintf(("MessageBoxA %x %s %s %x", hWnd, text, title, type));
263
264 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
265 return 0;
266 if(!(lpTemplate = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
267 return 0;
268
269 if (!text) text="<ODIN-NULL>";
270 if (!title)
271 title="Error";
272 mbox.lpszCaption = title;
273 mbox.lpszText = text;
274 mbox.dwStyle = type;
275 return DialogBoxIndirectParamA( GetWindowLongA(hWnd,GWL_HINSTANCE), lpTemplate,
276 hWnd, (DLGPROC)MSGBOX_DlgProc, (LPARAM)&mbox );
277}
278
279
280/**************************************************************************
281 * MessageBox32W (USER32.396)
282 */
283INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title,
284 UINT type )
285{
286 LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
287 LPSTR textA = HEAP_strdupWtoA( GetProcessHeap(), 0, text );
288 INT ret;
289
290 ret = MessageBoxA( hwnd, textA, titleA, type );
291 HeapFree( GetProcessHeap(), 0, titleA );
292 HeapFree( GetProcessHeap(), 0, textA );
293 return ret;
294}
295
296
297/**************************************************************************
298 * MessageBoxEx32A (USER32.392)
299 */
300INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
301 UINT type, WORD langid )
302{
303 /* ignore language id for now */
304 return MessageBoxA(hWnd,text,title,type);
305}
306
307/**************************************************************************
308 * MessageBoxEx32W (USER32.393)
309 */
310INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
311 UINT type, WORD langid )
312{
313 /* ignore language id for now */
314 return MessageBoxW(hWnd,text,title,type);
315}
316
317
318/**************************************************************************
319 * MessageBoxIndirect32A (USER32.394)
320 */
321INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
322{
323 LPVOID lpTemplate;
324 HRSRC hRes;
325
326 dprintf(("MessageBoxIndirectA %x", msgbox));
327
328 if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
329 return 0;
330 if(!(lpTemplate = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
331 return 0;
332
333 return DialogBoxIndirectParamA( msgbox->hInstance, lpTemplate,
334 msgbox->hwndOwner, (DLGPROC)MSGBOX_DlgProc,
335 (LPARAM)msgbox );
336}
337
338/**************************************************************************
339 * MessageBoxIndirect32W (USER32.395)
340 */
341INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
342{
343 MSGBOXPARAMSA msgboxa;
344
345 memcpy(&msgboxa,msgbox,sizeof(msgboxa));
346 if (msgbox->lpszCaption)
347 lstrcpyWtoA((LPSTR)msgboxa.lpszCaption,msgbox->lpszCaption);
348 if (msgbox->lpszText)
349 lstrcpyWtoA((LPSTR)msgboxa.lpszText,msgbox->lpszText);
350
351 return MessageBoxIndirectA(&msgboxa);
352}
353
354/*****************************************************************************
355 * Name : BOOL WIN32API SysErrorBox
356 * Purpose : Unknown
357 * Parameters: Unknown
358 * Variables :
359 * Result :
360 * Remark : HARDERR like ?
361 * Status : UNTESTED UNKNOWN STUB
362 *
363 * Author : Patrick Haller [Wed, 1998/06/16 11:55]
364 *****************************************************************************/
365
366BOOL WIN32API SysErrorBox(DWORD x1,
367 DWORD x2,
368 DWORD x3)
369{
370 dprintf(("USER32: SysErrorBox(%08xh,%08xh,%08xh) not implemented.\n",
371 x1,
372 x2,
373 x3));
374
375 return (FALSE); /* default */
376}
Note: See TracBrowser for help on using the repository browser.