msgbox.c 13.7 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Message boxes
 *
 * Copyright 1995 Bernd Schmidt
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21
#include <stdarg.h>
22
#include <string.h>
Patrik Stridvall's avatar
Patrik Stridvall committed
23

24
#include "windef.h"
25
#include "winbase.h"
26
#include "wingdi.h"
Patrik Stridvall's avatar
Patrik Stridvall committed
27
#include "wine/winbase16.h"
28
#include "wine/winuser16.h"
29
#include "winreg.h"
30
#include "winternl.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
31
#include "dlgs.h"
32
#include "user.h"
33
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
34

35
WINE_DEFAULT_DEBUG_CHANNEL(dialog);
36

37 38
#define MSGBOX_IDICON 1088
#define MSGBOX_IDTEXT 100
39
#define IDS_ERROR     2
Alexandre Julliard's avatar
Alexandre Julliard committed
40

41
static HFONT MSGBOX_OnInit(HWND hwnd, LPMSGBOXPARAMSW lpmb)
42
{
43
    HFONT hFont = 0, hPrevFont = 0;
44 45 46 47 48 49
    RECT rect;
    HWND hItem;
    HDC hdc;
    int i, buttons;
    int bspace, bw, bh, theight, tleft, wwidth, wheight, bpos;
    int borheight, borwidth, iheight, ileft, iwidth, twidth, tiheight;
50
    NONCLIENTMETRICSW nclm;
51 52
    LPCWSTR lpszText;
    WCHAR buf[256];
53

54 55 56 57 58 59 60 61 62
    nclm.cbSize = sizeof(nclm);
    SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
    hFont = CreateFontIndirectW (&nclm.lfMessageFont);
    /* set button font */
    for (i=1; i < 8; i++)
	SendDlgItemMessageW (hwnd, i, WM_SETFONT, (WPARAM)hFont, 0);
    /* set text font */
    SendDlgItemMessageW (hwnd, MSGBOX_IDTEXT, WM_SETFONT, (WPARAM)hFont, 0);

63
    if (HIWORD(lpmb->lpszCaption)) {
64
       SetWindowTextW(hwnd, lpmb->lpszCaption);
65
    } else {
66 67 68 69 70 71 72 73
       UINT res_id = LOWORD(lpmb->lpszCaption);
       if (res_id)
       {
           if (LoadStringW(lpmb->hInstance, res_id, buf, 256))
               SetWindowTextW(hwnd, buf);
       }
       else
       {
74
           if (LoadStringW(user32_module, IDS_ERROR, buf, 256))
75 76
               SetWindowTextW(hwnd, buf);
       }
77 78 79 80 81
    }
    if (HIWORD(lpmb->lpszText)) {
       lpszText = lpmb->lpszText;
    } else {
       lpszText = buf;
82
       if (!LoadStringW(lpmb->hInstance, LOWORD(lpmb->lpszText), buf, 256))
83 84
	  *buf = 0;	/* FIXME ?? */
    }
85
    SetWindowTextW(GetDlgItem(hwnd, MSGBOX_IDTEXT), lpszText);
86

Alexandre Julliard's avatar
Alexandre Julliard committed
87
    /* Hide not selected buttons */
Alexandre Julliard's avatar
Alexandre Julliard committed
88
    switch(lpmb->dwStyle & MB_TYPEMASK) {
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    case MB_OK:
	ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
	/* fall through */
    case MB_OKCANCEL:
	ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
	break;
    case MB_ABORTRETRYIGNORE:
	ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
	break;
    case MB_YESNO:
	ShowWindow(GetDlgItem(hwnd, IDCANCEL), SW_HIDE);
	/* fall through */
    case MB_YESNOCANCEL:
	ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDRETRY), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
	break;
    case MB_RETRYCANCEL:
	ShowWindow(GetDlgItem(hwnd, IDOK), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDABORT), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDIGNORE), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDYES), SW_HIDE);
	ShowWindow(GetDlgItem(hwnd, IDNO), SW_HIDE);
	break;
