theme.c 47.2 KB
Newer Older
1
/*
2 3 4
 * Desktop Integration
 * - Theme configuration code
 * - User Shell Folder mapping
5 6
 *
 * Copyright (c) 2005 by Frank Richter
7
 * Copyright (c) 2006 by Michael Jung
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 <assert.h>
26 27 28
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
29 30

#define COBJMACROS
31

32
#include <windows.h>
33 34
#include <commdlg.h>
#include <shellapi.h>
35 36 37
#include <uxtheme.h>
#include <tmschema.h>
#include <shlobj.h>
38
#include <shlwapi.h>
39 40 41 42 43 44 45 46
#include <wine/debug.h>

#include "resource.h"
#include "winecfg.h"

WINE_DEFAULT_DEBUG_CHANNEL(winecfg);

/* UXTHEME functions not in the headers */
47 48 49 50 51 52 53 54

typedef struct tagTHEMENAMES
{
    WCHAR szName[MAX_PATH+1];
    WCHAR szDisplayName[MAX_PATH+1];
    WCHAR szTooltip[MAX_PATH+1];
} THEMENAMES, *PTHEMENAMES;

55 56 57 58 59 60 61
typedef void* HTHEMEFILE;
typedef BOOL (CALLBACK *EnumThemeProc)(LPVOID lpReserved, 
				       LPCWSTR pszThemeFileName,
                                       LPCWSTR pszThemeName, 
				       LPCWSTR pszToolTip, LPVOID lpReserved2,
                                       LPVOID lpData);

62
HRESULT WINAPI EnumThemeColors (LPCWSTR pszThemeFileName, LPWSTR pszSizeName,
63
				DWORD dwColorNum, PTHEMENAMES pszColorNames);
64
HRESULT WINAPI EnumThemeSizes (LPCWSTR pszThemeFileName, LPWSTR pszColorName,
65
			       DWORD dwSizeNum, PTHEMENAMES pszSizeNames);
66 67 68 69 70 71 72 73
HRESULT WINAPI ApplyTheme (HTHEMEFILE hThemeFile, char* unknown, HWND hWnd);
HRESULT WINAPI OpenThemeFile (LPCWSTR pszThemeFileName, LPCWSTR pszColorName,
			      LPCWSTR pszSizeName, HTHEMEFILE* hThemeFile,
			      DWORD unknown);
HRESULT WINAPI CloseThemeFile (HTHEMEFILE hThemeFile);
HRESULT WINAPI EnumThemes (LPCWSTR pszThemePath, EnumThemeProc callback,
                           LPVOID lpData);

74 75 76
static void refresh_sysparams(HWND hDlg);
static void on_sysparam_change(HWND hDlg);

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
/* A struct to keep both the internal and "fancy" name of a color or size */
typedef struct
{
  WCHAR* name;
  WCHAR* fancyName;
} ThemeColorOrSize;

/* wrapper around DSA that also keeps an item count */
typedef struct
{
  HDSA dsa;
  int count;
} WrappedDsa;

/* Some helper functions to deal with ThemeColorOrSize structs in WrappedDSAs */

static void color_or_size_dsa_add (WrappedDsa* wdsa, const WCHAR* name,
				   const WCHAR* fancyName)
{
    ThemeColorOrSize item;
97 98

    item.name = malloc ((wcslen (name) + 1) * sizeof(WCHAR));
99 100
    lstrcpyW (item.name, name);

101
    item.fancyName = malloc ((wcslen (fancyName) + 1) * sizeof(WCHAR));
102 103 104 105 106 107 108 109
    lstrcpyW (item.fancyName, fancyName);

    DSA_InsertItem (wdsa->dsa, wdsa->count, &item);
    wdsa->count++;
}

static int CALLBACK dsa_destroy_callback (LPVOID p, LPVOID pData)
{
110
    ThemeColorOrSize* item = p;
111 112
    free (item->name);
    free (item->fancyName);
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    return 1;
}

static void free_color_or_size_dsa (WrappedDsa* wdsa)
{
    DSA_DestroyCallback (wdsa->dsa, dsa_destroy_callback, NULL);
}

static void create_color_or_size_dsa (WrappedDsa* wdsa)
{
    wdsa->dsa = DSA_Create (sizeof (ThemeColorOrSize), 1);
    wdsa->count = 0;
}

static ThemeColorOrSize* color_or_size_dsa_get (WrappedDsa* wdsa, int index)
{
129
    return DSA_GetItemPtr (wdsa->dsa, index);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
}

static int color_or_size_dsa_find (WrappedDsa* wdsa, const WCHAR* name)
{
    int i = 0;
    for (; i < wdsa->count; i++)
    {
	ThemeColorOrSize* item = color_or_size_dsa_get (wdsa, i);
	if (lstrcmpiW (item->name, name) == 0) break;
    }
    return i;
}

/* A theme file, contains file name, display name, color and size scheme names */
typedef struct
{
    WCHAR* themeFileName;
    WCHAR* fancyName;
    WrappedDsa colors;
    WrappedDsa sizes;
} ThemeFile;

static HDSA themeFiles = NULL;
static int themeFilesCount = 0;

static int CALLBACK theme_dsa_destroy_callback (LPVOID p, LPVOID pData)
{
157
    ThemeFile* item = p;
158 159
    free (item->themeFileName);
    free (item->fancyName);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    free_color_or_size_dsa (&item->colors);
    free_color_or_size_dsa (&item->sizes);
    return 1;
}

/* Free memory occupied by the theme list */
static void free_theme_files(void)
{
    if (themeFiles == NULL) return;
      
    DSA_DestroyCallback (themeFiles , theme_dsa_destroy_callback, NULL);
    themeFiles = NULL;
    themeFilesCount = 0;
}

175
typedef HRESULT (WINAPI * EnumTheme) (LPCWSTR, LPWSTR, DWORD, PTHEMENAMES);
176 177 178 179 180 181 182

/* fill a string list with either colors or sizes of a theme */
static void fill_theme_string_array (const WCHAR* filename, 
				     WrappedDsa* wdsa,
				     EnumTheme enumTheme)
{
    DWORD index = 0;
183
    THEMENAMES names;
184 185 186

    WINE_TRACE ("%s %p %p\n", wine_dbgstr_w (filename), wdsa, enumTheme);

187
    while (SUCCEEDED (enumTheme (filename, NULL, index++, &names)))
188
    {
189 190 191
	WINE_TRACE ("%s: %s\n", wine_dbgstr_w (names.szName), 
            wine_dbgstr_w (names.szDisplayName));
	color_or_size_dsa_add (wdsa, names.szName, names.szDisplayName);
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    }
}

/* Theme enumeration callback, adds theme to theme list */
static BOOL CALLBACK myEnumThemeProc (LPVOID lpReserved, 
				      LPCWSTR pszThemeFileName,
				      LPCWSTR pszThemeName, 
				      LPCWSTR pszToolTip, 
				      LPVOID lpReserved2, LPVOID lpData)
{
    ThemeFile newEntry;

    /* fill size/color lists */
    create_color_or_size_dsa (&newEntry.colors);
    fill_theme_string_array (pszThemeFileName, &newEntry.colors, EnumThemeColors);
    create_color_or_size_dsa (&newEntry.sizes);
    fill_theme_string_array (pszThemeFileName, &newEntry.sizes, EnumThemeSizes);

210
    newEntry.themeFileName = malloc ((wcslen (pszThemeFileName) + 1) * sizeof(WCHAR));
211
    lstrcpyW (newEntry.themeFileName, pszThemeFileName);
212 213

    newEntry.fancyName = malloc ((wcslen (pszThemeName) + 1) * sizeof(WCHAR));
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    lstrcpyW (newEntry.fancyName, pszThemeName);
  
    /*list_add_tail (&themeFiles, &newEntry->entry);*/
    DSA_InsertItem (themeFiles, themeFilesCount, &newEntry);
    themeFilesCount++;

    return TRUE;
}

