propsheet.c 103 KB
Newer Older
1 2 3 4
/*
 * Property Sheets
 *
 * Copyright 1998 Francis Beaudet
5
 * Copyright 1999 Thuy Nguyen
6
 * Copyright 2004 Maxime Bellenge
Filip Navara's avatar
Filip Navara committed
7
 * Copyright 2004 Filip Navara
8
 *
9 10 11 12 13 14 15 16 17 18 19 20
 * 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
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22
 *
Filip Navara's avatar
Filip Navara committed
23 24 25 26 27 28 29
 * This code was audited for completeness against the documented features
 * of Comctl32.dll version 6.0 on Sep. 12, 2004, by Filip Navara.
 * 
 * Unless otherwise noted, we believe this code to be complete, as per
 * the specification mentioned above.
 * If you discover missing features, or bugs, please note them below.
 *
30
 * TODO:
31
 *   - Tab order
Filip Navara's avatar
Filip Navara committed
32 33 34 35 36 37 38
 *   - Wizard 97 header resizing
 *   - Enforcing of minimal wizard size
 *   - Messages:
 *     o PSM_INSERTPAGE
 *     o PSM_RECALCPAGESIZES
 *     o PSM_SETHEADERSUBTITLE
 *     o PSM_SETHEADERTITLE
39 40
 *     o WM_HELP
 *     o WM_CONTEXTMENU
Filip Navara's avatar
Filip Navara committed
41 42 43 44 45 46 47 48 49 50 51 52
 *   - Notifications:
 *     o PSN_GETOBJECT
 *     o PSN_QUERYINITIALFOCUS
 *     o PSN_TRANSLATEACCELERATOR
 *   - Styles:
 *     o PSH_RTLREADING
 *     o PSH_STRETCHWATERMARK
 *     o PSH_USEPAGELANG
 *     o PSH_USEPSTARTPAGE
 *   - Page styles:
 *     o PSP_USEFUSIONCONTEXT
 *     o PSP_USEREFPARENT
53 54
 */

55
#include <stdarg.h>
56
#include <string.h>
57

58 59
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
60
#include "windef.h"
61
#include "winbase.h"
62 63 64
#include "wingdi.h"
#include "winuser.h"
#include "winnls.h"
65
#include "commctrl.h"
66
#include "prsht.h"
67
#include "comctl32.h"
68
#include "uxtheme.h"
69

70 71
#include "wine/debug.h"
#include "wine/unicode.h"
72 73 74 75

/******************************************************************************
 * Data structures
 */
76 77
#include "pshpack2.h"

78 79 80 81 82 83 84 85 86
typedef struct
{
  WORD dlgVer;
  WORD signature;
  DWORD helpID;
  DWORD exStyle;
  DWORD style;
} MyDLGTEMPLATEEX;

87 88 89 90 91 92 93
typedef struct
{
  DWORD helpid;
  DWORD exStyle;
  DWORD style;
  short x;
  short y;
94
  short cx;
95 96 97 98 99
  short cy;
  DWORD id;
} MyDLGITEMTEMPLATEEX;
#include "poppack.h"

100 101
typedef struct tagPropPageInfo
{
102
  HPROPSHEETPAGE hpage; /* to keep track of pages not passed to PropertySheet */
103 104 105 106 107
  HWND hwndPage;
  BOOL isDirty;
  LPCWSTR pszText;
  BOOL hasHelp;
  BOOL useCallback;
Alexandre Julliard's avatar
Alexandre Julliard committed
108
  BOOL hasIcon;
109 110 111 112
} PropPageInfo;

typedef struct tagPropSheetInfo
{
113
  HWND hwnd;
114
  PROPSHEETHEADERW ppshheader;
115
  BOOL unicode;
116
  LPWSTR strPropertiesFor;
117
  int nPages;
118 119 120 121
  int active_page;
  BOOL isModeless;
  BOOL hasHelp;
  BOOL hasApply;
122
  BOOL hasFinish;
123
  BOOL usePropPage;
124
  BOOL useCallback;
Alexandre Julliard's avatar
Alexandre Julliard committed
125
  BOOL activeValid;
126
  PropPageInfo* proppage;
127 128
  HFONT hFont;
  HFONT hFontBold;
129 130
  int width;
  int height;
131
  HIMAGELIST hImageList;
132
  BOOL ended;
133
  INT result;
134 135 136 137 138 139 140 141 142 143 144
} PropSheetInfo;

typedef struct
{
  int x;
  int y;
} PADDING_INFO;

/******************************************************************************
 * Defines and global variables
 */
145

146
static const WCHAR PropSheetInfoStr[] =
147
    {'P','r','o','p','e','r','t','y','S','h','e','e','t','I','n','f','o',0 };
148

149 150
#define PSP_INTERNAL_UNICODE 0x80000000

151
#define MAX_CAPTION_LENGTH 255
152
#define MAX_TABTEXT_LENGTH 255
Alexandre Julliard's avatar
Alexandre Julliard committed
153
#define MAX_BUTTONTEXT_LENGTH 64
154

155 156
#define INTRNL_ANY_WIZARD (PSH_WIZARD | PSH_WIZARD97_OLD | PSH_WIZARD97_NEW | PSH_WIZARD_LITE)

Filip Navara's avatar
Filip Navara committed
157 158 159 160
/* Wizard metrics specified in DLUs */
#define WIZARD_PADDING 7
#define WIZARD_HEADER_HEIGHT 36
                         	
161 162 163 164
/******************************************************************************
 * Prototypes
 */
static PADDING_INFO PROPSHEET_GetPaddingInfo(HWND hwndDlg);
165
static void PROPSHEET_SetTitleW(HWND hwndDlg, DWORD dwStyle, LPCWSTR lpszText);
166
static BOOL PROPSHEET_CanSetCurSel(HWND hwndDlg);
167 168
static BOOL PROPSHEET_SetCurSel(HWND hwndDlg,
                                int index,
169
                                int skipdir,
170
                                HPROPSHEETPAGE hpage);
171
static int PROPSHEET_GetPageIndex(HPROPSHEETPAGE hpage, const PropSheetInfo* psInfo);
172
static PADDING_INFO PROPSHEET_GetPaddingInfoWizard(HWND hwndDlg, const PropSheetInfo* psInfo);
173
static BOOL PROPSHEET_DoCommand(HWND hwnd, WORD wID);
174

175
static INT_PTR CALLBACK
176 177
PROPSHEET_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

178
WINE_DEFAULT_DEBUG_CHANNEL(propsheet);
179

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
#define add_flag(a) if (dwFlags & a) {strcat(string, #a );strcat(string," ");}
/******************************************************************************
 *            PROPSHEET_UnImplementedFlags
 *
 * Document use of flags we don't implement yet.
 */
static VOID PROPSHEET_UnImplementedFlags(DWORD dwFlags)
{
    CHAR string[256];

    string[0] = '\0';

  /*
   * unhandled header flags:
   *  PSH_RTLREADING         0x00000800
   *  PSH_STRETCHWATERMARK   0x00040000
   *  PSH_USEPAGELANG        0x00200000
   */

    add_flag(PSH_RTLREADING);
    add_flag(PSH_STRETCHWATERMARK);
    add_flag(PSH_USEPAGELANG);
    if (string[0] != '\0')
	FIXME("%s\n", string);
}
205
#undef add_flag
206

207 208 209 210 211
/******************************************************************************
 *            PROPSHEET_GetPageRect
 *
 * Retrieve rect from tab control and map into the dialog for SetWindowPos
 */
Filip Navara's avatar
Filip Navara committed
212 213
static void PROPSHEET_GetPageRect(const PropSheetInfo * psInfo, HWND hwndDlg,
                                  RECT *rc, LPCPROPSHEETPAGEW ppshpage)
214
{
Filip Navara's avatar
Filip Navara committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD) {     
        HWND hwndChild;
        RECT r;

        if (((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
             (psInfo->ppshheader.dwFlags & PSH_HEADER) &&
             !(ppshpage->dwFlags & PSP_HIDEHEADER)) ||
            (psInfo->ppshheader.dwFlags & PSH_WIZARD))
        {
            rc->left = rc->top = WIZARD_PADDING;
        }
        else
        {
            rc->left = rc->top = 0;
        }
        rc->right = psInfo->width - rc->left;
        rc->bottom = psInfo->height - rc->top;
        MapDialogRect(hwndDlg, rc);
233

Filip Navara's avatar
Filip Navara committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
        if ((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
            (psInfo->ppshheader.dwFlags & PSH_HEADER) &&
            !(ppshpage->dwFlags & PSP_HIDEHEADER))
        {
            hwndChild = GetDlgItem(hwndDlg, IDC_SUNKEN_LINEHEADER);
            GetClientRect(hwndChild, &r);
            MapWindowPoints(hwndChild, hwndDlg, (LPPOINT) &r, 2);
            rc->top += r.bottom + 1;
        }
    } else {
        HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
        GetClientRect(hwndTabCtrl, rc);
        SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, FALSE, (LPARAM)rc);
        MapWindowPoints(hwndTabCtrl, hwndDlg, (LPPOINT)rc, 2);
    }
249 250
}

251 252 253 254 255
/******************************************************************************
 *            PROPSHEET_FindPageByResId
 *
 * Find page index corresponding to page resource id.
 */
256
static INT PROPSHEET_FindPageByResId(const PropSheetInfo * psInfo, LRESULT resId)
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
{
   INT i;

   for (i = 0; i < psInfo->nPages; i++)
   {
      LPCPROPSHEETPAGEA lppsp = (LPCPROPSHEETPAGEA)psInfo->proppage[i].hpage;

      /* Fixme: if resource ID is a string shall we use strcmp ??? */
      if (lppsp->u.pszTemplate == (LPVOID)resId)
         break;
   }

   return i;
}

272 273 274 275 276 277 278 279
/******************************************************************************
 *            PROPSHEET_AtoW
 *
 * Convert ASCII to Unicode since all data is saved as Unicode.
 */
static void PROPSHEET_AtoW(LPCWSTR *tostr, LPCSTR frstr)
{
    INT len;
280
    WCHAR *to;
281 282 283

    TRACE("<%s>\n", frstr);
    len = MultiByteToWideChar(CP_ACP, 0, frstr, -1, 0, 0);
284 285 286
    to = Alloc(len * sizeof(WCHAR));
    MultiByteToWideChar(CP_ACP, 0, frstr, -1, to, len);
    *tostr = to;
287 288
}

289 290 291 292 293 294 295 296 297 298 299 300 301
/******************************************************************************
 *            PROPSHEET_CollectSheetInfoCommon
 *
 * Common code for PROPSHEET_CollectSheetInfoA/W
 */
static void PROPSHEET_CollectSheetInfoCommon(PropSheetInfo * psInfo, DWORD dwFlags)
{
  PROPSHEET_UnImplementedFlags(dwFlags);

  psInfo->hasHelp = dwFlags & PSH_HASHELP;
  psInfo->hasApply = !(dwFlags & PSH_NOAPPLYNOW);
  psInfo->hasFinish = dwFlags & PSH_WIZARDHASFINISH;
  psInfo->isModeless = dwFlags & PSH_MODELESS;
302
  psInfo->usePropPage = dwFlags & PSH_PROPSHEETPAGE;
303 304 305 306 307 308 309 310
  if (psInfo->active_page < 0 || psInfo->active_page >= psInfo->nPages)
     psInfo->active_page = 0;

  psInfo->result = 0;
  psInfo->hImageList = 0;
  psInfo->activeValid = FALSE;
}

311
/******************************************************************************
312
 *            PROPSHEET_CollectSheetInfoA
313 314 315
 *
 * Collect relevant data.
 */
316
static void PROPSHEET_CollectSheetInfoA(LPCPROPSHEETHEADERA lppsh,
317 318
                                       PropSheetInfo * psInfo)
{
319
  DWORD dwSize = min(lppsh->dwSize,sizeof(PROPSHEETHEADERA));
320 321
  DWORD dwFlags = lppsh->dwFlags;

322
  psInfo->useCallback = (dwFlags & PSH_USECALLBACK )&& (lppsh->pfnCallback);
Alexandre Julliard's avatar
Alexandre Julliard committed
323

324
  memcpy(&psInfo->ppshheader,lppsh,dwSize);
325
  TRACE("\n** PROPSHEETHEADER **\ndwSize\t\t%d\ndwFlags\t\t%08x\nhwndParent\t%p\nhInstance\t%p\npszCaption\t'%s'\nnPages\t\t%d\npfnCallback\t%p\n",
326 327 328
	lppsh->dwSize, lppsh->dwFlags, lppsh->hwndParent, lppsh->hInstance,
	debugstr_a(lppsh->pszCaption), lppsh->nPages, lppsh->pfnCallback);

329 330 331
  if (lppsh->dwFlags & INTRNL_ANY_WIZARD)
     psInfo->ppshheader.pszCaption = NULL;
  else
332
  {
333
     if (!IS_INTRESOURCE(lppsh->pszCaption))
334
     {
335
        int len = MultiByteToWideChar(CP_ACP, 0, lppsh->pszCaption, -1, NULL, 0);
336 337 338 339
        WCHAR *caption = Alloc( len*sizeof (WCHAR) );

        MultiByteToWideChar(CP_ACP, 0, lppsh->pszCaption, -1, caption, len);
        psInfo->ppshheader.pszCaption = caption;
340
     }
341
  }
342
  psInfo->nPages = lppsh->nPages;
343 344 345

  if (dwFlags & PSH_USEPSTARTPAGE)
  {
346
    TRACE("PSH_USEPSTARTPAGE is on\n");
347 348 349 350 351
    psInfo->active_page = 0;
  }
  else
    psInfo->active_page = lppsh->u2.nStartPage;

352
  PROPSHEET_CollectSheetInfoCommon(psInfo, dwFlags);
353 354 355
}

/******************************************************************************
356 357 358 359
 *            PROPSHEET_CollectSheetInfoW
 *
 * Collect relevant data.
 */
360
static void PROPSHEET_CollectSheetInfoW(LPCPROPSHEETHEADERW lppsh,
361 362 363 364 365
                                       PropSheetInfo * psInfo)
{
  DWORD dwSize = min(lppsh->dwSize,sizeof(PROPSHEETHEADERW));
  DWORD dwFlags = lppsh->dwFlags;

366
  psInfo->useCallback = (dwFlags & PSH_USECALLBACK) && (lppsh->pfnCallback);
367 368

  memcpy(&psInfo->ppshheader,lppsh,dwSize);
369
  TRACE("\n** PROPSHEETHEADER **\ndwSize\t\t%d\ndwFlags\t\t%08x\nhwndParent\t%p\nhInstance\t%p\npszCaption\t%s\nnPages\t\t%d\npfnCallback\t%p\n",
370 371
      lppsh->dwSize, lppsh->dwFlags, lppsh->hwndParent, lppsh->hInstance, debugstr_w(lppsh->pszCaption), lppsh->nPages, lppsh->pfnCallback);

372 373 374
  if (lppsh->dwFlags & INTRNL_ANY_WIZARD)
     psInfo->ppshheader.pszCaption = NULL;
  else
375
  {
376
     if (!IS_INTRESOURCE(lppsh->pszCaption))
377 378
     {
        int len = strlenW(lppsh->pszCaption);
379 380 381
        WCHAR *caption = Alloc( (len+1)*sizeof(WCHAR) );

        psInfo->ppshheader.pszCaption = strcpyW( caption, lppsh->pszCaption );
382
     }
383 384 385 386 387
  }
  psInfo->nPages = lppsh->nPages;

  if (dwFlags & PSH_USEPSTARTPAGE)
  {
388
    TRACE("PSH_USEPSTARTPAGE is on\n");
389 390 391 392 393
    psInfo->active_page = 0;
  }
  else
    psInfo->active_page = lppsh->u2.nStartPage;

394
  PROPSHEET_CollectSheetInfoCommon(psInfo, dwFlags);
395 396 397
}

/******************************************************************************
398
 *            PROPSHEET_CollectPageInfo
399 400 401 402
 *
 * Collect property sheet data.
 * With code taken from DIALOG_ParseTemplate32.
 */
403
static BOOL PROPSHEET_CollectPageInfo(LPCPROPSHEETPAGEW lppsp,
404
                               PropSheetInfo * psInfo,
405
                               int index, BOOL resize)
