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