/* Scan for themes */
static void scan_theme_files(void)
{
    WCHAR themesPath[MAX_PATH];

    free_theme_files();

    if (FAILED (SHGetFolderPathW (NULL, CSIDL_RESOURCES, NULL, 
        SHGFP_TYPE_CURRENT, themesPath))) return;

    themeFiles = DSA_Create (sizeof (ThemeFile), 1);
234
    lstrcatW (themesPath, L"\\Themes");
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

    EnumThemes (themesPath, myEnumThemeProc, 0);
}

/* fill the color & size combo boxes for a given theme */
static void fill_color_size_combos (ThemeFile* theme, HWND comboColor, 
                                    HWND comboSize)
{
    int i;

    SendMessageW (comboColor, CB_RESETCONTENT, 0, 0);
    for (i = 0; i < theme->colors.count; i++)
    {
	ThemeColorOrSize* item = color_or_size_dsa_get (&theme->colors, i);
	SendMessageW (comboColor, CB_ADDSTRING, 0, (LPARAM)item->fancyName);
    }

    SendMessageW (comboSize, CB_RESETCONTENT, 0, 0);
    for (i = 0; i < theme->sizes.count; i++)
    {
	ThemeColorOrSize* item = color_or_size_dsa_get (&theme->sizes, i);
	SendMessageW (comboSize, CB_ADDSTRING, 0, (LPARAM)item->fancyName);
    }
}

/* Select the item of a combo box that matches a theme's color and size 
 * scheme. */
static void select_color_and_size (ThemeFile* theme, 
			    const WCHAR* colorName, HWND comboColor, 
			    const WCHAR* sizeName, HWND comboSize)
{
    SendMessageW (comboColor, CB_SETCURSEL, 
	color_or_size_dsa_find (&theme->colors, colorName), 0);
    SendMessageW (comboSize, CB_SETCURSEL, 
	color_or_size_dsa_find (&theme->sizes, sizeName), 0);
}

/* Fill theme, color and sizes combo boxes with the know themes and select
 * the entries matching the currently active theme. */
static BOOL fill_theme_list (HWND comboTheme, HWND comboColor, HWND comboSize)
{
    WCHAR textNoTheme[256];
    int themeIndex = 0;
    BOOL ret = TRUE;
    int i;
    WCHAR currentTheme[MAX_PATH];
    WCHAR currentColor[MAX_PATH];
    WCHAR currentSize[MAX_PATH];
    ThemeFile* theme = NULL;

285
    LoadStringW(GetModuleHandleW(NULL), IDS_NOTHEME, textNoTheme, ARRAY_SIZE(textNoTheme));
286 287 288 289 290 291

    SendMessageW (comboTheme, CB_RESETCONTENT, 0, 0);
    SendMessageW (comboTheme, CB_ADDSTRING, 0, (LPARAM)textNoTheme);

    for (i = 0; i < themeFilesCount; i++)
    {
292
        ThemeFile* item = DSA_GetItemPtr (themeFiles, i);
293 294 295
	SendMessageW (comboTheme, CB_ADDSTRING, 0, 
	    (LPARAM)item->fancyName);
    }
296 297 298

    if (IsThemeActive() && SUCCEEDED(GetCurrentThemeName(currentTheme, ARRAY_SIZE(currentTheme),
                currentColor, ARRAY_SIZE(currentColor), currentSize, ARRAY_SIZE(currentSize))))
299 300 301 302 303
    {
	/* Determine the index of the currently active theme. */
	BOOL found = FALSE;
	for (i = 0; i < themeFilesCount; i++)
	{
304
            theme = DSA_GetItemPtr (themeFiles, i);
305 306 307 308 309 310 311 312 313 314
	    if (lstrcmpiW (theme->themeFileName, currentTheme) == 0)
	    {
		found = TRUE;
		themeIndex = i+1;
		break;
	    }
	}
	if (!found)
	{
	    /* Current theme not found?... add to the list, then... */
315
	    WINE_TRACE("Theme %s not in list of enumerated themes\n",
316 317 318 319
		wine_dbgstr_w (currentTheme));
	    myEnumThemeProc (NULL, currentTheme, currentTheme, 
		currentTheme, NULL, NULL);
	    themeIndex = themeFilesCount;
320
            theme = DSA_GetItemPtr (themeFiles, themeFilesCount-1);
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	}
	fill_color_size_combos (theme, comboColor, comboSize);
	select_color_and_size (theme, currentColor, comboColor,
	    currentSize, comboSize);
    }
    else
    {
	/* No theme selected */
	ret = FALSE;
    }

    SendMessageW (comboTheme, CB_SETCURSEL, themeIndex, 0);
    return ret;
}

/* Update the color & size combo boxes when the selection of the theme
 * combo changed. Selects the current color and size scheme if the theme
 * is currently active, otherwise the first color and size. */
339 340
static BOOL update_color_and_size (int themeIndex, HWND comboColor, 
				   HWND comboSize)
341 342 343 344 345 346 347 348 349 350
{
    if (themeIndex == 0)
    {
	return FALSE;
    }
    else
    {
	WCHAR currentTheme[MAX_PATH];
	WCHAR currentColor[MAX_PATH];
	WCHAR currentSize[MAX_PATH];
351
	ThemeFile* theme = DSA_GetItemPtr (themeFiles, themeIndex - 1);
352 353
    
	fill_color_size_combos (theme, comboColor, comboSize);
354 355 356

        if ((SUCCEEDED(GetCurrentThemeName (currentTheme, ARRAY_SIZE(currentTheme),
            currentColor, ARRAY_SIZE(currentColor), currentSize, ARRAY_SIZE(currentSize))))
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	    && (lstrcmpiW (currentTheme, theme->themeFileName) == 0))
	{
	    select_color_and_size (theme, currentColor, comboColor,
		currentSize, comboSize);
	}
	else
	{
	    SendMessageW (comboColor, CB_SETCURSEL, 0, 0);
	    SendMessageW (comboSize, CB_SETCURSEL, 0, 0);
	}
    }
    return TRUE;
}

/* Apply a theme from a given theme, color and size combo box item index. */
372
static void do_apply_theme (HWND dialog, int themeIndex, int colorIndex, int sizeIndex)
373 374 375 376 377 378 379 380 381 382
{
    static char b[] = "\0";

    if (themeIndex == 0)
    {
	/* no theme */
	ApplyTheme (NULL, b, NULL);
    }
    else
    {
383
        ThemeFile* theme = DSA_GetItemPtr (themeFiles, themeIndex-1);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
	const WCHAR* themeFileName = theme->themeFileName;
	const WCHAR* colorName = NULL;
	const WCHAR* sizeName = NULL;
	HTHEMEFILE hTheme;
	ThemeColorOrSize* item;
    
	item = color_or_size_dsa_get (&theme->colors, colorIndex);
	colorName = item->name;
	
	item = color_or_size_dsa_get (&theme->sizes, sizeIndex);
	sizeName = item->name;
	
	if (SUCCEEDED (OpenThemeFile (themeFileName, colorName, sizeName,
	    &hTheme, 0)))
	{
	    ApplyTheme (hTheme, b, NULL);
	    CloseThemeFile (hTheme);
	}
	else
	{
	    ApplyTheme (NULL, b, NULL);
	}
    }
407 408

    refresh_sysparams(dialog);
409 410
}

411
static BOOL updating_ui;
412
static BOOL theme_dirty;
413 414 415 416 417 418 419 420