406
{
407
  const DLGTEMPLATE* pTemplate;
408 409 410 411
  const WORD*  p;
  DWORD dwFlags;
  int width, height;

412 413 414
  if (!lppsp)
    return FALSE;

415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
  TRACE("\n");
  psInfo->proppage[index].hpage = (HPROPSHEETPAGE)lppsp;
  psInfo->proppage[index].hwndPage = 0;
  psInfo->proppage[index].isDirty = FALSE;

  /*
   * Process property page flags.
   */
  dwFlags = lppsp->dwFlags;
  psInfo->proppage[index].useCallback = (dwFlags & PSP_USECALLBACK) && (lppsp->pfnCallback);
  psInfo->proppage[index].hasHelp = dwFlags & PSP_HASHELP;
  psInfo->proppage[index].hasIcon = dwFlags & (PSP_USEHICON | PSP_USEICONID);

  /* as soon as we have a page with the help flag, set the sheet flag on */
  if (psInfo->proppage[index].hasHelp)
    psInfo->hasHelp = TRUE;

  /*
   * Process page template.
   */
  if (dwFlags & PSP_DLGINDIRECT)
436
    pTemplate = lppsp->u.pResource;
437
  else if(dwFlags & PSP_INTERNAL_UNICODE )
438 439 440
  {
    HRSRC hResource = FindResourceW(lppsp->hInstance,
                                    lppsp->u.pszTemplate,
441
                                    (LPWSTR)RT_DIALOG);
442 443
    HGLOBAL hTemplate = LoadResource(lppsp->hInstance,
                                     hResource);
444
    pTemplate = LockResource(hTemplate);
445
  }
446 447 448
  else
  {
    HRSRC hResource = FindResourceA(lppsp->hInstance,
449
                                    (LPCSTR)lppsp->u.pszTemplate,
450
                                    (LPSTR)RT_DIALOG);
451 452
    HGLOBAL hTemplate = LoadResource(lppsp->hInstance,
                                     hResource);
453
    pTemplate = LockResource(hTemplate);
454
  }
455 456 457 458 459 460 461 462 463

  /*
   * Extract the size of the page and the caption.
   */
  if (!pTemplate)
      return FALSE;

  p = (const WORD *)pTemplate;

464
  if (((const MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF)
465
  {
466
    /* DLGTEMPLATEEX (not defined in any std. header file) */
467 468 469 470 471 472 473 474 475

    p++;       /* dlgVer    */
    p++;       /* signature */
    p += 2;    /* help ID   */
    p += 2;    /* ext style */
    p += 2;    /* style     */
  }
  else
  {
476
    /* DLGTEMPLATE */
477 478 479 480 481 482 483 484 485 486 487

    p += 2;    /* style     */
    p += 2;    /* ext style */
  }

  p++;    /* nb items */
  p++;    /*   x      */
  p++;    /*   y      */
  width  = (WORD)*p; p++;
  height = (WORD)*p; p++;

Filip Navara's avatar
Filip Navara committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
  /* Special calculation for interior wizard pages so the largest page is
   * calculated correctly. We need to add all the padding and space occupied
   * by the header so the width and height sums up to the whole wizard client
   * area. */
  if ((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
      (psInfo->ppshheader.dwFlags & PSH_HEADER) &&
      !(dwFlags & PSP_HIDEHEADER))
  {
      height += 2 * WIZARD_PADDING + WIZARD_HEADER_HEIGHT;
      width += 2 * WIZARD_PADDING;
  }
  if (psInfo->ppshheader.dwFlags & PSH_WIZARD)
  {
      height += 2 * WIZARD_PADDING;
      width += 2 * WIZARD_PADDING;
  }

505
  /* remember the largest width and height */
506 507 508 509
  if (resize)
  {
      if (width > psInfo->width)
        psInfo->width = width;
510

511 512 513
      if (height > psInfo->height)
        psInfo->height = height;
  }
514 515 516 517 518 519 520 521 522 523 524

  /* menu */
  switch ((WORD)*p)
  {
    case 0x0000:
      p++;
      break;
    case 0xffff:
      p += 2;
      break;
    default:
525
      p += lstrlenW( p ) + 1;
526
      break;
527
  }
528 529 530 531 532 533 534 535 536 537 538

  /* class */
  switch ((WORD)*p)
  {
    case 0x0000:
      p++;
      break;
    case 0xffff:
      p += 2;
      break;
    default:
539
      p += lstrlenW( p ) + 1;
540 541 542 543
      break;
  }

  /* Extract the caption */
544 545
  psInfo->proppage[index].pszText = p;
  TRACE("Tab %d %s\n",index,debugstr_w( p ));
546 547 548 549 550

  if (dwFlags & PSP_USETITLE)
  {
    WCHAR szTitle[256];
    const WCHAR *pTitle;
551
    static const WCHAR pszNull[] = { '(','n','u','l','l',')',0 };
552
    WCHAR *text;
553 554
    int len;

555
    if (IS_INTRESOURCE( lppsp->pszTitle ))
556
    {
557
      if (!LoadStringW( lppsp->hInstance, (DWORD_PTR)lppsp->pszTitle,szTitle,sizeof(szTitle)/sizeof(szTitle[0]) ))
558 559 560 561 562 563 564 565 566 567
      {
        pTitle = pszNull;
	FIXME("Could not load resource #%04x?\n",LOWORD(lppsp->pszTitle));
      }
      else
        pTitle = szTitle;
    }
    else
      pTitle = lppsp->pszTitle;

568
    len = strlenW(pTitle);
569 570
    text = Alloc( (len+1)*sizeof (WCHAR) );
    psInfo->proppage[index].pszText = strcpyW( text, pTitle);
571 572 573 574 575
  }

  /*
   * Build the image list for icons
   */
576
  if ((dwFlags & PSP_USEHICON) || (dwFlags & PSP_USEICONID))
577 578 579 580 581 582
  {
    HICON hIcon;
    int icon_cx = GetSystemMetrics(SM_CXSMICON);
    int icon_cy = GetSystemMetrics(SM_CYSMICON);

    if (dwFlags & PSP_USEICONID)
583
      hIcon = LoadImageW(lppsp->hInstance, lppsp->u2.pszIcon, IMAGE_ICON,
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
                         icon_cx, icon_cy, LR_DEFAULTCOLOR);
    else
      hIcon = lppsp->u2.hIcon;

    if ( hIcon )
    {
      if (psInfo->hImageList == 0 )
	psInfo->hImageList = ImageList_Create(icon_cx, icon_cy, ILC_COLOR, 1, 1);

      ImageList_AddIcon(psInfo->hImageList, hIcon);
    }

  }

  return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
600

601 602 603 604 605
/******************************************************************************
 *            PROPSHEET_CreateDialog
 *
 * Creates the actual property sheet.
 */
606
static INT_PTR PROPSHEET_CreateDialog(PropSheetInfo* psInfo)
607 608 609
{
  LRESULT ret;
  LPCVOID template;
610
  LPVOID temp = 0;
611
  HRSRC hRes;
612
  DWORD resSize;
613 614
  WORD resID = IDD_PROPSHEET;

615
  TRACE("(%p)\n", psInfo);
616
  if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
617
    resID = IDD_WIZARD;
618

619 620 621
  if( psInfo->unicode )
  {
    if(!(hRes = FindResourceW(COMCTL32_hModule,
622
                            MAKEINTRESOURCEW(resID),
623
                            (LPWSTR)RT_DIALOG)))
624 625 626 627 628 629
      return -1;
  }
  else
  {
    if(!(hRes = FindResourceA(COMCTL32_hModule,
                            MAKEINTRESOURCEA(resID),
630
                            (LPSTR)RT_DIALOG)))
631 632
      return -1;
  }
633

634
  if(!(template = LoadResource(COMCTL32_hModule, hRes)))
635
    return -1;
636

637 638 639 640 641
  /*
   * Make a copy of the dialog template.
   */
  resSize = SizeofResource(COMCTL32_hModule, hRes);

642
  temp = Alloc(resSize);
643 644

  if (!temp)
645
    return -1;
646 647 648

  memcpy(temp, template, resSize);

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
  if (psInfo->ppshheader.dwFlags & PSH_NOCONTEXTHELP)
  {
    if (((MyDLGTEMPLATEEX*)temp)->signature == 0xFFFF)
      ((MyDLGTEMPLATEEX*)temp)->style &= ~DS_CONTEXTHELP;
    else
      ((DLGTEMPLATE*)temp)->style &= ~DS_CONTEXTHELP;
  }
  if ((psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD) &&
      (psInfo->ppshheader.dwFlags & PSH_WIZARDCONTEXTHELP))
  {
    if (((MyDLGTEMPLATEEX*)temp)->signature == 0xFFFF)
      ((MyDLGTEMPLATEEX*)temp)->style |= DS_CONTEXTHELP;
    else
      ((DLGTEMPLATE*)temp)->style |= DS_CONTEXTHELP;
  }

665
  if (psInfo->useCallback)
666
    (*(psInfo->ppshheader.pfnCallback))(0, PSCB_PRECREATE, (LPARAM)temp);
667

668 669 670 671
  /* NOTE: MSDN states "Returns a positive value if successful, or -1
   * otherwise for modal property sheets.", but this is wrong. The
   * actual return value is either TRUE (success), FALSE (cancel) or
   * -1 (error). */
672 673
  if( psInfo->unicode )
  {
Frank Richter's avatar
Frank Richter committed
674
    ret = (INT_PTR)CreateDialogIndirectParamW(psInfo->ppshheader.hInstance,
675 676
                                          temp, psInfo->ppshheader.hwndParent,
                                          PROPSHEET_DialogProc, (LPARAM)psInfo);
677
    if ( !ret ) ret = -1;
678 679 680
  }
  else
  {
Frank Richter's avatar
Frank Richter committed
681
    ret = (INT_PTR)CreateDialogIndirectParamA(psInfo->ppshheader.hInstance,
682 683
                                          temp, psInfo->ppshheader.hwndParent,
                                          PROPSHEET_DialogProc, (LPARAM)psInfo);
684
    if ( !ret ) ret = -1;
685
  }
686

687
  Free(temp);
688

689 690 691 692
  return ret;
}

/******************************************************************************
693
 *            PROPSHEET_SizeMismatch
694
 *
695 696
 *     Verify that the tab control and the "largest" property sheet page dlg. template
 *     match in size.
697
 */
698
static BOOL PROPSHEET_SizeMismatch(HWND hwndDlg, const PropSheetInfo* psInfo)
699 700 701 702 703 704 705 706
{
  HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
  RECT rcOrigTab, rcPage;

  /*
   * Original tab size.
   */
  GetClientRect(hwndTabCtrl, &rcOrigTab);
707
  TRACE("orig tab %s\n", wine_dbgstr_rect(&rcOrigTab));
708 709 710 711

  /*
   * Biggest page size.
   */
Filip Navara's avatar
Filip Navara committed
712 713
  rcPage.left   = 0;
  rcPage.top    = 0;
714 715 716 717
  rcPage.right  = psInfo->width;
  rcPage.bottom = psInfo->height;

  MapDialogRect(hwndDlg, &rcPage);
718
  TRACE("biggest page %s\n", wine_dbgstr_rect(&rcPage));
719

720
  if ( (rcPage.right - rcPage.left) != (rcOrigTab.right - rcOrigTab.left) )
721
    return TRUE;
722
  if ( (rcPage.bottom - rcPage.top) != (rcOrigTab.bottom - rcOrigTab.top) )
723 724 725 726 727 728 729 730 731 732 733 734 735 736
    return TRUE;

  return FALSE;
}

/******************************************************************************
 *            PROPSHEET_AdjustSize
 *
 * Resizes the property sheet and the tab control to fit the largest page.
 */
static BOOL PROPSHEET_AdjustSize(HWND hwndDlg, PropSheetInfo* psInfo)
{
  HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
  HWND hwndButton = GetDlgItem(hwndDlg, IDOK);
737
  RECT rc,tabRect;
738
  int buttonHeight;
739
  PADDING_INFO padding = PROPSHEET_GetPaddingInfo(hwndDlg);
740
  RECT units;
741
  LONG style;
742 743 744 745 746 747 748 749

  /* Get the height of buttons */
  GetClientRect(hwndButton, &rc);
  buttonHeight = rc.bottom;

  /*
   * Biggest page size.
   */
Filip Navara's avatar
Filip Navara committed
750 751
  rc.left   = 0;
  rc.top    = 0;
752 753 754 755 756
  rc.right  = psInfo->width;
  rc.bottom = psInfo->height;

  MapDialogRect(hwndDlg, &rc);

757 758 759 760 761
  /* retrieve the dialog units */
  units.left = units.right = 4;
  units.top = units.bottom = 8;
  MapDialogRect(hwndDlg, &units);

762 763 764
  /*
   * Resize the tab control.
   */
765 766
  GetClientRect(hwndTabCtrl,&tabRect);

767
  SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, FALSE, (LPARAM)&tabRect);
768 769 770 771

  if ((rc.bottom - rc.top) < (tabRect.bottom - tabRect.top))
  {
      rc.bottom = rc.top + tabRect.bottom - tabRect.top;
772
      psInfo->height = MulDiv((rc.bottom - rc.top),8,units.top);
773
  }
774

775 776 777
  if ((rc.right - rc.left) < (tabRect.right - tabRect.left))
  {
      rc.right = rc.left + tabRect.right - tabRect.left;
778
      psInfo->width  = MulDiv((rc.right - rc.left),4,units.left);
779 780
  }

781
  SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, TRUE, (LPARAM)&rc);
782 783 784

  rc.right -= rc.left;
  rc.bottom -= rc.top;
785
  TRACE("setting tab %p, rc (0,0)-(%d,%d)\n",
Frank Richter's avatar
Frank Richter committed
786
        hwndTabCtrl, rc.right, rc.bottom);
787 788 789 790 791
  SetWindowPos(hwndTabCtrl, 0, 0, 0, rc.right, rc.bottom,
               SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);

  GetClientRect(hwndTabCtrl, &rc);

792
  TRACE("tab client rc %s\n", wine_dbgstr_rect(&rc));
793

794 795 796
  rc.right += (padding.x * 2);
  rc.bottom += buttonHeight + (3 * padding.y);

797 798 799
  style = GetWindowLongW(hwndDlg, GWL_STYLE);
  if (!(style & WS_CHILD))
    AdjustWindowRect(&rc, style, FALSE);
800 801 802

  rc.right -= rc.left;
  rc.bottom -= rc.top;
803 804 805 806

  /*
   * Resize the property sheet.
   */
807
  TRACE("setting dialog %p, rc (0,0)-(%d,%d)\n",
Frank Richter's avatar
Frank Richter committed
808
        hwndDlg, rc.right, rc.bottom);
809 810 811 812 813
  SetWindowPos(hwndDlg, 0, 0, 0, rc.right, rc.bottom,
               SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  return TRUE;
}

814 815 816 817 818
/******************************************************************************
 *            PROPSHEET_AdjustSizeWizard
 *
 * Resizes the property sheet to fit the largest page.
 */
819
static BOOL PROPSHEET_AdjustSizeWizard(HWND hwndDlg, const PropSheetInfo* psInfo)
820 821
{
  HWND hwndLine = GetDlgItem(hwndDlg, IDC_SUNKEN_LINE);
Filip Navara's avatar
Filip Navara committed
822
  RECT rc, lineRect, dialogRect;
823

Filip Navara's avatar
Filip Navara committed
824 825 826
  /* Biggest page size */
  rc.left   = 0;
  rc.top    = 0;
827 828 829 830
  rc.right  = psInfo->width;
  rc.bottom = psInfo->height;
  MapDialogRect(hwndDlg, &rc);

831
  TRACE("Biggest page %s\n", wine_dbgstr_rect(&rc));
832

Filip Navara's avatar
Filip Navara committed
833 834 835 836 837
  /* Add space for the buttons row */
  GetWindowRect(hwndLine, &lineRect);
  MapWindowPoints(NULL, hwndDlg, (LPPOINT)&lineRect, 2);
  GetClientRect(hwndDlg, &dialogRect);
  rc.bottom += dialogRect.bottom - lineRect.top - 1;
838

Filip Navara's avatar
Filip Navara committed
839 840 841 842
  /* Convert the client coordinates to window coordinates */
  AdjustWindowRect(&rc, GetWindowLongW(hwndDlg, GWL_STYLE), FALSE);

  /* Resize the property sheet */
843
  TRACE("setting dialog %p, rc (0,0)-(%d,%d)\n",
Frank Richter's avatar
Frank Richter committed
844
        hwndDlg, rc.right, rc.bottom);
Filip Navara's avatar
Filip Navara committed
845
  SetWindowPos(hwndDlg, 0, 0, 0, rc.right - rc.left, rc.bottom - rc.top,
846
               SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
Filip Navara's avatar
Filip Navara committed
847

848 849 850
  return TRUE;
}

851 852 853 854 855
/******************************************************************************
 *            PROPSHEET_AdjustButtons
 *
 * Adjusts the buttons' positions.
 */
856
static BOOL PROPSHEET_AdjustButtons(HWND hwndParent, const PropSheetInfo* psInfo)
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
{
  HWND hwndButton = GetDlgItem(hwndParent, IDOK);
  RECT rcSheet;
  int x, y;
  int num_buttons = 2;
  int buttonWidth, buttonHeight;
  PADDING_INFO padding = PROPSHEET_GetPaddingInfo(hwndParent);

  if (psInfo->hasApply)
    num_buttons++;

  if (psInfo->hasHelp)
    num_buttons++;

  /*
   * Obtain the size of the buttons.
   */
  GetClientRect(hwndButton, &rcSheet);
  buttonWidth = rcSheet.right;
  buttonHeight = rcSheet.bottom;

  /*
   * Get the size of the property sheet.
880
   */
881 882
  GetClientRect(hwndParent, &rcSheet);

883
  /*
884 885 886 887 888
   * All buttons will be at this y coordinate.
   */
  y = rcSheet.bottom - (padding.y + buttonHeight);

  /*
889
   * Position OK button and make it default.
890 891 892 893 894 895 896 897
   */
  hwndButton = GetDlgItem(hwndParent, IDOK);

  x = rcSheet.right - ((padding.x + buttonWidth) * num_buttons);

  SetWindowPos(hwndButton, 0, x, y, 0, 0,
               SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

898
  SendMessageW(hwndParent, DM_SETDEFID, IDOK, 0);
899 900


901 902 903 904 905
  /*
   * Position Cancel button.
   */
  hwndButton = GetDlgItem(hwndParent, IDCANCEL);

906
  x += padding.x + buttonWidth;
907 908 909 910 911 912 913 914 915

  SetWindowPos(hwndButton, 0, x, y, 0, 0,
               SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

  /*
   * Position Apply button.
   */
  hwndButton = GetDlgItem(hwndParent, IDC_APPLY_BUTTON);

916 917
  if(psInfo->hasApply)
    x += padding.x + buttonWidth;
918 919 920
  else
    ShowWindow(hwndButton, SW_HIDE);

921 922 923 924
  SetWindowPos(hwndButton, 0, x, y, 0, 0,
              SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  EnableWindow(hwndButton, FALSE);

925 926 927 928 929
  /*
   * Position Help button.
   */
  hwndButton = GetDlgItem(hwndParent, IDHELP);

930 931 932
  x += padding.x + buttonWidth;
  SetWindowPos(hwndButton, 0, x, y, 0, 0,
              SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
933

934
  if(!psInfo->hasHelp)
935 936 937 938 939
    ShowWindow(hwndButton, SW_HIDE);

  return TRUE;
}

940 941 942 943 944 945
/******************************************************************************
 *            PROPSHEET_AdjustButtonsWizard
 *
 * Adjusts the buttons' positions.
 */
static BOOL PROPSHEET_AdjustButtonsWizard(HWND hwndParent,
946
                                          const PropSheetInfo* psInfo)
947 948 949
{
  HWND hwndButton = GetDlgItem(hwndParent, IDCANCEL);
  HWND hwndLine = GetDlgItem(hwndParent, IDC_SUNKEN_LINE);
950
  HWND hwndLineHeader = GetDlgItem(hwndParent, IDC_SUNKEN_LINEHEADER);
951 952 953 954
  RECT rcSheet;
  int x, y;
  int num_buttons = 3;
  int buttonWidth, buttonHeight, lineHeight, lineWidth;
955
  PADDING_INFO padding = PROPSHEET_GetPaddingInfoWizard(hwndParent, psInfo);
956 957 958

  if (psInfo->hasHelp)
    num_buttons++;
959 960
  if (psInfo->hasFinish)
    num_buttons++;
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980

  /*
   * Obtain the size of the buttons.
   */
  GetClientRect(hwndButton, &rcSheet);
  buttonWidth = rcSheet.right;
  buttonHeight = rcSheet.bottom;

  GetClientRect(hwndLine, &rcSheet);
  lineHeight = rcSheet.bottom;

  /*
   * Get the size of the property sheet.
   */
  GetClientRect(hwndParent, &rcSheet);

  /*
   * All buttons will be at this y coordinate.
   */
  y = rcSheet.bottom - (padding.y + buttonHeight);
981
  
982
  /*
983
   * Position the Back button.
984
   */
985
  hwndButton = GetDlgItem(hwndParent, IDC_BACK_BUTTON);
986

987
  x = rcSheet.right - ((padding.x + buttonWidth) * (num_buttons - 1)) - buttonWidth;
988 989 990 991

  SetWindowPos(hwndButton, 0, x, y, 0, 0,
               SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

992 993 994 995 996 997 998
  /*
   * Position the Next button.
   */
  hwndButton = GetDlgItem(hwndParent, IDC_NEXT_BUTTON);
  
  x += buttonWidth;
  
999 1000 1001 1002
  SetWindowPos(hwndButton, 0, x, y, 0, 0,
               SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

  /*
1003
   * Position the Finish button.
1004
   */
1005 1006 1007 1008
  hwndButton = GetDlgItem(hwndParent, IDC_FINISH_BUTTON);
  
  if (psInfo->hasFinish)
    x += padding.x + buttonWidth;
1009 1010 1011 1012

  SetWindowPos(hwndButton, 0, x, y, 0, 0,
               SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

1013 1014 1015
  if (!psInfo->hasFinish)
    ShowWindow(hwndButton, SW_HIDE);

1016 1017 1018 1019 1020
  /*
   * Position the Cancel button.
   */
  hwndButton = GetDlgItem(hwndParent, IDCANCEL);

1021
  x += padding.x + buttonWidth;
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032

  SetWindowPos(hwndButton, 0, x, y, 0, 0,
               SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

  /*
   * Position Help button.
   */
  hwndButton = GetDlgItem(hwndParent, IDHELP);

  if (psInfo->hasHelp)
  {
1033
    x += padding.x + buttonWidth;
1034 1035 1036 1037 1038 1039 1040

    SetWindowPos(hwndButton, 0, x, y, 0, 0,
                 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  }
  else
    ShowWindow(hwndButton, SW_HIDE);

Filip Navara's avatar
Filip Navara committed
1041 1042
  if (psInfo->ppshheader.dwFlags &
      (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW | PSH_WIZARD_LITE)) 
1043 1044
      padding.x = 0;

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
  /*
   * Position and resize the sunken line.
   */
  x = padding.x;
  y = rcSheet.bottom - ((padding.y * 2) + buttonHeight + lineHeight);

  lineWidth = rcSheet.right - (padding.x * 2);
  SetWindowPos(hwndLine, 0, x, y, lineWidth, 2,
               SWP_NOZORDER | SWP_NOACTIVATE);

1055 1056 1057 1058 1059 1060 1061 1062 1063
  /*
   * Position and resize the header sunken line.
   */
  
  SetWindowPos(hwndLineHeader, 0, 0, 0, rcSheet.right, 2,
	       SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
  if (!(psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)))
      ShowWindow(hwndLineHeader, SW_HIDE);

1064 1065 1066
  return TRUE;
}

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
/******************************************************************************
 *            PROPSHEET_GetPaddingInfo
 *
 * Returns the layout information.
 */
static PADDING_INFO PROPSHEET_GetPaddingInfo(HWND hwndDlg)
{
  HWND hwndTab = GetDlgItem(hwndDlg, IDC_TABCONTROL);
  RECT rcTab;
  PADDING_INFO padding;

  GetWindowRect(hwndTab, &rcTab);
1079
  MapWindowPoints( 0, hwndDlg, (POINT *)&rcTab, 2 );
1080

1081 1082
  padding.x = rcTab.left;
  padding.y = rcTab.top;
1083 1084 1085 1086

  return padding;
}

1087 1088 1089 1090 1091
/******************************************************************************
 *            PROPSHEET_GetPaddingInfoWizard
 *
 * Returns the layout information.
 * Vertical spacing is the distance between the line and the buttons.
1092 1093 1094 1095 1096 1097
 * Do NOT use the Help button to gather padding information when it isn't mapped
 * (PSH_HASHELP), as app writers aren't forced to supply correct coordinates
 * for it in this case !
 * FIXME: I'm not sure about any other coordinate problems with these evil
 * buttons. Fix it in case additional problems appear or maybe calculate
 * a padding in a completely different way, as this is somewhat messy.
1098
 */
1099 1100
static PADDING_INFO PROPSHEET_GetPaddingInfoWizard(HWND hwndDlg, const PropSheetInfo*
 psInfo)
1101 1102 1103 1104
{
  PADDING_INFO padding;
  RECT rc;
  HWND hwndControl;
1105 1106
  INT idButton;
  POINT ptButton, ptLine;
1107

1108
  TRACE("\n");
1109 1110 1111 1112 1113 1114
  if (psInfo->hasHelp)
  {
	idButton = IDHELP;
  }
  else
  {
1115
    if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
1116 1117 1118 1119 1120 1121 1122 1123 1124
    {
	idButton = IDC_NEXT_BUTTON;
    }
    else
    {
	/* hopefully this is ok */
	idButton = IDCANCEL;
    }
  }
1125

1126
  hwndControl = GetDlgItem(hwndDlg, idButton);
1127
  GetWindowRect(hwndControl, &rc);
1128
  MapWindowPoints( 0, hwndDlg, (POINT *)&rc, 2 );
1129 1130
  ptButton.x = rc.left;
  ptButton.y = rc.top;
1131 1132 1133 1134

  /* Line */
  hwndControl = GetDlgItem(hwndDlg, IDC_SUNKEN_LINE);
  GetWindowRect(hwndControl, &rc);
1135
  MapWindowPoints( 0, hwndDlg, (POINT *)&rc, 2 );
1136
  ptLine.x = rc.left;
1137 1138
  ptLine.y = rc.bottom;

1139 1140 1141 1142
  padding.y = ptButton.y - ptLine.y;

  if (padding.y < 0)
	  ERR("padding negative ! Please report this !\n");
1143

1144 1145
  /* this is most probably not correct, but the best we have now */
  padding.x = padding.y;
1146 1147 1148
  return padding;
}

1149 1150 1151 1152 1153 1154
/******************************************************************************
 *            PROPSHEET_CreateTabControl
 *
 * Insert the tabs in the tab control.
 */
static BOOL PROPSHEET_CreateTabControl(HWND hwndParent,
1155
                                       const PropSheetInfo * psInfo)
1156 1157
{
  HWND hwndTabCtrl = GetDlgItem(hwndParent, IDC_TABCONTROL);
1158
  TCITEMW item;
1159
  int i, nTabs;
Alexandre Julliard's avatar
Alexandre Julliard committed
1160
  int iImage = 0;
1161

1162
  TRACE("\n");
1163
  item.mask = TCIF_TEXT;
1164
  item.cchTextMax = MAX_TABTEXT_LENGTH;
1165

1166
  nTabs = psInfo->nPages;
1167

1168 1169 1170 1171 1172
  /*
   * Set the image list for icons.
   */
  if (psInfo->hImageList)
  {
1173
    SendMessageW(hwndTabCtrl, TCM_SETIMAGELIST, 0, (LPARAM)psInfo->hImageList);
1174 1175
  }

1176
  SendMessageW(GetDlgItem(hwndTabCtrl, IDC_TABCONTROL), WM_SETREDRAW, 0, 0);
1177 1178
  for (i = 0; i < nTabs; i++)
  {
Alexandre Julliard's avatar
Alexandre Julliard committed
1179 1180 1181 1182 1183 1184 1185 1186 1187
    if ( psInfo->proppage[i].hasIcon )
    {
      item.mask |= TCIF_IMAGE;
      item.iImage = iImage++;
    }
    else
    {
      item.mask &= ~TCIF_IMAGE;
    }
1188

1189
    item.pszText = (LPWSTR) psInfo->proppage[i].pszText;
1190
    SendMessageW(hwndTabCtrl, TCM_INSERTITEMW, i, (LPARAM)&item);
1191
  }
1192
  SendMessageW(GetDlgItem(hwndTabCtrl, IDC_TABCONTROL), WM_SETREDRAW, 1, 0);
1193 1194 1195

  return TRUE;
}
1196 1197 1198 1199

/******************************************************************************
 *            PROPSHEET_WizardSubclassProc
 *
1200
 * Subclassing window procedure for wizard exterior pages to prevent drawing
1201 1202
 * background and so drawing above the watermark.
 */
1203
static LRESULT CALLBACK
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
PROPSHEET_WizardSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uID, DWORD_PTR dwRef)
{
  switch (uMsg)
  {
    case WM_ERASEBKGND:
      return TRUE;

    case WM_CTLCOLORSTATIC:
      SetBkColor((HDC)wParam, GetSysColor(COLOR_WINDOW));
      return (INT_PTR)GetSysColorBrush(COLOR_WINDOW);
  }

  return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}

1219 1220 1221 1222
/*
 * Get the size of an in-memory Template
 *
 *( Based on the code of PROPSHEET_CollectPageInfo)
1223
 * See also dialog.c/DIALOG_ParseTemplate32().
1224 1225
 */

1226
static UINT GetTemplateSize(const DLGTEMPLATE* pTemplate)
1227 1228 1229

{
  const WORD*  p = (const WORD *)pTemplate;
1230
  BOOL  istemplateex = (((const MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF);
1231
  WORD nrofitems;
1232
  UINT ret;
1233 1234 1235

  if (istemplateex)
  {
1236
    /* DLGTEMPLATEEX (not defined in any std. header file) */
1237

1238
    TRACE("is DLGTEMPLATEEX\n");
1239 1240 1241 1242 1243 1244 1245 1246
    p++;       /* dlgVer    */
    p++;       /* signature */
    p += 2;    /* help ID   */
    p += 2;    /* ext style */
    p += 2;    /* style     */
  }
  else
  {
1247
    /* DLGTEMPLATE */
1248

1249
    TRACE("is DLGTEMPLATE\n");
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
    p += 2;    /* style     */
    p += 2;    /* ext style */
  }

  nrofitems =   (WORD)*p; p++;    /* nb items */
  p++;    /*   x      */
  p++;    /*   y      */
  p++;    /*   width  */
  p++;    /*   height */

  /* menu */
  switch ((WORD)*p)
  {
    case 0x0000:
      p++;
      break;
    case 0xffff:
      p += 2;
      break;
    default:
1270 1271
      TRACE("menu %s\n",debugstr_w( p ));
      p += lstrlenW( p ) + 1;
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
      break;
  }

  /* class */
  switch ((WORD)*p)
  {
    case 0x0000:
      p++;
      break;
    case 0xffff:
1282
      p += 2; /* 0xffff plus predefined window class ordinal value */
1283 1284
      break;
    default:
1285 1286
      TRACE("class %s\n",debugstr_w( p ));
      p += lstrlenW( p ) + 1;
1287 1288 1289
      break;
  }

1290
  /* title */
1291 1292
  TRACE("title %s\n",debugstr_w( p ));
  p += lstrlenW( p ) + 1;
1293

1294
  /* font, if DS_SETFONT set */
1295
  if ((DS_SETFONT & ((istemplateex)?  ((const MyDLGTEMPLATEEX*)pTemplate)->style :
1296 1297 1298
		     pTemplate->style)))
    {
      p+=(istemplateex)?3:1;
1299 1300
      TRACE("font %s\n",debugstr_w( p ));
      p += lstrlenW( p ) + 1; /* the font name */
1301 1302
    }

1303 1304
  /* now process the DLGITEMTEMPLATE(EX) structs (plus custom data)
   * that are following the DLGTEMPLATE(EX) data */
1305 1306 1307
  TRACE("%d items\n",nrofitems);
  while (nrofitems > 0)
    {
Frank Richter's avatar
Frank Richter committed
1308
      p = (WORD*)(((DWORD_PTR)p + 3) & ~3); /* DWORD align */
1309
      
1310
      /* skip header */
1311 1312
      p += (istemplateex ? sizeof(MyDLGITEMTEMPLATEEX) : sizeof(DLGITEMTEMPLATE))/sizeof(WORD);
      
1313
      /* check class */
1314 1315 1316 1317 1318 1319
      switch ((WORD)*p)
	{
	case 0x0000:
	  p++;
	  break;
	case 0xffff:
1320
          TRACE("class ordinal 0x%08x\n",*(const DWORD*)p);
1321 1322 1323
	  p += 2;
	  break;
	default:
1324 1325
	  TRACE("class %s\n",debugstr_w( p ));
	  p += lstrlenW( p ) + 1;
1326 1327
	  break;
	}
1328 1329

      /* check title text */
1330 1331 1332 1333 1334 1335
      switch ((WORD)*p)
	{
	case 0x0000:
	  p++;
	  break;
	case 0xffff:
1336
          TRACE("text ordinal 0x%08x\n",*(const DWORD*)p);
1337 1338 1339
	  p += 2;
	  break;
	default:
1340 1341
	  TRACE("text %s\n",debugstr_w( p ));
	  p += lstrlenW( p ) + 1;
1342 1343
	  break;
	}
1344
      p += *p / sizeof(WORD) + 1;    /* Skip extra data */
1345 1346 1347
      --nrofitems;
    }
  
1348
  ret = (p - (const WORD*)pTemplate) * sizeof(WORD);
1349 1350
  TRACE("%p %p size 0x%08x\n", p, pTemplate, ret);
  return ret;
1351
}
1352 1353 1354 1355

/******************************************************************************
 *            PROPSHEET_CreatePage
 *
1356
 * Creates a page.
1357
 */
1358
static BOOL PROPSHEET_CreatePage(HWND hwndParent,
1359
                                int index,
1360
                                const PropSheetInfo * psInfo,
1361
                                LPCPROPSHEETPAGEW ppshpage)
1362
{
1363
  const DLGTEMPLATE* pTemplate;
1364
  HWND hwndPage;
1365
  DWORD resSize;
1366
  DLGTEMPLATE* pTemplateCopy = NULL;
1367

1368
  TRACE("index %d\n", index);
1369

1370 1371 1372 1373 1374
  if (ppshpage == NULL)
  {
    return FALSE;
  }

1375
  if (ppshpage->dwFlags & PSP_DLGINDIRECT)
1376
    {
1377
      pTemplate = ppshpage->u.pResource;
1378 1379
      resSize = GetTemplateSize(pTemplate);
    }
1380
  else if(ppshpage->dwFlags & PSP_INTERNAL_UNICODE)
1381
  {
1382 1383 1384
    HRSRC hResource;
    HANDLE hTemplate;

1385
    hResource = FindResourceW(ppshpage->hInstance,
1386
                                    ppshpage->u.pszTemplate,
1387
                                    (LPWSTR)RT_DIALOG);
1388 1389 1390 1391 1392 1393 1394 1395 1396
    if(!hResource)
	return FALSE;

    resSize = SizeofResource(ppshpage->hInstance, hResource);

    hTemplate = LoadResource(ppshpage->hInstance, hResource);
    if(!hTemplate)
	return FALSE;

1397
    pTemplate = LockResource(hTemplate);
1398 1399 1400
    /*
     * Make a copy of the dialog template to make it writable
     */
1401
  }
1402 1403 1404 1405 1406 1407
  else
  {
    HRSRC hResource;
    HANDLE hTemplate;

    hResource = FindResourceA(ppshpage->hInstance,
1408
                                    (LPCSTR)ppshpage->u.pszTemplate,
1409
                                    (LPSTR)RT_DIALOG);
1410 1411 1412 1413 1414 1415 1416 1417 1418
    if(!hResource)
	return FALSE;

    resSize = SizeofResource(ppshpage->hInstance, hResource);

    hTemplate = LoadResource(ppshpage->hInstance, hResource);
    if(!hTemplate)
	return FALSE;

1419
    pTemplate = LockResource(hTemplate);
1420 1421 1422 1423
    /*
     * Make a copy of the dialog template to make it writable
     */
  }
1424 1425
  pTemplateCopy = Alloc(resSize);
  if (!pTemplateCopy)
1426 1427
    return FALSE;
  
1428 1429
  TRACE("copying pTemplate %p into pTemplateCopy %p (%d)\n", pTemplate, pTemplateCopy, resSize);
  memcpy(pTemplateCopy, pTemplate, resSize);
1430

1431
  if (((MyDLGTEMPLATEEX*)pTemplateCopy)->signature == 0xFFFF)
1432
  {
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
    ((MyDLGTEMPLATEEX*)pTemplateCopy)->style |= WS_CHILD | WS_TABSTOP | DS_CONTROL;
    ((MyDLGTEMPLATEEX*)pTemplateCopy)->style &= ~DS_MODALFRAME;
    ((MyDLGTEMPLATEEX*)pTemplateCopy)->style &= ~WS_CAPTION;
    ((MyDLGTEMPLATEEX*)pTemplateCopy)->style &= ~WS_SYSMENU;
    ((MyDLGTEMPLATEEX*)pTemplateCopy)->style &= ~WS_POPUP;
    ((MyDLGTEMPLATEEX*)pTemplateCopy)->style &= ~WS_DISABLED;
    ((MyDLGTEMPLATEEX*)pTemplateCopy)->style &= ~WS_VISIBLE;
    ((MyDLGTEMPLATEEX*)pTemplateCopy)->style &= ~WS_THICKFRAME;

    ((MyDLGTEMPLATEEX*)pTemplateCopy)->exStyle |= WS_EX_CONTROLPARENT;
1443 1444 1445
  }
  else
  {
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
    pTemplateCopy->style |= WS_CHILD | WS_TABSTOP | DS_CONTROL;
    pTemplateCopy->style &= ~DS_MODALFRAME;
    pTemplateCopy->style &= ~WS_CAPTION;
    pTemplateCopy->style &= ~WS_SYSMENU;
    pTemplateCopy->style &= ~WS_POPUP;
    pTemplateCopy->style &= ~WS_DISABLED;
    pTemplateCopy->style &= ~WS_VISIBLE;
    pTemplateCopy->style &= ~WS_THICKFRAME;

    pTemplateCopy->dwExtendedStyle |= WS_EX_CONTROLPARENT;
1456 1457 1458
  }

  if (psInfo->proppage[index].useCallback)
1459
    (*(ppshpage->pfnCallback))(0, PSPCB_CREATE,
1460
                               (LPPROPSHEETPAGEW)ppshpage);
1461

1462 1463
  if(ppshpage->dwFlags & PSP_INTERNAL_UNICODE)
     hwndPage = CreateDialogIndirectParamW(ppshpage->hInstance,
1464
					pTemplateCopy,
1465 1466 1467 1468 1469
					hwndParent,
					ppshpage->pfnDlgProc,
					(LPARAM)ppshpage);
  else
     hwndPage = CreateDialogIndirectParamA(ppshpage->hInstance,
1470
					pTemplateCopy,
1471 1472
					hwndParent,
					ppshpage->pfnDlgProc,
Marcus Meissner's avatar
Marcus Meissner committed
1473
					(LPARAM)ppshpage);
1474
  /* Free a no more needed copy */
1475
  Free(pTemplateCopy);
1476

1477
  psInfo->proppage[index].hwndPage = hwndPage;
1478

1479 1480 1481 1482 1483 1484 1485 1486
  /* Subclass exterior wizard pages */
  if((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
     (psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
     (ppshpage->dwFlags & PSP_HIDEHEADER))
  {
      SetWindowSubclass(hwndPage, PROPSHEET_WizardSubclassProc, 1,
                        (DWORD_PTR)ppshpage);
  }
1487 1488
  if (!(psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD))
      EnableThemeDialogTexture (hwndPage, ETDT_ENABLETAB);
1489

1490 1491 1492
  return TRUE;
}

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
/******************************************************************************
 *            PROPSHEET_LoadWizardBitmaps
 *
 * Loads the watermark and header bitmaps for a wizard.
 */
static VOID PROPSHEET_LoadWizardBitmaps(PropSheetInfo *psInfo)
{
  if (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD))
  {
    /* if PSH_USEHBMWATERMARK is not set, load the resource from pszbmWatermark 
       and put the HBITMAP in hbmWatermark. Thus all the rest of the code always 
       considers hbmWatermark as valid. */
    if ((psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
        !(psInfo->ppshheader.dwFlags & PSH_USEHBMWATERMARK))
    {
1508
      psInfo->ppshheader.u4.hbmWatermark =
Frank Richter's avatar
Frank Richter committed
1509
        CreateMappedBitmap(psInfo->ppshheader.hInstance, (INT_PTR)psInfo->ppshheader.u4.pszbmWatermark, 0, NULL, 0);
1510 1511 1512 1513 1514 1515
    }

    /* Same behavior as for watermarks */
    if ((psInfo->ppshheader.dwFlags & PSH_HEADER) &&
        !(psInfo->ppshheader.dwFlags & PSH_USEHBMHEADER))
    {
1516
      psInfo->ppshheader.u5.hbmHeader =
Frank Richter's avatar
Frank Richter committed
1517
        CreateMappedBitmap(psInfo->ppshheader.hInstance, (INT_PTR)psInfo->ppshheader.u5.pszbmHeader, 0, NULL, 0);
1518 1519 1520 1521 1522
    }
  }
}


1523 1524 1525 1526 1527 1528 1529
/******************************************************************************
 *            PROPSHEET_ShowPage
 *
 * Displays or creates the specified page.
 */
static BOOL PROPSHEET_ShowPage(HWND hwndDlg, int index, PropSheetInfo * psInfo)
{
1530
  HWND hwndTabCtrl;
1531
  HWND hwndLineHeader;
1532
  HWND control;
1533
  LPCPROPSHEETPAGEW ppshpage;
1534 1535

  TRACE("active_page %d, index %d\n", psInfo->active_page, index);
1536
  if (index == psInfo->active_page)
1537
  {
1538
      if (GetTopWindow(hwndDlg) != psInfo->proppage[index].hwndPage)
Alexandre Julliard's avatar
Alexandre Julliard committed
1539
          SetWindowPos(psInfo->proppage[index].hwndPage, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1540 1541
      return TRUE;
  }
1542

1543
  ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1544
  if (psInfo->proppage[index].hwndPage == 0)
1545
  {
Alexandre Julliard's avatar
Alexandre Julliard committed
1546
     PROPSHEET_CreatePage(hwndDlg, index, psInfo, ppshpage);
1547
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
1548

1549
  if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
1550 1551 1552
  {
     PROPSHEET_SetTitleW(hwndDlg, psInfo->ppshheader.dwFlags,
                         psInfo->proppage[index].pszText);
1553 1554 1555 1556

     control = GetNextDlgTabItem(psInfo->proppage[index].hwndPage, NULL, FALSE);
     if(control != NULL)
         SetFocus(control);
1557
  }
1558

1559 1560
  if (psInfo->active_page != -1)
     ShowWindow(psInfo->proppage[psInfo->active_page].hwndPage, SW_HIDE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1561

1562
  ShowWindow(psInfo->proppage[index].hwndPage, SW_SHOW);
1563

1564 1565 1566
  /* Synchronize current selection with tab control
   * It seems to be needed even in case of PSH_WIZARD (no tab controls there) */
  hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
1567
  SendMessageW(hwndTabCtrl, TCM_SETCURSEL, index, 0);
1568

1569
  psInfo->active_page = index;
Alexandre Julliard's avatar
Alexandre Julliard committed
1570
  psInfo->activeValid = TRUE;
1571

1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
  if (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW) )
  {
      hwndLineHeader = GetDlgItem(hwndDlg, IDC_SUNKEN_LINEHEADER);
      ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
      
      if ((ppshpage->dwFlags & PSP_HIDEHEADER) || (!(psInfo->ppshheader.dwFlags & PSH_HEADER)) )
	  ShowWindow(hwndLineHeader, SW_HIDE);
      else
	  ShowWindow(hwndLineHeader, SW_SHOW);
  }

1583 1584 1585
  return TRUE;
}

1586 1587 1588 1589 1590
/******************************************************************************
 *            PROPSHEET_Back
 */
static BOOL PROPSHEET_Back(HWND hwndDlg)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1591
  PSHNOTIFY psn;
1592
  HWND hwndPage;
1593
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1594
  LRESULT result;
1595
  int idx;
1596

1597
  TRACE("active_page %d\n", psInfo->active_page);
Alexandre Julliard's avatar
Alexandre Julliard committed
1598
  if (psInfo->active_page < 0)
1599 1600
     return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1601 1602 1603 1604
  psn.hdr.code     = PSN_WIZBACK;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1605

1606 1607
  hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;

1608
  result = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1609
  if (result == -1)
1610
    return FALSE;
1611 1612 1613 1614
  else if (result == 0)
     idx = psInfo->active_page - 1;
  else
     idx = PROPSHEET_FindPageByResId(psInfo, result);
1615

1616
  if (idx >= 0 && idx < psInfo->nPages)
1617
  {
1618
     if (PROPSHEET_CanSetCurSel(hwndDlg))
1619 1620 1621
     {
        SetFocus(GetDlgItem(hwndDlg, IDC_BACK_BUTTON));
        SendMessageW(hwndDlg, DM_SETDEFID, IDC_BACK_BUTTON, 0);
1622
        PROPSHEET_SetCurSel(hwndDlg, idx, -1, 0);
1623
     }
1624
  }
1625 1626 1627 1628 1629 1630 1631 1632
  return TRUE;
}

/******************************************************************************
 *            PROPSHEET_Next
 */
static BOOL PROPSHEET_Next(HWND hwndDlg)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1633
  PSHNOTIFY psn;
1634 1635
  HWND hwndPage;
  LRESULT msgResult = 0;
1636
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1637
  int idx;
1638

1639
  TRACE("active_page %d\n", psInfo->active_page);
Alexandre Julliard's avatar
Alexandre Julliard committed
1640
  if (psInfo->active_page < 0)
1641 1642
     return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1643 1644 1645 1646
  psn.hdr.code     = PSN_WIZNEXT;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1647

1648 1649
  hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;

1650
  msgResult = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1651 1652
  if (msgResult == -1)
    return FALSE;
1653 1654 1655 1656
  else if (msgResult == 0)
     idx = psInfo->active_page + 1;
  else
     idx = PROPSHEET_FindPageByResId(psInfo, msgResult);
1657

1658
  if (idx < psInfo->nPages )
1659
  {
1660
     if (PROPSHEET_CanSetCurSel(hwndDlg) != FALSE)
1661 1662 1663
     {
        SetFocus(GetDlgItem(hwndDlg, IDC_NEXT_BUTTON));
        SendMessageW(hwndDlg, DM_SETDEFID, IDC_NEXT_BUTTON, 0);
1664
        PROPSHEET_SetCurSel(hwndDlg, idx, 1, 0);
1665
     }
1666
  }
1667 1668 1669 1670 1671 1672 1673 1674 1675

  return TRUE;
}

/******************************************************************************
 *            PROPSHEET_Finish
 */
static BOOL PROPSHEET_Finish(HWND hwndDlg)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1676
  PSHNOTIFY psn;
1677 1678
  HWND hwndPage;
  LRESULT msgResult = 0;
1679
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1680

1681
  TRACE("active_page %d\n", psInfo->active_page);
1682 1683 1684
  if (psInfo->active_page < 0)
     return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1685 1686 1687 1688
  psn.hdr.code     = PSN_WIZFINISH;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1689

1690 1691
  hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;

1692
  msgResult = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1693 1694 1695 1696 1697 1698

  TRACE("msg result %ld\n", msgResult);

  if (msgResult != 0)
    return FALSE;

1699 1700
  if (psInfo->result == 0)
      psInfo->result = IDOK;
1701
  if (psInfo->isModeless)
Alexandre Julliard's avatar
Alexandre Julliard committed
1702
    psInfo->activeValid = FALSE;
1703
  else
1704
    psInfo->ended = TRUE;
1705 1706 1707 1708

  return TRUE;
}

1709 1710 1711
/******************************************************************************
 *            PROPSHEET_Apply
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1712
static BOOL PROPSHEET_Apply(HWND hwndDlg, LPARAM lParam)
1713 1714 1715
{
  int i;
  HWND hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1716
  PSHNOTIFY psn;
1717
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1718

1719
  TRACE("active_page %d\n", psInfo->active_page);
1720 1721 1722
  if (psInfo->active_page < 0)
     return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1723 1724 1725
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1726

1727 1728 1729 1730

  /*
   * Send PSN_KILLACTIVE to the current page.
   */
Alexandre Julliard's avatar
Alexandre Julliard committed
1731
  psn.hdr.code = PSN_KILLACTIVE;
1732 1733 1734

  hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;

1735
  if (SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn) != FALSE)
1736 1737 1738 1739 1740
    return FALSE;

  /*
   * Send PSN_APPLY to all pages.
   */
Alexandre Julliard's avatar
Alexandre Julliard committed
1741 1742
  psn.hdr.code = PSN_APPLY;
  psn.lParam   = lParam;
1743

1744
  for (i = 0; i < psInfo->nPages; i++)
1745 1746
  {
    hwndPage = psInfo->proppage[i].hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1747 1748
    if (hwndPage)
    {
1749
       switch (SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn))
1750 1751 1752 1753 1754 1755 1756
       {
       case PSNRET_INVALID:
           PROPSHEET_ShowPage(hwndDlg, i, psInfo);
           /* fall through */
       case PSNRET_INVALID_NOCHANGEPAGE:
           return FALSE;
       }
Alexandre Julliard's avatar
Alexandre Julliard committed
1757 1758
    }
  }
1759

Alexandre Julliard's avatar
Alexandre Julliard committed
1760 1761 1762 1763 1764 1765 1766 1767 1768
  if(lParam)
  {
     psInfo->activeValid = FALSE;
  }
  else if(psInfo->active_page >= 0)
  {
     psn.hdr.code = PSN_SETACTIVE;
     psn.lParam   = 0;
     hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1769
     SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1770 1771 1772 1773 1774 1775 1776 1777
  }

  return TRUE;
}

/******************************************************************************
 *            PROPSHEET_Cancel
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1778
static void PROPSHEET_Cancel(HWND hwndDlg, LPARAM lParam)
1779
{
1780
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1781
  HWND hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1782 1783
  PSHNOTIFY psn;
  int i;
1784

1785
  TRACE("active_page %d\n", psInfo->active_page);
1786 1787 1788 1789
  if (psInfo->active_page < 0)
     return;

  hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1790 1791 1792 1793
  psn.hdr.code     = PSN_QUERYCANCEL;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1794

1795
  if (SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn))
1796 1797
    return;

Alexandre Julliard's avatar
Alexandre Julliard committed
1798 1799
  psn.hdr.code = PSN_RESET;
  psn.lParam   = lParam;
1800

Alexandre Julliard's avatar
Alexandre Julliard committed
1801 1802 1803
  for (i = 0; i < psInfo->nPages; i++)
  {
    hwndPage = psInfo->proppage[i].hwndPage;
1804

Alexandre Julliard's avatar
Alexandre Julliard committed
1805
    if (hwndPage)
1806
       SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
Alexandre Julliard's avatar
Alexandre Julliard committed
1807
  }
1808 1809

  if (psInfo->isModeless)
Alexandre Julliard's avatar
Alexandre Julliard committed
1810 1811 1812 1813
  {
     /* makes PSM_GETCURRENTPAGEHWND return NULL */
     psInfo->activeValid = FALSE;
  }
1814
  else
1815
    psInfo->ended = TRUE;
1816 1817
}

1818 1819 1820 1821 1822
/******************************************************************************
 *            PROPSHEET_Help
 */
static void PROPSHEET_Help(HWND hwndDlg)
{
1823
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1824
  HWND hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1825
  PSHNOTIFY psn;
1826

1827
  TRACE("active_page %d\n", psInfo->active_page);
1828 1829 1830 1831
  if (psInfo->active_page < 0)
     return;

  hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1832 1833 1834 1835
  psn.hdr.code     = PSN_HELP;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1836

1837
  SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1838 1839
}

1840 1841 1842 1843 1844 1845
/******************************************************************************
 *            PROPSHEET_Changed
 */
static void PROPSHEET_Changed(HWND hwndDlg, HWND hwndDirtyPage)
{
  int i;
1846
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1847

1848
  TRACE("\n");
1849
  if (!psInfo) return;
1850 1851 1852
  /*
   * Set the dirty flag of this page.
   */
1853
  for (i = 0; i < psInfo->nPages; i++)
1854 1855 1856 1857
  {
    if (psInfo->proppage[i].hwndPage == hwndDirtyPage)
      psInfo->proppage[i].isDirty = TRUE;
  }
1858

1859 1860 1861 1862 1863 1864
  /*
   * Enable the Apply button.
   */
  if (psInfo->hasApply)
  {
    HWND hwndApplyBtn = GetDlgItem(hwndDlg, IDC_APPLY_BUTTON);
1865

1866 1867 1868
    EnableWindow(hwndApplyBtn, TRUE);
  }
}
1869

1870 1871 1872 1873 1874 1875 1876 1877
/******************************************************************************
 *            PROPSHEET_UnChanged
 */
static void PROPSHEET_UnChanged(HWND hwndDlg, HWND hwndCleanPage)
{
  int i;
  BOOL noPageDirty = TRUE;
  HWND hwndApplyBtn = GetDlgItem(hwndDlg, IDC_APPLY_BUTTON);
1878
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1879

1880
  TRACE("\n");
1881
  if ( !psInfo ) return;
1882
  for (i = 0; i < psInfo->nPages; i++)
1883 1884 1885 1886
  {
    /* set the specified page as clean */
    if (psInfo->proppage[i].hwndPage == hwndCleanPage)
      psInfo->proppage[i].isDirty = FALSE;
1887

1888 1889 1890 1891
    /* look to see if there's any dirty pages */
    if (psInfo->proppage[i].isDirty)
      noPageDirty = FALSE;
  }
1892

1893 1894 1895 1896 1897 1898
  /*
   * Disable Apply button.
   */
  if (noPageDirty)
    EnableWindow(hwndApplyBtn, FALSE);
}
1899

1900 1901 1902 1903 1904
/******************************************************************************
 *            PROPSHEET_PressButton
 */
static void PROPSHEET_PressButton(HWND hwndDlg, int buttonID)
{
1905
  TRACE("buttonID %d\n", buttonID);
1906 1907 1908
  switch (buttonID)
  {
    case PSBTN_APPLYNOW:
1909
      PROPSHEET_DoCommand(hwndDlg, IDC_APPLY_BUTTON);
1910 1911
      break;
    case PSBTN_BACK:
1912
      PROPSHEET_Back(hwndDlg);
1913 1914
      break;
    case PSBTN_CANCEL:
1915
      PROPSHEET_DoCommand(hwndDlg, IDCANCEL);
1916 1917
      break;
    case PSBTN_FINISH:
1918
      PROPSHEET_Finish(hwndDlg);
1919 1920
      break;
    case PSBTN_HELP:
1921
      PROPSHEET_DoCommand(hwndDlg, IDHELP);
1922 1923
      break;
    case PSBTN_NEXT:
1924
      PROPSHEET_Next(hwndDlg);
1925 1926
      break;
    case PSBTN_OK:
1927
      PROPSHEET_DoCommand(hwndDlg, IDOK);
1928 1929
      break;
    default:
1930
      FIXME("Invalid button index %d\n", buttonID);
1931 1932
  }
}
1933

1934 1935

/*************************************************************************
1936
 * BOOL PROPSHEET_CanSetCurSel [Internal]
1937
 *
1938
 * Test whether the current page can be changed by sending a PSN_KILLACTIVE
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949
 *
 * PARAMS
 *     hwndDlg        [I] handle to a Dialog hWnd
 *
 * RETURNS
 *     TRUE if Current Selection can change
 *
 * NOTES
 */
static BOOL PROPSHEET_CanSetCurSel(HWND hwndDlg)
{
1950
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1951
  HWND hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1952
  PSHNOTIFY psn;
1953
  BOOL res = FALSE;
1954

Alexandre Julliard's avatar
Alexandre Julliard committed
1955
  if (!psInfo)
1956 1957 1958 1959
  {
     res = FALSE;
     goto end;
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
1960

1961
  TRACE("active_page %d\n", psInfo->active_page);
Alexandre Julliard's avatar
Alexandre Julliard committed
1962
  if (psInfo->active_page < 0)
1963 1964 1965 1966
  {
     res = TRUE;
     goto end;
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
1967

1968 1969 1970 1971
  /*
   * Notify the current page.
   */
  hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1972 1973 1974 1975
  psn.hdr.code     = PSN_KILLACTIVE;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1976

1977
  res = !SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1978 1979 1980 1981

end:
  TRACE("<-- %d\n", res);
  return res;
1982 1983
}

1984 1985 1986 1987 1988
/******************************************************************************
 *            PROPSHEET_SetCurSel
 */
static BOOL PROPSHEET_SetCurSel(HWND hwndDlg,
                                int index,
1989 1990 1991
				int skipdir,
                                HPROPSHEETPAGE hpage
				)
1992
{
1993
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1994
  HWND hwndHelp  = GetDlgItem(hwndDlg, IDHELP);
1995
  HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
1996

1997
  TRACE("index %d, skipdir %d, hpage %p\n", index, skipdir, hpage);
Alexandre Julliard's avatar
Alexandre Julliard committed
1998
  /* hpage takes precedence over index */
1999
  if (hpage != NULL)
Thuy Nguyen's avatar
Thuy Nguyen committed
2000 2001
    index = PROPSHEET_GetPageIndex(hpage, psInfo);

2002 2003 2004 2005
  if (index < 0 || index >= psInfo->nPages)
  {
    TRACE("Could not find page to select!\n");
    return FALSE;
Thuy Nguyen's avatar
Thuy Nguyen committed
2006 2007
  }

2008 2009 2010 2011 2012
  /* unset active page while doing this transition. */
  if (psInfo->active_page != -1)
     ShowWindow(psInfo->proppage[psInfo->active_page].hwndPage, SW_HIDE);
  psInfo->active_page = -1;

2013
  while (1) {
2014 2015
    int result;
    PSHNOTIFY psn;
2016
    RECT rc;
Filip Navara's avatar
Filip Navara committed
2017
    LPCPROPSHEETPAGEW ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
2018

2019 2020 2021
    if (hwndTabControl)
	SendMessageW(hwndTabControl, TCM_SETCURSEL, index, 0);

2022 2023 2024 2025 2026
    psn.hdr.code     = PSN_SETACTIVE;
    psn.hdr.hwndFrom = hwndDlg;
    psn.hdr.idFrom   = 0;
    psn.lParam       = 0;

2027 2028 2029
    if (!psInfo->proppage[index].hwndPage) {
      PROPSHEET_CreatePage(hwndDlg, index, psInfo, ppshpage);
    }
2030

Filip Navara's avatar
Filip Navara committed
2031 2032 2033 2034
    /* Resize the property sheet page to the fit in the Tab control
     * (for regular property sheets) or to fit in the client area (for
     * wizards).
     * NOTE: The resizing happens every time the page is selected and
2035
     * not only when it's created (some applications depend on it). */
Filip Navara's avatar
Filip Navara committed
2036
    PROPSHEET_GetPageRect(psInfo, hwndDlg, &rc, ppshpage);
2037 2038
    TRACE("setting page %p, rc (%s) w=%d, h=%d\n",
          psInfo->proppage[index].hwndPage, wine_dbgstr_rect(&rc),
Filip Navara's avatar
Filip Navara committed
2039 2040 2041 2042
          rc.right - rc.left, rc.bottom - rc.top);
    SetWindowPos(psInfo->proppage[index].hwndPage, HWND_TOP,
                 rc.left, rc.top,
                 rc.right - rc.left, rc.bottom - rc.top, 0);
2043

2044
    result = SendMessageW(psInfo->proppage[index].hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
2045 2046 2047 2048 2049 2050
    if (!result)
      break;
    if (result == -1) {
      index+=skipdir;
      if (index < 0) {
	index = 0;
2051
	WARN("Tried to skip before first property sheet page!\n");
2052 2053 2054
	break;
      }
      if (index >= psInfo->nPages) {
2055
	WARN("Tried to skip after last property sheet page!\n");
2056 2057 2058 2059
	index = psInfo->nPages-1;
	break;
      }
    }
2060 2061
    else if (result != 0)
    {
2062 2063 2064 2065
      int old_index = index;
      index = PROPSHEET_FindPageByResId(psInfo, result);
      if(index >= psInfo->nPages) {
        index = old_index;
2066
        WARN("Tried to skip to nonexistent page by res id\n");
2067 2068 2069
        break;
      }
      continue;
2070
    }
2071
  }
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086

  /* Invalidate the header area */
  if ( (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
       (psInfo->ppshheader.dwFlags & PSH_HEADER) )
  {
    HWND hwndLineHeader = GetDlgItem(hwndDlg, IDC_SUNKEN_LINEHEADER);
    RECT r;

    GetClientRect(hwndLineHeader, &r);
    MapWindowPoints(hwndLineHeader, hwndDlg, (LPPOINT) &r, 2);
    SetRect(&r, 0, 0, r.right + 1, r.top - 1);

    InvalidateRect(hwndDlg, &r, TRUE);
  }

2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
  /*
   * Display the new page.
   */
  PROPSHEET_ShowPage(hwndDlg, index, psInfo);

  if (psInfo->proppage[index].hasHelp)
    EnableWindow(hwndHelp, TRUE);
  else
    EnableWindow(hwndHelp, FALSE);

  return TRUE;
}

2100 2101 2102 2103 2104 2105 2106 2107
/******************************************************************************
 *            PROPSHEET_SetCurSelId
 *
 * Selects the page, specified by resource id.
 */
static void PROPSHEET_SetCurSelId(HWND hwndDlg, int id)
{
      int idx;
2108
      PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2109 2110 2111 2112 2113 2114 2115 2116 2117

      idx = PROPSHEET_FindPageByResId(psInfo, id);
      if (idx < psInfo->nPages )
      {
          if (PROPSHEET_CanSetCurSel(hwndDlg) != FALSE)
              PROPSHEET_SetCurSel(hwndDlg, idx, 1, 0);
      }
}

2118 2119 2120 2121 2122
/******************************************************************************
 *            PROPSHEET_SetTitleA
 */
static void PROPSHEET_SetTitleA(HWND hwndDlg, DWORD dwStyle, LPCSTR lpszText)
{
2123
  if(!IS_INTRESOURCE(lpszText))
2124 2125
  {
     WCHAR szTitle[256];
2126
     MultiByteToWideChar(CP_ACP, 0, lpszText, -1,
2127
                         szTitle, sizeof(szTitle)/sizeof(WCHAR));
2128 2129 2130 2131 2132 2133 2134
     PROPSHEET_SetTitleW(hwndDlg, dwStyle, szTitle);
  }
  else
  {
     PROPSHEET_SetTitleW(hwndDlg, dwStyle, (LPCWSTR)lpszText);
  }
}
2135

2136 2137 2138 2139 2140
/******************************************************************************
 *            PROPSHEET_SetTitleW
 */
static void PROPSHEET_SetTitleW(HWND hwndDlg, DWORD dwStyle, LPCWSTR lpszText)
{
2141 2142
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
  WCHAR szTitle[256];
2143

2144
  TRACE("%s (style %08x)\n", debugstr_w(lpszText), dwStyle);
2145
  if (IS_INTRESOURCE(lpszText)) {
2146
    if (!LoadStringW(psInfo->ppshheader.hInstance,
2147
                     LOWORD(lpszText), szTitle, sizeof(szTitle)/sizeof(szTitle[0])))
2148 2149 2150
      return;
    lpszText = szTitle;
  }
2151 2152
  if (dwStyle & PSH_PROPTITLE)
  {
2153 2154 2155
    WCHAR* dest;
    int lentitle = strlenW(lpszText);
    int lenprop  = strlenW(psInfo->strPropertiesFor);
2156

2157
    dest = Alloc( (lentitle + lenprop + 1)*sizeof (WCHAR));
2158
    wsprintfW(dest, psInfo->strPropertiesFor, lpszText);
2159

2160
    SetWindowTextW(hwndDlg, dest);
2161
    Free(dest);
2162 2163
  }
  else
2164
    SetWindowTextW(hwndDlg, lpszText);
2165 2166
}

2167 2168 2169 2170 2171
/******************************************************************************
 *            PROPSHEET_SetFinishTextA
 */
static void PROPSHEET_SetFinishTextA(HWND hwndDlg, LPCSTR lpszText)
{
2172
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2173 2174
  HWND hwndButton = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);

2175
  TRACE("'%s'\n", lpszText);
2176 2177 2178 2179 2180 2181
  /* Set text, show and enable the Finish button */
  SetWindowTextA(hwndButton, lpszText);
  ShowWindow(hwndButton, SW_SHOW);
  EnableWindow(hwndButton, TRUE);

  /* Make it default pushbutton */
2182
  SendMessageW(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2183 2184 2185 2186 2187

  /* Hide Back button */
  hwndButton = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
  ShowWindow(hwndButton, SW_HIDE);

2188 2189 2190 2191 2192 2193
  if (!psInfo->hasFinish)
  {
    /* Hide Next button */
    hwndButton = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
    ShowWindow(hwndButton, SW_HIDE);
  }
2194 2195
}

2196 2197 2198 2199 2200
/******************************************************************************
 *            PROPSHEET_SetFinishTextW
 */
static void PROPSHEET_SetFinishTextW(HWND hwndDlg, LPCWSTR lpszText)
{
2201
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2202 2203
  HWND hwndButton = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);

2204
  TRACE("%s\n", debugstr_w(lpszText));
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
  /* Set text, show and enable the Finish button */
  SetWindowTextW(hwndButton, lpszText);
  ShowWindow(hwndButton, SW_SHOW);
  EnableWindow(hwndButton, TRUE);

  /* Make it default pushbutton */
  SendMessageW(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);

  /* Hide Back button */
  hwndButton = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
  ShowWindow(hwndButton, SW_HIDE);

2217 2218 2219 2220 2221 2222
  if (!psInfo->hasFinish)
  {
    /* Hide Next button */
    hwndButton = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
    ShowWindow(hwndButton, SW_HIDE);
  }
2223 2224
}

2225 2226 2227 2228 2229 2230 2231 2232 2233
/******************************************************************************
 *            PROPSHEET_QuerySiblings
 */
static LRESULT PROPSHEET_QuerySiblings(HWND hwndDlg,
                                       WPARAM wParam, LPARAM lParam)
{
  int i = 0;
  HWND hwndPage;
  LRESULT msgResult = 0;
2234
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2235

2236
  while ((i < psInfo->nPages) && (msgResult == 0))
2237 2238
  {
    hwndPage = psInfo->proppage[i].hwndPage;
2239
    msgResult = SendMessageW(hwndPage, PSM_QUERYSIBLINGS, wParam, lParam);
2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
    i++;
  }

  return msgResult;
}


/******************************************************************************
 *            PROPSHEET_AddPage
 */
static BOOL PROPSHEET_AddPage(HWND hwndDlg,
                              HPROPSHEETPAGE hpage)
{
2253
  PropPageInfo * ppi;
2254
  PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2255
  HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2256 2257
  TCITEMW item;
  LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)hpage;
2258

2259
  TRACE("hpage %p\n", hpage);
2260 2261 2262
  /*
   * Allocate and fill in a new PropPageInfo entry.
   */
2263
  ppi = ReAlloc(psInfo->proppage, sizeof(PropPageInfo) * (psInfo->nPages + 1));
2264 2265 2266 2267
  if (!ppi)
      return FALSE;

  psInfo->proppage = ppi;
2268
  if (!PROPSHEET_CollectPageInfo(ppsp, psInfo, psInfo->nPages, FALSE))
2269
      return FALSE;
2270

2271 2272
  psInfo->proppage[psInfo->nPages].hpage = hpage;

Alexandre Julliard's avatar
Alexandre Julliard committed
2273 2274 2275 2276 2277
  if (ppsp->dwFlags & PSP_PREMATURE)
  {
     /* Create the page but don't show it */
     PROPSHEET_CreatePage(hwndDlg, psInfo->nPages, psInfo, ppsp);
  }
2278 2279 2280 2281 2282

  /*
   * Add a new tab to the tab control.
   */
  item.mask = TCIF_TEXT;
2283
  item.pszText = (LPWSTR) psInfo->proppage[psInfo->nPages].pszText;
2284 2285
  item.cchTextMax = MAX_TABTEXT_LENGTH;

2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
  if (psInfo->hImageList)
  {
    SendMessageW(hwndTabControl, TCM_SETIMAGELIST, 0, (LPARAM)psInfo->hImageList);
  }

  if ( psInfo->proppage[psInfo->nPages].hasIcon )
  {
    item.mask |= TCIF_IMAGE;
    item.iImage = psInfo->nPages;
  }

2297
  SendMessageW(hwndTabControl, TCM_INSERTITEMW, psInfo->nPages + 1,
2298 2299 2300 2301
               (LPARAM)&item);

  psInfo->nPages++;

Alexandre Julliard's avatar
Alexandre Julliard committed
2302 2303
  /* If it is the only page - show it */
  if(psInfo->nPages == 1)
2304
     PROPSHEET_SetCurSel(hwndDlg, 0, 1, 0);
2305
  return TRUE;
2306 2307 2308 2309 2310 2311 2312 2313 2314
}

/******************************************************************************
 *            PROPSHEET_RemovePage
 */
static BOOL PROPSHEET_RemovePage(HWND hwndDlg,
                                 int index,
                                 HPROPSHEETPAGE hpage)
{
2315
  PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2316
  HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2317
  PropPageInfo* oldPages;
2318

2319
  TRACE("index %d, hpage %p\n", index, hpage);
2320 2321 2322
  if (!psInfo) {
    return FALSE;
  }
2323 2324 2325 2326 2327 2328
  /*
   * hpage takes precedence over index.
   */
  if (hpage != 0)
  {
    index = PROPSHEET_GetPageIndex(hpage, psInfo);
2329
  }
2330

2331
  /* Make sure that index is within range */
2332
  if (index < 0 || index >= psInfo->nPages)
Alexandre Julliard's avatar
Alexandre Julliard committed
2333
  {
2334
      TRACE("Could not find page to remove!\n");
2335
      return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2336
  }
2337

2338
  TRACE("total pages %d removing page %d active page %d\n",
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349
        psInfo->nPages, index, psInfo->active_page);
  /*
   * Check if we're removing the active page.
   */
  if (index == psInfo->active_page)
  {
    if (psInfo->nPages > 1)
    {
      if (index > 0)
      {
        /* activate previous page  */
2350
        PROPSHEET_SetCurSel(hwndDlg, index - 1, -1, 0);
2351 2352 2353 2354
      }
      else
      {
        /* activate the next page */
2355
        PROPSHEET_SetCurSel(hwndDlg, index + 1, 1, 0);
2356
        psInfo->active_page = index;
2357 2358 2359 2360
      }
    }
    else
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
2361 2362 2363
      psInfo->active_page = -1;
      if (!psInfo->isModeless)
      {
2364
         psInfo->ended = TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2365 2366
         return TRUE;
      }
2367 2368
    }
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
2369
  else if (index < psInfo->active_page)
2370 2371
    psInfo->active_page--;

2372 2373 2374 2375 2376 2377 2378 2379 2380
  /* Unsubclass the page dialog window */
  if((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD) &&
     (psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
     ((PROPSHEETPAGEW*)psInfo->proppage[index].hpage)->dwFlags & PSP_HIDEHEADER))
  {
     RemoveWindowSubclass(psInfo->proppage[index].hwndPage,
                          PROPSHEET_WizardSubclassProc, 1);
  }

Alexandre Julliard's avatar
Alexandre Julliard committed
2381 2382 2383 2384 2385 2386
  /* Destroy page dialog window */
  DestroyWindow(psInfo->proppage[index].hwndPage);

  /* Free page resources */
  if(psInfo->proppage[index].hpage)
  {
2387
     PROPSHEETPAGEW* psp = (PROPSHEETPAGEW*)psInfo->proppage[index].hpage;
Alexandre Julliard's avatar
Alexandre Julliard committed
2388

2389
     if (psp->dwFlags & PSP_USETITLE)
2390
        Free ((LPVOID)psInfo->proppage[index].pszText);
Alexandre Julliard's avatar
Alexandre Julliard committed
2391 2392 2393

     DestroyPropertySheetPage(psInfo->proppage[index].hpage);
  }
2394

2395
  /* Remove the tab */
2396
  SendMessageW(hwndTabControl, TCM_DELETEITEM, index, 0);
2397

2398
  oldPages = psInfo->proppage;
2399
  psInfo->nPages--;
2400
  psInfo->proppage = Alloc(sizeof(PropPageInfo) * psInfo->nPages);
2401

2402
  if (index > 0)
2403 2404 2405 2406 2407 2408
    memcpy(&psInfo->proppage[0], &oldPages[0], index * sizeof(PropPageInfo));

  if (index < psInfo->nPages)
    memcpy(&psInfo->proppage[index], &oldPages[index + 1],
           (psInfo->nPages - index) * sizeof(PropPageInfo));

2409
  Free(oldPages);
2410

2411 2412 2413
  return FALSE;
}

2414 2415 2416 2417 2418 2419 2420 2421 2422 2423
/******************************************************************************
 *            PROPSHEET_SetWizButtons
 *
 * This code will work if (and assumes that) the Next button is on top of the
 * Finish button. ie. Finish comes after Next in the Z order.
 * This means make sure the dialog template reflects this.
 *
 */
static void PROPSHEET_SetWizButtons(HWND hwndDlg, DWORD dwFlags)
{
2424
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
Alexandre Julliard's avatar
Alexandre Julliard committed
2425 2426 2427
  HWND hwndBack   = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
  HWND hwndNext   = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
  HWND hwndFinish = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2428

2429
  TRACE("%d\n", dwFlags);
2430

Alexandre Julliard's avatar
Alexandre Julliard committed
2431 2432 2433 2434
  EnableWindow(hwndBack, FALSE);
  EnableWindow(hwndNext, FALSE);
  EnableWindow(hwndFinish, FALSE);

2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445
  /* set the default pushbutton to an enabled button */
  if (((dwFlags & PSWIZB_FINISH) || psInfo->hasFinish) && !(dwFlags & PSWIZB_DISABLEDFINISH))
    SendMessageW(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
  else if (dwFlags & PSWIZB_NEXT)
    SendMessageW(hwndDlg, DM_SETDEFID, IDC_NEXT_BUTTON, 0);
  else if (dwFlags & PSWIZB_BACK)
    SendMessageW(hwndDlg, DM_SETDEFID, IDC_BACK_BUTTON, 0);
  else
    SendMessageW(hwndDlg, DM_SETDEFID, IDCANCEL, 0);


2446
  if (dwFlags & PSWIZB_BACK)
Alexandre Julliard's avatar
Alexandre Julliard committed
2447
    EnableWindow(hwndBack, TRUE);
2448 2449

  if (dwFlags & PSWIZB_NEXT)
Alexandre Julliard's avatar
Alexandre Julliard committed
2450
    EnableWindow(hwndNext, TRUE);
2451

2452
  if (!psInfo->hasFinish)
2453
  {
2454 2455 2456 2457 2458 2459 2460
    if ((dwFlags & PSWIZB_FINISH) || (dwFlags & PSWIZB_DISABLEDFINISH))
    {
      /* Hide the Next button */
      ShowWindow(hwndNext, SW_HIDE);
      
      /* Show the Finish button */
      ShowWindow(hwndFinish, SW_SHOW);
2461

2462 2463 2464
      if (!(dwFlags & PSWIZB_DISABLEDFINISH))
        EnableWindow(hwndFinish, TRUE);
    }
2465 2466 2467 2468 2469 2470 2471
    else
    {
      /* Hide the Finish button */
      ShowWindow(hwndFinish, SW_HIDE);
      /* Show the Next button */
      ShowWindow(hwndNext, SW_SHOW);
    }
2472
  }
2473 2474
  else if (!(dwFlags & PSWIZB_DISABLEDFINISH))
    EnableWindow(hwndFinish, TRUE);
2475 2476
}

2477 2478 2479 2480 2481
/******************************************************************************
 *            PROPSHEET_InsertPage
 */
static BOOL PROPSHEET_InsertPage(HWND hwndDlg, HPROPSHEETPAGE hpageInsertAfter, HPROPSHEETPAGE hpage)
{
2482
    if (IS_INTRESOURCE(hpageInsertAfter))
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525
        FIXME("(%p, %d, %p): stub\n", hwndDlg, LOWORD(hpageInsertAfter), hpage);
    else
        FIXME("(%p, %p, %p): stub\n", hwndDlg, hpageInsertAfter, hpage);
    return FALSE;
}

/******************************************************************************
 *            PROPSHEET_SetHeaderTitleW
 */
static void PROPSHEET_SetHeaderTitleW(HWND hwndDlg, int iPageIndex, LPCWSTR pszHeaderTitle)
{
    FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_w(pszHeaderTitle));
}

/******************************************************************************
 *            PROPSHEET_SetHeaderTitleA
 */
static void PROPSHEET_SetHeaderTitleA(HWND hwndDlg, int iPageIndex, LPCSTR pszHeaderTitle)
{
    FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_a(pszHeaderTitle));
}

/******************************************************************************
 *            PROPSHEET_SetHeaderSubTitleW
 */
static void PROPSHEET_SetHeaderSubTitleW(HWND hwndDlg, int iPageIndex, LPCWSTR pszHeaderSubTitle)
{
    FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_w(pszHeaderSubTitle));
}

/******************************************************************************
 *            PROPSHEET_SetHeaderSubTitleA
 */
static void PROPSHEET_SetHeaderSubTitleA(HWND hwndDlg, int iPageIndex, LPCSTR pszHeaderSubTitle)
{
    FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_a(pszHeaderSubTitle));
}

/******************************************************************************
 *            PROPSHEET_HwndToIndex
 */
static LRESULT PROPSHEET_HwndToIndex(HWND hwndDlg, HWND hPageDlg)
{
2526
    int index;
2527
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2528 2529 2530 2531 2532 2533 2534

    TRACE("(%p, %p)\n", hwndDlg, hPageDlg);

    for (index = 0; index < psInfo->nPages; index++)
        if (psInfo->proppage[index].hwndPage == hPageDlg)
            return index;
    WARN("%p not found\n", hPageDlg);
2535 2536 2537 2538 2539 2540 2541 2542
    return -1;
}

/******************************************************************************
 *            PROPSHEET_IndexToHwnd
 */
static LRESULT PROPSHEET_IndexToHwnd(HWND hwndDlg, int iPageIndex)
{
2543
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2544
    TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
2545 2546
    if (!psInfo)
        return 0;
2547 2548 2549 2550 2551
    if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
        WARN("%d out of range.\n", iPageIndex);
	return 0;
    }
    return (LRESULT)psInfo->proppage[iPageIndex].hwndPage;
2552 2553 2554 2555 2556 2557 2558
}

/******************************************************************************
 *            PROPSHEET_PageToIndex
 */
static LRESULT PROPSHEET_PageToIndex(HWND hwndDlg, HPROPSHEETPAGE hPage)
{
2559
    int index;
2560
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2561 2562 2563 2564 2565 2566 2567

    TRACE("(%p, %p)\n", hwndDlg, hPage);

    for (index = 0; index < psInfo->nPages; index++)
        if (psInfo->proppage[index].hpage == hPage)
            return index;
    WARN("%p not found\n", hPage);
2568 2569 2570 2571 2572 2573 2574 2575
    return -1;
}

/******************************************************************************
 *            PROPSHEET_IndexToPage
 */
static LRESULT PROPSHEET_IndexToPage(HWND hwndDlg, int iPageIndex)
{
2576
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2577 2578 2579 2580 2581 2582
    TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
    if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
        WARN("%d out of range.\n", iPageIndex);
	return 0;
    }
    return (LRESULT)psInfo->proppage[iPageIndex].hpage;
2583 2584 2585 2586 2587 2588 2589
}

/******************************************************************************
 *            PROPSHEET_IdToIndex
 */
static LRESULT PROPSHEET_IdToIndex(HWND hwndDlg, int iPageId)
{
2590 2591
    int index;
    LPCPROPSHEETPAGEW psp;
2592
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2593 2594 2595
    TRACE("(%p, %d)\n", hwndDlg, iPageId);
    for (index = 0; index < psInfo->nPages; index++) {
        psp = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
Frank Richter's avatar
Frank Richter committed
2596
        if (psp->u.pszTemplate == MAKEINTRESOURCEW(iPageId))
2597 2598 2599
            return index;
    }

2600 2601 2602 2603 2604 2605 2606 2607
    return -1;
}

/******************************************************************************
 *            PROPSHEET_IndexToId
 */
static LRESULT PROPSHEET_IndexToId(HWND hwndDlg, int iPageIndex)
{
2608
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2609 2610 2611 2612 2613 2614 2615
    LPCPROPSHEETPAGEW psp;
    TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
    if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
        WARN("%d out of range.\n", iPageIndex);
	return 0;
    }
    psp = (LPCPROPSHEETPAGEW)psInfo->proppage[iPageIndex].hpage;
2616
    if (psp->dwFlags & PSP_DLGINDIRECT || !IS_INTRESOURCE(psp->u.pszTemplate)) {
2617 2618 2619
        return 0;
    }
    return (LRESULT)psp->u.pszTemplate;
2620 2621 2622 2623 2624 2625 2626
}

/******************************************************************************
 *            PROPSHEET_GetResult
 */
static LRESULT PROPSHEET_GetResult(HWND hwndDlg)
{
2627
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2628
    return psInfo->result;
2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639
}

/******************************************************************************
 *            PROPSHEET_RecalcPageSizes
 */
static BOOL PROPSHEET_RecalcPageSizes(HWND hwndDlg)
{
    FIXME("(%p): stub\n", hwndDlg);
    return FALSE;
}

2640 2641 2642 2643 2644 2645
/******************************************************************************
 *            PROPSHEET_GetPageIndex
 *
 * Given a HPROPSHEETPAGE, returns the index of the corresponding page from
 * the array of PropPageInfo.
 */
2646
static int PROPSHEET_GetPageIndex(HPROPSHEETPAGE hpage, const PropSheetInfo* psInfo)
2647 2648 2649 2650
{
  BOOL found = FALSE;
  int index = 0;

2651
  TRACE("hpage %p\n", hpage);
2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665
  while ((index < psInfo->nPages) && (found == FALSE))
  {
    if (psInfo->proppage[index].hpage == hpage)
      found = TRUE;
    else
      index++;
  }

  if (found == FALSE)
    index = -1;

  return index;
}

2666 2667 2668 2669 2670
/******************************************************************************
 *            PROPSHEET_CleanUp
 */
static void PROPSHEET_CleanUp(HWND hwndDlg)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
2671
  int i;
2672
  PropSheetInfo* psInfo = RemovePropW(hwndDlg, PropSheetInfoStr);
2673 2674

  TRACE("\n");
2675
  if (!psInfo) return;
2676
  if (!IS_INTRESOURCE(psInfo->ppshheader.pszCaption))
2677
      Free ((LPVOID)psInfo->ppshheader.pszCaption);
Alexandre Julliard's avatar
Alexandre Julliard committed
2678 2679 2680 2681 2682

  for (i = 0; i < psInfo->nPages; i++)
  {
     PROPSHEETPAGEA* psp = (PROPSHEETPAGEA*)psInfo->proppage[i].hpage;

2683 2684 2685 2686 2687 2688 2689 2690 2691
     /* Unsubclass the page dialog window */
     if((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
        (psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
        (psp->dwFlags & PSP_HIDEHEADER))
     {
        RemoveWindowSubclass(psInfo->proppage[i].hwndPage,
                             PROPSHEET_WizardSubclassProc, 1);
     }

Alexandre Julliard's avatar
Alexandre Julliard committed
2692 2693 2694 2695 2696
     if(psInfo->proppage[i].hwndPage)
        DestroyWindow(psInfo->proppage[i].hwndPage);

     if(psp)
     {
2697
        if (psp->dwFlags & PSP_USETITLE)
2698
           Free ((LPVOID)psInfo->proppage[i].pszText);
Alexandre Julliard's avatar
Alexandre Julliard committed
2699 2700 2701 2702 2703

        DestroyPropertySheetPage(psInfo->proppage[i].hpage);
     }
  }

2704 2705 2706 2707 2708 2709 2710 2711 2712 2713
  DeleteObject(psInfo->hFont);
  DeleteObject(psInfo->hFontBold);
  /* If we created the bitmaps, destroy them */
  if ((psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
      (!(psInfo->ppshheader.dwFlags & PSH_USEHBMWATERMARK)) )
      DeleteObject(psInfo->ppshheader.u4.hbmWatermark);
  if ((psInfo->ppshheader.dwFlags & PSH_HEADER) &&
      (!(psInfo->ppshheader.dwFlags & PSH_USEHBMHEADER)) )
      DeleteObject(psInfo->ppshheader.u5.hbmHeader);

2714 2715
  Free(psInfo->proppage);
  Free(psInfo->strPropertiesFor);
2716
  ImageList_Destroy(psInfo->hImageList);
2717

2718
  GlobalFree(psInfo);
2719 2720
}

2721
static INT do_loop(const PropSheetInfo *psInfo)
2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
{
    MSG msg;
    INT ret = -1;
    HWND hwnd = psInfo->hwnd;

    while(IsWindow(hwnd) && !psInfo->ended && (ret = GetMessageW(&msg, NULL, 0, 0)))
    {
        if(ret == -1)
            break;

        if(!IsDialogMessageW(hwnd, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessageW(&msg);
        }
    }

    if(ret == 0)
    {
        PostQuitMessage(msg.wParam);
        ret = -1;
    }

2745 2746 2747
    if(ret != -1)
        ret = psInfo->result;

2748 2749 2750 2751
    DestroyWindow(hwnd);
    return ret;
}

2752 2753 2754 2755 2756 2757 2758 2759
/******************************************************************************
 *            PROPSHEET_PropertySheet
 *
 * Common code between PropertySheetA/W
 */
static INT_PTR PROPSHEET_PropertySheet(PropSheetInfo* psInfo, BOOL unicode)
{
  INT_PTR bRet = 0;
2760
  HWND parent = NULL;
2761 2762 2763 2764 2765 2766 2767 2768
  if (psInfo->active_page >= psInfo->nPages) psInfo->active_page = 0;
  TRACE("startpage: %d of %d pages\n", psInfo->active_page, psInfo->nPages);

  psInfo->unicode = unicode;
  psInfo->ended = FALSE;

  if(!psInfo->isModeless)
  {
2769
      parent = psInfo->ppshheader.hwndParent;
2770
      if (parent) EnableWindow(parent, FALSE);
2771 2772 2773 2774
  }
  bRet = PROPSHEET_CreateDialog(psInfo);
  if(!psInfo->isModeless)
  {
2775 2776 2777 2778 2779 2780
      bRet = do_loop(psInfo);
      if (parent) EnableWindow(parent, TRUE);
  }
  return bRet;
}

2781
/******************************************************************************
2782 2783
 *            PropertySheet    (COMCTL32.@)
 *            PropertySheetA   (COMCTL32.@)
2784 2785 2786 2787 2788 2789 2790 2791 2792
 *
 * Creates a property sheet in the specified property sheet header.
 *
 * RETURNS
 *     Modal property sheets: Positive if successful or -1 otherwise.
 *     Modeless property sheets: Property sheet handle.
 *     Or:
 *| ID_PSREBOOTSYSTEM - The user must reboot the computer for the changes to take effect.
 *| ID_PSRESTARTWINDOWS - The user must restart Windows for the changes to take effect.
2793
 */
Frank Richter's avatar
Frank Richter committed
2794
INT_PTR WINAPI PropertySheetA(LPCPROPSHEETHEADERA lppsh)
2795
{
2796
  PropSheetInfo* psInfo = GlobalAlloc(GPTR, sizeof(PropSheetInfo));
2797
  UINT i, n;
2798
  const BYTE* pByte;
2799

2800 2801 2802
  TRACE("(%p)\n", lppsh);

  PROPSHEET_CollectSheetInfoA(lppsh, psInfo);
2803

2804
  psInfo->proppage = Alloc(sizeof(PropPageInfo) * lppsh->nPages);
2805
  pByte = (const BYTE*) psInfo->ppshheader.u3.ppsp;
2806

2807
  for (n = i = 0; i < lppsh->nPages; i++, n++)
2808
  {
2809
    if (!psInfo->usePropPage)
2810
      psInfo->proppage[n].hpage = psInfo->ppshheader.u3.phpage[i];
Alexandre Julliard's avatar
Alexandre Julliard committed
2811 2812
    else
    {
2813
       psInfo->proppage[n].hpage = CreatePropertySheetPageA((LPCPROPSHEETPAGEA)pByte);
2814
       pByte += ((LPCPROPSHEETPAGEA)pByte)->dwSize;
Alexandre Julliard's avatar
Alexandre Julliard committed
2815 2816
    }

2817
    if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2818
                               psInfo, n, TRUE))
2819
    {
2820
	if (psInfo->usePropPage)
2821 2822 2823 2824
	    DestroyPropertySheetPage(psInfo->proppage[n].hpage);
	n--;
	psInfo->nPages--;
    }
2825
  }
2826

2827
  return PROPSHEET_PropertySheet(psInfo, FALSE);
2828 2829
}

2830
/******************************************************************************
2831
 *            PropertySheetW   (COMCTL32.@)
2832 2833
 *
 * See PropertySheetA.
2834
 */
Frank Richter's avatar
Frank Richter committed
2835
INT_PTR WINAPI PropertySheetW(LPCPROPSHEETHEADERW lppsh)
2836
{
2837
  PropSheetInfo* psInfo = GlobalAlloc(GPTR, sizeof(PropSheetInfo));
2838
  UINT i, n;
2839
  const BYTE* pByte;
2840 2841 2842 2843 2844

  TRACE("(%p)\n", lppsh);

  PROPSHEET_CollectSheetInfoW(lppsh, psInfo);

2845
  psInfo->proppage = Alloc(sizeof(PropPageInfo) * lppsh->nPages);
2846
  pByte = (const BYTE*) psInfo->ppshheader.u3.ppsp;
2847

2848 2849
  for (n = i = 0; i < lppsh->nPages; i++, n++)
  {
2850
    if (!psInfo->usePropPage)
2851 2852 2853 2854
      psInfo->proppage[n].hpage = psInfo->ppshheader.u3.phpage[i];
    else
    {
       psInfo->proppage[n].hpage = CreatePropertySheetPageW((LPCPROPSHEETPAGEW)pByte);
2855
       pByte += ((LPCPROPSHEETPAGEW)pByte)->dwSize;
2856 2857
    }

2858
    if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2859
                               psInfo, n, TRUE))
2860
    {
2861
	if (psInfo->usePropPage)
2862 2863 2864 2865 2866 2867
	    DestroyPropertySheetPage(psInfo->proppage[n].hpage);
	n--;
	psInfo->nPages--;
    }
  }

2868
  return PROPSHEET_PropertySheet(psInfo, TRUE);
2869 2870
}

2871 2872 2873 2874 2875 2876 2877 2878 2879 2880
static LPWSTR load_string( HINSTANCE instance, LPCWSTR str )
{
    LPWSTR ret;

    if (IS_INTRESOURCE(str))
    {
        HRSRC hrsrc;
        HGLOBAL hmem;
        WCHAR *ptr;
        WORD i, id = LOWORD(str);
2881
        UINT len;
2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906

        if (!(hrsrc = FindResourceW( instance, MAKEINTRESOURCEW((id >> 4) + 1), (LPWSTR)RT_STRING )))
            return NULL;
        if (!(hmem = LoadResource( instance, hrsrc ))) return NULL;
        if (!(ptr = LockResource( hmem ))) return NULL;
        for (i = id & 0x0f; i > 0; i--) ptr += *ptr + 1;
        len = *ptr;
        if (!len) return NULL;
        ret = Alloc( (len + 1) * sizeof(WCHAR) );
        if (ret)
        {
            memcpy( ret, ptr + 1, len * sizeof(WCHAR) );
            ret[len] = 0;
        }
    }
    else
    {
        int len = (strlenW(str) + 1) * sizeof(WCHAR);
        ret = Alloc( len );
        if (ret) memcpy( ret, str, len );
    }
    return ret;
}


2907
/******************************************************************************
2908 2909
 *            CreatePropertySheetPage    (COMCTL32.@)
 *            CreatePropertySheetPageA   (COMCTL32.@)
2910 2911 2912 2913 2914 2915 2916 2917 2918 2919
 *
 * Creates a new property sheet page.
 *
 * RETURNS
 *     Success: Handle to new property sheet page.
 *     Failure: NULL.
 *
 * NOTES
 *     An application must use the PSM_ADDPAGE message to add the new page to
 *     an existing property sheet.
2920
 */
2921 2922
HPROPSHEETPAGE WINAPI CreatePropertySheetPageA(
                          LPCPROPSHEETPAGEA lpPropSheetPage)
2923
{
2924
  PROPSHEETPAGEW* ppsp = Alloc(sizeof(PROPSHEETPAGEW));
2925

2926
  memcpy(ppsp,lpPropSheetPage,min(lpPropSheetPage->dwSize,sizeof(PROPSHEETPAGEA)));
2927

2928 2929
  ppsp->dwFlags &= ~ PSP_INTERNAL_UNICODE;

2930 2931
    if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) )
    {
2932
        if (!IS_INTRESOURCE( ppsp->u.pszTemplate ))
2933 2934
        {
            int len = strlen(lpPropSheetPage->u.pszTemplate) + 1;
2935 2936 2937
            char *template = Alloc( len );

            ppsp->u.pszTemplate = (LPWSTR)strcpy( template, lpPropSheetPage->u.pszTemplate );
2938 2939
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
2940

2941 2942
    if (ppsp->dwFlags & PSP_USEICONID)
    {
2943
        if (!IS_INTRESOURCE( ppsp->u2.pszIcon ))
2944 2945 2946 2947 2948
            PROPSHEET_AtoW(&ppsp->u2.pszIcon, lpPropSheetPage->u2.pszIcon);
    }

    if (ppsp->dwFlags & PSP_USETITLE)
    {
2949
        if (!IS_INTRESOURCE( ppsp->pszTitle ))
2950 2951
            PROPSHEET_AtoW( &ppsp->pszTitle, lpPropSheetPage->pszTitle );
        else
2952
            ppsp->pszTitle = load_string( ppsp->hInstance, ppsp->pszTitle );
2953 2954 2955
    }
    else
        ppsp->pszTitle = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
2956

2957 2958 2959 2960 2961
    if (ppsp->dwFlags & PSP_HIDEHEADER)
        ppsp->dwFlags &= ~(PSP_USEHEADERTITLE | PSP_USEHEADERSUBTITLE);

    if (ppsp->dwFlags & PSP_USEHEADERTITLE)
    {
2962
        if (!IS_INTRESOURCE( ppsp->pszHeaderTitle ))
2963 2964
            PROPSHEET_AtoW(&ppsp->pszHeaderTitle, lpPropSheetPage->pszHeaderTitle);
        else
2965
            ppsp->pszHeaderTitle = load_string( ppsp->hInstance, ppsp->pszHeaderTitle );
2966 2967 2968 2969 2970 2971
    }
    else
        ppsp->pszHeaderTitle = NULL;

    if (ppsp->dwFlags & PSP_USEHEADERSUBTITLE)
    {
2972
        if (!IS_INTRESOURCE( ppsp->pszHeaderSubTitle ))
2973 2974
            PROPSHEET_AtoW(&ppsp->pszHeaderSubTitle, lpPropSheetPage->pszHeaderSubTitle);
        else
2975
            ppsp->pszHeaderSubTitle = load_string( ppsp->hInstance, ppsp->pszHeaderSubTitle );
2976 2977 2978 2979 2980
    }
    else
        ppsp->pszHeaderSubTitle = NULL;

    return (HPROPSHEETPAGE)ppsp;
2981 2982
}

2983
/******************************************************************************
2984
 *            CreatePropertySheetPageW   (COMCTL32.@)
2985 2986
 *
 * See CreatePropertySheetA.
2987
 */
2988
HPROPSHEETPAGE WINAPI CreatePropertySheetPageW(LPCPROPSHEETPAGEW lpPropSheetPage)
2989
{
2990
  PROPSHEETPAGEW* ppsp = Alloc(sizeof(PROPSHEETPAGEW));
2991 2992 2993

  memcpy(ppsp,lpPropSheetPage,min(lpPropSheetPage->dwSize,sizeof(PROPSHEETPAGEW)));

2994 2995
  ppsp->dwFlags |= PSP_INTERNAL_UNICODE;

2996 2997
    if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) )
    {
2998
        if (!IS_INTRESOURCE( ppsp->u.pszTemplate ))
2999 3000
        {
            int len = strlenW(lpPropSheetPage->u.pszTemplate) + 1;
3001 3002 3003
            WCHAR *template = Alloc( len * sizeof (WCHAR) );

            ppsp->u.pszTemplate = strcpyW( template, lpPropSheetPage->u.pszTemplate );
3004 3005
        }
    }
3006

3007 3008
    if ( ppsp->dwFlags & PSP_USEICONID )
    {
3009
        if (!IS_INTRESOURCE( ppsp->u2.pszIcon ))
3010 3011
        {
            int len = strlenW(lpPropSheetPage->u2.pszIcon) + 1;
3012 3013 3014
            WCHAR *icon = Alloc( len * sizeof (WCHAR) );

            ppsp->u2.pszIcon = strcpyW( icon, lpPropSheetPage->u2.pszIcon );
3015 3016
        }
    }
3017

3018
    if (ppsp->dwFlags & PSP_USETITLE)
3019
        ppsp->pszTitle = load_string( ppsp->hInstance, ppsp->pszTitle );
3020 3021 3022 3023 3024 3025 3026
    else
        ppsp->pszTitle = NULL;

    if (ppsp->dwFlags & PSP_HIDEHEADER)
        ppsp->dwFlags &= ~(PSP_USEHEADERTITLE | PSP_USEHEADERSUBTITLE);

    if (ppsp->dwFlags & PSP_USEHEADERTITLE)
3027
        ppsp->pszHeaderTitle = load_string( ppsp->hInstance, ppsp->pszHeaderTitle );
3028 3029 3030 3031
    else
        ppsp->pszHeaderTitle = NULL;

    if (ppsp->dwFlags & PSP_USEHEADERSUBTITLE)
3032
        ppsp->pszHeaderSubTitle = load_string( ppsp->hInstance, ppsp->pszHeaderSubTitle );
3033 3034
    else
        ppsp->pszHeaderSubTitle = NULL;
3035

3036
    return (HPROPSHEETPAGE)ppsp;
3037 3038
}

3039
/******************************************************************************
3040
 *            DestroyPropertySheetPage   (COMCTL32.@)
3041 3042 3043 3044 3045 3046 3047 3048
 *
 * Destroys a property sheet page previously created with
 * CreatePropertySheetA() or CreatePropertySheetW() and frees the associated
 * memory.
 *
 * RETURNS
 *     Success: TRUE
 *     Failure: FALSE
3049
 */
3050
BOOL WINAPI DestroyPropertySheetPage(HPROPSHEETPAGE hPropPage)
3051
{
3052
  PROPSHEETPAGEW *psp = (PROPSHEETPAGEW *)hPropPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
3053 3054 3055 3056

  if (!psp)
     return FALSE;

3057
  if (!(psp->dwFlags & PSP_DLGINDIRECT) && !IS_INTRESOURCE( psp->u.pszTemplate ))
3058
     Free ((LPVOID)psp->u.pszTemplate);
Alexandre Julliard's avatar
Alexandre Julliard committed
3059

3060
  if ((psp->dwFlags & PSP_USEICONID) && !IS_INTRESOURCE( psp->u2.pszIcon ))
3061
     Free ((LPVOID)psp->u2.pszIcon);
Alexandre Julliard's avatar
Alexandre Julliard committed
3062

3063
  if ((psp->dwFlags & PSP_USETITLE) && !IS_INTRESOURCE( psp->pszTitle ))
3064
     Free ((LPVOID)psp->pszTitle);
Alexandre Julliard's avatar
Alexandre Julliard committed
3065

3066 3067 3068 3069 3070 3071
  if ((psp->dwFlags & PSP_USEHEADERTITLE) && !IS_INTRESOURCE( psp->pszHeaderTitle ))
     Free ((LPVOID)psp->pszHeaderTitle);

  if ((psp->dwFlags & PSP_USEHEADERSUBTITLE) && !IS_INTRESOURCE( psp->pszHeaderSubTitle ))
     Free ((LPVOID)psp->pszHeaderSubTitle);

3072
  Free(hPropPage);
3073 3074

  return TRUE;
3075 3076
}

Alexandre Julliard's avatar
Alexandre Julliard committed
3077 3078 3079 3080 3081
/******************************************************************************
 *            PROPSHEET_IsDialogMessage
 */
static BOOL PROPSHEET_IsDialogMessage(HWND hwnd, LPMSG lpMsg)
{
3082
   PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
Alexandre Julliard's avatar
Alexandre Julliard committed
3083

3084
   TRACE("\n");
Alexandre Julliard's avatar
Alexandre Julliard committed
3085 3086 3087 3088 3089 3090
   if (!psInfo || (hwnd != lpMsg->hwnd && !IsChild(hwnd, lpMsg->hwnd)))
      return FALSE;

   if (lpMsg->message == WM_KEYDOWN && (GetKeyState(VK_CONTROL) & 0x8000))
   {
      int new_page = 0;
3091
      INT dlgCode = SendMessageW(lpMsg->hwnd, WM_GETDLGCODE, 0, (LPARAM)lpMsg);
Alexandre Julliard's avatar
Alexandre Julliard committed
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119

      if (!(dlgCode & DLGC_WANTMESSAGE))
      {
         switch (lpMsg->wParam)
         {
            case VK_TAB:
               if (GetKeyState(VK_SHIFT) & 0x8000)
                   new_page = -1;
                else
                   new_page = 1;
               break;

            case VK_NEXT:   new_page = 1;  break;
            case VK_PRIOR:  new_page = -1; break;
         }
      }

      if (new_page)
      {
         if (PROPSHEET_CanSetCurSel(hwnd) != FALSE)
         {
            new_page += psInfo->active_page;

            if (new_page < 0)
               new_page = psInfo->nPages - 1;
            else if (new_page >= psInfo->nPages)
               new_page = 0;

3120
            PROPSHEET_SetCurSel(hwnd, new_page, 1, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
3121 3122 3123 3124 3125 3126
         }

         return TRUE;
      }
   }

3127
   return IsDialogMessageW(hwnd, lpMsg);
Alexandre Julliard's avatar
Alexandre Julliard committed
3128 3129
}

3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147
/******************************************************************************
 *            PROPSHEET_DoCommand
 */
static BOOL PROPSHEET_DoCommand(HWND hwnd, WORD wID)
{

    switch (wID) {

    case IDOK:
    case IDC_APPLY_BUTTON:
	{
	    HWND hwndApplyBtn = GetDlgItem(hwnd, IDC_APPLY_BUTTON);

	    if (PROPSHEET_Apply(hwnd, wID == IDOK ? 1: 0) == FALSE)
		break;

	    if (wID == IDOK)
		{
3148
                    PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
3149

3150 3151 3152
                    /* don't overwrite ID_PSRESTARTWINDOWS or ID_PSREBOOTSYSTEM */
                    if (psInfo->result == 0)
                        psInfo->result = IDOK;
3153 3154 3155 3156

		    if (psInfo->isModeless)
			psInfo->activeValid = FALSE;
		    else
3157
                        psInfo->ended = TRUE;
3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
		}
	    else
		EnableWindow(hwndApplyBtn, FALSE);

	    break;
	}

    case IDC_BACK_BUTTON:
	PROPSHEET_Back(hwnd);
	break;

    case IDC_NEXT_BUTTON:
	PROPSHEET_Next(hwnd);
	break;

    case IDC_FINISH_BUTTON:
	PROPSHEET_Finish(hwnd);
	break;

    case IDCANCEL:
	PROPSHEET_Cancel(hwnd, 0);
	break;

    case IDHELP:
	PROPSHEET_Help(hwnd);
	break;
3184 3185 3186

    default:
        return FALSE;
3187 3188 3189 3190 3191
    }

    return TRUE;
}

3192 3193 3194
/******************************************************************************
 *            PROPSHEET_Paint
 */
3195
static LRESULT PROPSHEET_Paint(HWND hwnd, HDC hdcParam)
3196
{
3197
    PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
3198 3199 3200 3201 3202 3203 3204
    PAINTSTRUCT ps;
    HDC hdc, hdcSrc;
    BITMAP bm;
    HBITMAP hbmp;
    HPALETTE hOldPal = 0;
    int offsety = 0;
    HBRUSH hbr;
3205
    RECT r, rzone;
3206
    LPCPROPSHEETPAGEW ppshpage;
3207 3208
    WCHAR szBuffer[256];
    int nLength;
3209

3210
    hdc = hdcParam ? hdcParam : BeginPaint(hwnd, &ps);
3211 3212 3213 3214 3215 3216 3217
    if (!hdc) return 1;

    hdcSrc = CreateCompatibleDC(0);

    if (psInfo->ppshheader.dwFlags & PSH_USEHPLWATERMARK) 
	hOldPal = SelectPalette(hdc, psInfo->ppshheader.hplWatermark, FALSE);

3218 3219 3220 3221 3222 3223
    if (psInfo->active_page < 0)
        ppshpage = NULL;
    else
        ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[psInfo->active_page].hpage;

    if ( (ppshpage && !(ppshpage->dwFlags & PSP_HIDEHEADER)) &&
3224 3225
	 (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
	 (psInfo->ppshheader.dwFlags & PSH_HEADER) ) 
3226 3227 3228 3229 3230 3231 3232
    {
	HWND hwndLineHeader = GetDlgItem(hwnd, IDC_SUNKEN_LINEHEADER);
	HFONT hOldFont;
	COLORREF clrOld = 0;
	int oldBkMode = 0;

	hbmp = SelectObject(hdcSrc, psInfo->ppshheader.u5.hbmHeader);
3233
	hOldFont = SelectObject(hdc, psInfo->hFontBold);
3234 3235 3236

	GetClientRect(hwndLineHeader, &r);
	MapWindowPoints(hwndLineHeader, hwnd, (LPPOINT) &r, 2);
3237
	SetRect(&rzone, 0, 0, r.right + 1, r.top - 1);
3238

3239
	GetObjectW(psInfo->ppshheader.u5.hbmHeader, sizeof(BITMAP), &bm);
3240

3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262
 	if (psInfo->ppshheader.dwFlags & PSH_WIZARD97_OLD)
 	{
 	    /* Fill the unoccupied part of the header with color of the
 	     * left-top pixel, but do it only when needed.
 	     */
 	    if (bm.bmWidth < r.right || bm.bmHeight < r.bottom)
 	    {
 	        hbr = CreateSolidBrush(GetPixel(hdcSrc, 0, 0));
 	        CopyRect(&r, &rzone);
 	        if (bm.bmWidth < r.right)
 	        {
 	            r.left = bm.bmWidth;
 	            FillRect(hdc, &r, hbr);
 	        }
 	        if (bm.bmHeight < r.bottom)
 	        {
 	            r.left = 0;
 	            r.top = bm.bmHeight;
 	            FillRect(hdc, &r, hbr);
 	        }
 	        DeleteObject(hbr);
 	    }
3263 3264 3265 3266 3267

 	    /* Draw the header itself. */
 	    BitBlt(hdc, 0, 0,
 	           bm.bmWidth, min(bm.bmHeight, rzone.bottom),
 	           hdcSrc, 0, 0, SRCCOPY);
3268 3269 3270
 	}
 	else
 	{
3271
            int margin;
3272
 	    hbr = GetSysColorBrush(COLOR_WINDOW);
3273
 	    FillRect(hdc, &rzone, hbr);
3274 3275 3276

 	    /* Draw the header bitmap. It's always centered like a
 	     * common 49 x 49 bitmap. */
3277 3278 3279 3280
            margin = (rzone.bottom - 49) / 2;
 	    BitBlt(hdc, rzone.right - 49 - margin, margin,
                   min(bm.bmWidth, 49), min(bm.bmHeight, 49),
                   hdcSrc, 0, 0, SRCCOPY);
3281 3282 3283 3284 3285

 	    /* NOTE: Native COMCTL32 draws a white stripe over the bitmap
 	     * if its height is smaller than 49 pixels. Because the reason
 	     * for this bug is unknown the current code doesn't try to
 	     * replicate it. */
3286
 	}
3287

3288 3289 3290
	clrOld = SetTextColor (hdc, 0x00000000);
	oldBkMode = SetBkMode (hdc, TRANSPARENT); 

3291
	if (ppshpage->dwFlags & PSP_USEHEADERTITLE) {
3292
	    SetRect(&r, 20, 10, 0, 0);
3293
            if (!IS_INTRESOURCE(ppshpage->pszHeaderTitle))
3294
                DrawTextW(hdc, ppshpage->pszHeaderTitle, -1, &r, DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
3295
	    else
3296
	    {
Frank Richter's avatar
Frank Richter committed
3297
		nLength = LoadStringW(ppshpage->hInstance, (UINT_PTR)ppshpage->pszHeaderTitle,
3298 3299 3300
				      szBuffer, 256);
		if (nLength != 0)
		{
3301
		    DrawTextW(hdc, szBuffer, nLength, &r, DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
3302 3303
		}
	    }
3304 3305 3306 3307
	}

	if (ppshpage->dwFlags & PSP_USEHEADERSUBTITLE) {
	    SelectObject(hdc, psInfo->hFont);
3308
	    SetRect(&r, 40, 25, rzone.right - 69, rzone.bottom);
3309
            if (!IS_INTRESOURCE(ppshpage->pszHeaderTitle))
3310
                DrawTextW(hdc, ppshpage->pszHeaderSubTitle, -1, &r, DT_LEFT | DT_WORDBREAK);
3311
	    else
3312
	    {
Frank Richter's avatar
Frank Richter committed
3313
		nLength = LoadStringW(ppshpage->hInstance, (UINT_PTR)ppshpage->pszHeaderSubTitle,
3314 3315 3316
				      szBuffer, 256);
		if (nLength != 0)
		{
3317
		    DrawTextW(hdc, szBuffer, nLength, &r, DT_LEFT | DT_WORDBREAK);
3318 3319
		}
	    }
3320 3321 3322 3323 3324 3325 3326 3327 3328 3329
	}

	offsety = rzone.bottom + 2;

	SetTextColor(hdc, clrOld);
	SetBkMode(hdc, oldBkMode);
	SelectObject(hdc, hOldFont);
	SelectObject(hdcSrc, hbmp);
    }

3330
    if ( (ppshpage && (ppshpage->dwFlags & PSP_HIDEHEADER)) &&
3331 3332
	 (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
	 (psInfo->ppshheader.dwFlags & PSH_WATERMARK) ) 
3333
    {
3334 3335 3336 3337 3338
	HWND hwndLine = GetDlgItem(hwnd, IDC_SUNKEN_LINE);	    

	GetClientRect(hwndLine, &r);
	MapWindowPoints(hwndLine, hwnd, (LPPOINT) &r, 2);

3339 3340 3341 3342 3343 3344 3345 3346
	rzone.left = 0;
	rzone.top = 0;
	rzone.right = r.right;
	rzone.bottom = r.top - 1;

	hbr = GetSysColorBrush(COLOR_WINDOW);
	FillRect(hdc, &rzone, hbr);

3347
	GetObjectW(psInfo->ppshheader.u4.hbmWatermark, sizeof(BITMAP), &bm);
3348 3349
	hbmp = SelectObject(hdcSrc, psInfo->ppshheader.u4.hbmWatermark);

3350 3351
        /* The watermark is truncated to a width of 164 pixels */
        r.right = min(r.right, 164);
3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364
	BitBlt(hdc, 0, offsety, min(bm.bmWidth, r.right),
	       min(bm.bmHeight, r.bottom), hdcSrc, 0, 0, SRCCOPY);

	/* If the bitmap is not big enough, fill the remaining area
	   with the color of pixel (0,0) of bitmap - see MSDN */
	if (r.top > bm.bmHeight) {
	    r.bottom = r.top - 1;
	    r.top = bm.bmHeight;
	    r.left = 0;
	    r.right = bm.bmWidth;
	    hbr = CreateSolidBrush(GetPixel(hdcSrc, 0, 0));
	    FillRect(hdc, &r, hbr);
	    DeleteObject(hbr);
3365
	}
3366 3367

	SelectObject(hdcSrc, hbmp);	    
3368 3369 3370 3371 3372 3373 3374
    }

    if (psInfo->ppshheader.dwFlags & PSH_USEHPLWATERMARK) 
	SelectPalette(hdc, hOldPal, FALSE);

    DeleteDC(hdcSrc);

3375
    if (!hdcParam) EndPaint(hwnd, &ps);
3376 3377 3378 3379

    return 0;
}

3380 3381 3382
/******************************************************************************
 *            PROPSHEET_DialogProc
 */
3383
static INT_PTR CALLBACK
3384 3385
PROPSHEET_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
3386
  TRACE("hwnd=%p msg=0x%04x wparam=%lx lparam=%lx\n",
3387 3388
	hwnd, uMsg, wParam, lParam);

3389 3390 3391 3392 3393
  switch (uMsg)
  {
    case WM_INITDIALOG:
    {
      PropSheetInfo* psInfo = (PropSheetInfo*) lParam;
3394
      WCHAR* strCaption = Alloc(MAX_CAPTION_LENGTH*sizeof(WCHAR));
3395
      HWND hwndTabCtrl = GetDlgItem(hwnd, IDC_TABCONTROL);
Alexandre Julliard's avatar
Alexandre Julliard committed
3396
      int idx;
3397
      LOGFONTW logFont;
3398

3399 3400 3401
      /* Using PropSheetInfoStr to store extra data doesn't match the native
       * common control: native uses TCM_[GS]ETITEM
       */
3402
      SetPropW(hwnd, PropSheetInfoStr, psInfo);
3403

3404
      /*
3405 3406
       * psInfo->hwnd is not being used by WINE code - it exists
       * for compatibility with "real" Windoze. The same about
3407
       * SetWindowLongPtr - WINE is only using the PropSheetInfoStr
3408 3409 3410
       * property.
       */
      psInfo->hwnd = hwnd;
3411
      SetWindowLongPtrW(hwnd, DWLP_USER, (DWORD_PTR)psInfo);
3412

3413 3414 3415 3416 3417
      if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
      {
        /* set up the Next and Back buttons by default */
        PROPSHEET_SetWizButtons(hwnd, PSWIZB_BACK|PSWIZB_NEXT);
      }
3418

3419
      /* Set up fonts */
3420 3421
      SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
      psInfo->hFont = CreateFontIndirectW (&logFont);
3422
      logFont.lfWeight = FW_BOLD;
3423
      psInfo->hFontBold = CreateFontIndirectW (&logFont);
3424
      
3425 3426 3427
      /*
       * Small icon in the title bar.
       */
3428 3429
      if ((psInfo->ppshheader.dwFlags & PSH_USEICONID) ||
          (psInfo->ppshheader.dwFlags & PSH_USEHICON))
3430 3431 3432 3433 3434
      {
        HICON hIcon;
        int icon_cx = GetSystemMetrics(SM_CXSMICON);
        int icon_cy = GetSystemMetrics(SM_CYSMICON);

3435
        if (psInfo->ppshheader.dwFlags & PSH_USEICONID)
3436
          hIcon = LoadImageW(psInfo->ppshheader.hInstance,
3437
                             psInfo->ppshheader.u.pszIcon,
3438 3439 3440 3441
                             IMAGE_ICON,
                             icon_cx, icon_cy,
                             LR_DEFAULTCOLOR);
        else
3442
          hIcon = psInfo->ppshheader.u.hIcon;
3443

3444
        SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)hIcon);
3445
      }
3446

3447
      if (psInfo->ppshheader.dwFlags & PSH_USEHICON)
3448
        SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)psInfo->ppshheader.u.hIcon);
3449

3450 3451
      psInfo->strPropertiesFor = strCaption;

3452
      GetWindowTextW(hwnd, psInfo->strPropertiesFor, MAX_CAPTION_LENGTH);
3453

3454 3455
      PROPSHEET_CreateTabControl(hwnd, psInfo);

3456 3457
      PROPSHEET_LoadWizardBitmaps(psInfo);

3458
      if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
3459
      {
3460
        ShowWindow(hwndTabCtrl, SW_HIDE);
Filip Navara's avatar
Filip Navara committed
3461 3462
        PROPSHEET_AdjustSizeWizard(hwnd, psInfo);
        PROPSHEET_AdjustButtonsWizard(hwnd, psInfo);
3463
        SetFocus(GetDlgItem(hwnd, IDC_NEXT_BUTTON));
3464 3465
      }
      else
3466
      {
3467
        if (PROPSHEET_SizeMismatch(hwnd, psInfo))
3468 3469 3470 3471
        {
          PROPSHEET_AdjustSize(hwnd, psInfo);
          PROPSHEET_AdjustButtons(hwnd, psInfo);
        }
3472
        SetFocus(GetDlgItem(hwnd, IDOK));
3473 3474
      }

3475
      if (IS_INTRESOURCE(psInfo->ppshheader.pszCaption) &&
3476
              psInfo->ppshheader.hInstance)
3477
      {
3478
         WCHAR szText[256];
3479

3480
         if (LoadStringW(psInfo->ppshheader.hInstance,
Frank Richter's avatar
Frank Richter committed
3481
                         (UINT_PTR)psInfo->ppshheader.pszCaption, szText, 255))
3482
            PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags, szText);
3483 3484 3485
      }
      else
      {
3486
         PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags,
3487
                         psInfo->ppshheader.pszCaption);
3488
      }
3489

3490 3491

      if (psInfo->useCallback)
3492
             (*(psInfo->ppshheader.pfnCallback))(hwnd, PSCB_INITIALIZED, 0);
3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503

      idx = psInfo->active_page;
      psInfo->active_page = -1;

      PROPSHEET_SetCurSel(hwnd, idx, 1, psInfo->proppage[idx].hpage);

      /* doing TCM_SETCURSEL seems to be needed even in case of PSH_WIZARD,
       * as some programs call TCM_GETCURSEL to get the current selection
       * from which to switch to the next page */
      SendMessageW(hwndTabCtrl, TCM_SETCURSEL, psInfo->active_page, 0);

3504
      PROPSHEET_UnChanged(hwnd, NULL);
3505

3506 3507 3508 3509
      /* wizards set their focus during init */
      if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
          return FALSE;

3510 3511 3512
      return TRUE;
    }

3513
    case WM_PRINTCLIENT:
3514
    case WM_PAINT:
3515
      PROPSHEET_Paint(hwnd, (HDC)wParam);
3516 3517
      return TRUE;

3518 3519 3520 3521 3522
    case WM_DESTROY:
      PROPSHEET_CleanUp(hwnd);
      return TRUE;

    case WM_CLOSE:
Alexandre Julliard's avatar
Alexandre Julliard committed
3523
      PROPSHEET_Cancel(hwnd, 1);
3524
      return FALSE; /* let DefDlgProc post us WM_COMMAND/IDCANCEL */
3525 3526

    case WM_COMMAND:
3527 3528
      if (!PROPSHEET_DoCommand(hwnd, LOWORD(wParam)))
      {
3529
          PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
3530

3531 3532 3533
          if (!psInfo)
              return FALSE;

3534 3535 3536 3537 3538 3539 3540 3541
          /* No default handler, forward notification to active page */
          if (psInfo->activeValid && psInfo->active_page != -1)
          {
             HWND hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
             SendMessageW(hwndPage, WM_COMMAND, wParam, lParam);
          }
      }
      return TRUE;
3542 3543 3544 3545 3546 3547 3548

    case WM_NOTIFY:
    {
      NMHDR* pnmh = (LPNMHDR) lParam;

      if (pnmh->code == TCN_SELCHANGE)
      {
3549
        int index = SendMessageW(pnmh->hwndFrom, TCM_GETCURSEL, 0, 0);
3550
        PROPSHEET_SetCurSel(hwnd, index, 1, 0);
3551 3552
      }

3553 3554 3555
      if(pnmh->code == TCN_SELCHANGING)
      {
        BOOL bRet = PROPSHEET_CanSetCurSel(hwnd);
3556
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, !bRet);
3557 3558 3559
        return TRUE;
      }

3560
      return FALSE;
3561
    }
3562 3563 3564 3565
  
    case WM_SYSCOLORCHANGE:
      COMCTL32_RefreshSysColors();
      return FALSE;
3566 3567 3568

    case PSM_GETCURRENTPAGEHWND:
    {
3569
      PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
3570 3571
      HWND hwndPage = 0;

3572 3573 3574
      if (!psInfo)
        return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
3575
      if (psInfo->activeValid && psInfo->active_page != -1)
3576 3577
        hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;

3578
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (DWORD_PTR)hwndPage);
3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594

      return TRUE;
    }

    case PSM_CHANGED:
      PROPSHEET_Changed(hwnd, (HWND)wParam);
      return TRUE;

    case PSM_UNCHANGED:
      PROPSHEET_UnChanged(hwnd, (HWND)wParam);
      return TRUE;

    case PSM_GETTABCONTROL:
    {
      HWND hwndTabCtrl = GetDlgItem(hwnd, IDC_TABCONTROL);

3595
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (DWORD_PTR)hwndTabCtrl);
3596 3597 3598 3599 3600 3601 3602 3603

      return TRUE;
    }

    case PSM_SETCURSEL:
    {
      BOOL msgResult;

3604 3605 3606 3607 3608
      msgResult = PROPSHEET_CanSetCurSel(hwnd);
      if(msgResult != FALSE)
      {
        msgResult = PROPSHEET_SetCurSel(hwnd,
                                       (int)wParam,
3609
				       1,
3610 3611
                                       (HPROPSHEETPAGE)lParam);
      }
3612

3613
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3614 3615 3616 3617 3618 3619

      return TRUE;
    }

    case PSM_CANCELTOCLOSE:
    {
3620
      WCHAR buf[MAX_BUTTONTEXT_LENGTH];
3621 3622 3623 3624
      HWND hwndOK = GetDlgItem(hwnd, IDOK);
      HWND hwndCancel = GetDlgItem(hwnd, IDCANCEL);

      EnableWindow(hwndCancel, FALSE);
3625
      if (LoadStringW(COMCTL32_hModule, IDS_CLOSE, buf, sizeof(buf)/sizeof(buf[0])))
3626
         SetWindowTextW(hwndOK, buf);
3627

Alexandre Julliard's avatar
Alexandre Julliard committed
3628
      return FALSE;
3629
    }
