quartz: Silence requests for ipin on filters.
[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     /* Index the order the buttons need to appear to an ID* constant */
79     static const int buttonOrder[10] = { 6, 7, 1, 3, 4, 2, 5, 10, 11, 9 };
80
81     nclm.cbSize = sizeof(nclm);
82     SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
83     hFont = CreateFontIndirectW (&nclm.lfMessageFont);
84     /* set button font */
85     for (i=1; i < 12; i++)
86         /* No button 8 (Close) */
87         if (i != 8) {
88             SendDlgItemMessageW (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
89         }
90     /* set text font */
91     SendDlgItemMessageW (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);
92
93     if (HIWORD(lpmb->lpszCaption)) {
94        SetWindowTextW(hwnd, lpmb->lpszCaption);
95     } else {
96        UINT res_id = LOWORD(lpmb->lpszCaption);
97        if (res_id)
98        {
99            if (LoadStringW(lpmb->hInstance, res_id, buf, 256))
100                SetWindowTextW(hwnd, buf);
101        }
102        else
103        {
104            if (LoadStringW(user32_module, IDS_ERROR, buf, 256))
105                SetWindowTextW(hwnd, buf);
106        }
107     }
108     if (HIWORD(lpmb->lpszText)) {
109        lpszText = lpmb->lpszText;
110     } else {
111        lpszText = buf;
112        if (!LoadStringW(lpmb->hInstance, LOWORD(lpmb->lpszText), buf, 256))
113           *buf = 0;     /* FIXME ?? */
114     }
115     
116     TRACE_(msgbox)("%s\n", debugstr_w(lpszText));
117     SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
118
119     /* Hide not selected buttons */
120     switch(lpmb->dwStyle & MB_TYPEMASK) {
121     case MB_OK:
122         ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
123         /* fall through */
124     case MB_OKCANCEL:
125         ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
126         ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
127         ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
128         ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
129         ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
130         ShowWindow(GetDlgItem(hwnd, IDTRYAGAIN), SW_HIDE);
131         ShowWindow(GetDlgItem(hwnd, IDCONTINUE), SW_HIDE);
132         break;
133     case MB_ABORTRETRYIGNORE:
134         ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
135         ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
136         ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
137         ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
138         ShowWindow(GetDlgItem(hwnd, IDCONTINUE), SW_HIDE);
139         ShowWindow(GetDlgItem(hwnd, IDTRYAGAIN), SW_HIDE);
140         break;
141     case MB_YESNO:
142         ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
143         /* fall through */
144     case MB_YESNOCANCEL:
145         ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
146         ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
147         ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
148         ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
149         ShowWindow(GetDlgItem(hwnd, IDCONTINUE), SW_HIDE);
150         ShowWindow(GetDlgItem(hwnd, IDTRYAGAIN), SW_HIDE);
151         break;
152     case MB_RETRYCANCEL:
153         ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
154         ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
155         ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
156         ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
157         ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
158         ShowWindow(GetDlgItem(hwnd, IDCONTINUE), SW_HIDE);
159         ShowWindow(GetDlgItem(hwnd, IDTRYAGAIN), SW_HIDE);
160         break;
161     case MB_CANCELTRYCONTINUE:
162         ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
163         ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
164         ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
165         ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
166         ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
167         ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
168     }
169     /* Set the icon */
170     switch(lpmb->dwStyle & MB_ICONMASK) {
171     case MB_ICONEXCLAMATION:
172         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
173                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
174         break;
175     case MB_ICONQUESTION:
176         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
177                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
178         break;
179     case MB_ICONASTERISK:
180         SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
181                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
182         break;
183     case MB_ICONHAND:
184       SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
185                             (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
186       break;
187     case MB_USERICON:
188       SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
189                           (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
190       break;
191     default:
192         /* By default, Windows 95/98/NT do not associate an icon to message boxes.
193          * So wine should do the same.
194          */
195         break;
196     }
197
198     /* Hide Help button unless MB_HELP supplied */
199     if (!(lpmb->dwStyle & MB_HELP)) {
200         ShowWindow(GetDlgItem(hwnd, IDHELP), SW_HIDE);
201     }
202
203     /* Position everything */
204     GetWindowRect(hwnd, &rect);
205     borheight = rect.bottom - rect.top;
206     borwidth  = rect.right - rect.left;
207     GetClientRect(hwnd, &rect);
208     borheight -= rect.bottom - rect.top;
209     borwidth  -= rect.right - rect.left;
210
211     /* Get the icon height */
212     GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
213     MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
214     if (!(lpmb->dwStyle & MB_ICONMASK))
215     {
216         rect.bottom = rect.top;
217         rect.right = rect.left;
218     }
219     iheight = rect.bottom - rect.top;
220     ileft = rect.left;
221     iwidth = rect.right - ileft;
222
223     hdc = GetDC(hwnd);
224     if (hFont)
225         hPrevFont = SelectObject(hdc, hFont);
226
227     /* Get the number of visible buttons and their size */
228     bh = bw = 1; /* Minimum button sizes */
229     for (buttons = 0, i = 1; i < 12; i++)
230     {
231         if (i == 8) continue; /* No CLOSE button */
232         hItem = GetDlgItem(hwnd, i);
233         if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
234         {
235             WCHAR buttonText[1024];
236             int w, h;
237             buttons++;
238             if (GetWindowTextW(hItem, buttonText, 1024))
239             {
240                 DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
241                 h = rect.bottom - rect.top;
242                 w = rect.right - rect.left;
243                 if (h > bh) bh = h;
244                 if (w > bw)  bw = w ;
245             }
246         }
247     }
248     bw = max(bw, bh * 2);
249     /* Button white space */
250     bh = bh * 2;
251     bw = bw * 2;
252     bspace = bw/3; /* Space between buttons */
253
254     /* Get the text size */
255     GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
256     rect.top = rect.left = rect.bottom = 0;
257     DrawTextW( hdc, lpszText, -1, &rect,
258                DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
259     /* Min text width corresponds to space for the buttons */
260     tleft = ileft;
261     if (iwidth) tleft += ileft + iwidth;
262     twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
263     theight = rect.bottom;
264
265     if (hFont)
266         SelectObject(hdc, hPrevFont);
267     ReleaseDC(hwnd, hdc);
268
269     tiheight = 16 + max(iheight, theight);
270     wwidth  = tleft + twidth + ileft + borwidth;
271     wheight = 8 + tiheight + bh + borheight;
272
273     /* Message boxes are always desktop centered, so query desktop size and center window */
274     monitor = MonitorFromWindow(lpmb->hwndOwner ? lpmb->hwndOwner : GetActiveWindow(), MONITOR_DEFAULTTOPRIMARY);
275     mon_info.cbSize = sizeof(mon_info);
276     GetMonitorInfoW(monitor, &mon_info);
277     wleft = (mon_info.rcWork.left + mon_info.rcWork.right - wwidth) / 2;
278     wtop = (mon_info.rcWork.top + mon_info.rcWork.bottom - wheight) / 2;
279
280     /* Resize and center the window */
281     SetWindowPos(hwnd, 0, wleft, wtop, wwidth, wheight,
282                  SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
283
284     /* Position the icon */
285     SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
286                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
287
288     /* Position the text */
289     SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
290                  SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
291
292     /* Position the buttons */
293     bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
294     for (buttons = i = 0; i < (sizeof(buttonOrder) / sizeof(buttonOrder[0])); i++) {
295
296         /* Convert the button order to ID* value to order for the buttons */
297         hItem = GetDlgItem(hwnd, buttonOrder[i]);
298         if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE) {
299             if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
300                 SetFocus(hItem);
301                 SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
302             }
303             SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
304                          SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
305             bpos += bw + bspace;
306         }
307     }
308
309     /*handle modal message boxes*/
310     if (((lpmb->dwStyle & MB_TASKMODAL) && (lpmb->hwndOwner==NULL)) || (lpmb->dwStyle & MB_SYSTEMMODAL))
311         SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
312
313     return hFont;
314 }
315
316
317 /**************************************************************************
318  *           MSGBOX_DlgProc
319  *
320  * Dialog procedure for message boxes.
321  */
322 static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
323                                         WPARAM wParam, LPARAM lParam )
324 {
325   HFONT hFont;
326
327   switch(message) {
328    case WM_INITDIALOG:
329    {
330        LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
331        SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
332        hFont = MSGBOX_OnInit(hwnd, mbp);
333        SetPropA(hwnd, "WINE_MSGBOX_HFONT", (HANDLE)hFont);
334        SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", (HANDLE)mbp->lpfnMsgBoxCallback);
335        break;
336    }
337
338    case WM_COMMAND:
339     switch (LOWORD(wParam))
340     {
341      case IDOK:
342      case IDCANCEL:
343      case IDABORT:
344      case IDRETRY:
345      case IDIGNORE:
346      case IDYES:
347      case IDNO:
348      case IDTRYAGAIN:
349      case IDCONTINUE:
350       hFont = GetPropA(hwnd, "WINE_MSGBOX_HFONT");
351       EndDialog(hwnd, wParam);
352       if (hFont)
353           DeleteObject(hFont);
354       break;
355      case IDHELP:
356       FIXME("Help button not supported yet\n");
357       break;
358     }
359     break;
360
361     case WM_HELP:
362     {
363         MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
364         HELPINFO hi;
365
366         memcpy(&hi, (void *)lParam, sizeof(hi));
367         hi.dwContextId = GetWindowContextHelpId(hwnd);
368
369         if (callback)
370             callback(&hi);
371         else
372             SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
373         break;
374    }
375
376    default:
377      /* Ok. Ignore all the other messages */
378      TRACE("Message number 0x%04x is being ignored.\n", message);
379     break;
380   }
381   return 0;
382 }
383
384
385 /**************************************************************************
386  *              MessageBoxA (USER32.@)
387  */
388 INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
389 {
390     return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
391 }
392
393
394 /**************************************************************************
395  *              MessageBoxW (USER32.@)
396  */
397 INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
398 {
399     return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
400 }
401
402
403 /**************************************************************************
404  *              MessageBoxExA (USER32.@)
405  */
406 INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
407                               UINT type, WORD langid )
408 {
409     MSGBOXPARAMSA msgbox;
410
411     msgbox.cbSize = sizeof(msgbox);
412     msgbox.hwndOwner = hWnd;
413     msgbox.hInstance = 0;
414     msgbox.lpszText = text;
415     msgbox.lpszCaption = title;
416     msgbox.dwStyle = type;
417     msgbox.lpszIcon = NULL;
418     msgbox.dwContextHelpId = 0;
419     msgbox.lpfnMsgBoxCallback = NULL;
420     msgbox.dwLanguageId = langid;
421
422     return MessageBoxIndirectA(&msgbox);
423 }
424
425 /**************************************************************************
426  *              MessageBoxExW (USER32.@)
427  */
428 INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
429                               UINT type, WORD langid )
430 {
431     MSGBOXPARAMSW msgbox;
432
433     msgbox.cbSize = sizeof(msgbox);
434     msgbox.hwndOwner = hWnd;
435     msgbox.hInstance = 0;
436     msgbox.lpszText = text;
437     msgbox.lpszCaption = title;
438     msgbox.dwStyle = type;
439     msgbox.lpszIcon = NULL;
440     msgbox.dwContextHelpId = 0;
441     msgbox.lpfnMsgBoxCallback = NULL;
442     msgbox.dwLanguageId = langid;
443
444     return MessageBoxIndirectW(&msgbox);
445 }
446
447 /**************************************************************************
448  *              MessageBoxIndirectA (USER32.@)
449  */
450 INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
451 {
452     MSGBOXPARAMSW msgboxW;
453     UNICODE_STRING textW, captionW, iconW;
454     int ret;
455
456     if (HIWORD(msgbox->lpszText))
457         RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
458     else
459         textW.Buffer = (LPWSTR)msgbox->lpszText;
460     if (HIWORD(msgbox->lpszCaption))
461         RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
462     else
463         captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
464
465     if (msgbox->dwStyle & MB_USERICON)
466     {
467         if (HIWORD(msgbox->lpszIcon))
468             RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
469         else
470             iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
471     }
472     else
473         iconW.Buffer = NULL;
474
475     msgboxW.cbSize = sizeof(msgboxW);
476     msgboxW.hwndOwner = msgbox->hwndOwner;
477     msgboxW.hInstance = msgbox->hInstance;
478     msgboxW.lpszText = textW.Buffer;
479     msgboxW.lpszCaption = captionW.Buffer;
480     msgboxW.dwStyle = msgbox->dwStyle;
481     msgboxW.lpszIcon = iconW.Buffer;
482     msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
483     msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
484     msgboxW.dwLanguageId = msgbox->dwLanguageId;
485
486     ret = MessageBoxIndirectW(&msgboxW);
487
488     if (HIWORD(textW.Buffer)) RtlFreeUnicodeString(&textW);
489     if (HIWORD(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
490     if (HIWORD(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
491     return ret;
492 }
493
494 /**************************************************************************
495  *              MessageBoxIndirectW (USER32.@)
496  */
497 INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
498 {
499     LPVOID tmplate;
500     HRSRC hRes;
501     int ret;
502     UINT i;
503     struct ThreadWindows threadWindows;
504     static const WCHAR msg_box_res_nameW[] = { 'M','S','G','B','O','X',0 };
505
506     if (!(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG,
507                                  msg_box_res_nameW, msgbox->dwLanguageId)))
508         return 0;
509     if (!(tmplate = LoadResource(user32_module, hRes)))
510         return 0;
511
512     if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
513     {
514         threadWindows.numHandles = 0;
515         threadWindows.numAllocs = 10;
516         threadWindows.handles = HeapAlloc(GetProcessHeap(), 0, 10*sizeof(HWND));
517         EnumThreadWindows(GetCurrentThreadId(), MSGBOX_EnumProc, (LPARAM)&threadWindows);
518     }
519
520     ret=DialogBoxIndirectParamW(msgbox->hInstance, tmplate,
521                                 msgbox->hwndOwner, MSGBOX_DlgProc, (LPARAM)msgbox);
522
523     if ((msgbox->dwStyle & MB_TASKMODAL) && (msgbox->hwndOwner==NULL))
524     {
525         for (i = 0; i < threadWindows.numHandles; i++)
526             EnableWindow(threadWindows.handles[i], TRUE);
527         HeapFree(GetProcessHeap(), 0, threadWindows.handles);
528     }
529     return ret;
530 }