xref: /reactos/dll/cpl/desk/appearance.c (revision c0fa4859)
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS Display Control Panel
5  * FILE:            lib/cpl/desk/appearance.c
6  * PURPOSE:         Appearance property page
7  *
8  * PROGRAMMERS:     Trevor McCort (lycan359@gmail.com)
9  *                  Timo Kreuzer (timo[dot]kreuzer[at]web[dot]de
10  */
11 
12 #include "desk.h"
13 #include "appearance.h"
14 
15 /******************************************************************************/
16 
17 /* This const assigns the color and metric numbers to the elements from the elements list */
18 
19 /* Size 1 (width)	Size 2 (height)	Color 1					Color 2							Font			Fontcolor */
20 const ASSIGNMENT g_Assignment[NUM_ELEMENTS] =
21 { {-1,				-1,				COLOR_DESKTOP,			-1,								-1,				-1},				/* -Desktop */
22   {SIZE_CAPTION_Y,	-1,				COLOR_INACTIVECAPTION,	COLOR_GRADIENTINACTIVECAPTION,	FONT_CAPTION,	-1},				/* inactive window caption */
23   {SIZE_BORDER_X,	SIZE_BORDER_Y,	COLOR_INACTIVEBORDER,	-1,								-1,				-1},  				/* inactive window border */
24   {SIZE_CAPTION_Y,	-1,				COLOR_ACTIVECAPTION,	COLOR_GRADIENTACTIVECAPTION,	FONT_CAPTION,	COLOR_CAPTIONTEXT},	/* -active window caption */
25   {SIZE_BORDER_X,	SIZE_BORDER_Y,	COLOR_ACTIVEBORDER,		-1,								-1,				-1},				/* active window border */
26   {SIZE_MENU_X,		SIZE_MENU_Y,	COLOR_MENU,				-1, 							FONT_MENU,		COLOR_MENUTEXT},	/* menu */
27   {SIZE_MENU_X,		SIZE_MENU_Y,	COLOR_HIGHLIGHT,		-1,								FONT_HILIGHT,	COLOR_HIGHLIGHTTEXT},/* marked element */
28   {-1,				-1,				COLOR_WINDOW,			-1 /*COLOR_WINDOWFRAME*/,				-1,				COLOR_WINDOWTEXT},	/* window */
29   {SIZE_SCROLL_X,	SIZE_SCROLL_Y,	COLOR_SCROLLBAR,		-1,								-1,				-1},				/* scroll bar */
30   {-1,				-1,				COLOR_3DFACE,			-1,								-1,				COLOR_BTNTEXT},		/* 3d objects */
31   {SIZE_SMCAPTION_Y,-1,				-1,						-1,								FONT_SMCAPTION,	-1},				/* palette window caption */
32   {-1,				-1,				-1,						-1,								-1,				-1},				/* symbol caption FIXME: Access? */
33   {SIZE_CAPTION_Y,	-1,				-1,						-1,								-1,				-1},				/* caption bar */
34   {-1,				-1,				-1,						-1,								-1,				COLOR_GRAYTEXT},	/* inactive menu item FIXME: Access? */
35   {-1,				-1,				-1,						-1,								FONT_DIALOG,	COLOR_WINDOWTEXT},	/* dialog */
36   {-1,				-1,				-1,						-1,								-1,				-1},				/* scrollbar controls FIXME: Access? */
37   {-1,				-1,				COLOR_APPWORKSPACE,		-1,								-1,				-1},				/* application background */
38   {-1,				-1,				-1,						-1,								-1,				-1},				/* small caption bar FIXME: Access? */
39   {SIZE_ICON_SPC_X,	-1,				-1,						-1,								-1,				-1},				/* symbol distance horiz. */
40   {SIZE_ICON_SPC_Y,	-1,				-1,						-1,								-1,				-1},				/* symbol distance vert. */
41   {-1,				-1,				COLOR_INFOBK,			-1,								FONT_INFO,		COLOR_INFOTEXT},	/* quickinfo */
42   {SIZE_ICON_X,		SIZE_ICON_Y,	-1,						-1,								FONT_ICON,		-1}};				/* symbol */
43 
44 /* This is the list of names for the colors stored in the registry */
45 const TCHAR g_RegColorNames[NUM_COLORS][MAX_COLORNAMELENGTH] =
46 	{TEXT("Scrollbar"),				/* 00 = COLOR_SCROLLBAR */
47 	TEXT("Background"),				/* 01 = COLOR_DESKTOP */
48 	TEXT("ActiveTitle"),			/* 02 = COLOR_ACTIVECAPTION  */
49 	TEXT("InactiveTitle"),			/* 03 = COLOR_INACTIVECAPTION */
50 	TEXT("Menu"),					/* 04 = COLOR_MENU */
51 	TEXT("Window"),					/* 05 = COLOR_WINDOW */
52 	TEXT("WindowFrame"),			/* 06 = COLOR_WINDOWFRAME */
53 	TEXT("MenuText"), 				/* 07 = COLOR_MENUTEXT */
54 	TEXT("WindowText"), 			/* 08 = COLOR_WINDOWTEXT */
55 	TEXT("TitleText"), 				/* 09 = COLOR_CAPTIONTEXT */
56 	TEXT("ActiveBorder"), 			/* 10 = COLOR_ACTIVEBORDER */
57 	TEXT("InactiveBorder"), 		/* 11 = COLOR_INACTIVEBORDER */
58 	TEXT("AppWorkSpace"), 			/* 12 = COLOR_APPWORKSPACE */
59 	TEXT("Hilight"), 				/* 13 = COLOR_HIGHLIGHT */
60 	TEXT("HilightText"), 			/* 14 = COLOR_HIGHLIGHTTEXT */
61 	TEXT("ButtonFace"), 			/* 15 = COLOR_BTNFACE */
62 	TEXT("ButtonShadow"), 			/* 16 = COLOR_BTNSHADOW */
63 	TEXT("GrayText"), 				/* 17 = COLOR_GRAYTEXT */
64 	TEXT("ButtonText"), 			/* 18 = COLOR_BTNTEXT */
65 	TEXT("InactiveTitleText"), 		/* 19 = COLOR_INACTIVECAPTIONTEXT */
66 	TEXT("ButtonHilight"), 			/* 20 = COLOR_BTNHIGHLIGHT */
67 	TEXT("ButtonDkShadow"), 		/* 21 = COLOR_3DDKSHADOW */
68 	TEXT("ButtonLight"), 			/* 22 = COLOR_3DLIGHT */
69 	TEXT("InfoText"), 				/* 23 = COLOR_INFOTEXT */
70 	TEXT("InfoWindow"), 			/* 24 = COLOR_INFOBK */
71 	TEXT("ButtonAlternateFace"),	/* 25 = COLOR_ALTERNATEBTNFACE */
72 	TEXT("HotTrackingColor"), 		/* 26 = COLOR_HOTLIGHT */
73 	TEXT("GradientActiveTitle"),	/* 27 = COLOR_GRADIENTACTIVECAPTION */
74 	TEXT("GradientInactiveTitle"),	/* 28 = COLOR_GRADIENTINACTIVECAPTION */
75 	TEXT("MenuHilight"), 			/* 29 = COLOR_MENUHILIGHT */
76 	TEXT("MenuBar"), 				/* 30 = COLOR_MENUBAR */
77 };
78 
79 /* This is the list of used metrics and their numbers */
80 const int g_SizeMetric[NUM_SIZES] =
81 {
82 	SM_CXBORDER,
83 	SM_CYBORDER,
84 	SM_CYCAPTION,
85 	SM_CXICON,
86 	SM_CYICON,
87 	SM_CXICONSPACING,
88 	SM_CYICONSPACING,
89 	SM_CXMENUSIZE,
90 	SM_CYMENU,
91 	SM_CXVSCROLL,
92 	SM_CYHSCROLL,
93 	SM_CYSMCAPTION
94 };
95 
96 /******************************************************************************/
97 
98 static VOID
99 LoadCurrentTheme(GLOBALS* g)
100 {
101 	INT i;
102 	NONCLIENTMETRICS NonClientMetrics;
103 
104 	g->Theme.bHasChanged = FALSE;
105 	/* FIXME: it may be custom! */
106 	g->Theme.bIsCustom = FALSE;
107 
108 	/* Load colors */
109 	for (i = 0; i <= 30; i++)
110 	{
111 		g->ColorList[i] = i;
112 		g->Theme.crColor[i] = (COLORREF)GetSysColor(i);
113 	}
114 
115 	/* Load sizes */
116 	for (i = 0; i <= 11; i++)
117 	{
118 		g->Theme.Size[i] = GetSystemMetrics(g_SizeMetric[i]);
119 	}
120 
121 	/* Load fonts */
122 	NonClientMetrics.cbSize = sizeof(NONCLIENTMETRICS);
123 	SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &NonClientMetrics, 0);
124 	g->Theme.lfFont[FONT_CAPTION] = NonClientMetrics.lfCaptionFont;
125 	g->Theme.lfFont[FONT_SMCAPTION] = NonClientMetrics.lfSmCaptionFont;
126 	g->Theme.lfFont[FONT_MENU] = NonClientMetrics.lfMenuFont;
127 	g->Theme.lfFont[FONT_INFO] = NonClientMetrics.lfStatusFont;
128 	g->Theme.lfFont[FONT_DIALOG] = NonClientMetrics.lfMessageFont;
129 	SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(LOGFONT), &g->Theme.lfFont[FONT_ICON], 0);
130 }
131 
132 
133 static BOOL
134 LoadThemeFromReg(GLOBALS* g, INT iPreset)
135 {
136 	INT i;
137 	TCHAR strSizeName[20] = {TEXT("Sizes\\0")};
138 	TCHAR strValueName[10];
139 	HKEY hkNewSchemes, hkScheme, hkSize;
140 	DWORD dwType, dwLength;
141 	BOOL Ret = FALSE;
142 
143 	if(RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Appearance\\New Schemes"),
144 		0, KEY_READ, &hkNewSchemes) == ERROR_SUCCESS)
145 	{
146 		if(RegOpenKeyEx(hkNewSchemes, g->ThemeTemplates[iPreset].strKeyName, 0, KEY_READ, &hkScheme) == ERROR_SUCCESS)
147 		{
148 			lstrcpyn(&strSizeName[6],g->ThemeTemplates[iPreset].strSizeName, 3);
149 			if(RegOpenKeyEx(hkScheme, strSizeName, 0, KEY_READ, &hkSize) == ERROR_SUCCESS)
150 			{
151 				Ret = TRUE;
152 
153 				dwLength = sizeof(DWORD);
154 				if (RegQueryValueEx(hkSize, TEXT("FlatMenus"), NULL, &dwType, (LPBYTE)&g->Theme.bFlatMenus, &dwLength) != ERROR_SUCCESS ||
155 				    dwType != REG_DWORD || dwLength != sizeof(DWORD))
156 				{
157 					/* Failed to read registry value */
158 					g->Theme.bFlatMenus = FALSE;
159 					Ret = FALSE;
160 				}
161 
162 				for (i = 0; i <= 30; i++)
163 				{
164 					wsprintf(strValueName, TEXT("Color #%d"), i);
165 					dwLength = sizeof(COLORREF);
166 					if (RegQueryValueEx(hkSize, strValueName, NULL, &dwType, (LPBYTE)&g->Theme.crColor[i], &dwLength) != ERROR_SUCCESS ||
167 					    dwType != REG_DWORD || dwLength != sizeof(COLORREF))
168 					{
169 						/* Failed to read registry value, initialize with current setting for now */
170 						g->Theme.crColor[i] = GetSysColor(i);
171 						Ret = FALSE;
172 					}
173 				}
174 				for (i = 0; i <= 5; i++)
175 				{
176 					wsprintf(strValueName, TEXT("Font #%d"), i);
177 					dwLength = sizeof(LOGFONT);
178 					g->Theme.lfFont[i].lfFaceName[0] = 'x';
179 					if (RegQueryValueEx(hkSize, strValueName, NULL, &dwType, (LPBYTE)&g->Theme.lfFont[i], &dwLength) != ERROR_SUCCESS ||
180 					    dwType != REG_BINARY || dwLength != sizeof(LOGFONT))
181 					{
182 						/* Failed to read registry value */
183 						Ret = FALSE;
184 					}
185 				}
186 				for (i = 0; i <= 8; i++)
187 				{
188 					wsprintf(strValueName, TEXT("Size #%d"), i);
189 					dwLength = sizeof(UINT64);
190 					if (RegQueryValueEx(hkSize, strValueName, NULL, &dwType, (LPBYTE)&g->Theme.Size[i], &dwLength) != ERROR_SUCCESS ||
191 					    dwType != REG_QWORD || dwLength != sizeof(UINT64))
192 					{
193 						/* Failed to read registry value, initialize with current setting for now */
194 						g->Theme.Size[i] = GetSystemMetrics(g_SizeMetric[i]);
195 						Ret = FALSE;
196 					}
197 				}
198 				RegCloseKey(hkScheme);
199 			}
200 			RegCloseKey(hkScheme);
201 		}
202 		RegCloseKey(hkNewSchemes);
203 	}
204 
205 	return Ret;
206 }
207 
208 
209 static VOID
210 ApplyTheme(GLOBALS* g)
211 {
212 	INT i, Result;
213 	HKEY hKey;
214 	DWORD dwDisposition = 0;
215 	TCHAR clText[16] = {0};
216 	NONCLIENTMETRICS NonClientMetrics;
217 	HFONT hMyFont;
218 	LOGFONT lfButtonFont;
219 
220 	if (!g->Theme.bHasChanged)
221 		return;
222 
223 	g->Theme.bHasChanged = FALSE;
224 
225 	/* Update some globals */
226 	g->crCOLOR_BTNFACE = g->Theme.crColor[COLOR_BTNFACE];
227 	g->crCOLOR_BTNTEXT = g->Theme.crColor[COLOR_BTNTEXT];
228 	g->crCOLOR_BTNSHADOW = g->Theme.crColor[COLOR_BTNSHADOW];
229 	g->crCOLOR_BTNHIGHLIGHT = g->Theme.crColor[COLOR_BTNHIGHLIGHT];
230 	lfButtonFont = g->Theme.lfFont[FONT_DIALOG];
231 
232 	/* Create new font for bold button */
233 	lfButtonFont.lfWeight = FW_BOLD;
234 	lfButtonFont.lfItalic = FALSE;
235 	hMyFont = CreateFontIndirect(&lfButtonFont);
236 	if (hMyFont)
237 	{
238 		if (g->hBoldFont)
239 			DeleteObject(g->hBoldFont);
240 		g->hBoldFont = hMyFont;
241 	}
242 
243 	/* Create new font for italic button */
244 	lfButtonFont.lfWeight = FW_REGULAR;
245 	lfButtonFont.lfItalic = TRUE;
246 	hMyFont = CreateFontIndirect(&lfButtonFont);
247 	if (hMyFont)
248 	{
249 		if (g->hItalicFont)
250 			DeleteObject(g->hItalicFont);
251 		g->hItalicFont = hMyFont;
252 	}
253 
254 	/* Apply Colors from global variable */
255 	SetSysColors(30, &g->ColorList[0], &g->Theme.crColor[0]);
256 
257 	/* Save colors to registry */
258 	Result = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Colors"), 0, KEY_ALL_ACCESS, &hKey);
259 	if (Result != ERROR_SUCCESS)
260 	{
261 		/* Could not open the key, try to create it */
262 		Result = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Colors"), 0, NULL, 0, KEY_ALL_ACCESS, NULL,&hKey, &dwDisposition);
263 	}
264 
265 	if (Result == ERROR_SUCCESS)
266 	{
267 		for (i = 0; i <= 30; i++)
268 		{
269 			DWORD red   = GetRValue(g->Theme.crColor[i]);
270 			DWORD green = GetGValue(g->Theme.crColor[i]);
271 			DWORD blue  = GetBValue(g->Theme.crColor[i]);
272 			wsprintf(clText, TEXT("%d %d %d"), red, green, blue);
273 			RegSetValueEx(hKey, g_RegColorNames[i], 0, REG_SZ, (BYTE *)clText, lstrlen( clText )*sizeof(TCHAR) + sizeof(TCHAR));
274 		}
275 
276 		RegCloseKey(hKey);
277 	}
278 
279 	/* Apply the fonts */
280 	NonClientMetrics.cbSize = sizeof(NONCLIENTMETRICS);
281 	SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &NonClientMetrics, 0);
282 	NonClientMetrics.lfCaptionFont = g->Theme.lfFont[FONT_CAPTION];
283 	NonClientMetrics.lfSmCaptionFont = g->Theme.lfFont[FONT_SMCAPTION];
284 	NonClientMetrics.lfMenuFont = g->Theme.lfFont[FONT_MENU];
285 	NonClientMetrics.lfStatusFont = g->Theme.lfFont[FONT_INFO];
286 	NonClientMetrics.lfMessageFont = g->Theme.lfFont[FONT_DIALOG];
287 	SystemParametersInfo(SPI_SETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &NonClientMetrics, 0);
288 	SystemParametersInfo(SPI_SETICONTITLELOGFONT, sizeof(LOGFONT), &g->Theme.lfFont[FONT_ICON], 0);
289 
290 	/* FIXME: Apply size metrics */
291 
292 	/* Save fonts and size metrics to registry */
293 	Result = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop\\WindowMetrics"), 0, KEY_ALL_ACCESS, &hKey);
294 	if (Result != ERROR_SUCCESS)
295 	{
296 		/* Could not open the key, try to create it */
297 		Result = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Desktop\\WindowMetrics"), 0, NULL, 0, KEY_ALL_ACCESS, NULL,&hKey, &dwDisposition);
298 	}
299 
300 	if (Result == ERROR_SUCCESS)
301 	{
302 		RegSetValueEx(hKey, TEXT("CaptionFont"), 0, REG_BINARY, (BYTE *)&g->Theme.lfFont[FONT_CAPTION], sizeof(LOGFONT));
303 		RegSetValueEx(hKey, TEXT("SmCaptionFont"), 0, REG_BINARY, (BYTE *)&g->Theme.lfFont[FONT_SMCAPTION], sizeof(LOGFONT));
304 		RegSetValueEx(hKey, TEXT("IconFont"), 0, REG_BINARY, (BYTE *)&g->Theme.lfFont[FONT_ICON], sizeof(LOGFONT));
305 		RegSetValueEx(hKey, TEXT("MenuFont"), 0, REG_BINARY, (BYTE *)&g->Theme.lfFont[FONT_MENU], sizeof(LOGFONT));
306 		RegSetValueEx(hKey, TEXT("StatusFont"), 0, REG_BINARY, (BYTE *)&g->Theme.lfFont[FONT_INFO], sizeof(LOGFONT));
307 		RegSetValueEx(hKey, TEXT("MessageFont"), 0, REG_BINARY, (BYTE *)&g->Theme.lfFont[FONT_DIALOG], sizeof(LOGFONT));
308 
309 		/* Save size metrics to registry */
310 		wsprintf(clText, TEXT("%d"), -15 * g->Theme.Size[SIZE_BORDER_X]);
311 		RegSetValueEx(hKey, TEXT("BorderWidth"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
312 		wsprintf(clText, TEXT("%d"), -15 * g->Theme.Size[SIZE_CAPTION_Y]);
313 		RegSetValueEx(hKey, TEXT("CaptionWidth"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
314 		wsprintf(clText, TEXT("%d"), -15 * g->Theme.Size[SIZE_CAPTION_Y]);
315 		RegSetValueEx(hKey, TEXT("CaptionHeight"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
316 		wsprintf(clText, TEXT("%d"), -15 * g->Theme.Size[SIZE_SMCAPTION_Y]);
317 		RegSetValueEx(hKey, TEXT("SmCaptionWidth"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
318 		wsprintf(clText, TEXT("%d"), -15 * g->Theme.Size[SIZE_SMCAPTION_Y]);
319 		RegSetValueEx(hKey, TEXT("SmCaptionHeight"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
320 		wsprintf(clText, TEXT("%d"), -15 * g->Theme.Size[SIZE_ICON_SPC_X]);
321 		RegSetValueEx(hKey, TEXT("IconSpacing"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
322 		wsprintf(clText, TEXT("%d"), -15 * g->Theme.Size[SIZE_ICON_SPC_Y]);
323 		RegSetValueEx(hKey, TEXT("IconVerticalSpacing"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
324 		wsprintf(clText, TEXT("%d"), -15 * g->Theme.Size[SIZE_MENU_X]);
325 		RegSetValueEx(hKey, TEXT("MenuWidth"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
326 		wsprintf(clText, TEXT("%d"), -15 * g->Theme.Size[SIZE_MENU_Y]);
327 		RegSetValueEx(hKey, TEXT("MenuHeight"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
328 		wsprintf(clText, TEXT("%d"), -15 * g->Theme.Size[SIZE_SCROLL_X]);
329 		RegSetValueEx(hKey, TEXT("ScrollWidth"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
330 		wsprintf(clText, TEXT("%d"), -15 * g->Theme.Size[SIZE_SCROLL_Y]);
331 		RegSetValueEx(hKey, TEXT("ScrollHeight"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
332 		wsprintf(clText, TEXT("%d"), g->Theme.Size[SIZE_ICON_X]);
333 		RegSetValueEx(hKey, TEXT("Shell Icon Sizet"), 0, REG_SZ, (BYTE *)clText, sizeof(clText));
334 
335 		RegCloseKey(hKey);
336 	}
337 }
338 
339 
340 static INT_PTR
341 AppearancePage_OnInit(HWND hwndDlg, GLOBALS *g)
342 {
343 	HKEY hkNewSchemes, hkScheme, hkSizes, hkSize;
344 	FILETIME ftLastWriteTime;
345 	TCHAR strSelectedStyle[4];
346 	DWORD dwLength, dwType;
347 	DWORD dwDisposition = 0;
348 	INT iStyle, iSize, iTemplateIndex, iListIndex = 0;
349 	INT Result;
350 
351 	g = (GLOBALS*)malloc(sizeof(GLOBALS));
352 	if (g == NULL)
353 	{
354 		return FALSE;
355 	}
356 
357 	SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)g);
358 
359 	LoadCurrentTheme(g);
360 
361 	/* Fill color schemes combo */
362 	Result = RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Appearance\\New Schemes"),
363 		0, KEY_READ, &hkNewSchemes);
364 	if (Result != ERROR_SUCCESS)
365 	{
366 		/* Could not open the key, try to create it */
367 		Result = RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Control Panel\\Appearance\\New Schemes"), 0, NULL, 0, KEY_ALL_ACCESS, NULL,&hkNewSchemes, &dwDisposition);
368 		if (Result == ERROR_SUCCESS)
369 		{
370 			/* FIXME: We have created it new, so let's put somethig there */
371 		}
372 	}
373 	if (Result == ERROR_SUCCESS)
374 	{
375 		/* First find out the currently selected template */
376 		dwLength = 8;
377 		RegQueryValueEx(hkNewSchemes, TEXT("SelectedStyle"), NULL, &dwType, (LPBYTE)&strSelectedStyle, &dwLength);
378 		iTemplateIndex = 0;
379 		iStyle = 0;
380 		dwLength = MAX_TEMPLATENAMELENTGH;
381 		while((RegEnumKeyEx(hkNewSchemes, iStyle, g->ThemeTemplates[iTemplateIndex].strKeyName, &dwLength,
382 			NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS) && (iTemplateIndex < MAX_TEMPLATES))
383 		{
384 			/* is it really a template or one of the other entries */
385 			if (dwLength < 5)
386 			{
387 				if (RegOpenKeyEx(hkNewSchemes, g->ThemeTemplates[iTemplateIndex].strKeyName, 0, KEY_READ, &hkScheme) == ERROR_SUCCESS)
388 				{
389 					if(RegOpenKeyEx(hkScheme, TEXT("Sizes"), 0, KEY_READ, &hkSizes) == ERROR_SUCCESS)
390 					{
391 						iSize = 0;
392 						dwLength = 3;
393 						while((RegEnumKeyEx(hkSizes, iSize, g->ThemeTemplates[iTemplateIndex].strSizeName, &dwLength,
394 							NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS) && (iSize <= 4))
395 						{
396 							if(RegOpenKeyEx(hkSizes, g->ThemeTemplates[iTemplateIndex].strSizeName, 0, KEY_READ, &hkSize) == ERROR_SUCCESS)
397 							{
398 								dwLength = MAX_TEMPLATENAMELENTGH;
399 								RegQueryValueEx(hkSize, TEXT("DisplayName"), NULL, &dwType, (LPBYTE)&g->ThemeTemplates[iTemplateIndex].strDisplayName, &dwLength);
400 								dwLength = MAX_TEMPLATENAMELENTGH;
401 								RegQueryValueEx(hkSize, TEXT("LegacyName"), NULL, &dwType, (LPBYTE)&g->ThemeTemplates[iTemplateIndex].strLegacyName, &dwLength);
402 								RegCloseKey(hkSize);
403 							}
404 							iListIndex = SendDlgItemMessage(hwndDlg, IDC_APPEARANCE_COLORSCHEME, CB_ADDSTRING, 0, (LPARAM)g->ThemeTemplates[iTemplateIndex].strLegacyName);
405 							SendDlgItemMessage(hwndDlg, IDC_APPEARANCE_COLORSCHEME, CB_SETITEMDATA, iListIndex, iTemplateIndex);
406 							if (lstrcmp(g->ThemeTemplates[iTemplateIndex].strKeyName, strSelectedStyle) == 0)
407 							{
408 								SendDlgItemMessage(hwndDlg, IDC_APPEARANCE_COLORSCHEME, CB_SETCURSEL, (WPARAM)iListIndex, 0);
409 							}
410 							iSize++;
411 							iTemplateIndex++;
412 							dwLength = 3;
413 						}
414 						RegCloseKey(hkSizes);
415 					}
416 					RegCloseKey(hkScheme);
417 				}
418 			}
419 			iStyle++;
420 			dwLength = MAX_TEMPLATENAMELENTGH;
421 		}
422 		RegCloseKey(hkNewSchemes);
423 	}
424 	SendMessage(GetDlgItem(hwndDlg, IDC_APPEARANCE_COLORSCHEME), LB_SETCURSEL, 0, 0);
425 
426 	return FALSE;
427 }
428 
429 
430 static INT_PTR
431 AppearancePage_OnDestroy(HWND hwndDlg, GLOBALS *g)
432 {
433 	free(g);
434 	return TRUE;
435 }
436 
437 
438 INT_PTR CALLBACK
439 AppearancePageProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
440 {
441 	INT i, index;
442 	GLOBALS *g;
443 	LPNMHDR lpnm;
444 
445 	g = (GLOBALS*)GetWindowLongPtr(hwndDlg, DWLP_USER);
446 
447 	switch (uMsg)
448 	{
449 		case WM_INITDIALOG:
450 			return AppearancePage_OnInit(hwndDlg, g);
451 
452 		case WM_DESTROY:
453 			return AppearancePage_OnDestroy(hwndDlg, g);
454 
455 		case WM_COMMAND:
456 			switch (LOWORD(wParam))
457 			{
458 				case IDC_APPEARANCE_ADVANCED:
459 					DialogBoxParam(hApplet, (LPCTSTR)IDD_ADVAPPEARANCE,
460 						hwndDlg, AdvAppearanceDlgProc, (LPARAM)g);
461 
462 					/* Was anything changed in the advanced appearance dialog? */
463 					if (memcmp(&g->Theme, &g->ThemeAdv, sizeof(THEME)) != 0)
464 					{
465 						PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
466 						g->Theme = g->ThemeAdv;
467 						g->Theme.bHasChanged = TRUE;
468 					}
469 					break;
470 
471 				case IDC_APPEARANCE_COLORSCHEME:
472 					if(HIWORD(wParam) == CBN_SELCHANGE)
473 					{
474 						PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
475 						g->Theme.bHasChanged = TRUE;
476 						i = SendDlgItemMessage(hwndDlg, IDC_APPEARANCE_COLORSCHEME, CB_GETCURSEL, 0, 0);
477 						index = SendDlgItemMessage(hwndDlg, IDC_APPEARANCE_COLORSCHEME, CB_GETITEMDATA, (WPARAM)i, 0);
478 						LoadThemeFromReg(g, index);
479 					}
480 					break;
481 
482 				default:
483 					return FALSE;
484 			}
485 			return TRUE;
486 
487 		case WM_NOTIFY:
488 			lpnm = (LPNMHDR)lParam;
489 			switch (lpnm->code)
490 			{
491 				case PSN_APPLY:
492 					if (g->Theme.bHasChanged)
493 					{
494 						ApplyTheme(g);
495 					}
496 					return TRUE;
497 
498 				default:
499 					return FALSE;
500 			}
501 			return TRUE;
502 
503 		default:
504 			return FALSE;
505 	}
506 
507 	return TRUE;
508 }
509