3630

3631 3632
    case PSM_RESTARTWINDOWS:
    {
3633
      PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
3634

3635 3636 3637
      if (!psInfo)
        return FALSE;

3638 3639 3640 3641
      /* reboot system takes precedence over restart windows */
      if (psInfo->result != ID_PSREBOOTSYSTEM)
          psInfo->result = ID_PSRESTARTWINDOWS;

3642 3643 3644 3645 3646
      return TRUE;
    }

    case PSM_REBOOTSYSTEM:
    {
3647
      PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
3648

3649 3650 3651
      if (!psInfo)
        return FALSE;

3652 3653
      psInfo->result = ID_PSREBOOTSYSTEM;

3654 3655 3656 3657 3658 3659
      return TRUE;
    }

    case PSM_SETTITLEA:
      PROPSHEET_SetTitleA(hwnd, (DWORD) wParam, (LPCSTR) lParam);
      return TRUE;
3660

3661 3662 3663
    case PSM_SETTITLEW:
      PROPSHEET_SetTitleW(hwnd, (DWORD) wParam, (LPCWSTR) lParam);
      return TRUE;
3664 3665 3666

    case PSM_APPLY:
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
3667
      BOOL msgResult = PROPSHEET_Apply(hwnd, 0);
3668

3669
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3670 3671 3672 3673 3674 3675 3676 3677

      return TRUE;
    }

    case PSM_QUERYSIBLINGS:
    {
      LRESULT msgResult = PROPSHEET_QuerySiblings(hwnd, wParam, lParam);

3678
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3679 3680 3681 3682

      return TRUE;
    }