Alexandre Julliard's avatar
Alexandre Julliard committed
121 122
    }
    /* Set the icon */
Alexandre Julliard's avatar
Alexandre Julliard committed
123
    switch(lpmb->dwStyle & MB_ICONMASK) {
124
    case MB_ICONEXCLAMATION:
125
	SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
126
			    (WPARAM)LoadIconW(0, (LPWSTR)IDI_EXCLAMATION), 0);
127 128
	break;
    case MB_ICONQUESTION:
129
	SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
130
			    (WPARAM)LoadIconW(0, (LPWSTR)IDI_QUESTION), 0);
131 132
	break;
    case MB_ICONASTERISK:
133
	SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
134
			    (WPARAM)LoadIconW(0, (LPWSTR)IDI_ASTERISK), 0);
135 136
	break;
    case MB_ICONHAND:
137
      SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
138
			    (WPARAM)LoadIconW(0, (LPWSTR)IDI_HAND), 0);
139 140 141 142
      break;
    case MB_USERICON:
      SendDlgItemMessageW(hwnd, stc1, STM_SETICON,
			  (WPARAM)LoadIconW(lpmb->hInstance, lpmb->lpszIcon), 0);
143 144 145 146 147
      break;
    default:
	/* By default, Windows 95/98/NT do not associate an icon to message boxes.
         * So wine should do the same.
         */
148
	break;
Alexandre Julliard's avatar
Alexandre Julliard committed
149
    }
150

Alexandre Julliard's avatar
Alexandre Julliard committed
151
    /* Position everything */
152
    GetWindowRect(hwnd, &rect);
Alexandre Julliard's avatar
Alexandre Julliard committed
153
    borheight = rect.bottom - rect.top;
154
    borwidth  = rect.right - rect.left;
155
    GetClientRect(hwnd, &rect);
Alexandre Julliard's avatar
Alexandre Julliard committed
156
    borheight -= rect.bottom - rect.top;
157
    borwidth  -= rect.right - rect.left;
158

Alexandre Julliard's avatar
Alexandre Julliard committed
159
    /* Get the icon height */
160 161
    GetWindowRect(GetDlgItem(hwnd, MSGBOX_IDICON), &rect);
    MapWindowPoints(0, hwnd, (LPPOINT)&rect, 2);