static void enable_size_and_color_controls (HWND dialog, BOOL enable)
{
    EnableWindow (GetDlgItem (dialog, IDC_THEME_COLORCOMBO), enable);
    EnableWindow (GetDlgItem (dialog, IDC_THEME_COLORTEXT), enable);
    EnableWindow (GetDlgItem (dialog, IDC_THEME_SIZECOMBO), enable);
    EnableWindow (GetDlgItem (dialog, IDC_THEME_SIZETEXT), enable);
}
421 422 423 424 425 426 427 428 429 430 431 432 433 434

static DWORD get_app_theme(void)
{
    DWORD ret = 0, len = sizeof(ret), type;
    HKEY hkey;

    if (RegOpenKeyExW( HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", 0, KEY_QUERY_VALUE, &hkey ))
        return 1;
    if (RegQueryValueExW( hkey, L"AppsUseLightTheme", NULL, &type, (BYTE *)&ret, &len ) || type != REG_DWORD)
        ret = 1;

    RegCloseKey( hkey );
    return ret;
}
435 436
  
static void init_dialog (HWND dialog)
437
{
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    DWORD apps_use_light_theme;
    WCHAR apps_theme_str[256];

    static const struct
    {
        int id;
        DWORD value;
    }
    app_themes[] =
    {
        { IDC_THEME_APPCOMBO_LIGHT, 1 },
        { IDC_THEME_APPCOMBO_DARK,  0 },
    };

    SendDlgItemMessageW( dialog, IDC_THEME_APPCOMBO, CB_RESETCONTENT, 0, 0 );

    LoadStringW( GetModuleHandleW(NULL), app_themes[0].id, apps_theme_str, ARRAY_SIZE(apps_theme_str) );
    SendDlgItemMessageW( dialog, IDC_THEME_APPCOMBO, CB_ADDSTRING, 0, (LPARAM)apps_theme_str );
    LoadStringW( GetModuleHandleW(NULL), app_themes[1].id, apps_theme_str, ARRAY_SIZE(apps_theme_str) );
    SendDlgItemMessageW( dialog, IDC_THEME_APPCOMBO, CB_ADDSTRING, 0, (LPARAM)apps_theme_str );

    apps_use_light_theme = get_app_theme();
    SendDlgItemMessageW( dialog, IDC_THEME_APPCOMBO, CB_SETCURSEL, app_themes[apps_use_light_theme].value, 0 );

    SendDlgItemMessageW( dialog, IDC_SYSPARAM_SIZE_UD, UDM_SETBUDDY, (WPARAM)GetDlgItem(dialog, IDC_SYSPARAM_SIZE), 0 );
463 464 465
}

static void update_dialog (HWND dialog)
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
{
    updating_ui = TRUE;
    
    scan_theme_files();
    if (!fill_theme_list (GetDlgItem (dialog, IDC_THEME_THEMECOMBO),
        GetDlgItem (dialog, IDC_THEME_COLORCOMBO),
        GetDlgItem (dialog, IDC_THEME_SIZECOMBO)))
    {
        SendMessageW (GetDlgItem (dialog, IDC_THEME_COLORCOMBO), CB_SETCURSEL, (WPARAM)-1, 0);
        SendMessageW (GetDlgItem (dialog, IDC_THEME_SIZECOMBO), CB_SETCURSEL, (WPARAM)-1, 0);
        enable_size_and_color_controls (dialog, FALSE);
    }
    else
    {
        enable_size_and_color_controls (dialog, TRUE);
    }
    theme_dirty = FALSE;
483 484 485

    SendDlgItemMessageW(dialog, IDC_SYSPARAM_SIZE_UD, UDM_SETRANGE, 0, MAKELONG(100, 8));

486 487 488 489
    updating_ui = FALSE;
}

static void on_theme_changed(HWND dialog) {
490 491 492
    int index;

    index = SendMessageW (GetDlgItem (dialog, IDC_THEME_THEMECOMBO), CB_GETCURSEL, 0, 0);
493
    if (!update_color_and_size (index, GetDlgItem (dialog, IDC_THEME_COLORCOMBO),
494 495
        GetDlgItem (dialog, IDC_THEME_SIZECOMBO)))
    {
496 497
        SendMessageW (GetDlgItem (dialog, IDC_THEME_COLORCOMBO), CB_SETCURSEL, -1, 0);
        SendMessageW (GetDlgItem (dialog, IDC_THEME_SIZECOMBO), CB_SETCURSEL, -1, 0);
498 499 500 501 502 503
        enable_size_and_color_controls (dialog, FALSE);
    }
    else
    {
        enable_size_and_color_controls (dialog, TRUE);
    }
504 505 506 507 508

    index = SendMessageW (GetDlgItem (dialog, IDC_THEME_APPCOMBO), CB_GETCURSEL, 0, 0);
    set_reg_key_dword(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
                      L"AppsUseLightTheme", !index);

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
    theme_dirty = TRUE;
}

static void apply_theme(HWND dialog)
{
    int themeIndex, colorIndex, sizeIndex;

    if (!theme_dirty) return;

    themeIndex = SendMessageW (GetDlgItem (dialog, IDC_THEME_THEMECOMBO),
        CB_GETCURSEL, 0, 0);
    colorIndex = SendMessageW (GetDlgItem (dialog, IDC_THEME_COLORCOMBO),
        CB_GETCURSEL, 0, 0);
    sizeIndex = SendMessageW (GetDlgItem (dialog, IDC_THEME_SIZECOMBO),
        CB_GETCURSEL, 0, 0);

525
    do_apply_theme (dialog, themeIndex, colorIndex, sizeIndex);
526 527 528
    theme_dirty = FALSE;
}

529 530 531
static struct
{
    int sm_idx, color_idx;
532
    const WCHAR *color_reg;
533 534 535 536 537
    int size;
    COLORREF color;
    LOGFONTW lf;
} metrics[] =
{
538 539 540 541 542 543 544 545 546 547 548 549
    {-1,                COLOR_BTNFACE,          L"ButtonFace"    }, /* IDC_SYSPARAMS_BUTTON */
    {-1,                COLOR_BTNTEXT,          L"ButtonText"    }, /* IDC_SYSPARAMS_BUTTON_TEXT */
    {-1,                COLOR_BACKGROUND,       L"Background"    }, /* IDC_SYSPARAMS_DESKTOP */
    {SM_CXMENUSIZE,     COLOR_MENU,             L"Menu"          }, /* IDC_SYSPARAMS_MENU */
    {-1,                COLOR_MENUTEXT,         L"MenuText"      }, /* IDC_SYSPARAMS_MENU_TEXT */
    {SM_CXVSCROLL,      COLOR_SCROLLBAR,        L"Scrollbar"     }, /* IDC_SYSPARAMS_SCROLLBAR */
    {-1,                COLOR_HIGHLIGHT,        L"Hilight"       }, /* IDC_SYSPARAMS_SELECTION */
    {-1,                COLOR_HIGHLIGHTTEXT,    L"HilightText"   }, /* IDC_SYSPARAMS_SELECTION_TEXT */
    {-1,                COLOR_INFOBK,           L"InfoWindow"    }, /* IDC_SYSPARAMS_TOOLTIP */
    {-1,                COLOR_INFOTEXT,         L"InfoText"      }, /* IDC_SYSPARAMS_TOOLTIP_TEXT */
    {-1,                COLOR_WINDOW,           L"Window"        }, /* IDC_SYSPARAMS_WINDOW */
    {-1,                COLOR_WINDOWTEXT,       L"WindowText"    }, /* IDC_SYSPARAMS_WINDOW_TEXT */
550
    {SM_CYSIZE,         COLOR_ACTIVECAPTION,    L"ActiveTitle"   }, /* IDC_SYSPARAMS_ACTIVE_TITLE */
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    {-1,                COLOR_CAPTIONTEXT,      L"TitleText"     }, /* IDC_SYSPARAMS_ACTIVE_TITLE_TEXT */
    {-1,                COLOR_INACTIVECAPTION,  L"InactiveTitle" }, /* IDC_SYSPARAMS_INACTIVE_TITLE */
    {-1,                COLOR_INACTIVECAPTIONTEXT,L"InactiveTitleText" }, /* IDC_SYSPARAMS_INACTIVE_TITLE_TEXT */
    {-1,                -1,                     L"MsgBoxText"    }, /* IDC_SYSPARAMS_MSGBOX_TEXT */
    {-1,                COLOR_APPWORKSPACE,     L"AppWorkSpace"  }, /* IDC_SYSPARAMS_APPWORKSPACE */
    {-1,                COLOR_WINDOWFRAME,      L"WindowFrame"   }, /* IDC_SYSPARAMS_WINDOW_FRAME */
    {-1,                COLOR_ACTIVEBORDER,     L"ActiveBorder"  }, /* IDC_SYSPARAMS_ACTIVE_BORDER */
    {-1,                COLOR_INACTIVEBORDER,   L"InactiveBorder" }, /* IDC_SYSPARAMS_INACTIVE_BORDER */
    {-1,                COLOR_BTNSHADOW,        L"ButtonShadow"  }, /* IDC_SYSPARAMS_BUTTON_SHADOW */
    {-1,                COLOR_GRAYTEXT,         L"GrayText"      }, /* IDC_SYSPARAMS_GRAY_TEXT */
    {-1,                COLOR_BTNHIGHLIGHT,     L"ButtonHilight" }, /* IDC_SYSPARAMS_BUTTON_HIGHLIGHT */
    {-1,                COLOR_3DDKSHADOW,       L"ButtonDkShadow" }, /* IDC_SYSPARAMS_BUTTON_DARK_SHADOW */
    {-1,                COLOR_3DLIGHT,          L"ButtonLight"   }, /* IDC_SYSPARAMS_BUTTON_LIGHT */
    {-1,                COLOR_ALTERNATEBTNFACE, L"ButtonAlternateFace" }, /* IDC_SYSPARAMS_BUTTON_ALTERNATE */
    {-1,                COLOR_HOTLIGHT,         L"HotTrackingColor" }, /* IDC_SYSPARAMS_HOT_TRACKING */
    {-1,                COLOR_GRADIENTACTIVECAPTION, L"GradientActiveTitle" }, /* IDC_SYSPARAMS_ACTIVE_TITLE_GRADIENT */
    {-1,                COLOR_GRADIENTINACTIVECAPTION, L"GradientInactiveTitle" }, /* IDC_SYSPARAMS_INACTIVE_TITLE_GRADIENT */
    {-1,                COLOR_MENUHILIGHT,      L"MenuHilight"   }, /* IDC_SYSPARAMS_MENU_HIGHLIGHT */
    {-1,                COLOR_MENUBAR,          L"MenuBar"       }, /* IDC_SYSPARAMS_MENUBAR */
570 571 572 573
};

static void save_sys_color(int idx, COLORREF clr)
{
574
    WCHAR buffer[13];
575

576 577
    swprintf(buffer, ARRAY_SIZE(buffer), L"%d %d %d",  GetRValue (clr), GetGValue (clr), GetBValue (clr));
    set_reg_key(HKEY_CURRENT_USER, L"Control Panel\\Colors", metrics[idx].color_reg, buffer);
578 579
}

580
static void set_color_from_theme(const WCHAR *keyName, COLORREF color)
581
{
582
    int i;
583

584
    for (i=0; i < ARRAY_SIZE(metrics); i++)
585
    {
586
        if (wcsicmp(metrics[i].color_reg, keyName)==0)
587 588 589 590 591 592 593 594 595 596
        {
            metrics[i].color = color;
            save_sys_color(i, color);
            break;
        }
    }
}

static void do_parse_theme(WCHAR *file)
{
597 598
    WCHAR *keyName, keyNameValue[MAX_PATH];
    DWORD len, allocLen = 512;
599 600 601 602 603
    WCHAR *keyNamePtr = NULL;
    int red = 0, green = 0, blue = 0;
    COLORREF color;

    WINE_TRACE("%s\n", wine_dbgstr_w(file));
604 605 606 607 608 609 610 611
    keyName = malloc(sizeof(*keyName) * allocLen);
    for (;;)
    {
        assert(keyName);
        len = GetPrivateProfileStringW(L"Control Panel\\Colors", NULL, NULL, keyName,
                                       allocLen, file);
        if (len < allocLen - 2)
            break;
612

613 614 615
        allocLen *= 2;
        keyName = realloc(keyName, sizeof(*keyName) * allocLen);
    }
616 617 618

    keyNamePtr = keyName;
    while (*keyNamePtr!=0) {
619
        GetPrivateProfileStringW(L"Control Panel\\Colors", keyNamePtr, NULL, keyNameValue,
620
                                 MAX_PATH, file);
621 622 623 624

        WINE_TRACE("parsing key: %s with value: %s\n",
                   wine_dbgstr_w(keyNamePtr), wine_dbgstr_w(keyNameValue));

625
        swscanf(keyNameValue, L"%d %d %d", &red, &green, &blue);
626

627
        color = RGB((BYTE)red, (BYTE)green, (BYTE)blue);
628 629 630 631 632
        set_color_from_theme(keyNamePtr, color);

        keyNamePtr+=lstrlenW(keyNamePtr);
        keyNamePtr++;
    }
633
    free(keyName);
634 635
}

636 637
static void on_theme_install(HWND dialog)
{
638
  static const WCHAR filterMask[] = L"\0*.msstyles;*.theme\0";
639 640 641 642 643 644
  OPENFILENAMEW ofn;
  WCHAR filetitle[MAX_PATH];
  WCHAR file[MAX_PATH];
  WCHAR filter[100];
  WCHAR title[100];

645 646
  LoadStringW(GetModuleHandleW(NULL), IDS_THEMEFILE, filter, ARRAY_SIZE(filter) - ARRAY_SIZE(filterMask));
  memcpy(filter + lstrlenW (filter), filterMask, sizeof(filterMask));
647
  LoadStringW(GetModuleHandleW(NULL), IDS_THEMEFILE_SELECT, title, ARRAY_SIZE(title));
648 649

  ofn.lStructSize = sizeof(OPENFILENAMEW);
650
  ofn.hwndOwner = dialog;
651 652 653 654 655 656 657
  ofn.hInstance = 0;
  ofn.lpstrFilter = filter;
  ofn.lpstrCustomFilter = NULL;
  ofn.nMaxCustFilter = 0;
  ofn.nFilterIndex = 0;
  ofn.lpstrFile = file;
  ofn.lpstrFile[0] = '\0';
658
  ofn.nMaxFile = ARRAY_SIZE(file);
659 660
  ofn.lpstrFileTitle = filetitle;
  ofn.lpstrFileTitle[0] = '\0';
661
  ofn.nMaxFileTitle = ARRAY_SIZE(filetitle);
662 663
  ofn.lpstrInitialDir = NULL;
  ofn.lpstrTitle = title;
664
  ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLESIZING;
665 666 667 668 669 670 671 672 673 674 675 676
  ofn.nFileOffset = 0;
  ofn.nFileExtension = 0;
  ofn.lpstrDefExt = NULL;
  ofn.lCustData = 0;
  ofn.lpfnHook = NULL;
  ofn.lpTemplateName = NULL;

  if (GetOpenFileNameW(&ofn))
  {
      WCHAR themeFilePath[MAX_PATH];
      SHFILEOPSTRUCTW shfop;

677
      if (FAILED (SHGetFolderPathW (NULL, CSIDL_RESOURCES|CSIDL_FLAG_CREATE, NULL, 
678 679
          SHGFP_TYPE_CURRENT, themeFilePath))) return;

680
      if (lstrcmpiW(PathFindExtensionW(filetitle), L".theme")==0)
681 682
      {
          do_parse_theme(file);
683
          SendMessageW(GetParent(dialog), PSM_CHANGED, 0, 0);
684 685 686
          return;
      }

687 688 689
      PathRemoveExtensionW (filetitle);

      /* Construct path into which the theme file goes */
690
      lstrcatW (themeFilePath, L"\\themes\\");
691 692 693 694 695 696
      lstrcatW (themeFilePath, filetitle);

      /* Create the directory */
      SHCreateDirectoryExW (dialog, themeFilePath, NULL);

      /* Append theme file name itself */
697
      lstrcatW (themeFilePath, L"\\");
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
      lstrcatW (themeFilePath, PathFindFileNameW (file));
      /* SHFileOperation() takes lists as input, so double-nullterminate */
      themeFilePath[lstrlenW (themeFilePath)+1] = 0;
      file[lstrlenW (file)+1] = 0;

      /* Do the copying */
      WINE_TRACE("copying: %s -> %s\n", wine_dbgstr_w (file), 
          wine_dbgstr_w (themeFilePath));
      shfop.hwnd = dialog;
      shfop.wFunc = FO_COPY;
      shfop.pFrom = file;
      shfop.pTo = themeFilePath;
      shfop.fFlags = FOF_NOCONFIRMMKDIR;
      if (SHFileOperationW (&shfop) == 0)
      {
          scan_theme_files();
          if (!fill_theme_list (GetDlgItem (dialog, IDC_THEME_THEMECOMBO),
              GetDlgItem (dialog, IDC_THEME_COLORCOMBO),
              GetDlgItem (dialog, IDC_THEME_SIZECOMBO)))
          {
718 719
              SendMessageW (GetDlgItem (dialog, IDC_THEME_COLORCOMBO), CB_SETCURSEL, -1, 0);
              SendMessageW (GetDlgItem (dialog, IDC_THEME_SIZECOMBO), CB_SETCURSEL, -1, 0);
720 721 722 723 724 725 726 727 728 729 730 731 732
              enable_size_and_color_controls (dialog, FALSE);
          }
          else
          {
              enable_size_and_color_controls (dialog, TRUE);
          }
      }
      else
          WINE_TRACE("copy operation failed\n");
  }
  else WINE_TRACE("user cancelled\n");
}

733 734 735
/* Information about symbolic link targets of certain User Shell Folders. */
struct ShellFolderInfo {
    int nFolder;
736
    char szLinkTarget[FILENAME_MAX]; /* in unix locale */
737 738
};

739 740
#define CSIDL_DOWNLOADS 0x0047

741 742 743 744 745
static struct ShellFolderInfo asfiInfo[] = {
    { CSIDL_DESKTOP,  "" },
    { CSIDL_PERSONAL, "" },
    { CSIDL_MYPICTURES, "" },
    { CSIDL_MYMUSIC, "" },
746 747 748
    { CSIDL_MYVIDEO, "" },
    { CSIDL_DOWNLOADS, "" },
    { CSIDL_TEMPLATES, "" }
749 750 751 752 753
};

static struct ShellFolderInfo *psfiSelected = NULL;

static void init_shell_folder_listview_headers(HWND dialog) {
754
    LVCOLUMNW listColumn;
755
    RECT viewRect;
756 757
    WCHAR szShellFolder[64] = L"Shell Folder";
    WCHAR szLinksTo[64] = L"Links to";
758 759
    int width;

760 761
    LoadStringW(GetModuleHandleW(NULL), IDS_SHELL_FOLDER, szShellFolder, ARRAY_SIZE(szShellFolder));
    LoadStringW(GetModuleHandleW(NULL), IDS_LINKS_TO, szLinksTo, ARRAY_SIZE(szLinksTo));
762

763
    GetClientRect(GetDlgItem(dialog, IDC_LIST_SFPATHS), &viewRect);
764
    width = (viewRect.right - viewRect.left) / 3;
765 766 767

    listColumn.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
    listColumn.pszText = szShellFolder;
768
    listColumn.cchTextMax = lstrlenW(listColumn.pszText);
769 770
    listColumn.cx = width;

771
    SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_INSERTCOLUMNW, 0, (LPARAM) &listColumn);
772 773

    listColumn.pszText = szLinksTo;
774
    listColumn.cchTextMax = lstrlenW(listColumn.pszText);
775 776
    listColumn.cx = viewRect.right - viewRect.left - width - 1;

777
    SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_INSERTCOLUMNW, 1, (LPARAM) &listColumn);
778 779 780
}

