propsheet.c 102 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 334
     if (HIWORD(lppsh->pszCaption))
     {
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 (HIWORD(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 546
  psInfo->proppage[index].pszText = p;
  TRACE("Tab %d %s\n",index,debugstr_w( p ));
  p += lstrlenW( p ) + 1;
547 548 549 550 551

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

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

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

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

    if (dwFlags & PSP_USEICONID)
584
      hIcon = LoadImageW(lppsp->hInstance, lppsp->u2.pszIcon, IMAGE_ICON,
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
                         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
601

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

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

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

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

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

643
  temp = Alloc(resSize);
644 645

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

  memcpy(temp, template, resSize);

650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
  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;
  }

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

669 670 671 672
  /* 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). */
673 674
  if( psInfo->unicode )
  {
Frank Richter's avatar
Frank Richter committed
675
    ret = (INT_PTR)CreateDialogIndirectParamW(psInfo->ppshheader.hInstance,
676 677
                                          temp, psInfo->ppshheader.hwndParent,
                                          PROPSHEET_DialogProc, (LPARAM)psInfo);
678
    if ( !ret ) ret = -1;
679 680 681
  }
  else
  {
Frank Richter's avatar
Frank Richter committed
682
    ret = (INT_PTR)CreateDialogIndirectParamA(psInfo->ppshheader.hInstance,
683 684
                                          temp, psInfo->ppshheader.hwndParent,
                                          PROPSHEET_DialogProc, (LPARAM)psInfo);
685
    if ( !ret ) ret = -1;
686
  }
687

688
  Free(temp);
689

690 691 692 693
  return ret;
}

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

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

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

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

721
  if ( (rcPage.right - rcPage.left) != (rcOrigTab.right - rcOrigTab.left) )
722
    return TRUE;
723
  if ( (rcPage.bottom - rcPage.top) != (rcOrigTab.bottom - rcOrigTab.top) )
724 725 726 727 728 729 730 731 732 733 734 735 736 737
    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);
738
  RECT rc,tabRect;
739
  int buttonHeight;
740
  PADDING_INFO padding = PROPSHEET_GetPaddingInfo(hwndDlg);
741
  RECT units;
742
  LONG style;
743 744 745 746 747 748 749 750

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

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

  MapDialogRect(hwndDlg, &rc);

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

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

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

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

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

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

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

  GetClientRect(hwndTabCtrl, &rc);

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

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

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

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

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

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

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

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

Filip Navara's avatar
Filip Navara committed
834 835 836 837 838
  /* 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;
839

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

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

849 850 851
  return TRUE;
}

852 853 854 855 856
/******************************************************************************
 *            PROPSHEET_AdjustButtons
 *
 * Adjusts the buttons' positions.
 */
857
static BOOL PROPSHEET_AdjustButtons(HWND hwndParent, const PropSheetInfo* psInfo)
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
{
  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.
881
   */
882 883
  GetClientRect(hwndParent, &rcSheet);

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

  /*
890
   * Position OK button and make it default.
891 892 893 894 895 896 897 898
   */
  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);

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


902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
  /*
   * Position Cancel button.
   */
  hwndButton = GetDlgItem(hwndParent, IDCANCEL);

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

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

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

  if (psInfo->hasApply)
  {
    if (psInfo->hasHelp)
      x = rcSheet.right - ((padding.x + buttonWidth) * 2);
    else
      x = rcSheet.right - (padding.x + buttonWidth);
923

924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
    SetWindowPos(hwndButton, 0, x, y, 0, 0,
                 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

    EnableWindow(hwndButton, FALSE);
  }
  else
    ShowWindow(hwndButton, SW_HIDE);

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

  if (psInfo->hasHelp)
  {
    x = rcSheet.right - (padding.x + buttonWidth);
940

941 942 943 944 945 946 947 948 949
    SetWindowPos(hwndButton, 0, x, y, 0, 0,
                 SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  }
  else
    ShowWindow(hwndButton, SW_HIDE);

  return TRUE;
}

950 951 952 953 954 955
/******************************************************************************
 *            PROPSHEET_AdjustButtonsWizard
 *
 * Adjusts the buttons' positions.
 */
static BOOL PROPSHEET_AdjustButtonsWizard(HWND hwndParent,
956
                                          const PropSheetInfo* psInfo)
957 958 959
{
  HWND hwndButton = GetDlgItem(hwndParent, IDCANCEL);
  HWND hwndLine = GetDlgItem(hwndParent, IDC_SUNKEN_LINE);
960
  HWND hwndLineHeader = GetDlgItem(hwndParent, IDC_SUNKEN_LINEHEADER);
961 962 963 964
  RECT rcSheet;
  int x, y;
  int num_buttons = 3;
  int buttonWidth, buttonHeight, lineHeight, lineWidth;
965
  PADDING_INFO padding = PROPSHEET_GetPaddingInfoWizard(hwndParent, psInfo);
966 967 968

  if (psInfo->hasHelp)
    num_buttons++;
969 970
  if (psInfo->hasFinish)
    num_buttons++;
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990

  /*
   * 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);
991
  
992
  /*
993
   * Position the Back button.
994
   */
995
  hwndButton = GetDlgItem(hwndParent, IDC_BACK_BUTTON);
996

997
  x = rcSheet.right - ((padding.x + buttonWidth) * (num_buttons - 1)) - buttonWidth;
998 999 1000 1001

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

1002 1003 1004 1005 1006 1007 1008
  /*
   * Position the Next button.
   */
  hwndButton = GetDlgItem(hwndParent, IDC_NEXT_BUTTON);
  
  x += buttonWidth;
  
1009 1010 1011 1012
  SetWindowPos(hwndButton, 0, x, y, 0, 0,
               SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

  /*
1013
   * Position the Finish button.
1014
   */
1015 1016 1017 1018
  hwndButton = GetDlgItem(hwndParent, IDC_FINISH_BUTTON);
  
  if (psInfo->hasFinish)
    x += padding.x + buttonWidth;
1019 1020 1021 1022

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

1023 1024 1025
  if (!psInfo->hasFinish)
    ShowWindow(hwndButton, SW_HIDE);

1026 1027 1028 1029 1030
  /*
   * Position the Cancel button.
   */
  hwndButton = GetDlgItem(hwndParent, IDCANCEL);

1031
  x += padding.x + buttonWidth;
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042

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

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

  if (psInfo->hasHelp)
  {
1043
    x += padding.x + buttonWidth;
1044 1045 1046 1047 1048 1049 1050

    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
1051 1052
  if (psInfo->ppshheader.dwFlags &
      (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW | PSH_WIZARD_LITE)) 
1053 1054
      padding.x = 0;

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
  /*
   * 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);

1065 1066 1067 1068 1069 1070 1071 1072 1073
  /*
   * 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);

1074 1075 1076
  return TRUE;
}

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
/******************************************************************************
 *            PROPSHEET_GetPaddingInfo
 *
 * Returns the layout information.
 */
static PADDING_INFO PROPSHEET_GetPaddingInfo(HWND hwndDlg)
{
  HWND hwndTab = GetDlgItem(hwndDlg, IDC_TABCONTROL);
  RECT rcTab;
  POINT tl;
  PADDING_INFO padding;

  GetWindowRect(hwndTab, &rcTab);

  tl.x = rcTab.left;
  tl.y = rcTab.top;

  ScreenToClient(hwndDlg, &tl);

  padding.x = tl.x;
  padding.y = tl.y;

  return padding;
}

1102 1103 1104 1105 1106
/******************************************************************************
 *            PROPSHEET_GetPaddingInfoWizard
 *
 * Returns the layout information.
 * Vertical spacing is the distance between the line and the buttons.
1107 1108 1109 1110 1111 1112
 * 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.
1113
 */
1114 1115
static PADDING_INFO PROPSHEET_GetPaddingInfoWizard(HWND hwndDlg, const PropSheetInfo*
 psInfo)
1116 1117 1118 1119
{
  PADDING_INFO padding;
  RECT rc;
  HWND hwndControl;
1120 1121
  INT idButton;
  POINT ptButton, ptLine;
1122

1123
  TRACE("\n");
1124 1125 1126 1127 1128 1129
  if (psInfo->hasHelp)
  {
	idButton = IDHELP;
  }
  else
  {
1130
    if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
1131 1132 1133 1134 1135 1136 1137 1138 1139
    {
	idButton = IDC_NEXT_BUTTON;
    }
    else
    {
	/* hopefully this is ok */
	idButton = IDCANCEL;
    }
  }
1140

1141
  hwndControl = GetDlgItem(hwndDlg, idButton);
1142 1143
  GetWindowRect(hwndControl, &rc);

1144 1145
  ptButton.x = rc.left;
  ptButton.y = rc.top;
1146

1147
  ScreenToClient(hwndDlg, &ptButton);
1148 1149 1150 1151 1152

  /* Line */
  hwndControl = GetDlgItem(hwndDlg, IDC_SUNKEN_LINE);
  GetWindowRect(hwndControl, &rc);

1153
  ptLine.x = rc.left;
1154 1155 1156 1157
  ptLine.y = rc.bottom;

  ScreenToClient(hwndDlg, &ptLine);

1158 1159 1160 1161
  padding.y = ptButton.y - ptLine.y;

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

1163 1164
  /* this is most probably not correct, but the best we have now */
  padding.x = padding.y;
1165 1166 1167
  return padding;
}

1168 1169 1170 1171 1172 1173
/******************************************************************************
 *            PROPSHEET_CreateTabControl
 *
 * Insert the tabs in the tab control.
 */
static BOOL PROPSHEET_CreateTabControl(HWND hwndParent,
1174
                                       const PropSheetInfo * psInfo)
1175 1176
{
  HWND hwndTabCtrl = GetDlgItem(hwndParent, IDC_TABCONTROL);
1177
  TCITEMW item;
1178
  int i, nTabs;
Alexandre Julliard's avatar
Alexandre Julliard committed
1179
  int iImage = 0;
1180

1181
  TRACE("\n");
1182
  item.mask = TCIF_TEXT;
1183
  item.cchTextMax = MAX_TABTEXT_LENGTH;
1184

1185
  nTabs = psInfo->nPages;
1186

1187 1188 1189 1190 1191
  /*
   * Set the image list for icons.
   */
  if (psInfo->hImageList)
  {
1192
    SendMessageW(hwndTabCtrl, TCM_SETIMAGELIST, 0, (LPARAM)psInfo->hImageList);
1193 1194
  }

1195
  SendMessageW(GetDlgItem(hwndTabCtrl, IDC_TABCONTROL), WM_SETREDRAW, 0, 0);
1196 1197
  for (i = 0; i < nTabs; i++)
  {
Alexandre Julliard's avatar
Alexandre Julliard committed
1198 1199 1200 1201 1202 1203 1204 1205 1206
    if ( psInfo->proppage[i].hasIcon )
    {
      item.mask |= TCIF_IMAGE;
      item.iImage = iImage++;
    }
    else
    {
      item.mask &= ~TCIF_IMAGE;
    }
1207

1208 1209
    item.pszText = (LPWSTR) psInfo->proppage[i].pszText;
    SendMessageW(hwndTabCtrl, TCM_INSERTITEMW, (WPARAM)i, (LPARAM)&item);
1210
  }
1211
  SendMessageW(GetDlgItem(hwndTabCtrl, IDC_TABCONTROL), WM_SETREDRAW, 1, 0);
1212 1213 1214

  return TRUE;
}
1215 1216 1217 1218

/******************************************************************************
 *            PROPSHEET_WizardSubclassProc
 *
1219
 * Subclassing window procedure for wizard exterior pages to prevent drawing
1220 1221
 * background and so drawing above the watermark.
 */
1222
static LRESULT CALLBACK
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
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);
}

1238 1239 1240 1241
/*
 * Get the size of an in-memory Template
 *
 *( Based on the code of PROPSHEET_CollectPageInfo)
1242
 * See also dialog.c/DIALOG_ParseTemplate32().
1243 1244
 */

1245
static UINT GetTemplateSize(const DLGTEMPLATE* pTemplate)
1246 1247 1248

{
  const WORD*  p = (const WORD *)pTemplate;
1249
  BOOL  istemplateex = (((const MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF);
1250
  WORD nrofitems;
1251
  UINT ret;
1252 1253 1254

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

1257
    TRACE("is DLGTEMPLATEEX\n");
1258 1259 1260 1261 1262 1263 1264 1265
    p++;       /* dlgVer    */
    p++;       /* signature */
    p += 2;    /* help ID   */
    p += 2;    /* ext style */
    p += 2;    /* style     */
  }
  else
  {
1266
    /* DLGTEMPLATE */
1267

1268
    TRACE("is DLGTEMPLATE\n");
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
    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:
1289 1290
      TRACE("menu %s\n",debugstr_w( p ));
      p += lstrlenW( p ) + 1;
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
      break;
  }

  /* class */
  switch ((WORD)*p)
  {
    case 0x0000:
      p++;
      break;
    case 0xffff:
1301
      p += 2; /* 0xffff plus predefined window class ordinal value */
1302 1303
      break;
    default:
1304 1305
      TRACE("class %s\n",debugstr_w( p ));
      p += lstrlenW( p ) + 1;
1306 1307 1308
      break;
  }

1309
  /* title */
1310 1311
  TRACE("title %s\n",debugstr_w( p ));
  p += lstrlenW( p ) + 1;
1312

1313
  /* font, if DS_SETFONT set */
1314
  if ((DS_SETFONT & ((istemplateex)?  ((const MyDLGTEMPLATEEX*)pTemplate)->style :
1315 1316 1317
		     pTemplate->style)))
    {
      p+=(istemplateex)?3:1;
1318 1319
      TRACE("font %s\n",debugstr_w( p ));
      p += lstrlenW( p ) + 1; /* the font name */
1320 1321
    }

1322 1323
  /* now process the DLGITEMTEMPLATE(EX) structs (plus custom data)
   * that are following the DLGTEMPLATE(EX) data */
1324 1325 1326
  TRACE("%d items\n",nrofitems);
  while (nrofitems > 0)
    {
Frank Richter's avatar
Frank Richter committed
1327
      p = (WORD*)(((DWORD_PTR)p + 3) & ~3); /* DWORD align */
1328
      
1329
      /* skip header */
1330 1331
      p += (istemplateex ? sizeof(MyDLGITEMTEMPLATEEX) : sizeof(DLGITEMTEMPLATE))/sizeof(WORD);
      
1332
      /* check class */
1333 1334 1335 1336 1337 1338
      switch ((WORD)*p)
	{
	case 0x0000:
	  p++;
	  break;
	case 0xffff:
1339
          TRACE("class ordinal 0x%08x\n",*(const DWORD*)p);
1340 1341 1342
	  p += 2;
	  break;
	default:
1343 1344
	  TRACE("class %s\n",debugstr_w( p ));
	  p += lstrlenW( p ) + 1;
1345 1346
	  break;
	}
1347 1348

      /* check title text */
1349 1350 1351 1352 1353 1354
      switch ((WORD)*p)
	{
	case 0x0000:
	  p++;
	  break;
	case 0xffff:
1355
          TRACE("text ordinal 0x%08x\n",*(const DWORD*)p);
1356 1357 1358
	  p += 2;
	  break;
	default:
1359 1360
	  TRACE("text %s\n",debugstr_w( p ));
	  p += lstrlenW( p ) + 1;
1361 1362
	  break;
	}
1363
      p += *p / sizeof(WORD) + 1;    /* Skip extra data */
1364 1365 1366
      --nrofitems;
    }
  
1367
  ret = (p - (const WORD*)pTemplate) * sizeof(WORD);
1368 1369
  TRACE("%p %p size 0x%08x\n", p, pTemplate, ret);
  return ret;
1370
}
1371 1372 1373 1374

/******************************************************************************
 *            PROPSHEET_CreatePage
 *
1375
 * Creates a page.
1376
 */
1377
static BOOL PROPSHEET_CreatePage(HWND hwndParent,
1378
                                int index,
1379
                                const PropSheetInfo * psInfo,
1380
                                LPCPROPSHEETPAGEW ppshpage)
1381 1382 1383
{
  DLGTEMPLATE* pTemplate;
  HWND hwndPage;
1384 1385
  DWORD resSize;
  LPVOID temp = NULL;
1386

1387
  TRACE("index %d\n", index);
1388

1389 1390 1391 1392 1393
  if (ppshpage == NULL)
  {
    return FALSE;
  }

1394
  if (ppshpage->dwFlags & PSP_DLGINDIRECT)
1395 1396 1397 1398
    {
      pTemplate = (DLGTEMPLATE*)ppshpage->u.pResource;
      resSize = GetTemplateSize(pTemplate);
    }
1399
  else if(ppshpage->dwFlags & PSP_INTERNAL_UNICODE)
1400
  {
1401 1402 1403
    HRSRC hResource;
    HANDLE hTemplate;

1404
    hResource = FindResourceW(ppshpage->hInstance,
1405
                                    ppshpage->u.pszTemplate,
1406
                                    (LPWSTR)RT_DIALOG);
1407 1408 1409 1410 1411 1412 1413 1414 1415
    if(!hResource)
	return FALSE;

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

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

1416
    pTemplate = LockResource(hTemplate);
1417 1418 1419
    /*
     * Make a copy of the dialog template to make it writable
     */
1420
  }
1421 1422 1423 1424 1425 1426
  else
  {
    HRSRC hResource;
    HANDLE hTemplate;

    hResource = FindResourceA(ppshpage->hInstance,
1427
                                    (LPCSTR)ppshpage->u.pszTemplate,
1428
                                    (LPSTR)RT_DIALOG);
1429 1430 1431 1432 1433 1434 1435 1436 1437
    if(!hResource)
	return FALSE;

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

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

1438
    pTemplate = LockResource(hTemplate);
1439 1440 1441 1442
    /*
     * Make a copy of the dialog template to make it writable
     */
  }
1443
  temp = Alloc(resSize);
1444 1445 1446
  if (!temp)
    return FALSE;
  
1447
  TRACE("copying pTemplate %p into temp %p (%d)\n", pTemplate, temp, resSize);
1448 1449
  memcpy(temp, pTemplate, resSize);
  pTemplate = temp;
1450 1451 1452

  if (((MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF)
  {
Filip Navara's avatar
Filip Navara committed
1453
    ((MyDLGTEMPLATEEX*)pTemplate)->style |= WS_CHILD | WS_TABSTOP | DS_CONTROL;
1454 1455 1456 1457
    ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~DS_MODALFRAME;
    ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_CAPTION;
    ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_SYSMENU;
    ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_POPUP;
1458
    ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_DISABLED;
Alexandre Julliard's avatar
Alexandre Julliard committed
1459
    ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_VISIBLE;
1460
    ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_THICKFRAME;
1461 1462

    ((MyDLGTEMPLATEEX*)pTemplate)->exStyle |= WS_EX_CONTROLPARENT;
1463 1464 1465
  }
  else
  {
Filip Navara's avatar
Filip Navara committed
1466
    pTemplate->style |= WS_CHILD | WS_TABSTOP | DS_CONTROL;
1467 1468 1469 1470
    pTemplate->style &= ~DS_MODALFRAME;
    pTemplate->style &= ~WS_CAPTION;
    pTemplate->style &= ~WS_SYSMENU;
    pTemplate->style &= ~WS_POPUP;
1471
    pTemplate->style &= ~WS_DISABLED;
Alexandre Julliard's avatar
Alexandre Julliard committed
1472
    pTemplate->style &= ~WS_VISIBLE;
1473
    pTemplate->style &= ~WS_THICKFRAME;
1474 1475

    pTemplate->dwExtendedStyle |= WS_EX_CONTROLPARENT;
1476 1477 1478
  }

  if (psInfo->proppage[index].useCallback)
1479
    (*(ppshpage->pfnCallback))(0, PSPCB_CREATE,
1480
                               (LPPROPSHEETPAGEW)ppshpage);
1481

1482 1483 1484 1485 1486 1487 1488 1489
  if(ppshpage->dwFlags & PSP_INTERNAL_UNICODE)
     hwndPage = CreateDialogIndirectParamW(ppshpage->hInstance,
					pTemplate,
					hwndParent,
					ppshpage->pfnDlgProc,
					(LPARAM)ppshpage);
  else
     hwndPage = CreateDialogIndirectParamA(ppshpage->hInstance,
1490 1491 1492
					pTemplate,
					hwndParent,
					ppshpage->pfnDlgProc,
Marcus Meissner's avatar
Marcus Meissner committed
1493
					(LPARAM)ppshpage);
1494
  /* Free a no more needed copy */
1495
  Free(temp);
1496

1497
  psInfo->proppage[index].hwndPage = hwndPage;
1498

1499 1500 1501 1502 1503 1504 1505 1506
  /* 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);
  }
1507 1508
  if (!(psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD))
      EnableThemeDialogTexture (hwndPage, ETDT_ENABLETAB);
1509

1510 1511 1512
  return TRUE;
}

1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
/******************************************************************************
 *            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))
    {
1528
      psInfo->ppshheader.u4.hbmWatermark =
Frank Richter's avatar
Frank Richter committed
1529
        CreateMappedBitmap(psInfo->ppshheader.hInstance, (INT_PTR)psInfo->ppshheader.u4.pszbmWatermark, 0, NULL, 0);
1530 1531 1532 1533 1534 1535
    }

    /* Same behavior as for watermarks */
    if ((psInfo->ppshheader.dwFlags & PSH_HEADER) &&
        !(psInfo->ppshheader.dwFlags & PSH_USEHBMHEADER))
    {
1536
      psInfo->ppshheader.u5.hbmHeader =
Frank Richter's avatar
Frank Richter committed
1537
        CreateMappedBitmap(psInfo->ppshheader.hInstance, (INT_PTR)psInfo->ppshheader.u5.pszbmHeader, 0, NULL, 0);
1538 1539 1540 1541 1542
    }
  }
}


1543 1544 1545 1546 1547 1548 1549
/******************************************************************************
 *            PROPSHEET_ShowPage
 *
 * Displays or creates the specified page.
 */
static BOOL PROPSHEET_ShowPage(HWND hwndDlg, int index, PropSheetInfo * psInfo)
{
1550
  HWND hwndTabCtrl;
1551 1552
  HWND hwndLineHeader;
  LPCPROPSHEETPAGEW ppshpage;
1553 1554

  TRACE("active_page %d, index %d\n", psInfo->active_page, index);
1555
  if (index == psInfo->active_page)
1556
  {
1557
      if (GetTopWindow(hwndDlg) != psInfo->proppage[index].hwndPage)
Alexandre Julliard's avatar
Alexandre Julliard committed
1558
          SetWindowPos(psInfo->proppage[index].hwndPage, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1559 1560
      return TRUE;
  }
1561

1562
  ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1563
  if (psInfo->proppage[index].hwndPage == 0)
1564
  {
Alexandre Julliard's avatar
Alexandre Julliard committed
1565
     PROPSHEET_CreatePage(hwndDlg, index, psInfo, ppshpage);
1566
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
1567

1568
  if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
1569 1570 1571 1572
  {
     PROPSHEET_SetTitleW(hwndDlg, psInfo->ppshheader.dwFlags,
                         psInfo->proppage[index].pszText);
  }
1573

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

1577
  ShowWindow(psInfo->proppage[index].hwndPage, SW_SHOW);
1578

1579 1580 1581
  /* 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);
1582
  SendMessageW(hwndTabCtrl, TCM_SETCURSEL, index, 0);
1583

1584
  psInfo->active_page = index;
Alexandre Julliard's avatar
Alexandre Julliard committed
1585
  psInfo->activeValid = TRUE;
1586

1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
  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);
  }

1598 1599 1600
  return TRUE;
}

1601 1602 1603 1604 1605
/******************************************************************************
 *            PROPSHEET_Back
 */
static BOOL PROPSHEET_Back(HWND hwndDlg)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1606
  PSHNOTIFY psn;
1607
  HWND hwndPage;
1608
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1609
  LRESULT result;
1610
  int idx;
1611

1612
  TRACE("active_page %d\n", psInfo->active_page);
Alexandre Julliard's avatar
Alexandre Julliard committed
1613
  if (psInfo->active_page < 0)
1614 1615
     return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1616 1617 1618 1619
  psn.hdr.code     = PSN_WIZBACK;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1620

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

1623
  result = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1624
  if (result == -1)
1625
    return FALSE;
1626 1627 1628 1629
  else if (result == 0)
     idx = psInfo->active_page - 1;
  else
     idx = PROPSHEET_FindPageByResId(psInfo, result);
1630

1631
  if (idx >= 0 && idx < psInfo->nPages)
1632
  {
1633 1634
     if (PROPSHEET_CanSetCurSel(hwndDlg))
        PROPSHEET_SetCurSel(hwndDlg, idx, -1, 0);
1635
  }
1636 1637 1638 1639 1640 1641 1642 1643
  return TRUE;
}

/******************************************************************************
 *            PROPSHEET_Next
 */
static BOOL PROPSHEET_Next(HWND hwndDlg)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1644
  PSHNOTIFY psn;
1645 1646
  HWND hwndPage;
  LRESULT msgResult = 0;
1647
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1648
  int idx;
1649

1650
  TRACE("active_page %d\n", psInfo->active_page);
Alexandre Julliard's avatar
Alexandre Julliard committed
1651
  if (psInfo->active_page < 0)
1652 1653
     return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1654 1655 1656 1657
  psn.hdr.code     = PSN_WIZNEXT;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1658

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

1661
  msgResult = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1662 1663
  if (msgResult == -1)
    return FALSE;
1664 1665 1666 1667
  else if (msgResult == 0)
     idx = psInfo->active_page + 1;
  else
     idx = PROPSHEET_FindPageByResId(psInfo, msgResult);
1668

1669
  if (idx < psInfo->nPages )
1670
  {
1671 1672
     if (PROPSHEET_CanSetCurSel(hwndDlg) != FALSE)
        PROPSHEET_SetCurSel(hwndDlg, idx, 1, 0);
1673
  }
1674 1675 1676 1677 1678 1679 1680 1681 1682

  return TRUE;
}

/******************************************************************************
 *            PROPSHEET_Finish
 */
static BOOL PROPSHEET_Finish(HWND hwndDlg)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1683
  PSHNOTIFY psn;
1684 1685
  HWND hwndPage;
  LRESULT msgResult = 0;
1686
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1687

1688
  TRACE("active_page %d\n", psInfo->active_page);
1689 1690 1691
  if (psInfo->active_page < 0)
     return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1692 1693 1694 1695
  psn.hdr.code     = PSN_WIZFINISH;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1696

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

1699
  msgResult = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1700 1701 1702 1703 1704 1705

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

  if (msgResult != 0)
    return FALSE;

1706 1707
  if (psInfo->result == 0)
      psInfo->result = IDOK;
1708
  if (psInfo->isModeless)
Alexandre Julliard's avatar
Alexandre Julliard committed
1709
    psInfo->activeValid = FALSE;
1710
  else
1711
    psInfo->ended = TRUE;
1712 1713 1714 1715

  return TRUE;
}

1716 1717 1718
/******************************************************************************
 *            PROPSHEET_Apply
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1719
static BOOL PROPSHEET_Apply(HWND hwndDlg, LPARAM lParam)
1720 1721 1722
{
  int i;
  HWND hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1723
  PSHNOTIFY psn;
1724
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1725

1726
  TRACE("active_page %d\n", psInfo->active_page);
1727 1728 1729
  if (psInfo->active_page < 0)
     return FALSE;

Alexandre Julliard's avatar
Alexandre Julliard committed
1730 1731 1732
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1733

1734 1735 1736 1737

  /*
   * Send PSN_KILLACTIVE to the current page.
   */
Alexandre Julliard's avatar
Alexandre Julliard committed
1738
  psn.hdr.code = PSN_KILLACTIVE;
1739 1740 1741

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

1742
  if (SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn) != FALSE)
1743 1744 1745 1746 1747
    return FALSE;

  /*
   * Send PSN_APPLY to all pages.
   */
Alexandre Julliard's avatar
Alexandre Julliard committed
1748 1749
  psn.hdr.code = PSN_APPLY;
  psn.lParam   = lParam;
1750

1751
  for (i = 0; i < psInfo->nPages; i++)
1752 1753
  {
    hwndPage = psInfo->proppage[i].hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1754 1755
    if (hwndPage)
    {
1756
       switch (SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn))
1757 1758 1759 1760 1761 1762 1763
       {
       case PSNRET_INVALID:
           PROPSHEET_ShowPage(hwndDlg, i, psInfo);
           /* fall through */
       case PSNRET_INVALID_NOCHANGEPAGE:
           return FALSE;
       }
Alexandre Julliard's avatar
Alexandre Julliard committed
1764 1765
    }
  }
1766

Alexandre Julliard's avatar
Alexandre Julliard committed
1767 1768 1769 1770 1771 1772 1773 1774 1775
  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;
1776
     SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1777 1778 1779 1780 1781 1782 1783 1784
  }

  return TRUE;
}

/******************************************************************************
 *            PROPSHEET_Cancel
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
1785
static void PROPSHEET_Cancel(HWND hwndDlg, LPARAM lParam)
1786
{
1787
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1788
  HWND hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1789 1790
  PSHNOTIFY psn;
  int i;
1791

1792
  TRACE("active_page %d\n", psInfo->active_page);
1793 1794 1795 1796
  if (psInfo->active_page < 0)
     return;

  hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1797 1798 1799 1800
  psn.hdr.code     = PSN_QUERYCANCEL;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1801

1802
  if (SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn))
1803 1804
    return;

Alexandre Julliard's avatar
Alexandre Julliard committed
1805 1806
  psn.hdr.code = PSN_RESET;
  psn.lParam   = lParam;
1807

Alexandre Julliard's avatar
Alexandre Julliard committed
1808 1809 1810
  for (i = 0; i < psInfo->nPages; i++)
  {
    hwndPage = psInfo->proppage[i].hwndPage;
1811

Alexandre Julliard's avatar
Alexandre Julliard committed
1812
    if (hwndPage)
1813
       SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
Alexandre Julliard's avatar
Alexandre Julliard committed
1814
  }
1815 1816

  if (psInfo->isModeless)
Alexandre Julliard's avatar
Alexandre Julliard committed
1817 1818 1819 1820
  {
     /* makes PSM_GETCURRENTPAGEHWND return NULL */
     psInfo->activeValid = FALSE;
  }