162 163 164 165 166
    if (!(lpmb->dwStyle & MB_ICONMASK))
    {
        rect.bottom = rect.top;
        rect.right = rect.left;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
167
    iheight = rect.bottom - rect.top;
168 169
    ileft = rect.left;
    iwidth = rect.right - ileft;
170

171 172 173
    hdc = GetDC(hwnd);
    if (hFont)
	hPrevFont = SelectObject(hdc, hFont);
174

175 176
    /* Get the number of visible buttons and their size */
    bh = bw = 1; /* Minimum button sizes */
Alexandre Julliard's avatar
Alexandre Julliard committed
177 178
    for (buttons = 0, i = 1; i < 8; i++)
    {
179
	hItem = GetDlgItem(hwnd, i);
180
	if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE)
181
	{
182
	    WCHAR buttonText[1024];
183 184
	    int w, h;
	    buttons++;
185
	    if (GetWindowTextW(hItem, buttonText, 1024))
186
	    {
187
		DrawTextW( hdc, buttonText, -1, &rect, DT_LEFT | DT_EXPANDTABS | DT_CALCRECT);
188 189 190 191 192 193
		h = rect.bottom - rect.top;
		w = rect.right - rect.left;
		if (h > bh) bh = h;
		if (w > bw)  bw = w ;
	    }
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
194
    }
195
    bw = max(bw, bh * 2);
196 197 198 199
    /* Button white space */
    bh = bh * 2;
    bw = bw * 2;
    bspace = bw/3; /* Space between buttons */
200

Alexandre Julliard's avatar
Alexandre Julliard committed
201
    /* Get the text size */
202 203
    GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
    rect.top = rect.left = rect.bottom = 0;
204
    DrawTextW( hdc, lpszText, -1, &rect,
205 206
	       DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
    /* Min text width corresponds to space for the buttons */
207 208
    tleft = ileft;
    if (iwidth) tleft += ileft + iwidth;
209
    twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
210
    theight = rect.bottom;
211

212 213
    if (hFont)
	SelectObject(hdc, hPrevFont);
214
    ReleaseDC(hItem, hdc);
215

216
    tiheight = 16 + max(iheight, theight);
217 218
    wwidth  = tleft + twidth + ileft + borwidth;
    wheight = 8 + tiheight + bh + borheight;
219

220 221 222
    /* Resize the window */
    SetWindowPos(hwnd, 0, 0, 0, wwidth, wheight,
		 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
223

Alexandre Julliard's avatar
Alexandre Julliard committed
224
    /* Position the icon */
225 226
    SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDICON), 0, ileft, (tiheight - iheight) / 2, 0, 0,
		 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
227

228 229 230
    /* Position the text */
    SetWindowPos(GetDlgItem(hwnd, MSGBOX_IDTEXT), 0, tleft, (tiheight - theight) / 2, twidth, theight,
		 SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOREDRAW);
231

Alexandre Julliard's avatar
Alexandre Julliard committed
232
    /* Position the buttons */
233
    bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
Alexandre Julliard's avatar
Alexandre Julliard committed
234
    for (buttons = i = 0; i < 7; i++) {
235 236
	/* some arithmetic to get the right order for YesNoCancel windows */
	hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
237
	if (GetWindowLongW(hItem, GWL_STYLE) & WS_VISIBLE) {
238 239
	    if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
		SetFocus(hItem);
240
		SendMessageW( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
241 242 243 244
	    }
	    SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
			 SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
	    bpos += bw + bspace;
Alexandre Julliard's avatar
Alexandre Julliard committed
245 246
	}
    }
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

    /* handle modal MessageBoxes */
    if (lpmb->dwStyle & (MB_TASKMODAL|MB_SYSTEMMODAL))
    {
	FIXME("%s modal msgbox ! Not modal yet.\n",
		lpmb->dwStyle & MB_TASKMODAL ? "task" : "system");
	/* Probably do EnumTaskWindows etc. here for TASKMODAL
	 * and work your way up to the top - I'm lazy (HWND_TOP) */
	SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
			SWP_NOSIZE | SWP_NOMOVE);
	if (lpmb->dwStyle & MB_TASKMODAL)
	    /* at least MB_TASKMODAL seems to imply a ShowWindow */
	    ShowWindow(hwnd, SW_SHOW);
    }
    if (lpmb->dwStyle & MB_APPLMODAL)
	FIXME("app modal msgbox ! Not modal yet.\n");
263

264 265 266 267 268 269 270 271 272
    return hFont;
}


/**************************************************************************
 *           MSGBOX_DlgProc
 *
 * Dialog procedure for message boxes.
 */
273
static INT_PTR CALLBACK MSGBOX_DlgProc( HWND hwnd, UINT message,
274
                                        WPARAM wParam, LPARAM lParam )
