Fix MsiGetPropertyA/W spec declarations.
[wine] / dlls / comctl32 / propsheet.c
1 /*
2  * Property Sheets
3  *
4  * Copyright 1998 Francis Beaudet
5  * Copyright 1999 Thuy Nguyen
6  * Copyright 2004 Maxime Bellenge
7  * Copyright 2004 Filip Navara
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  * This code was audited for completeness against the documented features
24  * of Comctl32.dll version 6.0 on Sep. 12, 2004, by Filip Navara.
25  * 
26  * Unless otherwise noted, we believe this code to be complete, as per
27  * the specification mentioned above.
28  * If you discover missing features, or bugs, please note them below.
29  *
30  * TODO:
31  *   - Tab order
32  *   - Wizard 97 header resizing
33  *   - Enforcing of minimal wizard size
34  *   - Messages:
35  *     o PSM_GETRESULT
36  *     o PSM_INSERTPAGE
37  *     o PSM_RECALCPAGESIZES
38  *     o PSM_SETHEADERSUBTITLE
39  *     o PSM_SETHEADERTITLE
40  *     o WM_HELP
41  *     o WM_CONTEXTMENU
42  *   - Notifications:
43  *     o PSN_GETOBJECT
44  *     o PSN_QUERYINITIALFOCUS
45  *     o PSN_TRANSLATEACCELERATOR
46  *   - Styles:
47  *     o PSH_RTLREADING
48  *     o PSH_STRETCHWATERMARK
49  *     o PSH_USEPAGELANG
50  *     o PSH_USEPSTARTPAGE
51  *   - Page styles:
52  *     o PSP_USEFUSIONCONTEXT
53  *     o PSP_USEREFPARENT
54  */
55
56 #include <stdarg.h>
57 #include <string.h>
58
59 #define NONAMELESSUNION
60 #define NONAMELESSSTRUCT
61 #include "windef.h"
62 #include "winbase.h"
63 #include "wingdi.h"
64 #include "winuser.h"
65 #include "winnls.h"
66 #include "commctrl.h"
67 #include "prsht.h"
68 #include "comctl32.h"
69 #include "uxtheme.h"
70
71 #include "wine/debug.h"
72 #include "wine/unicode.h"
73
74 /******************************************************************************
75  * Data structures
76  */
77 #include "pshpack2.h"
78
79 typedef struct
80 {
81   WORD dlgVer;
82   WORD signature;
83   DWORD helpID;
84   DWORD exStyle;
85   DWORD style;
86 } MyDLGTEMPLATEEX;
87
88 typedef struct
89 {
90   DWORD helpid;
91   DWORD exStyle;
92   DWORD style;
93   short x;
94   short y;
95   short cx;
96   short cy;
97   DWORD id;
98 } MyDLGITEMTEMPLATEEX;
99 #include "poppack.h"
100
101 typedef struct tagPropPageInfo
102 {
103   HPROPSHEETPAGE hpage; /* to keep track of pages not passed to PropertySheet */
104   HWND hwndPage;
105   BOOL isDirty;
106   LPCWSTR pszText;
107   BOOL hasHelp;
108   BOOL useCallback;
109   BOOL hasIcon;
110 } PropPageInfo;
111
112 typedef struct tagPropSheetInfo
113 {
114   HWND hwnd;
115   PROPSHEETHEADERW ppshheader;
116   BOOL unicode;
117   LPWSTR strPropertiesFor;
118   int nPages;
119   int active_page;
120   BOOL isModeless;
121   BOOL hasHelp;
122   BOOL hasApply;
123   BOOL hasFinish;
124   BOOL useCallback;
125   BOOL restartWindows;
126   BOOL rebootSystem;
127   BOOL activeValid;
128   PropPageInfo* proppage;
129   HFONT hFont;
130   HFONT hFontBold;
131   int width;
132   int height;
133   HIMAGELIST hImageList;
134   BOOL ended;
135 } PropSheetInfo;
136
137 typedef struct
138 {
139   int x;
140   int y;
141 } PADDING_INFO;
142
143 /******************************************************************************
144  * Defines and global variables
145  */
146
147 const WCHAR PropSheetInfoStr[] =
148     {'P','r','o','p','e','r','t','y','S','h','e','e','t','I','n','f','o',0 };
149
150 #define PSP_INTERNAL_UNICODE 0x80000000
151
152 #define MAX_CAPTION_LENGTH 255
153 #define MAX_TABTEXT_LENGTH 255
154 #define MAX_BUTTONTEXT_LENGTH 64
155
156 #define INTRNL_ANY_WIZARD (PSH_WIZARD | PSH_WIZARD97_OLD | PSH_WIZARD97_NEW | PSH_WIZARD_LITE)
157
158 /* Wizard metrics specified in DLUs */
159 #define WIZARD_PADDING 7
160 #define WIZARD_HEADER_HEIGHT 36
161                                 
162 /******************************************************************************
163  * Prototypes
164  */
165 static INT_PTR PROPSHEET_CreateDialog(PropSheetInfo* psInfo);
166 static BOOL PROPSHEET_SizeMismatch(HWND hwndDlg, PropSheetInfo* psInfo);
167 static BOOL PROPSHEET_AdjustSize(HWND hwndDlg, PropSheetInfo* psInfo);
168 static BOOL PROPSHEET_AdjustButtons(HWND hwndParent, PropSheetInfo* psInfo);
169 static BOOL PROPSHEET_CollectSheetInfoA(LPCPROPSHEETHEADERA lppsh,
170                                        PropSheetInfo * psInfo);
171 static BOOL PROPSHEET_CollectSheetInfoW(LPCPROPSHEETHEADERW lppsh,
172                                        PropSheetInfo * psInfo);
173 static BOOL PROPSHEET_CollectPageInfo(LPCPROPSHEETPAGEW lppsp,
174                                       PropSheetInfo * psInfo,
175                                       int index);
176 static BOOL PROPSHEET_CreateTabControl(HWND hwndParent,
177                                        PropSheetInfo * psInfo);
178 static BOOL PROPSHEET_CreatePage(HWND hwndParent, int index,
179                                 const PropSheetInfo * psInfo,
180                                 LPCPROPSHEETPAGEW ppshpage);
181 static BOOL PROPSHEET_ShowPage(HWND hwndDlg, int index, PropSheetInfo * psInfo);
182 static PADDING_INFO PROPSHEET_GetPaddingInfo(HWND hwndDlg);
183 static BOOL PROPSHEET_Back(HWND hwndDlg);
184 static BOOL PROPSHEET_Next(HWND hwndDlg);
185 static BOOL PROPSHEET_Finish(HWND hwndDlg);
186 static BOOL PROPSHEET_Apply(HWND hwndDlg, LPARAM lParam);
187 static void PROPSHEET_Cancel(HWND hwndDlg, LPARAM lParam);
188 static void PROPSHEET_Help(HWND hwndDlg);
189 static void PROPSHEET_Changed(HWND hwndDlg, HWND hwndDirtyPage);
190 static void PROPSHEET_UnChanged(HWND hwndDlg, HWND hwndCleanPage);
191 static void PROPSHEET_PressButton(HWND hwndDlg, int buttonID);
192 static void PROPSHEET_SetFinishTextA(HWND hwndDlg, LPCSTR lpszText);
193 static void PROPSHEET_SetFinishTextW(HWND hwndDlg, LPCWSTR lpszText);
194 static void PROPSHEET_SetTitleA(HWND hwndDlg, DWORD dwStyle, LPCSTR lpszText);
195 static void PROPSHEET_SetTitleW(HWND hwndDlg, DWORD dwStyle, LPCWSTR lpszText);
196 static BOOL PROPSHEET_CanSetCurSel(HWND hwndDlg);
197 static BOOL PROPSHEET_SetCurSel(HWND hwndDlg,
198                                 int index,
199                                 int skipdir,
200                                 HPROPSHEETPAGE hpage);
201 static void PROPSHEET_SetCurSelId(HWND hwndDlg, int id);
202 static LRESULT PROPSHEET_QuerySiblings(HWND hwndDlg,
203                                        WPARAM wParam, LPARAM lParam);
204 static BOOL PROPSHEET_AddPage(HWND hwndDlg,
205                               HPROPSHEETPAGE hpage);
206
207 static BOOL PROPSHEET_RemovePage(HWND hwndDlg,
208                                  int index,
209                                  HPROPSHEETPAGE hpage);
210 static void PROPSHEET_CleanUp(HWND hwndDlg);
211 static int PROPSHEET_GetPageIndex(HPROPSHEETPAGE hpage, PropSheetInfo* psInfo);
212 static void PROPSHEET_SetWizButtons(HWND hwndDlg, DWORD dwFlags);
213 static PADDING_INFO PROPSHEET_GetPaddingInfoWizard(HWND hwndDlg, const PropSheetInfo* psInfo);
214 static BOOL PROPSHEET_IsDialogMessage(HWND hwnd, LPMSG lpMsg);
215 static BOOL PROPSHEET_DoCommand(HWND hwnd, WORD wID);
216
217 INT_PTR CALLBACK
218 PROPSHEET_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
219
220 WINE_DEFAULT_DEBUG_CHANNEL(propsheet);
221
222 #define add_flag(a) if (dwFlags & a) {strcat(string, #a );strcat(string," ");}
223 /******************************************************************************
224  *            PROPSHEET_UnImplementedFlags
225  *
226  * Document use of flags we don't implement yet.
227  */
228 static VOID PROPSHEET_UnImplementedFlags(DWORD dwFlags)
229 {
230     CHAR string[256];
231
232     string[0] = '\0';
233
234   /*
235    * unhandled header flags:
236    *  PSH_RTLREADING         0x00000800
237    *  PSH_STRETCHWATERMARK   0x00040000
238    *  PSH_USEPAGELANG        0x00200000
239    */
240
241     add_flag(PSH_RTLREADING);
242     add_flag(PSH_STRETCHWATERMARK);
243     add_flag(PSH_USEPAGELANG);
244     if (string[0] != '\0')
245         FIXME("%s\n", string);
246 }
247 #undef add_flag
248
249 /******************************************************************************
250  *            PROPSHEET_GetPageRect
251  *
252  * Retrieve rect from tab control and map into the dialog for SetWindowPos
253  */
254 static void PROPSHEET_GetPageRect(const PropSheetInfo * psInfo, HWND hwndDlg,
255                                   RECT *rc, LPCPROPSHEETPAGEW ppshpage)
256 {
257     if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD) {     
258         HWND hwndChild;
259         RECT r;
260
261         if (((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
262              (psInfo->ppshheader.dwFlags & PSH_HEADER) &&
263              !(ppshpage->dwFlags & PSP_HIDEHEADER)) ||
264             (psInfo->ppshheader.dwFlags & PSH_WIZARD))
265         {
266             rc->left = rc->top = WIZARD_PADDING;
267         }
268         else
269         {
270             rc->left = rc->top = 0;
271         }
272         rc->right = psInfo->width - rc->left;
273         rc->bottom = psInfo->height - rc->top;
274         MapDialogRect(hwndDlg, rc);
275
276         if ((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
277             (psInfo->ppshheader.dwFlags & PSH_HEADER) &&
278             !(ppshpage->dwFlags & PSP_HIDEHEADER))
279         {
280             hwndChild = GetDlgItem(hwndDlg, IDC_SUNKEN_LINEHEADER);
281             GetClientRect(hwndChild, &r);
282             MapWindowPoints(hwndChild, hwndDlg, (LPPOINT) &r, 2);
283             rc->top += r.bottom + 1;
284         }
285     } else {
286         HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
287         GetClientRect(hwndTabCtrl, rc);
288         SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, FALSE, (LPARAM)rc);
289         MapWindowPoints(hwndTabCtrl, hwndDlg, (LPPOINT)rc, 2);
290     }
291 }
292
293 /******************************************************************************
294  *            PROPSHEET_FindPageByResId
295  *
296  * Find page index corresponding to page resource id.
297  */
298 static INT PROPSHEET_FindPageByResId(PropSheetInfo * psInfo, LRESULT resId)
299 {
300    INT i;
301
302    for (i = 0; i < psInfo->nPages; i++)
303    {
304       LPCPROPSHEETPAGEA lppsp = (LPCPROPSHEETPAGEA)psInfo->proppage[i].hpage;
305
306       /* Fixme: if resource ID is a string shall we use strcmp ??? */
307       if (lppsp->u.pszTemplate == (LPVOID)resId)
308          break;
309    }
310
311    return i;
312 }
313
314 /******************************************************************************
315  *            PROPSHEET_AtoW
316  *
317  * Convert ASCII to Unicode since all data is saved as Unicode.
318  */
319 static void PROPSHEET_AtoW(LPCWSTR *tostr, LPCSTR frstr)
320 {
321     INT len;
322
323     TRACE("<%s>\n", frstr);
324     len = MultiByteToWideChar(CP_ACP, 0, frstr, -1, 0, 0);
325     *tostr = Alloc(len * sizeof(WCHAR));
326     MultiByteToWideChar(CP_ACP, 0, frstr, -1, (LPWSTR)*tostr, len);
327 }
328
329 /******************************************************************************
330  *            PROPSHEET_CollectSheetInfoA
331  *
332  * Collect relevant data.
333  */
334 static BOOL PROPSHEET_CollectSheetInfoA(LPCPROPSHEETHEADERA lppsh,
335                                        PropSheetInfo * psInfo)
336 {
337   DWORD dwSize = min(lppsh->dwSize,sizeof(PROPSHEETHEADERA));
338   DWORD dwFlags = lppsh->dwFlags;
339
340   psInfo->hasHelp = dwFlags & PSH_HASHELP;
341   psInfo->hasApply = !(dwFlags & PSH_NOAPPLYNOW);
342   psInfo->hasFinish = dwFlags & PSH_WIZARDHASFINISH;
343   psInfo->useCallback = (dwFlags & PSH_USECALLBACK )&& (lppsh->pfnCallback);
344   psInfo->isModeless = dwFlags & PSH_MODELESS;
345
346   memcpy(&psInfo->ppshheader,lppsh,dwSize);
347   TRACE("\n** PROPSHEETHEADER **\ndwSize\t\t%ld\ndwFlags\t\t%08lx\nhwndParent\t%p\nhInstance\t%p\npszCaption\t'%s'\nnPages\t\t%d\npfnCallback\t%p\n",
348         lppsh->dwSize, lppsh->dwFlags, lppsh->hwndParent, lppsh->hInstance,
349         debugstr_a(lppsh->pszCaption), lppsh->nPages, lppsh->pfnCallback);
350
351   PROPSHEET_UnImplementedFlags(lppsh->dwFlags);
352
353   if (lppsh->dwFlags & INTRNL_ANY_WIZARD)
354      psInfo->ppshheader.pszCaption = NULL;
355   else
356   {
357      if (HIWORD(lppsh->pszCaption))
358      {
359         int len = MultiByteToWideChar(CP_ACP, 0, lppsh->pszCaption, -1, NULL, 0);
360         psInfo->ppshheader.pszCaption = Alloc( len*sizeof (WCHAR) );
361         MultiByteToWideChar(CP_ACP, 0, lppsh->pszCaption, -1, (LPWSTR) psInfo->ppshheader.pszCaption, len);
362      }
363   }
364   psInfo->nPages = lppsh->nPages;
365
366   if (dwFlags & PSH_USEPSTARTPAGE)
367   {
368     TRACE("PSH_USEPSTARTPAGE is on\n");
369     psInfo->active_page = 0;
370   }
371   else
372     psInfo->active_page = lppsh->u2.nStartPage;
373
374   if (psInfo->active_page < 0 || psInfo->active_page >= psInfo->nPages)
375      psInfo->active_page = 0;
376
377   psInfo->restartWindows = FALSE;
378   psInfo->rebootSystem = FALSE;
379   psInfo->hImageList = 0;
380   psInfo->activeValid = FALSE;
381
382   return TRUE;
383 }
384
385 /******************************************************************************
386  *            PROPSHEET_CollectSheetInfoW
387  *
388  * Collect relevant data.
389  */
390 static BOOL PROPSHEET_CollectSheetInfoW(LPCPROPSHEETHEADERW lppsh,
391                                        PropSheetInfo * psInfo)
392 {
393   DWORD dwSize = min(lppsh->dwSize,sizeof(PROPSHEETHEADERW));
394   DWORD dwFlags = lppsh->dwFlags;
395
396   psInfo->hasHelp = dwFlags & PSH_HASHELP;
397   psInfo->hasApply = !(dwFlags & PSH_NOAPPLYNOW);
398   psInfo->hasFinish = dwFlags & PSH_WIZARDHASFINISH;
399   psInfo->useCallback = (dwFlags & PSH_USECALLBACK) && (lppsh->pfnCallback);
400   psInfo->isModeless = dwFlags & PSH_MODELESS;
401
402   memcpy(&psInfo->ppshheader,lppsh,dwSize);
403   TRACE("\n** PROPSHEETHEADER **\ndwSize\t\t%ld\ndwFlags\t\t%08lx\nhwndParent\t%p\nhInstance\t%p\npszCaption\t'%s'\nnPages\t\t%d\npfnCallback\t%p\n",
404       lppsh->dwSize, lppsh->dwFlags, lppsh->hwndParent, lppsh->hInstance, debugstr_w(lppsh->pszCaption), lppsh->nPages, lppsh->pfnCallback);
405
406   PROPSHEET_UnImplementedFlags(lppsh->dwFlags);
407
408   if (lppsh->dwFlags & INTRNL_ANY_WIZARD)
409      psInfo->ppshheader.pszCaption = NULL;
410   else
411   {
412      if (HIWORD(lppsh->pszCaption))
413      {
414         int len = strlenW(lppsh->pszCaption);
415         psInfo->ppshheader.pszCaption = Alloc( (len+1)*sizeof(WCHAR) );
416         strcpyW( (WCHAR *)psInfo->ppshheader.pszCaption, lppsh->pszCaption );
417      }
418   }
419   psInfo->nPages = lppsh->nPages;
420
421   if (dwFlags & PSH_USEPSTARTPAGE)
422   {
423     TRACE("PSH_USEPSTARTPAGE is on\n");
424     psInfo->active_page = 0;
425   }
426   else
427     psInfo->active_page = lppsh->u2.nStartPage;
428
429   if (psInfo->active_page < 0 || psInfo->active_page >= psInfo->nPages)
430      psInfo->active_page = 0;
431
432   psInfo->restartWindows = FALSE;
433   psInfo->rebootSystem = FALSE;
434   psInfo->hImageList = 0;
435   psInfo->activeValid = FALSE;
436
437   return TRUE;
438 }
439
440 /******************************************************************************
441  *            PROPSHEET_CollectPageInfo
442  *
443  * Collect property sheet data.
444  * With code taken from DIALOG_ParseTemplate32.
445  */
446 BOOL PROPSHEET_CollectPageInfo(LPCPROPSHEETPAGEW lppsp,
447                                PropSheetInfo * psInfo,
448                                int index)
449 {
450   DLGTEMPLATE* pTemplate;
451   const WORD*  p;
452   DWORD dwFlags;
453   int width, height;
454
455   if (!lppsp)
456     return FALSE;
457
458   TRACE("\n");
459   psInfo->proppage[index].hpage = (HPROPSHEETPAGE)lppsp;
460   psInfo->proppage[index].hwndPage = 0;
461   psInfo->proppage[index].isDirty = FALSE;
462
463   /*
464    * Process property page flags.
465    */
466   dwFlags = lppsp->dwFlags;
467   psInfo->proppage[index].useCallback = (dwFlags & PSP_USECALLBACK) && (lppsp->pfnCallback);
468   psInfo->proppage[index].hasHelp = dwFlags & PSP_HASHELP;
469   psInfo->proppage[index].hasIcon = dwFlags & (PSP_USEHICON | PSP_USEICONID);
470
471   /* as soon as we have a page with the help flag, set the sheet flag on */
472   if (psInfo->proppage[index].hasHelp)
473     psInfo->hasHelp = TRUE;
474
475   /*
476    * Process page template.
477    */
478   if (dwFlags & PSP_DLGINDIRECT)
479     pTemplate = (DLGTEMPLATE*)lppsp->u.pResource;
480   else if(dwFlags & PSP_INTERNAL_UNICODE )
481   {
482     HRSRC hResource = FindResourceW(lppsp->hInstance,
483                                     lppsp->u.pszTemplate,
484                                     (LPWSTR)RT_DIALOG);
485     HGLOBAL hTemplate = LoadResource(lppsp->hInstance,
486                                      hResource);
487     pTemplate = (LPDLGTEMPLATEW)LockResource(hTemplate);
488   }
489   else
490   {
491     HRSRC hResource = FindResourceA(lppsp->hInstance,
492                                     (LPSTR)lppsp->u.pszTemplate,
493                                     (LPSTR)RT_DIALOG);
494     HGLOBAL hTemplate = LoadResource(lppsp->hInstance,
495                                      hResource);
496     pTemplate = (LPDLGTEMPLATEA)LockResource(hTemplate);
497   }
498
499   /*
500    * Extract the size of the page and the caption.
501    */
502   if (!pTemplate)
503       return FALSE;
504
505   p = (const WORD *)pTemplate;
506
507   if (((MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF)
508   {
509     /* DLGTEMPLATEEX (not defined in any std. header file) */
510
511     p++;       /* dlgVer    */
512     p++;       /* signature */
513     p += 2;    /* help ID   */
514     p += 2;    /* ext style */
515     p += 2;    /* style     */
516   }
517   else
518   {
519     /* DLGTEMPLATE */
520
521     p += 2;    /* style     */
522     p += 2;    /* ext style */
523   }
524
525   p++;    /* nb items */
526   p++;    /*   x      */
527   p++;    /*   y      */
528   width  = (WORD)*p; p++;
529   height = (WORD)*p; p++;
530
531   /* Special calculation for interior wizard pages so the largest page is
532    * calculated correctly. We need to add all the padding and space occupied
533    * by the header so the width and height sums up to the whole wizard client
534    * area. */
535   if ((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
536       (psInfo->ppshheader.dwFlags & PSH_HEADER) &&
537       !(dwFlags & PSP_HIDEHEADER))
538   {
539       height += 2 * WIZARD_PADDING + WIZARD_HEADER_HEIGHT;
540       width += 2 * WIZARD_PADDING;
541   }
542   if (psInfo->ppshheader.dwFlags & PSH_WIZARD)
543   {
544       height += 2 * WIZARD_PADDING;
545       width += 2 * WIZARD_PADDING;
546   }
547
548   /* remember the largest width and height */
549   if (width > psInfo->width)
550     psInfo->width = width;
551
552   if (height > psInfo->height)
553     psInfo->height = height;
554
555   /* menu */
556   switch ((WORD)*p)
557   {
558     case 0x0000:
559       p++;
560       break;
561     case 0xffff:
562       p += 2;
563       break;
564     default:
565       p += lstrlenW( (LPCWSTR)p ) + 1;
566       break;
567   }
568
569   /* class */
570   switch ((WORD)*p)
571   {
572     case 0x0000:
573       p++;
574       break;
575     case 0xffff:
576       p += 2;
577       break;
578     default:
579       p += lstrlenW( (LPCWSTR)p ) + 1;
580       break;
581   }
582
583   /* Extract the caption */
584   psInfo->proppage[index].pszText = (LPCWSTR)p;
585   TRACE("Tab %d %s\n",index,debugstr_w((LPCWSTR)p));
586   p += lstrlenW((LPCWSTR)p) + 1;
587
588   if (dwFlags & PSP_USETITLE)
589   {
590     WCHAR szTitle[256];
591     const WCHAR *pTitle;
592     static const WCHAR pszNull[] = { '(','n','u','l','l',')',0 };
593     int len;
594
595     if ( !HIWORD( lppsp->pszTitle ) )
596     {
597       if (!LoadStringW( lppsp->hInstance, (DWORD_PTR)lppsp->pszTitle,szTitle,sizeof(szTitle) ))
598       {
599         pTitle = pszNull;
600         FIXME("Could not load resource #%04x?\n",LOWORD(lppsp->pszTitle));
601       }
602       else
603         pTitle = szTitle;
604     }
605     else
606       pTitle = lppsp->pszTitle;
607
608     len = strlenW(pTitle);
609     psInfo->proppage[index].pszText = Alloc( (len+1)*sizeof (WCHAR) );
610     strcpyW( (LPWSTR)psInfo->proppage[index].pszText,pTitle);
611   }
612
613   /*
614    * Build the image list for icons
615    */
616   if ((dwFlags & PSP_USEHICON) || (dwFlags & PSP_USEICONID))
617   {
618     HICON hIcon;
619     int icon_cx = GetSystemMetrics(SM_CXSMICON);
620     int icon_cy = GetSystemMetrics(SM_CYSMICON);
621
622     if (dwFlags & PSP_USEICONID)
623       hIcon = LoadImageW(lppsp->hInstance, lppsp->u2.pszIcon, IMAGE_ICON,
624                          icon_cx, icon_cy, LR_DEFAULTCOLOR);
625     else
626       hIcon = lppsp->u2.hIcon;
627
628     if ( hIcon )
629     {
630       if (psInfo->hImageList == 0 )
631         psInfo->hImageList = ImageList_Create(icon_cx, icon_cy, ILC_COLOR, 1, 1);
632
633       ImageList_AddIcon(psInfo->hImageList, hIcon);
634     }
635
636   }
637
638   return TRUE;
639 }
640
641 /******************************************************************************
642  *            PROPSHEET_CreateDialog
643  *
644  * Creates the actual property sheet.
645  */
646 INT_PTR PROPSHEET_CreateDialog(PropSheetInfo* psInfo)
647 {
648   LRESULT ret;
649   LPCVOID template;
650   LPVOID temp = 0;
651   HRSRC hRes;
652   DWORD resSize;
653   WORD resID = IDD_PROPSHEET;
654
655   TRACE("\n");
656   if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
657     resID = IDD_WIZARD;
658
659   if( psInfo->unicode )
660   {
661     if(!(hRes = FindResourceW(COMCTL32_hModule,
662                             MAKEINTRESOURCEW(resID),
663                             (LPWSTR)RT_DIALOG)))
664       return -1;
665   }
666   else
667   {
668     if(!(hRes = FindResourceA(COMCTL32_hModule,
669                             MAKEINTRESOURCEA(resID),
670                             (LPSTR)RT_DIALOG)))
671       return -1;
672   }
673
674   if(!(template = (LPVOID)LoadResource(COMCTL32_hModule, hRes)))
675     return -1;
676
677   /*
678    * Make a copy of the dialog template.
679    */
680   resSize = SizeofResource(COMCTL32_hModule, hRes);
681
682   temp = Alloc(resSize);
683
684   if (!temp)
685     return -1;
686
687   memcpy(temp, template, resSize);
688
689   if (psInfo->ppshheader.dwFlags & PSH_NOCONTEXTHELP)
690   {
691     if (((MyDLGTEMPLATEEX*)temp)->signature == 0xFFFF)
692       ((MyDLGTEMPLATEEX*)temp)->style &= ~DS_CONTEXTHELP;
693     else
694       ((DLGTEMPLATE*)temp)->style &= ~DS_CONTEXTHELP;
695   }
696   if ((psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD) &&
697       (psInfo->ppshheader.dwFlags & PSH_WIZARDCONTEXTHELP))
698   {
699     if (((MyDLGTEMPLATEEX*)temp)->signature == 0xFFFF)
700       ((MyDLGTEMPLATEEX*)temp)->style |= DS_CONTEXTHELP;
701     else
702       ((DLGTEMPLATE*)temp)->style |= DS_CONTEXTHELP;
703   }
704
705   if (psInfo->useCallback)
706     (*(psInfo->ppshheader.pfnCallback))(0, PSCB_PRECREATE, (LPARAM)temp);
707
708   /* NOTE: MSDN states "Returns a positive value if successful, or -1
709    * otherwise for modal property sheets.", but this is wrong. The
710    * actual return value is either TRUE (success), FALSE (cancel) or
711    * -1 (error). */
712   if( psInfo->unicode )
713   {
714     ret = (INT_PTR)CreateDialogIndirectParamW(psInfo->ppshheader.hInstance,
715                                           (LPDLGTEMPLATEW) temp,
716                                           psInfo->ppshheader.hwndParent,
717                                           PROPSHEET_DialogProc,
718                                           (LPARAM)psInfo);
719     if ( !ret ) ret = -1;
720   }
721   else
722   {
723     ret = (INT_PTR)CreateDialogIndirectParamA(psInfo->ppshheader.hInstance,
724                                           (LPDLGTEMPLATEA) temp,
725                                           psInfo->ppshheader.hwndParent,
726                                           PROPSHEET_DialogProc,
727                                           (LPARAM)psInfo);
728     if ( !ret ) ret = -1;
729   }
730
731   Free(temp);
732
733   return ret;
734 }
735
736 /******************************************************************************
737  *            PROPSHEET_SizeMismatch
738  *
739  *     Verify that the tab control and the "largest" property sheet page dlg. template
740  *     match in size.
741  */
742 static BOOL PROPSHEET_SizeMismatch(HWND hwndDlg, PropSheetInfo* psInfo)
743 {
744   HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
745   RECT rcOrigTab, rcPage;
746
747   /*
748    * Original tab size.
749    */
750   GetClientRect(hwndTabCtrl, &rcOrigTab);
751   TRACE("orig tab %ld %ld %ld %ld\n", rcOrigTab.left, rcOrigTab.top,
752         rcOrigTab.right, rcOrigTab.bottom);
753
754   /*
755    * Biggest page size.
756    */
757   rcPage.left   = 0;
758   rcPage.top    = 0;
759   rcPage.right  = psInfo->width;
760   rcPage.bottom = psInfo->height;
761
762   MapDialogRect(hwndDlg, &rcPage);
763   TRACE("biggest page %ld %ld %ld %ld\n", rcPage.left, rcPage.top,
764         rcPage.right, rcPage.bottom);
765
766   if ( (rcPage.right - rcPage.left) != (rcOrigTab.right - rcOrigTab.left) )
767     return TRUE;
768   if ( (rcPage.bottom - rcPage.top) != (rcOrigTab.bottom - rcOrigTab.top) )
769     return TRUE;
770
771   return FALSE;
772 }
773
774 /******************************************************************************
775  *            PROPSHEET_AdjustSize
776  *
777  * Resizes the property sheet and the tab control to fit the largest page.
778  */
779 static BOOL PROPSHEET_AdjustSize(HWND hwndDlg, PropSheetInfo* psInfo)
780 {
781   HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
782   HWND hwndButton = GetDlgItem(hwndDlg, IDOK);
783   RECT rc,tabRect;
784   int tabOffsetX, tabOffsetY, buttonHeight;
785   PADDING_INFO padding = PROPSHEET_GetPaddingInfo(hwndDlg);
786   RECT units;
787
788   /* Get the height of buttons */
789   GetClientRect(hwndButton, &rc);
790   buttonHeight = rc.bottom;
791
792   /*
793    * Biggest page size.
794    */
795   rc.left   = 0;
796   rc.top    = 0;
797   rc.right  = psInfo->width;
798   rc.bottom = psInfo->height;
799
800   MapDialogRect(hwndDlg, &rc);
801
802   /* retrieve the dialog units */
803   units.left = units.right = 4;
804   units.top = units.bottom = 8;
805   MapDialogRect(hwndDlg, &units);
806
807   /*
808    * Resize the tab control.
809    */
810   GetClientRect(hwndTabCtrl,&tabRect);
811
812   SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, FALSE, (LPARAM)&tabRect);
813
814   if ((rc.bottom - rc.top) < (tabRect.bottom - tabRect.top))
815   {
816       rc.bottom = rc.top + tabRect.bottom - tabRect.top;
817       psInfo->height = MulDiv((rc.bottom - rc.top),8,units.top);
818   }
819
820   if ((rc.right - rc.left) < (tabRect.right - tabRect.left))
821   {
822       rc.right = rc.left + tabRect.right - tabRect.left;
823       psInfo->width  = MulDiv((rc.right - rc.left),4,units.left);
824   }
825
826   SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, TRUE, (LPARAM)&rc);
827
828   tabOffsetX = -(rc.left);
829   tabOffsetY = -(rc.top);
830
831   rc.right -= rc.left;
832   rc.bottom -= rc.top;
833   TRACE("setting tab %p, rc (0,0)-(%ld,%ld)\n",
834         hwndTabCtrl, rc.right, rc.bottom);
835   SetWindowPos(hwndTabCtrl, 0, 0, 0, rc.right, rc.bottom,
836                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
837
838   GetClientRect(hwndTabCtrl, &rc);
839
840   TRACE("tab client rc %ld %ld %ld %ld\n",
841         rc.left, rc.top, rc.right, rc.bottom);
842
843   rc.right += ((padding.x * 2) + tabOffsetX);
844   rc.bottom += (buttonHeight + (3 * padding.y) + tabOffsetY);
845
846   /*
847    * Resize the property sheet.
848    */
849   TRACE("setting dialog %p, rc (0,0)-(%ld,%ld)\n",
850         hwndDlg, rc.right, rc.bottom);
851   SetWindowPos(hwndDlg, 0, 0, 0, rc.right, rc.bottom,
852                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
853   return TRUE;
854 }
855
856 /******************************************************************************
857  *            PROPSHEET_AdjustSizeWizard
858  *
859  * Resizes the property sheet to fit the largest page.
860  */
861 static BOOL PROPSHEET_AdjustSizeWizard(HWND hwndDlg, PropSheetInfo* psInfo)
862 {
863   HWND hwndLine = GetDlgItem(hwndDlg, IDC_SUNKEN_LINE);
864   RECT rc, lineRect, dialogRect;
865
866   /* Biggest page size */
867   rc.left   = 0;
868   rc.top    = 0;
869   rc.right  = psInfo->width;
870   rc.bottom = psInfo->height;
871   MapDialogRect(hwndDlg, &rc);
872
873   TRACE("Biggest page %ld %ld %ld %ld\n", rc.left, rc.top, rc.right, rc.bottom);
874
875   /* Add space for the buttons row */
876   GetWindowRect(hwndLine, &lineRect);
877   MapWindowPoints(NULL, hwndDlg, (LPPOINT)&lineRect, 2);
878   GetClientRect(hwndDlg, &dialogRect);
879   rc.bottom += dialogRect.bottom - lineRect.top - 1;
880
881   /* Convert the client coordinates to window coordinates */
882   AdjustWindowRect(&rc, GetWindowLongW(hwndDlg, GWL_STYLE), FALSE);
883
884   /* Resize the property sheet */
885   TRACE("setting dialog %p, rc (0,0)-(%ld,%ld)\n",
886         hwndDlg, rc.right, rc.bottom);
887   SetWindowPos(hwndDlg, 0, 0, 0, rc.right - rc.left, rc.bottom - rc.top,
888                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
889
890   return TRUE;
891 }
892
893 /******************************************************************************
894  *            PROPSHEET_AdjustButtons
895  *
896  * Adjusts the buttons' positions.
897  */
898 static BOOL PROPSHEET_AdjustButtons(HWND hwndParent, PropSheetInfo* psInfo)
899 {
900   HWND hwndButton = GetDlgItem(hwndParent, IDOK);
901   RECT rcSheet;
902   int x, y;
903   int num_buttons = 2;
904   int buttonWidth, buttonHeight;
905   PADDING_INFO padding = PROPSHEET_GetPaddingInfo(hwndParent);
906
907   if (psInfo->hasApply)
908     num_buttons++;
909
910   if (psInfo->hasHelp)
911     num_buttons++;
912
913   /*
914    * Obtain the size of the buttons.
915    */
916   GetClientRect(hwndButton, &rcSheet);
917   buttonWidth = rcSheet.right;
918   buttonHeight = rcSheet.bottom;
919
920   /*
921    * Get the size of the property sheet.
922    */
923   GetClientRect(hwndParent, &rcSheet);
924
925   /*
926    * All buttons will be at this y coordinate.
927    */
928   y = rcSheet.bottom - (padding.y + buttonHeight);
929
930   /*
931    * Position OK button and make it default.
932    */
933   hwndButton = GetDlgItem(hwndParent, IDOK);
934
935   x = rcSheet.right - ((padding.x + buttonWidth) * num_buttons);
936
937   SetWindowPos(hwndButton, 0, x, y, 0, 0,
938                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
939
940   SendMessageW(hwndParent, DM_SETDEFID, IDOK, 0);
941
942
943   /*
944    * Position Cancel button.
945    */
946   hwndButton = GetDlgItem(hwndParent, IDCANCEL);
947
948   x = rcSheet.right - ((padding.x + buttonWidth) * (num_buttons - 1));
949
950   SetWindowPos(hwndButton, 0, x, y, 0, 0,
951                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
952
953   /*
954    * Position Apply button.
955    */
956   hwndButton = GetDlgItem(hwndParent, IDC_APPLY_BUTTON);
957
958   if (psInfo->hasApply)
959   {
960     if (psInfo->hasHelp)
961       x = rcSheet.right - ((padding.x + buttonWidth) * 2);
962     else
963       x = rcSheet.right - (padding.x + buttonWidth);
964
965     SetWindowPos(hwndButton, 0, x, y, 0, 0,
966                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
967
968     EnableWindow(hwndButton, FALSE);
969   }
970   else
971     ShowWindow(hwndButton, SW_HIDE);
972
973   /*
974    * Position Help button.
975    */
976   hwndButton = GetDlgItem(hwndParent, IDHELP);
977
978   if (psInfo->hasHelp)
979   {
980     x = rcSheet.right - (padding.x + buttonWidth);
981
982     SetWindowPos(hwndButton, 0, x, y, 0, 0,
983                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
984   }
985   else
986     ShowWindow(hwndButton, SW_HIDE);
987
988   return TRUE;
989 }
990
991 /******************************************************************************
992  *            PROPSHEET_AdjustButtonsWizard
993  *
994  * Adjusts the buttons' positions.
995  */
996 static BOOL PROPSHEET_AdjustButtonsWizard(HWND hwndParent,
997                                           PropSheetInfo* psInfo)
998 {
999   HWND hwndButton = GetDlgItem(hwndParent, IDCANCEL);
1000   HWND hwndLine = GetDlgItem(hwndParent, IDC_SUNKEN_LINE);
1001   HWND hwndLineHeader = GetDlgItem(hwndParent, IDC_SUNKEN_LINEHEADER);
1002   RECT rcSheet;
1003   int x, y;
1004   int num_buttons = 3;
1005   int buttonWidth, buttonHeight, lineHeight, lineWidth;
1006   PADDING_INFO padding = PROPSHEET_GetPaddingInfoWizard(hwndParent, psInfo);
1007
1008   if (psInfo->hasHelp)
1009     num_buttons++;
1010   if (psInfo->hasFinish)
1011     num_buttons++;
1012
1013   /*
1014    * Obtain the size of the buttons.
1015    */
1016   GetClientRect(hwndButton, &rcSheet);
1017   buttonWidth = rcSheet.right;
1018   buttonHeight = rcSheet.bottom;
1019
1020   GetClientRect(hwndLine, &rcSheet);
1021   lineHeight = rcSheet.bottom;
1022
1023   /*
1024    * Get the size of the property sheet.
1025    */
1026   GetClientRect(hwndParent, &rcSheet);
1027
1028   /*
1029    * All buttons will be at this y coordinate.
1030    */
1031   y = rcSheet.bottom - (padding.y + buttonHeight);
1032   
1033   /*
1034    * Position the Back button.
1035    */
1036   hwndButton = GetDlgItem(hwndParent, IDC_BACK_BUTTON);
1037
1038   x = rcSheet.right - ((padding.x + buttonWidth) * (num_buttons - 1)) - buttonWidth;
1039
1040   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1041                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1042
1043   /*
1044    * Position the Next button.
1045    */
1046   hwndButton = GetDlgItem(hwndParent, IDC_NEXT_BUTTON);
1047   
1048   x += buttonWidth;
1049   
1050   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1051                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1052
1053   /*
1054    * Position the Finish button.
1055    */
1056   hwndButton = GetDlgItem(hwndParent, IDC_FINISH_BUTTON);
1057   
1058   if (psInfo->hasFinish)
1059     x += padding.x + buttonWidth;
1060
1061   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1062                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1063
1064   if (!psInfo->hasFinish)
1065     ShowWindow(hwndButton, SW_HIDE);
1066
1067   /*
1068    * Position the Cancel button.
1069    */
1070   hwndButton = GetDlgItem(hwndParent, IDCANCEL);
1071
1072   x += padding.x + buttonWidth;
1073
1074   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1075                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1076
1077   /*
1078    * Position Help button.
1079    */
1080   hwndButton = GetDlgItem(hwndParent, IDHELP);
1081
1082   if (psInfo->hasHelp)
1083   {
1084     x += padding.x + buttonWidth;
1085
1086     SetWindowPos(hwndButton, 0, x, y, 0, 0,
1087                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1088   }
1089   else
1090     ShowWindow(hwndButton, SW_HIDE);
1091
1092   if (psInfo->ppshheader.dwFlags &
1093       (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW | PSH_WIZARD_LITE)) 
1094       padding.x = 0;
1095
1096   /*
1097    * Position and resize the sunken line.
1098    */
1099   x = padding.x;
1100   y = rcSheet.bottom - ((padding.y * 2) + buttonHeight + lineHeight);
1101
1102   lineWidth = rcSheet.right - (padding.x * 2);
1103   SetWindowPos(hwndLine, 0, x, y, lineWidth, 2,
1104                SWP_NOZORDER | SWP_NOACTIVATE);
1105
1106   /*
1107    * Position and resize the header sunken line.
1108    */
1109   
1110   SetWindowPos(hwndLineHeader, 0, 0, 0, rcSheet.right, 2,
1111                SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
1112   if (!(psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)))
1113       ShowWindow(hwndLineHeader, SW_HIDE);
1114
1115   return TRUE;
1116 }
1117
1118 /******************************************************************************
1119  *            PROPSHEET_GetPaddingInfo
1120  *
1121  * Returns the layout information.
1122  */
1123 static PADDING_INFO PROPSHEET_GetPaddingInfo(HWND hwndDlg)
1124 {
1125   HWND hwndTab = GetDlgItem(hwndDlg, IDC_TABCONTROL);
1126   RECT rcTab;
1127   POINT tl;
1128   PADDING_INFO padding;
1129
1130   GetWindowRect(hwndTab, &rcTab);
1131
1132   tl.x = rcTab.left;
1133   tl.y = rcTab.top;
1134
1135   ScreenToClient(hwndDlg, &tl);
1136
1137   padding.x = tl.x;
1138   padding.y = tl.y;
1139
1140   return padding;
1141 }
1142
1143 /******************************************************************************
1144  *            PROPSHEET_GetPaddingInfoWizard
1145  *
1146  * Returns the layout information.
1147  * Vertical spacing is the distance between the line and the buttons.
1148  * Do NOT use the Help button to gather padding information when it isn't mapped
1149  * (PSH_HASHELP), as app writers aren't forced to supply correct coordinates
1150  * for it in this case !
1151  * FIXME: I'm not sure about any other coordinate problems with these evil
1152  * buttons. Fix it in case additional problems appear or maybe calculate
1153  * a padding in a completely different way, as this is somewhat messy.
1154  */
1155 static PADDING_INFO PROPSHEET_GetPaddingInfoWizard(HWND hwndDlg, const PropSheetInfo*
1156  psInfo)
1157 {
1158   PADDING_INFO padding;
1159   RECT rc;
1160   HWND hwndControl;
1161   INT idButton;
1162   POINT ptButton, ptLine;
1163
1164   TRACE("\n");
1165   if (psInfo->hasHelp)
1166   {
1167         idButton = IDHELP;
1168   }
1169   else
1170   {
1171     if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
1172     {
1173         idButton = IDC_NEXT_BUTTON;
1174     }
1175     else
1176     {
1177         /* hopefully this is ok */
1178         idButton = IDCANCEL;
1179     }
1180   }
1181
1182   hwndControl = GetDlgItem(hwndDlg, idButton);
1183   GetWindowRect(hwndControl, &rc);
1184
1185   ptButton.x = rc.left;
1186   ptButton.y = rc.top;
1187
1188   ScreenToClient(hwndDlg, &ptButton);
1189
1190   /* Line */
1191   hwndControl = GetDlgItem(hwndDlg, IDC_SUNKEN_LINE);
1192   GetWindowRect(hwndControl, &rc);
1193
1194   ptLine.x = rc.left;
1195   ptLine.y = rc.bottom;
1196
1197   ScreenToClient(hwndDlg, &ptLine);
1198
1199   padding.y = ptButton.y - ptLine.y;
1200
1201   if (padding.y < 0)
1202           ERR("padding negative ! Please report this !\n");
1203
1204   /* this is most probably not correct, but the best we have now */
1205   padding.x = padding.y;
1206   return padding;
1207 }
1208
1209 /******************************************************************************
1210  *            PROPSHEET_CreateTabControl
1211  *
1212  * Insert the tabs in the tab control.
1213  */
1214 static BOOL PROPSHEET_CreateTabControl(HWND hwndParent,
1215                                        PropSheetInfo * psInfo)
1216 {
1217   HWND hwndTabCtrl = GetDlgItem(hwndParent, IDC_TABCONTROL);
1218   TCITEMW item;
1219   int i, nTabs;
1220   int iImage = 0;
1221
1222   TRACE("\n");
1223   item.mask = TCIF_TEXT;
1224   item.cchTextMax = MAX_TABTEXT_LENGTH;
1225
1226   nTabs = psInfo->nPages;
1227
1228   /*
1229    * Set the image list for icons.
1230    */
1231   if (psInfo->hImageList)
1232   {
1233     SendMessageW(hwndTabCtrl, TCM_SETIMAGELIST, 0, (LPARAM)psInfo->hImageList);
1234   }
1235
1236   SendMessageW(GetDlgItem(hwndTabCtrl, IDC_TABCONTROL), WM_SETREDRAW, 0, 0);
1237   for (i = 0; i < nTabs; i++)
1238   {
1239     if ( psInfo->proppage[i].hasIcon )
1240     {
1241       item.mask |= TCIF_IMAGE;
1242       item.iImage = iImage++;
1243     }
1244     else
1245     {
1246       item.mask &= ~TCIF_IMAGE;
1247     }
1248
1249     item.pszText = (LPWSTR) psInfo->proppage[i].pszText;
1250     SendMessageW(hwndTabCtrl, TCM_INSERTITEMW, (WPARAM)i, (LPARAM)&item);
1251   }
1252   SendMessageW(GetDlgItem(hwndTabCtrl, IDC_TABCONTROL), WM_SETREDRAW, 1, 0);
1253
1254   return TRUE;
1255 }
1256
1257 /******************************************************************************
1258  *            PROPSHEET_WizardSubclassProc
1259  *
1260  * Subclassing window procedure for wizard extrior pages to prevent drawing
1261  * background and so drawing above the watermark.
1262  */
1263 static LRESULT CALLBACK
1264 PROPSHEET_WizardSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uID, DWORD_PTR dwRef)
1265 {
1266   switch (uMsg)
1267   {
1268     case WM_ERASEBKGND:
1269       return TRUE;
1270
1271     case WM_CTLCOLORSTATIC:
1272       SetBkColor((HDC)wParam, GetSysColor(COLOR_WINDOW));
1273       return (INT_PTR)GetSysColorBrush(COLOR_WINDOW);
1274   }
1275
1276   return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1277 }
1278
1279 /*
1280  * Get the size of an in-memory Template
1281  *
1282  *( Based on the code of PROPSHEET_CollectPageInfo)
1283  * See also dialog.c/DIALOG_ParseTemplate32().
1284  */
1285
1286 static UINT GetTemplateSize(DLGTEMPLATE* pTemplate)
1287
1288 {
1289   const WORD*  p = (const WORD *)pTemplate;
1290   BOOL  istemplateex = (((MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF);
1291   WORD nrofitems;
1292
1293   if (istemplateex)
1294   {
1295     /* DLGTEMPLATEEX (not defined in any std. header file) */
1296
1297     TRACE("is DLGTEMPLATEEX\n");
1298     p++;       /* dlgVer    */
1299     p++;       /* signature */
1300     p += 2;    /* help ID   */
1301     p += 2;    /* ext style */
1302     p += 2;    /* style     */
1303   }
1304   else
1305   {
1306     /* DLGTEMPLATE */
1307
1308     TRACE("is DLGTEMPLATE\n");
1309     p += 2;    /* style     */
1310     p += 2;    /* ext style */
1311   }
1312
1313   nrofitems =   (WORD)*p; p++;    /* nb items */
1314   p++;    /*   x      */
1315   p++;    /*   y      */
1316   p++;    /*   width  */
1317   p++;    /*   height */
1318
1319   /* menu */
1320   switch ((WORD)*p)
1321   {
1322     case 0x0000:
1323       p++;
1324       break;
1325     case 0xffff:
1326       p += 2;
1327       break;
1328     default:
1329       TRACE("menu %s\n",debugstr_w((LPCWSTR)p));
1330       p += lstrlenW( (LPCWSTR)p ) + 1;
1331       break;
1332   }
1333
1334   /* class */
1335   switch ((WORD)*p)
1336   {
1337     case 0x0000:
1338       p++;
1339       break;
1340     case 0xffff:
1341       p += 2; /* 0xffff plus predefined window class ordinal value */
1342       break;
1343     default:
1344       TRACE("class %s\n",debugstr_w((LPCWSTR)p));
1345       p += lstrlenW( (LPCWSTR)p ) + 1;
1346       break;
1347   }
1348
1349   /* title */
1350   TRACE("title %s\n",debugstr_w((LPCWSTR)p));
1351   p += lstrlenW((LPCWSTR)p) + 1;
1352
1353   /* font, if DS_SETFONT set */
1354   if ((DS_SETFONT & ((istemplateex)?  ((MyDLGTEMPLATEEX*)pTemplate)->style :
1355                      pTemplate->style)))
1356     {
1357       p+=(istemplateex)?3:1;
1358       TRACE("font %s\n",debugstr_w((LPCWSTR)p));
1359       p += lstrlenW( (LPCWSTR)p ) + 1; /* the font name */
1360     }
1361
1362   /* now process the DLGITEMTEMPLATE(EX) structs (plus custom data)
1363    * that are following the DLGTEMPLATE(EX) data */
1364   TRACE("%d items\n",nrofitems);
1365   while (nrofitems > 0)
1366     {
1367       p = (WORD*)(((DWORD_PTR)p + 3) & ~3); /* DWORD align */
1368       
1369       /* skip header */
1370       p += (istemplateex ? sizeof(MyDLGITEMTEMPLATEEX) : sizeof(DLGITEMTEMPLATE))/sizeof(WORD);
1371       
1372       /* check class */
1373       switch ((WORD)*p)
1374         {
1375         case 0x0000:
1376           p++;
1377           break;
1378         case 0xffff:
1379           TRACE("class ordinal 0x%08lx\n",*(DWORD*)p);
1380           p += 2;
1381           break;
1382         default:
1383           TRACE("class %s\n",debugstr_w((LPCWSTR)p));
1384           p += lstrlenW( (LPCWSTR)p ) + 1;
1385           break;
1386         }
1387
1388       /* check title text */
1389       switch ((WORD)*p)
1390         {
1391         case 0x0000:
1392           p++;
1393           break;
1394         case 0xffff:
1395           TRACE("text ordinal 0x%08lx\n",*(DWORD*)p);
1396           p += 2;
1397           break;
1398         default:
1399           TRACE("text %s\n",debugstr_w((LPCWSTR)p));
1400           p += lstrlenW( (LPCWSTR)p ) + 1;
1401           break;
1402         }
1403       p += *p / sizeof(WORD) + 1;    /* Skip extra data */
1404       --nrofitems;
1405     }
1406   
1407   TRACE("%p %p size 0x%08x\n",p, (WORD*)pTemplate,sizeof(WORD)*(p - (WORD*)pTemplate));
1408   return (p - (WORD*)pTemplate)*sizeof(WORD);
1409   
1410 }
1411
1412 /******************************************************************************
1413  *            PROPSHEET_CreatePage
1414  *
1415  * Creates a page.
1416  */
1417 static BOOL PROPSHEET_CreatePage(HWND hwndParent,
1418                                 int index,
1419                                 const PropSheetInfo * psInfo,
1420                                 LPCPROPSHEETPAGEW ppshpage)
1421 {
1422   DLGTEMPLATE* pTemplate;
1423   HWND hwndPage;
1424   DWORD resSize;
1425   LPVOID temp = NULL;
1426
1427   TRACE("index %d\n", index);
1428
1429   if (ppshpage == NULL)
1430   {
1431     return FALSE;
1432   }
1433
1434   if (ppshpage->dwFlags & PSP_DLGINDIRECT)
1435     {
1436       pTemplate = (DLGTEMPLATE*)ppshpage->u.pResource;
1437       resSize = GetTemplateSize(pTemplate);
1438     }
1439   else if(ppshpage->dwFlags & PSP_INTERNAL_UNICODE)
1440   {
1441     HRSRC hResource;
1442     HANDLE hTemplate;
1443
1444     hResource = FindResourceW(ppshpage->hInstance,
1445                                     ppshpage->u.pszTemplate,
1446                                     (LPWSTR)RT_DIALOG);
1447     if(!hResource)
1448         return FALSE;
1449
1450     resSize = SizeofResource(ppshpage->hInstance, hResource);
1451
1452     hTemplate = LoadResource(ppshpage->hInstance, hResource);
1453     if(!hTemplate)
1454         return FALSE;
1455
1456     pTemplate = (LPDLGTEMPLATEW)LockResource(hTemplate);
1457     /*
1458      * Make a copy of the dialog template to make it writable
1459      */
1460   }
1461   else
1462   {
1463     HRSRC hResource;
1464     HANDLE hTemplate;
1465
1466     hResource = FindResourceA(ppshpage->hInstance,
1467                                     (LPSTR)ppshpage->u.pszTemplate,
1468                                     (LPSTR)RT_DIALOG);
1469     if(!hResource)
1470         return FALSE;
1471
1472     resSize = SizeofResource(ppshpage->hInstance, hResource);
1473
1474     hTemplate = LoadResource(ppshpage->hInstance, hResource);
1475     if(!hTemplate)
1476         return FALSE;
1477
1478     pTemplate = (LPDLGTEMPLATEA)LockResource(hTemplate);
1479     /*
1480      * Make a copy of the dialog template to make it writable
1481      */
1482   }
1483   temp = Alloc(resSize);
1484   if (!temp)
1485     return FALSE;
1486   
1487   TRACE("copying pTemplate %p into temp %p (%ld)\n", pTemplate, temp, resSize);
1488   memcpy(temp, pTemplate, resSize);
1489   pTemplate = temp;
1490
1491   if (((MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF)
1492   {
1493     ((MyDLGTEMPLATEEX*)pTemplate)->style |= WS_CHILD | WS_TABSTOP | DS_CONTROL;
1494     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~DS_MODALFRAME;
1495     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_CAPTION;
1496     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_SYSMENU;
1497     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_POPUP;
1498     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_DISABLED;
1499     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_VISIBLE;
1500     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_THICKFRAME;
1501
1502     ((MyDLGTEMPLATEEX*)pTemplate)->exStyle |= WS_EX_CONTROLPARENT;
1503   }
1504   else
1505   {
1506     pTemplate->style |= WS_CHILD | WS_TABSTOP | DS_CONTROL;
1507     pTemplate->style &= ~DS_MODALFRAME;
1508     pTemplate->style &= ~WS_CAPTION;
1509     pTemplate->style &= ~WS_SYSMENU;
1510     pTemplate->style &= ~WS_POPUP;
1511     pTemplate->style &= ~WS_DISABLED;
1512     pTemplate->style &= ~WS_VISIBLE;
1513     pTemplate->style &= ~WS_THICKFRAME;
1514
1515     pTemplate->dwExtendedStyle |= WS_EX_CONTROLPARENT;
1516   }
1517
1518   if (psInfo->proppage[index].useCallback)
1519     (*(ppshpage->pfnCallback))(0, PSPCB_CREATE,
1520                                (LPPROPSHEETPAGEW)ppshpage);
1521
1522   if(ppshpage->dwFlags & PSP_INTERNAL_UNICODE)
1523      hwndPage = CreateDialogIndirectParamW(ppshpage->hInstance,
1524                                         pTemplate,
1525                                         hwndParent,
1526                                         ppshpage->pfnDlgProc,
1527                                         (LPARAM)ppshpage);
1528   else
1529      hwndPage = CreateDialogIndirectParamA(ppshpage->hInstance,
1530                                         pTemplate,
1531                                         hwndParent,
1532                                         ppshpage->pfnDlgProc,
1533                                         (LPARAM)ppshpage);
1534   /* Free a no more needed copy */
1535   if(temp)
1536       Free(temp);
1537
1538   psInfo->proppage[index].hwndPage = hwndPage;
1539
1540   /* Subclass exterior wizard pages */
1541   if((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
1542      (psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
1543      (ppshpage->dwFlags & PSP_HIDEHEADER))
1544   {
1545       SetWindowSubclass(hwndPage, PROPSHEET_WizardSubclassProc, 1,
1546                         (DWORD_PTR)ppshpage);
1547   }
1548   if (!(psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD))
1549       EnableThemeDialogTexture (hwndPage, ETDT_ENABLETAB);
1550
1551   return TRUE;
1552 }
1553
1554 /******************************************************************************
1555  *            PROPSHEET_LoadWizardBitmaps
1556  *
1557  * Loads the watermark and header bitmaps for a wizard.
1558  */
1559 static VOID PROPSHEET_LoadWizardBitmaps(PropSheetInfo *psInfo)
1560 {
1561   if (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD))
1562   {
1563     /* if PSH_USEHBMWATERMARK is not set, load the resource from pszbmWatermark 
1564        and put the HBITMAP in hbmWatermark. Thus all the rest of the code always 
1565        considers hbmWatermark as valid. */
1566     if ((psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
1567         !(psInfo->ppshheader.dwFlags & PSH_USEHBMWATERMARK))
1568     {
1569       ((PropSheetInfo *)psInfo)->ppshheader.u4.hbmWatermark = 
1570         CreateMappedBitmap(psInfo->ppshheader.hInstance, (INT_PTR)psInfo->ppshheader.u4.pszbmWatermark, 0, NULL, 0);
1571     }
1572
1573     /* Same behavior as for watermarks */
1574     if ((psInfo->ppshheader.dwFlags & PSH_HEADER) &&
1575         !(psInfo->ppshheader.dwFlags & PSH_USEHBMHEADER))
1576     {
1577       ((PropSheetInfo *)psInfo)->ppshheader.u5.hbmHeader = 
1578         CreateMappedBitmap(psInfo->ppshheader.hInstance, (INT_PTR)psInfo->ppshheader.u5.pszbmHeader, 0, NULL, 0);
1579     }
1580   }
1581 }
1582
1583
1584 /******************************************************************************
1585  *            PROPSHEET_ShowPage
1586  *
1587  * Displays or creates the specified page.
1588  */
1589 static BOOL PROPSHEET_ShowPage(HWND hwndDlg, int index, PropSheetInfo * psInfo)
1590 {
1591   HWND hwndTabCtrl;
1592   HWND hwndLineHeader;
1593   LPCPROPSHEETPAGEW ppshpage;
1594
1595   TRACE("active_page %d, index %d\n", psInfo->active_page, index);
1596   if (index == psInfo->active_page)
1597   {
1598       if (GetTopWindow(hwndDlg) != psInfo->proppage[index].hwndPage)
1599           SetWindowPos(psInfo->proppage[index].hwndPage, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1600       return TRUE;
1601   }
1602
1603   ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
1604   if (psInfo->proppage[index].hwndPage == 0)
1605   {
1606      PROPSHEET_CreatePage(hwndDlg, index, psInfo, ppshpage);
1607   }
1608
1609   if ((psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD) &&
1610       (ppshpage->dwFlags & PSP_USETITLE))
1611   {
1612      PROPSHEET_SetTitleW(hwndDlg, psInfo->ppshheader.dwFlags,
1613                          psInfo->proppage[index].pszText);
1614   }
1615
1616   if (psInfo->active_page != -1)
1617      ShowWindow(psInfo->proppage[psInfo->active_page].hwndPage, SW_HIDE);
1618
1619   ShowWindow(psInfo->proppage[index].hwndPage, SW_SHOW);
1620
1621   /* Synchronize current selection with tab control
1622    * It seems to be needed even in case of PSH_WIZARD (no tab controls there) */
1623   hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
1624   SendMessageW(hwndTabCtrl, TCM_SETCURSEL, index, 0);
1625
1626   psInfo->active_page = index;
1627   psInfo->activeValid = TRUE;
1628
1629   if (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW) )
1630   {
1631       hwndLineHeader = GetDlgItem(hwndDlg, IDC_SUNKEN_LINEHEADER);
1632       ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
1633       
1634       if ((ppshpage->dwFlags & PSP_HIDEHEADER) || (!(psInfo->ppshheader.dwFlags & PSH_HEADER)) )
1635           ShowWindow(hwndLineHeader, SW_HIDE);
1636       else
1637           ShowWindow(hwndLineHeader, SW_SHOW);
1638   }
1639
1640   return TRUE;
1641 }
1642
1643 /******************************************************************************
1644  *            PROPSHEET_Back
1645  */
1646 static BOOL PROPSHEET_Back(HWND hwndDlg)
1647 {
1648   PSHNOTIFY psn;
1649   HWND hwndPage;
1650   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1651                                                     PropSheetInfoStr);
1652   LRESULT result;
1653   int idx;
1654
1655   TRACE("active_page %d\n", psInfo->active_page);
1656   if (psInfo->active_page < 0)
1657      return FALSE;
1658
1659   psn.hdr.code     = PSN_WIZBACK;
1660   psn.hdr.hwndFrom = hwndDlg;
1661   psn.hdr.idFrom   = 0;
1662   psn.lParam       = 0;
1663
1664   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1665
1666   result = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1667   if (result == -1)
1668     return FALSE;
1669   else if (result == 0)
1670      idx = psInfo->active_page - 1;
1671   else
1672      idx = PROPSHEET_FindPageByResId(psInfo, result);
1673
1674   if (idx >= 0 && idx < psInfo->nPages)
1675   {
1676      if (PROPSHEET_CanSetCurSel(hwndDlg))
1677         PROPSHEET_SetCurSel(hwndDlg, idx, -1, 0);
1678   }
1679   return TRUE;
1680 }
1681
1682 /******************************************************************************
1683  *            PROPSHEET_Next
1684  */
1685 static BOOL PROPSHEET_Next(HWND hwndDlg)
1686 {
1687   PSHNOTIFY psn;
1688   HWND hwndPage;
1689   LRESULT msgResult = 0;
1690   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1691                                                     PropSheetInfoStr);
1692   int idx;
1693
1694   TRACE("active_page %d\n", psInfo->active_page);
1695   if (psInfo->active_page < 0)
1696      return FALSE;
1697
1698   psn.hdr.code     = PSN_WIZNEXT;
1699   psn.hdr.hwndFrom = hwndDlg;
1700   psn.hdr.idFrom   = 0;
1701   psn.lParam       = 0;
1702
1703   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1704
1705   msgResult = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1706   if (msgResult == -1)
1707     return FALSE;
1708   else if (msgResult == 0)
1709      idx = psInfo->active_page + 1;
1710   else
1711      idx = PROPSHEET_FindPageByResId(psInfo, msgResult);
1712
1713   if (idx < psInfo->nPages )
1714   {
1715      if (PROPSHEET_CanSetCurSel(hwndDlg) != FALSE)
1716         PROPSHEET_SetCurSel(hwndDlg, idx, 1, 0);
1717   }
1718
1719   return TRUE;
1720 }
1721
1722 /******************************************************************************
1723  *            PROPSHEET_Finish
1724  */
1725 static BOOL PROPSHEET_Finish(HWND hwndDlg)
1726 {
1727   PSHNOTIFY psn;
1728   HWND hwndPage;
1729   LRESULT msgResult = 0;
1730   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1731                                                     PropSheetInfoStr);
1732
1733   TRACE("active_page %d\n", psInfo->active_page);
1734   if (psInfo->active_page < 0)
1735      return FALSE;
1736
1737   psn.hdr.code     = PSN_WIZFINISH;
1738   psn.hdr.hwndFrom = hwndDlg;
1739   psn.hdr.idFrom   = 0;
1740   psn.lParam       = 0;
1741
1742   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1743
1744   msgResult = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1745
1746   TRACE("msg result %ld\n", msgResult);
1747
1748   if (msgResult != 0)
1749     return FALSE;
1750
1751   if (psInfo->isModeless)
1752     psInfo->activeValid = FALSE;
1753   else
1754     psInfo->ended = TRUE;
1755
1756   return TRUE;
1757 }
1758
1759 /******************************************************************************
1760  *            PROPSHEET_Apply
1761  */
1762 static BOOL PROPSHEET_Apply(HWND hwndDlg, LPARAM lParam)
1763 {
1764   int i;
1765   HWND hwndPage;
1766   PSHNOTIFY psn;
1767   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1768                                                     PropSheetInfoStr);
1769
1770   TRACE("active_page %d\n", psInfo->active_page);
1771   if (psInfo->active_page < 0)
1772      return FALSE;
1773
1774   psn.hdr.hwndFrom = hwndDlg;
1775   psn.hdr.idFrom   = 0;
1776   psn.lParam       = 0;
1777
1778
1779   /*
1780    * Send PSN_KILLACTIVE to the current page.
1781    */
1782   psn.hdr.code = PSN_KILLACTIVE;
1783
1784   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1785
1786   if (SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn) != FALSE)
1787     return FALSE;
1788
1789   /*
1790    * Send PSN_APPLY to all pages.
1791    */
1792   psn.hdr.code = PSN_APPLY;
1793   psn.lParam   = lParam;
1794
1795   for (i = 0; i < psInfo->nPages; i++)
1796   {
1797     hwndPage = psInfo->proppage[i].hwndPage;
1798     if (hwndPage)
1799     {
1800        switch (SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn))
1801        {
1802        case PSNRET_INVALID:
1803            PROPSHEET_ShowPage(hwndDlg, i, psInfo);
1804            /* fall through */
1805        case PSNRET_INVALID_NOCHANGEPAGE:
1806            return FALSE;
1807        }
1808     }
1809   }
1810
1811   if(lParam)
1812   {
1813      psInfo->activeValid = FALSE;
1814   }
1815   else if(psInfo->active_page >= 0)
1816   {
1817      psn.hdr.code = PSN_SETACTIVE;
1818      psn.lParam   = 0;
1819      hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1820      SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1821   }
1822
1823   return TRUE;
1824 }
1825
1826 /******************************************************************************
1827  *            PROPSHEET_Cancel
1828  */
1829 static void PROPSHEET_Cancel(HWND hwndDlg, LPARAM lParam)
1830 {
1831   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1832                                                     PropSheetInfoStr);
1833   HWND hwndPage;
1834   PSHNOTIFY psn;
1835   int i;
1836
1837   TRACE("active_page %d\n", psInfo->active_page);
1838   if (psInfo->active_page < 0)
1839      return;
1840
1841   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1842   psn.hdr.code     = PSN_QUERYCANCEL;
1843   psn.hdr.hwndFrom = hwndDlg;
1844   psn.hdr.idFrom   = 0;
1845   psn.lParam       = 0;
1846
1847   if (SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn))
1848     return;
1849
1850   psn.hdr.code = PSN_RESET;
1851   psn.lParam   = lParam;
1852
1853   for (i = 0; i < psInfo->nPages; i++)
1854   {
1855     hwndPage = psInfo->proppage[i].hwndPage;
1856
1857     if (hwndPage)
1858        SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1859   }
1860
1861   if (psInfo->isModeless)
1862   {
1863      /* makes PSM_GETCURRENTPAGEHWND return NULL */
1864      psInfo->activeValid = FALSE;
1865   }
1866   else
1867     psInfo->ended = TRUE;
1868 }
1869
1870 /******************************************************************************
1871  *            PROPSHEET_Help
1872  */
1873 static void PROPSHEET_Help(HWND hwndDlg)
1874 {
1875   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1876                                                     PropSheetInfoStr);
1877   HWND hwndPage;
1878   PSHNOTIFY psn;
1879
1880   TRACE("active_page %d\n", psInfo->active_page);
1881   if (psInfo->active_page < 0)
1882      return;
1883
1884   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1885   psn.hdr.code     = PSN_HELP;
1886   psn.hdr.hwndFrom = hwndDlg;
1887   psn.hdr.idFrom   = 0;
1888   psn.lParam       = 0;
1889
1890   SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1891 }
1892
1893 /******************************************************************************
1894  *            PROPSHEET_Changed
1895  */
1896 static void PROPSHEET_Changed(HWND hwndDlg, HWND hwndDirtyPage)
1897 {
1898   int i;
1899   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1900                                                     PropSheetInfoStr);
1901
1902   TRACE("\n");
1903   if (!psInfo) return;
1904   /*
1905    * Set the dirty flag of this page.
1906    */
1907   for (i = 0; i < psInfo->nPages; i++)
1908   {
1909     if (psInfo->proppage[i].hwndPage == hwndDirtyPage)
1910       psInfo->proppage[i].isDirty = TRUE;
1911   }
1912
1913   /*
1914    * Enable the Apply button.
1915    */
1916   if (psInfo->hasApply)
1917   {
1918     HWND hwndApplyBtn = GetDlgItem(hwndDlg, IDC_APPLY_BUTTON);
1919
1920     EnableWindow(hwndApplyBtn, TRUE);
1921   }
1922 }
1923
1924 /******************************************************************************
1925  *            PROPSHEET_UnChanged
1926  */
1927 static void PROPSHEET_UnChanged(HWND hwndDlg, HWND hwndCleanPage)
1928 {
1929   int i;
1930   BOOL noPageDirty = TRUE;
1931   HWND hwndApplyBtn = GetDlgItem(hwndDlg, IDC_APPLY_BUTTON);
1932   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1933                                                     PropSheetInfoStr);
1934
1935   TRACE("\n");
1936   if ( !psInfo ) return;
1937   for (i = 0; i < psInfo->nPages; i++)
1938   {
1939     /* set the specified page as clean */
1940     if (psInfo->proppage[i].hwndPage == hwndCleanPage)
1941       psInfo->proppage[i].isDirty = FALSE;
1942
1943     /* look to see if there's any dirty pages */
1944     if (psInfo->proppage[i].isDirty)
1945       noPageDirty = FALSE;
1946   }
1947
1948   /*
1949    * Disable Apply button.
1950    */
1951   if (noPageDirty)
1952     EnableWindow(hwndApplyBtn, FALSE);
1953 }
1954
1955 /******************************************************************************
1956  *            PROPSHEET_PressButton
1957  */
1958 static void PROPSHEET_PressButton(HWND hwndDlg, int buttonID)
1959 {
1960   TRACE("buttonID %d\n", buttonID);
1961   switch (buttonID)
1962   {
1963     case PSBTN_APPLYNOW:
1964       PROPSHEET_DoCommand(hwndDlg, IDC_APPLY_BUTTON);
1965       break;
1966     case PSBTN_BACK:
1967       PROPSHEET_Back(hwndDlg);
1968       break;
1969     case PSBTN_CANCEL:
1970       PROPSHEET_DoCommand(hwndDlg, IDCANCEL);
1971       break;
1972     case PSBTN_FINISH:
1973       PROPSHEET_Finish(hwndDlg);
1974       break;
1975     case PSBTN_HELP:
1976       PROPSHEET_DoCommand(hwndDlg, IDHELP);
1977       break;
1978     case PSBTN_NEXT:
1979       PROPSHEET_Next(hwndDlg);
1980       break;
1981     case PSBTN_OK:
1982       PROPSHEET_DoCommand(hwndDlg, IDOK);
1983       break;
1984     default:
1985       FIXME("Invalid button index %d\n", buttonID);
1986   }
1987 }
1988
1989
1990 /*************************************************************************
1991  * BOOL PROPSHEET_CanSetCurSel [Internal]
1992  *
1993  * Test whether the current page can be changed by sending a PSN_KILLACTIVE
1994  *
1995  * PARAMS
1996  *     hwndDlg        [I] handle to a Dialog hWnd
1997  *
1998  * RETURNS
1999  *     TRUE if Current Selection can change
2000  *
2001  * NOTES
2002  */
2003 static BOOL PROPSHEET_CanSetCurSel(HWND hwndDlg)
2004 {
2005   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2006                                                     PropSheetInfoStr);
2007   HWND hwndPage;
2008   PSHNOTIFY psn;
2009   BOOL res = FALSE;
2010
2011   TRACE("active_page %d\n", psInfo->active_page);
2012   if (!psInfo)
2013   {
2014      res = FALSE;
2015      goto end;
2016   }
2017
2018   if (psInfo->active_page < 0)
2019   {
2020      res = TRUE;
2021      goto end;
2022   }
2023
2024   /*
2025    * Notify the current page.
2026    */
2027   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
2028   psn.hdr.code     = PSN_KILLACTIVE;
2029   psn.hdr.hwndFrom = hwndDlg;
2030   psn.hdr.idFrom   = 0;
2031   psn.lParam       = 0;
2032
2033   res = !SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
2034
2035 end:
2036   TRACE("<-- %d\n", res);
2037   return res;
2038 }
2039
2040 /******************************************************************************
2041  *            PROPSHEET_SetCurSel
2042  */
2043 static BOOL PROPSHEET_SetCurSel(HWND hwndDlg,
2044                                 int index,
2045                                 int skipdir,
2046                                 HPROPSHEETPAGE hpage
2047                                 )
2048 {
2049   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
2050   HWND hwndHelp  = GetDlgItem(hwndDlg, IDHELP);
2051   HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2052
2053   TRACE("index %d, skipdir %d, hpage %p\n", index, skipdir, hpage);
2054   /* hpage takes precedence over index */
2055   if (hpage != NULL)
2056     index = PROPSHEET_GetPageIndex(hpage, psInfo);
2057
2058   if (index < 0 || index >= psInfo->nPages)
2059   {
2060     TRACE("Could not find page to select!\n");
2061     return FALSE;
2062   }
2063
2064   while (1) {
2065     int result;
2066     PSHNOTIFY psn;
2067     RECT rc;
2068     LPCPROPSHEETPAGEW ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
2069
2070     if (hwndTabControl)
2071         SendMessageW(hwndTabControl, TCM_SETCURSEL, index, 0);
2072
2073     psn.hdr.code     = PSN_SETACTIVE;
2074     psn.hdr.hwndFrom = hwndDlg;
2075     psn.hdr.idFrom   = 0;
2076     psn.lParam       = 0;
2077
2078     if (!psInfo->proppage[index].hwndPage) {
2079       PROPSHEET_CreatePage(hwndDlg, index, psInfo, ppshpage);
2080     }
2081
2082     /* Resize the property sheet page to the fit in the Tab control
2083      * (for regular property sheets) or to fit in the client area (for
2084      * wizards).
2085      * NOTE: The resizing happens every time the page is selected and
2086      * not only when it's created (some applications depend on it). */
2087     PROPSHEET_GetPageRect(psInfo, hwndDlg, &rc, ppshpage);
2088     TRACE("setting page %p, rc (%ld,%ld)-(%ld,%ld) w=%ld, h=%ld\n",
2089           psInfo->proppage[index].hwndPage, rc.left, rc.top, rc.right, rc.bottom,
2090           rc.right - rc.left, rc.bottom - rc.top);
2091     SetWindowPos(psInfo->proppage[index].hwndPage, HWND_TOP,
2092                  rc.left, rc.top,
2093                  rc.right - rc.left, rc.bottom - rc.top, 0);
2094
2095     result = SendMessageW(psInfo->proppage[index].hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
2096     if (!result)
2097       break;
2098     if (result == -1) {
2099       index+=skipdir;
2100       if (index < 0) {
2101         index = 0;
2102         WARN("Tried to skip before first property sheet page!\n");
2103         break;
2104       }
2105       if (index >= psInfo->nPages) {
2106         WARN("Tried to skip after last property sheet page!\n");
2107         index = psInfo->nPages-1;
2108         break;
2109       }
2110     }
2111     else if (result != 0)
2112     {
2113       int old_index = index;
2114       index = PROPSHEET_FindPageByResId(psInfo, result);
2115       if(index >= psInfo->nPages) {
2116         index = old_index;
2117         WARN("Tried to skip to nonexistant page by res id\n");
2118         break;
2119       }
2120       continue;
2121     }
2122   }
2123   /*
2124    * Display the new page.
2125    */
2126   PROPSHEET_ShowPage(hwndDlg, index, psInfo);
2127
2128   if (psInfo->proppage[index].hasHelp)
2129     EnableWindow(hwndHelp, TRUE);
2130   else
2131     EnableWindow(hwndHelp, FALSE);
2132
2133   return TRUE;
2134 }
2135
2136 /******************************************************************************
2137  *            PROPSHEET_SetCurSelId
2138  *
2139  * Selects the page, specified by resource id.
2140  */
2141 static void PROPSHEET_SetCurSelId(HWND hwndDlg, int id)
2142 {
2143       int idx;
2144       PropSheetInfo* psInfo =
2145           (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
2146
2147       idx = PROPSHEET_FindPageByResId(psInfo, id);
2148       if (idx < psInfo->nPages )
2149       {
2150           if (PROPSHEET_CanSetCurSel(hwndDlg) != FALSE)
2151               PROPSHEET_SetCurSel(hwndDlg, idx, 1, 0);
2152       }
2153 }
2154
2155 /******************************************************************************
2156  *            PROPSHEET_SetTitleA
2157  */
2158 static void PROPSHEET_SetTitleA(HWND hwndDlg, DWORD dwStyle, LPCSTR lpszText)
2159 {
2160   if(HIWORD(lpszText))
2161   {
2162      WCHAR szTitle[256];
2163      MultiByteToWideChar(CP_ACP, 0, lpszText, -1,
2164                          szTitle, sizeof(szTitle)/sizeof(WCHAR));
2165      PROPSHEET_SetTitleW(hwndDlg, dwStyle, szTitle);
2166   }
2167   else
2168   {
2169      PROPSHEET_SetTitleW(hwndDlg, dwStyle, (LPCWSTR)lpszText);
2170   }
2171 }
2172
2173 /******************************************************************************
2174  *            PROPSHEET_SetTitleW
2175  */
2176 static void PROPSHEET_SetTitleW(HWND hwndDlg, DWORD dwStyle, LPCWSTR lpszText)
2177 {
2178   PropSheetInfo*        psInfo = (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
2179   WCHAR                 szTitle[256];
2180
2181   TRACE("'%s' (style %08lx)\n", debugstr_w(lpszText), dwStyle);
2182   if (HIWORD(lpszText) == 0) {
2183     if (!LoadStringW(psInfo->ppshheader.hInstance,
2184                      LOWORD(lpszText), szTitle, sizeof(szTitle)-sizeof(WCHAR)))
2185       return;
2186     lpszText = szTitle;
2187   }
2188   if (dwStyle & PSH_PROPTITLE)
2189   {
2190     WCHAR* dest;
2191     int lentitle = strlenW(lpszText);
2192     int lenprop  = strlenW(psInfo->strPropertiesFor);
2193
2194     dest = Alloc( (lentitle + lenprop + 1)*sizeof (WCHAR));
2195     strcpyW(dest, psInfo->strPropertiesFor);
2196     strcatW(dest, lpszText);
2197
2198     SetWindowTextW(hwndDlg, dest);
2199     Free(dest);
2200   }
2201   else
2202     SetWindowTextW(hwndDlg, lpszText);
2203 }
2204
2205 /******************************************************************************
2206  *            PROPSHEET_SetFinishTextA
2207  */
2208 static void PROPSHEET_SetFinishTextA(HWND hwndDlg, LPCSTR lpszText)
2209 {
2210   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
2211   HWND hwndButton = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2212
2213   TRACE("'%s'\n", lpszText);
2214   /* Set text, show and enable the Finish button */
2215   SetWindowTextA(hwndButton, lpszText);
2216   ShowWindow(hwndButton, SW_SHOW);
2217   EnableWindow(hwndButton, TRUE);
2218
2219   /* Make it default pushbutton */
2220   SendMessageW(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2221
2222   /* Hide Back button */
2223   hwndButton = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
2224   ShowWindow(hwndButton, SW_HIDE);
2225
2226   if (!psInfo->hasFinish)
2227   {
2228     /* Hide Next button */
2229     hwndButton = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
2230     ShowWindow(hwndButton, SW_HIDE);
2231   }
2232 }
2233
2234 /******************************************************************************
2235  *            PROPSHEET_SetFinishTextW
2236  */
2237 static void PROPSHEET_SetFinishTextW(HWND hwndDlg, LPCWSTR lpszText)
2238 {
2239   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
2240   HWND hwndButton = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2241
2242   TRACE("'%s'\n", debugstr_w(lpszText));
2243   /* Set text, show and enable the Finish button */
2244   SetWindowTextW(hwndButton, lpszText);
2245   ShowWindow(hwndButton, SW_SHOW);
2246   EnableWindow(hwndButton, TRUE);
2247
2248   /* Make it default pushbutton */
2249   SendMessageW(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2250
2251   /* Hide Back button */
2252   hwndButton = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
2253   ShowWindow(hwndButton, SW_HIDE);
2254
2255   if (!psInfo->hasFinish)
2256   {
2257     /* Hide Next button */
2258     hwndButton = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
2259     ShowWindow(hwndButton, SW_HIDE);
2260   }
2261 }
2262
2263 /******************************************************************************
2264  *            PROPSHEET_QuerySiblings
2265  */
2266 static LRESULT PROPSHEET_QuerySiblings(HWND hwndDlg,
2267                                        WPARAM wParam, LPARAM lParam)
2268 {
2269   int i = 0;
2270   HWND hwndPage;
2271   LRESULT msgResult = 0;
2272   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
2273
2274   while ((i < psInfo->nPages) && (msgResult == 0))
2275   {
2276     hwndPage = psInfo->proppage[i].hwndPage;
2277     msgResult = SendMessageW(hwndPage, PSM_QUERYSIBLINGS, wParam, lParam);
2278     i++;
2279   }
2280
2281   return msgResult;
2282 }
2283
2284
2285 /******************************************************************************
2286  *            PROPSHEET_AddPage
2287  */
2288 static BOOL PROPSHEET_AddPage(HWND hwndDlg,
2289                               HPROPSHEETPAGE hpage)
2290 {
2291   PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2292                                                      PropSheetInfoStr);
2293   HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2294   TCITEMW item;
2295   LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)hpage;
2296
2297   TRACE("hpage %p\n", hpage);
2298   /*
2299    * Allocate and fill in a new PropPageInfo entry.
2300    */
2301   psInfo->proppage = (PropPageInfo*) ReAlloc(psInfo->proppage,
2302                                                       sizeof(PropPageInfo) *
2303                                                       (psInfo->nPages + 1));
2304   if (!PROPSHEET_CollectPageInfo(ppsp, psInfo, psInfo->nPages))
2305       return FALSE;
2306
2307   psInfo->proppage[psInfo->nPages].hpage = hpage;
2308
2309   if (ppsp->dwFlags & PSP_PREMATURE)
2310   {
2311      /* Create the page but don't show it */
2312      PROPSHEET_CreatePage(hwndDlg, psInfo->nPages, psInfo, ppsp);
2313   }
2314
2315   /*
2316    * Add a new tab to the tab control.
2317    */
2318   item.mask = TCIF_TEXT;
2319   item.pszText = (LPWSTR) psInfo->proppage[psInfo->nPages].pszText;
2320   item.cchTextMax = MAX_TABTEXT_LENGTH;
2321
2322   if (psInfo->hImageList)
2323   {
2324     SendMessageW(hwndTabControl, TCM_SETIMAGELIST, 0, (LPARAM)psInfo->hImageList);
2325   }
2326
2327   if ( psInfo->proppage[psInfo->nPages].hasIcon )
2328   {
2329     item.mask |= TCIF_IMAGE;
2330     item.iImage = psInfo->nPages;
2331   }
2332
2333   SendMessageW(hwndTabControl, TCM_INSERTITEMW, psInfo->nPages + 1,
2334                (LPARAM)&item);
2335
2336   psInfo->nPages++;
2337
2338   /* If it is the only page - show it */
2339   if(psInfo->nPages == 1)
2340      PROPSHEET_SetCurSel(hwndDlg, 0, 1, 0);
2341   return TRUE;
2342 }
2343
2344 /******************************************************************************
2345  *            PROPSHEET_RemovePage
2346  */
2347 static BOOL PROPSHEET_RemovePage(HWND hwndDlg,
2348                                  int index,
2349                                  HPROPSHEETPAGE hpage)
2350 {
2351   PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2352                                                      PropSheetInfoStr);
2353   HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2354   PropPageInfo* oldPages;
2355
2356   TRACE("index %d, hpage %p\n", index, hpage);
2357   if (!psInfo) {
2358     return FALSE;
2359   }
2360   /*
2361    * hpage takes precedence over index.
2362    */
2363   if (hpage != 0)
2364   {
2365     index = PROPSHEET_GetPageIndex(hpage, psInfo);
2366   }
2367
2368   /* Make sure that index is within range */
2369   if (index < 0 || index >= psInfo->nPages)
2370   {
2371       TRACE("Could not find page to remove!\n");
2372       return FALSE;
2373   }
2374
2375   TRACE("total pages %d removing page %d active page %d\n",
2376         psInfo->nPages, index, psInfo->active_page);
2377   /*
2378    * Check if we're removing the active page.
2379    */
2380   if (index == psInfo->active_page)
2381   {
2382     if (psInfo->nPages > 1)
2383     {
2384       if (index > 0)
2385       {
2386         /* activate previous page  */
2387         PROPSHEET_SetCurSel(hwndDlg, index - 1, -1, 0);
2388       }
2389       else
2390       {
2391         /* activate the next page */
2392         PROPSHEET_SetCurSel(hwndDlg, index + 1, 1, 0);
2393         psInfo->active_page = index;
2394       }
2395     }
2396     else
2397     {
2398       psInfo->active_page = -1;
2399       if (!psInfo->isModeless)
2400       {
2401          psInfo->ended = TRUE;
2402          return TRUE;
2403       }
2404     }
2405   }
2406   else if (index < psInfo->active_page)
2407     psInfo->active_page--;
2408
2409   /* Unsubclass the page dialog window */
2410   if((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD) &&
2411      (psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
2412      ((PROPSHEETPAGEW*)psInfo->proppage[index].hpage)->dwFlags & PSP_HIDEHEADER))
2413   {
2414      RemoveWindowSubclass(psInfo->proppage[index].hwndPage,
2415                           PROPSHEET_WizardSubclassProc, 1);
2416   }
2417
2418   /* Destroy page dialog window */
2419   DestroyWindow(psInfo->proppage[index].hwndPage);
2420
2421   /* Free page resources */
2422   if(psInfo->proppage[index].hpage)
2423   {
2424      PROPSHEETPAGEW* psp = (PROPSHEETPAGEW*)psInfo->proppage[index].hpage;
2425
2426      if ((psp->dwFlags & PSP_USETITLE) && psInfo->proppage[index].pszText)
2427         Free ((LPVOID)psInfo->proppage[index].pszText);
2428
2429      DestroyPropertySheetPage(psInfo->proppage[index].hpage);
2430   }
2431
2432   /* Remove the tab */
2433   SendMessageW(hwndTabControl, TCM_DELETEITEM, index, 0);
2434
2435   oldPages = psInfo->proppage;
2436   psInfo->nPages--;
2437   psInfo->proppage = Alloc(sizeof(PropPageInfo) * psInfo->nPages);
2438
2439   if (index > 0)
2440     memcpy(&psInfo->proppage[0], &oldPages[0], index * sizeof(PropPageInfo));
2441
2442   if (index < psInfo->nPages)
2443     memcpy(&psInfo->proppage[index], &oldPages[index + 1],
2444            (psInfo->nPages - index) * sizeof(PropPageInfo));
2445
2446   Free(oldPages);
2447
2448   return FALSE;
2449 }
2450
2451 /******************************************************************************
2452  *            PROPSHEET_SetWizButtons
2453  *
2454  * This code will work if (and assumes that) the Next button is on top of the
2455  * Finish button. ie. Finish comes after Next in the Z order.
2456  * This means make sure the dialog template reflects this.
2457  *
2458  */
2459 static void PROPSHEET_SetWizButtons(HWND hwndDlg, DWORD dwFlags)
2460 {
2461   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2462                                                     PropSheetInfoStr);
2463   HWND hwndBack   = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
2464   HWND hwndNext   = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
2465   HWND hwndFinish = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2466
2467   TRACE("%ld\n", dwFlags);
2468
2469   EnableWindow(hwndBack, FALSE);
2470   EnableWindow(hwndNext, FALSE);
2471   EnableWindow(hwndFinish, FALSE);
2472
2473   if (dwFlags & PSWIZB_BACK)
2474     EnableWindow(hwndBack, TRUE);
2475
2476   if (dwFlags & PSWIZB_NEXT)
2477   {
2478     if (!psInfo->hasFinish)
2479     {
2480       /* Hide the Finish button */
2481       ShowWindow(hwndFinish, SW_HIDE);
2482     }
2483
2484     /* Show and enable the Next button */
2485     ShowWindow(hwndNext, SW_SHOW);
2486     EnableWindow(hwndNext, TRUE);
2487
2488     /* Set the Next button as the default pushbutton  */
2489     SendMessageW(hwndDlg, DM_SETDEFID, IDC_NEXT_BUTTON, 0);
2490   }
2491
2492   if (!psInfo->hasFinish)
2493   {
2494     if ((dwFlags & PSWIZB_FINISH) || (dwFlags & PSWIZB_DISABLEDFINISH))
2495     {
2496       /* Hide the Next button */
2497       ShowWindow(hwndNext, SW_HIDE);
2498       
2499       /* Show the Finish button */
2500       ShowWindow(hwndFinish, SW_SHOW);
2501
2502       if (!(dwFlags & PSWIZB_DISABLEDFINISH))
2503       {
2504         EnableWindow(hwndFinish, TRUE);
2505
2506         /* Set the Finish button as the default pushbutton  */
2507         SendMessageW(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2508       }
2509     }
2510   }
2511   else if (!(dwFlags & PSWIZB_DISABLEDFINISH))
2512     EnableWindow(hwndFinish, TRUE);
2513 }
2514
2515 /******************************************************************************
2516  *            PROPSHEET_InsertPage
2517  */
2518 static BOOL PROPSHEET_InsertPage(HWND hwndDlg, HPROPSHEETPAGE hpageInsertAfter, HPROPSHEETPAGE hpage)
2519 {
2520     if (!HIWORD(hpageInsertAfter))
2521         FIXME("(%p, %d, %p): stub\n", hwndDlg, LOWORD(hpageInsertAfter), hpage);
2522     else
2523         FIXME("(%p, %p, %p): stub\n", hwndDlg, hpageInsertAfter, hpage);
2524     return FALSE;
2525 }
2526
2527 /******************************************************************************
2528  *            PROPSHEET_SetHeaderTitleW
2529  */
2530 static void PROPSHEET_SetHeaderTitleW(HWND hwndDlg, int iPageIndex, LPCWSTR pszHeaderTitle)
2531 {
2532     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_w(pszHeaderTitle));
2533 }
2534
2535 /******************************************************************************
2536  *            PROPSHEET_SetHeaderTitleA
2537  */
2538 static void PROPSHEET_SetHeaderTitleA(HWND hwndDlg, int iPageIndex, LPCSTR pszHeaderTitle)
2539 {
2540     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_a(pszHeaderTitle));
2541 }
2542
2543 /******************************************************************************
2544  *            PROPSHEET_SetHeaderSubTitleW
2545  */
2546 static void PROPSHEET_SetHeaderSubTitleW(HWND hwndDlg, int iPageIndex, LPCWSTR pszHeaderSubTitle)
2547 {
2548     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_w(pszHeaderSubTitle));
2549 }
2550
2551 /******************************************************************************
2552  *            PROPSHEET_SetHeaderSubTitleA
2553  */
2554 static void PROPSHEET_SetHeaderSubTitleA(HWND hwndDlg, int iPageIndex, LPCSTR pszHeaderSubTitle)
2555 {
2556     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_a(pszHeaderSubTitle));
2557 }
2558
2559 /******************************************************************************
2560  *            PROPSHEET_HwndToIndex
2561  */
2562 static LRESULT PROPSHEET_HwndToIndex(HWND hwndDlg, HWND hPageDlg)
2563 {
2564     int index;
2565     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2566                                                        PropSheetInfoStr);
2567
2568     TRACE("(%p, %p)\n", hwndDlg, hPageDlg);
2569
2570     for (index = 0; index < psInfo->nPages; index++)
2571         if (psInfo->proppage[index].hwndPage == hPageDlg)
2572             return index;
2573     WARN("%p not found\n", hPageDlg);
2574     return -1;
2575 }
2576
2577 /******************************************************************************
2578  *            PROPSHEET_IndexToHwnd
2579  */
2580 static LRESULT PROPSHEET_IndexToHwnd(HWND hwndDlg, int iPageIndex)
2581 {
2582     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2583                                                        PropSheetInfoStr);
2584     TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
2585     if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
2586         WARN("%d out of range.\n", iPageIndex);
2587         return 0;
2588     }
2589     return (LRESULT)psInfo->proppage[iPageIndex].hwndPage;
2590 }
2591
2592 /******************************************************************************
2593  *            PROPSHEET_PageToIndex
2594  */
2595 static LRESULT PROPSHEET_PageToIndex(HWND hwndDlg, HPROPSHEETPAGE hPage)
2596 {
2597     int index;
2598     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2599                                                        PropSheetInfoStr);
2600
2601     TRACE("(%p, %p)\n", hwndDlg, hPage);
2602
2603     for (index = 0; index < psInfo->nPages; index++)
2604         if (psInfo->proppage[index].hpage == hPage)
2605             return index;
2606     WARN("%p not found\n", hPage);
2607     return -1;
2608 }
2609
2610 /******************************************************************************
2611  *            PROPSHEET_IndexToPage
2612  */
2613 static LRESULT PROPSHEET_IndexToPage(HWND hwndDlg, int iPageIndex)
2614 {
2615     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2616                                                        PropSheetInfoStr);
2617     TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
2618     if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
2619         WARN("%d out of range.\n", iPageIndex);
2620         return 0;
2621     }
2622     return (LRESULT)psInfo->proppage[iPageIndex].hpage;
2623 }
2624
2625 /******************************************************************************
2626  *            PROPSHEET_IdToIndex
2627  */
2628 static LRESULT PROPSHEET_IdToIndex(HWND hwndDlg, int iPageId)
2629 {
2630     int index;
2631     LPCPROPSHEETPAGEW psp;
2632     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2633                                                        PropSheetInfoStr);
2634     TRACE("(%p, %d)\n", hwndDlg, iPageId);
2635     for (index = 0; index < psInfo->nPages; index++) {
2636         psp = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
2637         if (psp->u.pszTemplate == MAKEINTRESOURCEW(iPageId))
2638             return index;
2639     }
2640
2641     return -1;
2642 }
2643
2644 /******************************************************************************
2645  *            PROPSHEET_IndexToId
2646  */
2647 static LRESULT PROPSHEET_IndexToId(HWND hwndDlg, int iPageIndex)
2648 {
2649     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2650                                                        PropSheetInfoStr);
2651     LPCPROPSHEETPAGEW psp;
2652     TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
2653     if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
2654         WARN("%d out of range.\n", iPageIndex);
2655         return 0;
2656     }
2657     psp = (LPCPROPSHEETPAGEW)psInfo->proppage[iPageIndex].hpage;
2658     if (psp->dwFlags & PSP_DLGINDIRECT || HIWORD(psp->u.pszTemplate)) {
2659         return 0;
2660     }
2661     return (LRESULT)psp->u.pszTemplate;
2662 }
2663
2664 /******************************************************************************
2665  *            PROPSHEET_GetResult
2666  */
2667 static LRESULT PROPSHEET_GetResult(HWND hwndDlg)
2668 {
2669     FIXME("(%p): stub\n", hwndDlg);
2670     return -1;
2671 }
2672
2673 /******************************************************************************
2674  *            PROPSHEET_RecalcPageSizes
2675  */
2676 static BOOL PROPSHEET_RecalcPageSizes(HWND hwndDlg)
2677 {
2678     FIXME("(%p): stub\n", hwndDlg);
2679     return FALSE;
2680 }
2681
2682 /******************************************************************************
2683  *            PROPSHEET_GetPageIndex
2684  *
2685  * Given a HPROPSHEETPAGE, returns the index of the corresponding page from
2686  * the array of PropPageInfo.
2687  */
2688 static int PROPSHEET_GetPageIndex(HPROPSHEETPAGE hpage, PropSheetInfo* psInfo)
2689 {
2690   BOOL found = FALSE;
2691   int index = 0;
2692
2693   TRACE("hpage %p\n", hpage);
2694   while ((index < psInfo->nPages) && (found == FALSE))
2695   {
2696     if (psInfo->proppage[index].hpage == hpage)
2697       found = TRUE;
2698     else
2699       index++;
2700   }
2701
2702   if (found == FALSE)
2703     index = -1;
2704
2705   return index;
2706 }
2707
2708 /******************************************************************************
2709  *            PROPSHEET_CleanUp
2710  */
2711 static void PROPSHEET_CleanUp(HWND hwndDlg)
2712 {
2713   int i;
2714   PropSheetInfo* psInfo = (PropSheetInfo*) RemovePropW(hwndDlg,
2715                                                        PropSheetInfoStr);
2716
2717   TRACE("\n");
2718   if (!psInfo) return;
2719   if (HIWORD(psInfo->ppshheader.pszCaption))
2720       Free ((LPVOID)psInfo->ppshheader.pszCaption);
2721
2722   for (i = 0; i < psInfo->nPages; i++)
2723   {
2724      PROPSHEETPAGEA* psp = (PROPSHEETPAGEA*)psInfo->proppage[i].hpage;
2725
2726      /* Unsubclass the page dialog window */
2727      if((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
2728         (psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
2729         (psp->dwFlags & PSP_HIDEHEADER))
2730      {
2731         RemoveWindowSubclass(psInfo->proppage[i].hwndPage,
2732                              PROPSHEET_WizardSubclassProc, 1);
2733      }
2734
2735      if(psInfo->proppage[i].hwndPage)
2736         DestroyWindow(psInfo->proppage[i].hwndPage);
2737
2738      if(psp)
2739      {
2740         if ((psp->dwFlags & PSP_USETITLE) && psInfo->proppage[i].pszText)
2741            Free ((LPVOID)psInfo->proppage[i].pszText);
2742
2743         DestroyPropertySheetPage(psInfo->proppage[i].hpage);
2744      }
2745   }
2746
2747   DeleteObject(psInfo->hFont);
2748   DeleteObject(psInfo->hFontBold);
2749   /* If we created the bitmaps, destroy them */
2750   if ((psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
2751       (!(psInfo->ppshheader.dwFlags & PSH_USEHBMWATERMARK)) )
2752       DeleteObject(psInfo->ppshheader.u4.hbmWatermark);
2753   if ((psInfo->ppshheader.dwFlags & PSH_HEADER) &&
2754       (!(psInfo->ppshheader.dwFlags & PSH_USEHBMHEADER)) )
2755       DeleteObject(psInfo->ppshheader.u5.hbmHeader);
2756
2757   Free(psInfo->proppage);
2758   Free(psInfo->strPropertiesFor);
2759   ImageList_Destroy(psInfo->hImageList);
2760
2761   GlobalFree((HGLOBAL)psInfo);
2762 }
2763
2764 static INT do_loop(PropSheetInfo *psInfo)
2765 {
2766     MSG msg;
2767     INT ret = -1;
2768     HWND hwnd = psInfo->hwnd;
2769
2770     while(IsWindow(hwnd) && !psInfo->ended && (ret = GetMessageW(&msg, NULL, 0, 0)))
2771     {
2772         if(ret == -1)
2773             break;
2774
2775         if(!IsDialogMessageW(hwnd, &msg))
2776         {
2777             TranslateMessage(&msg);
2778             DispatchMessageW(&msg);
2779         }
2780     }
2781
2782     if(ret == 0)
2783     {
2784         PostQuitMessage(msg.wParam);
2785         ret = -1;
2786     }
2787
2788     DestroyWindow(hwnd);
2789     return ret;
2790 }
2791
2792 /******************************************************************************
2793  *            PropertySheet    (COMCTL32.@)
2794  *            PropertySheetA   (COMCTL32.@)
2795  *
2796  * Creates a property sheet in the specified property sheet header.
2797  *
2798  * RETURNS
2799  *     Modal property sheets: Positive if successful or -1 otherwise.
2800  *     Modeless property sheets: Property sheet handle.
2801  *     Or:
2802  *| ID_PSREBOOTSYSTEM - The user must reboot the computer for the changes to take effect.
2803  *| ID_PSRESTARTWINDOWS - The user must restart Windows for the changes to take effect.
2804  */
2805 INT_PTR WINAPI PropertySheetA(LPCPROPSHEETHEADERA lppsh)
2806 {
2807   INT_PTR bRet = 0;
2808   PropSheetInfo* psInfo = (PropSheetInfo*) GlobalAlloc(GPTR,
2809                                                        sizeof(PropSheetInfo));
2810   UINT i, n;
2811   BYTE* pByte;
2812
2813   TRACE("(%p)\n", lppsh);
2814
2815   PROPSHEET_CollectSheetInfoA(lppsh, psInfo);
2816
2817   psInfo->proppage = (PropPageInfo*) Alloc(sizeof(PropPageInfo) *
2818                                                     lppsh->nPages);
2819   pByte = (BYTE*) psInfo->ppshheader.u3.ppsp;
2820
2821   for (n = i = 0; i < lppsh->nPages; i++, n++)
2822   {
2823     if (!(lppsh->dwFlags & PSH_PROPSHEETPAGE))
2824       psInfo->proppage[n].hpage = psInfo->ppshheader.u3.phpage[i];
2825     else
2826     {
2827        psInfo->proppage[n].hpage = CreatePropertySheetPageA((LPCPROPSHEETPAGEA)pByte);
2828        pByte += ((LPPROPSHEETPAGEA)pByte)->dwSize;
2829     }
2830
2831     if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2832                                psInfo, n))
2833     {
2834         if (lppsh->dwFlags & PSH_PROPSHEETPAGE)
2835             DestroyPropertySheetPage(psInfo->proppage[n].hpage);
2836         n--;
2837         psInfo->nPages--;
2838     }
2839   }
2840
2841   psInfo->unicode = FALSE;
2842   psInfo->ended = FALSE;
2843
2844   bRet = PROPSHEET_CreateDialog(psInfo);
2845   if(!psInfo->isModeless)
2846       bRet = do_loop(psInfo);
2847
2848   return bRet;
2849 }
2850
2851 /******************************************************************************
2852  *            PropertySheetW   (COMCTL32.@)
2853  *
2854  * See PropertySheetA.
2855  */
2856 INT_PTR WINAPI PropertySheetW(LPCPROPSHEETHEADERW lppsh)
2857 {
2858   INT_PTR bRet = 0;
2859   PropSheetInfo* psInfo = (PropSheetInfo*) GlobalAlloc(GPTR,
2860                                                        sizeof(PropSheetInfo));
2861   UINT i, n;
2862   BYTE* pByte;
2863
2864   TRACE("(%p)\n", lppsh);
2865
2866   PROPSHEET_CollectSheetInfoW(lppsh, psInfo);
2867
2868   psInfo->proppage = (PropPageInfo*) Alloc(sizeof(PropPageInfo) *
2869                                                     lppsh->nPages);
2870   pByte = (BYTE*) psInfo->ppshheader.u3.ppsp;
2871
2872   for (n = i = 0; i < lppsh->nPages; i++, n++)
2873   {
2874     if (!(lppsh->dwFlags & PSH_PROPSHEETPAGE))
2875       psInfo->proppage[n].hpage = psInfo->ppshheader.u3.phpage[i];
2876     else
2877     {
2878        psInfo->proppage[n].hpage = CreatePropertySheetPageW((LPCPROPSHEETPAGEW)pByte);
2879        pByte += ((LPPROPSHEETPAGEW)pByte)->dwSize;
2880     }
2881
2882     if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2883                                psInfo, n))
2884     {
2885         if (lppsh->dwFlags & PSH_PROPSHEETPAGE)
2886             DestroyPropertySheetPage(psInfo->proppage[n].hpage);
2887         n--;
2888         psInfo->nPages--;
2889     }
2890   }
2891
2892   psInfo->unicode = TRUE;
2893   psInfo->ended = FALSE;
2894
2895   bRet = PROPSHEET_CreateDialog(psInfo);
2896   if(!psInfo->isModeless)
2897       bRet = do_loop(psInfo);
2898
2899   return bRet;
2900 }
2901
2902 /******************************************************************************
2903  *            CreatePropertySheetPage    (COMCTL32.@)
2904  *            CreatePropertySheetPageA   (COMCTL32.@)
2905  *
2906  * Creates a new property sheet page.
2907  *
2908  * RETURNS
2909  *     Success: Handle to new property sheet page.
2910  *     Failure: NULL.
2911  *
2912  * NOTES
2913  *     An application must use the PSM_ADDPAGE message to add the new page to
2914  *     an existing property sheet.
2915  */
2916 HPROPSHEETPAGE WINAPI CreatePropertySheetPageA(
2917                           LPCPROPSHEETPAGEA lpPropSheetPage)
2918 {
2919   PROPSHEETPAGEW* ppsp = Alloc(sizeof(PROPSHEETPAGEW));
2920
2921   memcpy(ppsp,lpPropSheetPage,min(lpPropSheetPage->dwSize,sizeof(PROPSHEETPAGEA)));
2922
2923   ppsp->dwFlags &= ~ PSP_INTERNAL_UNICODE;
2924   if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) && HIWORD( ppsp->u.pszTemplate ) )
2925   {
2926      int len = strlen(lpPropSheetPage->u.pszTemplate);
2927
2928      ppsp->u.pszTemplate = Alloc( len+1 );
2929      strcpy( (LPSTR)ppsp->u.pszTemplate, lpPropSheetPage->u.pszTemplate );
2930   }
2931   if ( (ppsp->dwFlags & PSP_USEICONID) && HIWORD( ppsp->u2.pszIcon ) )
2932   {
2933       PROPSHEET_AtoW(&ppsp->u2.pszIcon, lpPropSheetPage->u2.pszIcon);
2934   }
2935
2936   if ((ppsp->dwFlags & PSP_USETITLE) && HIWORD( ppsp->pszTitle ))
2937   {
2938       PROPSHEET_AtoW(&ppsp->pszTitle, lpPropSheetPage->pszTitle);
2939   }
2940   else if ( !(ppsp->dwFlags & PSP_USETITLE) )
2941       ppsp->pszTitle = NULL;
2942
2943   return (HPROPSHEETPAGE)ppsp;
2944 }
2945
2946 /******************************************************************************
2947  *            CreatePropertySheetPageW   (COMCTL32.@)
2948  *
2949  * See CreatePropertySheetA.
2950  */
2951 HPROPSHEETPAGE WINAPI CreatePropertySheetPageW(LPCPROPSHEETPAGEW lpPropSheetPage)
2952 {
2953   PROPSHEETPAGEW* ppsp = Alloc(sizeof(PROPSHEETPAGEW));
2954
2955   memcpy(ppsp,lpPropSheetPage,min(lpPropSheetPage->dwSize,sizeof(PROPSHEETPAGEW)));
2956
2957   ppsp->dwFlags |= PSP_INTERNAL_UNICODE;
2958
2959   if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) && HIWORD( ppsp->u.pszTemplate ) )
2960   {
2961     int len = strlenW(lpPropSheetPage->u.pszTemplate);
2962
2963     ppsp->u.pszTemplate = Alloc( (len+1)*sizeof (WCHAR) );
2964     strcpyW( (WCHAR *)ppsp->u.pszTemplate, lpPropSheetPage->u.pszTemplate );
2965   }
2966   if ( (ppsp->dwFlags & PSP_USEICONID) && HIWORD( ppsp->u2.pszIcon ) )
2967   {
2968       int len = strlenW(lpPropSheetPage->u2.pszIcon);
2969       ppsp->u2.pszIcon = Alloc( (len+1)*sizeof (WCHAR) );
2970       strcpyW( (WCHAR *)ppsp->u2.pszIcon, lpPropSheetPage->u2.pszIcon );
2971   }
2972
2973   if ((ppsp->dwFlags & PSP_USETITLE) && HIWORD( ppsp->pszTitle ))
2974   {
2975       int len = strlenW(lpPropSheetPage->pszTitle);
2976       ppsp->pszTitle = Alloc( (len+1)*sizeof (WCHAR) );
2977       strcpyW( (WCHAR *)ppsp->pszTitle, lpPropSheetPage->pszTitle );
2978   }
2979   else if ( !(ppsp->dwFlags & PSP_USETITLE) )
2980       ppsp->pszTitle = NULL;
2981
2982   return (HPROPSHEETPAGE)ppsp;
2983 }
2984
2985 /******************************************************************************
2986  *            DestroyPropertySheetPage   (COMCTL32.@)
2987  *
2988  * Destroys a property sheet page previously created with
2989  * CreatePropertySheetA() or CreatePropertySheetW() and frees the associated
2990  * memory.
2991  *
2992  * RETURNS
2993  *     Success: TRUE
2994  *     Failure: FALSE
2995  */
2996 BOOL WINAPI DestroyPropertySheetPage(HPROPSHEETPAGE hPropPage)
2997 {
2998   PROPSHEETPAGEW *psp = (PROPSHEETPAGEW *)hPropPage;
2999
3000   if (!psp)
3001      return FALSE;
3002
3003   if ( !(psp->dwFlags & PSP_DLGINDIRECT) && HIWORD( psp->u.pszTemplate ) )
3004      Free ((LPVOID)psp->u.pszTemplate);
3005
3006   if ( (psp->dwFlags & PSP_USEICONID) && HIWORD( psp->u2.pszIcon ) )
3007      Free ((LPVOID)psp->u2.pszIcon);
3008
3009   if ((psp->dwFlags & PSP_USETITLE) && HIWORD( psp->pszTitle ))
3010      Free ((LPVOID)psp->pszTitle);
3011
3012   Free(hPropPage);
3013
3014   return TRUE;
3015 }
3016
3017 /******************************************************************************
3018  *            PROPSHEET_IsDialogMessage
3019  */
3020 static BOOL PROPSHEET_IsDialogMessage(HWND hwnd, LPMSG lpMsg)
3021 {
3022    PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd, PropSheetInfoStr);
3023
3024    TRACE("\n");
3025    if (!psInfo || (hwnd != lpMsg->hwnd && !IsChild(hwnd, lpMsg->hwnd)))
3026       return FALSE;
3027
3028    if (lpMsg->message == WM_KEYDOWN && (GetKeyState(VK_CONTROL) & 0x8000))
3029    {
3030       int new_page = 0;
3031       INT dlgCode = SendMessageW(lpMsg->hwnd, WM_GETDLGCODE, 0, (LPARAM)lpMsg);
3032
3033       if (!(dlgCode & DLGC_WANTMESSAGE))
3034       {
3035          switch (lpMsg->wParam)
3036          {
3037             case VK_TAB:
3038                if (GetKeyState(VK_SHIFT) & 0x8000)
3039                    new_page = -1;
3040                 else
3041                    new_page = 1;
3042                break;
3043
3044             case VK_NEXT:   new_page = 1;  break;
3045             case VK_PRIOR:  new_page = -1; break;
3046          }
3047       }
3048
3049       if (new_page)
3050       {
3051          if (PROPSHEET_CanSetCurSel(hwnd) != FALSE)
3052          {
3053             new_page += psInfo->active_page;
3054
3055             if (new_page < 0)
3056                new_page = psInfo->nPages - 1;
3057             else if (new_page >= psInfo->nPages)
3058                new_page = 0;
3059
3060             PROPSHEET_SetCurSel(hwnd, new_page, 1, 0);
3061          }
3062
3063          return TRUE;
3064       }
3065    }
3066
3067    return IsDialogMessageW(hwnd, lpMsg);
3068 }
3069
3070 /******************************************************************************
3071  *            PROPSHEET_DoCommand
3072  */
3073 static BOOL PROPSHEET_DoCommand(HWND hwnd, WORD wID)
3074 {
3075
3076     switch (wID) {
3077
3078     case IDOK:
3079     case IDC_APPLY_BUTTON:
3080         {
3081             HWND hwndApplyBtn = GetDlgItem(hwnd, IDC_APPLY_BUTTON);
3082
3083             if (PROPSHEET_Apply(hwnd, wID == IDOK ? 1: 0) == FALSE)
3084                 break;
3085
3086             if (wID == IDOK)
3087                 {
3088                     PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3089                                                                       PropSheetInfoStr);
3090                     int result = TRUE;
3091
3092                     if (psInfo->restartWindows)
3093                         result = ID_PSRESTARTWINDOWS;
3094
3095                     /* reboot system takes precedence over restart windows */
3096                     if (psInfo->rebootSystem)
3097                         result = ID_PSREBOOTSYSTEM;
3098
3099                     if (psInfo->isModeless)
3100                         psInfo->activeValid = FALSE;
3101                     else
3102                         psInfo->ended = TRUE;
3103                 }
3104             else
3105                 EnableWindow(hwndApplyBtn, FALSE);
3106
3107             break;
3108         }
3109
3110     case IDC_BACK_BUTTON:
3111         PROPSHEET_Back(hwnd);
3112         break;
3113
3114     case IDC_NEXT_BUTTON:
3115         PROPSHEET_Next(hwnd);
3116         break;
3117
3118     case IDC_FINISH_BUTTON:
3119         PROPSHEET_Finish(hwnd);
3120         break;
3121
3122     case IDCANCEL:
3123         PROPSHEET_Cancel(hwnd, 0);
3124         break;
3125
3126     case IDHELP:
3127         PROPSHEET_Help(hwnd);
3128         break;
3129
3130     default:
3131         return FALSE;
3132     }
3133
3134     return TRUE;
3135 }
3136
3137 /******************************************************************************
3138  *            PROPSHEET_Paint
3139  */
3140 static LRESULT PROPSHEET_Paint(HWND hwnd, HDC hdcParam)
3141 {
3142     PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd, PropSheetInfoStr);
3143     PAINTSTRUCT ps;
3144     HDC hdc, hdcSrc;
3145     BITMAP bm;
3146     HBITMAP hbmp;
3147     HPALETTE hOldPal = 0;
3148     int offsety = 0;
3149     HBRUSH hbr;
3150     RECT r, rzone;
3151     LPCPROPSHEETPAGEW ppshpage;
3152     WCHAR szBuffer[256];
3153     int nLength;
3154
3155     hdc = hdcParam ? hdcParam : BeginPaint(hwnd, &ps);
3156     if (!hdc) return 1;
3157
3158     hdcSrc = CreateCompatibleDC(0);
3159     ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[psInfo->active_page].hpage;
3160
3161     if (psInfo->ppshheader.dwFlags & PSH_USEHPLWATERMARK) 
3162         hOldPal = SelectPalette(hdc, psInfo->ppshheader.hplWatermark, FALSE);
3163
3164     if ( (!(ppshpage->dwFlags & PSP_HIDEHEADER)) &&
3165          (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
3166          (psInfo->ppshheader.dwFlags & PSH_HEADER) ) 
3167     {
3168         HWND hwndLineHeader = GetDlgItem(hwnd, IDC_SUNKEN_LINEHEADER);
3169         HFONT hOldFont;
3170         COLORREF clrOld = 0;
3171         int oldBkMode = 0;
3172
3173         hbmp = SelectObject(hdcSrc, psInfo->ppshheader.u5.hbmHeader);
3174         hOldFont = SelectObject(hdc, psInfo->hFontBold);
3175
3176         GetClientRect(hwndLineHeader, &r);
3177         MapWindowPoints(hwndLineHeader, hwnd, (LPPOINT) &r, 2);
3178         SetRect(&rzone, 0, 0, r.right + 1, r.top - 1);
3179
3180         GetObjectW(psInfo->ppshheader.u5.hbmHeader, sizeof(BITMAP), (LPVOID)&bm);               
3181
3182         if (psInfo->ppshheader.dwFlags & PSH_WIZARD97_OLD)
3183         {
3184             /* Fill the unoccupied part of the header with color of the
3185              * left-top pixel, but do it only when needed.
3186              */
3187             if (bm.bmWidth < r.right || bm.bmHeight < r.bottom)
3188             {
3189                 hbr = CreateSolidBrush(GetPixel(hdcSrc, 0, 0));
3190                 CopyRect(&r, &rzone);
3191                 if (bm.bmWidth < r.right)
3192                 {
3193                     r.left = bm.bmWidth;
3194                     FillRect(hdc, &r, hbr);
3195                 }
3196                 if (bm.bmHeight < r.bottom)
3197                 {
3198                     r.left = 0;
3199                     r.top = bm.bmHeight;
3200                     FillRect(hdc, &r, hbr);
3201                 }
3202                 DeleteObject(hbr);
3203             }
3204
3205             /* Draw the header itself. */
3206             BitBlt(hdc, 0, 0,
3207                    bm.bmWidth, min(bm.bmHeight, rzone.bottom),
3208                    hdcSrc, 0, 0, SRCCOPY);
3209         }
3210         else
3211         {
3212             hbr = GetSysColorBrush(COLOR_WINDOW);
3213             FillRect(hdc, &rzone, hbr);
3214
3215             /* Draw the header bitmap. It's always centered like a
3216              * common 49 x 49 bitmap. */
3217             BitBlt(hdc, rzone.right - 49 - ((rzone.bottom - 49) / 2),
3218                    (rzone.bottom - 49) / 2,
3219                    bm.bmWidth, bm.bmHeight,
3220                    hdcSrc, 0, 0, SRCCOPY);
3221
3222             /* NOTE: Native COMCTL32 draws a white stripe over the bitmap
3223              * if its height is smaller than 49 pixels. Because the reason
3224              * for this bug is unknown the current code doesn't try to
3225              * replicate it. */
3226         }
3227
3228         clrOld = SetTextColor (hdc, 0x00000000);
3229         oldBkMode = SetBkMode (hdc, TRANSPARENT); 
3230
3231         if (ppshpage->dwFlags & PSP_USEHEADERTITLE) {
3232             SetRect(&r, 20, 10, 0, 0);
3233             if (HIWORD(ppshpage->pszHeaderTitle))
3234             {
3235                 if (psInfo->unicode)
3236                     DrawTextW(hdc, (LPWSTR)ppshpage->pszHeaderTitle,
3237                               -1, &r, DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
3238                 else
3239                     DrawTextA(hdc, (LPCSTR)ppshpage->pszHeaderTitle,
3240                               -1, &r, DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
3241             }
3242             else
3243             {
3244                 nLength = LoadStringW(ppshpage->hInstance, (UINT_PTR)ppshpage->pszHeaderTitle,
3245                                       szBuffer, 256);
3246                 if (nLength != 0)
3247                 {
3248                     DrawTextW(hdc, szBuffer, nLength,
3249                               &r, DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
3250                 }
3251             }
3252         }
3253
3254         if (ppshpage->dwFlags & PSP_USEHEADERSUBTITLE) {
3255             SelectObject(hdc, psInfo->hFont);
3256             SetRect(&r, 40, 25, rzone.right - 69, rzone.bottom);
3257             if (HIWORD(ppshpage->pszHeaderTitle))
3258             {
3259                 if (psInfo->unicode)
3260                     DrawTextW(hdc, (LPWSTR)ppshpage->pszHeaderSubTitle,
3261                               -1, &r, DT_LEFT | DT_SINGLELINE);
3262                 else
3263                     DrawTextA(hdc, (LPCSTR)ppshpage->pszHeaderSubTitle,
3264                               -1, &r, DT_LEFT | DT_SINGLELINE);
3265             }
3266             else
3267             {
3268                 nLength = LoadStringW(ppshpage->hInstance, (UINT_PTR)ppshpage->pszHeaderSubTitle,
3269                                       szBuffer, 256);
3270                 if (nLength != 0)
3271                 {
3272                     DrawTextW(hdc, szBuffer, nLength,
3273                               &r, DT_LEFT | DT_SINGLELINE);
3274                 }
3275             }
3276         }
3277
3278         offsety = rzone.bottom + 2;
3279
3280         SetTextColor(hdc, clrOld);
3281         SetBkMode(hdc, oldBkMode);
3282         SelectObject(hdc, hOldFont);
3283         SelectObject(hdcSrc, hbmp);
3284     }
3285
3286     if ( (ppshpage->dwFlags & PSP_HIDEHEADER) &&
3287          (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
3288          (psInfo->ppshheader.dwFlags & PSH_WATERMARK) ) 
3289     {
3290         HWND hwndLine = GetDlgItem(hwnd, IDC_SUNKEN_LINE);          
3291
3292         GetClientRect(hwndLine, &r);
3293         MapWindowPoints(hwndLine, hwnd, (LPPOINT) &r, 2);
3294
3295         rzone.left = 0;
3296         rzone.top = 0;
3297         rzone.right = r.right;
3298         rzone.bottom = r.top - 1;
3299
3300         hbr = GetSysColorBrush(COLOR_WINDOW);
3301         FillRect(hdc, &rzone, hbr);
3302
3303         GetObjectW(psInfo->ppshheader.u4.hbmWatermark, sizeof(BITMAP), (LPVOID)&bm);
3304         hbmp = SelectObject(hdcSrc, psInfo->ppshheader.u4.hbmWatermark);
3305
3306         BitBlt(hdc, 0, offsety, min(bm.bmWidth, r.right),
3307                min(bm.bmHeight, r.bottom), hdcSrc, 0, 0, SRCCOPY);
3308
3309         /* If the bitmap is not big enough, fill the remaining area
3310            with the color of pixel (0,0) of bitmap - see MSDN */
3311         if (r.top > bm.bmHeight) {
3312             r.bottom = r.top - 1;
3313             r.top = bm.bmHeight;
3314             r.left = 0;
3315             r.right = bm.bmWidth;
3316             hbr = CreateSolidBrush(GetPixel(hdcSrc, 0, 0));
3317             FillRect(hdc, &r, hbr);
3318             DeleteObject(hbr);
3319         }
3320
3321         SelectObject(hdcSrc, hbmp);         
3322     }
3323
3324     if (psInfo->ppshheader.dwFlags & PSH_USEHPLWATERMARK) 
3325         SelectPalette(hdc, hOldPal, FALSE);
3326
3327     DeleteDC(hdcSrc);
3328
3329     if (!hdcParam) EndPaint(hwnd, &ps);
3330
3331     return 0;
3332 }
3333
3334 /******************************************************************************
3335  *            PROPSHEET_DialogProc
3336  */
3337 INT_PTR CALLBACK
3338 PROPSHEET_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3339 {
3340   TRACE("hwnd=%p msg=0x%04x wparam=%x lparam=%lx\n",
3341         hwnd, uMsg, wParam, lParam);
3342
3343   switch (uMsg)
3344   {
3345     case WM_INITDIALOG:
3346     {
3347       PropSheetInfo* psInfo = (PropSheetInfo*) lParam;
3348       WCHAR* strCaption = (WCHAR*)Alloc(MAX_CAPTION_LENGTH*sizeof(WCHAR));
3349       HWND hwndTabCtrl = GetDlgItem(hwnd, IDC_TABCONTROL);
3350       LPCPROPSHEETPAGEW ppshpage;
3351       int idx;
3352       LOGFONTW logFont;
3353
3354       /* Using PropSheetInfoStr to store extra data doesn't match the native
3355        * common control: native uses TCM_[GS]ETITEM
3356        */
3357       SetPropW(hwnd, PropSheetInfoStr, (HANDLE)psInfo);
3358
3359       /*
3360        * psInfo->hwnd is not being used by WINE code - it exists
3361        * for compatibility with "real" Windoze. The same about
3362        * SetWindowLongPtr - WINE is only using the PropSheetInfoStr
3363        * property.
3364        */
3365       psInfo->hwnd = hwnd;
3366       SetWindowLongPtrW(hwnd, DWLP_USER, (DWORD_PTR)psInfo);
3367
3368       /* set up the Next and Back buttons by default */
3369       PROPSHEET_SetWizButtons(hwnd, PSWIZB_BACK|PSWIZB_NEXT);
3370
3371       /* Set up fonts */
3372       SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
3373       psInfo->hFont = CreateFontIndirectW (&logFont);
3374       logFont.lfWeight = FW_BOLD;
3375       psInfo->hFontBold = CreateFontIndirectW (&logFont);
3376       
3377       /*
3378        * Small icon in the title bar.
3379        */
3380       if ((psInfo->ppshheader.dwFlags & PSH_USEICONID) ||
3381           (psInfo->ppshheader.dwFlags & PSH_USEHICON))
3382       {
3383         HICON hIcon;
3384         int icon_cx = GetSystemMetrics(SM_CXSMICON);
3385         int icon_cy = GetSystemMetrics(SM_CYSMICON);
3386
3387         if (psInfo->ppshheader.dwFlags & PSH_USEICONID)
3388           hIcon = LoadImageW(psInfo->ppshheader.hInstance,
3389                              psInfo->ppshheader.u.pszIcon,
3390                              IMAGE_ICON,
3391                              icon_cx, icon_cy,
3392                              LR_DEFAULTCOLOR);
3393         else
3394           hIcon = psInfo->ppshheader.u.hIcon;
3395
3396         SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)hIcon);
3397       }
3398
3399       if (psInfo->ppshheader.dwFlags & PSH_USEHICON)
3400         SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)psInfo->ppshheader.u.hIcon);
3401
3402       psInfo->strPropertiesFor = strCaption;
3403
3404       GetWindowTextW(hwnd, psInfo->strPropertiesFor, MAX_CAPTION_LENGTH);
3405
3406       PROPSHEET_CreateTabControl(hwnd, psInfo);
3407
3408       PROPSHEET_LoadWizardBitmaps(psInfo);
3409
3410       if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
3411       {
3412         ShowWindow(hwndTabCtrl, SW_HIDE);
3413         PROPSHEET_AdjustSizeWizard(hwnd, psInfo);
3414         PROPSHEET_AdjustButtonsWizard(hwnd, psInfo);
3415       }
3416       else
3417       {
3418         if (PROPSHEET_SizeMismatch(hwnd, psInfo))
3419         {
3420           PROPSHEET_AdjustSize(hwnd, psInfo);
3421           PROPSHEET_AdjustButtons(hwnd, psInfo);
3422         }
3423       }
3424
3425       if (psInfo->useCallback)
3426              (*(psInfo->ppshheader.pfnCallback))(hwnd,
3427                                               PSCB_INITIALIZED, (LPARAM)0);
3428
3429       idx = psInfo->active_page;
3430       ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[idx].hpage;
3431       psInfo->active_page = -1;
3432
3433       PROPSHEET_SetCurSel(hwnd, idx, 1, psInfo->proppage[idx].hpage);
3434
3435       /* doing TCM_SETCURSEL seems to be needed even in case of PSH_WIZARD,
3436        * as some programs call TCM_GETCURSEL to get the current selection
3437        * from which to switch to the next page */
3438       SendMessageW(hwndTabCtrl, TCM_SETCURSEL, psInfo->active_page, 0);
3439
3440       if (!HIWORD(psInfo->ppshheader.pszCaption) &&
3441               psInfo->ppshheader.hInstance)
3442       {
3443          WCHAR szText[256];
3444
3445          if (LoadStringW(psInfo->ppshheader.hInstance,
3446                          (UINT_PTR)psInfo->ppshheader.pszCaption, szText, 255))
3447             PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags, szText);
3448       }
3449       else
3450       {
3451          PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags,
3452                          psInfo->ppshheader.pszCaption);
3453       }
3454
3455       return TRUE;
3456     }
3457
3458     case WM_PAINT:
3459       PROPSHEET_Paint(hwnd, (HDC)wParam);
3460       return TRUE;
3461
3462     case WM_DESTROY:
3463       PROPSHEET_CleanUp(hwnd);
3464       return TRUE;
3465
3466     case WM_CLOSE:
3467       PROPSHEET_Cancel(hwnd, 1);
3468       return TRUE;
3469
3470     case WM_COMMAND:
3471       if (!PROPSHEET_DoCommand(hwnd, LOWORD(wParam)))
3472       {
3473           PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd, PropSheetInfoStr);
3474
3475           if (!psInfo)
3476               return FALSE;
3477
3478           /* No default handler, forward notification to active page */
3479           if (psInfo->activeValid && psInfo->active_page != -1)
3480           {
3481              HWND hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
3482              SendMessageW(hwndPage, WM_COMMAND, wParam, lParam);
3483           }
3484       }
3485       return TRUE;
3486
3487     case WM_SYSCOMMAND:
3488       switch(wParam & 0xfff0)
3489       {
3490         case SC_CLOSE:
3491           PROPSHEET_Cancel(hwnd, 1);
3492           return TRUE;
3493
3494         default:
3495           return FALSE;
3496       }
3497
3498     case WM_NOTIFY:
3499     {
3500       NMHDR* pnmh = (LPNMHDR) lParam;
3501
3502       if (pnmh->code == TCN_SELCHANGE)
3503       {
3504         int index = SendMessageW(pnmh->hwndFrom, TCM_GETCURSEL, 0, 0);
3505         PROPSHEET_SetCurSel(hwnd, index, 1, 0);
3506       }
3507
3508       if(pnmh->code == TCN_SELCHANGING)
3509       {
3510         BOOL bRet = PROPSHEET_CanSetCurSel(hwnd);
3511         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, !bRet);
3512         return TRUE;
3513       }
3514
3515       return FALSE;
3516     }
3517   
3518     case WM_SYSCOLORCHANGE:
3519       COMCTL32_RefreshSysColors();
3520       return FALSE;
3521
3522     case PSM_GETCURRENTPAGEHWND:
3523     {
3524       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3525                                                         PropSheetInfoStr);
3526       HWND hwndPage = 0;
3527
3528       if (!psInfo)
3529         return FALSE;
3530
3531       if (psInfo->activeValid && psInfo->active_page != -1)
3532         hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
3533
3534       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (DWORD_PTR)hwndPage);
3535
3536       return TRUE;
3537     }
3538
3539     case PSM_CHANGED:
3540       PROPSHEET_Changed(hwnd, (HWND)wParam);
3541       return TRUE;
3542
3543     case PSM_UNCHANGED:
3544       PROPSHEET_UnChanged(hwnd, (HWND)wParam);
3545       return TRUE;
3546
3547     case PSM_GETTABCONTROL:
3548     {
3549       HWND hwndTabCtrl = GetDlgItem(hwnd, IDC_TABCONTROL);
3550
3551       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (DWORD_PTR)hwndTabCtrl);
3552
3553       return TRUE;
3554     }
3555
3556     case PSM_SETCURSEL:
3557     {
3558       BOOL msgResult;
3559
3560       msgResult = PROPSHEET_CanSetCurSel(hwnd);
3561       if(msgResult != FALSE)
3562       {
3563         msgResult = PROPSHEET_SetCurSel(hwnd,
3564                                        (int)wParam,
3565                                        1,
3566                                        (HPROPSHEETPAGE)lParam);
3567       }
3568
3569       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3570
3571       return TRUE;
3572     }
3573
3574     case PSM_CANCELTOCLOSE:
3575     {
3576       WCHAR buf[MAX_BUTTONTEXT_LENGTH];
3577       HWND hwndOK = GetDlgItem(hwnd, IDOK);
3578       HWND hwndCancel = GetDlgItem(hwnd, IDCANCEL);
3579
3580       EnableWindow(hwndCancel, FALSE);
3581       if (LoadStringW(COMCTL32_hModule, IDS_CLOSE, buf, sizeof(buf)))
3582          SetWindowTextW(hwndOK, buf);
3583
3584       return FALSE;
3585     }
3586
3587     case PSM_RESTARTWINDOWS:
3588     {
3589       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3590                                                         PropSheetInfoStr);
3591
3592       if (!psInfo)
3593         return FALSE;
3594
3595       psInfo->restartWindows = TRUE;
3596       return TRUE;
3597     }
3598
3599     case PSM_REBOOTSYSTEM:
3600     {
3601       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3602                                                         PropSheetInfoStr);
3603
3604       if (!psInfo)
3605         return FALSE;
3606
3607       psInfo->rebootSystem = TRUE;
3608       return TRUE;
3609     }
3610
3611     case PSM_SETTITLEA:
3612       PROPSHEET_SetTitleA(hwnd, (DWORD) wParam, (LPCSTR) lParam);
3613       return TRUE;
3614
3615     case PSM_SETTITLEW:
3616       PROPSHEET_SetTitleW(hwnd, (DWORD) wParam, (LPCWSTR) lParam);
3617       return TRUE;
3618
3619     case PSM_APPLY:
3620     {
3621       BOOL msgResult = PROPSHEET_Apply(hwnd, 0);
3622
3623       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3624
3625       return TRUE;
3626     }
3627
3628     case PSM_QUERYSIBLINGS:
3629     {
3630       LRESULT msgResult = PROPSHEET_QuerySiblings(hwnd, wParam, lParam);
3631
3632       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3633
3634       return TRUE;
3635     }
3636
3637     case PSM_ADDPAGE:
3638     {
3639       /*
3640        * Note: MSVC++ 6.0 documentation says that PSM_ADDPAGE does not have
3641        *       a return value. This is not true. PSM_ADDPAGE returns TRUE
3642        *       on success or FALSE otherwise, as specified on MSDN Online.
3643        *       Also see the MFC code for
3644        *       CPropertySheet::AddPage(CPropertyPage* pPage).
3645        */
3646
3647       BOOL msgResult = PROPSHEET_AddPage(hwnd, (HPROPSHEETPAGE)lParam);
3648
3649       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3650
3651       return TRUE;
3652     }
3653
3654     case PSM_REMOVEPAGE:
3655       PROPSHEET_RemovePage(hwnd, (int)wParam, (HPROPSHEETPAGE)lParam);
3656       return TRUE;
3657
3658     case PSM_ISDIALOGMESSAGE:
3659     {
3660        BOOL msgResult = PROPSHEET_IsDialogMessage(hwnd, (LPMSG)lParam);
3661        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3662        return TRUE;
3663     }
3664
3665     case PSM_PRESSBUTTON:
3666       PROPSHEET_PressButton(hwnd, (int)wParam);
3667       return TRUE;
3668
3669     case PSM_SETFINISHTEXTA:
3670       PROPSHEET_SetFinishTextA(hwnd, (LPCSTR) lParam);
3671       return TRUE;
3672
3673     case PSM_SETWIZBUTTONS:
3674       PROPSHEET_SetWizButtons(hwnd, (DWORD)lParam);
3675       return TRUE;
3676
3677     case PSM_SETCURSELID:
3678         PROPSHEET_SetCurSelId(hwnd, (int)lParam);
3679         return TRUE;
3680
3681     case PSM_SETFINISHTEXTW:
3682         PROPSHEET_SetFinishTextW(hwnd, (LPCWSTR) lParam);
3683         return FALSE;
3684
3685     case PSM_INSERTPAGE:
3686     {
3687         BOOL msgResult = PROPSHEET_InsertPage(hwnd, (HPROPSHEETPAGE)wParam, (HPROPSHEETPAGE)lParam);
3688         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3689         return TRUE;
3690     }
3691
3692     case PSM_SETHEADERTITLEW:
3693         PROPSHEET_SetHeaderTitleW(hwnd, (int)wParam, (LPCWSTR)lParam);
3694         return TRUE;
3695
3696     case PSM_SETHEADERTITLEA:
3697         PROPSHEET_SetHeaderTitleA(hwnd, (int)wParam, (LPCSTR)lParam);
3698         return TRUE;
3699
3700     case PSM_SETHEADERSUBTITLEW:
3701         PROPSHEET_SetHeaderSubTitleW(hwnd, (int)wParam, (LPCWSTR)lParam);
3702         return TRUE;
3703
3704     case PSM_SETHEADERSUBTITLEA:
3705         PROPSHEET_SetHeaderSubTitleA(hwnd, (int)wParam, (LPCSTR)lParam);
3706         return TRUE;
3707
3708     case PSM_HWNDTOINDEX:
3709     {
3710         LRESULT msgResult = PROPSHEET_HwndToIndex(hwnd, (HWND)wParam);
3711         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3712         return TRUE;
3713     }
3714
3715     case PSM_INDEXTOHWND:
3716     {
3717         LRESULT msgResult = PROPSHEET_IndexToHwnd(hwnd, (int)wParam);
3718         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3719         return TRUE;
3720     }
3721
3722     case PSM_PAGETOINDEX:
3723     {
3724         LRESULT msgResult = PROPSHEET_PageToIndex(hwnd, (HPROPSHEETPAGE)wParam);
3725         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3726         return TRUE;
3727     }
3728
3729     case PSM_INDEXTOPAGE:
3730     {
3731         LRESULT msgResult = PROPSHEET_IndexToPage(hwnd, (int)wParam);
3732         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3733         return TRUE;
3734     }
3735
3736     case PSM_IDTOINDEX:
3737     {
3738         LRESULT msgResult = PROPSHEET_IdToIndex(hwnd, (int)lParam);
3739         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3740         return TRUE;
3741     }
3742
3743     case PSM_INDEXTOID:
3744     {
3745         LRESULT msgResult = PROPSHEET_IndexToId(hwnd, (int)wParam);
3746         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3747         return TRUE;
3748     }
3749
3750     case PSM_GETRESULT:
3751     {
3752         LRESULT msgResult = PROPSHEET_GetResult(hwnd);
3753         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3754         return TRUE;
3755     }
3756
3757     case PSM_RECALCPAGESIZES:
3758     {
3759         LRESULT msgResult = PROPSHEET_RecalcPageSizes(hwnd);
3760         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3761         return TRUE;
3762     }
3763
3764     default:
3765       return FALSE;
3766   }
3767 }