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