1821
  else
1822
    psInfo->ended = TRUE;
1823 1824
}

1825 1826 1827 1828 1829
/******************************************************************************
 *            PROPSHEET_Help
 */
static void PROPSHEET_Help(HWND hwndDlg)
{
1830
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1831
  HWND hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1832
  PSHNOTIFY psn;
1833

1834
  TRACE("active_page %d\n", psInfo->active_page);
1835 1836 1837 1838
  if (psInfo->active_page < 0)
     return;

  hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1839 1840 1841 1842
  psn.hdr.code     = PSN_HELP;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1843

1844
  SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1845 1846
}

1847 1848 1849 1850 1851 1852
/******************************************************************************
 *            PROPSHEET_Changed
 */
static void PROPSHEET_Changed(HWND hwndDlg, HWND hwndDirtyPage)
{
  int i;
1853
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1854

1855
  TRACE("\n");
1856
  if (!psInfo) return;
1857 1858 1859
  /*
   * Set the dirty flag of this page.
   */
1860
  for (i = 0; i < psInfo->nPages; i++)
1861 1862 1863 1864
  {
    if (psInfo->proppage[i].hwndPage == hwndDirtyPage)
      psInfo->proppage[i].isDirty = TRUE;
  }
1865

1866 1867 1868 1869 1870 1871
  /*
   * Enable the Apply button.
   */
  if (psInfo->hasApply)
  {
    HWND hwndApplyBtn = GetDlgItem(hwndDlg, IDC_APPLY_BUTTON);
1872

1873 1874 1875
    EnableWindow(hwndApplyBtn, TRUE);
  }
}
1876

