Avoid problems during changing the Z-order if the window and the
[wine] / windows / msgbox.c
1 /*
2  * Message boxes
3  *
4  * Copyright 1995 Bernd Schmidt
5  */
6
7 #include <string.h>
8
9 #include "wine/winbase16.h"
10 #include "wine/winuser16.h"
11 #include "dlgs.h"
12 #include "heap.h"
13 #include "ldt.h"
14 #include "debugtools.h"
15 #include "debugstr.h"
16 #include "tweak.h"
17
18 DEFAULT_DEBUG_CHANNEL(dialog)
19
20 #define MSGBOX_IDICON 1088
21 #define MSGBOX_IDTEXT 100
22
23 static 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
35     if (TWEAK_WineLook >= WIN95_LOOK) {
36         NONCLIENTMETRICSA nclm;
37         INT i;
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);
46     }
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);
52     }
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 ?? */
59     }
60     SetWindowTextA(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
61
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;
96     }
97     /* Set the icon */
98     switch(lpmb->dwStyle & MB_ICONMASK) {
99     case MB_ICONEXCLAMATION:
100         SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
101                              (WPARAM16)LoadIcon16(0, IDI_EXCLAMATION16), 0);
102         break;
103     case MB_ICONQUESTION:
104         SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
105                              (WPARAM16)LoadIcon16(0, IDI_QUESTION16), 0);
106         break;
107     case MB_ICONASTERISK:
108         SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
109                              (WPARAM16)LoadIcon16(0, IDI_ASTERISK16), 0);
110         break;
111     case MB_ICONHAND:
112     default:
113         SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
114                              (WPARAM16)LoadIcon16(0, IDI_HAND16), 0);
115         break;
116     }
117     
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;
125     
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;
132     
133     hdc = GetDC(hwnd);
134     if (hFont)
135         hPrevFont = SelectObject(hdc, hFont);
136     
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++)
140     {
141         hItem = GetDlgItem(hwnd, i);
142         if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE)
143         {
144             char buttonText[1024];
145             int w, h;
146             buttons++;
147             if (GetWindowTextA(hItem, buttonText, sizeof buttonText))
148             {
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 ;
154             }
155         }
156     }
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 */
162     
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;
172     
173     if (hFont)
174         SelectObject(hdc, hPrevFont);
175     ReleaseDC(hItem, hdc);
176     
177     tiheight = 16 + MAX(iheight, theight);
178     wwidth  = tleft + twidth + ileft + borwidth;
179     wheight = 8 + tiheight + bh + borheight;
180     
181     /* Resize the window */
182     SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
183                  SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
184     
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);
188     
189     /* Position the text */
190     SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
191                  SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
192     
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 );
202             }
203             SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
204                          SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
205             bpos += bw + bspace;
206         }
207     }
208     return hFont;
209 }
210
211
212 /**************************************************************************
213  *           MSGBOX_DlgProc
214  *
215  * Dialog procedure for message boxes.
216  */
217 static LRESULT CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
218                                         WPARAM wParam, LPARAM lParam )
219 {  
220   static HFONT hFont;
221   switch(message) {
222    case WM_INITDIALOG:
223     hFont = MSGBOX_OnInit(hwnd, (LPMSGBOXPARAMSA)lParam);
224     return 0;
225     
226    case WM_COMMAND:
227     switch (wParam)
228     {
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;
240     }
241
242    default:
243      /* Ok. Ignore all the other messages */
244      TRACE("Message number %i is being ignored.\n", message);
245     break;
246   }
247   return 0;
248 }
249
250
251 /**************************************************************************
252  *           MessageBox16   (USER.1)
253  */
254 INT16 WINAPI MessageBox16( HWND16 hwnd, LPCSTR text, LPCSTR title, UINT16 type)
255 {
256     WARN("Messagebox\n");
257     return MessageBoxA( hwnd, text, title, type );
258 }
259
260
261 /**************************************************************************
262  *           MessageBox32A   (USER32.391)
263  *
264  * NOTES
265  *   The WARN is here to help debug erroneous MessageBoxes
266  *   Use: -debugmsg warn+dialog,+relay
267  */
268 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
269 {
270     LPVOID template;
271     HRSRC hRes;
272     MSGBOXPARAMSA mbox;
273
274     WARN("Messagebox\n");
275
276     if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
277         return 0;
278     if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
279         return 0;
280
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 );
289 }
290
291
292 /**************************************************************************
293  *           MessageBox32W   (USER32.396)
294  */
295 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title,
296                             UINT type )
297 {
298     LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
299     LPSTR textA  = HEAP_strdupWtoA( GetProcessHeap(), 0, text );
300     INT ret;
301     
302     WARN("Messagebox\n");
303
304     ret = MessageBoxA( hwnd, textA, titleA, type );
305     HeapFree( GetProcessHeap(), 0, titleA );
306     HeapFree( GetProcessHeap(), 0, textA );
307     return ret;
308 }
309
310
311 /**************************************************************************
312  *           MessageBoxEx32A   (USER32.392)
313  */
314 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
315                               UINT type, WORD langid )
316 {
317     WARN("Messagebox\n");
318     /* ignore language id for now */
319     return MessageBoxA(hWnd,text,title,type);
320 }
321
322 /**************************************************************************
323  *           MessageBoxEx32W   (USER32.393)
324  */
325 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
326                               UINT type, WORD langid )
327 {
328     WARN("Messagebox\n");
329     /* ignore language id for now */
330     return MessageBoxW(hWnd,text,title,type);
331 }
332
333 /**************************************************************************
334  *           MessageBoxIndirect16   (USER.827)
335  */
336 INT16 WINAPI MessageBoxIndirect16( LPMSGBOXPARAMS16 msgbox )
337 {
338     LPVOID template;
339     HRSRC hRes;
340     MSGBOXPARAMSA msgbox32;
341
342     WARN("Messagebox\n");    
343     
344     if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
345         return 0;
346     if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
347         return 0;
348
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;
359
360     return DialogBoxIndirectParamA( msgbox32.hInstance, template,
361                                       msgbox32.hwndOwner, (DLGPROC)MSGBOX_DlgProc,
362                                       (LPARAM)&msgbox32 );
363 }
364
365 /**************************************************************************
366  *           MessageBoxIndirect32A   (USER32.394)
367  */
368 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
369 {
370     LPVOID template;
371     HRSRC hRes;
372
373     WARN("Messagebox\n");
374
375     if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
376         return 0;
377     if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
378         return 0;
379
380     return DialogBoxIndirectParamA( msgbox->hInstance, template,
381                                       msgbox->hwndOwner, (DLGPROC)MSGBOX_DlgProc,
382                                       (LPARAM)msgbox );
383 }
384
385 /**************************************************************************
386  *           MessageBoxIndirect32W   (USER32.395)
387  */
388 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
389 {
390     MSGBOXPARAMSA       msgboxa;
391     WARN("Messagebox\n");
392
393     memcpy(&msgboxa,msgbox,sizeof(msgboxa));
394     if (msgbox->lpszCaption)    
395       lstrcpyWtoA((LPSTR)msgboxa.lpszCaption,msgbox->lpszCaption);
396     if (msgbox->lpszText)       
397       lstrcpyWtoA((LPSTR)msgboxa.lpszText,msgbox->lpszText);
398
399     return MessageBoxIndirectA(&msgboxa);
400 }
401
402
403 /**************************************************************************
404  *           FatalAppExit16   (KERNEL.137)
405  */
406 void WINAPI FatalAppExit16( UINT16 action, LPCSTR str )
407 {
408     WARN("AppExit\n");
409     FatalAppExitA( action, str );
410 }
411
412
413 /**************************************************************************
414  *           FatalAppExit32A   (KERNEL32.108)
415  */
416 void WINAPI FatalAppExitA( UINT action, LPCSTR str )
417 {
418     WARN("AppExit\n");
419     MessageBoxA( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
420     ExitProcess(0);
421 }
422
423
424 /**************************************************************************
425  *           FatalAppExit32W   (KERNEL32.109)
426  */
427 void WINAPI FatalAppExitW( UINT action, LPCWSTR str )
428 {
429     WARN("AppExit\n");
430     MessageBoxW( 0, str, NULL, MB_SYSTEMMODAL | MB_OK );
431     ExitProcess(0);
432 }
433
434