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