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