Various documentation fixes.
[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 "debugtools.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         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, LoadIconA(0, IDI_EXCLAMATIONA), 0);
100         break;
101     case MB_ICONQUESTION:
102         SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_QUESTIONA), 0);
103         break;
104     case MB_ICONASTERISK:
105         SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_ASTERISKA), 0);
106         break;
107     case MB_ICONHAND:
108       SendDlgItemMessageA(hwnd, stc1, STM_SETICON, LoadIconA(0, IDI_HANDA), 0);
109       break;
110     default:
111         /* By default, Windows 95/98/NT do not associate an icon to message boxes.
112          * So wine should do the same.
113          */
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  */
216 static 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 /**************************************************************************
251  *              MessageBox (USER.1)
252  */
253 INT16 WINAPI MessageBox16( HWND16 hwnd, LPCSTR text, LPCSTR title, UINT16 type)
254 {
255     WARN("Messagebox\n");
256     return MessageBoxA( hwnd, text, title, type );
257 }
258
259
260 /**************************************************************************
261  *              MessageBoxA (USER32.@)
262  *
263  * NOTES
264  *   The WARN is here to help debug erroneous MessageBoxes
265  *   Use: -debugmsg warn+dialog,+relay
266  */
267 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
268 {
269     LPVOID template;
270     HRSRC hRes;
271     MSGBOXPARAMSA mbox;
272
273     WARN("Messagebox\n");
274
275     if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
276         return 0;
277     if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
278         return 0;
279
280     if (!text) text="<WINE-NULL>";
281     if (!title)
282       title="Error";
283     mbox.lpszCaption = title;
284     mbox.lpszText  = text;
285     mbox.dwStyle  = type;
286     return DialogBoxIndirectParamA( GetWindowLongA(hWnd,GWL_HINSTANCE), template,
287                                       hWnd, (DLGPROC)MSGBOX_DlgProc, (LPARAM)&mbox );
288 }
289
290
291 /**************************************************************************
292  *              MessageBoxW (USER32.@)
293  */
294 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title,
295                             UINT type )
296 {
297     LPSTR titleA = HEAP_strdupWtoA( GetProcessHeap(), 0, title );
298     LPSTR textA  = HEAP_strdupWtoA( GetProcessHeap(), 0, text );
299     INT ret;
300     
301     WARN("Messagebox\n");
302
303     ret = MessageBoxA( hwnd, textA, titleA, type );
304     HeapFree( GetProcessHeap(), 0, titleA );
305     HeapFree( GetProcessHeap(), 0, textA );
306     return ret;
307 }
308
309
310 /**************************************************************************
311  *              MessageBoxExA (USER32.@)
312  */
313 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
314                               UINT type, WORD langid )
315 {
316     WARN("Messagebox\n");
317     /* ignore language id for now */
318     return MessageBoxA(hWnd,text,title,type);
319 }
320
321 /**************************************************************************
322  *              MessageBoxExW (USER32.@)
323  */
324 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
325                               UINT type, WORD langid )
326 {
327     WARN("Messagebox\n");
328     /* ignore language id for now */
329     return MessageBoxW(hWnd,text,title,type);
330 }
331
332 /**************************************************************************
333  *              MessageBoxIndirect (USER.827)
334  */
335 INT16 WINAPI MessageBoxIndirect16( LPMSGBOXPARAMS16 msgbox )
336 {
337     LPVOID template;
338     HRSRC hRes;
339     MSGBOXPARAMSA msgbox32;
340
341     WARN("Messagebox\n");    
342     
343     if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
344         return 0;
345     if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
346         return 0;
347
348     msgbox32.cbSize             = msgbox->cbSize;
349     msgbox32.hwndOwner          = msgbox->hwndOwner;
350     msgbox32.hInstance          = msgbox->hInstance;
351     msgbox32.lpszText           = MapSL(msgbox->lpszText);
352     msgbox32.lpszCaption        = MapSL(msgbox->lpszCaption);
353     msgbox32.dwStyle            = msgbox->dwStyle;
354     msgbox32.lpszIcon           = MapSL(msgbox->lpszIcon);
355     msgbox32.dwContextHelpId    = msgbox->dwContextHelpId;
356     msgbox32.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
357     msgbox32.dwLanguageId       = msgbox->dwLanguageId;
358
359     return DialogBoxIndirectParamA( msgbox32.hInstance, template,
360                                       msgbox32.hwndOwner, (DLGPROC)MSGBOX_DlgProc,
361                                       (LPARAM)&msgbox32 );
362 }
363
364 /**************************************************************************
365  *              MessageBoxIndirectA (USER32.@)
366  */
367 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
368 {
369     LPVOID template;
370     HRSRC hRes;
371
372     WARN("Messagebox\n");
373
374     if(!(hRes = FindResourceA(GetModuleHandleA("USER32"), "MSGBOX", RT_DIALOGA)))
375         return 0;
376     if(!(template = (LPVOID)LoadResource(GetModuleHandleA("USER32"), hRes)))
377         return 0;
378
379     return DialogBoxIndirectParamA( msgbox->hInstance, template,
380                                       msgbox->hwndOwner, (DLGPROC)MSGBOX_DlgProc,
381                                       (LPARAM)msgbox );
382 }
383
384 /**************************************************************************
385  *              MessageBoxIndirectW (USER32.@)
386  */
387 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
388 {
389     MSGBOXPARAMSA       msgboxa;
390     memcpy(&msgboxa,msgbox,sizeof(msgboxa));
391     msgboxa.lpszCaption = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszCaption );
392     msgboxa.lpszText = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszText );
393     msgboxa.lpszIcon = HEAP_strdupWtoA( GetProcessHeap(), 0, msgbox->lpszIcon );
394     return MessageBoxIndirectA(&msgboxa);
395 }