1877 1878 1879 1880 1881 1882 1883 1884
/******************************************************************************
 *            PROPSHEET_UnChanged
 */
static void PROPSHEET_UnChanged(HWND hwndDlg, HWND hwndCleanPage)
{
  int i;
  BOOL noPageDirty = TRUE;
  HWND hwndApplyBtn = GetDlgItem(hwndDlg, IDC_APPLY_BUTTON);
1885
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
1886

1887
  TRACE("\n");
1888
  if ( !psInfo ) return;
1889
  for (i = 0; i < psInfo->nPages; i++)
1890 1891 1892 1893
  {
    /* set the specified page as clean */
    if (psInfo->proppage[i].hwndPage == hwndCleanPage)
      psInfo->proppage[i].isDirty = FALSE;
1894

1895 1896 1897 1898
    /* look to see if there's any dirty pages */
    if (psInfo->proppage[i].isDirty)
      noPageDirty = FALSE;
  }
1899

1900 1901 1902 1903 1904 1905
  /*
   * Disable Apply button.
   */
  if (noPageDirty)
    EnableWindow(hwndApplyBtn, FALSE);
}
1906

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

1941 1942

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

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

  if (psInfo->active_page < 0)
1970 1971 1972 1973
  {
     res = TRUE;
     goto end;
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
1974

1975 1976 1977 1978
  /*
   * Notify the current page.
   */
  hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
Alexandre Julliard's avatar
Alexandre Julliard committed
1979 1980 1981 1982
  psn.hdr.code     = PSN_KILLACTIVE;
  psn.hdr.hwndFrom = hwndDlg;
  psn.hdr.idFrom   = 0;
  psn.lParam       = 0;
1983

1984
  res = !SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1985 1986 1987 1988

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

1991 1992 1993 1994 1995
/******************************************************************************
 *            PROPSHEET_SetCurSel
 */
static BOOL PROPSHEET_SetCurSel(HWND hwndDlg,
                                int index,
1996 1997 1998
				int skipdir,
                                HPROPSHEETPAGE hpage
				)
1999
{
2000
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2001
  HWND hwndHelp  = GetDlgItem(hwndDlg, IDHELP);
2002
  HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2003

2004
  TRACE("index %d, skipdir %d, hpage %p\n", index, skipdir, hpage);
Alexandre Julliard's avatar
Alexandre Julliard committed
2005
  /* hpage takes precedence over index */
2006
  if (hpage != NULL)
Thuy Nguyen's avatar
Thuy Nguyen committed
2007 2008
    index = PROPSHEET_GetPageIndex(hpage, psInfo);

2009 2010 2011 2012
  if (index < 0 || index >= psInfo->nPages)
  {
    TRACE("Could not find page to select!\n");
    return FALSE;
Thuy Nguyen's avatar
Thuy Nguyen committed
2013 2014
  }

2015 2016 2017 2018 2019
  /* 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;

2020
  while (1) {
2021 2022
    int result;
    PSHNOTIFY psn;
2023
    RECT rc;
Filip Navara's avatar
Filip Navara committed
2024
    LPCPROPSHEETPAGEW ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
2025

2026 2027 2028
    if (hwndTabControl)
	SendMessageW(hwndTabControl, TCM_SETCURSEL, index, 0);

2029 2030 2031 2032 2033
    psn.hdr.code     = PSN_SETACTIVE;
    psn.hdr.hwndFrom = hwndDlg;
    psn.hdr.idFrom   = 0;
    psn.lParam       = 0;

2034 2035 2036
    if (!psInfo->proppage[index].hwndPage) {
      PROPSHEET_CreatePage(hwndDlg, index, psInfo, ppshpage);
    }
2037

Filip Navara's avatar
Filip Navara committed
2038 2039 2040 2041
    /* 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
2042
     * not only when it's created (some applications depend on it). */
Filip Navara's avatar
Filip Navara committed
2043
    PROPSHEET_GetPageRect(psInfo, hwndDlg, &rc, ppshpage);
2044 2045
    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
2046 2047 2048 2049
          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);
2050

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

  /* 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);
  }

2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
  /*
   * Display the new page.
   */
  PROPSHEET_ShowPage(hwndDlg, index, psInfo);

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

  return TRUE;
}

