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