275
{
276 277
  HFONT hFont;

278 279
  switch(message) {
   case WM_INITDIALOG:
280 281 282 283 284 285 286 287
   {
       LPMSGBOXPARAMSW mbp = (LPMSGBOXPARAMSW)lParam;
       SetWindowContextHelpId(hwnd, mbp->dwContextHelpId);
       hFont = MSGBOX_OnInit(hwnd, mbp);
       SetPropA(hwnd, "WINE_MSGBOX_HFONT", (HANDLE)hFont);
       SetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK", (HANDLE)mbp->lpfnMsgBoxCallback);
       break;
   }
288

Alexandre Julliard's avatar
Alexandre Julliard committed
289
   case WM_COMMAND:
290
    switch (LOWORD(wParam))
Alexandre Julliard's avatar
Alexandre Julliard committed
291
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
292 293 294 295 296 297 298
     case IDOK:
     case IDCANCEL:
     case IDABORT:
     case IDRETRY:
     case IDIGNORE:
     case IDYES:
     case IDNO:
299
      hFont = GetPropA(hwnd, "WINE_MSGBOX_HFONT");
300
      EndDialog(hwnd, wParam);
301 302
      if (hFont)
	  DeleteObject(hFont);
Alexandre Julliard's avatar
Alexandre Julliard committed
303 304
      break;
    }
305
    break;
Alexandre Julliard's avatar
Alexandre Julliard committed
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    case WM_HELP:
    {
        MSGBOXCALLBACK callback = (MSGBOXCALLBACK)GetPropA(hwnd, "WINE_MSGBOX_HELPCALLBACK");
        HELPINFO hi;

        memcpy(&hi, (void *)lParam, sizeof(hi));
        hi.dwContextId = GetWindowContextHelpId(hwnd);

        if (callback)
            callback(&hi);
        else
            SendMessageW(GetWindow(hwnd, GW_OWNER), WM_HELP, 0, (LPARAM)&hi);
	break;
   }

Alexandre Julliard's avatar
Alexandre Julliard committed
322 323
   default:
     /* Ok. Ignore all the other messages */
324
     TRACE("Message number 0x%04x is being ignored.\n", message);
Alexandre Julliard's avatar
Alexandre Julliard committed
325 326 327 328 329
    break;
  }
  return 0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
330

Alexandre Julliard's avatar
Alexandre Julliard committed
331
/**************************************************************************
332
 *		MessageBoxA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
333 334 335 336
 *
 * NOTES
 *   The WARN is here to help debug erroneous MessageBoxes
 *   Use: -debugmsg warn+dialog,+relay
Alexandre Julliard's avatar
Alexandre Julliard committed
337
 */
338
INT WINAPI MessageBoxA(HWND hWnd, LPCSTR text, LPCSTR title, UINT type)
Alexandre Julliard's avatar
Alexandre Julliard committed
339
{
340
    return MessageBoxExA(hWnd, text, title, type, LANG_NEUTRAL);
Alexandre Julliard's avatar
Alexandre Julliard committed
341 342
}

Alexandre Julliard's avatar
Alexandre Julliard committed
343

Alexandre Julliard's avatar
Alexandre Julliard committed
344
/**************************************************************************
345
 *		MessageBoxW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
346
 */
347
INT WINAPI MessageBoxW( HWND hwnd, LPCWSTR text, LPCWSTR title, UINT type )
Alexandre Julliard's avatar
Alexandre Julliard committed
348
{
349
    return MessageBoxExW(hwnd, text, title, type, LANG_NEUTRAL);
Alexandre Julliard's avatar
Alexandre Julliard committed
350
}
Alexandre Julliard's avatar
Alexandre Julliard committed
351 352


Alexandre Julliard's avatar
Alexandre Julliard committed
353
/**************************************************************************
354
 *		MessageBoxExA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
355
 */
356 357
INT WINAPI MessageBoxExA( HWND hWnd, LPCSTR text, LPCSTR title,
                              UINT type, WORD langid )
Alexandre Julliard's avatar
Alexandre Julliard committed
358
{
359 360 361 362 363 364 365 366 367 368 369 370 371 372
    MSGBOXPARAMSA msgbox;

    msgbox.cbSize = sizeof(msgbox);
    msgbox.hwndOwner = hWnd;
    msgbox.hInstance = 0;
    msgbox.lpszText = text;
    msgbox.lpszCaption = title;
    msgbox.dwStyle = type;
    msgbox.lpszIcon = NULL;
    msgbox.dwContextHelpId = 0;
    msgbox.lpfnMsgBoxCallback = NULL;
    msgbox.dwLanguageId = langid;

    return MessageBoxIndirectA(&msgbox);
Alexandre Julliard's avatar
Alexandre Julliard committed
373 374 375
}

/**************************************************************************
376
 *		MessageBoxExW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
377
 */
378 379
INT WINAPI MessageBoxExW( HWND hWnd, LPCWSTR text, LPCWSTR title,
                              UINT type, WORD langid )
