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