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