user32/tests: Check the correct variable for the correct value.
[wine] / programs / winecfg / theme.c
1 /*
2  * Desktop Integration
3  * - Theme configuration code
4  * - User Shell Folder mapping
5  *
6  * Copyright (c) 2005 by Frank Richter
7  * Copyright (c) 2006 by Michael Jung
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  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #ifdef HAVE_SYS_STAT_H
32 #include <sys/stat.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #ifdef HAVE_DIRECT_H
38 #include <direct.h>
39 #endif
40
41 #define COBJMACROS
42
43 #include <windows.h>
44 #include <commdlg.h>
45 #include <shellapi.h>
46 #include <uxtheme.h>
47 #include <tmschema.h>
48 #include <shlobj.h>
49 #include <shlwapi.h>
50 #include <wine/debug.h>
51 #include <wine/unicode.h>
52
53 #include "resource.h"
54 #include "winecfg.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
57
58 /* UXTHEME functions not in the headers */
59
60 typedef struct tagTHEMENAMES
61 {
62     WCHAR szName[MAX_PATH+1];
63     WCHAR szDisplayName[MAX_PATH+1];
64     WCHAR szTooltip[MAX_PATH+1];
65 } THEMENAMES, *PTHEMENAMES;
66
67 typedef void* HTHEMEFILE;
68 typedef BOOL (CALLBACK *EnumThemeProc)(LPVOID lpReserved, 
69                                        LPCWSTR pszThemeFileName,
70                                        LPCWSTR pszThemeName, 
71                                        LPCWSTR pszToolTip, LPVOID lpReserved2,
72                                        LPVOID lpData);
73
74 HRESULT WINAPI EnumThemeColors (LPCWSTR pszThemeFileName, LPWSTR pszSizeName,
75                                 DWORD dwColorNum, PTHEMENAMES pszColorNames);
76 HRESULT WINAPI EnumThemeSizes (LPCWSTR pszThemeFileName, LPWSTR pszColorName,
77                                DWORD dwSizeNum, PTHEMENAMES pszSizeNames);
78 HRESULT WINAPI ApplyTheme (HTHEMEFILE hThemeFile, char* unknown, HWND hWnd);
79 HRESULT WINAPI OpenThemeFile (LPCWSTR pszThemeFileName, LPCWSTR pszColorName,
80                               LPCWSTR pszSizeName, HTHEMEFILE* hThemeFile,
81                               DWORD unknown);
82 HRESULT WINAPI CloseThemeFile (HTHEMEFILE hThemeFile);
83 HRESULT WINAPI EnumThemes (LPCWSTR pszThemePath, EnumThemeProc callback,
84                            LPVOID lpData);
85
86 /* A struct to keep both the internal and "fancy" name of a color or size */
87 typedef struct
88 {
89   WCHAR* name;
90   WCHAR* fancyName;
91 } ThemeColorOrSize;
92
93 /* wrapper around DSA that also keeps an item count */
94 typedef struct
95 {
96   HDSA dsa;
97   int count;
98 } WrappedDsa;
99
100 /* Some helper functions to deal with ThemeColorOrSize structs in WrappedDSAs */
101
102 static void color_or_size_dsa_add (WrappedDsa* wdsa, const WCHAR* name,
103                                    const WCHAR* fancyName)
104 {
105     ThemeColorOrSize item;
106     
107     item.name = HeapAlloc (GetProcessHeap(), 0, 
108         (lstrlenW (name) + 1) * sizeof(WCHAR));
109     lstrcpyW (item.name, name);
110
111     item.fancyName = HeapAlloc (GetProcessHeap(), 0, 
112         (lstrlenW (fancyName) + 1) * sizeof(WCHAR));
113     lstrcpyW (item.fancyName, fancyName);
114
115     DSA_InsertItem (wdsa->dsa, wdsa->count, &item);
116     wdsa->count++;
117 }
118
119 static int CALLBACK dsa_destroy_callback (LPVOID p, LPVOID pData)
120 {
121     ThemeColorOrSize* item = p;
122     HeapFree (GetProcessHeap(), 0, item->name);
123     HeapFree (GetProcessHeap(), 0, item->fancyName);
124     return 1;
125 }
126
127 static void free_color_or_size_dsa (WrappedDsa* wdsa)
128 {
129     DSA_DestroyCallback (wdsa->dsa, dsa_destroy_callback, NULL);
130 }
131
132 static void create_color_or_size_dsa (WrappedDsa* wdsa)
133 {
134     wdsa->dsa = DSA_Create (sizeof (ThemeColorOrSize), 1);
135     wdsa->count = 0;
136 }
137
138 static ThemeColorOrSize* color_or_size_dsa_get (WrappedDsa* wdsa, int index)
139 {
140     return DSA_GetItemPtr (wdsa->dsa, index);
141 }
142
143 static int color_or_size_dsa_find (WrappedDsa* wdsa, const WCHAR* name)
144 {
145     int i = 0;
146     for (; i < wdsa->count; i++)
147     {
148         ThemeColorOrSize* item = color_or_size_dsa_get (wdsa, i);
149         if (lstrcmpiW (item->name, name) == 0) break;
150     }
151     return i;
152 }
153
154 /* A theme file, contains file name, display name, color and size scheme names */
155 typedef struct
156 {
157     WCHAR* themeFileName;
158     WCHAR* fancyName;
159     WrappedDsa colors;
160     WrappedDsa sizes;
161 } ThemeFile;
162
163 static HDSA themeFiles = NULL;
164 static int themeFilesCount = 0;
165
166 static int CALLBACK theme_dsa_destroy_callback (LPVOID p, LPVOID pData)
167 {
168     ThemeFile* item = p;
169     HeapFree (GetProcessHeap(), 0, item->themeFileName);
170     HeapFree (GetProcessHeap(), 0, item->fancyName);
171     free_color_or_size_dsa (&item->colors);
172     free_color_or_size_dsa (&item->sizes);
173     return 1;
174 }
175
176 /* Free memory occupied by the theme list */
177 static void free_theme_files(void)
178 {
179     if (themeFiles == NULL) return;
180       
181     DSA_DestroyCallback (themeFiles , theme_dsa_destroy_callback, NULL);
182     themeFiles = NULL;
183     themeFilesCount = 0;
184 }
185
186 typedef HRESULT (WINAPI * EnumTheme) (LPCWSTR, LPWSTR, DWORD, PTHEMENAMES);
187
188 /* fill a string list with either colors or sizes of a theme */
189 static void fill_theme_string_array (const WCHAR* filename, 
190                                      WrappedDsa* wdsa,
191                                      EnumTheme enumTheme)
192 {
193     DWORD index = 0;
194     THEMENAMES names;
195
196     WINE_TRACE ("%s %p %p\n", wine_dbgstr_w (filename), wdsa, enumTheme);
197
198     while (SUCCEEDED (enumTheme (filename, NULL, index++, &names)))
199     {
200         WINE_TRACE ("%s: %s\n", wine_dbgstr_w (names.szName), 
201             wine_dbgstr_w (names.szDisplayName));
202         color_or_size_dsa_add (wdsa, names.szName, names.szDisplayName);
203     }
204 }
205
206 /* Theme enumeration callback, adds theme to theme list */
207 static BOOL CALLBACK myEnumThemeProc (LPVOID lpReserved, 
208                                       LPCWSTR pszThemeFileName,
209                                       LPCWSTR pszThemeName, 
210                                       LPCWSTR pszToolTip, 
211                                       LPVOID lpReserved2, LPVOID lpData)
212 {
213     ThemeFile newEntry;
214
215     /* fill size/color lists */
216     create_color_or_size_dsa (&newEntry.colors);
217     fill_theme_string_array (pszThemeFileName, &newEntry.colors, EnumThemeColors);
218     create_color_or_size_dsa (&newEntry.sizes);
219     fill_theme_string_array (pszThemeFileName, &newEntry.sizes, EnumThemeSizes);
220
221     newEntry.themeFileName = HeapAlloc (GetProcessHeap(), 0, 
222         (lstrlenW (pszThemeFileName) + 1) * sizeof(WCHAR));
223     lstrcpyW (newEntry.themeFileName, pszThemeFileName);
224   
225     newEntry.fancyName = HeapAlloc (GetProcessHeap(), 0, 
226         (lstrlenW (pszThemeName) + 1) * sizeof(WCHAR));
227     lstrcpyW (newEntry.fancyName, pszThemeName);
228   
229     /*list_add_tail (&themeFiles, &newEntry->entry);*/
230     DSA_InsertItem (themeFiles, themeFilesCount, &newEntry);
231     themeFilesCount++;
232
233     return TRUE;
234 }
235
236 /* Scan for themes */
237 static void scan_theme_files(void)
238 {
239     static const WCHAR themesSubdir[] = { '\\','T','h','e','m','e','s',0 };
240     WCHAR themesPath[MAX_PATH];
241
242     free_theme_files();
243
244     if (FAILED (SHGetFolderPathW (NULL, CSIDL_RESOURCES, NULL, 
245         SHGFP_TYPE_CURRENT, themesPath))) return;
246
247     themeFiles = DSA_Create (sizeof (ThemeFile), 1);
248     lstrcatW (themesPath, themesSubdir);
249
250     EnumThemes (themesPath, myEnumThemeProc, 0);
251 }
252
253 /* fill the color & size combo boxes for a given theme */
254 static void fill_color_size_combos (ThemeFile* theme, HWND comboColor, 
255                                     HWND comboSize)
256 {
257     int i;
258
259     SendMessageW (comboColor, CB_RESETCONTENT, 0, 0);
260     for (i = 0; i < theme->colors.count; i++)
261     {
262         ThemeColorOrSize* item = color_or_size_dsa_get (&theme->colors, i);
263         SendMessageW (comboColor, CB_ADDSTRING, 0, (LPARAM)item->fancyName);
264     }
265
266     SendMessageW (comboSize, CB_RESETCONTENT, 0, 0);
267     for (i = 0; i < theme->sizes.count; i++)
268     {
269         ThemeColorOrSize* item = color_or_size_dsa_get (&theme->sizes, i);
270         SendMessageW (comboSize, CB_ADDSTRING, 0, (LPARAM)item->fancyName);
271     }
272 }
273
274 /* Select the item of a combo box that matches a theme's color and size 
275  * scheme. */
276 static void select_color_and_size (ThemeFile* theme, 
277                             const WCHAR* colorName, HWND comboColor, 
278                             const WCHAR* sizeName, HWND comboSize)
279 {
280     SendMessageW (comboColor, CB_SETCURSEL, 
281         color_or_size_dsa_find (&theme->colors, colorName), 0);
282     SendMessageW (comboSize, CB_SETCURSEL, 
283         color_or_size_dsa_find (&theme->sizes, sizeName), 0);
284 }
285
286 /* Fill theme, color and sizes combo boxes with the know themes and select
287  * the entries matching the currently active theme. */
288 static BOOL fill_theme_list (HWND comboTheme, HWND comboColor, HWND comboSize)
289 {
290     WCHAR textNoTheme[256];
291     int themeIndex = 0;
292     BOOL ret = TRUE;
293     int i;
294     WCHAR currentTheme[MAX_PATH];
295     WCHAR currentColor[MAX_PATH];
296     WCHAR currentSize[MAX_PATH];
297     ThemeFile* theme = NULL;
298
299     LoadStringW (GetModuleHandleW(NULL), IDS_NOTHEME, textNoTheme,
300         sizeof(textNoTheme) / sizeof(WCHAR));
301
302     SendMessageW (comboTheme, CB_RESETCONTENT, 0, 0);
303     SendMessageW (comboTheme, CB_ADDSTRING, 0, (LPARAM)textNoTheme);
304
305     for (i = 0; i < themeFilesCount; i++)
306     {
307         ThemeFile* item = DSA_GetItemPtr (themeFiles, i);
308         SendMessageW (comboTheme, CB_ADDSTRING, 0, 
309             (LPARAM)item->fancyName);
310     }
311   
312     if (IsThemeActive () && SUCCEEDED (GetCurrentThemeName (currentTheme, 
313             sizeof(currentTheme) / sizeof(WCHAR),
314             currentColor, sizeof(currentColor) / sizeof(WCHAR),
315             currentSize, sizeof(currentSize) / sizeof(WCHAR))))
316     {
317         /* Determine the index of the currently active theme. */
318         BOOL found = FALSE;
319         for (i = 0; i < themeFilesCount; i++)
320         {
321             theme = DSA_GetItemPtr (themeFiles, i);
322             if (lstrcmpiW (theme->themeFileName, currentTheme) == 0)
323             {
324                 found = TRUE;
325                 themeIndex = i+1;
326                 break;
327             }
328         }
329         if (!found)
330         {
331             /* Current theme not found?... add to the list, then... */
332             WINE_TRACE("Theme %s not in list of enumerated themes\n",
333                 wine_dbgstr_w (currentTheme));
334             myEnumThemeProc (NULL, currentTheme, currentTheme, 
335                 currentTheme, NULL, NULL);
336             themeIndex = themeFilesCount;
337             theme = DSA_GetItemPtr (themeFiles, themeFilesCount-1);
338         }
339         fill_color_size_combos (theme, comboColor, comboSize);
340         select_color_and_size (theme, currentColor, comboColor,
341             currentSize, comboSize);
342     }
343     else
344     {
345         /* No theme selected */
346         ret = FALSE;
347     }
348
349     SendMessageW (comboTheme, CB_SETCURSEL, themeIndex, 0);
350     return ret;
351 }
352
353 /* Update the color & size combo boxes when the selection of the theme
354  * combo changed. Selects the current color and size scheme if the theme
355  * is currently active, otherwise the first color and size. */
356 static BOOL update_color_and_size (int themeIndex, HWND comboColor, 
357                                    HWND comboSize)
358 {
359     if (themeIndex == 0)
360     {
361         return FALSE;
362     }
363     else
364     {
365         WCHAR currentTheme[MAX_PATH];
366         WCHAR currentColor[MAX_PATH];
367         WCHAR currentSize[MAX_PATH];
368         ThemeFile* theme = DSA_GetItemPtr (themeFiles, themeIndex - 1);
369     
370         fill_color_size_combos (theme, comboColor, comboSize);
371       
372         if ((SUCCEEDED (GetCurrentThemeName (currentTheme, 
373             sizeof(currentTheme) / sizeof(WCHAR),
374             currentColor, sizeof(currentColor) / sizeof(WCHAR),
375             currentSize, sizeof(currentSize) / sizeof(WCHAR))))
376             && (lstrcmpiW (currentTheme, theme->themeFileName) == 0))
377         {
378             select_color_and_size (theme, currentColor, comboColor,
379                 currentSize, comboSize);
380         }
381         else
382         {
383             SendMessageW (comboColor, CB_SETCURSEL, 0, 0);
384             SendMessageW (comboSize, CB_SETCURSEL, 0, 0);
385         }
386     }
387     return TRUE;
388 }
389
390 /* Apply a theme from a given theme, color and size combo box item index. */
391 static void do_apply_theme (int themeIndex, int colorIndex, int sizeIndex)
392 {
393     static char b[] = "\0";
394
395     if (themeIndex == 0)
396     {
397         /* no theme */
398         ApplyTheme (NULL, b, NULL);
399     }
400     else
401     {
402         ThemeFile* theme = DSA_GetItemPtr (themeFiles, themeIndex-1);
403         const WCHAR* themeFileName = theme->themeFileName;
404         const WCHAR* colorName = NULL;
405         const WCHAR* sizeName = NULL;
406         HTHEMEFILE hTheme;
407         ThemeColorOrSize* item;
408     
409         item = color_or_size_dsa_get (&theme->colors, colorIndex);
410         colorName = item->name;
411         
412         item = color_or_size_dsa_get (&theme->sizes, sizeIndex);
413         sizeName = item->name;
414         
415         if (SUCCEEDED (OpenThemeFile (themeFileName, colorName, sizeName,
416             &hTheme, 0)))
417         {
418             ApplyTheme (hTheme, b, NULL);
419             CloseThemeFile (hTheme);
420         }
421         else
422         {
423             ApplyTheme (NULL, b, NULL);
424         }
425     }
426 }
427
428 static int updating_ui;
429 static BOOL theme_dirty;
430
431 static void enable_size_and_color_controls (HWND dialog, BOOL enable)
432 {
433     EnableWindow (GetDlgItem (dialog, IDC_THEME_COLORCOMBO), enable);
434     EnableWindow (GetDlgItem (dialog, IDC_THEME_COLORTEXT), enable);
435     EnableWindow (GetDlgItem (dialog, IDC_THEME_SIZECOMBO), enable);
436     EnableWindow (GetDlgItem (dialog, IDC_THEME_SIZETEXT), enable);
437 }
438   
439 static void init_dialog (HWND dialog)
440 {
441     updating_ui = TRUE;
442     
443     scan_theme_files();
444     if (!fill_theme_list (GetDlgItem (dialog, IDC_THEME_THEMECOMBO),
445         GetDlgItem (dialog, IDC_THEME_COLORCOMBO),
446         GetDlgItem (dialog, IDC_THEME_SIZECOMBO)))
447     {
448         SendMessageW (GetDlgItem (dialog, IDC_THEME_COLORCOMBO), CB_SETCURSEL, (WPARAM)-1, 0);
449         SendMessageW (GetDlgItem (dialog, IDC_THEME_SIZECOMBO), CB_SETCURSEL, (WPARAM)-1, 0);
450         enable_size_and_color_controls (dialog, FALSE);
451     }
452     else
453     {
454         enable_size_and_color_controls (dialog, TRUE);
455     }
456     theme_dirty = FALSE;
457
458     SendDlgItemMessageW(dialog, IDC_SYSPARAM_SIZE_UD, UDM_SETBUDDY, (WPARAM)GetDlgItem(dialog, IDC_SYSPARAM_SIZE), 0);
459     SendDlgItemMessageW(dialog, IDC_SYSPARAM_SIZE_UD, UDM_SETRANGE, 0, MAKELONG(100, 8));
460
461     updating_ui = FALSE;
462 }
463
464 static void on_theme_changed(HWND dialog) {
465     int index = SendMessageW (GetDlgItem (dialog, IDC_THEME_THEMECOMBO),
466         CB_GETCURSEL, 0, 0);
467     if (!update_color_and_size (index, GetDlgItem (dialog, IDC_THEME_COLORCOMBO),
468         GetDlgItem (dialog, IDC_THEME_SIZECOMBO)))
469     {
470         SendMessageW (GetDlgItem (dialog, IDC_THEME_COLORCOMBO), CB_SETCURSEL, -1, 0);
471         SendMessageW (GetDlgItem (dialog, IDC_THEME_SIZECOMBO), CB_SETCURSEL, -1, 0);
472         enable_size_and_color_controls (dialog, FALSE);
473     }
474     else
475     {
476         enable_size_and_color_controls (dialog, TRUE);
477     }
478     theme_dirty = TRUE;
479 }
480
481 static void apply_theme(HWND dialog)
482 {
483     int themeIndex, colorIndex, sizeIndex;
484
485     if (!theme_dirty) return;
486
487     themeIndex = SendMessageW (GetDlgItem (dialog, IDC_THEME_THEMECOMBO),
488         CB_GETCURSEL, 0, 0);
489     colorIndex = SendMessageW (GetDlgItem (dialog, IDC_THEME_COLORCOMBO),
490         CB_GETCURSEL, 0, 0);
491     sizeIndex = SendMessageW (GetDlgItem (dialog, IDC_THEME_SIZECOMBO),
492         CB_GETCURSEL, 0, 0);
493
494     do_apply_theme (themeIndex, colorIndex, sizeIndex);
495     theme_dirty = FALSE;
496 }
497
498 static struct
499 {
500     int sm_idx, color_idx;
501     const char *color_reg;
502     int size;
503     COLORREF color;
504     LOGFONTW lf;
505 } metrics[] =
506 {
507     {-1,                COLOR_BTNFACE,          "ButtonFace"    }, /* IDC_SYSPARAMS_BUTTON */
508     {-1,                COLOR_BTNTEXT,          "ButtonText"    }, /* IDC_SYSPARAMS_BUTTON_TEXT */
509     {-1,                COLOR_BACKGROUND,       "Background"    }, /* IDC_SYSPARAMS_DESKTOP */
510     {SM_CXMENUSIZE,     COLOR_MENU,             "Menu"          }, /* IDC_SYSPARAMS_MENU */
511     {-1,                COLOR_MENUTEXT,         "MenuText"      }, /* IDC_SYSPARAMS_MENU_TEXT */
512     {SM_CXVSCROLL,      COLOR_SCROLLBAR,        "Scrollbar"     }, /* IDC_SYSPARAMS_SCROLLBAR */
513     {-1,                COLOR_HIGHLIGHT,        "Hilight"       }, /* IDC_SYSPARAMS_SELECTION */
514     {-1,                COLOR_HIGHLIGHTTEXT,    "HilightText"   }, /* IDC_SYSPARAMS_SELECTION_TEXT */
515     {-1,                COLOR_INFOBK,           "InfoWindow"    }, /* IDC_SYSPARAMS_TOOLTIP */
516     {-1,                COLOR_INFOTEXT,         "InfoText"      }, /* IDC_SYSPARAMS_TOOLTIP_TEXT */
517     {-1,                COLOR_WINDOW,           "Window"        }, /* IDC_SYSPARAMS_WINDOW */
518     {-1,                COLOR_WINDOWTEXT,       "WindowText"    }, /* IDC_SYSPARAMS_WINDOW_TEXT */
519     {SM_CXSIZE,         COLOR_ACTIVECAPTION,    "ActiveTitle"   }, /* IDC_SYSPARAMS_ACTIVE_TITLE */
520     {-1,                COLOR_CAPTIONTEXT,      "TitleText"     }, /* IDC_SYSPARAMS_ACTIVE_TITLE_TEXT */
521     {-1,                COLOR_INACTIVECAPTION,  "InactiveTitle" }, /* IDC_SYSPARAMS_INACTIVE_TITLE */
522     {-1,                COLOR_INACTIVECAPTIONTEXT,"InactiveTitleText" }, /* IDC_SYSPARAMS_INACTIVE_TITLE_TEXT */
523     {-1,                -1,                     "MsgBoxText"    }, /* IDC_SYSPARAMS_MSGBOX_TEXT */
524     {-1,                COLOR_APPWORKSPACE,     "AppWorkSpace"  }, /* IDC_SYSPARAMS_APPWORKSPACE */
525     {-1,                COLOR_WINDOWFRAME,      "WindowFrame"   }, /* IDC_SYSPARAMS_WINDOW_FRAME */
526     {-1,                COLOR_ACTIVEBORDER,     "ActiveBorder"  }, /* IDC_SYSPARAMS_ACTIVE_BORDER */
527     {-1,                COLOR_INACTIVEBORDER,   "InactiveBorder" }, /* IDC_SYSPARAMS_INACTIVE_BORDER */
528     {-1,                COLOR_BTNSHADOW,        "ButtonShadow"  }, /* IDC_SYSPARAMS_BUTTON_SHADOW */
529     {-1,                COLOR_GRAYTEXT,         "GrayText"      }, /* IDC_SYSPARAMS_GRAY_TEXT */
530     {-1,                COLOR_BTNHILIGHT,       "ButtonHilight" }, /* IDC_SYSPARAMS_BUTTON_HILIGHT */
531     {-1,                COLOR_3DDKSHADOW,       "ButtonDkShadow" }, /* IDC_SYSPARAMS_BUTTON_DARK_SHADOW */
532     {-1,                COLOR_3DLIGHT,          "ButtonLight"   }, /* IDC_SYSPARAMS_BUTTON_LIGHT */
533     {-1,                COLOR_ALTERNATEBTNFACE, "ButtonAlternateFace" }, /* IDC_SYSPARAMS_BUTTON_ALTERNATE */
534     {-1,                COLOR_HOTLIGHT,         "HotTrackingColor" }, /* IDC_SYSPARAMS_HOT_TRACKING */
535     {-1,                COLOR_GRADIENTACTIVECAPTION, "GradientActiveTitle" }, /* IDC_SYSPARAMS_ACTIVE_TITLE_GRADIENT */
536     {-1,                COLOR_GRADIENTINACTIVECAPTION, "GradientInactiveTitle" }, /* IDC_SYSPARAMS_INACTIVE_TITLE_GRADIENT */
537     {-1,                COLOR_MENUHILIGHT,      "MenuHilight"   }, /* IDC_SYSPARAMS_MENU_HILIGHT */
538     {-1,                COLOR_MENUBAR,          "MenuBar"       }, /* IDC_SYSPARAMS_MENUBAR */
539 };
540
541 static void save_sys_color(int idx, COLORREF clr)
542 {
543     char buffer[13];
544
545     sprintf(buffer, "%d %d %d",  GetRValue (clr), GetGValue (clr), GetBValue (clr));
546     set_reg_key(HKEY_CURRENT_USER, "Control Panel\\Colors", metrics[idx].color_reg, buffer);
547 }
548
549 static void set_color_from_theme(WCHAR *keyName, COLORREF color)
550 {
551     char *keyNameA = NULL;
552     int keyNameSize=0, i=0;
553
554     keyNameSize = WideCharToMultiByte(CP_ACP, 0, keyName, -1, keyNameA, 0, NULL, NULL);
555     keyNameA = HeapAlloc(GetProcessHeap(), 0, keyNameSize);
556     WideCharToMultiByte(CP_ACP, 0, keyName, -1, keyNameA, keyNameSize, NULL, NULL);
557
558     for (i=0; i<sizeof(metrics)/sizeof(metrics[0]); i++)
559     {
560         if (lstrcmpiA(metrics[i].color_reg, keyNameA)==0)
561         {
562             metrics[i].color = color;
563             save_sys_color(i, color);
564             break;
565         }
566     }
567     HeapFree(GetProcessHeap(), 0, keyNameA);
568 }
569
570 static void do_parse_theme(WCHAR *file)
571 {
572     static const WCHAR colorSect[] = {
573         'C','o','n','t','r','o','l',' ','P','a','n','e','l','\\',
574         'C','o','l','o','r','s',0};
575     WCHAR keyName[MAX_PATH], keyNameValue[MAX_PATH];
576     WCHAR *keyNamePtr = NULL;
577     char *keyNameValueA = NULL;
578     int keyNameValueSize = 0;
579     int red = 0, green = 0, blue = 0;
580     COLORREF color;
581
582     WINE_TRACE("%s\n", wine_dbgstr_w(file));
583
584     GetPrivateProfileStringW(colorSect, NULL, NULL, keyName,
585                              MAX_PATH, file);
586
587     keyNamePtr = keyName;
588     while (*keyNamePtr!=0) {
589         GetPrivateProfileStringW(colorSect, keyNamePtr, NULL, keyNameValue,
590                                  MAX_PATH, file);
591
592         keyNameValueSize = WideCharToMultiByte(CP_ACP, 0, keyNameValue, -1,
593                                                keyNameValueA, 0, NULL, NULL);
594         keyNameValueA = HeapAlloc(GetProcessHeap(), 0, keyNameValueSize);
595         WideCharToMultiByte(CP_ACP, 0, keyNameValue, -1, keyNameValueA, keyNameValueSize, NULL, NULL);
596
597         WINE_TRACE("parsing key: %s with value: %s\n",
598                    wine_dbgstr_w(keyNamePtr), wine_dbgstr_w(keyNameValue));
599
600         sscanf(keyNameValueA, "%d %d %d", &red, &green, &blue);
601
602         color = RGB((BYTE)red, (BYTE)green, (BYTE)blue);
603
604         HeapFree(GetProcessHeap(), 0, keyNameValueA);
605
606         set_color_from_theme(keyNamePtr, color);
607
608         keyNamePtr+=lstrlenW(keyNamePtr);
609         keyNamePtr++;
610     }
611 }
612
613 static void on_theme_install(HWND dialog)
614 {
615   static const WCHAR filterMask[] = {0,'*','.','m','s','s','t','y','l','e','s',';',
616       '*','.','t','h','e','m','e',0,0};
617   static const WCHAR themeExt[] = {'.','T','h','e','m','e',0};
618   const int filterMaskLen = sizeof(filterMask)/sizeof(filterMask[0]);
619   OPENFILENAMEW ofn;
620   WCHAR filetitle[MAX_PATH];
621   WCHAR file[MAX_PATH];
622   WCHAR filter[100];
623   WCHAR title[100];
624
625   LoadStringW (GetModuleHandleW(NULL), IDS_THEMEFILE,
626       filter, sizeof (filter) / sizeof (filter[0]) - filterMaskLen);
627   memcpy (filter + lstrlenW (filter), filterMask, 
628       filterMaskLen * sizeof (WCHAR));
629   LoadStringW (GetModuleHandleW(NULL), IDS_THEMEFILE_SELECT,
630       title, sizeof (title) / sizeof (title[0]));
631
632   ofn.lStructSize = sizeof(OPENFILENAMEW);
633   ofn.hwndOwner = 0;
634   ofn.hInstance = 0;
635   ofn.lpstrFilter = filter;
636   ofn.lpstrCustomFilter = NULL;
637   ofn.nMaxCustFilter = 0;
638   ofn.nFilterIndex = 0;
639   ofn.lpstrFile = file;
640   ofn.lpstrFile[0] = '\0';
641   ofn.nMaxFile = sizeof(file)/sizeof(filetitle[0]);
642   ofn.lpstrFileTitle = filetitle;
643   ofn.lpstrFileTitle[0] = '\0';
644   ofn.nMaxFileTitle = sizeof(filetitle)/sizeof(filetitle[0]);
645   ofn.lpstrInitialDir = NULL;
646   ofn.lpstrTitle = title;
647   ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLESIZING;
648   ofn.nFileOffset = 0;
649   ofn.nFileExtension = 0;
650   ofn.lpstrDefExt = NULL;
651   ofn.lCustData = 0;
652   ofn.lpfnHook = NULL;
653   ofn.lpTemplateName = NULL;
654
655   if (GetOpenFileNameW(&ofn))
656   {
657       static const WCHAR themesSubdir[] = { '\\','T','h','e','m','e','s',0 };
658       static const WCHAR backslash[] = { '\\',0 };
659       WCHAR themeFilePath[MAX_PATH];
660       SHFILEOPSTRUCTW shfop;
661
662       if (FAILED (SHGetFolderPathW (NULL, CSIDL_RESOURCES|CSIDL_FLAG_CREATE, NULL, 
663           SHGFP_TYPE_CURRENT, themeFilePath))) return;
664
665       if (lstrcmpiW(PathFindExtensionW(filetitle), themeExt)==0)
666       {
667           do_parse_theme(file);
668           SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
669           return;
670       }
671
672       PathRemoveExtensionW (filetitle);
673
674       /* Construct path into which the theme file goes */
675       lstrcatW (themeFilePath, themesSubdir);
676       lstrcatW (themeFilePath, backslash);
677       lstrcatW (themeFilePath, filetitle);
678
679       /* Create the directory */
680       SHCreateDirectoryExW (dialog, themeFilePath, NULL);
681
682       /* Append theme file name itself */
683       lstrcatW (themeFilePath, backslash);
684       lstrcatW (themeFilePath, PathFindFileNameW (file));
685       /* SHFileOperation() takes lists as input, so double-nullterminate */
686       themeFilePath[lstrlenW (themeFilePath)+1] = 0;
687       file[lstrlenW (file)+1] = 0;
688
689       /* Do the copying */
690       WINE_TRACE("copying: %s -> %s\n", wine_dbgstr_w (file), 
691           wine_dbgstr_w (themeFilePath));
692       shfop.hwnd = dialog;
693       shfop.wFunc = FO_COPY;
694       shfop.pFrom = file;
695       shfop.pTo = themeFilePath;
696       shfop.fFlags = FOF_NOCONFIRMMKDIR;
697       if (SHFileOperationW (&shfop) == 0)
698       {
699           scan_theme_files();
700           if (!fill_theme_list (GetDlgItem (dialog, IDC_THEME_THEMECOMBO),
701               GetDlgItem (dialog, IDC_THEME_COLORCOMBO),
702               GetDlgItem (dialog, IDC_THEME_SIZECOMBO)))
703           {
704               SendMessageW (GetDlgItem (dialog, IDC_THEME_COLORCOMBO), CB_SETCURSEL, -1, 0);
705               SendMessageW (GetDlgItem (dialog, IDC_THEME_SIZECOMBO), CB_SETCURSEL, -1, 0);
706               enable_size_and_color_controls (dialog, FALSE);
707           }
708           else
709           {
710               enable_size_and_color_controls (dialog, TRUE);
711           }
712       }
713       else
714           WINE_TRACE("copy operation failed\n");
715   }
716   else WINE_TRACE("user cancelled\n");
717 }
718
719 /* Information about symbolic link targets of certain User Shell Folders. */
720 struct ShellFolderInfo {
721     int nFolder;
722     char szLinkTarget[FILENAME_MAX]; /* in unix locale */
723 };
724
725 static struct ShellFolderInfo asfiInfo[] = {
726     { CSIDL_DESKTOP,  "" },
727     { CSIDL_PERSONAL, "" },
728     { CSIDL_MYPICTURES, "" },
729     { CSIDL_MYMUSIC, "" },
730     { CSIDL_MYVIDEO, "" }
731 };
732
733 static struct ShellFolderInfo *psfiSelected = NULL;
734
735 #define NUM_ELEMS(x) (sizeof(x)/sizeof(*(x)))
736
737 static void init_shell_folder_listview_headers(HWND dialog) {
738     LVCOLUMNW listColumn;
739     RECT viewRect;
740     WCHAR szShellFolder[64] = {'S','h','e','l','l',' ','F','o','l','d','e','r',0};
741     WCHAR szLinksTo[64] = {'L','i','n','k','s',' ','t','o',0};
742     int width;
743
744     LoadStringW(GetModuleHandleW(NULL), IDS_SHELL_FOLDER, szShellFolder, sizeof(szShellFolder)/sizeof(WCHAR));
745     LoadStringW(GetModuleHandleW(NULL), IDS_LINKS_TO, szLinksTo, sizeof(szLinksTo)/sizeof(WCHAR));
746
747     GetClientRect(GetDlgItem(dialog, IDC_LIST_SFPATHS), &viewRect);
748     width = (viewRect.right - viewRect.left) / 4;
749
750     listColumn.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
751     listColumn.pszText = szShellFolder;
752     listColumn.cchTextMax = strlenW(listColumn.pszText);
753     listColumn.cx = width;
754
755     SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_INSERTCOLUMNW, 0, (LPARAM) &listColumn);
756
757     listColumn.pszText = szLinksTo;
758     listColumn.cchTextMax = strlenW(listColumn.pszText);
759     listColumn.cx = viewRect.right - viewRect.left - width - 1;
760
761     SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_INSERTCOLUMNW, 1, (LPARAM) &listColumn);
762 }
763
764 /* Reads the currently set shell folder symbol link targets into asfiInfo. */
765 static void read_shell_folder_link_targets(void) {
766     WCHAR wszPath[MAX_PATH];
767     HRESULT hr;
768     int i;
769    
770     for (i=0; i<NUM_ELEMS(asfiInfo); i++) {
771         asfiInfo[i].szLinkTarget[0] = '\0';
772         hr = SHGetFolderPathW(NULL, asfiInfo[i].nFolder|CSIDL_FLAG_DONT_VERIFY, NULL, 
773                               SHGFP_TYPE_CURRENT, wszPath);
774         if (SUCCEEDED(hr)) {
775             char *pszUnixPath = wine_get_unix_file_name(wszPath);
776             if (pszUnixPath) {
777                 struct stat statPath;
778                 if (!lstat(pszUnixPath, &statPath) && S_ISLNK(statPath.st_mode)) {
779                     int cLen = readlink(pszUnixPath, asfiInfo[i].szLinkTarget, FILENAME_MAX-1);
780                     if (cLen >= 0) asfiInfo[i].szLinkTarget[cLen] = '\0';
781                 }
782                 HeapFree(GetProcessHeap(), 0, pszUnixPath);
783             }
784         } 
785     }    
786 }
787
788 static void update_shell_folder_listview(HWND dialog) {
789     int i;
790     LVITEMW item;
791     LONG lSelected = SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_GETNEXTITEM, -1,
792                                         MAKELPARAM(LVNI_SELECTED,0));
793
794     SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_DELETEALLITEMS, 0, 0);
795
796     for (i=0; i<NUM_ELEMS(asfiInfo); i++) {
797         WCHAR buffer[MAX_PATH];
798         HRESULT hr;
799         LPITEMIDLIST pidlCurrent;
800
801         /* Some acrobatic to get the localized name of the shell folder */
802         hr = SHGetFolderLocation(dialog, asfiInfo[i].nFolder, NULL, 0, &pidlCurrent);
803         if (SUCCEEDED(hr)) { 
804             LPSHELLFOLDER psfParent;
805             LPCITEMIDLIST pidlLast;
806             hr = SHBindToParent(pidlCurrent, &IID_IShellFolder, (LPVOID*)&psfParent, &pidlLast);
807             if (SUCCEEDED(hr)) {
808                 STRRET strRet;
809                 hr = IShellFolder_GetDisplayNameOf(psfParent, pidlLast, SHGDN_FORADDRESSBAR, &strRet);
810                 if (SUCCEEDED(hr)) {
811                     hr = StrRetToBufW(&strRet, pidlLast, buffer, MAX_PATH);
812                 }
813                 IShellFolder_Release(psfParent);
814             }
815             ILFree(pidlCurrent);
816         }
817
818         /* If there's a dangling symlink for the current shell folder, SHGetFolderLocation
819          * will fail above. We fall back to the (non-verified) path of the shell folder. */
820         if (FAILED(hr)) {
821             hr = SHGetFolderPathW(dialog, asfiInfo[i].nFolder|CSIDL_FLAG_DONT_VERIFY, NULL,
822                                  SHGFP_TYPE_CURRENT, buffer);
823         }
824     
825         item.mask = LVIF_TEXT | LVIF_PARAM;
826         item.iItem = i;
827         item.iSubItem = 0;
828         item.pszText = buffer;
829         item.lParam = (LPARAM)&asfiInfo[i];
830         SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_INSERTITEMW, 0, (LPARAM)&item);
831
832         item.mask = LVIF_TEXT;
833         item.iItem = i;
834         item.iSubItem = 1;
835         item.pszText = strdupU2W(asfiInfo[i].szLinkTarget);
836         SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_SETITEMW, 0, (LPARAM)&item);
837         HeapFree(GetProcessHeap(), 0, item.pszText);
838     }
839
840     /* Ensure that the previously selected item is selected again. */
841     if (lSelected >= 0) {
842         item.mask = LVIF_STATE;
843         item.state = LVIS_SELECTED;
844         item.stateMask = LVIS_SELECTED;
845         SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_SETITEMSTATE, lSelected, (LPARAM)&item);
846     }
847 }
848
849 static void on_shell_folder_selection_changed(HWND hDlg, LPNMLISTVIEW lpnm) {
850     if (lpnm->uNewState & LVIS_SELECTED) {
851         psfiSelected = (struct ShellFolderInfo *)lpnm->lParam;
852         EnableWindow(GetDlgItem(hDlg, IDC_LINK_SFPATH), 1);
853         if (strlen(psfiSelected->szLinkTarget)) {
854             WCHAR *link;
855             CheckDlgButton(hDlg, IDC_LINK_SFPATH, BST_CHECKED);
856             EnableWindow(GetDlgItem(hDlg, IDC_EDIT_SFPATH), 1);
857             EnableWindow(GetDlgItem(hDlg, IDC_BROWSE_SFPATH), 1);
858             link = strdupU2W(psfiSelected->szLinkTarget);
859             set_textW(hDlg, IDC_EDIT_SFPATH, link);
860             HeapFree(GetProcessHeap(), 0, link);
861         } else {
862             CheckDlgButton(hDlg, IDC_LINK_SFPATH, BST_UNCHECKED);
863             EnableWindow(GetDlgItem(hDlg, IDC_EDIT_SFPATH), 0);
864             EnableWindow(GetDlgItem(hDlg, IDC_BROWSE_SFPATH), 0);
865             set_text(hDlg, IDC_EDIT_SFPATH, "");
866         }
867     } else {
868         psfiSelected = NULL;
869         CheckDlgButton(hDlg, IDC_LINK_SFPATH, BST_UNCHECKED);
870         set_text(hDlg, IDC_EDIT_SFPATH, "");
871         EnableWindow(GetDlgItem(hDlg, IDC_LINK_SFPATH), 0);
872         EnableWindow(GetDlgItem(hDlg, IDC_EDIT_SFPATH), 0);
873         EnableWindow(GetDlgItem(hDlg, IDC_BROWSE_SFPATH), 0);
874     }
875 }
876
877 /* Keep the contents of the edit control, the listview control and the symlink 
878  * information in sync. */
879 static void on_shell_folder_edit_changed(HWND hDlg) {
880     LVITEMW item;
881     WCHAR *text = get_textW(hDlg, IDC_EDIT_SFPATH);
882     LONG iSel = SendDlgItemMessageW(hDlg, IDC_LIST_SFPATHS, LVM_GETNEXTITEM, -1,
883                                     MAKELPARAM(LVNI_SELECTED,0));
884
885     if (!text || !psfiSelected || iSel < 0) {
886         HeapFree(GetProcessHeap(), 0, text);
887         return;
888     }
889
890     WideCharToMultiByte(CP_UNIXCP, 0, text, -1,
891                         psfiSelected->szLinkTarget, FILENAME_MAX, NULL, NULL);
892
893     item.mask = LVIF_TEXT;
894     item.iItem = iSel;
895     item.iSubItem = 1;
896     item.pszText = text;
897     SendDlgItemMessageW(hDlg, IDC_LIST_SFPATHS, LVM_SETITEMW, 0, (LPARAM)&item);
898
899     HeapFree(GetProcessHeap(), 0, text);
900
901     SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
902 }
903
904 static void apply_shell_folder_changes(void) {
905     WCHAR wszPath[MAX_PATH];
906     char szBackupPath[FILENAME_MAX], szUnixPath[FILENAME_MAX], *pszUnixPath = NULL;
907     int i;
908     struct stat statPath;
909     HRESULT hr;
910
911     for (i=0; i<NUM_ELEMS(asfiInfo); i++) {
912         /* Ignore nonexistent link targets */
913         if (asfiInfo[i].szLinkTarget[0] && stat(asfiInfo[i].szLinkTarget, &statPath))
914             continue;
915         
916         hr = SHGetFolderPathW(NULL, asfiInfo[i].nFolder|CSIDL_FLAG_CREATE, NULL, 
917                               SHGFP_TYPE_CURRENT, wszPath);
918         if (FAILED(hr)) continue;
919
920         /* Retrieve the corresponding unix path. */
921         pszUnixPath = wine_get_unix_file_name(wszPath);
922         if (!pszUnixPath) continue;
923         lstrcpyA(szUnixPath, pszUnixPath);
924         HeapFree(GetProcessHeap(), 0, pszUnixPath);
925             
926         /* Derive name for folder backup. */
927         lstrcpyA(szBackupPath, szUnixPath);
928         lstrcatA(szBackupPath, ".winecfg");
929         
930         if (lstat(szUnixPath, &statPath)) continue;
931     
932         /* Move old folder/link out of the way. */
933         if (S_ISLNK(statPath.st_mode)) {
934             if (unlink(szUnixPath)) continue; /* Unable to remove link. */
935         } else { 
936             if (!*asfiInfo[i].szLinkTarget) {
937                 continue; /* We are done. Old was real folder, as new shall be. */
938             } else { 
939                 if (rename(szUnixPath, szBackupPath)) { /* Move folder out of the way. */
940                     continue; /* Unable to move old folder. */
941                 }
942             }
943         }
944     
945         /* Create new link/folder. */
946         if (*asfiInfo[i].szLinkTarget) {
947             symlink(asfiInfo[i].szLinkTarget, szUnixPath);
948         } else {
949             /* If there's a backup folder, restore it. Else create new folder. */
950             if (!lstat(szBackupPath, &statPath) && S_ISDIR(statPath.st_mode)) {
951                 rename(szBackupPath, szUnixPath);
952             } else {
953                 mkdir(szUnixPath, 0777);
954             }
955         }
956     }
957 }
958
959 static void read_sysparams(HWND hDlg)
960 {
961     WCHAR buffer[256];
962     HWND list = GetDlgItem(hDlg, IDC_SYSPARAM_COMBO);
963     NONCLIENTMETRICSW nonclient_metrics;
964     int i, idx;
965
966     for (i = 0; i < sizeof(metrics) / sizeof(metrics[0]); i++)
967     {
968         LoadStringW(GetModuleHandleW(NULL), i + IDC_SYSPARAMS_BUTTON, buffer,
969                     sizeof(buffer) / sizeof(buffer[0]));
970         idx = SendMessageW(list, CB_ADDSTRING, 0, (LPARAM)buffer);
971         if (idx != CB_ERR) SendMessageW(list, CB_SETITEMDATA, idx, i);
972
973         if (metrics[i].sm_idx != -1)
974             metrics[i].size = GetSystemMetrics(metrics[i].sm_idx);
975         if (metrics[i].color_idx != -1)
976             metrics[i].color = GetSysColor(metrics[i].color_idx);
977     }
978
979     nonclient_metrics.cbSize = sizeof(NONCLIENTMETRICSW);
980     SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &nonclient_metrics, 0);
981
982     memcpy(&(metrics[IDC_SYSPARAMS_MENU_TEXT - IDC_SYSPARAMS_BUTTON].lf),
983            &(nonclient_metrics.lfMenuFont), sizeof(LOGFONTW));
984     memcpy(&(metrics[IDC_SYSPARAMS_ACTIVE_TITLE_TEXT - IDC_SYSPARAMS_BUTTON].lf),
985            &(nonclient_metrics.lfCaptionFont), sizeof(LOGFONTW));
986     memcpy(&(metrics[IDC_SYSPARAMS_TOOLTIP_TEXT - IDC_SYSPARAMS_BUTTON].lf),
987            &(nonclient_metrics.lfStatusFont), sizeof(LOGFONTW));
988     memcpy(&(metrics[IDC_SYSPARAMS_MSGBOX_TEXT - IDC_SYSPARAMS_BUTTON].lf),
989            &(nonclient_metrics.lfMessageFont), sizeof(LOGFONTW));
990 }
991
992 static void apply_sysparams(void)
993 {
994     NONCLIENTMETRICSW nonclient_metrics;
995     int i, cnt = 0;
996     int colors_idx[sizeof(metrics) / sizeof(metrics[0])];
997     COLORREF colors[sizeof(metrics) / sizeof(metrics[0])];
998
999     nonclient_metrics.cbSize = sizeof(nonclient_metrics);
1000     SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(nonclient_metrics), &nonclient_metrics, 0);
1001
1002     nonclient_metrics.iMenuWidth = nonclient_metrics.iMenuHeight =
1003             metrics[IDC_SYSPARAMS_MENU - IDC_SYSPARAMS_BUTTON].size;
1004     nonclient_metrics.iCaptionWidth = nonclient_metrics.iCaptionHeight =
1005             metrics[IDC_SYSPARAMS_ACTIVE_TITLE - IDC_SYSPARAMS_BUTTON].size;
1006     nonclient_metrics.iScrollWidth = nonclient_metrics.iScrollHeight =
1007             metrics[IDC_SYSPARAMS_SCROLLBAR - IDC_SYSPARAMS_BUTTON].size;
1008
1009     memcpy(&(nonclient_metrics.lfMenuFont),
1010            &(metrics[IDC_SYSPARAMS_MENU_TEXT - IDC_SYSPARAMS_BUTTON].lf),
1011            sizeof(LOGFONTW));
1012     memcpy(&(nonclient_metrics.lfCaptionFont),
1013            &(metrics[IDC_SYSPARAMS_ACTIVE_TITLE_TEXT - IDC_SYSPARAMS_BUTTON].lf),
1014            sizeof(LOGFONTW));
1015     memcpy(&(nonclient_metrics.lfStatusFont),
1016            &(metrics[IDC_SYSPARAMS_TOOLTIP_TEXT - IDC_SYSPARAMS_BUTTON].lf),
1017            sizeof(LOGFONTW));
1018     memcpy(&(nonclient_metrics.lfMessageFont),
1019            &(metrics[IDC_SYSPARAMS_MSGBOX_TEXT - IDC_SYSPARAMS_BUTTON].lf),
1020            sizeof(LOGFONTW));
1021
1022     SystemParametersInfoW(SPI_SETNONCLIENTMETRICS, sizeof(nonclient_metrics), &nonclient_metrics,
1023                           SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
1024
1025     for (i = 0; i < sizeof(metrics) / sizeof(metrics[0]); i++)
1026         if (metrics[i].color_idx != -1)
1027         {
1028             colors_idx[cnt] = metrics[i].color_idx;
1029             colors[cnt++] = metrics[i].color;
1030         }
1031     SetSysColors(cnt, colors_idx, colors);
1032 }
1033
1034 static void on_sysparam_change(HWND hDlg)
1035 {
1036     int index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETCURSEL, 0, 0);
1037
1038     index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETITEMDATA, index, 0);
1039
1040     updating_ui = TRUE;
1041
1042     EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_COLOR_TEXT), metrics[index].color_idx != -1);
1043     EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_COLOR), metrics[index].color_idx != -1);
1044     InvalidateRect(GetDlgItem(hDlg, IDC_SYSPARAM_COLOR), NULL, TRUE);
1045
1046     EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_SIZE_TEXT), metrics[index].sm_idx != -1);
1047     EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_SIZE), metrics[index].sm_idx != -1);
1048     EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_SIZE_UD), metrics[index].sm_idx != -1);
1049     if (metrics[index].sm_idx != -1)
1050         SendDlgItemMessageW(hDlg, IDC_SYSPARAM_SIZE_UD, UDM_SETPOS, 0, MAKELONG(metrics[index].size, 0));
1051     else
1052         set_text(hDlg, IDC_SYSPARAM_SIZE, "");
1053
1054     EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_FONT),
1055         index == IDC_SYSPARAMS_MENU_TEXT-IDC_SYSPARAMS_BUTTON ||
1056         index == IDC_SYSPARAMS_ACTIVE_TITLE_TEXT-IDC_SYSPARAMS_BUTTON ||
1057         index == IDC_SYSPARAMS_TOOLTIP_TEXT-IDC_SYSPARAMS_BUTTON ||
1058         index == IDC_SYSPARAMS_MSGBOX_TEXT-IDC_SYSPARAMS_BUTTON
1059     );
1060
1061     updating_ui = FALSE;
1062 }
1063
1064 static void on_draw_item(HWND hDlg, WPARAM wParam, LPARAM lParam)
1065 {
1066     static HBRUSH black_brush = 0;
1067     LPDRAWITEMSTRUCT draw_info = (LPDRAWITEMSTRUCT)lParam;
1068
1069     if (!black_brush) black_brush = CreateSolidBrush(0);
1070
1071     if (draw_info->CtlID == IDC_SYSPARAM_COLOR)
1072     {
1073         UINT state = DFCS_ADJUSTRECT | DFCS_BUTTONPUSH;
1074
1075         if (draw_info->itemState & ODS_DISABLED)
1076             state |= DFCS_INACTIVE;
1077         else
1078             state |= draw_info->itemState & ODS_SELECTED ? DFCS_PUSHED : 0;
1079
1080         DrawFrameControl(draw_info->hDC, &draw_info->rcItem, DFC_BUTTON, state);
1081
1082         if (!(draw_info->itemState & ODS_DISABLED))
1083         {
1084             HBRUSH brush;
1085             int index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETCURSEL, 0, 0);
1086
1087             index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETITEMDATA, index, 0);
1088             brush = CreateSolidBrush(metrics[index].color);
1089
1090             InflateRect(&draw_info->rcItem, -1, -1);
1091             FrameRect(draw_info->hDC, &draw_info->rcItem, black_brush);
1092             InflateRect(&draw_info->rcItem, -1, -1);
1093             FillRect(draw_info->hDC, &draw_info->rcItem, brush);
1094             DeleteObject(brush);
1095         }
1096     }
1097 }
1098
1099 static void on_select_font(HWND hDlg)
1100 {
1101     CHOOSEFONTW cf;
1102     int index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETCURSEL, 0, 0);
1103     index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETITEMDATA, index, 0);
1104
1105     ZeroMemory(&cf, sizeof(cf));
1106     cf.lStructSize = sizeof(CHOOSEFONTW);
1107     cf.hwndOwner = hDlg;
1108     cf.lpLogFont = &(metrics[index].lf);
1109     cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT | CF_NOSCRIPTSEL;
1110
1111     ChooseFontW(&cf);
1112 }
1113
1114 INT_PTR CALLBACK
1115 ThemeDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
1116 {
1117     switch (uMsg) {
1118         case WM_INITDIALOG:
1119             read_shell_folder_link_targets();
1120             init_shell_folder_listview_headers(hDlg);
1121             update_shell_folder_listview(hDlg);
1122             read_sysparams(hDlg);
1123             break;
1124         
1125         case WM_DESTROY:
1126             free_theme_files();
1127             break;
1128
1129         case WM_SHOWWINDOW:
1130             set_window_title(hDlg);
1131             break;
1132             
1133         case WM_COMMAND:
1134             switch(HIWORD(wParam)) {
1135                 case CBN_SELCHANGE: {
1136                     if (updating_ui) break;
1137                     switch (LOWORD(wParam))
1138                     {
1139                         case IDC_THEME_THEMECOMBO: on_theme_changed(hDlg); break;
1140                         case IDC_THEME_COLORCOMBO: /* fall through */
1141                         case IDC_THEME_SIZECOMBO: theme_dirty = TRUE; break;
1142                         case IDC_SYSPARAM_COMBO: on_sysparam_change(hDlg); return FALSE;
1143                     }
1144                     SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1145                     break;
1146                 }
1147                 case EN_CHANGE: {
1148                     if (updating_ui) break;
1149                     switch (LOWORD(wParam))
1150                     {
1151                         case IDC_EDIT_SFPATH: on_shell_folder_edit_changed(hDlg); break;
1152                         case IDC_SYSPARAM_SIZE:
1153                         {
1154                             char *text = get_text(hDlg, IDC_SYSPARAM_SIZE);
1155                             int index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETCURSEL, 0, 0);
1156
1157                             index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETITEMDATA, index, 0);
1158                             metrics[index].size = atoi(text);
1159                             HeapFree(GetProcessHeap(), 0, text);
1160
1161                             SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1162                             break;
1163                         }
1164                     }
1165                     break;
1166                 }
1167                 case BN_CLICKED:
1168                     switch (LOWORD(wParam))
1169                     {
1170                         case IDC_THEME_INSTALL:
1171                             on_theme_install (hDlg);
1172                             break;
1173
1174                         case IDC_SYSPARAM_FONT:
1175                             on_select_font(hDlg);
1176                             break;
1177
1178                         case IDC_BROWSE_SFPATH:
1179                         {
1180                             WCHAR link[FILENAME_MAX];
1181                             if (browse_for_unix_folder(hDlg, link)) {
1182                                 WideCharToMultiByte(CP_UNIXCP, 0, link, -1,
1183                                                     psfiSelected->szLinkTarget, FILENAME_MAX,
1184                                                     NULL, NULL);
1185                                 update_shell_folder_listview(hDlg);
1186                                 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1187                             }
1188                             break;
1189                         }
1190
1191                         case IDC_LINK_SFPATH:
1192                             if (IsDlgButtonChecked(hDlg, IDC_LINK_SFPATH)) {
1193                                 WCHAR link[FILENAME_MAX];
1194                                 if (browse_for_unix_folder(hDlg, link)) {
1195                                     WideCharToMultiByte(CP_UNIXCP, 0, link, -1,
1196                                                         psfiSelected->szLinkTarget, FILENAME_MAX,
1197                                                         NULL, NULL);
1198                                     update_shell_folder_listview(hDlg);
1199                                     SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1200                                 } else {
1201                                     CheckDlgButton(hDlg, IDC_LINK_SFPATH, BST_UNCHECKED);
1202                                 }
1203                             } else {
1204                                 psfiSelected->szLinkTarget[0] = '\0';
1205                                 update_shell_folder_listview(hDlg);
1206                                 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1207                             }
1208                             break;    
1209
1210                         case IDC_SYSPARAM_COLOR:
1211                         {
1212                             static COLORREF user_colors[16];
1213                             CHOOSECOLORW c_color;
1214                             int index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETCURSEL, 0, 0);
1215
1216                             index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETITEMDATA, index, 0);
1217
1218                             memset(&c_color, 0, sizeof(c_color));
1219                             c_color.lStructSize = sizeof(c_color);
1220                             c_color.lpCustColors = user_colors;
1221                             c_color.rgbResult = metrics[index].color;
1222                             c_color.Flags = CC_ANYCOLOR | CC_RGBINIT;
1223                             c_color.hwndOwner = hDlg;
1224                             if (ChooseColorW(&c_color))
1225                             {
1226                                 metrics[index].color = c_color.rgbResult;
1227                                 save_sys_color(index, metrics[index].color);
1228                                 InvalidateRect(GetDlgItem(hDlg, IDC_SYSPARAM_COLOR), NULL, TRUE);
1229                                 SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1230                             }
1231                             break;
1232                         }
1233                     }
1234                     break;
1235             }
1236             break;
1237         
1238         case WM_NOTIFY:
1239             switch (((LPNMHDR)lParam)->code) {
1240                 case PSN_KILLACTIVE: {
1241                     SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, FALSE);
1242                     break;
1243                 }
1244                 case PSN_APPLY: {
1245                     apply();
1246                     apply_theme(hDlg);
1247                     apply_shell_folder_changes();
1248                     apply_sysparams();
1249                     read_shell_folder_link_targets();
1250                     update_shell_folder_listview(hDlg);
1251                     SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
1252                     break;
1253                 }
1254                 case LVN_ITEMCHANGED: { 
1255                     if (wParam == IDC_LIST_SFPATHS)  
1256                         on_shell_folder_selection_changed(hDlg, (LPNMLISTVIEW)lParam);
1257                     break;
1258                 }
1259                 case PSN_SETACTIVE: {
1260                     init_dialog (hDlg);
1261                     break;
1262                 }
1263             }
1264             break;
1265
1266         case WM_DRAWITEM:
1267             on_draw_item(hDlg, wParam, lParam);
1268             break;
1269
1270         default:
1271             break;
1272     }
1273     return FALSE;
1274 }