RenderThemeAndroid.cpp [plain text]
#include "config.h"
#include "RenderThemeAndroid.h"
#include "Color.h"
#include "Element.h"
#include "GraphicsContext.h"
#include "HTMLNames.h"
#include "HTMLOptionElement.h"
#include "HTMLSelectElement.h"
#include "Node.h"
#include "PlatformGraphicsContext.h"
#include "RenderSkinAndroid.h"
#include "RenderSkinButton.h"
#include "RenderSkinCombo.h"
#include "RenderSkinRadio.h"
#include "SkCanvas.h"
namespace WebCore {
const int buttonPadding = 18;
const int listboxPadding = 5;
const RGBA32 selectionColor = makeRGB(255, 146, 0);
static SkCanvas* getCanvasFromInfo(const RenderObject::PaintInfo& info)
{
return info.context->platformContext()->mCanvas;
}
RenderTheme* theme()
{
DEFINE_STATIC_LOCAL(RenderThemeAndroid, androidTheme, ());
return &androidTheme;
}
PassRefPtr<RenderTheme> RenderTheme::themeForPage(Page* page)
{
static RenderTheme* rt = RenderThemeAndroid::create().releaseRef();
return rt;
}
PassRefPtr<RenderTheme> RenderThemeAndroid::create()
{
return adoptRef(new RenderThemeAndroid());
}
RenderThemeAndroid::RenderThemeAndroid()
{
}
RenderThemeAndroid::~RenderThemeAndroid()
{
}
void RenderThemeAndroid::close()
{
}
bool RenderThemeAndroid::stateChanged(RenderObject* obj, ControlState state) const
{
if (CheckedState == state) {
obj->repaint();
return true;
}
return false;
}
Color RenderThemeAndroid::platformActiveSelectionBackgroundColor() const
{
return Color(selectionColor);
}
Color RenderThemeAndroid::platformInactiveSelectionBackgroundColor() const
{
return Color(Color::transparent);
}
Color RenderThemeAndroid::platformActiveSelectionForegroundColor() const
{
return Color::black;
}
Color RenderThemeAndroid::platformInactiveSelectionForegroundColor() const
{
return Color::black;
}
Color RenderThemeAndroid::platformTextSearchHighlightColor() const
{
return Color(Color::transparent);
}
Color RenderThemeAndroid::platformActiveListBoxSelectionBackgroundColor() const
{
return Color(Color::transparent);
}
Color RenderThemeAndroid::platformInactiveListBoxSelectionBackgroundColor() const
{
return Color(Color::transparent);
}
Color RenderThemeAndroid::platformActiveListBoxSelectionForegroundColor() const
{
return Color(Color::transparent);
}
Color RenderThemeAndroid::platformInactiveListBoxSelectionForegroundColor() const
{
return Color(Color::transparent);
}
int RenderThemeAndroid::baselinePosition(const RenderObject* obj) const
{
return RenderTheme::baselinePosition(obj) - 2;
}
void RenderThemeAndroid::addIntrinsicMargins(RenderStyle* style) const
{
if (style->fontSize() < 11)
return;
const int m = 2;
if (style->width().isIntrinsicOrAuto()) {
if (style->marginLeft().quirk())
style->setMarginLeft(Length(m, Fixed));
if (style->marginRight().quirk())
style->setMarginRight(Length(m, Fixed));
}
if (style->height().isAuto()) {
if (style->marginTop().quirk())
style->setMarginTop(Length(m, Fixed));
if (style->marginBottom().quirk())
style->setMarginBottom(Length(m, Fixed));
}
}
bool RenderThemeAndroid::supportsFocus(ControlPart appearance)
{
switch (appearance) {
case PushButtonPart:
case ButtonPart:
case TextFieldPart:
return true;
default:
return false;
}
return false;
}
void RenderThemeAndroid::adjustButtonStyle(CSSStyleSelector*, RenderStyle* style, WebCore::Element*) const
{
const int padding = 8;
style->setPaddingLeft(Length(padding, Fixed));
style->setPaddingRight(Length(padding, Fixed));
style->setMinHeight(Length(style->fontSize() + buttonPadding, Fixed));
}
bool RenderThemeAndroid::paintCheckbox(RenderObject* obj, const RenderObject::PaintInfo& info, const IntRect& rect)
{
RenderSkinRadio::Draw(getCanvasFromInfo(info), obj->node(), rect, true);
return false;
}
bool RenderThemeAndroid::paintButton(RenderObject* obj, const RenderObject::PaintInfo& info, const IntRect& rect)
{
Node* node = obj->node();
Element* formControlElement = static_cast<Element*>(node);
if (formControlElement && !formControlElement->isEnabledFormControl())
RenderSkinButton::Draw(getCanvasFromInfo(info), rect, RenderSkinAndroid::kDisabled);
else
info.context->platformContext()->storeButtonInfo(node, rect);
return false;
}
bool RenderThemeAndroid::paintRadio(RenderObject* obj, const RenderObject::PaintInfo& info, const IntRect& rect)
{
RenderSkinRadio::Draw(getCanvasFromInfo(info), obj->node(), rect, false);
return false;
}
void RenderThemeAndroid::setCheckboxSize(RenderStyle* style) const
{
style->setWidth(Length(19, Fixed));
style->setHeight(Length(19, Fixed));
}
void RenderThemeAndroid::setRadioSize(RenderStyle* style) const
{
setCheckboxSize(style);
}
void RenderThemeAndroid::adjustTextFieldStyle(CSSStyleSelector*, RenderStyle* style, WebCore::Element*) const
{
addIntrinsicMargins(style);
}
bool RenderThemeAndroid::paintTextField(RenderObject*, const RenderObject::PaintInfo&, const IntRect&)
{
return true;
}
void RenderThemeAndroid::adjustTextAreaStyle(CSSStyleSelector*, RenderStyle* style, WebCore::Element*) const
{
addIntrinsicMargins(style);
}
bool RenderThemeAndroid::paintTextArea(RenderObject* obj, const RenderObject::PaintInfo& info, const IntRect& rect)
{
if (!obj->isListBox())
return true;
paintCombo(obj, info, rect);
RenderStyle* style = obj->style();
if (style)
style->setColor(Color::transparent);
Node* node = obj->node();
if (!node || !node->hasTagName(HTMLNames::selectTag))
return true;
HTMLSelectElement* select = static_cast<HTMLSelectElement*>(node);
node = select->item(0);
if (node) {
RenderObject* renderer = node->renderer();
if (renderer) {
RenderStyle* renderStyle = renderer->style();
if (renderStyle)
renderStyle->setColor(Color::transparent);
}
}
int index = select->selectedIndex();
node = select->item(index);
if (!node || !node->hasTagName(HTMLNames::optionTag))
return true;
HTMLOptionElement* option = static_cast<HTMLOptionElement*>(node);
String label = option->textIndentedToRespectGroupLabel();
SkRect r(rect);
SkPaint paint;
paint.setAntiAlias(true);
paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
paint.setTextSize(r.height() - SkIntToScalar(6));
SkCanvas* canvas = getCanvasFromInfo(info);
int saveCount = canvas->save();
r.fRight -= SkIntToScalar(RenderSkinCombo::extraWidth());
canvas->clipRect(r);
canvas->drawText(label.characters(), label.length() << 1,
r.fLeft + SkIntToScalar(5), r.fBottom - SkIntToScalar(5), paint);
canvas->restoreToCount(saveCount);
return true;
}
void RenderThemeAndroid::adjustSearchFieldStyle(CSSStyleSelector*, RenderStyle* style, Element*) const
{
addIntrinsicMargins(style);
}
bool RenderThemeAndroid::paintSearchField(RenderObject*, const RenderObject::PaintInfo&, const IntRect&)
{
return true;
}
void RenderThemeAndroid::adjustListboxStyle(CSSStyleSelector*, RenderStyle* style, Element*) const
{
style->setPaddingRight(Length(RenderSkinCombo::extraWidth(), Fixed));
style->setMaxHeight(Length(style->fontSize() + listboxPadding, Fixed));
style->setColor(Color::transparent);
addIntrinsicMargins(style);
}
static void adjustMenuListStyleCommon(RenderStyle* style, Element* e)
{
style->setPaddingRight(Length(RenderSkinCombo::extraWidth(), Fixed));
}
void RenderThemeAndroid::adjustMenuListStyle(CSSStyleSelector*, RenderStyle* style, Element* e) const
{
adjustMenuListStyleCommon(style, e);
addIntrinsicMargins(style);
}
bool RenderThemeAndroid::paintCombo(RenderObject* obj, const RenderObject::PaintInfo& info, const IntRect& rect)
{
if (obj->style() && !obj->style()->backgroundColor().alpha())
return true;
return RenderSkinCombo::Draw(getCanvasFromInfo(info), obj->node(), rect.x(), rect.y(), rect.width(), rect.height());
}
bool RenderThemeAndroid::paintMenuList(RenderObject* obj, const RenderObject::PaintInfo& info, const IntRect& rect)
{
return paintCombo(obj, info, rect);
}
void RenderThemeAndroid::adjustMenuListButtonStyle(CSSStyleSelector*, RenderStyle* style, Element* e) const
{
const float baseFontSize = 11.0f;
const int baseBorderRadius = 5;
float fontScale = style->fontSize() / baseFontSize;
style->resetPadding();
style->setBorderRadius(IntSize(int(baseBorderRadius + fontScale - 1), int(baseBorderRadius + fontScale - 1)));
const int minHeight = 15;
style->setMinHeight(Length(minHeight, Fixed));
style->setLineHeight(RenderStyle::initialLineHeight());
const int padding = 4;
style->setPaddingTop(Length(padding, Fixed));
style->setPaddingLeft(Length(padding, Fixed));
adjustMenuListStyleCommon(style, e);
}
bool RenderThemeAndroid::paintMenuListButton(RenderObject* obj, const RenderObject::PaintInfo& info, const IntRect& rect)
{
return paintCombo(obj, info, rect);
}
bool RenderThemeAndroid::supportsFocusRing(const RenderStyle* style) const
{
return style->opacity() > 0
&& style->hasAppearance()
&& style->appearance() != TextFieldPart
&& style->appearance() != SearchFieldPart
&& style->appearance() != TextAreaPart
&& style->appearance() != CheckboxPart
&& style->appearance() != RadioPart
&& style->appearance() != PushButtonPart
&& style->appearance() != SquareButtonPart
&& style->appearance() != ButtonPart
&& style->appearance() != ButtonBevelPart
&& style->appearance() != MenulistPart
&& style->appearance() != MenulistButtonPart;
}
}