3683
    case PSM_ADDPAGE:
3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694
    {
      /*
       * Note: MSVC++ 6.0 documentation says that PSM_ADDPAGE does not have
       *       a return value. This is not true. PSM_ADDPAGE returns TRUE
       *       on success or FALSE otherwise, as specified on MSDN Online.
       *       Also see the MFC code for
       *       CPropertySheet::AddPage(CPropertyPage* pPage).
       */

      BOOL msgResult = PROPSHEET_AddPage(hwnd, (HPROPSHEETPAGE)lParam);

3695
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3696

3697
      return TRUE;
3698
    }
3699 3700 3701 3702 3703

    case PSM_REMOVEPAGE:
      PROPSHEET_RemovePage(hwnd, (int)wParam, (HPROPSHEETPAGE)lParam);
      return TRUE;

3704 3705
    case PSM_ISDIALOGMESSAGE:
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
3706
       BOOL msgResult = PROPSHEET_IsDialogMessage(hwnd, (LPMSG)lParam);
3707
       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
Alexandre Julliard's avatar
Alexandre Julliard committed
3708
       return TRUE;
3709 3710 3711 3712 3713 3714
    }

    case PSM_PRESSBUTTON:
      PROPSHEET_PressButton(hwnd, (int)wParam);
      return TRUE;

