michael@0: /* vim: se cin sw=2 ts=2 et : */ michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: #include "mozilla/WindowsVersion.h" michael@0: michael@0: #include "nsUXThemeData.h" michael@0: #include "nsDebug.h" michael@0: #include "nsToolkit.h" michael@0: #include "nsUXThemeConstants.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::widget; michael@0: michael@0: const wchar_t michael@0: nsUXThemeData::kThemeLibraryName[] = L"uxtheme.dll"; michael@0: michael@0: HANDLE michael@0: nsUXThemeData::sThemes[eUXNumClasses]; michael@0: michael@0: HMODULE michael@0: nsUXThemeData::sThemeDLL = nullptr; michael@0: michael@0: bool michael@0: nsUXThemeData::sFlatMenus = false; michael@0: michael@0: bool nsUXThemeData::sTitlebarInfoPopulatedAero = false; michael@0: bool nsUXThemeData::sTitlebarInfoPopulatedThemed = false; michael@0: SIZE nsUXThemeData::sCommandButtons[4]; michael@0: michael@0: void michael@0: nsUXThemeData::Teardown() { michael@0: Invalidate(); michael@0: if(sThemeDLL) michael@0: FreeLibrary(sThemeDLL); michael@0: } michael@0: michael@0: void michael@0: nsUXThemeData::Initialize() michael@0: { michael@0: ::ZeroMemory(sThemes, sizeof(sThemes)); michael@0: NS_ASSERTION(!sThemeDLL, "nsUXThemeData being initialized twice!"); michael@0: michael@0: CheckForCompositor(true); michael@0: Invalidate(); michael@0: } michael@0: michael@0: void michael@0: nsUXThemeData::Invalidate() { michael@0: for(int i = 0; i < eUXNumClasses; i++) { michael@0: if(sThemes[i]) { michael@0: CloseThemeData(sThemes[i]); michael@0: sThemes[i] = nullptr; michael@0: } michael@0: } michael@0: BOOL useFlat = FALSE; michael@0: sFlatMenus = ::SystemParametersInfo(SPI_GETFLATMENU, 0, &useFlat, 0) ? michael@0: useFlat : false; michael@0: } michael@0: michael@0: HANDLE michael@0: nsUXThemeData::GetTheme(nsUXThemeClass cls) { michael@0: NS_ASSERTION(cls < eUXNumClasses, "Invalid theme class!"); michael@0: if(!sThemes[cls]) michael@0: { michael@0: sThemes[cls] = OpenThemeData(nullptr, GetClassName(cls)); michael@0: } michael@0: return sThemes[cls]; michael@0: } michael@0: michael@0: HMODULE michael@0: nsUXThemeData::GetThemeDLL() { michael@0: if (!sThemeDLL) michael@0: sThemeDLL = ::LoadLibraryW(kThemeLibraryName); michael@0: return sThemeDLL; michael@0: } michael@0: michael@0: const wchar_t *nsUXThemeData::GetClassName(nsUXThemeClass cls) { michael@0: switch(cls) { michael@0: case eUXButton: michael@0: return L"Button"; michael@0: case eUXEdit: michael@0: return L"Edit"; michael@0: case eUXTooltip: michael@0: return L"Tooltip"; michael@0: case eUXRebar: michael@0: return L"Rebar"; michael@0: case eUXMediaRebar: michael@0: return L"Media::Rebar"; michael@0: case eUXCommunicationsRebar: michael@0: return L"Communications::Rebar"; michael@0: case eUXBrowserTabBarRebar: michael@0: return L"BrowserTabBar::Rebar"; michael@0: case eUXToolbar: michael@0: return L"Toolbar"; michael@0: case eUXMediaToolbar: michael@0: return L"Media::Toolbar"; michael@0: case eUXCommunicationsToolbar: michael@0: return L"Communications::Toolbar"; michael@0: case eUXProgress: michael@0: return L"Progress"; michael@0: case eUXTab: michael@0: return L"Tab"; michael@0: case eUXScrollbar: michael@0: return L"Scrollbar"; michael@0: case eUXTrackbar: michael@0: return L"Trackbar"; michael@0: case eUXSpin: michael@0: return L"Spin"; michael@0: case eUXStatus: michael@0: return L"Status"; michael@0: case eUXCombobox: michael@0: return L"Combobox"; michael@0: case eUXHeader: michael@0: return L"Header"; michael@0: case eUXListview: michael@0: return L"Listview"; michael@0: case eUXMenu: michael@0: return L"Menu"; michael@0: case eUXWindowFrame: michael@0: return L"Window"; michael@0: default: michael@0: NS_NOTREACHED("unknown uxtheme class"); michael@0: return L""; michael@0: } michael@0: } michael@0: michael@0: // static michael@0: void michael@0: nsUXThemeData::InitTitlebarInfo() michael@0: { michael@0: // Pre-populate with generic metrics. These likley will not match michael@0: // the current theme, but they insure the buttons at least show up. michael@0: sCommandButtons[0].cx = GetSystemMetrics(SM_CXSIZE); michael@0: sCommandButtons[0].cy = GetSystemMetrics(SM_CYSIZE); michael@0: sCommandButtons[1].cx = sCommandButtons[2].cx = sCommandButtons[0].cx; michael@0: sCommandButtons[1].cy = sCommandButtons[2].cy = sCommandButtons[0].cy; michael@0: sCommandButtons[3].cx = sCommandButtons[0].cx * 3; michael@0: sCommandButtons[3].cy = sCommandButtons[0].cy; michael@0: michael@0: // Use system metrics for pre-vista, otherwise trigger a michael@0: // refresh on the next layout. michael@0: sTitlebarInfoPopulatedAero = sTitlebarInfoPopulatedThemed = michael@0: !IsVistaOrLater(); michael@0: } michael@0: michael@0: // static michael@0: void michael@0: nsUXThemeData::UpdateTitlebarInfo(HWND aWnd) michael@0: { michael@0: if (!aWnd) michael@0: return; michael@0: michael@0: if (!sTitlebarInfoPopulatedAero && nsUXThemeData::CheckForCompositor()) { michael@0: RECT captionButtons; michael@0: if (SUCCEEDED(WinUtils::dwmGetWindowAttributePtr(aWnd, michael@0: DWMWA_CAPTION_BUTTON_BOUNDS, michael@0: &captionButtons, michael@0: sizeof(captionButtons)))) { michael@0: sCommandButtons[CMDBUTTONIDX_BUTTONBOX].cx = captionButtons.right - captionButtons.left - 3; michael@0: sCommandButtons[CMDBUTTONIDX_BUTTONBOX].cy = (captionButtons.bottom - captionButtons.top) - 1; michael@0: sTitlebarInfoPopulatedAero = true; michael@0: } michael@0: } michael@0: michael@0: if (sTitlebarInfoPopulatedThemed) michael@0: return; michael@0: michael@0: // Query a temporary, visible window with command buttons to get michael@0: // the right metrics. michael@0: nsAutoString className; michael@0: className.AssignLiteral(kClassNameTemp); michael@0: WNDCLASSW wc; michael@0: wc.style = 0; michael@0: wc.lpfnWndProc = ::DefWindowProcW; michael@0: wc.cbClsExtra = 0; michael@0: wc.cbWndExtra = 0; michael@0: wc.hInstance = nsToolkit::mDllInstance; michael@0: wc.hIcon = nullptr; michael@0: wc.hCursor = nullptr; michael@0: wc.hbrBackground = nullptr; michael@0: wc.lpszMenuName = nullptr; michael@0: wc.lpszClassName = className.get(); michael@0: ::RegisterClassW(&wc); michael@0: michael@0: // Create a transparent descendant of the window passed in. This michael@0: // keeps the window from showing up on the desktop or the taskbar. michael@0: // Note the parent (browser) window is usually still hidden, we michael@0: // don't want to display it, so we can't query it directly. michael@0: HWND hWnd = CreateWindowExW(WS_EX_LAYERED, michael@0: className.get(), L"", michael@0: WS_OVERLAPPEDWINDOW, michael@0: 0, 0, 0, 0, aWnd, nullptr, michael@0: nsToolkit::mDllInstance, nullptr); michael@0: NS_ASSERTION(hWnd, "UpdateTitlebarInfo window creation failed."); michael@0: michael@0: ShowWindow(hWnd, SW_SHOW); michael@0: TITLEBARINFOEX info = {0}; michael@0: info.cbSize = sizeof(TITLEBARINFOEX); michael@0: SendMessage(hWnd, WM_GETTITLEBARINFOEX, 0, (LPARAM)&info); michael@0: DestroyWindow(hWnd); michael@0: michael@0: // Only set if we have valid data for all three buttons we use. michael@0: if ((info.rgrect[2].right - info.rgrect[2].left) == 0 || michael@0: (info.rgrect[3].right - info.rgrect[3].left) == 0 || michael@0: (info.rgrect[5].right - info.rgrect[5].left) == 0) { michael@0: NS_WARNING("WM_GETTITLEBARINFOEX query failed to find usable metrics."); michael@0: return; michael@0: } michael@0: // minimize michael@0: sCommandButtons[0].cx = info.rgrect[2].right - info.rgrect[2].left; michael@0: sCommandButtons[0].cy = info.rgrect[2].bottom - info.rgrect[2].top; michael@0: // maximize/restore michael@0: sCommandButtons[1].cx = info.rgrect[3].right - info.rgrect[3].left; michael@0: sCommandButtons[1].cy = info.rgrect[3].bottom - info.rgrect[3].top; michael@0: // close michael@0: sCommandButtons[2].cx = info.rgrect[5].right - info.rgrect[5].left; michael@0: sCommandButtons[2].cy = info.rgrect[5].bottom - info.rgrect[5].top; michael@0: michael@0: sTitlebarInfoPopulatedThemed = true; michael@0: } michael@0: michael@0: // visual style (aero glass, aero basic) michael@0: // theme (aero, luna, zune) michael@0: // theme color (silver, olive, blue) michael@0: // system colors michael@0: michael@0: struct THEMELIST { michael@0: LPCWSTR name; michael@0: int type; michael@0: }; michael@0: michael@0: const THEMELIST knownThemes[] = { michael@0: { L"aero.msstyles", WINTHEME_AERO }, michael@0: { L"aerolite.msstyles", WINTHEME_AERO_LITE }, michael@0: { L"luna.msstyles", WINTHEME_LUNA }, michael@0: { L"zune.msstyles", WINTHEME_ZUNE }, michael@0: { L"royale.msstyles", WINTHEME_ROYALE } michael@0: }; michael@0: michael@0: const THEMELIST knownColors[] = { michael@0: { L"normalcolor", WINTHEMECOLOR_NORMAL }, michael@0: { L"homestead", WINTHEMECOLOR_HOMESTEAD }, michael@0: { L"metallic", WINTHEMECOLOR_METALLIC } michael@0: }; michael@0: michael@0: LookAndFeel::WindowsTheme michael@0: nsUXThemeData::sThemeId = LookAndFeel::eWindowsTheme_Generic; michael@0: michael@0: bool michael@0: nsUXThemeData::sIsDefaultWindowsTheme = false; michael@0: bool michael@0: nsUXThemeData::sIsHighContrastOn = false; michael@0: michael@0: // static michael@0: LookAndFeel::WindowsTheme michael@0: nsUXThemeData::GetNativeThemeId() michael@0: { michael@0: return sThemeId; michael@0: } michael@0: michael@0: // static michael@0: bool nsUXThemeData::IsDefaultWindowTheme() michael@0: { michael@0: return sIsDefaultWindowsTheme; michael@0: } michael@0: michael@0: bool nsUXThemeData::IsHighContrastOn() michael@0: { michael@0: return sIsHighContrastOn; michael@0: } michael@0: michael@0: // static michael@0: bool nsUXThemeData::CheckForCompositor(bool aUpdateCache) michael@0: { michael@0: static BOOL sCachedValue = FALSE; michael@0: if (aUpdateCache && WinUtils::dwmIsCompositionEnabledPtr) { michael@0: WinUtils::dwmIsCompositionEnabledPtr(&sCachedValue); michael@0: } michael@0: return sCachedValue; michael@0: } michael@0: michael@0: // static michael@0: void michael@0: nsUXThemeData::UpdateNativeThemeInfo() michael@0: { michael@0: // Trigger a refresh of themed button metrics if needed michael@0: sTitlebarInfoPopulatedThemed = !IsVistaOrLater(); michael@0: michael@0: sIsDefaultWindowsTheme = false; michael@0: sThemeId = LookAndFeel::eWindowsTheme_Generic; michael@0: michael@0: HIGHCONTRAST highContrastInfo; michael@0: highContrastInfo.cbSize = sizeof(HIGHCONTRAST); michael@0: if (SystemParametersInfo(SPI_GETHIGHCONTRAST, 0, &highContrastInfo, 0)) { michael@0: sIsHighContrastOn = ((highContrastInfo.dwFlags & HCF_HIGHCONTRASTON) != 0); michael@0: } else { michael@0: sIsHighContrastOn = false; michael@0: } michael@0: michael@0: if (!IsAppThemed()) { michael@0: sThemeId = LookAndFeel::eWindowsTheme_Classic; michael@0: return; michael@0: } michael@0: michael@0: WCHAR themeFileName[MAX_PATH + 1]; michael@0: WCHAR themeColor[MAX_PATH + 1]; michael@0: if (FAILED(GetCurrentThemeName(themeFileName, michael@0: MAX_PATH, michael@0: themeColor, michael@0: MAX_PATH, michael@0: nullptr, 0))) { michael@0: sThemeId = LookAndFeel::eWindowsTheme_Classic; michael@0: return; michael@0: } michael@0: michael@0: LPCWSTR themeName = wcsrchr(themeFileName, L'\\'); michael@0: themeName = themeName ? themeName + 1 : themeFileName; michael@0: michael@0: WindowsTheme theme = WINTHEME_UNRECOGNIZED; michael@0: for (size_t i = 0; i < ArrayLength(knownThemes); ++i) { michael@0: if (!lstrcmpiW(themeName, knownThemes[i].name)) { michael@0: theme = (WindowsTheme)knownThemes[i].type; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (theme == WINTHEME_UNRECOGNIZED) michael@0: return; michael@0: michael@0: // We're using the default theme if we're using any of Aero, Aero Lite, or michael@0: // luna. However, on Win8, GetCurrentThemeName (see above) returns michael@0: // AeroLite.msstyles for the 4 builtin highcontrast themes as well. Those michael@0: // themes "don't count" as default themes, so we specifically check for high michael@0: // contrast mode in that situation. michael@0: if (!(IsWin8OrLater() && sIsHighContrastOn) && michael@0: (theme == WINTHEME_AERO || theme == WINTHEME_AERO_LITE || theme == WINTHEME_LUNA)) { michael@0: sIsDefaultWindowsTheme = true; michael@0: } michael@0: michael@0: if (theme != WINTHEME_LUNA) { michael@0: switch(theme) { michael@0: case WINTHEME_AERO: michael@0: sThemeId = LookAndFeel::eWindowsTheme_Aero; michael@0: return; michael@0: case WINTHEME_AERO_LITE: michael@0: sThemeId = LookAndFeel::eWindowsTheme_AeroLite; michael@0: return; michael@0: case WINTHEME_ZUNE: michael@0: sThemeId = LookAndFeel::eWindowsTheme_Zune; michael@0: return; michael@0: case WINTHEME_ROYALE: michael@0: sThemeId = LookAndFeel::eWindowsTheme_Royale; michael@0: return; michael@0: default: michael@0: NS_WARNING("unhandled theme type."); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // calculate the luna color scheme michael@0: WindowsThemeColor color = WINTHEMECOLOR_UNRECOGNIZED; michael@0: for (size_t i = 0; i < ArrayLength(knownColors); ++i) { michael@0: if (!lstrcmpiW(themeColor, knownColors[i].name)) { michael@0: color = (WindowsThemeColor)knownColors[i].type; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: switch(color) { michael@0: case WINTHEMECOLOR_NORMAL: michael@0: sThemeId = LookAndFeel::eWindowsTheme_LunaBlue; michael@0: return; michael@0: case WINTHEMECOLOR_HOMESTEAD: michael@0: sThemeId = LookAndFeel::eWindowsTheme_LunaOlive; michael@0: return; michael@0: case WINTHEMECOLOR_METALLIC: michael@0: sThemeId = LookAndFeel::eWindowsTheme_LunaSilver; michael@0: return; michael@0: default: michael@0: NS_WARNING("unhandled theme color."); michael@0: return; michael@0: } michael@0: }