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