/* Reads the currently set shell folder symbol link targets into asfiInfo. */
781
static void read_shell_folder_link_targets(void) {
782 783
    WCHAR wszPath[MAX_PATH];
    int i;
784 785

    for (i=0; i<ARRAY_SIZE(asfiInfo); i++) {
786
        asfiInfo[i].szLinkTarget[0] = '\0';
787 788 789 790
        if (SUCCEEDED( SHGetFolderPathW( NULL, asfiInfo[i].nFolder | CSIDL_FLAG_DONT_VERIFY, NULL,
                                         SHGFP_TYPE_CURRENT, wszPath )))
            query_shell_folder( wszPath, asfiInfo[i].szLinkTarget, FILENAME_MAX );
    }
791 792 793 794
}

static void update_shell_folder_listview(HWND dialog) {
    int i;
795
    LVITEMW item;
796
    LONG lSelected = SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_GETNEXTITEM, -1,
797
                                        MAKELPARAM(LVNI_SELECTED,0));
798 799

    SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_DELETEALLITEMS, 0, 0);
800

801
    for (i=0; i<ARRAY_SIZE(asfiInfo); i++) {
802
        WCHAR buffer[MAX_PATH];
803 804 805 806 807 808 809 810 811 812 813 814 815
        HRESULT hr;
        LPITEMIDLIST pidlCurrent;

        /* Some acrobatic to get the localized name of the shell folder */
        hr = SHGetFolderLocation(dialog, asfiInfo[i].nFolder, NULL, 0, &pidlCurrent);
        if (SUCCEEDED(hr)) { 
            LPSHELLFOLDER psfParent;
            LPCITEMIDLIST pidlLast;
            hr = SHBindToParent(pidlCurrent, &IID_IShellFolder, (LPVOID*)&psfParent, &pidlLast);
            if (SUCCEEDED(hr)) {
                STRRET strRet;
                hr = IShellFolder_GetDisplayNameOf(psfParent, pidlLast, SHGDN_FORADDRESSBAR, &strRet);
                if (SUCCEEDED(hr)) {
816
                    hr = StrRetToBufW(&strRet, pidlLast, buffer, MAX_PATH);
817 818 819 820 821 822 823 824 825
                }
                IShellFolder_Release(psfParent);
            }
            ILFree(pidlCurrent);
        }

        /* If there's a dangling symlink for the current shell folder, SHGetFolderLocation
         * will fail above. We fall back to the (non-verified) path of the shell folder. */
        if (FAILED(hr)) {
826
            hr = SHGetFolderPathW(dialog, asfiInfo[i].nFolder|CSIDL_FLAG_DONT_VERIFY, NULL,
827 828 829 830 831 832 833 834
                                 SHGFP_TYPE_CURRENT, buffer);
        }
    
        item.mask = LVIF_TEXT | LVIF_PARAM;
        item.iItem = i;
        item.iSubItem = 0;
        item.pszText = buffer;
        item.lParam = (LPARAM)&asfiInfo[i];
835
        SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_INSERTITEMW, 0, (LPARAM)&item);
