~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/comctl32/propsheet.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

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