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