836 837 838 839

        item.mask = LVIF_TEXT;
        item.iItem = i;
        item.iSubItem = 1;
840
        item.pszText = strdupU2W(asfiInfo[i].szLinkTarget);
841
        SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_SETITEMW, 0, (LPARAM)&item);
842
        free(item.pszText);
843 844 845 846 847 848 849
    }

    /* Ensure that the previously selected item is selected again. */
    if (lSelected >= 0) {
        item.mask = LVIF_STATE;
        item.state = LVIS_SELECTED;
        item.stateMask = LVIS_SELECTED;
850
        SendDlgItemMessageW(dialog, IDC_LIST_SFPATHS, LVM_SETITEMSTATE, lSelected, (LPARAM)&item);
851 852 853 854 855 856 857
    }
}

static void on_shell_folder_selection_changed(HWND hDlg, LPNMLISTVIEW lpnm) {
    if (lpnm->uNewState & LVIS_SELECTED) {
        psfiSelected = (struct ShellFolderInfo *)lpnm->lParam;
        EnableWindow(GetDlgItem(hDlg, IDC_LINK_SFPATH), 1);
858
        if (*psfiSelected->szLinkTarget) {
859
            WCHAR *link;
860 861 862
            CheckDlgButton(hDlg, IDC_LINK_SFPATH, BST_CHECKED);
            EnableWindow(GetDlgItem(hDlg, IDC_EDIT_SFPATH), 1);
            EnableWindow(GetDlgItem(hDlg, IDC_BROWSE_SFPATH), 1);
863 864
            link = strdupU2W(psfiSelected->szLinkTarget);
            set_textW(hDlg, IDC_EDIT_SFPATH, link);
865
            free(link);
866 867 868 869
        } else {
            CheckDlgButton(hDlg, IDC_LINK_SFPATH, BST_UNCHECKED);
            EnableWindow(GetDlgItem(hDlg, IDC_EDIT_SFPATH), 0);
            EnableWindow(GetDlgItem(hDlg, IDC_BROWSE_SFPATH), 0);
870
            set_text(hDlg, IDC_EDIT_SFPATH, "");
871 872 873 874
        }
    } else {
        psfiSelected = NULL;
        CheckDlgButton(hDlg, IDC_LINK_SFPATH, BST_UNCHECKED);
875
        set_text(hDlg, IDC_EDIT_SFPATH, "");
876 877 878 879 880 881 882 883 884
        EnableWindow(GetDlgItem(hDlg, IDC_LINK_SFPATH), 0);
        EnableWindow(GetDlgItem(hDlg, IDC_EDIT_SFPATH), 0);
        EnableWindow(GetDlgItem(hDlg, IDC_BROWSE_SFPATH), 0);
    }
}