2107 2108 2109 2110 2111 2112 2113 2114
/******************************************************************************
 *            PROPSHEET_SetCurSelId
 *
 * Selects the page, specified by resource id.
 */
static void PROPSHEET_SetCurSelId(HWND hwndDlg, int id)
{
      int idx;
2115
      PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2116 2117 2118 2119 2120 2121 2122 2123 2124

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

2125 2126 2127 2128 2129
/******************************************************************************
 *            PROPSHEET_SetTitleA
 */
static void PROPSHEET_SetTitleA(HWND hwndDlg, DWORD dwStyle, LPCSTR lpszText)
{
2130 2131 2132
  if(HIWORD(lpszText))
  {
     WCHAR szTitle[256];
2133
     MultiByteToWideChar(CP_ACP, 0, lpszText, -1,
2134
                         szTitle, sizeof(szTitle)/sizeof(WCHAR));
2135 2136 2137 2138 2139 2140 2141
     PROPSHEET_SetTitleW(hwndDlg, dwStyle, szTitle);
  }
  else
  {
     PROPSHEET_SetTitleW(hwndDlg, dwStyle, (LPCWSTR)lpszText);
  }
}
2142

2143 2144 2145 2146 2147
/******************************************************************************
 *            PROPSHEET_SetTitleW
 */
static void PROPSHEET_SetTitleW(HWND hwndDlg, DWORD dwStyle, LPCWSTR lpszText)
{
2148 2149
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
  WCHAR szTitle[256];
2150

2151
  TRACE("%s (style %08x)\n", debugstr_w(lpszText), dwStyle);
2152
  if (HIWORD(lpszText) == 0) {
2153
    if (!LoadStringW(psInfo->ppshheader.hInstance,
2154
                     LOWORD(lpszText), szTitle, sizeof(szTitle)/sizeof(szTitle[0])))
2155 2156 2157
      return;
    lpszText = szTitle;
  }
2158 2159
  if (dwStyle & PSH_PROPTITLE)
  {
2160 2161 2162
    WCHAR* dest;
    int lentitle = strlenW(lpszText);
    int lenprop  = strlenW(psInfo->strPropertiesFor);
2163

2164
    dest = Alloc( (lentitle + lenprop + 1)*sizeof (WCHAR));
2165
    wsprintfW(dest, psInfo->strPropertiesFor, lpszText);
2166

2167
    SetWindowTextW(hwndDlg, dest);
2168
    Free(dest);
2169 2170
  }
  else
2171
    SetWindowTextW(hwndDlg, lpszText);
2172 2173
}

2174 2175 2176 2177 2178
/******************************************************************************
 *            PROPSHEET_SetFinishTextA
 */
static void PROPSHEET_SetFinishTextA(HWND hwndDlg, LPCSTR lpszText)
{
2179
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2180 2181
  HWND hwndButton = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);

2182
  TRACE("'%s'\n", lpszText);
2183 2184 2185 2186 2187 2188
  /* Set text, show and enable the Finish button */
  SetWindowTextA(hwndButton, lpszText);
  ShowWindow(hwndButton, SW_SHOW);
  EnableWindow(hwndButton, TRUE);

  /* Make it default pushbutton */
2189
  SendMessageW(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2190 2191 2192 2193 2194

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

2195 2196 2197 2198 2199 2200
  if (!psInfo->hasFinish)
  {
    /* Hide Next button */
    hwndButton = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
    ShowWindow(hwndButton, SW_HIDE);
  }
2201 2202
}

2203 2204 2205 2206 2207
/******************************************************************************
 *            PROPSHEET_SetFinishTextW
 */
static void PROPSHEET_SetFinishTextW(HWND hwndDlg, LPCWSTR lpszText)
{
2208
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2209 2210
  HWND hwndButton = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);

2211
  TRACE("%s\n", debugstr_w(lpszText));
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
  /* 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);

2224 2225 2226 2227 2228 2229
  if (!psInfo->hasFinish)
  {
    /* Hide Next button */
    hwndButton = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
    ShowWindow(hwndButton, SW_HIDE);
  }
