Removed local variable shadows warnings.
[wine] / windows / msgbox.c
1 /*
2  * Message boxes
3  *
4  * Copyright 1995 Bernd Schmidt
5  */
6
7 #include <string.h>
8
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"
18
19 DEFAULT_DEBUG_CHANNEL(dialog)
20
21 #define MSGBOX_IDICON 1088
22 #define MSGBOX_IDTEXT 100
23
24 static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSA lpmb)
25 {
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];
35
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);
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       SendDlgItemMessage16(hwnd, stc1, STM_SETICON16,
113                              (WPARAM16)LoadIcon16(0, IDI_HAND16), 0);
114       break;
115     default:
116         /* By default, Windows 95/98/NT do not associate an icon to message boxes.
117          * So wine should do the same.
118          */
119         break;
120     }
121     
122     /* Position everything */
123     GetWindowRect(hwnd, &rect);
124     borheight = rect.bottom - rect.top;
125     borwidth  = rect.right - rect.left;
126     GetClientRect(hwnd, &rect);
127     borheight -= rect.bottom - rect.top;
128     borwidth  -= rect.right - rect.left;
129     
130     /* Get the icon height */
131     GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
132     MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
133     iheight = rect.bottom - rect.top;
134     ileft = rect.left;
135     iwidth = rect.right - ileft;
136     
137     hdc = GetDC(hwnd);
138     if (hFont)
139         hPrevFont = SelectObject(hdc, hFont);
140     
141     /* Get the number of visible buttons and their size */
142     bh = bw = 1; /* Minimum button sizes */
143     for (buttons = 0, i = 1; i < 8; i++)
144     {
145         hItem = GetDlgItem(hwnd, i);
146         if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE)
147         {
148             char buttonText[1024];
149             int w, h;
150             buttons++;
151             if (GetWindowTextA(hItem, buttonText, sizeof buttonText))
152             {
153                 DrawTextA( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
154                 h = rect.bottom - rect.top;
155                 w = rect.right - rect.left;
156                 if (h > bh) bh = h;
157                 if (w > bw)  bw = w ;
158             }
159         }
160     }
161     bw = max(bw, bh * 2);
162     /* Button white space */
163     bh = bh * 2;
164     bw = bw * 2;
165     bspace = bw/3; /* Space between buttons */
166     
167     /* Get the text size */
168     GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
169     rect.top = rect.left = rect.bottom = 0;
170     DrawTextA( hdc, lpszText, -1, &rect,
171                DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
172     /* Min text width corresponds to space for the buttons */
173     tleft = 2 * ileft + iwidth;
174     twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
175     theight = rect.bottom;
176     
177     if (hFont)
178         SelectObject(hdc, hPrevFont);
179     ReleaseDC(hItem, hdc);
180     
181     tiheight = 16 + max(iheight, theight);
182     wwidth  = tleft + twidth + ileft + borwidth;
183     wheight = 8 + tiheight + bh + borheight;
184     
185     /* Resize the window */
186     SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
187                  SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
188     
189     /* Position the icon */
190     SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
191                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
192     
193     /* Position the text */
194     SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
195                  SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
196     
197     /* Position the buttons */
198     bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
199     for (buttons = i = 0; i < 7; i++) {
200         /* some arithmetic to get the right order for YesNoCancel windows */
201         hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
202         if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE) {
203             if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
204                 SetFocus(hItem);
205                 SendMessageA( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
206             }
207             SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
208                          SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
209             bpos += bw + bspace;
210         }
211     }
212     return hFont;
213 }
214
215
216 /**************************************************************************
217  *           MSGBOX_DlgProc
218  *
219  * Dialog procedure for message boxes.
220  */
221 static LRESULT CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
222                                         WPARAM wParam, LPARAM lParam )
223 {  
224   static HFONT hFont;
225   switch(message) {
226    case WM_INITDIALOG:
227     hFont = MSGBOX_OnInit(hwnd, (LPMSGBOXPARAMSA)lParam);
228     return 0;
229     
230    case WM_COMMAND:
231     switch (wParam)
232     {
233      case IDOK:
234      case IDCANCEL:
235      case IDABORT:
236      case IDRETRY:
237      case IDIGNORE:
238      case IDYES:
239      case IDNO:
240       EndDialog(hwnd, wParam);
241       if (hFont)
242           DeleteObject(hFont);
243       break;
244     }
245
246    default:
247      /* Ok. Ignore all the other messages */
248      TRACE("Message number %i is being ignored.\n", message);
249     break;
250   }
251   return 0;
252 }
253
254
255 /**************************************************************************
256  *           MessageBox16   (USER.1)
257  */
258 INT16 WINAPI MessageBox16( HWND16 hwnd, LPCSTR text, LPCSTR title, UINT16 type)
259 {
260     WARN("Messagebox\n");
261     return MessageBoxA( hwnd, text, title, type );
262 }
263
264
265 /**************************************************************************
266  *           MessageBoxA   (USER32.391)
267  *
268  * NOTES
269  *   The WARN is here to help debug erroneous MessageBoxes
270  *   Use: -debugmsg warn+dialog,+relay
271  */
272 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
273 {
274     LPVOID template;
275     HRSRC hRes;
276     MSGBOXPARAMSA mbox;
277
278     WARN("Messagebox\n");
279
280     if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
281         return 0;
282     if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
283         return 0;
284
285     if (!text) text="<WINE-NULL>";
286     if (!title)
287       title="Error";
288     mbox.lpszCaption = title;
289     mbox.lpszText  = text;
290     mbox.dwStyle  = type;
291     return DialogBoxIndirectParamA( GetWindowLongA(hWnd,GWL_HINSTANCE), template,
292                                       hWnd, (DLGPROC)MSGBOX_DlgProc, (LPARAM)&mbox );
293 }
294
295
296 /**************************************************************************
297  *           MessageBoxW   (USER32.396)
298  */
299 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title,
300                             UINT type )
301 {
302     LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
303     LPSTR textA  = HEAP_strdupWtoA( GetProcessHeap(), 0, text );
304     INT ret;
305     
306     WARN("Messagebox\n");
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  *           MessageBoxExA   (USER32.392)
317  */
318 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
319                               UINT type, WORD langid )
320 {
321     WARN("Messagebox\n");
322     /* ignore language id for now */
323     return MessageBoxA(hWnd,text,title,type);
324 }
325
326 /**************************************************************************
327  *           MessageBoxExW   (USER32.393)
328  */
329 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
330                               UINT type, WORD langid )
331 {
332     WARN("Messagebox\n");
333     /* ignore language id for now */
334     return MessageBoxW(hWnd,text,title,type);
335 }
336
337 /**************************************************************************
338  *           MessageBoxIndirect16   (USER.827)
339  */
340 INT16 WINAPI MessageBoxIndirect16( LPMSGBOXPARAMS16 msgbox )
341 {
342     LPVOID template;
343     HRSRC hRes;
344     MSGBOXPARAMSA msgbox32;
345
346     WARN("Messagebox\n");    
347     
348     if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
349         return 0;
350     if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
351         return 0;
352
353     msgbox32.cbSize             = msgbox->cbSize;
354     msgbox32.hwndOwner          = msgbox->hwndOwner;
355     msgbox32.hInstance          = msgbox->hInstance;
356     msgbox32.lpszText           = PTR_SEG_TO_LIN(msgbox->lpszText);
357     msgbox32.lpszCaption        = PTR_SEG_TO_LIN(msgbox->lpszCaption);
358     msgbox32.dwStyle            = msgbox->dwStyle;
359     msgbox32.lpszIcon           = PTR_SEG_TO_LIN(msgbox->lpszIcon);
360     msgbox32.dwContextHelpId    = msgbox->dwContextHelpId;
361     msgbox32.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
362     msgbox32.dwLanguageId       = msgbox->dwLanguageId;
363
364     return DialogBoxIndirectParamA( msgbox32.hInstance, template,
365                                       msgbox32.hwndOwner, (DLGPROC)MSGBOX_DlgProc,
366                                       (LPARAM)&msgbox32 );
367 }
368
369 /**************************************************************************
370  *           MessageBoxIndirectA   (USER32.394)
371  */
372 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
373 {
374     LPVOID template;
375     HRSRC hRes;
376
377     WARN("Messagebox\n");
378
379     if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
380         return 0;
381     if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
382         return 0;
383
384     return DialogBoxIndirectParamA( msgbox->hInstance, template,
385                                       msgbox->hwndOwner, (DLGPROC)MSGBOX_DlgProc,
386                                       (LPARAM)msgbox );
387 }
388
389 /**************************************************************************
390  *           MessageBoxIndirectW   (USER32.395)
391  */
392 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
393 {
394     MSGBOXPARAMSA       msgboxa;
395     WARN("Messagebox\n");
396
397     memcpy(&msgboxa,msgbox,sizeof(msgboxa));
398     if (msgbox->lpszCaption)    
399       lstrcpyWtoA((LPSTR)msgboxa.lpszCaption,msgbox->lpszCaption);
400     if (msgbox->lpszText)       
401       lstrcpyWtoA((LPSTR)msgboxa.lpszText,msgbox->lpszText);
402
403     return MessageBoxIndirectA(&msgboxa);
404 }