/* Keep the contents of the edit control, the listview control and the symlink 
 * information in sync. */
static void on_shell_folder_edit_changed(HWND hDlg) {
885
    LVITEMW item;
886
    WCHAR *text = get_text(hDlg, IDC_EDIT_SFPATH);
887 888 889
    LONG iSel = SendDlgItemMessageW(hDlg, IDC_LIST_SFPATHS, LVM_GETNEXTITEM, -1,
                                    MAKELPARAM(LVNI_SELECTED,0));

890
    if (!text || !psfiSelected || iSel < 0) {
891
        free(text);
892 893 894
        return;
    }

895 896
    WideCharToMultiByte(CP_UNIXCP, 0, text, -1,
                        psfiSelected->szLinkTarget, FILENAME_MAX, NULL, NULL);
897 898 899 900

    item.mask = LVIF_TEXT;
    item.iItem = iSel;
    item.iSubItem = 1;
901
    item.pszText = text;
902
    SendDlgItemMessageW(hDlg, IDC_LIST_SFPATHS, LVM_SETITEMW, 0, (LPARAM)&item);
903

904
    free(text);
905

906
    SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
907 908
}

909
static void apply_shell_folder_changes(void) {
910
    WCHAR wszPath[MAX_PATH];
911
    int i;
912

913
    for (i=0; i<ARRAY_SIZE(asfiInfo); i++) {
914 915 916
        if (SUCCEEDED( SHGetFolderPathW( NULL, asfiInfo[i].nFolder | CSIDL_FLAG_CREATE, NULL,
                                         SHGFP_TYPE_CURRENT, wszPath )))
            set_shell_folder( wszPath, asfiInfo[i].szLinkTarget );
917 918 919
    }
}

920 921 922 923
static void refresh_sysparams(HWND hDlg)
{
    int i;

924
    for (i = 0; i < ARRAY_SIZE(metrics); i++)
925 926 927 928 929 930 931 932 933 934
    {
        if (metrics[i].sm_idx != -1)
            metrics[i].size = GetSystemMetrics(metrics[i].sm_idx);
        if (metrics[i].color_idx != -1)
            metrics[i].color = GetSysColor(metrics[i].color_idx);
    }

    on_sysparam_change(hDlg);
}

935 936 937 938
static void read_sysparams(HWND hDlg)
{
    WCHAR buffer[256];
    HWND list = GetDlgItem(hDlg, IDC_SYSPARAM_COMBO);
939
    NONCLIENTMETRICSW nonclient_metrics;
940 941
    int i, idx;

942
    for (i = 0; i < ARRAY_SIZE(metrics); i++)
943
    {
944
        LoadStringW(GetModuleHandleW(NULL), i + IDC_SYSPARAMS_BUTTON, buffer, ARRAY_SIZE(buffer));
945 946 947 948 949 950 951 952
        idx = SendMessageW(list, CB_ADDSTRING, 0, (LPARAM)buffer);
        if (idx != CB_ERR) SendMessageW(list, CB_SETITEMDATA, idx, i);

        if (metrics[i].sm_idx != -1)
            metrics[i].size = GetSystemMetrics(metrics[i].sm_idx);
        if (metrics[i].color_idx != -1)
            metrics[i].color = GetSysColor(metrics[i].color_idx);
    }
953 954 955 956 957 958 959 960 961 962 963 964

    nonclient_metrics.cbSize = sizeof(NONCLIENTMETRICSW);
    SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICSW), &nonclient_metrics, 0);

    memcpy(&(metrics[IDC_SYSPARAMS_MENU_TEXT - IDC_SYSPARAMS_BUTTON].lf),
           &(nonclient_metrics.lfMenuFont), sizeof(LOGFONTW));
    memcpy(&(metrics[IDC_SYSPARAMS_ACTIVE_TITLE_TEXT - IDC_SYSPARAMS_BUTTON].lf),
           &(nonclient_metrics.lfCaptionFont), sizeof(LOGFONTW));
    memcpy(&(metrics[IDC_SYSPARAMS_TOOLTIP_TEXT - IDC_SYSPARAMS_BUTTON].lf),
           &(nonclient_metrics.lfStatusFont), sizeof(LOGFONTW));
    memcpy(&(metrics[IDC_SYSPARAMS_MSGBOX_TEXT - IDC_SYSPARAMS_BUTTON].lf),
           &(nonclient_metrics.lfMessageFont), sizeof(LOGFONTW));
965 966 967 968
}