2230 2231
}

2232 2233 2234 2235 2236 2237 2238 2239 2240
/******************************************************************************
 *            PROPSHEET_QuerySiblings
 */
static LRESULT PROPSHEET_QuerySiblings(HWND hwndDlg,
                                       WPARAM wParam, LPARAM lParam)
{
  int i = 0;
  HWND hwndPage;
  LRESULT msgResult = 0;
2241
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2242

2243
  while ((i < psInfo->nPages) && (msgResult == 0))
2244 2245
  {
    hwndPage = psInfo->proppage[i].hwndPage;
2246
    msgResult = SendMessageW(hwndPage, PSM_QUERYSIBLINGS, wParam, lParam);
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259
    i++;
  }

  return msgResult;
}


/******************************************************************************
 *            PROPSHEET_AddPage
 */
static BOOL PROPSHEET_AddPage(HWND hwndDlg,
                              HPROPSHEETPAGE hpage)
{
2260
  PropPageInfo * ppi;
2261
  PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2262
  HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2263 2264
  TCITEMW item;
  LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)hpage;
2265

2266
  TRACE("hpage %p\n", hpage);
2267 2268 2269
  /*
   * Allocate and fill in a new PropPageInfo entry.
   */
2270
  ppi = ReAlloc(psInfo->proppage, sizeof(PropPageInfo) * (psInfo->nPages + 1));
2271 2272 2273 2274
  if (!ppi)
      return FALSE;

  psInfo->proppage = ppi;
2275
  if (!PROPSHEET_CollectPageInfo(ppsp, psInfo, psInfo->nPages, FALSE))
2276
      return FALSE;
2277

2278 2279
  psInfo->proppage[psInfo->nPages].hpage = hpage;

Alexandre Julliard's avatar
Alexandre Julliard committed
2280 2281 2282 2283 2284
  if (ppsp->dwFlags & PSP_PREMATURE)
  {
     /* Create the page but don't show it */
     PROPSHEET_CreatePage(hwndDlg, psInfo->nPages, psInfo, ppsp);
  }
2285 2286 2287 2288 2289

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

2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303
  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;
  }

2304
  SendMessageW(hwndTabControl, TCM_INSERTITEMW, psInfo->nPages + 1,
2305 2306 2307 2308
               (LPARAM)&item);

  psInfo->nPages++;

Alexandre Julliard's avatar
Alexandre Julliard committed
2309 2310
  /* If it is the only page - show it */
  if(psInfo->nPages == 1)
2311
     PROPSHEET_SetCurSel(hwndDlg, 0, 1, 0);
2312
  return TRUE;
2313 2314 2315 2316 2317 2318 2319 2320 2321
}

/******************************************************************************
 *            PROPSHEET_RemovePage
 */
static BOOL PROPSHEET_RemovePage(HWND hwndDlg,
                                 int index,
                                 HPROPSHEETPAGE hpage)
{
2322
  PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2323
  HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2324
  PropPageInfo* oldPages;
2325

2326
  TRACE("index %d, hpage %p\n", index, hpage);
2327 2328 2329
  if (!psInfo) {
    return FALSE;
  }
2330 2331 2332 2333 2334 2335
  /*
   * hpage takes precedence over index.
   */
  if (hpage != 0)
  {
    index = PROPSHEET_GetPageIndex(hpage, psInfo);
2336
  }
2337

2338
  /* Make sure that index is within range */
2339
  if (index < 0 || index >= psInfo->nPages)
Alexandre Julliard's avatar
Alexandre Julliard committed
2340
  {
2341
      TRACE("Could not find page to remove!\n");
2342
      return FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2343
  }
2344

2345
  TRACE("total pages %d removing page %d active page %d\n",
2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356
        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  */
2357
        PROPSHEET_SetCurSel(hwndDlg, index - 1, -1, 0);
2358 2359 2360 2361
      }
      else
      {
        /* activate the next page */
2362
        PROPSHEET_SetCurSel(hwndDlg, index + 1, 1, 0);
2363
        psInfo->active_page = index;
2364 2365 2366 2367
      }
    }
    else
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
2368 2369 2370
      psInfo->active_page = -1;
      if (!psInfo->isModeless)
      {
2371
         psInfo->ended = TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2372 2373
         return TRUE;
      }
2374 2375
    }
  }
Alexandre Julliard's avatar
Alexandre Julliard committed
2376
  else if (index < psInfo->active_page)
2377 2378
    psInfo->active_page--;

2379 2380 2381 2382 2383 2384 2385 2386 2387
  /* 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
2388 2389 2390 2391 2392 2393
  /* Destroy page dialog window */
  DestroyWindow(psInfo->proppage[index].hwndPage);

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

2396
     if (psp->dwFlags & PSP_USETITLE)
2397
        Free ((LPVOID)psInfo->proppage[index].pszText);
Alexandre Julliard's avatar
Alexandre Julliard committed
2398 2399 2400

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

2402
  /* Remove the tab */
2403
  SendMessageW(hwndTabControl, TCM_DELETEITEM, index, 0);
2404

2405
  oldPages = psInfo->proppage;
2406
  psInfo->nPages--;
2407
  psInfo->proppage = Alloc(sizeof(PropPageInfo) * psInfo->nPages);
2408

2409
  if (index > 0)
2410 2411 2412 2413 2414 2415
    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));

2416
  Free(oldPages);
2417

2418 2419 2420
  return FALSE;
}

2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
/******************************************************************************
 *            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)
{
2431
  PropSheetInfo* psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
Alexandre Julliard's avatar
Alexandre Julliard committed
2432 2433 2434
  HWND hwndBack   = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
  HWND hwndNext   = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
  HWND hwndFinish = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2435

2436
  TRACE("%d\n", dwFlags);
2437

Alexandre Julliard's avatar
Alexandre Julliard committed
2438 2439 2440 2441
  EnableWindow(hwndBack, FALSE);
  EnableWindow(hwndNext, FALSE);
  EnableWindow(hwndFinish, FALSE);

2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452
  /* 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);


2453
  if (dwFlags & PSWIZB_BACK)
Alexandre Julliard's avatar
Alexandre Julliard committed
2454
    EnableWindow(hwndBack, TRUE);
2455 2456

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

2459
  if (!psInfo->hasFinish)
2460
  {
2461 2462 2463 2464 2465 2466 2467
    if ((dwFlags & PSWIZB_FINISH) || (dwFlags & PSWIZB_DISABLEDFINISH))
    {
      /* Hide the Next button */
      ShowWindow(hwndNext, SW_HIDE);
      
      /* Show the Finish button */
      ShowWindow(hwndFinish, SW_SHOW);
2468

2469 2470 2471
      if (!(dwFlags & PSWIZB_DISABLEDFINISH))
        EnableWindow(hwndFinish, TRUE);
    }
2472 2473 2474 2475 2476 2477 2478
    else
    {
      /* Hide the Finish button */
      ShowWindow(hwndFinish, SW_HIDE);
      /* Show the Next button */
      ShowWindow(hwndNext, SW_SHOW);
    }
2479
  }
2480 2481
  else if (!(dwFlags & PSWIZB_DISABLEDFINISH))
    EnableWindow(hwndFinish, TRUE);
2482 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 2526 2527 2528 2529 2530 2531 2532
/******************************************************************************
 *            PROPSHEET_InsertPage
 */
static BOOL PROPSHEET_InsertPage(HWND hwndDlg, HPROPSHEETPAGE hpageInsertAfter, HPROPSHEETPAGE hpage)
{
    if (!HIWORD(hpageInsertAfter))
        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)
{
2533
    int index;
2534
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2535 2536 2537 2538 2539 2540 2541

    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);
