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