static void apply_sysparams(void)
{
969
    NONCLIENTMETRICSW ncm;
970
    int i, cnt = 0;
971 972
    int colors_idx[ARRAY_SIZE(metrics)];
    COLORREF colors[ARRAY_SIZE(metrics)];
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
    HDC hdc;
    int dpi;

    hdc = GetDC( 0 );
    dpi = GetDeviceCaps( hdc, LOGPIXELSY );
    ReleaseDC( 0, hdc );

    ncm.cbSize = sizeof(ncm);
    SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);

    /* convert metrics back to twips */
    ncm.iMenuWidth = ncm.iMenuHeight =
        MulDiv( metrics[IDC_SYSPARAMS_MENU - IDC_SYSPARAMS_BUTTON].size, -1440, dpi );
    ncm.iCaptionWidth = ncm.iCaptionHeight =
        MulDiv( metrics[IDC_SYSPARAMS_ACTIVE_TITLE - IDC_SYSPARAMS_BUTTON].size, -1440, dpi );
    ncm.iScrollWidth = ncm.iScrollHeight =
        MulDiv( metrics[IDC_SYSPARAMS_SCROLLBAR - IDC_SYSPARAMS_BUTTON].size, -1440, dpi );
    ncm.iSmCaptionWidth = MulDiv( ncm.iSmCaptionWidth, -1440, dpi );
    ncm.iSmCaptionHeight = MulDiv( ncm.iSmCaptionHeight, -1440, dpi );

    ncm.lfMenuFont    = metrics[IDC_SYSPARAMS_MENU_TEXT - IDC_SYSPARAMS_BUTTON].lf;
    ncm.lfCaptionFont = metrics[IDC_SYSPARAMS_ACTIVE_TITLE_TEXT - IDC_SYSPARAMS_BUTTON].lf;
    ncm.lfStatusFont  = metrics[IDC_SYSPARAMS_TOOLTIP_TEXT - IDC_SYSPARAMS_BUTTON].lf;
    ncm.lfMessageFont = metrics[IDC_SYSPARAMS_MSGBOX_TEXT - IDC_SYSPARAMS_BUTTON].lf;

    SystemParametersInfoW(SPI_SETNONCLIENTMETRICS, sizeof(ncm), &ncm,
999 1000
                          SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);

1001
    for (i = 0; i < ARRAY_SIZE(metrics); i++)
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
        if (metrics[i].color_idx != -1)
        {
            colors_idx[cnt] = metrics[i].color_idx;
            colors[cnt++] = metrics[i].color;
        }
    SetSysColors(cnt, colors_idx, colors);
}

static void on_sysparam_change(HWND hDlg)
{
    int index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETCURSEL, 0, 0);

    index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETITEMDATA, index, 0);

    updating_ui = TRUE;

    EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_COLOR_TEXT), metrics[index].color_idx != -1);
    EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_COLOR), metrics[index].color_idx != -1);
    InvalidateRect(GetDlgItem(hDlg, IDC_SYSPARAM_COLOR), NULL, TRUE);

    EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_SIZE_TEXT), metrics[index].sm_idx != -1);
    EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_SIZE), metrics[index].sm_idx != -1);
    EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_SIZE_UD), metrics[index].sm_idx != -1);
    if (metrics[index].sm_idx != -1)
        SendDlgItemMessageW(hDlg, IDC_SYSPARAM_SIZE_UD, UDM_SETPOS, 0, MAKELONG(metrics[index].size, 0));
    else
        set_text(hDlg, IDC_SYSPARAM_SIZE, "");

1030 1031 1032 1033 1034 1035 1036
    EnableWindow(GetDlgItem(hDlg, IDC_SYSPARAM_FONT),
        index == IDC_SYSPARAMS_MENU_TEXT-IDC_SYSPARAMS_BUTTON ||
        index == IDC_SYSPARAMS_ACTIVE_TITLE_TEXT-IDC_SYSPARAMS_BUTTON ||
        index == IDC_SYSPARAMS_TOOLTIP_TEXT-IDC_SYSPARAMS_BUTTON ||
        index == IDC_SYSPARAMS_MSGBOX_TEXT-IDC_SYSPARAMS_BUTTON
    );

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    updating_ui = FALSE;
}

static void on_draw_item(HWND hDlg, WPARAM wParam, LPARAM lParam)
{
    static HBRUSH black_brush = 0;
    LPDRAWITEMSTRUCT draw_info = (LPDRAWITEMSTRUCT)lParam;

    if (!black_brush) black_brush = CreateSolidBrush(0);

    if (draw_info->CtlID == IDC_SYSPARAM_COLOR)
    {
1049 1050 1051
        UINT state;
        HTHEME theme;
        RECT buttonrect;
1052

1053
        theme = OpenThemeDataForDpi(NULL, WC_BUTTONW, GetDpiForWindow(hDlg));
1054

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
        if (theme) {
            MARGINS margins;

            if (draw_info->itemState & ODS_DISABLED)
                state = PBS_DISABLED;
            else if (draw_info->itemState & ODS_SELECTED)
                state = PBS_PRESSED;
            else
                state = PBS_NORMAL;

            if (IsThemeBackgroundPartiallyTransparent(theme, BP_PUSHBUTTON, state))
                DrawThemeParentBackground(draw_info->hwndItem, draw_info->hDC, NULL);

            DrawThemeBackground(theme, draw_info->hDC, BP_PUSHBUTTON, state, &draw_info->rcItem, NULL);

            buttonrect = draw_info->rcItem;

            GetThemeMargins(theme, draw_info->hDC, BP_PUSHBUTTON, state, TMT_CONTENTMARGINS, &draw_info->rcItem, &margins);

            buttonrect.left += margins.cxLeftWidth;
            buttonrect.top += margins.cyTopHeight;
            buttonrect.right -= margins.cxRightWidth;
            buttonrect.bottom -= margins.cyBottomHeight;

            if (draw_info->itemState & ODS_FOCUS)
                DrawFocusRect(draw_info->hDC, &buttonrect);

            CloseThemeData(theme);
        } else {
            state = DFCS_ADJUSTRECT | DFCS_BUTTONPUSH;

            if (draw_info->itemState & ODS_DISABLED)
                state |= DFCS_INACTIVE;
            else
                state |= draw_info->itemState & ODS_SELECTED ? DFCS_PUSHED : 0;

            DrawFrameControl(draw_info->hDC, &draw_info->rcItem, DFC_BUTTON, state);

            buttonrect = draw_info->rcItem;
        }
1095 1096 1097 1098 1099 1100 1101 1102 1103

        if (!(draw_info->itemState & ODS_DISABLED))
        {
            HBRUSH brush;
            int index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETCURSEL, 0, 0);

            index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETITEMDATA, index, 0);
            brush = CreateSolidBrush(metrics[index].color);

1104 1105 1106 1107
            InflateRect(&buttonrect, -1, -1);
            FrameRect(draw_info->hDC, &buttonrect, black_brush);
            InflateRect(&buttonrect, -1, -1);
            FillRect(draw_info->hDC, &buttonrect, brush);
1108 1109 1110 1111 1112
            DeleteObject(brush);
        }
    }
}

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
static void on_select_font(HWND hDlg)
{
    CHOOSEFONTW cf;
    int index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETCURSEL, 0, 0);
    index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETITEMDATA, index, 0);

    ZeroMemory(&cf, sizeof(cf));
    cf.lStructSize = sizeof(CHOOSEFONTW);
    cf.hwndOwner = hDlg;
    cf.lpLogFont = &(metrics[index].lf);
1123
    cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT | CF_NOSCRIPTSEL | CF_NOVERTFONTS;
1124

1125 1126
    if (ChooseFontW(&cf))
        SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1127 1128
}

1129 1130
static void init_mime_types(HWND hDlg)
{
1131
    WCHAR *buf = get_reg_key(config_key, keypath(L"FileOpenAssociations"), L"Enable", L"Y");
1132 1133 1134 1135
    int state = IS_OPTION_TRUE(*buf) ? BST_CHECKED : BST_UNCHECKED;

    CheckDlgButton(hDlg, IDC_ENABLE_FILE_ASSOCIATIONS, state);

1136
    free(buf);
1137 1138 1139 1140
}

static void update_mime_types(HWND hDlg)
{
1141
    const WCHAR *state = L"Y";
1142 1143

    if (IsDlgButtonChecked(hDlg, IDC_ENABLE_FILE_ASSOCIATIONS) != BST_CHECKED)
1144
        state = L"N";
1145

1146
    set_reg_key(config_key, keypath(L"FileOpenAssociations"), L"Enable", state);
1147 1148
}