3715
    case PSM_SETFINISHTEXTA:
3716
      PROPSHEET_SetFinishTextA(hwnd, (LPCSTR) lParam);
3717 3718 3719 3720 3721 3722
      return TRUE;

    case PSM_SETWIZBUTTONS:
      PROPSHEET_SetWizButtons(hwnd, (DWORD)lParam);
      return TRUE;

3723
    case PSM_SETCURSELID:
3724 3725
        PROPSHEET_SetCurSelId(hwnd, (int)lParam);
        return TRUE;
3726

3727
    case PSM_SETFINISHTEXTW:
3728
        PROPSHEET_SetFinishTextW(hwnd, (LPCWSTR) lParam);
3729
        return FALSE;
3730

3731 3732 3733
    case PSM_INSERTPAGE:
    {
        BOOL msgResult = PROPSHEET_InsertPage(hwnd, (HPROPSHEETPAGE)wParam, (HPROPSHEETPAGE)lParam);
3734
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756
        return TRUE;
    }

    case PSM_SETHEADERTITLEW:
        PROPSHEET_SetHeaderTitleW(hwnd, (int)wParam, (LPCWSTR)lParam);
        return TRUE;

    case PSM_SETHEADERTITLEA:
        PROPSHEET_SetHeaderTitleA(hwnd, (int)wParam, (LPCSTR)lParam);
        return TRUE;

    case PSM_SETHEADERSUBTITLEW:
        PROPSHEET_SetHeaderSubTitleW(hwnd, (int)wParam, (LPCWSTR)lParam);
        return TRUE;

    case PSM_SETHEADERSUBTITLEA:
        PROPSHEET_SetHeaderSubTitleA(hwnd, (int)wParam, (LPCSTR)lParam);
        return TRUE;

    case PSM_HWNDTOINDEX:
    {
        LRESULT msgResult = PROPSHEET_HwndToIndex(hwnd, (HWND)wParam);
3757
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3758 3759 3760 3761 3762 3763
        return TRUE;
    }

    case PSM_INDEXTOHWND:
    {
        LRESULT msgResult = PROPSHEET_IndexToHwnd(hwnd, (int)wParam);
3764
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3765 3766 3767 3768 3769 3770
        return TRUE;
    }

    case PSM_PAGETOINDEX:
    {
        LRESULT msgResult = PROPSHEET_PageToIndex(hwnd, (HPROPSHEETPAGE)wParam);
3771
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3772 3773 3774 3775 3776 3777
        return TRUE;
    }

    case PSM_INDEXTOPAGE:
    {
        LRESULT msgResult = PROPSHEET_IndexToPage(hwnd, (int)wParam);
3778
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3779 3780 3781 3782 3783 3784
        return TRUE;
    }

    case PSM_IDTOINDEX:
    {
        LRESULT msgResult = PROPSHEET_IdToIndex(hwnd, (int)lParam);
3785
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3786 3787 3788 3789 3790 3791
        return TRUE;
    }

    case PSM_INDEXTOID:
    {
        LRESULT msgResult = PROPSHEET_IndexToId(hwnd, (int)wParam);
3792
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3793 3794 3795 3796 3797 3798
        return TRUE;
    }

    case PSM_GETRESULT:
    {
        LRESULT msgResult = PROPSHEET_GetResult(hwnd);
3799
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3800 3801 3802 3803 3804 3805
        return TRUE;
    }

    case PSM_RECALCPAGESIZES:
    {
        LRESULT msgResult = PROPSHEET_RecalcPageSizes(hwnd);
3806
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3807 3808 3809
        return TRUE;
    }

3810 3811 3812
    default:
      return FALSE;
  }
3813
}