RenderThemeChromiumWin.cpp [plain text]
#include "config.h"
#include "RenderThemeChromiumWin.h"
#include <windows.h>
#include <uxtheme.h>
#include <vssym32.h>
#include "ChromiumBridge.h"
#include "CSSValueKeywords.h"
#include "FontSelector.h"
#include "FontUtilsChromiumWin.h"
#include "GraphicsContext.h"
#include "HTMLMediaElement.h"
#include "HTMLNames.h"
#include "MediaControlElements.h"
#include "RenderBox.h"
#include "RenderSlider.h"
#include "ScrollbarTheme.h"
#include "TransparencyWin.h"
#include "WindowsVersion.h"
#include <skia/ext/skia_utils_win.h>
#define SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(structName, member) \
offsetof(structName, member) + \
(sizeof static_cast<structName*>(0)->member)
#define NONCLIENTMETRICS_SIZE_PRE_VISTA \
SIZEOF_STRUCT_WITH_SPECIFIED_LAST_MEMBER(NONCLIENTMETRICS, lfMessageFont)
namespace WebCore {
namespace {
class ThemePainter : public TransparencyWin {
public:
ThemePainter(GraphicsContext* context, const IntRect& r)
{
TransformMode transformMode = getTransformMode(context->getCTM());
init(context, getLayerMode(context, transformMode), transformMode, r);
}
~ThemePainter()
{
composite();
}
private:
static bool canvasHasMultipleLayers(const SkCanvas* canvas)
{
SkCanvas::LayerIter iter(const_cast<SkCanvas*>(canvas), false);
iter.next(); return !iter.done(); }
static LayerMode getLayerMode(GraphicsContext* context, TransformMode transformMode)
{
if (context->platformContext()->isDrawingToImageBuffer()) return WhiteLayer;
else if (canvasHasMultipleLayers(context->platformContext()->canvas())) return OpaqueCompositeLayer;
else return transformMode == KeepTransform ? NoLayer : OpaqueCompositeLayer;
}
static TransformMode getTransformMode(const TransformationMatrix& matrix)
{
if (matrix.b() != 0 || matrix.c() != 0) return Untransform;
else if (matrix.a() != 1.0 || matrix.d() != 1.0) return ScaleTransform;
else return KeepTransform;
}
};
}
static void getNonClientMetrics(NONCLIENTMETRICS* metrics)
{
static UINT size = WebCore::isVistaOrNewer() ?
sizeof(NONCLIENTMETRICS) : NONCLIENTMETRICS_SIZE_PRE_VISTA;
metrics->cbSize = size;
bool success = !!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, size, metrics, 0);
ASSERT(success);
}
static FontDescription smallSystemFont;
static FontDescription menuFont;
static FontDescription labelFont;
static bool supportsFocus(ControlPart appearance)
{
switch (appearance) {
case PushButtonPart:
case ButtonPart:
case DefaultButtonPart:
case SearchFieldPart:
case TextFieldPart:
case TextAreaPart:
return true;
}
return false;
}
static float systemFontSize(const LOGFONT& font)
{
float size = -font.lfHeight;
if (size < 0) {
HFONT hFont = CreateFontIndirect(&font);
if (hFont) {
HDC hdc = GetDC(0); if (hdc) {
HGDIOBJ hObject = SelectObject(hdc, hFont);
TEXTMETRIC tm;
GetTextMetrics(hdc, &tm);
SelectObject(hdc, hObject);
ReleaseDC(0, hdc);
size = tm.tmAscent;
}
DeleteObject(hFont);
}
}
return ((size < 12.0f) && (GetACP() == 936)) ? 12.0f : size;
}
static float pointsToPixels(float points)
{
static float pixelsPerInch = 0.0f;
if (!pixelsPerInch) {
HDC hdc = GetDC(0); if (hdc) { pixelsPerInch = GetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(0, hdc);
} else {
pixelsPerInch = 96.0f;
}
}
static const float pointsPerInch = 72.0f;
return points / pointsPerInch * pixelsPerInch;
}
static double querySystemBlinkInterval(double defaultInterval)
{
UINT blinkTime = GetCaretBlinkTime();
if (blinkTime == 0)
return defaultInterval;
if (blinkTime == INFINITE)
return 0;
return blinkTime / 1000.0;
}
PassRefPtr<RenderTheme> RenderThemeChromiumWin::create()
{
return adoptRef(new RenderThemeChromiumWin);
}
PassRefPtr<RenderTheme> RenderTheme::themeForPage(Page* page)
{
static RenderTheme* rt = RenderThemeChromiumWin::create().releaseRef();
return rt;
}
bool RenderThemeChromiumWin::supportsFocusRing(const RenderStyle* style) const
{
return style->appearance() == ButtonPart
|| style->appearance() == PushButtonPart;
}
Color RenderThemeChromiumWin::platformActiveSelectionBackgroundColor() const
{
if (ChromiumBridge::layoutTestMode())
return Color(0x00, 0x00, 0xff); COLORREF color = GetSysColor(COLOR_HIGHLIGHT);
return Color(GetRValue(color), GetGValue(color), GetBValue(color), 0xff);
}
Color RenderThemeChromiumWin::platformInactiveSelectionBackgroundColor() const
{
if (ChromiumBridge::layoutTestMode())
return Color(0x99, 0x99, 0x99); COLORREF color = GetSysColor(COLOR_GRAYTEXT);
return Color(GetRValue(color), GetGValue(color), GetBValue(color), 0xff);
}
Color RenderThemeChromiumWin::platformActiveSelectionForegroundColor() const
{
if (ChromiumBridge::layoutTestMode())
return Color(0xff, 0xff, 0xcc); COLORREF color = GetSysColor(COLOR_HIGHLIGHTTEXT);
return Color(GetRValue(color), GetGValue(color), GetBValue(color), 0xff);
}
Color RenderThemeChromiumWin::platformInactiveSelectionForegroundColor() const
{
return Color::white;
}
Color RenderThemeChromiumWin::platformActiveTextSearchHighlightColor() const
{
return Color(0xff, 0x96, 0x32); }
Color RenderThemeChromiumWin::platformInactiveTextSearchHighlightColor() const
{
return Color(0xff, 0xff, 0x96); }
void RenderThemeChromiumWin::systemFont(int propId, FontDescription& fontDescription) const
{
FontDescription* cachedDesc = 0;
AtomicString faceName;
float fontSize = 0;
switch (propId) {
case CSSValueSmallCaption:
cachedDesc = &smallSystemFont;
if (!smallSystemFont.isAbsoluteSize()) {
NONCLIENTMETRICS metrics;
getNonClientMetrics(&metrics);
faceName = AtomicString(metrics.lfSmCaptionFont.lfFaceName, wcslen(metrics.lfSmCaptionFont.lfFaceName));
fontSize = systemFontSize(metrics.lfSmCaptionFont);
}
break;
case CSSValueMenu:
cachedDesc = &menuFont;
if (!menuFont.isAbsoluteSize()) {
NONCLIENTMETRICS metrics;
getNonClientMetrics(&metrics);
faceName = AtomicString(metrics.lfMenuFont.lfFaceName, wcslen(metrics.lfMenuFont.lfFaceName));
fontSize = systemFontSize(metrics.lfMenuFont);
}
break;
case CSSValueStatusBar:
cachedDesc = &labelFont;
if (!labelFont.isAbsoluteSize()) {
NONCLIENTMETRICS metrics;
getNonClientMetrics(&metrics);
faceName = metrics.lfStatusFont.lfFaceName;
fontSize = systemFontSize(metrics.lfStatusFont);
}
break;
case CSSValueWebkitMiniControl:
case CSSValueWebkitSmallControl:
case CSSValueWebkitControl:
faceName = defaultGUIFont();
fontSize = defaultFontSize - pointsToPixels(2);
break;
default:
faceName = defaultGUIFont();
fontSize = defaultFontSize;
break;
}
if (!cachedDesc)
cachedDesc = &fontDescription;
if (fontSize) {
cachedDesc->firstFamily().setFamily(faceName);
cachedDesc->setIsAbsoluteSize(true);
cachedDesc->setGenericFamily(FontDescription::NoFamily);
cachedDesc->setSpecifiedSize(fontSize);
cachedDesc->setWeight(FontWeightNormal);
cachedDesc->setItalic(false);
}
fontDescription = *cachedDesc;
}
static int cssValueIdToSysColorIndex(int cssValueId)
{
switch (cssValueId) {
case CSSValueActiveborder: return COLOR_ACTIVEBORDER;
case CSSValueActivecaption: return COLOR_ACTIVECAPTION;
case CSSValueAppworkspace: return COLOR_APPWORKSPACE;
case CSSValueBackground: return COLOR_BACKGROUND;
case CSSValueButtonface: return COLOR_BTNFACE;
case CSSValueButtonhighlight: return COLOR_BTNHIGHLIGHT;
case CSSValueButtonshadow: return COLOR_BTNSHADOW;
case CSSValueButtontext: return COLOR_BTNTEXT;
case CSSValueCaptiontext: return COLOR_CAPTIONTEXT;
case CSSValueGraytext: return COLOR_GRAYTEXT;
case CSSValueHighlight: return COLOR_HIGHLIGHT;
case CSSValueHighlighttext: return COLOR_HIGHLIGHTTEXT;
case CSSValueInactiveborder: return COLOR_INACTIVEBORDER;
case CSSValueInactivecaption: return COLOR_INACTIVECAPTION;
case CSSValueInactivecaptiontext: return COLOR_INACTIVECAPTIONTEXT;
case CSSValueInfobackground: return COLOR_INFOBK;
case CSSValueInfotext: return COLOR_INFOTEXT;
case CSSValueMenu: return COLOR_MENU;
case CSSValueMenutext: return COLOR_MENUTEXT;
case CSSValueScrollbar: return COLOR_SCROLLBAR;
case CSSValueThreeddarkshadow: return COLOR_3DDKSHADOW;
case CSSValueThreedface: return COLOR_3DFACE;
case CSSValueThreedhighlight: return COLOR_3DHIGHLIGHT;
case CSSValueThreedlightshadow: return COLOR_3DLIGHT;
case CSSValueThreedshadow: return COLOR_3DSHADOW;
case CSSValueWindow: return COLOR_WINDOW;
case CSSValueWindowframe: return COLOR_WINDOWFRAME;
case CSSValueWindowtext: return COLOR_WINDOWTEXT;
default: return -1; }
}
Color RenderThemeChromiumWin::systemColor(int cssValueId) const
{
int sysColorIndex = cssValueIdToSysColorIndex(cssValueId);
if (ChromiumBridge::layoutTestMode() || (sysColorIndex == -1))
return RenderTheme::systemColor(cssValueId);
COLORREF color = GetSysColor(sysColorIndex);
return Color(GetRValue(color), GetGValue(color), GetBValue(color));
}
void RenderThemeChromiumWin::adjustSliderThumbSize(RenderObject* o) const
{
const int sliderThumbAlongAxis = 11;
const int sliderThumbAcrossAxis = 21;
if (o->style()->appearance() == SliderThumbHorizontalPart) {
o->style()->setWidth(Length(sliderThumbAlongAxis, Fixed));
o->style()->setHeight(Length(sliderThumbAcrossAxis, Fixed));
} else if (o->style()->appearance() == SliderThumbVerticalPart) {
o->style()->setWidth(Length(sliderThumbAcrossAxis, Fixed));
o->style()->setHeight(Length(sliderThumbAlongAxis, Fixed));
} else
RenderThemeChromiumSkia::adjustSliderThumbSize(o);
}
bool RenderThemeChromiumWin::paintCheckbox(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
{
return paintButton(o, i, r);
}
bool RenderThemeChromiumWin::paintRadio(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
{
return paintButton(o, i, r);
}
bool RenderThemeChromiumWin::paintButton(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
{
const ThemeData& themeData = getThemeData(o);
WebCore::ThemePainter painter(i.context, r);
ChromiumBridge::paintButton(painter.context(),
themeData.m_part,
themeData.m_state,
themeData.m_classicState,
painter.drawRect());
return false;
}
bool RenderThemeChromiumWin::paintTextField(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
{
return paintTextFieldInternal(o, i, r, true);
}
bool RenderThemeChromiumWin::paintSliderTrack(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
{
const ThemeData& themeData = getThemeData(o);
WebCore::ThemePainter painter(i.context, r);
ChromiumBridge::paintTrackbar(painter.context(),
themeData.m_part,
themeData.m_state,
themeData.m_classicState,
painter.drawRect());
return false;
}
bool RenderThemeChromiumWin::paintSliderThumb(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
{
return paintSliderTrack(o, i, r);
}
bool RenderThemeChromiumWin::paintMenuList(RenderObject* o, const RenderObject::PaintInfo& i, const IntRect& r)
{
if (!o->isBox())
return false;
const RenderBox* box = toRenderBox(o);
int borderRight = box->borderRight();
int borderLeft = box->borderLeft();
int borderTop = box->borderTop();
int borderBottom = box->borderBottom();
bool drawEdges = !(borderRight == 0 && borderLeft == 0 && borderTop == 0 && borderBottom == 0);
paintTextFieldInternal(o, i, r, drawEdges);
const int buttonWidth = GetSystemMetrics(SM_CXVSCROLL);
int spacingLeft = borderLeft + box->paddingLeft();
int spacingRight = borderRight + box->paddingRight();
int spacingTop = borderTop + box->paddingTop();
int spacingBottom = borderBottom + box->paddingBottom();
int buttonX;
if (r.right() - r.x() < buttonWidth)
buttonX = r.x();
else
buttonX = o->style()->direction() == LTR ? r.right() - spacingRight - buttonWidth : r.x() + spacingLeft;
IntRect rect(buttonX,
r.y() + spacingTop,
std::min(buttonWidth, r.right() - r.x()),
r.height() - (spacingTop + spacingBottom));
WebCore::ThemePainter painter(i.context, rect);
ChromiumBridge::paintMenuList(painter.context(),
CP_DROPDOWNBUTTON,
determineState(o),
determineClassicState(o),
painter.drawRect());
return false;
}
void RenderThemeChromiumWin::setDefaultFontSize(int fontSize)
{
RenderThemeChromiumSkia::setDefaultFontSize(fontSize);
smallSystemFont = menuFont = labelFont = FontDescription();
}
double RenderThemeChromiumWin::caretBlinkIntervalInternal() const
{
static double blinkInterval = querySystemBlinkInterval(RenderTheme::caretBlinkInterval());
return blinkInterval;
}
unsigned RenderThemeChromiumWin::determineState(RenderObject* o)
{
unsigned result = TS_NORMAL;
ControlPart appearance = o->style()->appearance();
if (!isEnabled(o))
result = TS_DISABLED;
else if (isReadOnlyControl(o) && (TextFieldPart == appearance || TextAreaPart == appearance || SearchFieldPart == appearance))
result = ETS_READONLY; else if (isPressed(o)) result = TS_PRESSED;
else if (supportsFocus(appearance) && isFocused(o))
result = ETS_FOCUSED;
else if (isHovered(o))
result = TS_HOT;
if (isChecked(o))
result += 4; return result;
}
unsigned RenderThemeChromiumWin::determineSliderThumbState(RenderObject* o)
{
unsigned result = TUS_NORMAL;
if (!isEnabled(o->parent()))
result = TUS_DISABLED;
else if (supportsFocus(o->style()->appearance()) && isFocused(o->parent()))
result = TUS_FOCUSED;
else if (toRenderSlider(o->parent())->inDragMode())
result = TUS_PRESSED;
else if (isHovered(o))
result = TUS_HOT;
return result;
}
unsigned RenderThemeChromiumWin::determineClassicState(RenderObject* o)
{
unsigned result = 0;
ControlPart part = o->style()->appearance();
if (part == SliderHorizontalPart || part == SliderVerticalPart)
return result;
if (isReadOnlyControl(o) && (part == TextFieldPart || part == TextAreaPart || part == SearchFieldPart))
return result;
if (part == SliderThumbHorizontalPart || part == SliderThumbVerticalPart) {
if (!isEnabled(o->parent()))
result = DFCS_INACTIVE;
else if (toRenderSlider(o->parent())->inDragMode()) result = DFCS_PUSHED;
else if (isHovered(o))
result = DFCS_HOT;
} else {
if (!isEnabled(o))
result = DFCS_INACTIVE;
else if (isPressed(o)) result = DFCS_PUSHED;
else if (supportsFocus(part) && isFocused(o)) result = 0;
else if (isHovered(o))
result = DFCS_HOT;
if (isChecked(o))
result |= DFCS_CHECKED;
}
return result;
}
ThemeData RenderThemeChromiumWin::getThemeData(RenderObject* o)
{
ThemeData result;
switch (o->style()->appearance()) {
case CheckboxPart:
result.m_part = BP_CHECKBOX;
result.m_state = determineState(o);
result.m_classicState = DFCS_BUTTONCHECK;
break;
case RadioPart:
result.m_part = BP_RADIOBUTTON;
result.m_state = determineState(o);
result.m_classicState = DFCS_BUTTONRADIO;
break;
case PushButtonPart:
case ButtonPart:
result.m_part = BP_PUSHBUTTON;
result.m_state = determineState(o);
result.m_classicState = DFCS_BUTTONPUSH;
break;
case SliderHorizontalPart:
result.m_part = TKP_TRACK;
result.m_state = TRS_NORMAL;
break;
case SliderVerticalPart:
result.m_part = TKP_TRACKVERT;
result.m_state = TRVS_NORMAL;
break;
case SliderThumbHorizontalPart:
result.m_part = TKP_THUMBBOTTOM;
result.m_state = determineSliderThumbState(o);
break;
case SliderThumbVerticalPart:
result.m_part = TKP_THUMBVERT;
result.m_state = determineSliderThumbState(o);
break;
case ListboxPart:
case MenulistPart:
case MenulistButtonPart:
case SearchFieldPart:
case TextFieldPart:
case TextAreaPart:
result.m_part = EP_EDITTEXT;
result.m_state = determineState(o);
break;
}
result.m_classicState |= determineClassicState(o);
return result;
}
bool RenderThemeChromiumWin::paintTextFieldInternal(RenderObject* o,
const RenderObject::PaintInfo& i,
const IntRect& r,
bool drawEdges)
{
Color backgroundColor(Color::white);
if (o->style()->backgroundColor().isValid())
backgroundColor = o->style()->backgroundColor();
bool fillContentArea = !o->style()->hasBackgroundImage() && backgroundColor.alpha();
if (o->style()->hasBorderRadius()) {
i.context->save();
IntSize topLeft, topRight, bottomLeft, bottomRight;
o->style()->getBorderRadiiForRect(r, topLeft, topRight, bottomLeft, bottomRight);
i.context->addRoundedRectClip(r, topLeft, topRight, bottomLeft, bottomRight);
}
{
const ThemeData& themeData = getThemeData(o);
WebCore::ThemePainter painter(i.context, r);
ChromiumBridge::paintTextField(painter.context(),
themeData.m_part,
themeData.m_state,
themeData.m_classicState,
painter.drawRect(),
backgroundColor,
fillContentArea,
drawEdges);
}
if (o->style()->hasBorderRadius())
i.context->restore();
return false;
}
}