1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
static BOOL CALLBACK update_window_pos_proc(HWND hwnd, LPARAM lp)
{
    RECT rect;

    GetClientRect(hwnd, &rect);
    AdjustWindowRectEx(&rect, GetWindowLongW(hwnd, GWL_STYLE), !!GetMenu(hwnd),
                       GetWindowLongW(hwnd, GWL_EXSTYLE));
    SetWindowPos(hwnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top,
                 SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
    return TRUE;
}

/* Adjust the rectangle for top-level windows because the new non-client metrics may be different */
static void update_window_pos(void)
{
    EnumWindows(update_window_pos_proc, 0);
}

1167 1168 1169 1170 1171
INT_PTR CALLBACK
ThemeDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg) {
        case WM_INITDIALOG:
1172 1173 1174
            read_shell_folder_link_targets();
            init_shell_folder_listview_headers(hDlg);
            update_shell_folder_listview(hDlg);
1175
            read_sysparams(hDlg);
1176
            init_mime_types(hDlg);
1177
            init_dialog(hDlg);
1178
            break;
1179

1180 1181 1182 1183 1184 1185 1186
        case WM_DESTROY:
            free_theme_files();
            break;

        case WM_SHOWWINDOW:
            set_window_title(hDlg);
            break;
1187

1188 1189 1190 1191 1192 1193
        case WM_COMMAND:
            switch(HIWORD(wParam)) {
                case CBN_SELCHANGE: {
                    if (updating_ui) break;
                    switch (LOWORD(wParam))
                    {
1194
                        case IDC_THEME_APPCOMBO: /* fall through */
1195 1196 1197
                        case IDC_THEME_THEMECOMBO: on_theme_changed(hDlg); break;
                        case IDC_THEME_COLORCOMBO: /* fall through */
                        case IDC_THEME_SIZECOMBO: theme_dirty = TRUE; break;
1198
                        case IDC_SYSPARAM_COMBO: on_sysparam_change(hDlg); return FALSE;
1199
                    }
1200
                    SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1201 1202
                    break;
                }
1203
                case EN_CHANGE: {
1204 1205 1206 1207 1208 1209
                    if (updating_ui) break;
                    switch (LOWORD(wParam))
                    {
                        case IDC_EDIT_SFPATH: on_shell_folder_edit_changed(hDlg); break;
                        case IDC_SYSPARAM_SIZE:
                        {
1210
                            WCHAR *text = get_text(hDlg, IDC_SYSPARAM_SIZE);
1211 1212 1213
                            int index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETCURSEL, 0, 0);

                            index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETITEMDATA, index, 0);
1214 1215 1216

                            if (text)
                            {
1217
                                metrics[index].size = wcstol(text, NULL, 10);
1218
                                free(text);
1219 1220 1221 1222 1223 1224
                            }
                            else
                            {
                                /* for empty string set to minimum value */
                                SendDlgItemMessageW(hDlg, IDC_SYSPARAM_SIZE_UD, UDM_GETRANGE32, (WPARAM)&metrics[index].size, 0);
                            }
1225

1226
                            SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1227 1228 1229
                            break;
                        }
                    }
1230
                    break;
1231 1232 1233 1234 1235 1236 1237 1238
                }
                case BN_CLICKED:
                    switch (LOWORD(wParam))
                    {
                        case IDC_THEME_INSTALL:
                            on_theme_install (hDlg);
                            break;

1239 1240 1241 1242
                        case IDC_SYSPARAM_FONT:
                            on_select_font(hDlg);
                            break;

1243
                        case IDC_BROWSE_SFPATH:
1244 1245 1246 1247 1248 1249
                        {
                            WCHAR link[FILENAME_MAX];
                            if (browse_for_unix_folder(hDlg, link)) {
                                WideCharToMultiByte(CP_UNIXCP, 0, link, -1,
                                                    psfiSelected->szLinkTarget, FILENAME_MAX,
                                                    NULL, NULL);
1250
                                update_shell_folder_listview(hDlg);
1251
                                SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1252 1253
                            }
                            break;
1254
                        }
1255 1256 1257

                        case IDC_LINK_SFPATH:
                            if (IsDlgButtonChecked(hDlg, IDC_LINK_SFPATH)) {
1258 1259 1260 1261 1262
                                WCHAR link[FILENAME_MAX];
                                if (browse_for_unix_folder(hDlg, link)) {
                                    WideCharToMultiByte(CP_UNIXCP, 0, link, -1,
                                                        psfiSelected->szLinkTarget, FILENAME_MAX,
                                                        NULL, NULL);
1263
                                    update_shell_folder_listview(hDlg);
1264
                                    SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1265 1266 1267 1268 1269 1270
                                } else {
                                    CheckDlgButton(hDlg, IDC_LINK_SFPATH, BST_UNCHECKED);
                                }
                            } else {
                                psfiSelected->szLinkTarget[0] = '\0';
                                update_shell_folder_listview(hDlg);
1271
                                SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1272 1273
                            }
                            break;    
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293

                        case IDC_SYSPARAM_COLOR:
                        {
                            static COLORREF user_colors[16];
                            CHOOSECOLORW c_color;
                            int index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETCURSEL, 0, 0);

                            index = SendDlgItemMessageW(hDlg, IDC_SYSPARAM_COMBO, CB_GETITEMDATA, index, 0);

                            memset(&c_color, 0, sizeof(c_color));
                            c_color.lStructSize = sizeof(c_color);
                            c_color.lpCustColors = user_colors;
                            c_color.rgbResult = metrics[index].color;
                            c_color.Flags = CC_ANYCOLOR | CC_RGBINIT;
                            c_color.hwndOwner = hDlg;
                            if (ChooseColorW(&c_color))
                            {
                                metrics[index].color = c_color.rgbResult;
                                save_sys_color(index, metrics[index].color);
                                InvalidateRect(GetDlgItem(hDlg, IDC_SYSPARAM_COLOR), NULL, TRUE);
1294
                                SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1295 1296 1297
                            }
                            break;
                        }
1298 1299 1300

                        case IDC_ENABLE_FILE_ASSOCIATIONS:
                            update_mime_types(hDlg);
1301
                            SendMessageW(GetParent(hDlg), PSM_CHANGED, 0, 0);
1302
                            break;
1303
                    }
1304 1305
                    break;
            }
1306 1307 1308 1309 1310
            break;
        
        case WM_NOTIFY:
            switch (((LPNMHDR)lParam)->code) {
                case PSN_KILLACTIVE: {
1311
                    SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, FALSE);
1312 1313 1314 1315 1316
                    break;
                }
                case PSN_APPLY: {
                    apply();
                    apply_theme(hDlg);
1317
                    apply_shell_folder_changes();
1318
                    apply_sysparams();
1319 1320
                    read_shell_folder_link_targets();
                    update_shell_folder_listview(hDlg);
1321
                    update_mime_types(hDlg);
1322
                    update_window_pos();
1323
                    SetWindowLongPtrW(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
1324 1325
                    break;
                }
1326 1327 1328 1329 1330
                case LVN_ITEMCHANGED: { 
                    if (wParam == IDC_LIST_SFPATHS)  
                        on_shell_folder_selection_changed(hDlg, (LPNMLISTVIEW)lParam);
                    break;
                }
1331
                case PSN_SETACTIVE: {
1332
                    update_dialog(hDlg);
1333 1334 1335 1336 1337
                    break;
                }
            }
            break;

1338 1339 1340 1341
        case WM_DRAWITEM:
            on_draw_item(hDlg, wParam, lParam);
            break;

1342 1343 1344 1345 1346
        default:
            break;
    }
    return FALSE;
}