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