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