widl: Generate client and server code for using context handles.
[wine] / dlls / user32 / msgbox.c
1 /*
2  * Message boxes
3  *
4  * Copyright 1995 Bernd Schmidt
5  * Copyright 2004 Ivan Leo Puoti, Juan Lang
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdarg.h>
23 #include <string.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wingdi.h"
28 #include "winternl.h"
29 #include "dlgs.h"
30 #include "user_private.h"
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(dialog);
34 WINE_DECLARE_DEBUG_CHANNEL(msgbox);
35
36 #define MSGBOX_IDICON 1088
37 #define MSGBOX_IDTEXT 100
38 #define IDS_ERROR     2
39
40 struct ThreadWindows
41 {
42     UINT numHandles;
43     UINT numAllocs;
44     HWND *handles;
45 };
46
47 static BOOL CALLBACK MSGBOX_EnumProc(HWND hwnd, LPARAM lParam)
48 {
49     struct ThreadWindows *threadWindows = (struct ThreadWindows *)lParam;
50
51     if (!EnableWindow(hwnd, FALSE))
52     {
53         if(threadWindows->numHandles >= threadWindows->numAllocs)
54         {
55             threadWindows->handles = HeapReAlloc(GetProcessHeap(), 0, threadWindows->handles,
56                                                  (threadWindows->numAllocs*2)*sizeof(HWND));
57             threadWindows->numAllocs *= 2;
58         }
59         threadWindows->handles[threadWindows->numHandles++]=hwnd;
60     }
61    return TRUE;
62 }
63
64 static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
65 {
66     HFONT hFont = 0, hPrevFont = 0;
67     RECT rect;
68     HWND hItem;
69     HDC hdc;
70     int i, buttons;
71     int bspace, bw, bh, theight, tleft, wwidth, wheight, wleft, wtop, bpos;
72     int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
73     NONCLIENTMETRICSW nclm;
74     HMONITOR monitor = 0;
75     MONITORINFO mon_info;
76     LPCWSTR lpszText;
77     WCHAR buf[256];
78
79     nclm.cbSize = sizeof(nclm);
80     SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
81     hFont = CreateFontIndirectW (&nclm.lfMessageFont);
82     /* set button font */
83     for (i=1; i < 8; i++)
84         SendDlgItemMessageW (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
85     /* set text font */
86     SendDlgItemMessageW (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
87
88     if (HIWORD(lpmb->lpszCaption)) {
89        SetWindowTextW(hwnd, lpmb->lpszCaption);
90     } else {
91        UINT res_id = LOWORD(lpmb->lpszCaption);
92        if (res_id)
93        {
94            if (LoadStringW(lpmb->hInstance, res_id, buf, 256))
95                SetWindowTextW(hwnd, buf);
96        }
97        else
98        {
99            if (LoadStringW(user32_module, IDS_ERROR, buf, 256))
100                SetWindowTextW(hwnd, buf);
101        }
102     }
103     if (HIWORD(lpmb->lpszText)) {
104        lpszText = lpmb->lpszText;
105     } else {
106        lpszText = buf;
107        if (!LoadStringW(lpmb->hInstance, LOWORD(lpmb->lpszText), buf, 256))
108           *buf = 0;     /* FIXME ?? */
109     }
110     
111     TRACE_(msgbox)("%s\n", debugstr_w(lpszText));
112     SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
113
114     /* Hide not selected buttons */
115     switch(lpmb->dwStyle & MB_TYPEMASK) {
116     case MB_OK:
117         ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
118         /* fall through */
119     case MB_OKCANCEL:
120         ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
121         ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
122         ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
123         ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
124         ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
125         break;
126     case MB_ABORTRETRYIGNORE:
127         ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
128         ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
129         ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
130         ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
131         break;
132     case MB_YESNO:
133         ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
134         /* fall through */
135     case MB_YESNOCANCEL:
136         ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
137         ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
138         ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
139         ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
140         break;
141     case MB_RETRYCANCEL:
142         ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
143         ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
144         ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
145         ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
146         ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
147         break;
148     }
149     /* Set the icon */
150     switch(lpmb->dwStyle & MB_ICONMASK) {
151     case MB_ICONEXCLAMATION:
152         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
153                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
154         break;
155     case MB_ICONQUESTION:
156         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
157                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
158         break;
159     case MB_ICONASTERISK:
160         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
161                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
162         break;
163     case MB_ICONHAND:
164       SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
165                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
166       break;
167     case MB_USERICON:
168       SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
169                           (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
170       break;
171     default:
172         /* By default, Windows 95/98/NT do not associate an icon to message boxes.
173          * So wine should do the same.
174          */
175         break;
176     }
177
178     /* Position everything */
179     GetWindowRect(hwnd, &rect);
180     borheight = rect.bottom - rect.top;
181     borwidth  = rect.right - rect.left;
182     GetClientRect(hwnd, &rect);
183     borheight -= rect.bottom - rect.top;
184     borwidth  -= rect.right - rect.left;
185
186     /* Get the icon height */
187     GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
188     MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
189     if (!(lpmb->dwStyle & MB_ICONMASK))
190     {
191         rect.bottom = rect.top;
192         rect.right = rect.left;
193     }
194     iheight = rect.bottom - rect.top;
195     ileft = rect.left;
196     iwidth = rect.right - ileft;
197
198     hdc = GetDC(hwnd);
199     if (hFont)
200         hPrevFont = SelectObject(hdc, hFont);
201
202     /* Get the number of visible buttons and their size */
203     bh = bw = 1; /* Minimum button sizes */
204     for (buttons = 0, i = 1; i < 8; i++)
205     {
206         hItem = GetDlgItem(hwnd, i);
207         if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
208         {
209             WCHAR buttonText[1024];
210             int w, h;
211             buttons++;
212             if (GetWindowTextW(hItem, buttonText, 1024))
213             {
214                 DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
215                 h = rect.bottom - rect.top;
216                 w = rect.right - rect.left;
217                 if (h > bh) bh = h;
218                 if (w > bw)  bw = w ;
219             }
220         }
221     }
222     bw = max(bw, bh * 2);
223     /* Button white space */
224     bh = bh * 2;
225     bw = bw * 2;
226     bspace = bw/3; /* Space between buttons */
227
228     /* Get the text size */
229     GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
230     rect.top = rect.left = rect.bottom = 0;
231     DrawTextW( hdc, lpszText, -1, &rect,
232                DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
233     /* Min text width corresponds to space for the buttons */
234     tleft = ileft;
235     if (iwidth) tleft += ileft + iwidth;
236     twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
237     theight = rect.bottom;
238
239     if (hFont)
240         SelectObject(hdc, hPrevFont);
241     ReleaseDC(hwnd, hdc);
242
243     tiheight = 16 + max(iheight, theight);
244     wwidth  = tleft + twidth + ileft + borwidth;
245     wheight = 8 + tiheight + bh + borheight;
246
247     /* Message boxes are always desktop centered, so query desktop size and center window */
248     monitor = MonitorFromWindow(lpmb->hwndOwner ? lpmb->hwndOwner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY);
249     mon_info.cbSize = sizeof(mon_info);
250     GetMonitorInfoW(monitor, &mon_info);
251     wleft = (mon_info.rcWork.left + mon_info.rcWork.right - wwidth) / 2;
252     wtop = (mon_info.rcWork.top + mon_info.rcWork.bottom - wheight) / 2;
253
254     /* Resize and center the window */
255     SetWindowPos(hwnd, 0, wleft, wtop, wwidth, wheight,
256                  SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
257
258     /* Position the icon */
259     SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
260                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
261
262     /* Position the text */
263     SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
264                  SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
265
266     /* Position the buttons */
267     bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
268     for (buttons = i = 0; i < 7; i++) {
269         /* some arithmetic to get the right order for YesNoCancel windows */
270         hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
271         if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE) {
272             if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
273                 SetFocus(hItem);
274                 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
275             }
276             SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
277                          SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
278             bpos += bw + bspace;
279         }
280     }
281
282     /*handle modal message boxes*/
283     if (((lpmb->dwStyle & MB_TASKMODAL) && (lpmb->hwndOwner==NULL)) || (lpmb->dwStyle & MB_SYSTEMMODAL))
284         SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
285
286     return hFont;
287 }
288
289
290 /**************************************************************************
291  *           MSGBOX_DlgProc
292  *
293  * Dialog procedure for message boxes.
294  */
295 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
296                                         WPARAM wParam, LPARAM lParam )
297 {
298   HFONT hFont;
299
300   switch(message) {
301    case WM_INITDIALOG:
302    {
303        LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
304        SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
305        hFont = MSGBOX_OnInit(hwnd, mbp);
306        SetPropA(hwnd, "WINE_MSGBOX_HFONT", (HANDLE)hFont);
307        SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", (HANDLE)mbp->lpfnMsgBoxCallback);
308        break;
309    }
310
311    case WM_COMMAND:
312     switch (LOWORD(wParam))
313     {
314      case IDOK:
315      case IDCANCEL:
316      case IDABORT:
317      case IDRETRY:
318      case IDIGNORE:
319      case IDYES:
320      case IDNO:
321       hFont = GetPropA(hwnd, "WINE_MSGBOX_HFONT");
322       EndDialog(hwnd, wParam);
323       if (hFont)
324           DeleteObject(hFont);
325       break;
326     }
327     break;
328
329     case WM_HELP:
330     {
331         MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
332         HELPINFO hi;
333
334         memcpy(&hi, (void *)lParam, sizeof(hi));
335         hi.dwContextId = GetWindowContextHelpId(hwnd);
336
337         if (callback)
338             callback(&hi);
339         else
340             SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
341         break;
342    }
343
344    default:
345      /* Ok. Ignore all the other messages */
346      TRACE("Message number 0x%04x is being ignored.\n", message);
347     break;
348   }
349   return 0;
350 }
351
352
353 /**************************************************************************
354  *              MessageBoxA (USER32.@)
355  */
356 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
357 {
358     return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
359 }
360
361
362 /**************************************************************************
363  *              MessageBoxW (USER32.@)
364  */
365 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
366 {
367     return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
368 }
369
370
371 /**************************************************************************
372  *              MessageBoxExA (USER32.@)
373  */
374 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
375                               UINT type, WORD langid )
376 {
377     MSGBOXPARAMSA msgbox;
378
379     msgbox.cbSize = sizeof(msgbox);
380     msgbox.hwndOwner = hWnd;
381     msgbox.hInstance = 0;
382     msgbox.lpszText = text;
383     msgbox.lpszCaption = title;
384     msgbox.dwStyle = type;
385     msgbox.lpszIcon = NULL;
386     msgbox.dwContextHelpId = 0;
387     msgbox.lpfnMsgBoxCallback = NULL;
388     msgbox.dwLanguageId = langid;
389
390     return MessageBoxIndirectA(&msgbox);
391 }
392
393 /**************************************************************************
394  *              MessageBoxExW (USER32.@)
395  */
396 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
397                               UINT type, WORD langid )
398 {
399     MSGBOXPARAMSW msgbox;
400
401     msgbox.cbSize = sizeof(msgbox);
402     msgbox.hwndOwner = hWnd;
403     msgbox.hInstance = 0;
404     msgbox.lpszText = text;
405     msgbox.lpszCaption = title;
406     msgbox.dwStyle = type;
407     msgbox.lpszIcon = NULL;
408     msgbox.dwContextHelpId = 0;
409     msgbox.lpfnMsgBoxCallback = NULL;
410     msgbox.dwLanguageId = langid;
411
412     return MessageBoxIndirectW(&msgbox);
413 }
414
415 /**************************************************************************
416  *              MessageBoxIndirectA (USER32.@)
417  */
418 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
419 {
420     MSGBOXPARAMSW msgboxW;
421     UNICODE_STRING textW, captionW, iconW;
422     int ret;
423
424     if (HIWORD(msgbox->lpszText))
425         RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
426     else
427         textW.Buffer = (LPWSTR)msgbox->lpszText;
428     if (HIWORD(msgbox->lpszCaption))
429         RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
430     else
431         captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
432
433     if (msgbox->dwStyle & MB_USERICON)
434     {
435         if (HIWORD(msgbox->lpszIcon))
436             RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
437         else
438             iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
439     }
440     else
441         iconW.Buffer = NULL;
442
443     msgboxW.cbSize = sizeof(msgboxW);
444     msgboxW.hwndOwner = msgbox->hwndOwner;
445     msgboxW.hInstance = msgbox->hInstance;
446     msgboxW.lpszText = textW.Buffer;
447     msgboxW.lpszCaption = captionW.Buffer;
448     msgboxW.dwStyle = msgbox->dwStyle;
449     msgboxW.lpszIcon = iconW.Buffer;
450     msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
451     msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
452     msgboxW.dwLanguageId = msgbox->dwLanguageId;
453
454     ret = MessageBoxIndirectW(&msgboxW);
455
456     if (HIWORD(textW.Buffer)) RtlFreeUnicodeString(&textW);
457     if (HIWORD(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
458     if (HIWORD(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
459     return ret;
460 }
461
462 /**************************************************************************
463  *              MessageBoxIndirectW (USER32.@)
464  */
465 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
466 {
467     LPVOID tmplate;
468     HRSRC hRes;
469     int ret;
470     UINT i;
471     struct ThreadWindows threadWindows;
472     static const WCHAR msg_box_res_nameW[] = { 'M','S','G','B','O','X',0 };
473
474     if (!(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG,
475                                  msg_box_res_nameW, msgbox->dwLanguageId)))
476         return 0;
477     if (!(tmplate = (LPVOID)LoadResource(user32_module, hRes)))
478         return 0;
479
480     if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
481     {
482         threadWindows.numHandles = 0;
483         threadWindows.numAllocs = 10;
484         threadWindows.handles = HeapAlloc(GetProcessHeap(), 0, 10*sizeof(HWND));
485         EnumThreadWindows(GetCurrentThreadId(), MSGBOX_EnumProc, (LPARAM)&threadWindows);
486     }
487
488     ret=DialogBoxIndirectParamW(msgbox->hInstance, tmplate,
489                                 msgbox->hwndOwner, MSGBOX_DlgProc, (LPARAM)msgbox);
490
491     if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
492     {
493         for (i = 0; i < threadWindows.numHandles; i++)
494             EnableWindow(threadWindows.handles[i], TRUE);
495         HeapFree(GetProcessHeap(), 0, threadWindows.handles);
496     }
497     return ret;
498 }