2542 2543 2544 2545 2546 2547 2548 2549
    return -1;
}

/******************************************************************************
 *            PROPSHEET_IndexToHwnd
 */
static LRESULT PROPSHEET_IndexToHwnd(HWND hwndDlg, int iPageIndex)
{
2550
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2551 2552 2553 2554 2555 2556
    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].hwndPage;
2557 2558 2559 2560 2561 2562 2563
}

/******************************************************************************
 *            PROPSHEET_PageToIndex
 */
static LRESULT PROPSHEET_PageToIndex(HWND hwndDlg, HPROPSHEETPAGE hPage)
{
2564
    int index;
2565
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2566 2567 2568 2569 2570 2571 2572

    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);
2573 2574 2575 2576 2577 2578 2579 2580
    return -1;
}

/******************************************************************************
 *            PROPSHEET_IndexToPage
 */
static LRESULT PROPSHEET_IndexToPage(HWND hwndDlg, int iPageIndex)
{
2581
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2582 2583 2584 2585 2586 2587
    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;
2588 2589 2590 2591 2592 2593 2594
}

/******************************************************************************
 *            PROPSHEET_IdToIndex
 */
static LRESULT PROPSHEET_IdToIndex(HWND hwndDlg, int iPageId)
{
2595 2596
    int index;
    LPCPROPSHEETPAGEW psp;
2597
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2598 2599 2600
    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
2601
        if (psp->u.pszTemplate == MAKEINTRESOURCEW(iPageId))
2602 2603 2604
            return index;
    }

2605 2606 2607 2608 2609 2610 2611 2612
    return -1;
}

/******************************************************************************
 *            PROPSHEET_IndexToId
 */
static LRESULT PROPSHEET_IndexToId(HWND hwndDlg, int iPageIndex)
{
2613
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
    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;
    if (psp->dwFlags & PSP_DLGINDIRECT || HIWORD(psp->u.pszTemplate)) {
        return 0;
    }
    return (LRESULT)psp->u.pszTemplate;
2625 2626 2627 2628 2629 2630 2631
}

/******************************************************************************
 *            PROPSHEET_GetResult
 */
static LRESULT PROPSHEET_GetResult(HWND hwndDlg)
{
2632
    PropSheetInfo * psInfo = GetPropW(hwndDlg, PropSheetInfoStr);
2633
    return psInfo->result;
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644
}

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

2645 2646 2647 2648 2649 2650
/******************************************************************************
 *            PROPSHEET_GetPageIndex
 *
 * Given a HPROPSHEETPAGE, returns the index of the corresponding page from
 * the array of PropPageInfo.
 */
2651
static int PROPSHEET_GetPageIndex(HPROPSHEETPAGE hpage, const PropSheetInfo* psInfo)
2652 2653 2654 2655
{
  BOOL found = FALSE;
  int index = 0;

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

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

  return index;
}

2671 2672 2673 2674 2675
/******************************************************************************
 *            PROPSHEET_CleanUp
 */
static void PROPSHEET_CleanUp(HWND hwndDlg)
{
Alexandre Julliard's avatar
Alexandre Julliard committed
2676
  int i;
2677
  PropSheetInfo* psInfo = RemovePropW(hwndDlg, PropSheetInfoStr);
2678 2679

  TRACE("\n");
2680
  if (!psInfo) return;
2681
  if (HIWORD(psInfo->ppshheader.pszCaption))
2682
      Free ((LPVOID)psInfo->ppshheader.pszCaption);
Alexandre Julliard's avatar
Alexandre Julliard committed
2683 2684 2685 2686 2687

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

2688 2689 2690 2691 2692 2693 2694 2695 2696
     /* 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
2697 2698 2699 2700 2701
     if(psInfo->proppage[i].hwndPage)
        DestroyWindow(psInfo->proppage[i].hwndPage);

     if(psp)
     {
2702
        if (psp->dwFlags & PSP_USETITLE)
2703
           Free ((LPVOID)psInfo->proppage[i].pszText);
Alexandre Julliard's avatar
Alexandre Julliard committed
2704 2705 2706 2707 2708

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

2709 2710 2711 2712 2713 2714 2715 2716 2717 2718
  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);

2719 2720
  Free(psInfo->proppage);
  Free(psInfo->strPropertiesFor);
2721
  ImageList_Destroy(psInfo->hImageList);
2722

2723
  GlobalFree(psInfo);
2724 2725
}

2726
static INT do_loop(const PropSheetInfo *psInfo)
2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
{
    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;
    }

2750 2751 2752
    if(ret != -1)
        ret = psInfo->result;

2753 2754 2755 2756
    DestroyWindow(hwnd);
    return ret;
}

2757 2758 2759 2760 2761 2762 2763 2764
/******************************************************************************
 *            PROPSHEET_PropertySheet
 *
 * Common code between PropertySheetA/W
 */
static INT_PTR PROPSHEET_PropertySheet(PropSheetInfo* psInfo, BOOL unicode)
{
  INT_PTR bRet = 0;
2765
  HWND parent = NULL;
2766 2767 2768 2769 2770 2771 2772 2773
  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)
  {
2774
      parent = psInfo->ppshheader.hwndParent;
2775
      if (parent) EnableWindow(parent, FALSE);
2776 2777 2778 2779
  }
  bRet = PROPSHEET_CreateDialog(psInfo);
  if(!psInfo->isModeless)
  {
2780 2781 2782 2783 2784 2785
      bRet = do_loop(psInfo);
      if (parent) EnableWindow(parent, TRUE);
  }
  return bRet;
}

2786
/******************************************************************************
2787 2788
 *            PropertySheet    (COMCTL32.@)
 *            PropertySheetA   (COMCTL32.@)
2789 2790 2791 2792 2793 2794 2795 2796 2797
 *
 * 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.
2798
 */
Frank Richter's avatar
Frank Richter committed
2799
INT_PTR WINAPI PropertySheetA(LPCPROPSHEETHEADERA lppsh)
2800
{
2801
  PropSheetInfo* psInfo = GlobalAlloc(GPTR, sizeof(PropSheetInfo));
2802
  UINT i, n;
2803
  const BYTE* pByte;
2804

2805 2806 2807
  TRACE("(%p)\n", lppsh);

  PROPSHEET_CollectSheetInfoA(lppsh, psInfo);
2808

2809
  psInfo->proppage = Alloc(sizeof(PropPageInfo) * lppsh->nPages);
2810
  pByte = (const BYTE*) psInfo->ppshheader.u3.ppsp;
2811

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

2822
    if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2823
                               psInfo, n, TRUE))
2824
    {
2825
	if (psInfo->usePropPage)
2826 2827 2828 2829
	    DestroyPropertySheetPage(psInfo->proppage[n].hpage);
	n--;
	psInfo->nPages--;
    }
2830
  }
2831

2832
  return PROPSHEET_PropertySheet(psInfo, FALSE);
2833 2834
}

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

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

  PROPSHEET_CollectSheetInfoW(lppsh, psInfo);

2850
  psInfo->proppage = Alloc(sizeof(PropPageInfo) * lppsh->nPages);
2851
  pByte = (const BYTE*) psInfo->ppshheader.u3.ppsp;
2852

2853 2854
  for (n = i = 0; i < lppsh->nPages; i++, n++)
  {
2855
    if (!psInfo->usePropPage)
2856 2857 2858 2859
      psInfo->proppage[n].hpage = psInfo->ppshheader.u3.phpage[i];
    else
    {
       psInfo->proppage[n].hpage = CreatePropertySheetPageW((LPCPROPSHEETPAGEW)pByte);
2860
       pByte += ((LPCPROPSHEETPAGEW)pByte)->dwSize;
2861 2862
    }

2863
    if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2864
                               psInfo, n, TRUE))
2865
    {
2866
	if (psInfo->usePropPage)
2867 2868 2869 2870 2871 2872
	    DestroyPropertySheetPage(psInfo->proppage[n].hpage);
	n--;
	psInfo->nPages--;
    }
  }

2873
  return PROPSHEET_PropertySheet(psInfo, TRUE);
2874 2875
}

2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
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);
2886
        UINT len;
2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911

        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;
}


2912
/******************************************************************************
2913 2914
 *            CreatePropertySheetPage    (COMCTL32.@)
 *            CreatePropertySheetPageA   (COMCTL32.@)
2915 2916 2917 2918 2919 2920 2921 2922 2923 2924
 *
 * 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.
2925
 */
2926 2927
HPROPSHEETPAGE WINAPI CreatePropertySheetPageA(
                          LPCPROPSHEETPAGEA lpPropSheetPage)
