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