Now that reference counting is correct, there is no need to check if
[wine] / dlls / comctl32 / propsheet.c
1 /*
2  * Property Sheets
3  *
4  * Copyright 1998 Francis Beaudet
5  * Copyright 1999 Thuy Nguyen
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  * TODO:
22  *   - Tab order
23  *   - Unicode property sheets
24  */
25
26 #include <string.h>
27
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "winbase.h"
31 #include "commctrl.h"
32 #include "prsht.h"
33 #include "winnls.h"
34 #include "comctl32.h"
35
36 #include "wine/debug.h"
37 #include "wine/unicode.h"
38
39 /******************************************************************************
40  * Data structures
41  */
42 #include "pshpack2.h"
43
44 typedef struct
45 {
46   WORD dlgVer;
47   WORD signature;
48   DWORD helpID;
49   DWORD exStyle;
50   DWORD style;
51 } MyDLGTEMPLATEEX;
52
53 typedef struct
54 {
55   DWORD helpid;
56   DWORD exStyle;
57   DWORD style;
58   short x;
59   short y;
60   short cx;
61   short cy;
62   DWORD id;
63 } MyDLGITEMTEMPLATEEX;
64 #include "poppack.h"
65
66 typedef struct tagPropPageInfo
67 {
68   HPROPSHEETPAGE hpage; /* to keep track of pages not passed to PropertySheet */
69   HWND hwndPage;
70   BOOL isDirty;
71   LPCWSTR pszText;
72   BOOL hasHelp;
73   BOOL useCallback;
74   BOOL hasIcon;
75 } PropPageInfo;
76
77 typedef struct tagPropSheetInfo
78 {
79   HWND hwnd;
80   PROPSHEETHEADERW ppshheader;
81   BOOL unicode;
82   LPWSTR strPropertiesFor;
83   int nPages;
84   int active_page;
85   BOOL isModeless;
86   BOOL hasHelp;
87   BOOL hasApply;
88   BOOL useCallback;
89   BOOL restartWindows;
90   BOOL rebootSystem;
91   BOOL activeValid;
92   PropPageInfo* proppage;
93   int x;
94   int y;
95   int width;
96   int height;
97   HIMAGELIST hImageList;
98 } PropSheetInfo;
99
100 typedef struct
101 {
102   int x;
103   int y;
104 } PADDING_INFO;
105
106 /******************************************************************************
107  * Defines and global variables
108  */
109
110 const WCHAR PropSheetInfoStr[] =
111     {'P','r','o','p','e','r','t','y','S','h','e','e','t','I','n','f','o',0 };
112
113 #define PSP_INTERNAL_UNICODE 0x80000000
114
115 #define MAX_CAPTION_LENGTH 255
116 #define MAX_TABTEXT_LENGTH 255
117 #define MAX_BUTTONTEXT_LENGTH 64
118
119 #define INTRNL_ANY_WIZARD (PSH_WIZARD | PSH_WIZARD97_OLD | PSH_WIZARD97_NEW | PSH_WIZARD_LITE)
120
121 /******************************************************************************
122  * Prototypes
123  */
124 static int PROPSHEET_CreateDialog(PropSheetInfo* psInfo);
125 static BOOL PROPSHEET_SizeMismatch(HWND hwndDlg, PropSheetInfo* psInfo);
126 static BOOL PROPSHEET_AdjustSize(HWND hwndDlg, PropSheetInfo* psInfo);
127 static BOOL PROPSHEET_AdjustButtons(HWND hwndParent, PropSheetInfo* psInfo);
128 static BOOL PROPSHEET_CollectSheetInfoA(LPCPROPSHEETHEADERA lppsh,
129                                        PropSheetInfo * psInfo);
130 static BOOL PROPSHEET_CollectSheetInfoW(LPCPROPSHEETHEADERW lppsh,
131                                        PropSheetInfo * psInfo);
132 static BOOL PROPSHEET_CollectPageInfo(LPCPROPSHEETPAGEW lppsp,
133                                       PropSheetInfo * psInfo,
134                                       int index);
135 static BOOL PROPSHEET_CreateTabControl(HWND hwndParent,
136                                        PropSheetInfo * psInfo);
137 static BOOL PROPSHEET_CreatePage(HWND hwndParent, int index,
138                                 const PropSheetInfo * psInfo,
139                                 LPCPROPSHEETPAGEW ppshpage);
140 static BOOL PROPSHEET_ShowPage(HWND hwndDlg, int index, PropSheetInfo * psInfo);
141 static PADDING_INFO PROPSHEET_GetPaddingInfo(HWND hwndDlg);
142 static BOOL PROPSHEET_Back(HWND hwndDlg);
143 static BOOL PROPSHEET_Next(HWND hwndDlg);
144 static BOOL PROPSHEET_Finish(HWND hwndDlg);
145 static BOOL PROPSHEET_Apply(HWND hwndDlg, LPARAM lParam);
146 static void PROPSHEET_Cancel(HWND hwndDlg, LPARAM lParam);
147 static void PROPSHEET_Help(HWND hwndDlg);
148 static void PROPSHEET_Changed(HWND hwndDlg, HWND hwndDirtyPage);
149 static void PROPSHEET_UnChanged(HWND hwndDlg, HWND hwndCleanPage);
150 static void PROPSHEET_PressButton(HWND hwndDlg, int buttonID);
151 static void PROPSHEET_SetFinishTextA(HWND hwndDlg, LPCSTR lpszText);
152 static void PROPSHEET_SetFinishTextW(HWND hwndDlg, LPCWSTR lpszText);
153 static void PROPSHEET_SetTitleA(HWND hwndDlg, DWORD dwStyle, LPCSTR lpszText);
154 static void PROPSHEET_SetTitleW(HWND hwndDlg, DWORD dwStyle, LPCWSTR lpszText);
155 static BOOL PROPSHEET_CanSetCurSel(HWND hwndDlg);
156 static BOOL PROPSHEET_SetCurSel(HWND hwndDlg,
157                                 int index,
158                                 int skipdir,
159                                 HPROPSHEETPAGE hpage);
160 static void PROPSHEET_SetCurSelId(HWND hwndDlg, int id);
161 static LRESULT PROPSHEET_QuerySiblings(HWND hwndDlg,
162                                        WPARAM wParam, LPARAM lParam);
163 static BOOL PROPSHEET_AddPage(HWND hwndDlg,
164                               HPROPSHEETPAGE hpage);
165
166 static BOOL PROPSHEET_RemovePage(HWND hwndDlg,
167                                  int index,
168                                  HPROPSHEETPAGE hpage);
169 static void PROPSHEET_CleanUp();
170 static int PROPSHEET_GetPageIndex(HPROPSHEETPAGE hpage, PropSheetInfo* psInfo);
171 static void PROPSHEET_SetWizButtons(HWND hwndDlg, DWORD dwFlags);
172 static PADDING_INFO PROPSHEET_GetPaddingInfoWizard(HWND hwndDlg, const PropSheetInfo* psInfo);
173 static BOOL PROPSHEET_IsDialogMessage(HWND hwnd, LPMSG lpMsg);
174 static BOOL PROPSHEET_DoCommand(HWND hwnd, WORD wID);
175
176 INT_PTR CALLBACK
177 PROPSHEET_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
178
179 WINE_DEFAULT_DEBUG_CHANNEL(propsheet);
180
181 #define add_flag(a) if (dwFlags & a) {strcat(string, #a );strcat(string," ");}
182 /******************************************************************************
183  *            PROPSHEET_UnImplementedFlags
184  *
185  * Document use of flags we don't implement yet.
186  */
187 static VOID PROPSHEET_UnImplementedFlags(DWORD dwFlags)
188 {
189     CHAR string[256];
190
191     string[0] = '\0';
192
193   /*
194    * unhandled header flags:
195    *  PSH_DEFAULT            0x00000000
196    *  PSH_WIZARDHASFINISH    0x00000010
197    *  PSH_RTLREADING         0x00000800
198    *  PSH_WIZARDCONTEXTHELP  0x00001000
199    *  PSH_WIZARD97           0x00002000  (pre IE 5)
200    *  PSH_WATERMARK          0x00008000
201    *  PSH_USEHBMWATERMARK    0x00010000
202    *  PSH_USEHPLWATERMARK    0x00020000
203    *  PSH_STRETCHWATERMARK   0x00040000
204    *  PSH_HEADER             0x00080000
205    *  PSH_USEHBMHEADER       0x00100000
206    *  PSH_USEPAGELANG        0x00200000
207    *  PSH_WIZARD_LITE        0x00400000      also not in .h
208    *  PSH_WIZARD97           0x01000000  (IE 5 and above)
209    *  PSH_NOCONTEXTHELP      0x02000000      also not in .h
210    */
211
212     add_flag(PSH_WIZARDHASFINISH);
213     add_flag(PSH_RTLREADING);
214     add_flag(PSH_WIZARDCONTEXTHELP);
215     add_flag(PSH_WIZARD97_OLD);
216     add_flag(PSH_WATERMARK);
217     add_flag(PSH_USEHBMWATERMARK);
218     add_flag(PSH_USEHPLWATERMARK);
219     add_flag(PSH_STRETCHWATERMARK);
220     add_flag(PSH_HEADER);
221     add_flag(PSH_USEHBMHEADER);
222     add_flag(PSH_USEPAGELANG);
223     add_flag(PSH_WIZARD_LITE);
224     add_flag(PSH_WIZARD97_NEW);
225     add_flag(PSH_NOCONTEXTHELP);
226     if (string[0] != '\0')
227         FIXME("%s\n", string);
228 }
229 #undef add_flag
230
231 /******************************************************************************
232  *            PROPSHEET_GetPageRect
233  *
234  * Retrieve rect from tab control and map into the dialog for SetWindowPos
235  */
236 static void PROPSHEET_GetPageRect(const PropSheetInfo * psInfo, HWND hwndDlg, RECT *rc)
237 {
238     HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
239
240     GetClientRect(hwndTabCtrl, rc);
241     SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, FALSE, (LPARAM)rc);
242     MapWindowPoints(hwndTabCtrl, hwndDlg, (LPPOINT)rc, 2);
243 }
244
245 /******************************************************************************
246  *            PROPSHEET_FindPageByResId
247  *
248  * Find page index corresponding to page resource id.
249  */
250 INT PROPSHEET_FindPageByResId(PropSheetInfo * psInfo, LRESULT resId)
251 {
252    INT i;
253
254    for (i = 0; i < psInfo->nPages; i++)
255    {
256       LPCPROPSHEETPAGEA lppsp = (LPCPROPSHEETPAGEA)psInfo->proppage[i].hpage;
257
258       /* Fixme: if resource ID is a string shall we use strcmp ??? */
259       if (lppsp->u.pszTemplate == (LPVOID)resId)
260          break;
261    }
262
263    return i;
264 }
265
266 /******************************************************************************
267  *            PROPSHEET_AtoW
268  *
269  * Convert ASCII to Unicode since all data is saved as Unicode.
270  */
271 static void PROPSHEET_AtoW(LPCWSTR *tostr, LPCSTR frstr)
272 {
273     INT len;
274
275     TRACE("<%s>\n", frstr);
276     len = MultiByteToWideChar(CP_ACP, 0, frstr, -1, 0, 0);
277     *tostr = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
278     MultiByteToWideChar(CP_ACP, 0, frstr, -1, (LPWSTR)*tostr, len);
279 }
280
281 /******************************************************************************
282  *            PROPSHEET_CollectSheetInfoA
283  *
284  * Collect relevant data.
285  */
286 static BOOL PROPSHEET_CollectSheetInfoA(LPCPROPSHEETHEADERA lppsh,
287                                        PropSheetInfo * psInfo)
288 {
289   DWORD dwSize = min(lppsh->dwSize,sizeof(PROPSHEETHEADERA));
290   DWORD dwFlags = lppsh->dwFlags;
291
292   psInfo->hasHelp = dwFlags & PSH_HASHELP;
293   psInfo->hasApply = !(dwFlags & PSH_NOAPPLYNOW);
294   psInfo->useCallback = (dwFlags & PSH_USECALLBACK )&& (lppsh->pfnCallback);
295   psInfo->isModeless = dwFlags & PSH_MODELESS;
296
297   memcpy(&psInfo->ppshheader,lppsh,dwSize);
298   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",
299         lppsh->dwSize, lppsh->dwFlags, lppsh->hwndParent, lppsh->hInstance,
300         debugstr_a(lppsh->pszCaption), lppsh->nPages, lppsh->pfnCallback);
301
302   PROPSHEET_UnImplementedFlags(lppsh->dwFlags);
303
304   if (HIWORD(lppsh->pszCaption))
305   {
306      int len = strlen(lppsh->pszCaption);
307      psInfo->ppshheader.pszCaption = HeapAlloc( GetProcessHeap(), 0, (len+1)*sizeof (WCHAR) );
308      MultiByteToWideChar(CP_ACP, 0, lppsh->pszCaption, -1, (LPWSTR) psInfo->ppshheader.pszCaption, len+1);
309      /* strcpy( (char *)psInfo->ppshheader.pszCaption, lppsh->pszCaption ); */
310   }
311   psInfo->nPages = lppsh->nPages;
312
313   if (dwFlags & PSH_USEPSTARTPAGE)
314   {
315     TRACE("PSH_USEPSTARTPAGE is on");
316     psInfo->active_page = 0;
317   }
318   else
319     psInfo->active_page = lppsh->u2.nStartPage;
320
321   if (psInfo->active_page < 0 || psInfo->active_page >= psInfo->nPages)
322      psInfo->active_page = 0;
323
324   psInfo->restartWindows = FALSE;
325   psInfo->rebootSystem = FALSE;
326   psInfo->hImageList = 0;
327   psInfo->activeValid = FALSE;
328
329   return TRUE;
330 }
331
332 /******************************************************************************
333  *            PROPSHEET_CollectSheetInfoW
334  *
335  * Collect relevant data.
336  */
337 static BOOL PROPSHEET_CollectSheetInfoW(LPCPROPSHEETHEADERW lppsh,
338                                        PropSheetInfo * psInfo)
339 {
340   DWORD dwSize = min(lppsh->dwSize,sizeof(PROPSHEETHEADERW));
341   DWORD dwFlags = lppsh->dwFlags;
342
343   psInfo->hasHelp = dwFlags & PSH_HASHELP;
344   psInfo->hasApply = !(dwFlags & PSH_NOAPPLYNOW);
345   psInfo->useCallback = (dwFlags & PSH_USECALLBACK) && (lppsh->pfnCallback);
346   psInfo->isModeless = dwFlags & PSH_MODELESS;
347
348   memcpy(&psInfo->ppshheader,lppsh,dwSize);
349   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",
350       lppsh->dwSize, lppsh->dwFlags, lppsh->hwndParent, lppsh->hInstance, debugstr_w(lppsh->pszCaption), lppsh->nPages, lppsh->pfnCallback);
351
352   PROPSHEET_UnImplementedFlags(lppsh->dwFlags);
353
354   if (HIWORD(lppsh->pszCaption))
355   {
356      int len = strlenW(lppsh->pszCaption);
357      psInfo->ppshheader.pszCaption = HeapAlloc( GetProcessHeap(), 0, (len+1)*sizeof(WCHAR) );
358      strcpyW( (WCHAR *)psInfo->ppshheader.pszCaption, lppsh->pszCaption );
359   }
360   psInfo->nPages = lppsh->nPages;
361
362   if (dwFlags & PSH_USEPSTARTPAGE)
363   {
364     TRACE("PSH_USEPSTARTPAGE is on");
365     psInfo->active_page = 0;
366   }
367   else
368     psInfo->active_page = lppsh->u2.nStartPage;
369
370   if (psInfo->active_page < 0 || psInfo->active_page >= psInfo->nPages)
371      psInfo->active_page = 0;
372
373   psInfo->restartWindows = FALSE;
374   psInfo->rebootSystem = FALSE;
375   psInfo->hImageList = 0;
376   psInfo->activeValid = FALSE;
377
378   return TRUE;
379 }
380
381 /******************************************************************************
382  *            PROPSHEET_CollectPageInfo
383  *
384  * Collect property sheet data.
385  * With code taken from DIALOG_ParseTemplate32.
386  */
387 BOOL PROPSHEET_CollectPageInfo(LPCPROPSHEETPAGEW lppsp,
388                                PropSheetInfo * psInfo,
389                                int index)
390 {
391   DLGTEMPLATE* pTemplate;
392   const WORD*  p;
393   DWORD dwFlags;
394   int width, height;
395
396   TRACE("\n");
397   psInfo->proppage[index].hpage = (HPROPSHEETPAGE)lppsp;
398   psInfo->proppage[index].hwndPage = 0;
399   psInfo->proppage[index].isDirty = FALSE;
400
401   /*
402    * Process property page flags.
403    */
404   dwFlags = lppsp->dwFlags;
405   psInfo->proppage[index].useCallback = (dwFlags & PSP_USECALLBACK) && (lppsp->pfnCallback);
406   psInfo->proppage[index].hasHelp = dwFlags & PSP_HASHELP;
407   psInfo->proppage[index].hasIcon = dwFlags & (PSP_USEHICON | PSP_USEICONID);
408
409   /* as soon as we have a page with the help flag, set the sheet flag on */
410   if (psInfo->proppage[index].hasHelp)
411     psInfo->hasHelp = TRUE;
412
413   /*
414    * Process page template.
415    */
416   if (dwFlags & PSP_DLGINDIRECT)
417     pTemplate = (DLGTEMPLATE*)lppsp->u.pResource;
418   else if(dwFlags & PSP_INTERNAL_UNICODE )
419   {
420     HRSRC hResource = FindResourceW(lppsp->hInstance,
421                                     lppsp->u.pszTemplate,
422                                     RT_DIALOGW);
423     HGLOBAL hTemplate = LoadResource(lppsp->hInstance,
424                                      hResource);
425     pTemplate = (LPDLGTEMPLATEW)LockResource(hTemplate);
426   }
427   else
428   {
429     HRSRC hResource = FindResourceA(lppsp->hInstance,
430                                     (LPSTR)lppsp->u.pszTemplate,
431                                     RT_DIALOGA);
432     HGLOBAL hTemplate = LoadResource(lppsp->hInstance,
433                                      hResource);
434     pTemplate = (LPDLGTEMPLATEA)LockResource(hTemplate);
435   }
436
437   /*
438    * Extract the size of the page and the caption.
439    */
440   if (!pTemplate)
441       return FALSE;
442
443   p = (const WORD *)pTemplate;
444
445   if (((MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF)
446   {
447     /* DLGTEMPLATEEX (not defined in any std. header file) */
448
449     p++;       /* dlgVer    */
450     p++;       /* signature */
451     p += 2;    /* help ID   */
452     p += 2;    /* ext style */
453     p += 2;    /* style     */
454   }
455   else
456   {
457     /* DLGTEMPLATE */
458
459     p += 2;    /* style     */
460     p += 2;    /* ext style */
461   }
462
463   p++;    /* nb items */
464   p++;    /*   x      */
465   p++;    /*   y      */
466   width  = (WORD)*p; p++;
467   height = (WORD)*p; p++;
468
469   /* remember the largest width and height */
470   if (width > psInfo->width)
471     psInfo->width = width;
472
473   if (height > psInfo->height)
474     psInfo->height = height;
475
476   /* menu */
477   switch ((WORD)*p)
478   {
479     case 0x0000:
480       p++;
481       break;
482     case 0xffff:
483       p += 2;
484       break;
485     default:
486       p += lstrlenW( (LPCWSTR)p ) + 1;
487       break;
488   }
489
490   /* class */
491   switch ((WORD)*p)
492   {
493     case 0x0000:
494       p++;
495       break;
496     case 0xffff:
497       p += 2;
498       break;
499     default:
500       p += lstrlenW( (LPCWSTR)p ) + 1;
501       break;
502   }
503
504   /* Extract the caption */
505   psInfo->proppage[index].pszText = (LPCWSTR)p;
506   TRACE("Tab %d %s\n",index,debugstr_w((LPCWSTR)p));
507   p += lstrlenW((LPCWSTR)p) + 1;
508
509   if (dwFlags & PSP_USETITLE)
510   {
511     WCHAR szTitle[256];
512     const WCHAR *pTitle;
513     static WCHAR pszNull[] = { '(','n','u','l','l',')',0 };
514     int len;
515
516     if ( !HIWORD( lppsp->pszTitle ) )
517     {
518       if (!LoadStringW( lppsp->hInstance, (UINT)lppsp->pszTitle,szTitle,sizeof(szTitle) ))
519       {
520         pTitle = pszNull;
521         FIXME("Could not load resource #%04x?\n",LOWORD(lppsp->pszTitle));
522       }
523       else
524         pTitle = szTitle;
525     }
526     else
527       pTitle = lppsp->pszTitle;
528
529     len = strlenW(pTitle);
530     psInfo->proppage[index].pszText = COMCTL32_Alloc( (len+1)*sizeof (WCHAR) );
531     strcpyW( (LPWSTR)psInfo->proppage[index].pszText,pTitle);
532   }
533
534   /*
535    * Build the image list for icons
536    */
537   if ((dwFlags & PSP_USEHICON) || (dwFlags & PSP_USEICONID))
538   {
539     HICON hIcon;
540     int icon_cx = GetSystemMetrics(SM_CXSMICON);
541     int icon_cy = GetSystemMetrics(SM_CYSMICON);
542
543     if (dwFlags & PSP_USEICONID)
544       hIcon = LoadImageW(lppsp->hInstance, lppsp->u2.pszIcon, IMAGE_ICON,
545                          icon_cx, icon_cy, LR_DEFAULTCOLOR);
546     else
547       hIcon = lppsp->u2.hIcon;
548
549     if ( hIcon )
550     {
551       if (psInfo->hImageList == 0 )
552         psInfo->hImageList = ImageList_Create(icon_cx, icon_cy, ILC_COLOR, 1, 1);
553
554       ImageList_AddIcon(psInfo->hImageList, hIcon);
555     }
556
557   }
558
559   return TRUE;
560 }
561
562 /******************************************************************************
563  *            PROPSHEET_CreateDialog
564  *
565  * Creates the actual property sheet.
566  */
567 int PROPSHEET_CreateDialog(PropSheetInfo* psInfo)
568 {
569   LRESULT ret;
570   LPCVOID template;
571   LPVOID temp = 0;
572   HRSRC hRes;
573   DWORD resSize;
574   WORD resID = IDD_PROPSHEET;
575
576   TRACE("\n");
577   if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
578     resID = IDD_WIZARD;
579
580   if( psInfo->unicode )
581   {
582     if(!(hRes = FindResourceW(COMCTL32_hModule,
583                             MAKEINTRESOURCEW(resID),
584                             RT_DIALOGW)))
585       return -1;
586   }
587   else
588   {
589     if(!(hRes = FindResourceA(COMCTL32_hModule,
590                             MAKEINTRESOURCEA(resID),
591                             RT_DIALOGA)))
592       return -1;
593   }
594
595   if(!(template = (LPVOID)LoadResource(COMCTL32_hModule, hRes)))
596     return -1;
597
598   /*
599    * Make a copy of the dialog template.
600    */
601   resSize = SizeofResource(COMCTL32_hModule, hRes);
602
603   temp = COMCTL32_Alloc(resSize);
604
605   if (!temp)
606     return -1;
607
608   memcpy(temp, template, resSize);
609
610   if (psInfo->useCallback)
611     (*(psInfo->ppshheader.pfnCallback))(0, PSCB_PRECREATE, (LPARAM)temp);
612
613   if( psInfo->unicode )
614   {
615     if (!(psInfo->ppshheader.dwFlags & PSH_MODELESS))
616       ret = DialogBoxIndirectParamW(psInfo->ppshheader.hInstance,
617                                     (LPDLGTEMPLATEW) temp,
618                                     psInfo->ppshheader.hwndParent,
619                                     PROPSHEET_DialogProc,
620                                     (LPARAM)psInfo);
621     else
622     {
623       ret = (int)CreateDialogIndirectParamW(psInfo->ppshheader.hInstance,
624                                             (LPDLGTEMPLATEW) temp,
625                                             psInfo->ppshheader.hwndParent,
626                                             PROPSHEET_DialogProc,
627                                             (LPARAM)psInfo);
628       if ( !ret ) ret = -1;
629     }
630   }
631   else
632   {
633     if (!(psInfo->ppshheader.dwFlags & PSH_MODELESS))
634       ret = DialogBoxIndirectParamA(psInfo->ppshheader.hInstance,
635                                     (LPDLGTEMPLATEA) temp,
636                                     psInfo->ppshheader.hwndParent,
637                                     PROPSHEET_DialogProc,
638                                     (LPARAM)psInfo);
639     else
640     {
641       ret = (int)CreateDialogIndirectParamA(psInfo->ppshheader.hInstance,
642                                             (LPDLGTEMPLATEA) temp,
643                                             psInfo->ppshheader.hwndParent,
644                                             PROPSHEET_DialogProc,
645                                             (LPARAM)psInfo);
646       if ( !ret ) ret = -1;
647     }
648   }
649
650   COMCTL32_Free(temp);
651
652   return ret;
653 }
654
655 /******************************************************************************
656  *            PROPSHEET_SizeMismatch
657  *
658  *     Verify that the tab control and the "largest" property sheet page dlg. template
659  *     match in size.
660  */
661 static BOOL PROPSHEET_SizeMismatch(HWND hwndDlg, PropSheetInfo* psInfo)
662 {
663   HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
664   RECT rcOrigTab, rcPage;
665
666   /*
667    * Original tab size.
668    */
669   GetClientRect(hwndTabCtrl, &rcOrigTab);
670   TRACE("orig tab %ld %ld %ld %ld\n", rcOrigTab.left, rcOrigTab.top,
671         rcOrigTab.right, rcOrigTab.bottom);
672
673   /*
674    * Biggest page size.
675    */
676   rcPage.left   = psInfo->x;
677   rcPage.top    = psInfo->y;
678   rcPage.right  = psInfo->width;
679   rcPage.bottom = psInfo->height;
680
681   MapDialogRect(hwndDlg, &rcPage);
682   TRACE("biggest page %ld %ld %ld %ld\n", rcPage.left, rcPage.top,
683         rcPage.right, rcPage.bottom);
684
685   if ( (rcPage.right - rcPage.left) != (rcOrigTab.right - rcOrigTab.left) )
686     return TRUE;
687   if ( (rcPage.bottom - rcPage.top) != (rcOrigTab.bottom - rcOrigTab.top) )
688     return TRUE;
689
690   return FALSE;
691 }
692
693 /******************************************************************************
694  *            PROPSHEET_IsTooSmallWizard
695  *
696  * Verify that the default property sheet is big enough.
697  */
698 static BOOL PROPSHEET_IsTooSmallWizard(HWND hwndDlg, PropSheetInfo* psInfo)
699 {
700   RECT rcSheetRect, rcPage, rcLine, rcSheetClient;
701   HWND hwndLine = GetDlgItem(hwndDlg, IDC_SUNKEN_LINE);
702   PADDING_INFO padding = PROPSHEET_GetPaddingInfoWizard(hwndDlg, psInfo);
703
704   GetClientRect(hwndDlg, &rcSheetClient);
705   GetWindowRect(hwndDlg, &rcSheetRect);
706   GetWindowRect(hwndLine, &rcLine);
707
708   /* Remove the space below the sunken line */
709   rcSheetClient.bottom -= (rcSheetRect.bottom - rcLine.top);
710
711   /* Remove the buffer zone all around the edge */
712   rcSheetClient.bottom -= (padding.y * 2);
713   rcSheetClient.right -= (padding.x * 2);
714
715   /*
716    * Biggest page size.
717    */
718   rcPage.left   = psInfo->x;
719   rcPage.top    = psInfo->y;
720   rcPage.right  = psInfo->width;
721   rcPage.bottom = psInfo->height;
722
723   MapDialogRect(hwndDlg, &rcPage);
724   TRACE("biggest page %ld %ld %ld %ld\n", rcPage.left, rcPage.top,
725         rcPage.right, rcPage.bottom);
726
727   if (rcPage.right > rcSheetClient.right)
728     return TRUE;
729
730   if (rcPage.bottom > rcSheetClient.bottom)
731     return TRUE;
732
733   return FALSE;
734 }
735
736 /******************************************************************************
737  *            PROPSHEET_AdjustSize
738  *
739  * Resizes the property sheet and the tab control to fit the largest page.
740  */
741 static BOOL PROPSHEET_AdjustSize(HWND hwndDlg, PropSheetInfo* psInfo)
742 {
743   HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
744   HWND hwndButton = GetDlgItem(hwndDlg, IDOK);
745   RECT rc,tabRect;
746   int tabOffsetX, tabOffsetY, buttonHeight;
747   PADDING_INFO padding = PROPSHEET_GetPaddingInfo(hwndDlg);
748   RECT units;
749
750   /* Get the height of buttons */
751   GetClientRect(hwndButton, &rc);
752   buttonHeight = rc.bottom;
753
754   /*
755    * Biggest page size.
756    */
757   rc.left   = psInfo->x;
758   rc.top    = psInfo->y;
759   rc.right  = psInfo->width;
760   rc.bottom = psInfo->height;
761
762   MapDialogRect(hwndDlg, &rc);
763
764   /* retrieve the dialog units */
765   units.left = units.right = 4;
766   units.top = units.bottom = 8;
767   MapDialogRect(hwndDlg, &units);
768
769   /*
770    * Resize the tab control.
771    */
772   GetClientRect(hwndTabCtrl,&tabRect);
773
774   SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, FALSE, (LPARAM)&tabRect);
775
776   if ((rc.bottom - rc.top) < (tabRect.bottom - tabRect.top))
777   {
778       rc.bottom = rc.top + tabRect.bottom - tabRect.top;
779       psInfo->height = MulDiv((rc.bottom - rc.top),8,units.top);
780   }
781
782   if ((rc.right - rc.left) < (tabRect.right - tabRect.left))
783   {
784       rc.right = rc.left + tabRect.right - tabRect.left;
785       psInfo->width  = MulDiv((rc.right - rc.left),4,units.left);
786   }
787
788   SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, TRUE, (LPARAM)&rc);
789
790   tabOffsetX = -(rc.left);
791   tabOffsetY = -(rc.top);
792
793   rc.right -= rc.left;
794   rc.bottom -= rc.top;
795   TRACE("setting tab %08lx, rc (0,0)-(%ld,%ld)\n",
796         (DWORD)hwndTabCtrl, rc.right, rc.bottom);
797   SetWindowPos(hwndTabCtrl, 0, 0, 0, rc.right, rc.bottom,
798                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
799
800   GetClientRect(hwndTabCtrl, &rc);
801
802   TRACE("tab client rc %ld %ld %ld %ld\n",
803         rc.left, rc.top, rc.right, rc.bottom);
804
805   rc.right += ((padding.x * 2) + tabOffsetX);
806   rc.bottom += (buttonHeight + (3 * padding.y) + tabOffsetY);
807
808   /*
809    * Resize the property sheet.
810    */
811   TRACE("setting dialog %08lx, rc (0,0)-(%ld,%ld)\n",
812         (DWORD)hwndDlg, rc.right, rc.bottom);
813   SetWindowPos(hwndDlg, 0, 0, 0, rc.right, rc.bottom,
814                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
815   return TRUE;
816 }
817
818 /******************************************************************************
819  *            PROPSHEET_AdjustSizeWizard
820  *
821  * Resizes the property sheet to fit the largest page.
822  */
823 static BOOL PROPSHEET_AdjustSizeWizard(HWND hwndDlg, PropSheetInfo* psInfo)
824 {
825   HWND hwndButton = GetDlgItem(hwndDlg, IDCANCEL);
826   HWND hwndLine = GetDlgItem(hwndDlg, IDC_SUNKEN_LINE);
827   HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
828   RECT rc,tabRect;
829   int buttonHeight, lineHeight;
830   PADDING_INFO padding = PROPSHEET_GetPaddingInfoWizard(hwndDlg, psInfo);
831   RECT units;
832
833   /* Get the height of buttons */
834   GetClientRect(hwndButton, &rc);
835   buttonHeight = rc.bottom;
836
837   GetClientRect(hwndLine, &rc);
838   lineHeight = rc.bottom;
839
840   /* retrieve the dialog units */
841   units.left = units.right = 4;
842   units.top = units.bottom = 8;
843   MapDialogRect(hwndDlg, &units);
844
845   /*
846    * Biggest page size.
847    */
848   rc.left   = psInfo->x;
849   rc.top    = psInfo->y;
850   rc.right  = psInfo->width;
851   rc.bottom = psInfo->height;
852
853   MapDialogRect(hwndDlg, &rc);
854
855   GetClientRect(hwndTabCtrl,&tabRect);
856
857   if ((rc.bottom - rc.top) < (tabRect.bottom - tabRect.top))
858   {
859       rc.bottom = rc.top + tabRect.bottom - tabRect.top;
860       psInfo->height = MulDiv((rc.bottom - rc.top), 8, units.top);
861   }
862
863   if ((rc.right - rc.left) < (tabRect.right - tabRect.left))
864   {
865       rc.right = rc.left + tabRect.right - tabRect.left;
866       psInfo->width  = MulDiv((rc.right - rc.left), 4, units.left);
867   }
868
869   TRACE("Biggest page %ld %ld %ld %ld\n", rc.left, rc.top, rc.right, rc.bottom);
870   TRACE("   constants padx=%d, pady=%d, butH=%d, lH=%d\n",
871         padding.x, padding.y, buttonHeight, lineHeight);
872
873   /* Make room */
874   rc.right += (padding.x * 2);
875   rc.bottom += (buttonHeight + (5 * padding.y) + lineHeight);
876
877   /*
878    * Resize the property sheet.
879    */
880   TRACE("setting dialog %08lx, rc (0,0)-(%ld,%ld)\n",
881         (DWORD)hwndDlg, rc.right, rc.bottom);
882   SetWindowPos(hwndDlg, 0, 0, 0, rc.right, rc.bottom,
883                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
884   return TRUE;
885 }
886
887 /******************************************************************************
888  *            PROPSHEET_AdjustButtons
889  *
890  * Adjusts the buttons' positions.
891  */
892 static BOOL PROPSHEET_AdjustButtons(HWND hwndParent, PropSheetInfo* psInfo)
893 {
894   HWND hwndButton = GetDlgItem(hwndParent, IDOK);
895   RECT rcSheet;
896   int x, y;
897   int num_buttons = 2;
898   int buttonWidth, buttonHeight;
899   PADDING_INFO padding = PROPSHEET_GetPaddingInfo(hwndParent);
900
901   if (psInfo->hasApply)
902     num_buttons++;
903
904   if (psInfo->hasHelp)
905     num_buttons++;
906
907   /*
908    * Obtain the size of the buttons.
909    */
910   GetClientRect(hwndButton, &rcSheet);
911   buttonWidth = rcSheet.right;
912   buttonHeight = rcSheet.bottom;
913
914   /*
915    * Get the size of the property sheet.
916    */
917   GetClientRect(hwndParent, &rcSheet);
918
919   /*
920    * All buttons will be at this y coordinate.
921    */
922   y = rcSheet.bottom - (padding.y + buttonHeight);
923
924   /*
925    * Position OK button.
926    */
927   hwndButton = GetDlgItem(hwndParent, IDOK);
928
929   x = rcSheet.right - ((padding.x + buttonWidth) * num_buttons);
930
931   SetWindowPos(hwndButton, 0, x, y, 0, 0,
932                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
933
934   /*
935    * Position Cancel button.
936    */
937   hwndButton = GetDlgItem(hwndParent, IDCANCEL);
938
939   x = rcSheet.right - ((padding.x + buttonWidth) * (num_buttons - 1));
940
941   SetWindowPos(hwndButton, 0, x, y, 0, 0,
942                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
943
944   /*
945    * Position Apply button.
946    */
947   hwndButton = GetDlgItem(hwndParent, IDC_APPLY_BUTTON);
948
949   if (psInfo->hasApply)
950   {
951     if (psInfo->hasHelp)
952       x = rcSheet.right - ((padding.x + buttonWidth) * 2);
953     else
954       x = rcSheet.right - (padding.x + buttonWidth);
955
956     SetWindowPos(hwndButton, 0, x, y, 0, 0,
957                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
958
959     EnableWindow(hwndButton, FALSE);
960   }
961   else
962     ShowWindow(hwndButton, SW_HIDE);
963
964   /*
965    * Position Help button.
966    */
967   hwndButton = GetDlgItem(hwndParent, IDHELP);
968
969   if (psInfo->hasHelp)
970   {
971     x = rcSheet.right - (padding.x + buttonWidth);
972
973     SetWindowPos(hwndButton, 0, x, y, 0, 0,
974                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
975   }
976   else
977     ShowWindow(hwndButton, SW_HIDE);
978
979   return TRUE;
980 }
981
982 /******************************************************************************
983  *            PROPSHEET_AdjustButtonsWizard
984  *
985  * Adjusts the buttons' positions.
986  */
987 static BOOL PROPSHEET_AdjustButtonsWizard(HWND hwndParent,
988                                           PropSheetInfo* psInfo)
989 {
990   HWND hwndButton = GetDlgItem(hwndParent, IDCANCEL);
991   HWND hwndLine = GetDlgItem(hwndParent, IDC_SUNKEN_LINE);
992   RECT rcSheet;
993   int x, y;
994   int num_buttons = 3;
995   int buttonWidth, buttonHeight, lineHeight, lineWidth;
996   PADDING_INFO padding = PROPSHEET_GetPaddingInfoWizard(hwndParent, psInfo);
997
998   if (psInfo->hasHelp)
999     num_buttons++;
1000
1001   /*
1002    * Obtain the size of the buttons.
1003    */
1004   GetClientRect(hwndButton, &rcSheet);
1005   buttonWidth = rcSheet.right;
1006   buttonHeight = rcSheet.bottom;
1007
1008   GetClientRect(hwndLine, &rcSheet);
1009   lineHeight = rcSheet.bottom;
1010
1011   /*
1012    * Get the size of the property sheet.
1013    */
1014   GetClientRect(hwndParent, &rcSheet);
1015
1016   /*
1017    * All buttons will be at this y coordinate.
1018    */
1019   y = rcSheet.bottom - (padding.y + buttonHeight);
1020
1021   /*
1022    * Position the Next and the Finish buttons.
1023    */
1024   hwndButton = GetDlgItem(hwndParent, IDC_NEXT_BUTTON);
1025
1026   x = rcSheet.right - ((padding.x + buttonWidth) * (num_buttons - 1));
1027
1028   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1029                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1030
1031   hwndButton = GetDlgItem(hwndParent, IDC_FINISH_BUTTON);
1032
1033   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1034                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1035
1036   ShowWindow(hwndButton, SW_HIDE);
1037
1038   /*
1039    * Position the Back button.
1040    */
1041   hwndButton = GetDlgItem(hwndParent, IDC_BACK_BUTTON);
1042
1043   x -= buttonWidth;
1044
1045   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1046                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1047
1048   /*
1049    * Position the Cancel button.
1050    */
1051   hwndButton = GetDlgItem(hwndParent, IDCANCEL);
1052
1053   x = rcSheet.right - ((padding.x + buttonWidth) * (num_buttons - 2));
1054
1055   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1056                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1057
1058   /*
1059    * Position Help button.
1060    */
1061   hwndButton = GetDlgItem(hwndParent, IDHELP);
1062
1063   if (psInfo->hasHelp)
1064   {
1065     x = rcSheet.right - (padding.x + buttonWidth);
1066
1067     SetWindowPos(hwndButton, 0, x, y, 0, 0,
1068                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1069   }
1070   else
1071     ShowWindow(hwndButton, SW_HIDE);
1072
1073   /*
1074    * Position and resize the sunken line.
1075    */
1076   x = padding.x;
1077   y = rcSheet.bottom - ((padding.y * 2) + buttonHeight + lineHeight);
1078
1079   GetClientRect(hwndParent, &rcSheet);
1080   lineWidth = rcSheet.right - (padding.x * 2);
1081
1082   SetWindowPos(hwndLine, 0, x, y, lineWidth, 2,
1083                SWP_NOZORDER | SWP_NOACTIVATE);
1084
1085   return TRUE;
1086 }
1087
1088 /******************************************************************************
1089  *            PROPSHEET_GetPaddingInfo
1090  *
1091  * Returns the layout information.
1092  */
1093 static PADDING_INFO PROPSHEET_GetPaddingInfo(HWND hwndDlg)
1094 {
1095   HWND hwndTab = GetDlgItem(hwndDlg, IDC_TABCONTROL);
1096   RECT rcTab;
1097   POINT tl;
1098   PADDING_INFO padding;
1099
1100   GetWindowRect(hwndTab, &rcTab);
1101
1102   tl.x = rcTab.left;
1103   tl.y = rcTab.top;
1104
1105   ScreenToClient(hwndDlg, &tl);
1106
1107   padding.x = tl.x;
1108   padding.y = tl.y;
1109
1110   return padding;
1111 }
1112
1113 /******************************************************************************
1114  *            PROPSHEET_GetPaddingInfoWizard
1115  *
1116  * Returns the layout information.
1117  * Vertical spacing is the distance between the line and the buttons.
1118  * Do NOT use the Help button to gather padding information when it isn't mapped
1119  * (PSH_HASHELP), as app writers aren't forced to supply correct coordinates
1120  * for it in this case !
1121  * FIXME: I'm not sure about any other coordinate problems with these evil
1122  * buttons. Fix it in case additional problems appear or maybe calculate
1123  * a padding in a completely different way, as this is somewhat messy.
1124  */
1125 static PADDING_INFO PROPSHEET_GetPaddingInfoWizard(HWND hwndDlg, const PropSheetInfo*
1126  psInfo)
1127 {
1128   PADDING_INFO padding;
1129   RECT rc;
1130   HWND hwndControl;
1131   INT idButton;
1132   POINT ptButton, ptLine;
1133
1134   TRACE("\n");
1135   if (psInfo->hasHelp)
1136   {
1137         idButton = IDHELP;
1138   }
1139   else
1140   {
1141     if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
1142     {
1143         idButton = IDC_NEXT_BUTTON;
1144     }
1145     else
1146     {
1147         /* hopefully this is ok */
1148         idButton = IDCANCEL;
1149     }
1150   }
1151
1152   hwndControl = GetDlgItem(hwndDlg, idButton);
1153   GetWindowRect(hwndControl, &rc);
1154
1155   ptButton.x = rc.left;
1156   ptButton.y = rc.top;
1157
1158   ScreenToClient(hwndDlg, &ptButton);
1159
1160   /* Line */
1161   hwndControl = GetDlgItem(hwndDlg, IDC_SUNKEN_LINE);
1162   GetWindowRect(hwndControl, &rc);
1163
1164   ptLine.x = 0;
1165   ptLine.y = rc.bottom;
1166
1167   ScreenToClient(hwndDlg, &ptLine);
1168
1169   padding.y = ptButton.y - ptLine.y;
1170
1171   if (padding.y < 0)
1172           ERR("padding negative ! Please report this !\n");
1173
1174   /* this is most probably not correct, but the best we have now */
1175   padding.x = padding.y;
1176   return padding;
1177 }
1178
1179 /******************************************************************************
1180  *            PROPSHEET_CreateTabControl
1181  *
1182  * Insert the tabs in the tab control.
1183  */
1184 static BOOL PROPSHEET_CreateTabControl(HWND hwndParent,
1185                                        PropSheetInfo * psInfo)
1186 {
1187   HWND hwndTabCtrl = GetDlgItem(hwndParent, IDC_TABCONTROL);
1188   TCITEMW item;
1189   int i, nTabs;
1190   int iImage = 0;
1191
1192   TRACE("\n");
1193   item.mask = TCIF_TEXT;
1194   item.cchTextMax = MAX_TABTEXT_LENGTH;
1195
1196   nTabs = psInfo->nPages;
1197
1198   /*
1199    * Set the image list for icons.
1200    */
1201   if (psInfo->hImageList)
1202   {
1203     SendMessageW(hwndTabCtrl, TCM_SETIMAGELIST, 0, (LPARAM)psInfo->hImageList);
1204   }
1205
1206   for (i = 0; i < nTabs; i++)
1207   {
1208     if ( psInfo->proppage[i].hasIcon )
1209     {
1210       item.mask |= TCIF_IMAGE;
1211       item.iImage = iImage++;
1212     }
1213     else
1214     {
1215       item.mask &= ~TCIF_IMAGE;
1216     }
1217
1218     item.pszText = (LPWSTR) psInfo->proppage[i].pszText;
1219     SendMessageW(hwndTabCtrl, TCM_INSERTITEMW, (WPARAM)i, (LPARAM)&item);
1220   }
1221
1222   return TRUE;
1223 }
1224 /*
1225  * Get the size of an in-memory Template
1226  *
1227  *( Based on the code of PROPSHEET_CollectPageInfo)
1228  * See also dialog.c/DIALOG_ParseTemplate32().
1229  */
1230
1231 static UINT GetTemplateSize(DLGTEMPLATE* pTemplate)
1232
1233 {
1234   const WORD*  p = (const WORD *)pTemplate;
1235   BOOL  istemplateex = (((MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF);
1236   WORD nrofitems;
1237
1238   if (istemplateex)
1239   {
1240     /* DLGTEMPLATEEX (not defined in any std. header file) */
1241
1242     TRACE("is DLGTEMPLATEEX\n");
1243     p++;       /* dlgVer    */
1244     p++;       /* signature */
1245     p += 2;    /* help ID   */
1246     p += 2;    /* ext style */
1247     p += 2;    /* style     */
1248   }
1249   else
1250   {
1251     /* DLGTEMPLATE */
1252
1253     TRACE("is DLGTEMPLATE\n");
1254     p += 2;    /* style     */
1255     p += 2;    /* ext style */
1256   }
1257
1258   nrofitems =   (WORD)*p; p++;    /* nb items */
1259   p++;    /*   x      */
1260   p++;    /*   y      */
1261   p++;    /*   width  */
1262   p++;    /*   height */
1263
1264   /* menu */
1265   switch ((WORD)*p)
1266   {
1267     case 0x0000:
1268       p++;
1269       break;
1270     case 0xffff:
1271       p += 2;
1272       break;
1273     default:
1274       TRACE("menu %s\n",debugstr_w((LPCWSTR)p));
1275       p += lstrlenW( (LPCWSTR)p ) + 1;
1276       break;
1277   }
1278
1279   /* class */
1280   switch ((WORD)*p)
1281   {
1282     case 0x0000:
1283       p++;
1284       break;
1285     case 0xffff:
1286       p += 2; /* 0xffff plus predefined window class ordinal value */
1287       break;
1288     default:
1289       TRACE("class %s\n",debugstr_w((LPCWSTR)p));
1290       p += lstrlenW( (LPCWSTR)p ) + 1;
1291       break;
1292   }
1293
1294   /* title */
1295   TRACE("title %s\n",debugstr_w((LPCWSTR)p));
1296   p += lstrlenW((LPCWSTR)p) + 1;
1297
1298   /* font, if DS_SETFONT set */
1299   if ((DS_SETFONT & ((istemplateex)?  ((MyDLGTEMPLATEEX*)pTemplate)->style :
1300                      pTemplate->style)))
1301     {
1302       p+=(istemplateex)?3:1;
1303       TRACE("font %s\n",debugstr_w((LPCWSTR)p));
1304       p += lstrlenW( (LPCWSTR)p ) + 1; /* the font name */
1305     }
1306
1307   /* now process the DLGITEMTEMPLATE(EX) structs (plus custom data)
1308    * that are following the DLGTEMPLATE(EX) data */
1309   TRACE("%d items\n",nrofitems);
1310   while (nrofitems > 0)
1311     {
1312       p = (WORD*)(((DWORD)p + 3) & ~3); /* DWORD align */
1313       
1314       /* skip header */
1315       p += (istemplateex ? sizeof(MyDLGITEMTEMPLATEEX) : sizeof(DLGITEMTEMPLATE))/sizeof(WORD);
1316       
1317       /* check class */
1318       switch ((WORD)*p)
1319         {
1320         case 0x0000:
1321           p++;
1322           break;
1323         case 0xffff:
1324           TRACE("class ordinal 0x%08lx\n",*(DWORD*)p);
1325           p += 2;
1326           break;
1327         default:
1328           TRACE("class %s\n",debugstr_w((LPCWSTR)p));
1329           p += lstrlenW( (LPCWSTR)p ) + 1;
1330           break;
1331         }
1332
1333       /* check title text */
1334       switch ((WORD)*p)
1335         {
1336         case 0x0000:
1337           p++;
1338           break;
1339         case 0xffff:
1340           TRACE("text ordinal 0x%08lx\n",*(DWORD*)p);
1341           p += 2;
1342           break;
1343         default:
1344           TRACE("text %s\n",debugstr_w((LPCWSTR)p));
1345           p += lstrlenW( (LPCWSTR)p ) + 1;
1346           break;
1347         }
1348       p += *p + 1;    /* Skip extra data */
1349       --nrofitems;
1350     }
1351   
1352   TRACE("%p %p size 0x%08x\n",p, (WORD*)pTemplate,sizeof(WORD)*(p - (WORD*)pTemplate));
1353   return (p - (WORD*)pTemplate)*sizeof(WORD);
1354   
1355 }
1356
1357 /******************************************************************************
1358  *            PROPSHEET_CreatePage
1359  *
1360  * Creates a page.
1361  */
1362 static BOOL PROPSHEET_CreatePage(HWND hwndParent,
1363                                 int index,
1364                                 const PropSheetInfo * psInfo,
1365                                 LPCPROPSHEETPAGEW ppshpage)
1366 {
1367   DLGTEMPLATE* pTemplate;
1368   HWND hwndPage;
1369   RECT rc;
1370   PropPageInfo* ppInfo = psInfo->proppage;
1371   PADDING_INFO padding;
1372   UINT pageWidth,pageHeight;
1373   DWORD resSize;
1374   LPVOID temp = NULL;
1375
1376   TRACE("index %d\n", index);
1377
1378   if (ppshpage == NULL)
1379   {
1380     return FALSE;
1381   }
1382
1383   if (ppshpage->dwFlags & PSP_DLGINDIRECT)
1384     {
1385       pTemplate = (DLGTEMPLATE*)ppshpage->u.pResource;
1386       resSize = GetTemplateSize(pTemplate);
1387     }
1388   else if(ppshpage->dwFlags & PSP_INTERNAL_UNICODE)
1389   {
1390     HRSRC hResource;
1391     HANDLE hTemplate;
1392
1393     hResource = FindResourceW(ppshpage->hInstance,
1394                                     ppshpage->u.pszTemplate,
1395                                     RT_DIALOGW);
1396     if(!hResource)
1397         return FALSE;
1398
1399     resSize = SizeofResource(ppshpage->hInstance, hResource);
1400
1401     hTemplate = LoadResource(ppshpage->hInstance, hResource);
1402     if(!hTemplate)
1403         return FALSE;
1404
1405     pTemplate = (LPDLGTEMPLATEW)LockResource(hTemplate);
1406     /*
1407      * Make a copy of the dialog template to make it writable
1408      */
1409   }
1410   else
1411   {
1412     HRSRC hResource;
1413     HANDLE hTemplate;
1414
1415     hResource = FindResourceA(ppshpage->hInstance,
1416                                     (LPSTR)ppshpage->u.pszTemplate,
1417                                     RT_DIALOGA);
1418     if(!hResource)
1419         return FALSE;
1420
1421     resSize = SizeofResource(ppshpage->hInstance, hResource);
1422
1423     hTemplate = LoadResource(ppshpage->hInstance, hResource);
1424     if(!hTemplate)
1425         return FALSE;
1426
1427     pTemplate = (LPDLGTEMPLATEA)LockResource(hTemplate);
1428     /*
1429      * Make a copy of the dialog template to make it writable
1430      */
1431   }
1432   temp = COMCTL32_Alloc(resSize);
1433   if (!temp)
1434     return FALSE;
1435   
1436   TRACE("copying pTemplate %p into temp %p (%ld)\n", pTemplate, temp, resSize);
1437   memcpy(temp, pTemplate, resSize);
1438   pTemplate = temp;
1439
1440   if (((MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF)
1441   {
1442     ((MyDLGTEMPLATEEX*)pTemplate)->style |= WS_CHILD | DS_CONTROL;
1443     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~DS_MODALFRAME;
1444     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_CAPTION;
1445     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_SYSMENU;
1446     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_POPUP;
1447     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_DISABLED;
1448     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_VISIBLE;
1449     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_THICKFRAME;
1450   }
1451   else
1452   {
1453     pTemplate->style |= WS_CHILD | DS_CONTROL;
1454     pTemplate->style &= ~DS_MODALFRAME;
1455     pTemplate->style &= ~WS_CAPTION;
1456     pTemplate->style &= ~WS_SYSMENU;
1457     pTemplate->style &= ~WS_POPUP;
1458     pTemplate->style &= ~WS_DISABLED;
1459     pTemplate->style &= ~WS_VISIBLE;
1460     pTemplate->style &= ~WS_THICKFRAME;
1461   }
1462
1463   if (psInfo->proppage[index].useCallback)
1464     (*(ppshpage->pfnCallback))(hwndParent,
1465                                PSPCB_CREATE,
1466                                (LPPROPSHEETPAGEW)ppshpage);
1467
1468   if(ppshpage->dwFlags & PSP_INTERNAL_UNICODE)
1469      hwndPage = CreateDialogIndirectParamW(ppshpage->hInstance,
1470                                         pTemplate,
1471                                         hwndParent,
1472                                         ppshpage->pfnDlgProc,
1473                                         (LPARAM)ppshpage);
1474   else
1475      hwndPage = CreateDialogIndirectParamA(ppshpage->hInstance,
1476                                         pTemplate,
1477                                         hwndParent,
1478                                         ppshpage->pfnDlgProc,
1479                                         (LPARAM)ppshpage);
1480   /* Free a no more needed copy */
1481   if(temp)
1482       COMCTL32_Free(temp);
1483
1484   ppInfo[index].hwndPage = hwndPage;
1485
1486   if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD) {
1487       /* FIXME: This code may no longer be correct.
1488        *        It was not for the non-wizard path.  (GLA 6/02)
1489        */
1490       rc.left = psInfo->x;
1491       rc.top = psInfo->y;
1492       rc.right = psInfo->width;
1493       rc.bottom = psInfo->height;
1494
1495       MapDialogRect(hwndParent, &rc);
1496
1497       pageWidth = rc.right - rc.left;
1498       pageHeight = rc.bottom - rc.top;
1499
1500       padding = PROPSHEET_GetPaddingInfoWizard(hwndParent, psInfo);
1501       TRACE("setting page %08lx, rc (%ld,%ld)-(%ld,%ld) w=%d, h=%d, padx=%d, pady=%d\n",
1502             (DWORD)hwndPage, rc.left, rc.top, rc.right, rc.bottom,
1503             pageWidth, pageHeight, padding.x, padding.y);
1504       SetWindowPos(hwndPage, HWND_TOP,
1505                    rc.left + padding.x/2,
1506                    rc.top + padding.y/2,
1507                    pageWidth, pageHeight, 0);
1508   }
1509   else {
1510       /*
1511        * Ask the Tab control to reduce the client rectangle to that
1512        * it has available.
1513        */
1514       PROPSHEET_GetPageRect(psInfo, hwndParent, &rc);
1515       pageWidth = rc.right - rc.left;
1516       pageHeight = rc.bottom - rc.top;
1517       TRACE("setting page %08lx, rc (%ld,%ld)-(%ld,%ld) w=%d, h=%d\n",
1518             (DWORD)hwndPage, rc.left, rc.top, rc.right, rc.bottom,
1519             pageWidth, pageHeight);
1520       SetWindowPos(hwndPage, HWND_TOP,
1521                    rc.left, rc.top,
1522                    pageWidth, pageHeight, 0);
1523   }
1524
1525   return TRUE;
1526 }
1527
1528 /******************************************************************************
1529  *            PROPSHEET_ShowPage
1530  *
1531  * Displays or creates the specified page.
1532  */
1533 static BOOL PROPSHEET_ShowPage(HWND hwndDlg, int index, PropSheetInfo * psInfo)
1534 {
1535   HWND hwndTabCtrl;
1536
1537   TRACE("active_page %d, index %d\n", psInfo->active_page, index);
1538   if (index == psInfo->active_page)
1539   {
1540       if (GetTopWindow(hwndDlg) != psInfo->proppage[index].hwndPage)
1541           SetWindowPos(psInfo->proppage[index].hwndPage, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1542       return TRUE;
1543   }
1544
1545   if (psInfo->proppage[index].hwndPage == 0)
1546   {
1547      LPCPROPSHEETPAGEW ppshpage;
1548
1549      ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
1550      PROPSHEET_CreatePage(hwndDlg, index, psInfo, ppshpage);
1551   }
1552
1553   if (psInfo->active_page != -1)
1554      ShowWindow(psInfo->proppage[psInfo->active_page].hwndPage, SW_HIDE);
1555
1556   ShowWindow(psInfo->proppage[index].hwndPage, SW_SHOW);
1557
1558   /* Synchronize current selection with tab control
1559    * It seems to be needed even in case of PSH_WIZARD (no tab controls there) */
1560   hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
1561   SendMessageW(hwndTabCtrl, TCM_SETCURSEL, index, 0);
1562
1563   psInfo->active_page = index;
1564   psInfo->activeValid = TRUE;
1565
1566   return TRUE;
1567 }
1568
1569 /******************************************************************************
1570  *            PROPSHEET_Back
1571  */
1572 static BOOL PROPSHEET_Back(HWND hwndDlg)
1573 {
1574   PSHNOTIFY psn;
1575   HWND hwndPage;
1576   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1577                                                     PropSheetInfoStr);
1578   LRESULT result;
1579   int idx;
1580
1581   TRACE("active_page %d\n", psInfo->active_page);
1582   if (psInfo->active_page < 0)
1583      return FALSE;
1584
1585   psn.hdr.code     = PSN_WIZBACK;
1586   psn.hdr.hwndFrom = hwndDlg;
1587   psn.hdr.idFrom   = 0;
1588   psn.lParam       = 0;
1589
1590   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1591
1592   result = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1593   if (result == -1)
1594     return FALSE;
1595   else if (result == 0)
1596      idx = psInfo->active_page - 1;
1597   else
1598      idx = PROPSHEET_FindPageByResId(psInfo, result);
1599
1600   if (idx >= 0 && idx < psInfo->nPages)
1601   {
1602      if (PROPSHEET_CanSetCurSel(hwndDlg))
1603         PROPSHEET_SetCurSel(hwndDlg, idx, -1, 0);
1604   }
1605   return TRUE;
1606 }
1607
1608 /******************************************************************************
1609  *            PROPSHEET_Next
1610  */
1611 static BOOL PROPSHEET_Next(HWND hwndDlg)
1612 {
1613   PSHNOTIFY psn;
1614   HWND hwndPage;
1615   LRESULT msgResult = 0;
1616   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1617                                                     PropSheetInfoStr);
1618   int idx;
1619
1620   TRACE("active_page %d\n", psInfo->active_page);
1621   if (psInfo->active_page < 0)
1622      return FALSE;
1623
1624   psn.hdr.code     = PSN_WIZNEXT;
1625   psn.hdr.hwndFrom = hwndDlg;
1626   psn.hdr.idFrom   = 0;
1627   psn.lParam       = 0;
1628
1629   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1630
1631   msgResult = SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1632   if (msgResult == -1)
1633     return FALSE;
1634   else if (msgResult == 0)
1635      idx = psInfo->active_page + 1;
1636   else
1637      idx = PROPSHEET_FindPageByResId(psInfo, msgResult);
1638
1639   if (idx < psInfo->nPages )
1640   {
1641      if (PROPSHEET_CanSetCurSel(hwndDlg) != FALSE)
1642         PROPSHEET_SetCurSel(hwndDlg, idx, 1, 0);
1643   }
1644
1645   return TRUE;
1646 }
1647
1648 /******************************************************************************
1649  *            PROPSHEET_Finish
1650  */
1651 static BOOL PROPSHEET_Finish(HWND hwndDlg)
1652 {
1653   PSHNOTIFY psn;
1654   HWND hwndPage;
1655   LRESULT msgResult = 0;
1656   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1657                                                     PropSheetInfoStr);
1658
1659   TRACE("active_page %d\n", psInfo->active_page);
1660   if (psInfo->active_page < 0)
1661      return FALSE;
1662
1663   psn.hdr.code     = PSN_WIZFINISH;
1664   psn.hdr.hwndFrom = hwndDlg;
1665   psn.hdr.idFrom   = 0;
1666   psn.lParam       = 0;
1667
1668   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1669
1670   msgResult = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1671
1672   TRACE("msg result %ld\n", msgResult);
1673
1674   if (msgResult != 0)
1675     return FALSE;
1676
1677   if (psInfo->isModeless)
1678     psInfo->activeValid = FALSE;
1679   else
1680     EndDialog(hwndDlg, TRUE);
1681
1682   return TRUE;
1683 }
1684
1685 /******************************************************************************
1686  *            PROPSHEET_Apply
1687  */
1688 static BOOL PROPSHEET_Apply(HWND hwndDlg, LPARAM lParam)
1689 {
1690   int i;
1691   HWND hwndPage;
1692   PSHNOTIFY psn;
1693   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1694                                                     PropSheetInfoStr);
1695
1696   TRACE("active_page %d\n", psInfo->active_page);
1697   if (psInfo->active_page < 0)
1698      return FALSE;
1699
1700   psn.hdr.hwndFrom = hwndDlg;
1701   psn.hdr.idFrom   = 0;
1702   psn.lParam       = 0;
1703
1704
1705   /*
1706    * Send PSN_KILLACTIVE to the current page.
1707    */
1708   psn.hdr.code = PSN_KILLACTIVE;
1709
1710   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1711
1712   if (SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn) != FALSE)
1713     return FALSE;
1714
1715   /*
1716    * Send PSN_APPLY to all pages.
1717    */
1718   psn.hdr.code = PSN_APPLY;
1719   psn.lParam   = lParam;
1720
1721   for (i = 0; i < psInfo->nPages; i++)
1722   {
1723     hwndPage = psInfo->proppage[i].hwndPage;
1724     if (hwndPage)
1725     {
1726        switch (SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn))
1727        {
1728        case PSNRET_INVALID:
1729            PROPSHEET_ShowPage(hwndDlg, i, psInfo);
1730            /* fall through */
1731        case PSNRET_INVALID_NOCHANGEPAGE:
1732            return FALSE;
1733        }
1734     }
1735   }
1736
1737   if(lParam)
1738   {
1739      psInfo->activeValid = FALSE;
1740   }
1741   else if(psInfo->active_page >= 0)
1742   {
1743      psn.hdr.code = PSN_SETACTIVE;
1744      psn.lParam   = 0;
1745      hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1746      SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1747   }
1748
1749   return TRUE;
1750 }
1751
1752 /******************************************************************************
1753  *            PROPSHEET_Cancel
1754  */
1755 static void PROPSHEET_Cancel(HWND hwndDlg, LPARAM lParam)
1756 {
1757   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1758                                                     PropSheetInfoStr);
1759   HWND hwndPage;
1760   PSHNOTIFY psn;
1761   int i;
1762
1763   TRACE("active_page %d\n", psInfo->active_page);
1764   if (psInfo->active_page < 0)
1765      return;
1766
1767   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1768   psn.hdr.code     = PSN_QUERYCANCEL;
1769   psn.hdr.hwndFrom = hwndDlg;
1770   psn.hdr.idFrom   = 0;
1771   psn.lParam       = 0;
1772
1773   if (SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn))
1774     return;
1775
1776   psn.hdr.code = PSN_RESET;
1777   psn.lParam   = lParam;
1778
1779   for (i = 0; i < psInfo->nPages; i++)
1780   {
1781     hwndPage = psInfo->proppage[i].hwndPage;
1782
1783     if (hwndPage)
1784        SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1785   }
1786
1787   if (psInfo->isModeless)
1788   {
1789      /* makes PSM_GETCURRENTPAGEHWND return NULL */
1790      psInfo->activeValid = FALSE;
1791   }
1792   else
1793     EndDialog(hwndDlg, FALSE);
1794 }
1795
1796 /******************************************************************************
1797  *            PROPSHEET_Help
1798  */
1799 static void PROPSHEET_Help(HWND hwndDlg)
1800 {
1801   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1802                                                     PropSheetInfoStr);
1803   HWND hwndPage;
1804   PSHNOTIFY psn;
1805
1806   TRACE("active_page %d\n", psInfo->active_page);
1807   if (psInfo->active_page < 0)
1808      return;
1809
1810   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1811   psn.hdr.code     = PSN_HELP;
1812   psn.hdr.hwndFrom = hwndDlg;
1813   psn.hdr.idFrom   = 0;
1814   psn.lParam       = 0;
1815
1816   SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1817 }
1818
1819 /******************************************************************************
1820  *            PROPSHEET_Changed
1821  */
1822 static void PROPSHEET_Changed(HWND hwndDlg, HWND hwndDirtyPage)
1823 {
1824   int i;
1825   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1826                                                     PropSheetInfoStr);
1827
1828   TRACE("\n");
1829   if (!psInfo) return;
1830   /*
1831    * Set the dirty flag of this page.
1832    */
1833   for (i = 0; i < psInfo->nPages; i++)
1834   {
1835     if (psInfo->proppage[i].hwndPage == hwndDirtyPage)
1836       psInfo->proppage[i].isDirty = TRUE;
1837   }
1838
1839   /*
1840    * Enable the Apply button.
1841    */
1842   if (psInfo->hasApply)
1843   {
1844     HWND hwndApplyBtn = GetDlgItem(hwndDlg, IDC_APPLY_BUTTON);
1845
1846     EnableWindow(hwndApplyBtn, TRUE);
1847   }
1848 }
1849
1850 /******************************************************************************
1851  *            PROPSHEET_UnChanged
1852  */
1853 static void PROPSHEET_UnChanged(HWND hwndDlg, HWND hwndCleanPage)
1854 {
1855   int i;
1856   BOOL noPageDirty = TRUE;
1857   HWND hwndApplyBtn = GetDlgItem(hwndDlg, IDC_APPLY_BUTTON);
1858   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1859                                                     PropSheetInfoStr);
1860
1861   TRACE("\n");
1862   if ( !psInfo ) return;
1863   for (i = 0; i < psInfo->nPages; i++)
1864   {
1865     /* set the specified page as clean */
1866     if (psInfo->proppage[i].hwndPage == hwndCleanPage)
1867       psInfo->proppage[i].isDirty = FALSE;
1868
1869     /* look to see if there's any dirty pages */
1870     if (psInfo->proppage[i].isDirty)
1871       noPageDirty = FALSE;
1872   }
1873
1874   /*
1875    * Disable Apply button.
1876    */
1877   if (noPageDirty)
1878     EnableWindow(hwndApplyBtn, FALSE);
1879 }
1880
1881 /******************************************************************************
1882  *            PROPSHEET_PressButton
1883  */
1884 static void PROPSHEET_PressButton(HWND hwndDlg, int buttonID)
1885 {
1886   TRACE("buttonID %d\n", buttonID);
1887   switch (buttonID)
1888   {
1889     case PSBTN_APPLYNOW:
1890       PROPSHEET_DoCommand(hwndDlg, IDC_APPLY_BUTTON);
1891       break;
1892     case PSBTN_BACK:
1893       PROPSHEET_Back(hwndDlg);
1894       break;
1895     case PSBTN_CANCEL:
1896       PROPSHEET_DoCommand(hwndDlg, IDCANCEL);
1897       break;
1898     case PSBTN_FINISH:
1899       PROPSHEET_Finish(hwndDlg);
1900       break;
1901     case PSBTN_HELP:
1902       PROPSHEET_DoCommand(hwndDlg, IDHELP);
1903       break;
1904     case PSBTN_NEXT:
1905       PROPSHEET_Next(hwndDlg);
1906       break;
1907     case PSBTN_OK:
1908       PROPSHEET_DoCommand(hwndDlg, IDOK);
1909       break;
1910     default:
1911       FIXME("Invalid button index %d\n", buttonID);
1912   }
1913 }
1914
1915
1916 /*************************************************************************
1917  * BOOL PROPSHEET_CanSetCurSel [Internal]
1918  *
1919  * Test whether the current page can be changed by sending a PSN_KILLACTIVE
1920  *
1921  * PARAMS
1922  *     hwndDlg        [I] handle to a Dialog hWnd
1923  *
1924  * RETURNS
1925  *     TRUE if Current Selection can change
1926  *
1927  * NOTES
1928  */
1929 static BOOL PROPSHEET_CanSetCurSel(HWND hwndDlg)
1930 {
1931   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1932                                                     PropSheetInfoStr);
1933   HWND hwndPage;
1934   PSHNOTIFY psn;
1935   BOOL res = FALSE;
1936
1937   TRACE("active_page %d\n", psInfo->active_page);
1938   if (!psInfo)
1939   {
1940      res = FALSE;
1941      goto end;
1942   }
1943
1944   if (psInfo->active_page < 0)
1945   {
1946      res = TRUE;
1947      goto end;
1948   }
1949
1950   /*
1951    * Notify the current page.
1952    */
1953   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1954   psn.hdr.code     = PSN_KILLACTIVE;
1955   psn.hdr.hwndFrom = hwndDlg;
1956   psn.hdr.idFrom   = 0;
1957   psn.lParam       = 0;
1958
1959   res = !SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1960
1961 end:
1962   TRACE("<-- %d\n", res);
1963   return res;
1964 }
1965
1966 /******************************************************************************
1967  *            PROPSHEET_SetCurSel
1968  */
1969 static BOOL PROPSHEET_SetCurSel(HWND hwndDlg,
1970                                 int index,
1971                                 int skipdir,
1972                                 HPROPSHEETPAGE hpage
1973                                 )
1974 {
1975   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
1976   HWND hwndHelp  = GetDlgItem(hwndDlg, IDHELP);
1977   HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
1978
1979   TRACE("index %d, skipdir %d, hpage %p\n", index, skipdir, hpage);
1980   /* hpage takes precedence over index */
1981   if (hpage != NULL)
1982     index = PROPSHEET_GetPageIndex(hpage, psInfo);
1983
1984   if (index < 0 || index >= psInfo->nPages)
1985   {
1986     TRACE("Could not find page to select!\n");
1987     return FALSE;
1988   }
1989
1990   while (1) {
1991     int result;
1992     PSHNOTIFY psn;
1993
1994     if (hwndTabControl)
1995         SendMessageW(hwndTabControl, TCM_SETCURSEL, index, 0);
1996
1997     psn.hdr.code     = PSN_SETACTIVE;
1998     psn.hdr.hwndFrom = hwndDlg;
1999     psn.hdr.idFrom   = 0;
2000     psn.lParam       = 0;
2001
2002     if (!psInfo->proppage[index].hwndPage) {
2003       LPCPROPSHEETPAGEW ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
2004       PROPSHEET_CreatePage(hwndDlg, index, psInfo, ppshpage);
2005     }
2006
2007     result = SendMessageW(psInfo->proppage[index].hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
2008     if (!result)
2009       break;
2010     if (result == -1) {
2011       index+=skipdir;
2012       if (index < 0) {
2013         index = 0;
2014         FIXME("Tried to skip before first property sheet page!\n");
2015         break;
2016       }
2017       if (index >= psInfo->nPages) {
2018         FIXME("Tried to skip after last property sheet page!\n");
2019         index = psInfo->nPages-1;
2020         break;
2021       }
2022     }
2023     else if (result != 0)
2024     {
2025        index = PROPSHEET_FindPageByResId(psInfo, result);
2026        continue;
2027     }
2028   }
2029   /*
2030    * Display the new page.
2031    */
2032   PROPSHEET_ShowPage(hwndDlg, index, psInfo);
2033
2034   if (psInfo->proppage[index].hasHelp)
2035     EnableWindow(hwndHelp, TRUE);
2036   else
2037     EnableWindow(hwndHelp, FALSE);
2038
2039   return TRUE;
2040 }
2041
2042 /******************************************************************************
2043  *            PROPSHEET_SetCurSelId
2044  *
2045  * Selects the page, specified by resource id.
2046  */
2047 static void PROPSHEET_SetCurSelId(HWND hwndDlg, int id)
2048 {
2049       int idx;
2050       PropSheetInfo* psInfo =
2051           (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
2052
2053       idx = PROPSHEET_FindPageByResId(psInfo, id);
2054       if (idx < psInfo->nPages )
2055       {
2056           if (PROPSHEET_CanSetCurSel(hwndDlg) != FALSE)
2057               PROPSHEET_SetCurSel(hwndDlg, idx, 1, 0);
2058       }
2059 }
2060
2061 /******************************************************************************
2062  *            PROPSHEET_SetTitleA
2063  */
2064 static void PROPSHEET_SetTitleA(HWND hwndDlg, DWORD dwStyle, LPCSTR lpszText)
2065 {
2066   if(HIWORD(lpszText))
2067   {
2068      WCHAR szTitle[256];
2069      MultiByteToWideChar(CP_ACP, 0, lpszText, -1,
2070                              szTitle, sizeof(szTitle));
2071      PROPSHEET_SetTitleW(hwndDlg, dwStyle, szTitle);
2072   }
2073   else
2074   {
2075      PROPSHEET_SetTitleW(hwndDlg, dwStyle, (LPCWSTR)lpszText);
2076   }
2077 }
2078
2079 /******************************************************************************
2080  *            PROPSHEET_SetTitleW
2081  */
2082 static void PROPSHEET_SetTitleW(HWND hwndDlg, DWORD dwStyle, LPCWSTR lpszText)
2083 {
2084   PropSheetInfo*        psInfo = (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
2085   WCHAR                 szTitle[256];
2086
2087   TRACE("'%s' (style %08lx)\n", debugstr_w(lpszText), dwStyle);
2088   if (HIWORD(lpszText) == 0) {
2089     if (!LoadStringW(psInfo->ppshheader.hInstance,
2090                      LOWORD(lpszText), szTitle, sizeof(szTitle)-sizeof(WCHAR)))
2091       return;
2092     lpszText = szTitle;
2093   }
2094   if (dwStyle & PSH_PROPTITLE)
2095   {
2096     WCHAR* dest;
2097     int lentitle = strlenW(lpszText);
2098     int lenprop  = strlenW(psInfo->strPropertiesFor);
2099
2100     dest = COMCTL32_Alloc( (lentitle + lenprop + 1)*sizeof (WCHAR));
2101     strcpyW(dest, psInfo->strPropertiesFor);
2102     strcatW(dest, lpszText);
2103
2104     SetWindowTextW(hwndDlg, dest);
2105     COMCTL32_Free(dest);
2106   }
2107   else
2108     SetWindowTextW(hwndDlg, lpszText);
2109 }
2110
2111 /******************************************************************************
2112  *            PROPSHEET_SetFinishTextA
2113  */
2114 static void PROPSHEET_SetFinishTextA(HWND hwndDlg, LPCSTR lpszText)
2115 {
2116   HWND hwndButton = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2117
2118   TRACE("'%s'\n", lpszText);
2119   /* Set text, show and enable the Finish button */
2120   SetWindowTextA(hwndButton, lpszText);
2121   ShowWindow(hwndButton, SW_SHOW);
2122   EnableWindow(hwndButton, TRUE);
2123
2124   /* Make it default pushbutton */
2125   SendMessageA(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2126
2127   /* Hide Back button */
2128   hwndButton = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
2129   ShowWindow(hwndButton, SW_HIDE);
2130
2131   /* Hide Next button */
2132   hwndButton = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
2133   ShowWindow(hwndButton, SW_HIDE);
2134 }
2135
2136 /******************************************************************************
2137  *            PROPSHEET_SetFinishTextW
2138  */
2139 static void PROPSHEET_SetFinishTextW(HWND hwndDlg, LPCWSTR lpszText)
2140 {
2141   HWND hwndButton = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2142
2143   TRACE("'%s'\n", debugstr_w(lpszText));
2144   /* Set text, show and enable the Finish button */
2145   SetWindowTextW(hwndButton, lpszText);
2146   ShowWindow(hwndButton, SW_SHOW);
2147   EnableWindow(hwndButton, TRUE);
2148
2149   /* Make it default pushbutton */
2150   SendMessageW(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2151
2152   /* Hide Back button */
2153   hwndButton = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
2154   ShowWindow(hwndButton, SW_HIDE);
2155
2156   /* Hide Next button */
2157   hwndButton = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
2158   ShowWindow(hwndButton, SW_HIDE);
2159 }
2160
2161 /******************************************************************************
2162  *            PROPSHEET_QuerySiblings
2163  */
2164 static LRESULT PROPSHEET_QuerySiblings(HWND hwndDlg,
2165                                        WPARAM wParam, LPARAM lParam)
2166 {
2167   int i = 0;
2168   HWND hwndPage;
2169   LRESULT msgResult = 0;
2170   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2171                                                     PropSheetInfoStr);
2172
2173   while ((i < psInfo->nPages) && (msgResult == 0))
2174   {
2175     hwndPage = psInfo->proppage[i].hwndPage;
2176     msgResult = SendMessageA(hwndPage, PSM_QUERYSIBLINGS, wParam, lParam);
2177     i++;
2178   }
2179
2180   return msgResult;
2181 }
2182
2183
2184 /******************************************************************************
2185  *            PROPSHEET_AddPage
2186  */
2187 static BOOL PROPSHEET_AddPage(HWND hwndDlg,
2188                               HPROPSHEETPAGE hpage)
2189 {
2190   PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2191                                                      PropSheetInfoStr);
2192   HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2193   TCITEMW item;
2194   LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)hpage;
2195
2196   TRACE("hpage %p\n", hpage);
2197   /*
2198    * Allocate and fill in a new PropPageInfo entry.
2199    */
2200   psInfo->proppage = (PropPageInfo*) COMCTL32_ReAlloc(psInfo->proppage,
2201                                                       sizeof(PropPageInfo) *
2202                                                       (psInfo->nPages + 1));
2203   if (!PROPSHEET_CollectPageInfo(ppsp, psInfo, psInfo->nPages))
2204       return FALSE;
2205
2206   psInfo->proppage[psInfo->nPages].hpage = hpage;
2207
2208   if (ppsp->dwFlags & PSP_PREMATURE)
2209   {
2210      /* Create the page but don't show it */
2211      PROPSHEET_CreatePage(hwndDlg, psInfo->nPages, psInfo, ppsp);
2212   }
2213
2214   /*
2215    * Add a new tab to the tab control.
2216    */
2217   item.mask = TCIF_TEXT;
2218   item.pszText = (LPWSTR) psInfo->proppage[psInfo->nPages].pszText;
2219   item.cchTextMax = MAX_TABTEXT_LENGTH;
2220
2221   if (psInfo->hImageList)
2222   {
2223     SendMessageW(hwndTabControl, TCM_SETIMAGELIST, 0, (LPARAM)psInfo->hImageList);
2224   }
2225
2226   if ( psInfo->proppage[psInfo->nPages].hasIcon )
2227   {
2228     item.mask |= TCIF_IMAGE;
2229     item.iImage = psInfo->nPages;
2230   }
2231
2232   SendMessageW(hwndTabControl, TCM_INSERTITEMW, psInfo->nPages + 1,
2233                (LPARAM)&item);
2234
2235   psInfo->nPages++;
2236
2237   /* If it is the only page - show it */
2238   if(psInfo->nPages == 1)
2239      PROPSHEET_SetCurSel(hwndDlg, 0, 1, 0);
2240   return TRUE;
2241 }
2242
2243 /******************************************************************************
2244  *            PROPSHEET_RemovePage
2245  */
2246 static BOOL PROPSHEET_RemovePage(HWND hwndDlg,
2247                                  int index,
2248                                  HPROPSHEETPAGE hpage)
2249 {
2250   PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2251                                                      PropSheetInfoStr);
2252   HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2253   PropPageInfo* oldPages;
2254
2255   TRACE("index %d, hpage %p\n", index, hpage);
2256   if (!psInfo) {
2257     return FALSE;
2258   }
2259   oldPages = psInfo->proppage;
2260   /*
2261    * hpage takes precedence over index.
2262    */
2263   if (hpage != 0)
2264   {
2265     index = PROPSHEET_GetPageIndex(hpage, psInfo);
2266   }
2267
2268   /* Make sure that index is within range */
2269   if (index < 0 || index >= psInfo->nPages)
2270   {
2271       TRACE("Could not find page to remove!\n");
2272       return FALSE;
2273   }
2274
2275   TRACE("total pages %d removing page %d active page %d\n",
2276         psInfo->nPages, index, psInfo->active_page);
2277   /*
2278    * Check if we're removing the active page.
2279    */
2280   if (index == psInfo->active_page)
2281   {
2282     if (psInfo->nPages > 1)
2283     {
2284       if (index > 0)
2285       {
2286         /* activate previous page  */
2287         PROPSHEET_SetCurSel(hwndDlg, index - 1, -1, 0);
2288       }
2289       else
2290       {
2291         /* activate the next page */
2292         PROPSHEET_SetCurSel(hwndDlg, index + 1, 1, 0);
2293         psInfo->active_page = index;
2294       }
2295     }
2296     else
2297     {
2298       psInfo->active_page = -1;
2299       if (!psInfo->isModeless)
2300       {
2301          EndDialog(hwndDlg, FALSE);
2302          return TRUE;
2303       }
2304     }
2305   }
2306   else if (index < psInfo->active_page)
2307     psInfo->active_page--;
2308
2309   /* Destroy page dialog window */
2310   DestroyWindow(psInfo->proppage[index].hwndPage);
2311
2312   /* Free page resources */
2313   if(psInfo->proppage[index].hpage)
2314   {
2315      PROPSHEETPAGEW* psp = (PROPSHEETPAGEW*)psInfo->proppage[index].hpage;
2316
2317      if ((psp->dwFlags & PSP_USETITLE) && psInfo->proppage[index].pszText)
2318         HeapFree(GetProcessHeap(), 0, (LPVOID)psInfo->proppage[index].pszText);
2319
2320      DestroyPropertySheetPage(psInfo->proppage[index].hpage);
2321   }
2322
2323   /* Remove the tab */
2324   SendMessageW(hwndTabControl, TCM_DELETEITEM, index, 0);
2325
2326   psInfo->nPages--;
2327   psInfo->proppage = COMCTL32_Alloc(sizeof(PropPageInfo) * psInfo->nPages);
2328
2329   if (index > 0)
2330     memcpy(&psInfo->proppage[0], &oldPages[0], index * sizeof(PropPageInfo));
2331
2332   if (index < psInfo->nPages)
2333     memcpy(&psInfo->proppage[index], &oldPages[index + 1],
2334            (psInfo->nPages - index) * sizeof(PropPageInfo));
2335
2336   COMCTL32_Free(oldPages);
2337
2338   return FALSE;
2339 }
2340
2341 /******************************************************************************
2342  *            PROPSHEET_SetWizButtons
2343  *
2344  * This code will work if (and assumes that) the Next button is on top of the
2345  * Finish button. ie. Finish comes after Next in the Z order.
2346  * This means make sure the dialog template reflects this.
2347  *
2348  */
2349 static void PROPSHEET_SetWizButtons(HWND hwndDlg, DWORD dwFlags)
2350 {
2351   HWND hwndBack   = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
2352   HWND hwndNext   = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
2353   HWND hwndFinish = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2354
2355   TRACE("%ld\n", dwFlags);
2356
2357   EnableWindow(hwndBack, FALSE);
2358   EnableWindow(hwndNext, FALSE);
2359   EnableWindow(hwndFinish, FALSE);
2360
2361   if (dwFlags & PSWIZB_BACK)
2362     EnableWindow(hwndBack, TRUE);
2363
2364   if (dwFlags & PSWIZB_NEXT)
2365   {
2366     /* Hide the Finish button */
2367     ShowWindow(hwndFinish, SW_HIDE);
2368
2369     /* Show and enable the Next button */
2370     ShowWindow(hwndNext, SW_SHOW);
2371     EnableWindow(hwndNext, TRUE);
2372
2373     /* Set the Next button as the default pushbutton  */
2374     SendMessageA(hwndDlg, DM_SETDEFID, IDC_NEXT_BUTTON, 0);
2375   }
2376
2377   if ((dwFlags & PSWIZB_FINISH) || (dwFlags & PSWIZB_DISABLEDFINISH))
2378   {
2379     /* Hide the Next button */
2380     ShowWindow(hwndNext, SW_HIDE);
2381
2382     /* Show the Finish button */
2383     ShowWindow(hwndFinish, SW_SHOW);
2384
2385     if (dwFlags & PSWIZB_FINISH)
2386       EnableWindow(hwndFinish, TRUE);
2387
2388     /* Set the Finish button as the default pushbutton  */
2389     SendMessageA(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2390   }
2391 }
2392
2393 /******************************************************************************
2394  *            PROPSHEET_GetPageIndex
2395  *
2396  * Given a HPROPSHEETPAGE, returns the index of the corresponding page from
2397  * the array of PropPageInfo.
2398  */
2399 static int PROPSHEET_GetPageIndex(HPROPSHEETPAGE hpage, PropSheetInfo* psInfo)
2400 {
2401   BOOL found = FALSE;
2402   int index = 0;
2403
2404   TRACE("hpage %p\n", hpage);
2405   while ((index < psInfo->nPages) && (found == FALSE))
2406   {
2407     if (psInfo->proppage[index].hpage == hpage)
2408       found = TRUE;
2409     else
2410       index++;
2411   }
2412
2413   if (found == FALSE)
2414     index = -1;
2415
2416   return index;
2417 }
2418
2419 /******************************************************************************
2420  *            PROPSHEET_CleanUp
2421  */
2422 static void PROPSHEET_CleanUp(HWND hwndDlg)
2423 {
2424   int i;
2425   PropSheetInfo* psInfo = (PropSheetInfo*) RemovePropW(hwndDlg,
2426                                                        PropSheetInfoStr);
2427
2428   TRACE("\n");
2429   if (!psInfo) return;
2430   if (HIWORD(psInfo->ppshheader.pszCaption))
2431       HeapFree(GetProcessHeap(), 0, (LPVOID)psInfo->ppshheader.pszCaption);
2432
2433   for (i = 0; i < psInfo->nPages; i++)
2434   {
2435      PROPSHEETPAGEA* psp = (PROPSHEETPAGEA*)psInfo->proppage[i].hpage;
2436
2437      if(psInfo->proppage[i].hwndPage)
2438         DestroyWindow(psInfo->proppage[i].hwndPage);
2439
2440      if(psp)
2441      {
2442         if ((psp->dwFlags & PSP_USETITLE) && psInfo->proppage[i].pszText)
2443            HeapFree(GetProcessHeap(), 0, (LPVOID)psInfo->proppage[i].pszText);
2444
2445         DestroyPropertySheetPage(psInfo->proppage[i].hpage);
2446      }
2447   }
2448
2449   COMCTL32_Free(psInfo->proppage);
2450   COMCTL32_Free(psInfo->strPropertiesFor);
2451   ImageList_Destroy(psInfo->hImageList);
2452
2453   GlobalFree((HGLOBAL)psInfo);
2454 }
2455
2456 /******************************************************************************
2457  *            PropertySheet    (COMCTL32.@)
2458  *            PropertySheetA   (COMCTL32.@)
2459  */
2460 INT WINAPI PropertySheetA(LPCPROPSHEETHEADERA lppsh)
2461 {
2462   int bRet = 0;
2463   PropSheetInfo* psInfo = (PropSheetInfo*) GlobalAlloc(GPTR,
2464                                                        sizeof(PropSheetInfo));
2465   UINT i, n;
2466   BYTE* pByte;
2467
2468   TRACE("(%p)\n", lppsh);
2469
2470   PROPSHEET_CollectSheetInfoA(lppsh, psInfo);
2471
2472   psInfo->proppage = (PropPageInfo*) COMCTL32_Alloc(sizeof(PropPageInfo) *
2473                                                     lppsh->nPages);
2474   pByte = (BYTE*) psInfo->ppshheader.u3.ppsp;
2475
2476   for (n = i = 0; i < lppsh->nPages; i++, n++)
2477   {
2478     if (!(lppsh->dwFlags & PSH_PROPSHEETPAGE))
2479       psInfo->proppage[n].hpage = psInfo->ppshheader.u3.phpage[i];
2480     else
2481     {
2482        psInfo->proppage[n].hpage = CreatePropertySheetPageA((LPCPROPSHEETPAGEA)pByte);
2483        pByte += ((LPPROPSHEETPAGEA)pByte)->dwSize;
2484     }
2485
2486     if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2487                                psInfo, n))
2488     {
2489         if (lppsh->dwFlags & PSH_PROPSHEETPAGE)
2490             DestroyPropertySheetPage(psInfo->proppage[n].hpage);
2491         n--;
2492         psInfo->nPages--;
2493     }
2494   }
2495
2496   psInfo->unicode = FALSE;
2497   bRet = PROPSHEET_CreateDialog(psInfo);
2498
2499   return bRet;
2500 }
2501
2502 /******************************************************************************
2503  *            PropertySheetW   (COMCTL32.@)
2504  */
2505 INT WINAPI PropertySheetW(LPCPROPSHEETHEADERW lppsh)
2506 {
2507   int bRet = 0;
2508   PropSheetInfo* psInfo = (PropSheetInfo*) GlobalAlloc(GPTR,
2509                                                        sizeof(PropSheetInfo));
2510   UINT i, n;
2511   BYTE* pByte;
2512
2513   TRACE("(%p)\n", lppsh);
2514
2515   PROPSHEET_CollectSheetInfoW(lppsh, psInfo);
2516
2517   psInfo->proppage = (PropPageInfo*) COMCTL32_Alloc(sizeof(PropPageInfo) *
2518                                                     lppsh->nPages);
2519   pByte = (BYTE*) psInfo->ppshheader.u3.ppsp;
2520
2521   for (n = i = 0; i < lppsh->nPages; i++, n++)
2522   {
2523     if (!(lppsh->dwFlags & PSH_PROPSHEETPAGE))
2524       psInfo->proppage[n].hpage = psInfo->ppshheader.u3.phpage[i];
2525     else
2526     {
2527        psInfo->proppage[n].hpage = CreatePropertySheetPageW((LPCPROPSHEETPAGEW)pByte);
2528        pByte += ((LPPROPSHEETPAGEW)pByte)->dwSize;
2529     }
2530
2531     if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2532                                psInfo, n))
2533     {
2534         if (lppsh->dwFlags & PSH_PROPSHEETPAGE)
2535             DestroyPropertySheetPage(psInfo->proppage[n].hpage);
2536         n--;
2537         psInfo->nPages--;
2538     }
2539   }
2540
2541   psInfo->unicode = TRUE;
2542   bRet = PROPSHEET_CreateDialog(psInfo);
2543
2544   return bRet;
2545 }
2546
2547 /******************************************************************************
2548  *            CreatePropertySheetPage    (COMCTL32.@)
2549  *            CreatePropertySheetPageA   (COMCTL32.@)
2550  */
2551 HPROPSHEETPAGE WINAPI CreatePropertySheetPageA(
2552                           LPCPROPSHEETPAGEA lpPropSheetPage)
2553 {
2554   PROPSHEETPAGEW* ppsp = COMCTL32_Alloc(sizeof(PROPSHEETPAGEW));
2555
2556   memcpy(ppsp,lpPropSheetPage,min(lpPropSheetPage->dwSize,sizeof(PROPSHEETPAGEA)));
2557
2558   ppsp->dwFlags &= ~ PSP_INTERNAL_UNICODE;
2559   if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) && HIWORD( ppsp->u.pszTemplate ) )
2560   {
2561      int len = strlen(lpPropSheetPage->u.pszTemplate);
2562
2563      ppsp->u.pszTemplate = HeapAlloc( GetProcessHeap(),0,len+1 );
2564      strcpy( (LPSTR)ppsp->u.pszTemplate, lpPropSheetPage->u.pszTemplate );
2565   }
2566   if ( (ppsp->dwFlags & PSP_USEICONID) && HIWORD( ppsp->u2.pszIcon ) )
2567   {
2568       PROPSHEET_AtoW(&ppsp->u2.pszIcon, lpPropSheetPage->u2.pszIcon);
2569   }
2570
2571   if ((ppsp->dwFlags & PSP_USETITLE) && HIWORD( ppsp->pszTitle ))
2572   {
2573       PROPSHEET_AtoW(&ppsp->pszTitle, lpPropSheetPage->pszTitle);
2574   }
2575   else if ( !(ppsp->dwFlags & PSP_USETITLE) )
2576       ppsp->pszTitle = NULL;
2577
2578   return (HPROPSHEETPAGE)ppsp;
2579 }
2580
2581 /******************************************************************************
2582  *            CreatePropertySheetPageW   (COMCTL32.@)
2583  */
2584 HPROPSHEETPAGE WINAPI CreatePropertySheetPageW(LPCPROPSHEETPAGEW lpPropSheetPage)
2585 {
2586   PROPSHEETPAGEW* ppsp = COMCTL32_Alloc(sizeof(PROPSHEETPAGEW));
2587
2588   memcpy(ppsp,lpPropSheetPage,min(lpPropSheetPage->dwSize,sizeof(PROPSHEETPAGEW)));
2589
2590   ppsp->dwFlags |= PSP_INTERNAL_UNICODE;
2591
2592   if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) && HIWORD( ppsp->u.pszTemplate ) )
2593   {
2594     int len = strlenW(lpPropSheetPage->u.pszTemplate);
2595
2596     ppsp->u.pszTemplate = HeapAlloc( GetProcessHeap(),0,(len+1)*sizeof (WCHAR) );
2597     strcpyW( (WCHAR *)ppsp->u.pszTemplate, lpPropSheetPage->u.pszTemplate );
2598   }
2599   if ( (ppsp->dwFlags & PSP_USEICONID) && HIWORD( ppsp->u2.pszIcon ) )
2600   {
2601       int len = strlenW(lpPropSheetPage->u2.pszIcon);
2602       ppsp->u2.pszIcon = HeapAlloc( GetProcessHeap(), 0, (len+1)*sizeof (WCHAR) );
2603       strcpyW( (WCHAR *)ppsp->u2.pszIcon, lpPropSheetPage->u2.pszIcon );
2604   }
2605
2606   if ((ppsp->dwFlags & PSP_USETITLE) && HIWORD( ppsp->pszTitle ))
2607   {
2608       int len = strlenW(lpPropSheetPage->pszTitle);
2609       ppsp->pszTitle = HeapAlloc( GetProcessHeap(), 0, (len+1)*sizeof (WCHAR) );
2610       strcpyW( (WCHAR *)ppsp->pszTitle, lpPropSheetPage->pszTitle );
2611   }
2612   else if ( !(ppsp->dwFlags & PSP_USETITLE) )
2613       ppsp->pszTitle = NULL;
2614
2615   return (HPROPSHEETPAGE)ppsp;
2616 }
2617
2618 /******************************************************************************
2619  *            DestroyPropertySheetPage   (COMCTL32.@)
2620  */
2621 BOOL WINAPI DestroyPropertySheetPage(HPROPSHEETPAGE hPropPage)
2622 {
2623   PROPSHEETPAGEW *psp = (PROPSHEETPAGEW *)hPropPage;
2624
2625   if (!psp)
2626      return FALSE;
2627
2628   if ( !(psp->dwFlags & PSP_DLGINDIRECT) && HIWORD( psp->u.pszTemplate ) )
2629      HeapFree(GetProcessHeap(), 0, (LPVOID)psp->u.pszTemplate);
2630
2631   if ( (psp->dwFlags & PSP_USEICONID) && HIWORD( psp->u2.pszIcon ) )
2632      HeapFree(GetProcessHeap(), 0, (LPVOID)psp->u2.pszIcon);
2633
2634   if ((psp->dwFlags & PSP_USETITLE) && HIWORD( psp->pszTitle ))
2635       HeapFree(GetProcessHeap(), 0, (LPVOID)psp->pszTitle);
2636
2637   COMCTL32_Free(hPropPage);
2638
2639   return TRUE;
2640 }
2641
2642 /******************************************************************************
2643  *            PROPSHEET_IsDialogMessage
2644  */
2645 static BOOL PROPSHEET_IsDialogMessage(HWND hwnd, LPMSG lpMsg)
2646 {
2647    PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd, PropSheetInfoStr);
2648
2649    TRACE("\n");
2650    if (!psInfo || (hwnd != lpMsg->hwnd && !IsChild(hwnd, lpMsg->hwnd)))
2651       return FALSE;
2652
2653    if (lpMsg->message == WM_KEYDOWN && (GetKeyState(VK_CONTROL) & 0x8000))
2654    {
2655       int new_page = 0;
2656       INT dlgCode = SendMessageA(lpMsg->hwnd, WM_GETDLGCODE, 0, (LPARAM)lpMsg);
2657
2658       if (!(dlgCode & DLGC_WANTMESSAGE))
2659       {
2660          switch (lpMsg->wParam)
2661          {
2662             case VK_TAB:
2663                if (GetKeyState(VK_SHIFT) & 0x8000)
2664                    new_page = -1;
2665                 else
2666                    new_page = 1;
2667                break;
2668
2669             case VK_NEXT:   new_page = 1;  break;
2670             case VK_PRIOR:  new_page = -1; break;
2671          }
2672       }
2673
2674       if (new_page)
2675       {
2676          if (PROPSHEET_CanSetCurSel(hwnd) != FALSE)
2677          {
2678             new_page += psInfo->active_page;
2679
2680             if (new_page < 0)
2681                new_page = psInfo->nPages - 1;
2682             else if (new_page >= psInfo->nPages)
2683                new_page = 0;
2684
2685             PROPSHEET_SetCurSel(hwnd, new_page, 1, 0);
2686          }
2687
2688          return TRUE;
2689       }
2690    }
2691
2692    return IsDialogMessageA(hwnd, lpMsg);
2693 }
2694
2695 /******************************************************************************
2696  *            PROPSHEET_DoCommand
2697  */
2698 static BOOL PROPSHEET_DoCommand(HWND hwnd, WORD wID)
2699 {
2700
2701     switch (wID) {
2702
2703     case IDOK:
2704     case IDC_APPLY_BUTTON:
2705         {
2706             HWND hwndApplyBtn = GetDlgItem(hwnd, IDC_APPLY_BUTTON);
2707
2708             if (PROPSHEET_Apply(hwnd, wID == IDOK ? 1: 0) == FALSE)
2709                 break;
2710
2711             if (wID == IDOK)
2712                 {
2713                     PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
2714                                                                       PropSheetInfoStr);
2715                     int result = TRUE;
2716
2717                     if (psInfo->restartWindows)
2718                         result = ID_PSRESTARTWINDOWS;
2719
2720                     /* reboot system takes precedence over restart windows */
2721                     if (psInfo->rebootSystem)
2722                         result = ID_PSREBOOTSYSTEM;
2723
2724                     if (psInfo->isModeless)
2725                         psInfo->activeValid = FALSE;
2726                     else
2727                         EndDialog(hwnd, result);
2728                 }
2729             else
2730                 EnableWindow(hwndApplyBtn, FALSE);
2731
2732             break;
2733         }
2734
2735     case IDC_BACK_BUTTON:
2736         PROPSHEET_Back(hwnd);
2737         break;
2738
2739     case IDC_NEXT_BUTTON:
2740         PROPSHEET_Next(hwnd);
2741         break;
2742
2743     case IDC_FINISH_BUTTON:
2744         PROPSHEET_Finish(hwnd);
2745         break;
2746
2747     case IDCANCEL:
2748         PROPSHEET_Cancel(hwnd, 0);
2749         break;
2750
2751     case IDHELP:
2752         PROPSHEET_Help(hwnd);
2753         break;
2754     }
2755
2756     return TRUE;
2757 }
2758
2759 /******************************************************************************
2760  *            PROPSHEET_DialogProc
2761  */
2762 INT_PTR CALLBACK
2763 PROPSHEET_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
2764 {
2765   TRACE("hwnd=%p msg=0x%04x wparam=%x lparam=%lx\n",
2766         hwnd, uMsg, wParam, lParam);
2767
2768   switch (uMsg)
2769   {
2770     case WM_INITDIALOG:
2771     {
2772       PropSheetInfo* psInfo = (PropSheetInfo*) lParam;
2773       WCHAR* strCaption = (WCHAR*)COMCTL32_Alloc(MAX_CAPTION_LENGTH*sizeof(WCHAR));
2774       HWND hwndTabCtrl = GetDlgItem(hwnd, IDC_TABCONTROL);
2775       LPCPROPSHEETPAGEW ppshpage;
2776       int idx;
2777
2778       SetPropW(hwnd, PropSheetInfoStr, (HANDLE)psInfo);
2779
2780       /*
2781        * psInfo->hwnd is not being used by WINE code - it exists
2782        * for compatibility with "real" Windoze. The same about
2783        * SetWindowLong - WINE is only using the PropSheetInfoStr
2784        * property.
2785        */
2786       psInfo->hwnd = hwnd;
2787       SetWindowLongW(hwnd,DWL_USER,(LONG)psInfo);
2788
2789       /* set up the Next and Back buttons by default */
2790       PROPSHEET_SetWizButtons(hwnd, PSWIZB_BACK|PSWIZB_NEXT);
2791
2792       /*
2793        * Small icon in the title bar.
2794        */
2795       if ((psInfo->ppshheader.dwFlags & PSH_USEICONID) ||
2796           (psInfo->ppshheader.dwFlags & PSH_USEHICON))
2797       {
2798         HICON hIcon;
2799         int icon_cx = GetSystemMetrics(SM_CXSMICON);
2800         int icon_cy = GetSystemMetrics(SM_CYSMICON);
2801
2802         if (psInfo->ppshheader.dwFlags & PSH_USEICONID)
2803           hIcon = LoadImageW(psInfo->ppshheader.hInstance,
2804                              psInfo->ppshheader.u.pszIcon,
2805                              IMAGE_ICON,
2806                              icon_cx, icon_cy,
2807                              LR_DEFAULTCOLOR);
2808         else
2809           hIcon = psInfo->ppshheader.u.hIcon;
2810
2811         SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)hIcon);
2812       }
2813
2814       if (psInfo->ppshheader.dwFlags & PSH_USEHICON)
2815         SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)psInfo->ppshheader.u.hIcon);
2816
2817       psInfo->strPropertiesFor = strCaption;
2818
2819       GetWindowTextW(hwnd, psInfo->strPropertiesFor, MAX_CAPTION_LENGTH);
2820
2821       PROPSHEET_CreateTabControl(hwnd, psInfo);
2822
2823       if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
2824       {
2825         if (PROPSHEET_IsTooSmallWizard(hwnd, psInfo))
2826         {
2827           PROPSHEET_AdjustSizeWizard(hwnd, psInfo);
2828           PROPSHEET_AdjustButtonsWizard(hwnd, psInfo);
2829         }
2830       }
2831       else
2832       {
2833         if (PROPSHEET_SizeMismatch(hwnd, psInfo))
2834         {
2835           PROPSHEET_AdjustSize(hwnd, psInfo);
2836           PROPSHEET_AdjustButtons(hwnd, psInfo);
2837         }
2838       }
2839
2840       if (psInfo->useCallback)
2841              (*(psInfo->ppshheader.pfnCallback))(hwnd,
2842                                               PSCB_INITIALIZED, (LPARAM)0);
2843
2844       idx = psInfo->active_page;
2845       ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[idx].hpage;
2846       psInfo->active_page = -1;
2847
2848       PROPSHEET_SetCurSel(hwnd, idx, 1, psInfo->proppage[idx].hpage);
2849
2850       /* doing TCM_SETCURSEL seems to be needed even in case of PSH_WIZARD,
2851        * as some programs call TCM_GETCURSEL to get the current selection
2852        * from which to switch to the next page */
2853       SendMessageW(hwndTabCtrl, TCM_SETCURSEL, psInfo->active_page, 0);
2854
2855       if (!HIWORD(psInfo->ppshheader.pszCaption) &&
2856               psInfo->ppshheader.hInstance)
2857       {
2858          WCHAR szText[256];
2859
2860          if (LoadStringW(psInfo->ppshheader.hInstance,
2861                  (UINT)psInfo->ppshheader.pszCaption, szText, 255))
2862             PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags, szText);
2863       }
2864       else
2865       {
2866          PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags,
2867                          psInfo->ppshheader.pszCaption);
2868       }
2869
2870       return TRUE;
2871     }
2872
2873     case WM_DESTROY:
2874       PROPSHEET_CleanUp(hwnd);
2875       return TRUE;
2876
2877     case WM_CLOSE:
2878       PROPSHEET_Cancel(hwnd, 1);
2879       return TRUE;
2880
2881     case WM_COMMAND:
2882       return PROPSHEET_DoCommand(hwnd, LOWORD(wParam));
2883
2884     case WM_NOTIFY:
2885     {
2886       NMHDR* pnmh = (LPNMHDR) lParam;
2887
2888       if (pnmh->code == TCN_SELCHANGE)
2889       {
2890         int index = SendMessageW(pnmh->hwndFrom, TCM_GETCURSEL, 0, 0);
2891         PROPSHEET_SetCurSel(hwnd, index, 1, 0);
2892       }
2893
2894       if(pnmh->code == TCN_SELCHANGING)
2895       {
2896         BOOL bRet = PROPSHEET_CanSetCurSel(hwnd);
2897         SetWindowLongW(hwnd, DWL_MSGRESULT, !bRet);
2898         return TRUE;
2899       }
2900
2901       return FALSE;
2902     }
2903
2904     case PSM_GETCURRENTPAGEHWND:
2905     {
2906       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
2907                                                         PropSheetInfoStr);
2908       HWND hwndPage = 0;
2909
2910       if (psInfo->activeValid && psInfo->active_page != -1)
2911         hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
2912
2913       SetWindowLongW(hwnd, DWL_MSGRESULT, (LONG)hwndPage);
2914
2915       return TRUE;
2916     }
2917
2918     case PSM_CHANGED:
2919       PROPSHEET_Changed(hwnd, (HWND)wParam);
2920       return TRUE;
2921
2922     case PSM_UNCHANGED:
2923       PROPSHEET_UnChanged(hwnd, (HWND)wParam);
2924       return TRUE;
2925
2926     case PSM_GETTABCONTROL:
2927     {
2928       HWND hwndTabCtrl = GetDlgItem(hwnd, IDC_TABCONTROL);
2929
2930       SetWindowLongW(hwnd, DWL_MSGRESULT, (LONG)hwndTabCtrl);
2931
2932       return TRUE;
2933     }
2934
2935     case PSM_SETCURSEL:
2936     {
2937       BOOL msgResult;
2938
2939       msgResult = PROPSHEET_CanSetCurSel(hwnd);
2940       if(msgResult != FALSE)
2941       {
2942         msgResult = PROPSHEET_SetCurSel(hwnd,
2943                                        (int)wParam,
2944                                        1,
2945                                        (HPROPSHEETPAGE)lParam);
2946       }
2947
2948       SetWindowLongW(hwnd, DWL_MSGRESULT, msgResult);
2949
2950       return TRUE;
2951     }
2952
2953     case PSM_CANCELTOCLOSE:
2954     {
2955       WCHAR buf[MAX_BUTTONTEXT_LENGTH];
2956       HWND hwndOK = GetDlgItem(hwnd, IDOK);
2957       HWND hwndCancel = GetDlgItem(hwnd, IDCANCEL);
2958
2959       EnableWindow(hwndCancel, FALSE);
2960       if (LoadStringW(COMCTL32_hModule, IDS_CLOSE, buf, sizeof(buf)))
2961          SetWindowTextW(hwndOK, buf);
2962
2963       return FALSE;
2964     }
2965
2966     case PSM_RESTARTWINDOWS:
2967     {
2968       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
2969                                                         PropSheetInfoStr);
2970
2971       psInfo->restartWindows = TRUE;
2972       return TRUE;
2973     }
2974
2975     case PSM_REBOOTSYSTEM:
2976     {
2977       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
2978                                                         PropSheetInfoStr);
2979
2980       psInfo->rebootSystem = TRUE;
2981       return TRUE;
2982     }
2983
2984     case PSM_SETTITLEA:
2985       PROPSHEET_SetTitleA(hwnd, (DWORD) wParam, (LPCSTR) lParam);
2986       return TRUE;
2987
2988     case PSM_SETTITLEW:
2989       PROPSHEET_SetTitleW(hwnd, (DWORD) wParam, (LPCWSTR) lParam);
2990       return TRUE;
2991
2992     case PSM_APPLY:
2993     {
2994       BOOL msgResult = PROPSHEET_Apply(hwnd, 0);
2995
2996       SetWindowLongW(hwnd, DWL_MSGRESULT, msgResult);
2997
2998       return TRUE;
2999     }
3000
3001     case PSM_QUERYSIBLINGS:
3002     {
3003       LRESULT msgResult = PROPSHEET_QuerySiblings(hwnd, wParam, lParam);
3004
3005       SetWindowLongW(hwnd, DWL_MSGRESULT, msgResult);
3006
3007       return TRUE;
3008     }
3009
3010     case PSM_ADDPAGE:
3011     {
3012       /*
3013        * Note: MSVC++ 6.0 documentation says that PSM_ADDPAGE does not have
3014        *       a return value. This is not true. PSM_ADDPAGE returns TRUE
3015        *       on success or FALSE otherwise, as specified on MSDN Online.
3016        *       Also see the MFC code for
3017        *       CPropertySheet::AddPage(CPropertyPage* pPage).
3018        */
3019
3020       BOOL msgResult = PROPSHEET_AddPage(hwnd, (HPROPSHEETPAGE)lParam);
3021
3022       SetWindowLongW(hwnd, DWL_MSGRESULT, msgResult);
3023
3024       return TRUE;
3025     }
3026
3027     case PSM_REMOVEPAGE:
3028       PROPSHEET_RemovePage(hwnd, (int)wParam, (HPROPSHEETPAGE)lParam);
3029       return TRUE;
3030
3031     case PSM_ISDIALOGMESSAGE:
3032     {
3033        BOOL msgResult = PROPSHEET_IsDialogMessage(hwnd, (LPMSG)lParam);
3034        SetWindowLongA(hwnd, DWL_MSGRESULT, msgResult);
3035        return TRUE;
3036     }
3037
3038     case PSM_PRESSBUTTON:
3039       PROPSHEET_PressButton(hwnd, (int)wParam);
3040       return TRUE;
3041
3042     case PSM_SETFINISHTEXTA:
3043       PROPSHEET_SetFinishTextA(hwnd, (LPCSTR) lParam);
3044       return TRUE;
3045
3046     case PSM_SETWIZBUTTONS:
3047       PROPSHEET_SetWizButtons(hwnd, (DWORD)lParam);
3048       return TRUE;
3049
3050     case PSM_SETCURSELID:
3051         PROPSHEET_SetCurSelId(hwnd, (int)lParam);
3052         return TRUE;
3053
3054     case PSM_SETFINISHTEXTW:
3055         PROPSHEET_SetFinishTextW(hwnd, (LPCWSTR) lParam);
3056         return FALSE;
3057
3058     default:
3059       return FALSE;
3060   }
3061
3062   return FALSE;
3063 }