2928
{
2929
  PROPSHEETPAGEW* ppsp = Alloc(sizeof(PROPSHEETPAGEW));
2930

2931
  memcpy(ppsp,lpPropSheetPage,min(lpPropSheetPage->dwSize,sizeof(PROPSHEETPAGEA)));
2932

2933 2934
  ppsp->dwFlags &= ~ PSP_INTERNAL_UNICODE;

2935 2936 2937 2938 2939
    if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) )
    {
        if (HIWORD( ppsp->u.pszTemplate ))
        {
            int len = strlen(lpPropSheetPage->u.pszTemplate) + 1;
2940 2941 2942
            char *template = Alloc( len );

            ppsp->u.pszTemplate = (LPWSTR)strcpy( template, lpPropSheetPage->u.pszTemplate );
2943 2944
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
2945

2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956
    if (ppsp->dwFlags & PSP_USEICONID)
    {
        if (HIWORD( ppsp->u2.pszIcon ))
            PROPSHEET_AtoW(&ppsp->u2.pszIcon, lpPropSheetPage->u2.pszIcon);
    }

    if (ppsp->dwFlags & PSP_USETITLE)
    {
        if (HIWORD( ppsp->pszTitle ))
            PROPSHEET_AtoW( &ppsp->pszTitle, lpPropSheetPage->pszTitle );
        else
2957
            ppsp->pszTitle = load_string( ppsp->hInstance, ppsp->pszTitle );
2958 2959 2960
    }
    else
        ppsp->pszTitle = NULL;
Alexandre Julliard's avatar
Alexandre Julliard committed
2961

2962 2963 2964 2965 2966 2967 2968 2969
    if (ppsp->dwFlags & PSP_HIDEHEADER)
        ppsp->dwFlags &= ~(PSP_USEHEADERTITLE | PSP_USEHEADERSUBTITLE);

    if (ppsp->dwFlags & PSP_USEHEADERTITLE)
    {
        if (HIWORD( ppsp->pszHeaderTitle ))
            PROPSHEET_AtoW(&ppsp->pszHeaderTitle, lpPropSheetPage->pszHeaderTitle);
        else
2970
            ppsp->pszHeaderTitle = load_string( ppsp->hInstance, ppsp->pszHeaderTitle );
2971 2972 2973 2974 2975 2976 2977 2978 2979
    }
    else
        ppsp->pszHeaderTitle = NULL;

    if (ppsp->dwFlags & PSP_USEHEADERSUBTITLE)
    {
        if (HIWORD( ppsp->pszHeaderSubTitle ))
            PROPSHEET_AtoW(&ppsp->pszHeaderSubTitle, lpPropSheetPage->pszHeaderSubTitle);
        else
2980
            ppsp->pszHeaderSubTitle = load_string( ppsp->hInstance, ppsp->pszHeaderSubTitle );
2981 2982 2983 2984 2985
    }
    else
        ppsp->pszHeaderSubTitle = NULL;

    return (HPROPSHEETPAGE)ppsp;
2986 2987
}

2988
/******************************************************************************
2989
 *            CreatePropertySheetPageW   (COMCTL32.@)
2990 2991
 *
 * See CreatePropertySheetA.
2992
 */
2993
HPROPSHEETPAGE WINAPI CreatePropertySheetPageW(LPCPROPSHEETPAGEW lpPropSheetPage)
2994
{
2995
  PROPSHEETPAGEW* ppsp = Alloc(sizeof(PROPSHEETPAGEW));
2996 2997 2998

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

2999 3000
  ppsp->dwFlags |= PSP_INTERNAL_UNICODE;

3001 3002 3003 3004 3005
    if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) )
    {
        if (HIWORD( ppsp->u.pszTemplate ))
        {
            int len = strlenW(lpPropSheetPage->u.pszTemplate) + 1;
3006 3007 3008
            WCHAR *template = Alloc( len * sizeof (WCHAR) );

            ppsp->u.pszTemplate = strcpyW( template, lpPropSheetPage->u.pszTemplate );
3009 3010
        }
    }
3011

3012 3013 3014 3015 3016
    if ( ppsp->dwFlags & PSP_USEICONID )
    {
        if (HIWORD( ppsp->u2.pszIcon ))
        {
            int len = strlenW(lpPropSheetPage->u2.pszIcon) + 1;
3017 3018 3019
            WCHAR *icon = Alloc( len * sizeof (WCHAR) );

            ppsp->u2.pszIcon = strcpyW( icon, lpPropSheetPage->u2.pszIcon );
3020 3021
        }
    }
3022

3023
    if (ppsp->dwFlags & PSP_USETITLE)
3024
        ppsp->pszTitle = load_string( ppsp->hInstance, ppsp->pszTitle );
3025 3026 3027 3028 3029 3030 3031
    else
        ppsp->pszTitle = NULL;

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

    if (ppsp->dwFlags & PSP_USEHEADERTITLE)
3032
        ppsp->pszHeaderTitle = load_string( ppsp->hInstance, ppsp->pszHeaderTitle );
3033 3034 3035 3036
    else
        ppsp->pszHeaderTitle = NULL;

    if (ppsp->dwFlags & PSP_USEHEADERSUBTITLE)
3037
        ppsp->pszHeaderSubTitle = load_string( ppsp->hInstance, ppsp->pszHeaderSubTitle );
3038 3039
    else
        ppsp->pszHeaderSubTitle = NULL;
3040

3041
    return (HPROPSHEETPAGE)ppsp;
3042 3043
}

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

  if (!psp)
     return FALSE;

3062
  if ( !(psp->dwFlags & PSP_DLGINDIRECT) && HIWORD( psp->u.pszTemplate ) )
3063
     Free ((LPVOID)psp->u.pszTemplate);
Alexandre Julliard's avatar
Alexandre Julliard committed
3064 3065

  if ( (psp->dwFlags & PSP_USEICONID) && HIWORD( psp->u2.pszIcon ) )
3066
     Free ((LPVOID)psp->u2.pszIcon);
Alexandre Julliard's avatar
Alexandre Julliard committed
3067 3068

  if ((psp->dwFlags & PSP_USETITLE) && HIWORD( psp->pszTitle ))
3069
     Free ((LPVOID)psp->pszTitle);
Alexandre Julliard's avatar
Alexandre Julliard committed
3070

3071
  Free(hPropPage);
3072 3073

  return TRUE;
3074 3075
}

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

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

   if (lpMsg->message == WM_KEYDOWN && (GetKeyState(VK_CONTROL) & 0x8000))
   {
      int new_page = 0;
3090
      INT dlgCode = SendMessageW(lpMsg->hwnd, WM_GETDLGCODE, 0, (LPARAM)lpMsg);
Alexandre Julliard's avatar
Alexandre Julliard committed
3091 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

      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;

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

         return TRUE;
      }
   }

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

3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146
/******************************************************************************
 *            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)
		{
3147
                    PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
3148

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

		    if (psInfo->isModeless)
			psInfo->activeValid = FALSE;
		    else
3156
                        psInfo->ended = TRUE;
3157 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
		}
	    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;
3183 3184 3185

    default:
        return FALSE;
3186 3187 3188 3189 3190
    }

    return TRUE;
}

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

3209
    if (psInfo->active_page < 0) return 1;
3210
    hdc = hdcParam ? hdcParam : BeginPaint(hwnd, &ps);
3211 3212 3213 3214 3215 3216 3217 3218 3219
    if (!hdc) return 1;

    hdcSrc = CreateCompatibleDC(0);
    ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[psInfo->active_page].hpage;

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

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

	hbmp = SelectObject(hdcSrc, psInfo->ppshheader.u5.hbmHeader);
3229
	hOldFont = SelectObject(hdc, psInfo->hFontBold);
3230 3231 3232

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

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

3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258
 	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);
 	    }
3259 3260 3261 3262 3263

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

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

 	    /* 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. */
3282
 	}
3283

3284 3285 3286
	clrOld = SetTextColor (hdc, 0x00000000);
	oldBkMode = SetBkMode (hdc, TRANSPARENT); 

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

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

	offsety = rzone.bottom + 2;

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

3326
    if ( (ppshpage->dwFlags & PSP_HIDEHEADER) &&
3327 3328
	 (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
	 (psInfo->ppshheader.dwFlags & PSH_WATERMARK) ) 
3329
    {
3330 3331 3332 3333 3334
	HWND hwndLine = GetDlgItem(hwnd, IDC_SUNKEN_LINE);	    

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

3335 3336 3337 3338 3339 3340 3341 3342
	rzone.left = 0;
	rzone.top = 0;
	rzone.right = r.right;
	rzone.bottom = r.top - 1;

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

3343
	GetObjectW(psInfo->ppshheader.u4.hbmWatermark, sizeof(BITMAP), &bm);
3344 3345
	hbmp = SelectObject(hdcSrc, psInfo->ppshheader.u4.hbmWatermark);

3346 3347
        /* The watermark is truncated to a width of 164 pixels */
        r.right = min(r.right, 164);
3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360
	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);
3361
	}
3362 3363

	SelectObject(hdcSrc, hbmp);	    
3364 3365 3366 3367 3368 3369 3370
    }

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

    DeleteDC(hdcSrc);

3371
    if (!hdcParam) EndPaint(hwnd, &ps);
3372 3373 3374 3375

    return 0;
}

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

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

3395 3396 3397
      /* Using PropSheetInfoStr to store extra data doesn't match the native
       * common control: native uses TCM_[GS]ETITEM
       */
3398
      SetPropW(hwnd, PropSheetInfoStr, psInfo);
3399

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

3409 3410 3411
      /* set up the Next and Back buttons by default */
      PROPSHEET_SetWizButtons(hwnd, PSWIZB_BACK|PSWIZB_NEXT);

3412
      /* Set up fonts */
3413 3414
      SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
      psInfo->hFont = CreateFontIndirectW (&logFont);
3415
      logFont.lfWeight = FW_BOLD;
3416
      psInfo->hFontBold = CreateFontIndirectW (&logFont);
3417
      
3418 3419 3420
      /*
       * Small icon in the title bar.
       */
3421 3422
      if ((psInfo->ppshheader.dwFlags & PSH_USEICONID) ||
          (psInfo->ppshheader.dwFlags & PSH_USEHICON))
3423 3424 3425 3426 3427
      {
        HICON hIcon;
        int icon_cx = GetSystemMetrics(SM_CXSMICON);
        int icon_cy = GetSystemMetrics(SM_CYSMICON);

3428
        if (psInfo->ppshheader.dwFlags & PSH_USEICONID)
3429
          hIcon = LoadImageW(psInfo->ppshheader.hInstance,
3430
                             psInfo->ppshheader.u.pszIcon,
3431 3432 3433 3434
                             IMAGE_ICON,
                             icon_cx, icon_cy,
                             LR_DEFAULTCOLOR);
        else
3435
          hIcon = psInfo->ppshheader.u.hIcon;
3436

3437
        SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)hIcon);