Alexandre Julliard's avatar
Alexandre Julliard committed
380
{
381 382 383 384 385 386 387 388 389 390 391 392 393 394
    MSGBOXPARAMSW msgbox;

    msgbox.cbSize = sizeof(msgbox);
    msgbox.hwndOwner = hWnd;
    msgbox.hInstance = 0;
    msgbox.lpszText = text;
    msgbox.lpszCaption = title;
    msgbox.dwStyle = type;
    msgbox.lpszIcon = NULL;
    msgbox.dwContextHelpId = 0;
    msgbox.lpfnMsgBoxCallback = NULL;
    msgbox.dwLanguageId = langid;

    return MessageBoxIndirectW(&msgbox);
Alexandre Julliard's avatar
Alexandre Julliard committed
395 396
}

Alexandre Julliard's avatar
Alexandre Julliard committed
397
/**************************************************************************
398
 *		MessageBoxIndirectA (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
399
 */
400
INT WINAPI MessageBoxIndirectA( LPMSGBOXPARAMSA msgbox )
Alexandre Julliard's avatar
Alexandre Julliard committed
401
{
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    MSGBOXPARAMSW msgboxW;
    UNICODE_STRING textW, captionW, iconW;
    int ret;

    if (HIWORD(msgbox->lpszText))
        RtlCreateUnicodeStringFromAsciiz(&textW, msgbox->lpszText);
    else
        textW.Buffer = (LPWSTR)msgbox->lpszText;
    if (HIWORD(msgbox->lpszCaption))
        RtlCreateUnicodeStringFromAsciiz(&captionW, msgbox->lpszCaption);
    else
        captionW.Buffer = (LPWSTR)msgbox->lpszCaption;
    if (HIWORD(msgbox->lpszIcon))
        RtlCreateUnicodeStringFromAsciiz(&iconW, msgbox->lpszIcon);
    else
417
        iconW.Buffer = (LPWSTR)msgbox->lpszIcon;
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

    msgboxW.cbSize = sizeof(msgboxW);
    msgboxW.hwndOwner = msgbox->hwndOwner;
    msgboxW.hInstance = msgbox->hInstance;
    msgboxW.lpszText = textW.Buffer;
    msgboxW.lpszCaption = captionW.Buffer;
    msgboxW.dwStyle = msgbox->dwStyle;
    msgboxW.lpszIcon = iconW.Buffer;
    msgboxW.dwContextHelpId = msgbox->dwContextHelpId;
    msgboxW.lpfnMsgBoxCallback = msgbox->lpfnMsgBoxCallback;
    msgboxW.dwLanguageId = msgbox->dwLanguageId;

    ret = MessageBoxIndirectW(&msgboxW);

    if (HIWORD(textW.Buffer)) RtlFreeUnicodeString(&textW);
    if (HIWORD(captionW.Buffer)) RtlFreeUnicodeString(&captionW);
    if (HIWORD(iconW.Buffer)) RtlFreeUnicodeString(&iconW);
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
436 437 438
}

/**************************************************************************
439
 *		MessageBoxIndirectW (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
440
 */
441
INT WINAPI MessageBoxIndirectW( LPMSGBOXPARAMSW msgbox )
Alexandre Julliard's avatar
Alexandre Julliard committed
442
{
443 444 445
    LPVOID tmplate;
    HRSRC hRes;
    static const WCHAR msg_box_res_nameW[] = { 'M','S','G','B','O','X',0 };
446

447 448
    if (!(hRes = FindResourceExW(user32_module, (LPWSTR)RT_DIALOG,
                                 msg_box_res_nameW, msgbox->dwLanguageId)))
449
        return 0;
450
    if (!(tmplate = (LPVOID)LoadResource(user32_module, hRes)))
451 452 453 454
        return 0;

    return DialogBoxIndirectParamW(msgbox->hInstance, tmplate, msgbox->hwndOwner,
                                   MSGBOX_DlgProc, (LPARAM)msgbox);
Alexandre Julliard's avatar
Alexandre Julliard committed
455
}