Source code
Revision control
Copy as Markdown
Other Tools
/* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
#include "nsNativeThemeWin.h"
#include <algorithm>
#include <malloc.h>
#include "gfxContext.h"
#include "gfxPlatform.h"
#include "gfxWindowsNativeDrawing.h"
#include "gfxWindowsPlatform.h"
#include "gfxWindowsSurface.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/gfx/Types.h" // for Color::FromABGR
#include "mozilla/Logging.h"
#include "mozilla/RelativeLuminanceUtils.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/StaticPrefs_widget.h"
#include "mozilla/WindowsVersion.h"
#include "mozilla/dom/XULButtonElement.h"
#include "nsColor.h"
#include "nsComboboxControlFrame.h"
#include "nsDeviceContext.h"
#include "nsGkAtoms.h"
#include "nsIContent.h"
#include "nsIContentInlines.h"
#include "nsIFrame.h"
#include "nsLayoutUtils.h"
#include "nsLookAndFeel.h"
#include "nsNameSpaceManager.h"
#include "Theme.h"
#include "nsPresContext.h"
#include "nsRect.h"
#include "nsSize.h"
#include "nsStyleConsts.h"
#include "nsTransform2D.h"
#include "nsWindow.h"
#include "prinrval.h"
#include "WinUtils.h"
using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::widget;
using ElementState = dom::ElementState;
extern mozilla::LazyLogModule gWindowsLog;
namespace mozilla::widget {
nsNativeThemeWin::nsNativeThemeWin()
: Theme(ScrollbarStyle()),
mProgressDeterminateTimeStamp(TimeStamp::Now()),
mProgressIndeterminateTimeStamp(TimeStamp::Now()),
mBorderCacheValid(),
mMinimumWidgetSizeCacheValid(),
mGutterSizeCacheValid(false) {
// If there is a relevant change in forms.css for windows platform,
// static widget style variables (e.g. sButtonBorderSize) should be
// reinitialized here.
}
nsNativeThemeWin::~nsNativeThemeWin() { nsUXThemeData::Invalidate(); }
auto nsNativeThemeWin::IsWidgetNonNative(nsIFrame* aFrame,
StyleAppearance aAppearance)
-> NonNative {
if (IsWidgetScrollbarPart(aAppearance) ||
aAppearance == StyleAppearance::FocusOutline) {
return NonNative::Always;
}
// We only know how to draw light widgets, so we defer to the non-native
// theme when appropriate.
if (Theme::ThemeSupportsWidget(aFrame->PresContext(), aFrame, aAppearance) &&
LookAndFeel::ColorSchemeForFrame(aFrame) ==
LookAndFeel::ColorScheme::Dark) {
return NonNative::BecauseColorMismatch;
}
return NonNative::No;
}
static int32_t GetTopLevelWindowActiveState(nsIFrame* aFrame) {
// Used by window frame and button box rendering. We can end up in here in
// the content process when rendering one of these moz styles freely in a
// page. Bail in this case, there is no applicable window focus state.
if (!XRE_IsParentProcess()) {
return mozilla::widget::themeconst::FS_INACTIVE;
}
// All headless windows are considered active so they are painted.
if (gfxPlatform::IsHeadless()) {
return mozilla::widget::themeconst::FS_ACTIVE;
}
// Get the widget. nsIFrame's GetNearestWidget walks up the view chain
// until it finds a real window.
nsIWidget* widget = aFrame->GetNearestWidget();
nsWindow* window = static_cast<nsWindow*>(widget);
if (!window) return mozilla::widget::themeconst::FS_INACTIVE;
if (widget && !window->IsTopLevelWidget() &&
!(window = window->GetParentWindowBase(false)))
return mozilla::widget::themeconst::FS_INACTIVE;
if (window->GetWindowHandle() == ::GetActiveWindow())
return mozilla::widget::themeconst::FS_ACTIVE;
return mozilla::widget::themeconst::FS_INACTIVE;
}
static int32_t GetWindowFrameButtonState(nsIFrame* aFrame,
ElementState elementState) {
if (GetTopLevelWindowActiveState(aFrame) ==
mozilla::widget::themeconst::FS_INACTIVE) {
if (elementState.HasState(ElementState::HOVER))
return mozilla::widget::themeconst::BS_HOT;
return mozilla::widget::themeconst::BS_INACTIVE;
}
if (elementState.HasState(ElementState::HOVER)) {
if (elementState.HasState(ElementState::ACTIVE))
return mozilla::widget::themeconst::BS_PUSHED;
return mozilla::widget::themeconst::BS_HOT;
}
return mozilla::widget::themeconst::BS_NORMAL;
}
static int32_t GetClassicWindowFrameButtonState(ElementState elementState) {
if (elementState.HasState(ElementState::ACTIVE) &&
elementState.HasState(ElementState::HOVER))
return DFCS_BUTTONPUSH | DFCS_PUSHED;
return DFCS_BUTTONPUSH;
}
static bool IsTopLevelMenu(nsIFrame* aFrame) {
auto* menu = dom::XULButtonElement::FromNodeOrNull(aFrame->GetContent());
return menu && menu->IsOnMenuBar();
}
static MARGINS GetCheckboxMargins(HANDLE theme, HDC hdc) {
MARGINS checkboxContent = {0};
GetThemeMargins(theme, hdc, MENU_POPUPCHECK, MCB_NORMAL, TMT_CONTENTMARGINS,
nullptr, &checkboxContent);
return checkboxContent;
}
static SIZE GetCheckboxBGSize(HANDLE theme, HDC hdc) {
SIZE checkboxSize;
GetThemePartSize(theme, hdc, MENU_POPUPCHECK, MC_CHECKMARKNORMAL, nullptr,
TS_TRUE, &checkboxSize);
MARGINS checkboxMargins = GetCheckboxMargins(theme, hdc);
int leftMargin = checkboxMargins.cxLeftWidth;
int rightMargin = checkboxMargins.cxRightWidth;
int topMargin = checkboxMargins.cyTopHeight;
int bottomMargin = checkboxMargins.cyBottomHeight;
int width = leftMargin + checkboxSize.cx + rightMargin;
int height = topMargin + checkboxSize.cy + bottomMargin;
SIZE ret;
ret.cx = width;
ret.cy = height;
return ret;
}
static SIZE GetCheckboxBGBounds(HANDLE theme, HDC hdc) {
MARGINS checkboxBGSizing = {0};
MARGINS checkboxBGContent = {0};
GetThemeMargins(theme, hdc, MENU_POPUPCHECKBACKGROUND, MCB_NORMAL,
TMT_SIZINGMARGINS, nullptr, &checkboxBGSizing);
GetThemeMargins(theme, hdc, MENU_POPUPCHECKBACKGROUND, MCB_NORMAL,
TMT_CONTENTMARGINS, nullptr, &checkboxBGContent);
#define posdx(d) ((d) > 0 ? d : 0)
int dx =
posdx(checkboxBGContent.cxRightWidth - checkboxBGSizing.cxRightWidth) +
posdx(checkboxBGContent.cxLeftWidth - checkboxBGSizing.cxLeftWidth);
int dy =
posdx(checkboxBGContent.cyTopHeight - checkboxBGSizing.cyTopHeight) +
posdx(checkboxBGContent.cyBottomHeight - checkboxBGSizing.cyBottomHeight);
#undef posdx
SIZE ret(GetCheckboxBGSize(theme, hdc));
ret.cx += dx;
ret.cy += dy;
return ret;
}
static SIZE GetGutterSize(HANDLE theme, HDC hdc) {
SIZE gutterSize;
GetThemePartSize(theme, hdc, MENU_POPUPGUTTER, 0, nullptr, TS_TRUE,
&gutterSize);
SIZE checkboxBGSize(GetCheckboxBGBounds(theme, hdc));
SIZE itemSize;
GetThemePartSize(theme, hdc, MENU_POPUPITEM, MPI_NORMAL, nullptr, TS_TRUE,
&itemSize);
// Figure out how big the menuitem's icon will be (if present) at current DPI
// Needs the system scale for consistency with Windows Theme API.
double scaleFactor = WinUtils::SystemScaleFactor();
int iconDevicePixels = NSToIntRound(16 * scaleFactor);
SIZE iconSize = {iconDevicePixels, iconDevicePixels};
// Not really sure what margins should be used here, but this seems to work in
// practice...
MARGINS margins = {0};
GetThemeMargins(theme, hdc, MENU_POPUPCHECKBACKGROUND, MCB_NORMAL,
TMT_CONTENTMARGINS, nullptr, &margins);
iconSize.cx += margins.cxLeftWidth + margins.cxRightWidth;
iconSize.cy += margins.cyTopHeight + margins.cyBottomHeight;
int width = std::max(
itemSize.cx, std::max(iconSize.cx, checkboxBGSize.cx) + gutterSize.cx);
int height = std::max(itemSize.cy, std::max(iconSize.cy, checkboxBGSize.cy));
SIZE ret;
ret.cx = width;
ret.cy = height;
return ret;
}
SIZE nsNativeThemeWin::GetCachedGutterSize(HANDLE theme) {
if (mGutterSizeCacheValid) {
return mGutterSizeCache;
}
mGutterSizeCache = GetGutterSize(theme, nullptr);
mGutterSizeCacheValid = true;
return mGutterSizeCache;
}
/* DrawThemeBGRTLAware - render a theme part based on rtl state.
* Some widgets are not direction-neutral and need to be drawn reversed for
* RTL. Windows provides a way to do this with SetLayout, but this reverses
* the entire drawing area of a given device context, which means that its
* use will also affect the positioning of the widget. There are two ways
* to work around this:
*
* Option 1: Alter the position of the rect that we send so that we cancel
* out the positioning effects of SetLayout
* Option 2: Create a memory DC with the widgetRect's dimensions, draw onto
* that, and then transfer the results back to our DC
*
* This function tries to implement option 1, under the assumption that the
* correct way to reverse the effects of SetLayout is to translate the rect
* such that the offset from the DC bitmap's left edge to the old rect's
* left edge is equal to the offset from the DC bitmap's right edge to the
* new rect's right edge. In other words,
* (oldRect.left + vpOrg.x) == ((dcBMP.width - vpOrg.x) - newRect.right)
*/
static HRESULT DrawThemeBGRTLAware(HANDLE aTheme, HDC aHdc, int aPart,
int aState, const RECT* aWidgetRect,
const RECT* aClipRect, bool aIsRtl) {
NS_ASSERTION(aTheme, "Bad theme handle.");
NS_ASSERTION(aHdc, "Bad hdc.");
NS_ASSERTION(aWidgetRect, "Bad rect.");
NS_ASSERTION(aClipRect, "Bad clip rect.");
if (!aIsRtl) {
return DrawThemeBackground(aTheme, aHdc, aPart, aState, aWidgetRect,
aClipRect);
}
HGDIOBJ hObj = GetCurrentObject(aHdc, OBJ_BITMAP);
BITMAP bitmap;
POINT vpOrg;
if (hObj && GetObject(hObj, sizeof(bitmap), &bitmap) &&
GetViewportOrgEx(aHdc, &vpOrg)) {
RECT newWRect(*aWidgetRect);
newWRect.left = bitmap.bmWidth - (aWidgetRect->right + 2 * vpOrg.x);
newWRect.right = bitmap.bmWidth - (aWidgetRect->left + 2 * vpOrg.x);
RECT newCRect;
RECT* newCRectPtr = nullptr;
if (aClipRect) {
newCRect.top = aClipRect->top;
newCRect.bottom = aClipRect->bottom;
newCRect.left = bitmap.bmWidth - (aClipRect->right + 2 * vpOrg.x);
newCRect.right = bitmap.bmWidth - (aClipRect->left + 2 * vpOrg.x);
newCRectPtr = &newCRect;
}
SetLayout(aHdc, LAYOUT_RTL);
HRESULT hr = DrawThemeBackground(aTheme, aHdc, aPart, aState, &newWRect,
newCRectPtr);
SetLayout(aHdc, 0);
if (SUCCEEDED(hr)) {
return hr;
}
}
return DrawThemeBackground(aTheme, aHdc, aPart, aState, aWidgetRect,
aClipRect);
}
/*
* Caption button padding data - 'hot' button padding.
* These areas are considered hot, in that they activate
* a button when hovered or clicked. The button graphic
* is drawn inside the padding border. Unrecognized themes
* are treated as their recognized counterparts for now.
* left top right bottom
* classic min 1 2 0 1
* classic max 0 2 1 1
* classic close 1 2 2 1
*
* aero basic min 1 2 0 2
* aero basic max 0 2 1 2
* aero basic close 1 2 1 2
*
* 'cold' button padding - generic button padding, should
* be handled in css.
* left top right bottom
* classic min 0 0 0 0
* classic max 0 0 0 0
* classic close 0 0 0 0
*
* aero basic min 0 0 1 0
* aero basic max 1 0 0 0
* aero basic close 0 0 0 0
*/
enum CaptionDesktopTheme {
CAPTION_CLASSIC = 0,
CAPTION_BASIC,
};
enum CaptionButton {
CAPTIONBUTTON_MINIMIZE = 0,
CAPTIONBUTTON_RESTORE,
CAPTIONBUTTON_CLOSE,
};
struct CaptionButtonPadding {
RECT hotPadding[3];
};
// RECT: left, top, right, bottom
static CaptionButtonPadding buttonData[3] = {
{{{1, 2, 0, 1}, {0, 2, 1, 1}, {1, 2, 2, 1}}},
{{{1, 2, 0, 2}, {0, 2, 1, 2}, {1, 2, 2, 2}}},
{{{0, 2, 0, 2}, {0, 2, 1, 2}, {1, 2, 2, 2}}}};
// Adds "hot" caption button padding to minimum widget size.
static void AddPaddingRect(LayoutDeviceIntSize* aSize, CaptionButton button) {
if (!aSize) return;
RECT offset;
if (!nsUXThemeData::IsAppThemed())
offset = buttonData[CAPTION_CLASSIC].hotPadding[button];
else
offset = buttonData[CAPTION_BASIC].hotPadding[button];
aSize->width += offset.left + offset.right;
aSize->height += offset.top + offset.bottom;
}
// If we've added padding to the minimum widget size, offset
// the area we draw into to compensate.
static void OffsetBackgroundRect(RECT& rect, CaptionButton button) {
RECT offset;
if (!nsUXThemeData::IsAppThemed())
offset = buttonData[CAPTION_CLASSIC].hotPadding[button];
else
offset = buttonData[CAPTION_BASIC].hotPadding[button];
rect.left += offset.left;
rect.top += offset.top;
rect.right -= offset.right;
rect.bottom -= offset.bottom;
}
/*
* Notes on progress track and meter part constants:
* xp and up:
* PP_BAR(_VERT) - base progress track
* PP_TRANSPARENTBAR(_VERT) - transparent progress track. this only works if
* the underlying surface supports alpha. otherwise
* theme lib's DrawThemeBackground falls back on
* opaque PP_BAR. we currently don't use this.
* PP_CHUNK(_VERT) - xp progress meter. this does not draw an xp style
* progress w/chunks, it draws fill using the chunk
* graphic.
* vista and up:
* PP_FILL(_VERT) - progress meter. these have four states/colors.
* PP_PULSEOVERLAY(_VERT) - white reflection - an overlay, not sure what this
* is used for.
* PP_MOVEOVERLAY(_VERT) - green pulse - the pulse effect overlay on
* determined progress bars. we also use this for
* indeterminate chunk.
*
* Notes on state constants:
* PBBS_NORMAL - green progress
* PBBVS_PARTIAL/PBFVS_ERROR - red error progress
* PBFS_PAUSED - yellow paused progress
*
* There is no common controls style indeterminate part on vista and up.
*/
/*
* Progress bar related constants. These values are found by experimenting and
* comparing against native widgets used by the system. They are very unlikely
* exact but try to not be too wrong.
*/
// The amount of time we animate progress meters parts across the frame.
static const double kProgressDeterminateTimeSpan = 3.0;
static const double kProgressIndeterminateTimeSpan = 5.0;
// The width of the overlay used to animate the horizontal progress bar (Vista
// and later).
static const int32_t kProgressHorizontalOverlaySize = 120;
// The height of the overlay used to animate the vertical progress bar (Vista
// and later).
static const int32_t kProgressVerticalOverlaySize = 45;
// The height of the overlay used for the vertical indeterminate progress bar
// (Vista and later).
static const int32_t kProgressVerticalIndeterminateOverlaySize = 60;
// The width of the overlay used to animate the indeterminate progress bar
// (Windows Classic).
static const int32_t kProgressClassicOverlaySize = 40;
/*
* GetProgressOverlayStyle - returns the proper overlay part for themed
* progress bars based on os and orientation.
*/
static int32_t GetProgressOverlayStyle(bool aIsVertical) {
return aIsVertical ? PP_MOVEOVERLAYVERT : PP_MOVEOVERLAY;
}
/*
* GetProgressOverlaySize - returns the minimum width or height for themed
* progress bar overlays. This includes the width of indeterminate chunks
* and vista pulse overlays.
*/
static int32_t GetProgressOverlaySize(bool aIsVertical, bool aIsIndeterminate) {
if (aIsVertical) {
return aIsIndeterminate ? kProgressVerticalIndeterminateOverlaySize
: kProgressVerticalOverlaySize;
}
return kProgressHorizontalOverlaySize;
}
/*
* IsProgressMeterFilled - Determines if a progress meter is at 100% fill based
* on a comparison of the current value and maximum.
*/
static bool IsProgressMeterFilled(nsIFrame* aFrame) {
NS_ENSURE_TRUE(aFrame, false);
nsIFrame* parentFrame = aFrame->GetParent();
NS_ENSURE_TRUE(parentFrame, false);
return nsNativeTheme::GetProgressValue(parentFrame) ==
nsNativeTheme::GetProgressMaxValue(parentFrame);
}
/*
* CalculateProgressOverlayRect - returns the padded overlay animation rect
* used in rendering progress bars. Resulting rects are used in rendering
* vista+ pulse overlays and indeterminate progress meters. Graphics should
* be rendered at the origin.
*/
RECT nsNativeThemeWin::CalculateProgressOverlayRect(nsIFrame* aFrame,
RECT* aWidgetRect,
bool aIsVertical,
bool aIsIndeterminate,
bool aIsClassic) {
NS_ASSERTION(aFrame, "bad frame pointer");
NS_ASSERTION(aWidgetRect, "bad rect pointer");
int32_t frameSize = aIsVertical ? aWidgetRect->bottom - aWidgetRect->top
: aWidgetRect->right - aWidgetRect->left;
// Recycle a set of progress pulse timers - these timers control the position
// of all progress overlays and indeterminate chunks that get rendered.
double span = aIsIndeterminate ? kProgressIndeterminateTimeSpan
: kProgressDeterminateTimeSpan;
TimeDuration period;
if (!aIsIndeterminate) {
if (TimeStamp::Now() >
(mProgressDeterminateTimeStamp + TimeDuration::FromSeconds(span))) {
mProgressDeterminateTimeStamp = TimeStamp::Now();
}
period = TimeStamp::Now() - mProgressDeterminateTimeStamp;
} else {
if (TimeStamp::Now() >
(mProgressIndeterminateTimeStamp + TimeDuration::FromSeconds(span))) {
mProgressIndeterminateTimeStamp = TimeStamp::Now();
}
period = TimeStamp::Now() - mProgressIndeterminateTimeStamp;
}
double percent = period / TimeDuration::FromSeconds(span);
if (!aIsVertical && IsFrameRTL(aFrame)) percent = 1 - percent;
RECT overlayRect = *aWidgetRect;
int32_t overlaySize;
if (!aIsClassic) {
overlaySize = GetProgressOverlaySize(aIsVertical, aIsIndeterminate);
} else {
overlaySize = kProgressClassicOverlaySize;
}
// Calculate a bounds that is larger than the meters frame such that the
// overlay starts and ends completely off the edge of the frame:
// [overlay][frame][overlay]
// This also yields a nice delay on rotation. Use overlaySize as the minimum
// size for [overlay] based on the graphics dims. If [frame] is larger, use
// the frame size instead.
int trackWidth = frameSize > overlaySize ? frameSize : overlaySize;
if (!aIsVertical) {
int xPos = aWidgetRect->left - trackWidth;
xPos += (int)ceil(((double)(trackWidth * 2) * percent));
overlayRect.left = xPos;
overlayRect.right = xPos + overlaySize;
} else {
int yPos = aWidgetRect->bottom + trackWidth;
yPos -= (int)ceil(((double)(trackWidth * 2) * percent));
overlayRect.bottom = yPos;
overlayRect.top = yPos - overlaySize;
}
return overlayRect;
}
/*
* DrawProgressMeter - render an appropriate progress meter based on progress
* meter style, orientation, and os. Note, this does not render the underlying
* progress track.
*
* @param aFrame the widget frame
* @param aAppearance type of widget
* @param aTheme progress theme handle
* @param aHdc hdc returned by gfxWindowsNativeDrawing
* @param aPart the PP_X progress part
* @param aState the theme state
* @param aWidgetRect bounding rect for the widget
* @param aClipRect dirty rect that needs drawing.
* @param aAppUnits app units per device pixel
*/
void nsNativeThemeWin::DrawThemedProgressMeter(
nsIFrame* aFrame, StyleAppearance aAppearance, HANDLE aTheme, HDC aHdc,
int aPart, int aState, RECT* aWidgetRect, RECT* aClipRect) {
if (!aFrame || !aTheme || !aHdc) return;
NS_ASSERTION(aWidgetRect, "bad rect pointer");
NS_ASSERTION(aClipRect, "bad clip rect pointer");
RECT adjWidgetRect, adjClipRect;
adjWidgetRect = *aWidgetRect;
adjClipRect = *aClipRect;
nsIFrame* parentFrame = aFrame->GetParent();
if (!parentFrame) {
// We have no parent to work with, just bail.
NS_WARNING("No parent frame for progress rendering. Can't paint.");
return;
}
ElementState elementState = GetContentState(parentFrame, aAppearance);
bool vertical = IsVerticalProgress(parentFrame);
bool indeterminate = elementState.HasState(ElementState::INDETERMINATE);
bool animate = indeterminate;
// Vista and up progress meter is fill style, rendered here. We render
// the pulse overlay in the follow up section below.
DrawThemeBackground(aTheme, aHdc, aPart, aState, &adjWidgetRect,
&adjClipRect);
if (!IsProgressMeterFilled(aFrame)) {
animate = true;
}
if (animate) {
// Indeterminate rendering
int32_t overlayPart = GetProgressOverlayStyle(vertical);
RECT overlayRect = CalculateProgressOverlayRect(
aFrame, &adjWidgetRect, vertical, indeterminate, false);
DrawThemeBackground(aTheme, aHdc, overlayPart, aState, &overlayRect,
&adjClipRect);
if (!QueueAnimatedContentForRefresh(aFrame->GetContent(), 60)) {
NS_WARNING("unable to animate progress widget!");
}
}
}
LayoutDeviceIntMargin nsNativeThemeWin::GetCachedWidgetBorder(
HTHEME aTheme, nsUXThemeClass aThemeClass, StyleAppearance aAppearance,
int32_t aPart, int32_t aState) {
int32_t cacheIndex = aThemeClass * THEME_PART_DISTINCT_VALUE_COUNT + aPart;
int32_t cacheBitIndex = cacheIndex / 8;
uint8_t cacheBit = 1u << (cacheIndex % 8);
if (mBorderCacheValid[cacheBitIndex] & cacheBit) {
return mBorderCache[cacheIndex];
}
// Get our info.
RECT outerRect; // Create a fake outer rect.
outerRect.top = outerRect.left = 100;
outerRect.right = outerRect.bottom = 200;
RECT contentRect(outerRect);
HRESULT res = GetThemeBackgroundContentRect(aTheme, nullptr, aPart, aState,
&outerRect, &contentRect);
if (FAILED(res)) {
return LayoutDeviceIntMargin();
}
// Now compute the delta in each direction and place it in our
// nsIntMargin struct.
LayoutDeviceIntMargin result;
result.top = contentRect.top - outerRect.top;
result.bottom = outerRect.bottom - contentRect.bottom;
result.left = contentRect.left - outerRect.left;
result.right = outerRect.right - contentRect.right;
mBorderCacheValid[cacheBitIndex] |= cacheBit;
mBorderCache[cacheIndex] = result;
return result;
}
nsresult nsNativeThemeWin::GetCachedMinimumWidgetSize(
nsIFrame* aFrame, HANDLE aTheme, nsUXThemeClass aThemeClass,
StyleAppearance aAppearance, int32_t aPart, int32_t aState,
THEMESIZE aSizeReq, mozilla::LayoutDeviceIntSize* aResult) {
int32_t cachePart = aPart;
if (aAppearance == StyleAppearance::Button && aSizeReq == TS_MIN) {
// In practice, StyleAppearance::Button is the only widget type which has an
// aSizeReq that varies for us, and it can only be TS_MIN or TS_TRUE. Just
// stuff that extra bit into the aPart part of the cache, since BP_Count is
// well below THEME_PART_DISTINCT_VALUE_COUNT anyway.
cachePart = BP_Count;
}
MOZ_ASSERT(aPart < THEME_PART_DISTINCT_VALUE_COUNT);
int32_t cacheIndex =
aThemeClass * THEME_PART_DISTINCT_VALUE_COUNT + cachePart;
int32_t cacheBitIndex = cacheIndex / 8;
uint8_t cacheBit = 1u << (cacheIndex % 8);
if (mMinimumWidgetSizeCacheValid[cacheBitIndex] & cacheBit) {
*aResult = mMinimumWidgetSizeCache[cacheIndex];
return NS_OK;
}
HDC hdc = ::GetDC(NULL);
if (!hdc) {
return NS_ERROR_FAILURE;
}
SIZE sz;
GetThemePartSize(aTheme, hdc, aPart, aState, nullptr, aSizeReq, &sz);
aResult->width = sz.cx;
aResult->height = sz.cy;
switch (aAppearance) {
case StyleAppearance::SpinnerUpbutton:
case StyleAppearance::SpinnerDownbutton:
aResult->width++;
aResult->height = aResult->height / 2 + 1;
break;
case StyleAppearance::Menuseparator: {
SIZE gutterSize(GetGutterSize(aTheme, hdc));
aResult->width += gutterSize.cx;
break;
}
case StyleAppearance::Menuarrow:
// Use the width of the arrow glyph as padding. See the drawing
// code for details.
aResult->width *= 2;
break;
default:
break;
}
::ReleaseDC(nullptr, hdc);
mMinimumWidgetSizeCacheValid[cacheBitIndex] |= cacheBit;
mMinimumWidgetSizeCache[cacheIndex] = *aResult;
return NS_OK;
}
mozilla::Maybe<nsUXThemeClass> nsNativeThemeWin::GetThemeClass(
StyleAppearance aAppearance) {
switch (aAppearance) {
case StyleAppearance::Button:
case StyleAppearance::Radio:
case StyleAppearance::Checkbox:
case StyleAppearance::Groupbox:
return Some(eUXButton);
case StyleAppearance::NumberInput:
case StyleAppearance::Textfield:
case StyleAppearance::Textarea:
return Some(eUXEdit);
case StyleAppearance::Toolbox:
return Some(eUXRebar);
case StyleAppearance::MozWinMediaToolbox:
return Some(eUXMediaRebar);
case StyleAppearance::MozWinCommunicationsToolbox:
return Some(eUXCommunicationsRebar);
case StyleAppearance::MozWinBrowsertabbarToolbox:
return Some(eUXBrowserTabBarRebar);
case StyleAppearance::Toolbar:
case StyleAppearance::Toolbarbutton:
case StyleAppearance::Separator:
return Some(eUXToolbar);
case StyleAppearance::ProgressBar:
case StyleAppearance::Progresschunk:
return Some(eUXProgress);
case StyleAppearance::Tab:
case StyleAppearance::Tabpanel:
case StyleAppearance::Tabpanels:
return Some(eUXTab);
case StyleAppearance::Range:
case StyleAppearance::RangeThumb:
return Some(eUXTrackbar);
case StyleAppearance::SpinnerUpbutton:
case StyleAppearance::SpinnerDownbutton:
return Some(eUXSpin);
case StyleAppearance::Menulist:
case StyleAppearance::MenulistButton:
case StyleAppearance::MozMenulistArrowButton:
return Some(eUXCombobox);
case StyleAppearance::Treeheadercell:
case StyleAppearance::Treeheadersortarrow:
return Some(eUXHeader);
case StyleAppearance::Listbox:
case StyleAppearance::Treeview:
case StyleAppearance::Treetwistyopen:
case StyleAppearance::Treeitem:
return Some(eUXListview);
case StyleAppearance::Menubar:
case StyleAppearance::Menupopup:
case StyleAppearance::Menuitem:
case StyleAppearance::Checkmenuitem:
case StyleAppearance::Radiomenuitem:
case StyleAppearance::Menucheckbox:
case StyleAppearance::Menuradio:
case StyleAppearance::Menuseparator:
case StyleAppearance::Menuarrow:
case StyleAppearance::Menuimage:
case StyleAppearance::Menuitemtext:
return Some(eUXMenu);
case StyleAppearance::MozWindowTitlebar:
case StyleAppearance::MozWindowTitlebarMaximized:
case StyleAppearance::MozWindowButtonClose:
case StyleAppearance::MozWindowButtonMinimize:
case StyleAppearance::MozWindowButtonMaximize:
case StyleAppearance::MozWindowButtonRestore:
case StyleAppearance::MozWindowButtonBox:
case StyleAppearance::MozWindowButtonBoxMaximized:
case StyleAppearance::MozWinBorderlessGlass:
return Some(eUXWindowFrame);
default:
return Nothing();
}
}
HANDLE
nsNativeThemeWin::GetTheme(StyleAppearance aAppearance) {
mozilla::Maybe<nsUXThemeClass> themeClass = GetThemeClass(aAppearance);
if (themeClass.isNothing()) {
return nullptr;
}
return nsUXThemeData::GetTheme(themeClass.value());
}
int32_t nsNativeThemeWin::StandardGetState(nsIFrame* aFrame,
StyleAppearance aAppearance,
bool wantFocused) {
ElementState elementState = GetContentState(aFrame, aAppearance);
if (elementState.HasAllStates(ElementState::HOVER | ElementState::ACTIVE)) {
return TS_ACTIVE;
}
if (elementState.HasState(ElementState::HOVER)) {
return TS_HOVER;
}
if (wantFocused) {
if (elementState.HasState(ElementState::FOCUSRING)) {
return TS_FOCUSED;
}
// On Windows, focused buttons are always drawn as such by the native
// theme, that's why we check ElementState::FOCUS instead of
// ElementState::FOCUSRING.
if (aAppearance == StyleAppearance::Button &&
elementState.HasState(ElementState::FOCUS)) {
return TS_FOCUSED;
}
}
return TS_NORMAL;
}
bool nsNativeThemeWin::IsMenuActive(nsIFrame* aFrame,
StyleAppearance aAppearance) {
nsIContent* content = aFrame->GetContent();
if (content->IsXULElement() &&
content->NodeInfo()->Equals(nsGkAtoms::richlistitem))
return CheckBooleanAttr(aFrame, nsGkAtoms::selected);
return CheckBooleanAttr(aFrame, nsGkAtoms::menuactive);
}
/**
* aPart is filled in with the UXTheme part code. On return, values > 0
* are the actual UXTheme part code; -1 means the widget will be drawn by
* us; 0 means that we should use part code 0, which isn't a real part code
* but elicits some kind of default behaviour from UXTheme when drawing
* (but isThemeBackgroundPartiallyTransparent may not work).
*/
nsresult nsNativeThemeWin::GetThemePartAndState(nsIFrame* aFrame,
StyleAppearance aAppearance,
int32_t& aPart,
int32_t& aState) {
switch (aAppearance) {
case StyleAppearance::Button: {
aPart = BP_BUTTON;
if (!aFrame) {
aState = TS_NORMAL;
return NS_OK;
}
ElementState elementState = GetContentState(aFrame, aAppearance);
if (elementState.HasState(ElementState::DISABLED)) {
aState = TS_DISABLED;
return NS_OK;
}
if (IsOpenButton(aFrame) || IsCheckedButton(aFrame)) {
aState = TS_ACTIVE;
return NS_OK;
}
aState = StandardGetState(aFrame, aAppearance, true);
// Check for default dialog buttons. These buttons should always look
// focused.
if (aState == TS_NORMAL && IsDefaultButton(aFrame)) aState = TS_FOCUSED;
return NS_OK;
}
case StyleAppearance::Checkbox:
case StyleAppearance::Radio: {
bool isCheckbox = (aAppearance == StyleAppearance::Checkbox);
aPart = isCheckbox ? BP_CHECKBOX : BP_RADIO;
enum InputState { UNCHECKED = 0, CHECKED, INDETERMINATE };
InputState inputState = UNCHECKED;
if (!aFrame) {
aState = TS_NORMAL;
} else {
ElementState elementState = GetContentState(aFrame, aAppearance);
if (elementState.HasState(ElementState::CHECKED)) {
inputState = CHECKED;
}
if (isCheckbox && elementState.HasState(ElementState::INDETERMINATE)) {
inputState = INDETERMINATE;
}
if (elementState.HasState(ElementState::DISABLED)) {
aState = TS_DISABLED;
} else {
aState = StandardGetState(aFrame, aAppearance, false);
}
}
// 4 unchecked states, 4 checked states, 4 indeterminate states.
aState += inputState * 4;
return NS_OK;
}
case StyleAppearance::Groupbox: {
aPart = BP_GROUPBOX;
aState = TS_NORMAL;
// Since we don't support groupbox disabled and GBS_DISABLED looks the
// same as GBS_NORMAL don't bother supporting GBS_DISABLED.
return NS_OK;
}
case StyleAppearance::NumberInput:
case StyleAppearance::Textfield:
case StyleAppearance::Textarea: {
ElementState elementState = GetContentState(aFrame, aAppearance);
/* Note: the NOSCROLL type has a rounded corner in each corner. The more
* specific HSCROLL, VSCROLL, HVSCROLL types have side and/or top/bottom
* edges rendered as straight horizontal lines with sharp corners to
* accommodate a scrollbar. However, the scrollbar gets rendered on top
* of this for us, so we don't care, and can just use NOSCROLL here.
*/
aPart = TFP_EDITBORDER_NOSCROLL;
if (!aFrame) {
aState = TFS_EDITBORDER_NORMAL;
} else if (elementState.HasState(ElementState::DISABLED)) {
aState = TFS_EDITBORDER_DISABLED;
} else if (IsReadOnly(aFrame)) {
/* no special read-only state */
aState = TFS_EDITBORDER_NORMAL;
} else if (elementState.HasAtLeastOneOfStates(ElementState::ACTIVE |
ElementState::FOCUSRING)) {
aState = TFS_EDITBORDER_FOCUSED;
} else if (elementState.HasState(ElementState::HOVER)) {
aState = TFS_EDITBORDER_HOVER;
} else {
aState = TFS_EDITBORDER_NORMAL;
}
return NS_OK;
}
case StyleAppearance::ProgressBar: {
bool vertical = IsVerticalProgress(aFrame);
aPart = vertical ? PP_BARVERT : PP_BAR;
aState = PBBS_NORMAL;
return NS_OK;
}
case StyleAppearance::Progresschunk: {
nsIFrame* parentFrame = aFrame->GetParent();
if (IsVerticalProgress(parentFrame)) {
aPart = PP_FILLVERT;
} else {
aPart = PP_FILL;
}
aState = PBBVS_NORMAL;
return NS_OK;
}
case StyleAppearance::Toolbarbutton: {
aPart = BP_BUTTON;
if (!aFrame) {
aState = TS_NORMAL;
return NS_OK;
}
ElementState elementState = GetContentState(aFrame, aAppearance);
if (elementState.HasState(ElementState::DISABLED)) {
aState = TS_DISABLED;
return NS_OK;
}
if (IsOpenButton(aFrame)) {
aState = TS_ACTIVE;
return NS_OK;
}
if (elementState.HasAllStates(ElementState::HOVER | ElementState::ACTIVE))
aState = TS_ACTIVE;
else if (elementState.HasState(ElementState::HOVER)) {
if (IsCheckedButton(aFrame))
aState = TB_HOVER_CHECKED;
else
aState = TS_HOVER;
} else {
if (IsCheckedButton(aFrame))
aState = TB_CHECKED;
else
aState = TS_NORMAL;
}
return NS_OK;
}
case StyleAppearance::Separator: {
aPart = TP_SEPARATOR;
aState = TS_NORMAL;
return NS_OK;
}
case StyleAppearance::Range: {
if (IsRangeHorizontal(aFrame)) {
aPart = TKP_TRACK;
aState = TRS_NORMAL;
} else {
aPart = TKP_TRACKVERT;
aState = TRVS_NORMAL;
}
return NS_OK;
}
case StyleAppearance::RangeThumb: {
if (IsRangeHorizontal(aFrame)) {
aPart = TKP_THUMBBOTTOM;
} else {
aPart = IsFrameRTL(aFrame) ? TKP_THUMBLEFT : TKP_THUMBRIGHT;
}
ElementState elementState = GetContentState(aFrame, aAppearance);
if (!aFrame) {
aState = TS_NORMAL;
} else if (elementState.HasState(ElementState::DISABLED)) {
aState = TKP_DISABLED;
} else {
if (elementState.HasState(
ElementState::ACTIVE)) // Hover is not also a requirement for
// the thumb, since the drag is not
// canceled when you move outside the
// thumb.
aState = TS_ACTIVE;
else if (elementState.HasState(ElementState::FOCUSRING))
aState = TKP_FOCUSED;
else if (elementState.HasState(ElementState::HOVER))
aState = TS_HOVER;
else
aState = TS_NORMAL;
}
return NS_OK;
}
case StyleAppearance::SpinnerUpbutton:
case StyleAppearance::SpinnerDownbutton: {
aPart = (aAppearance == StyleAppearance::SpinnerUpbutton) ? SPNP_UP
: SPNP_DOWN;
ElementState elementState = GetContentState(aFrame, aAppearance);
if (!aFrame) {
aState = TS_NORMAL;
} else if (elementState.HasState(ElementState::DISABLED)) {
aState = TS_DISABLED;
} else {
aState = StandardGetState(aFrame, aAppearance, false);
}
return NS_OK;
}
case StyleAppearance::Toolbox:
case StyleAppearance::MozWinMediaToolbox:
case StyleAppearance::MozWinCommunicationsToolbox:
case StyleAppearance::MozWinBrowsertabbarToolbox: {
aState = 0;
aPart = RP_BACKGROUND;
return NS_OK;
}
case StyleAppearance::Toolbar: {
// Use -1 to indicate we don't wish to have the theme background drawn
// for this item. We will pass any nessessary information via aState,
// and will render the item using separate code.
aPart = -1;
aState = 0;
if (aFrame) {
nsIContent* content = aFrame->GetContent();
nsIContent* parent = content->GetParent();
// XXXzeniko hiding the first toolbar will result in an unwanted margin
if (parent && parent->GetFirstChild() == content) {
aState = 1;
}
}
return NS_OK;
}
case StyleAppearance::Treeview:
case StyleAppearance::Listbox: {
aPart = TREEVIEW_BODY;
aState = TS_NORMAL;
return NS_OK;
}
case StyleAppearance::Tabpanels: {
aPart = TABP_PANELS;
aState = TS_NORMAL;
return NS_OK;
}
case StyleAppearance::Tabpanel: {
aPart = TABP_PANEL;
aState = TS_NORMAL;
return NS_OK;
}
case StyleAppearance::Tab: {
aPart = TABP_TAB;
if (!aFrame) {
aState = TS_NORMAL;
return NS_OK;
}
ElementState elementState = GetContentState(aFrame, aAppearance);
if (elementState.HasState(ElementState::DISABLED)) {
aState = TS_DISABLED;
return NS_OK;
}
if (IsSelectedTab(aFrame)) {
aPart = TABP_TAB_SELECTED;
aState = TS_ACTIVE; // The selected tab is always "pressed".
} else
aState = StandardGetState(aFrame, aAppearance, true);
return NS_OK;
}
case StyleAppearance::Treeheadersortarrow: {
// XXX Probably will never work due to a bug in the Luna theme.
aPart = 4;
aState = 1;
return NS_OK;
}
case StyleAppearance::Treeheadercell: {
aPart = 1;
if (!aFrame) {
aState = TS_NORMAL;
return NS_OK;
}
aState = StandardGetState(aFrame, aAppearance, true);
return NS_OK;
}
case StyleAppearance::MenulistButton:
case StyleAppearance::Menulist: {
nsIContent* content = aFrame->GetContent();
bool useDropBorder = content && content->IsHTMLElement();
ElementState elementState = GetContentState(aFrame, aAppearance);
/* On Vista/Win7, we use CBP_DROPBORDER instead of DROPFRAME for HTML
* content or for editable menulists; this gives us the thin outline,
* instead of the gradient-filled background */
if (useDropBorder)
aPart = CBP_DROPBORDER;
else
aPart = CBP_DROPFRAME;
if (elementState.HasState(ElementState::DISABLED)) {
aState = TS_DISABLED;
} else if (IsReadOnly(aFrame)) {
aState = TS_NORMAL;
} else if (IsOpenButton(aFrame)) {
aState = TS_ACTIVE;
} else if (useDropBorder &&
elementState.HasState(ElementState::FOCUSRING)) {
aState = TS_ACTIVE;
} else if (elementState.HasAllStates(ElementState::HOVER |
ElementState::ACTIVE)) {
aState = TS_ACTIVE;
} else if (elementState.HasState(ElementState::HOVER)) {
aState = TS_HOVER;
} else {
aState = TS_NORMAL;
}
return NS_OK;
}
case StyleAppearance::MozMenulistArrowButton: {
bool isOpen = false;
// HTML select and XUL menulist dropdown buttons get state from the
// parent.
nsIFrame* parentFrame = aFrame->GetParent();
aFrame = parentFrame;
ElementState elementState = GetContentState(aFrame, aAppearance);
aPart = CBP_DROPMARKER_VISTA;
// For HTML controls with author styling, we should fall
// back to the old dropmarker style to avoid clashes with
// author-specified backgrounds and borders (bug #441034)
if (IsWidgetStyled(aFrame->PresContext(), aFrame,
StyleAppearance::Menulist)) {
aPart = CBP_DROPMARKER;
}
if (elementState.HasState(ElementState::DISABLED)) {
aState = TS_DISABLED;
return NS_OK;
}
if (nsComboboxControlFrame* ccf = do_QueryFrame(aFrame)) {
isOpen = ccf->IsDroppedDown();
if (isOpen) {
/* Hover is propagated, but we need to know whether we're hovering
* just the combobox frame, not the dropdown frame. But, we can't get
* that information, since hover is on the content node, and they
* share the same content node. So, instead, we cheat -- if the
* dropdown is open, we always show the hover state. This looks fine
* in practice.
*/
aState = TS_HOVER;
return NS_OK;
}
} else {
/* The dropdown indicator on a menulist button in chrome is not given a
* hover effect. When the frame isn't isn't HTML content, we cheat and
*/
isOpen = IsOpenButton(aFrame);
aState = TS_NORMAL;
return NS_OK;
}
aState = TS_NORMAL;
// Dropdown button active state doesn't need :hover.
if (elementState.HasState(ElementState::ACTIVE)) {
if (isOpen) {
// XXX Button should look active until the mouse is released, but
// without making it look active when the popup is clicked.
return NS_OK;
}
aState = TS_ACTIVE;
} else if (elementState.HasState(ElementState::HOVER)) {
// No hover effect for XUL menulists and autocomplete dropdown buttons
// while the dropdown menu is open.
if (isOpen) {
// XXX HTML select dropdown buttons should have the hover effect when
// hovering the combobox frame, but not the popup frame.
return NS_OK;
}
aState = TS_HOVER;
}
return NS_OK;
}
case StyleAppearance::Menupopup: {
aPart = MENU_POPUPBACKGROUND;
aState = MB_ACTIVE;
return NS_OK;
}
case StyleAppearance::Menuitem:
case StyleAppearance::Checkmenuitem:
case StyleAppearance::Radiomenuitem: {
ElementState elementState = GetContentState(aFrame, aAppearance);
auto* menu = dom::XULButtonElement::FromNodeOrNull(aFrame->GetContent());
const bool isTopLevel = IsTopLevelMenu(aFrame);
const bool isOpen = menu && menu->IsMenuPopupOpen();
const bool isHover = IsMenuActive(aFrame, aAppearance);
if (isTopLevel) {
aPart = MENU_BARITEM;
if (isOpen)
aState = MBI_PUSHED;
else if (isHover)
aState = MBI_HOT;
else
aState = MBI_NORMAL;
// the disabled states are offset by 3
if (elementState.HasState(ElementState::DISABLED)) {
aState += 3;
}
} else {
aPart = MENU_POPUPITEM;
if (isHover)
aState = MPI_HOT;
else
aState = MPI_NORMAL;
// the disabled states are offset by 2
if (elementState.HasState(ElementState::DISABLED)) {
aState += 2;
}
}
return NS_OK;
}
case StyleAppearance::Menuseparator:
aPart = MENU_POPUPSEPARATOR;
aState = 0;
return NS_OK;
case StyleAppearance::Menuarrow: {
aPart = MENU_POPUPSUBMENU;
ElementState elementState = GetContentState(aFrame, aAppearance);
aState = elementState.HasState(ElementState::DISABLED) ? MSM_DISABLED
: MSM_NORMAL;
return NS_OK;
}
case StyleAppearance::Menucheckbox:
case StyleAppearance::Menuradio: {
ElementState elementState = GetContentState(aFrame, aAppearance);
aPart = MENU_POPUPCHECK;
aState = MC_CHECKMARKNORMAL;
// Radio states are offset by 2
if (aAppearance == StyleAppearance::Menuradio) aState += 2;
// the disabled states are offset by 1
if (elementState.HasState(ElementState::DISABLED)) {
aState += 1;
}
return NS_OK;
}
case StyleAppearance::Menuitemtext:
case StyleAppearance::Menuimage:
aPart = -1;
aState = 0;
return NS_OK;
case StyleAppearance::MozWindowTitlebar:
aPart = mozilla::widget::themeconst::WP_CAPTION;
aState = GetTopLevelWindowActiveState(aFrame);
return NS_OK;
case StyleAppearance::MozWindowTitlebarMaximized:
aPart = mozilla::widget::themeconst::WP_MAXCAPTION;
aState = GetTopLevelWindowActiveState(aFrame);
return NS_OK;
case StyleAppearance::MozWindowButtonClose:
aPart = mozilla::widget::themeconst::WP_CLOSEBUTTON;
aState = GetWindowFrameButtonState(aFrame,
GetContentState(aFrame, aAppearance));
return NS_OK;
case StyleAppearance::MozWindowButtonMinimize:
aPart = mozilla::widget::themeconst::WP_MINBUTTON;
aState = GetWindowFrameButtonState(aFrame,
GetContentState(aFrame, aAppearance));
return NS_OK;
case StyleAppearance::MozWindowButtonMaximize:
aPart = mozilla::widget::themeconst::WP_MAXBUTTON;
aState = GetWindowFrameButtonState(aFrame,
GetContentState(aFrame, aAppearance));
return NS_OK;
case StyleAppearance::MozWindowButtonRestore:
aPart = mozilla::widget::themeconst::WP_RESTOREBUTTON;
aState = GetWindowFrameButtonState(aFrame,
GetContentState(aFrame, aAppearance));
return NS_OK;
case StyleAppearance::MozWindowButtonBox:
case StyleAppearance::MozWindowButtonBoxMaximized:
case StyleAppearance::MozWinBorderlessGlass:
aPart = -1;
aState = 0;
return NS_OK;
default:
aPart = 0;
aState = 0;
return NS_ERROR_FAILURE;
}
}
static bool AssumeThemePartAndStateAreTransparent(int32_t aPart,
int32_t aState) {
if (!(IsWin8Point1OrLater() && nsUXThemeData::IsHighContrastOn()) &&
aPart == MENU_POPUPITEM && aState == MBI_NORMAL) {
return true;
}
return false;
}
// When running with per-monitor DPI (on Win8.1+), and rendering on a display
// with a different DPI setting from the system's default scaling, we need to
// apply scaling to native-themed elements as the Windows theme APIs assume
// the system default resolution.
static inline double GetThemeDpiScaleFactor(nsPresContext* aPresContext) {
if (WinUtils::IsPerMonitorDPIAware() ||
StaticPrefs::layout_css_devPixelsPerPx() > 0.0) {
nsCOMPtr<nsIWidget> rootWidget = aPresContext->GetRootWidget();
if (rootWidget) {
double systemScale = WinUtils::SystemScaleFactor();
return rootWidget->GetDefaultScale().scale / systemScale;
}
}
return 1.0;
}
static inline double GetThemeDpiScaleFactor(nsIFrame* aFrame) {
return GetThemeDpiScaleFactor(aFrame->PresContext());
}
NS_IMETHODIMP
nsNativeThemeWin::DrawWidgetBackground(gfxContext* aContext, nsIFrame* aFrame,
StyleAppearance aAppearance,
const nsRect& aRect,
const nsRect& aDirtyRect,
DrawOverflow aDrawOverflow) {
if (IsWidgetNonNative(aFrame, aAppearance) != NonNative::No) {
return Theme::DrawWidgetBackground(aContext, aFrame, aAppearance, aRect,
aDirtyRect, aDrawOverflow);
}
HANDLE theme = GetTheme(aAppearance);
if (!theme)
return ClassicDrawWidgetBackground(aContext, aFrame, aAppearance, aRect,
aDirtyRect);
// ^^ without the right sdk, assume xp theming and fall through.
if (gfxWindowsPlatform::GetPlatform()->DwmCompositionEnabled()) {
switch (aAppearance) {
case StyleAppearance::MozWindowTitlebar:
case StyleAppearance::MozWindowTitlebarMaximized:
// Nothing to draw, these areas are glass. Minimum dimensions
// should be set, so xul content should be layed out correctly.
return NS_OK;
case StyleAppearance::MozWindowButtonClose:
case StyleAppearance::MozWindowButtonMinimize:
case StyleAppearance::MozWindowButtonMaximize:
case StyleAppearance::MozWindowButtonRestore:
// Not conventional bitmaps, can't be retrieved. If we fall
// through here and call the theme library we'll get aero
// basic bitmaps.
return NS_OK;
case StyleAppearance::MozWinBorderlessGlass:
// Nothing to draw, this is the glass background.
return NS_OK;
case StyleAppearance::MozWindowButtonBox:
case StyleAppearance::MozWindowButtonBoxMaximized:
// We handle these through nsIWidget::UpdateThemeGeometries
return NS_OK;
default:
break;
}
}
int32_t part, state;
nsresult rv = GetThemePartAndState(aFrame, aAppearance, part, state);
if (NS_FAILED(rv)) return rv;
if (AssumeThemePartAndStateAreTransparent(part, state)) {
return NS_OK;
}
gfxContextMatrixAutoSaveRestore save(aContext);
double themeScale = GetThemeDpiScaleFactor(aFrame);
if (themeScale != 1.0) {
aContext->SetMatrix(
aContext->CurrentMatrix().PreScale(themeScale, themeScale));
}
gfxFloat p2a = gfxFloat(aFrame->PresContext()->AppUnitsPerDevPixel());
RECT widgetRect;
RECT clipRect;
gfxRect tr(aRect.X(), aRect.Y(), aRect.Width(), aRect.Height()),
dr(aDirtyRect.X(), aDirtyRect.Y(), aDirtyRect.Width(),
aDirtyRect.Height());
tr.Scale(1.0 / (p2a * themeScale));
dr.Scale(1.0 / (p2a * themeScale));
gfxWindowsNativeDrawing nativeDrawing(
aContext, dr, GetWidgetNativeDrawingFlags(aAppearance));
RENDER_AGAIN:
HDC hdc = nativeDrawing.BeginNativeDrawing();
if (!hdc) return NS_ERROR_FAILURE;
nativeDrawing.TransformToNativeRect(tr, widgetRect);
nativeDrawing.TransformToNativeRect(dr, clipRect);
#if 0
{
MOZ_LOG(gWindowsLog, LogLevel::Error,
(stderr, "xform: %f %f %f %f [%f %f]\n", m._11, m._21, m._12, m._22,
m._31, m._32));
MOZ_LOG(gWindowsLog, LogLevel::Error,
(stderr, "tr: [%d %d %d %d]\ndr: [%d %d %d %d]\noff: [%f %f]\n",
tr.x, tr.y, tr.width, tr.height, dr.x, dr.y, dr.width, dr.height,
offset.x, offset.y));
}
#endif
if (aAppearance == StyleAppearance::MozWindowTitlebar) {
// Clip out the left and right corners of the frame, all we want in
// is the middle section.
widgetRect.left -= GetSystemMetrics(SM_CXFRAME);
widgetRect.right += GetSystemMetrics(SM_CXFRAME);
} else if (aAppearance == StyleAppearance::MozWindowTitlebarMaximized) {
// The origin of the window is off screen when maximized and windows
// doesn't compensate for this in rendering the background. Push the
// top of the bitmap down by SM_CYFRAME so we get the full graphic.
widgetRect.top += GetSystemMetrics(SM_CYFRAME);
} else if (aAppearance == StyleAppearance::Tab) {
// For left edge and right edge tabs, we need to adjust the widget
// rects and clip rects so that the edges don't get drawn.
bool isLeft = IsLeftToSelectedTab(aFrame);
bool isRight = !isLeft && IsRightToSelectedTab(aFrame);
if (isLeft || isRight) {
// HACK ALERT: There appears to be no way to really obtain this value, so
// we're forced to just use the default value for Luna (which also happens
// to be correct for all the other skins I've tried).
int32_t edgeSize = 2;
// Armed with the size of the edge, we now need to either shift to the
// left or to the right. The clip rect won't include this extra area, so
// we know that we're effectively shifting the edge out of view (such that
// it won't be painted).
if (isLeft)
// The right edge should not be drawn. Extend our rect by the edge
// size.
widgetRect.right += edgeSize;
else
// The left edge should not be drawn. Move the widget rect's left coord
// back.
widgetRect.left -= edgeSize;
}
} else if (aAppearance == StyleAppearance::MozWindowButtonMinimize) {
OffsetBackgroundRect(widgetRect, CAPTIONBUTTON_MINIMIZE);
} else if (aAppearance == StyleAppearance::MozWindowButtonMaximize ||
aAppearance == StyleAppearance::MozWindowButtonRestore) {
OffsetBackgroundRect(widgetRect, CAPTIONBUTTON_RESTORE);
} else if (aAppearance == StyleAppearance::MozWindowButtonClose) {
OffsetBackgroundRect(widgetRect, CAPTIONBUTTON_CLOSE);
}
// widgetRect is the bounding box for a widget, yet the scale track is only
// a small portion of this size, so the edges of the scale need to be
// adjusted to the real size of the track.
if (aAppearance == StyleAppearance::Range) {
RECT contentRect;
GetThemeBackgroundContentRect(theme, hdc, part, state, &widgetRect,
&contentRect);
SIZE siz;
GetThemePartSize(theme, hdc, part, state, &widgetRect, TS_TRUE, &siz);
// When rounding is necessary, we round the position of the track
// away from the chevron of the thumb to make it look better.
if (IsRangeHorizontal(aFrame)) {
contentRect.top += (contentRect.bottom - contentRect.top - siz.cy) / 2;
contentRect.bottom = contentRect.top + siz.cy;
} else {
if (!IsFrameRTL(aFrame)) {
contentRect.left += (contentRect.right - contentRect.left - siz.cx) / 2;
contentRect.right = contentRect.left + siz.cx;
} else {
contentRect.right -=
(contentRect.right - contentRect.left - siz.cx) / 2;
contentRect.left = contentRect.right - siz.cx;
}
}
DrawThemeBackground(theme, hdc, part, state, &contentRect, &clipRect);
} else if (aAppearance == StyleAppearance::Menucheckbox ||
aAppearance == StyleAppearance::Menuradio) {
bool isChecked = false;
isChecked = CheckBooleanAttr(aFrame, nsGkAtoms::checked);
if (isChecked) {
int bgState = MCB_NORMAL;
ElementState elementState = GetContentState(aFrame, aAppearance);
// the disabled states are offset by 1
if (elementState.HasState(ElementState::DISABLED)) {
bgState += 1;
}
SIZE checkboxBGSize(GetCheckboxBGSize(theme, hdc));
RECT checkBGRect = widgetRect;
if (IsFrameRTL(aFrame)) {
checkBGRect.left = checkBGRect.right - checkboxBGSize.cx;
} else {
checkBGRect.right = checkBGRect.left + checkboxBGSize.cx;
}
// Center the checkbox background vertically in the menuitem
checkBGRect.top +=
(checkBGRect.bottom - checkBGRect.top) / 2 - checkboxBGSize.cy / 2;
checkBGRect.bottom = checkBGRect.top + checkboxBGSize.cy;
DrawThemeBackground(theme, hdc, MENU_POPUPCHECKBACKGROUND, bgState,
&checkBGRect, &clipRect);
MARGINS checkMargins = GetCheckboxMargins(theme, hdc);
RECT checkRect = checkBGRect;
checkRect.left += checkMargins.cxLeftWidth;
checkRect.right -= checkMargins.cxRightWidth;
checkRect.top += checkMargins.cyTopHeight;
checkRect.bottom -= checkMargins.cyBottomHeight;
DrawThemeBackground(theme, hdc, MENU_POPUPCHECK, state, &checkRect,
&clipRect);
}
} else if (aAppearance == StyleAppearance::Menupopup) {
DrawThemeBackground(theme, hdc, MENU_POPUPBORDERS, /* state */ 0,
&widgetRect, &clipRect);
SIZE borderSize<