3438
      }
3439

3440
      if (psInfo->ppshheader.dwFlags & PSH_USEHICON)
3441
        SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)psInfo->ppshheader.u.hIcon);
3442

3443 3444
      psInfo->strPropertiesFor = strCaption;

3445
      GetWindowTextW(hwnd, psInfo->strPropertiesFor, MAX_CAPTION_LENGTH);
3446

3447 3448
      PROPSHEET_CreateTabControl(hwnd, psInfo);

3449 3450
      PROPSHEET_LoadWizardBitmaps(psInfo);

3451
      if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
3452
      {
3453
        ShowWindow(hwndTabCtrl, SW_HIDE);
Filip Navara's avatar
Filip Navara committed
3454 3455
        PROPSHEET_AdjustSizeWizard(hwnd, psInfo);
        PROPSHEET_AdjustButtonsWizard(hwnd, psInfo);
3456 3457
      }
      else
3458
      {
3459
        if (PROPSHEET_SizeMismatch(hwnd, psInfo))
3460 3461 3462 3463
        {
          PROPSHEET_AdjustSize(hwnd, psInfo);
          PROPSHEET_AdjustButtons(hwnd, psInfo);
        }
3464 3465
      }

3466 3467
      if (!HIWORD(psInfo->ppshheader.pszCaption) &&
              psInfo->ppshheader.hInstance)
3468
      {
3469
         WCHAR szText[256];
3470

3471
         if (LoadStringW(psInfo->ppshheader.hInstance,
Frank Richter's avatar
Frank Richter committed
3472
                         (UINT_PTR)psInfo->ppshheader.pszCaption, szText, 255))
3473
            PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags, szText);
3474 3475 3476
      }
      else
      {
3477
         PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags,
3478
                         psInfo->ppshheader.pszCaption);
3479
      }
3480

3481 3482

      if (psInfo->useCallback)
3483
             (*(psInfo->ppshheader.pfnCallback))(hwnd, PSCB_INITIALIZED, 0);
3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494

      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);

3495 3496
      PROPSHEET_UnChanged(hwnd, (HWND)wParam);

3497 3498 3499
      return TRUE;
    }

3500
    case WM_PRINTCLIENT:
3501
    case WM_PAINT:
3502
      PROPSHEET_Paint(hwnd, (HDC)wParam);
3503 3504
      return TRUE;

3505 3506 3507 3508 3509
    case WM_DESTROY:
      PROPSHEET_CleanUp(hwnd);
      return TRUE;

    case WM_CLOSE:
Alexandre Julliard's avatar
Alexandre Julliard committed
3510
      PROPSHEET_Cancel(hwnd, 1);
3511
      return FALSE; /* let DefDlgProc post us WM_COMMAND/IDCANCEL */
3512 3513

    case WM_COMMAND:
3514 3515
      if (!PROPSHEET_DoCommand(hwnd, LOWORD(wParam)))
      {
3516
          PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
3517

3518 3519 3520
          if (!psInfo)
              return FALSE;

3521 3522 3523 3524 3525 3526 3527 3528
          /* 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;
3529 3530 3531 3532 3533 3534 3535

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

      if (pnmh->code == TCN_SELCHANGE)
      {
3536
        int index = SendMessageW(pnmh->hwndFrom, TCM_GETCURSEL, 0, 0);
3537
        PROPSHEET_SetCurSel(hwnd, index, 1, 0);
3538 3539
      }

3540 3541 3542
      if(pnmh->code == TCN_SELCHANGING)
      {
        BOOL bRet = PROPSHEET_CanSetCurSel(hwnd);
3543
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, !bRet);
3544 3545 3546
        return TRUE;
      }

3547
      return FALSE;
3548
    }
3549 3550 3551 3552
  
    case WM_SYSCOLORCHANGE:
      COMCTL32_RefreshSysColors();
      return FALSE;
3553 3554 3555

    case PSM_GETCURRENTPAGEHWND:
    {
3556
      PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
3557 3558
      HWND hwndPage = 0;

3559 3560 3561
      if (!psInfo)
        return FALSE;

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

3565
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (DWORD_PTR)hwndPage);
3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581

      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);

3582
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (DWORD_PTR)hwndTabCtrl);
3583 3584 3585 3586 3587 3588 3589 3590

      return TRUE;
    }

    case PSM_SETCURSEL:
    {
      BOOL msgResult;

3591 3592 3593 3594 3595
      msgResult = PROPSHEET_CanSetCurSel(hwnd);
      if(msgResult != FALSE)
      {
        msgResult = PROPSHEET_SetCurSel(hwnd,
                                       (int)wParam,
3596
				       1,
3597 3598
                                       (HPROPSHEETPAGE)lParam);
      }
3599

3600
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3601 3602 3603 3604 3605 3606

      return TRUE;
    }

    case PSM_CANCELTOCLOSE:
    {
3607
      WCHAR buf[MAX_BUTTONTEXT_LENGTH];
3608 3609 3610 3611
      HWND hwndOK = GetDlgItem(hwnd, IDOK);
      HWND hwndCancel = GetDlgItem(hwnd, IDCANCEL);

      EnableWindow(hwndCancel, FALSE);
3612
      if (LoadStringW(COMCTL32_hModule, IDS_CLOSE, buf, sizeof(buf)/sizeof(buf[0])))
3613
         SetWindowTextW(hwndOK, buf);
3614

Alexandre Julliard's avatar
Alexandre Julliard committed
3615
      return FALSE;
3616
    }
3617

3618 3619
    case PSM_RESTARTWINDOWS:
    {
3620
      PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
3621

3622 3623 3624
      if (!psInfo)
        return FALSE;

3625 3626 3627 3628
      /* reboot system takes precedence over restart windows */
      if (psInfo->result != ID_PSREBOOTSYSTEM)
          psInfo->result = ID_PSRESTARTWINDOWS;

3629 3630 3631 3632 3633
      return TRUE;
    }

    case PSM_REBOOTSYSTEM:
    {
3634
      PropSheetInfo* psInfo = GetPropW(hwnd, PropSheetInfoStr);
3635

3636 3637 3638
      if (!psInfo)
        return FALSE;

3639 3640
      psInfo->result = ID_PSREBOOTSYSTEM;

3641 3642 3643 3644 3645 3646
      return TRUE;
    }

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

3648 3649 3650
    case PSM_SETTITLEW:
      PROPSHEET_SetTitleW(hwnd, (DWORD) wParam, (LPCWSTR) lParam);
      return TRUE;
3651 3652 3653

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

3656
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3657 3658 3659 3660 3661 3662 3663 3664

      return TRUE;
    }

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

3665
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3666 3667 3668 3669

      return TRUE;
    }

3670
    case PSM_ADDPAGE:
3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681
    {
      /*
       * 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);

3682
      SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3683

3684
      return TRUE;
3685
    }
3686 3687 3688 3689 3690

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

3691 3692
    case PSM_ISDIALOGMESSAGE:
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
3693
       BOOL msgResult = PROPSHEET_IsDialogMessage(hwnd, (LPMSG)lParam);
3694
       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
Alexandre Julliard's avatar
Alexandre Julliard committed
3695
       return TRUE;
3696 3697 3698 3699 3700 3701
    }

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

3702
    case PSM_SETFINISHTEXTA:
3703
      PROPSHEET_SetFinishTextA(hwnd, (LPCSTR) lParam);
3704 3705 3706 3707 3708 3709
      return TRUE;

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

3710
    case PSM_SETCURSELID:
3711 3712
        PROPSHEET_SetCurSelId(hwnd, (int)lParam);
        return TRUE;
3713

3714
    case PSM_SETFINISHTEXTW:
3715
        PROPSHEET_SetFinishTextW(hwnd, (LPCWSTR) lParam);
3716
        return FALSE;
3717

3718 3719 3720
    case PSM_INSERTPAGE:
    {
        BOOL msgResult = PROPSHEET_InsertPage(hwnd, (HPROPSHEETPAGE)wParam, (HPROPSHEETPAGE)lParam);
3721
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743
        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);
3744
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3745 3746 3747 3748 3749 3750
        return TRUE;
    }

    case PSM_INDEXTOHWND:
    {
        LRESULT msgResult = PROPSHEET_IndexToHwnd(hwnd, (int)wParam);
3751
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3752 3753 3754 3755 3756 3757
        return TRUE;
    }

    case PSM_PAGETOINDEX:
    {
        LRESULT msgResult = PROPSHEET_PageToIndex(hwnd, (HPROPSHEETPAGE)wParam);
3758
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3759 3760 3761 3762 3763 3764
        return TRUE;
    }

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

    case PSM_IDTOINDEX:
    {
        LRESULT msgResult = PROPSHEET_IdToIndex(hwnd, (int)lParam);
3772
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3773 3774 3775 3776 3777 3778
        return TRUE;
    }

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

    case PSM_GETRESULT:
    {
        LRESULT msgResult = PROPSHEET_GetResult(hwnd);
3786
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3787 3788 3789 3790 3791 3792
        return TRUE;
    }

    case PSM_RECALCPAGESIZES:
    {
        LRESULT msgResult = PROPSHEET_RecalcPageSizes(hwnd);
3793
        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3794 3795 3796
        return TRUE;
    }

3797 3798 3799
    default:
      return FALSE;
  }
3800
}