2 * Copyright (C) 2007 Apple Inc.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 * Copyright (C) 2008 Collabora Ltd.
5 * Copyright (C) 2008, 2009 Google Inc.
6 * Copyright (C) 2009 Kenneth Rohde Christiansen
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
26 #include "core/rendering/RenderThemeChromiumDefault.h"
28 #include "core/CSSValueKeywords.h"
29 #include "core/UserAgentStyleSheets.h"
30 #include "core/rendering/PaintInfo.h"
31 #include "core/rendering/RenderObject.h"
32 #include "core/rendering/RenderProgress.h"
33 #include "platform/LayoutTestSupport.h"
34 #include "platform/graphics/Color.h"
35 #include "platform/graphics/GraphicsContext.h"
36 #include "platform/graphics/GraphicsContextStateSaver.h"
37 #include "public/platform/Platform.h"
38 #include "public/platform/WebRect.h"
39 #include "public/platform/WebThemeEngine.h"
40 #include "wtf/StdLibExtras.h"
44 static bool useMockTheme()
46 return LayoutTestSupport::isRunningLayoutTest();
49 unsigned RenderThemeChromiumDefault::m_activeSelectionBackgroundColor =
51 unsigned RenderThemeChromiumDefault::m_activeSelectionForegroundColor =
53 unsigned RenderThemeChromiumDefault::m_inactiveSelectionBackgroundColor =
55 unsigned RenderThemeChromiumDefault::m_inactiveSelectionForegroundColor =
58 double RenderThemeChromiumDefault::m_caretBlinkInterval;
60 static const unsigned defaultButtonBackgroundColor = 0xffdddddd;
62 static WebThemeEngine::State getWebThemeState(const RenderTheme* theme, const RenderObject* o)
64 if (!theme->isEnabled(o))
65 return WebThemeEngine::StateDisabled;
66 if (useMockTheme() && theme->isReadOnlyControl(o))
67 return WebThemeEngine::StateReadonly;
68 if (theme->isPressed(o))
69 return WebThemeEngine::StatePressed;
70 if (useMockTheme() && theme->isFocused(o))
71 return WebThemeEngine::StateFocused;
72 if (theme->isHovered(o))
73 return WebThemeEngine::StateHover;
75 return WebThemeEngine::StateNormal;
78 PassRefPtr<RenderTheme> RenderThemeChromiumDefault::create()
80 return adoptRef(new RenderThemeChromiumDefault());
83 // RenderTheme::theme for Android is defined in RenderThemeChromiumAndroid.cpp.
85 RenderTheme& RenderTheme::theme()
87 DEFINE_STATIC_REF(RenderTheme, renderTheme, (RenderThemeChromiumDefault::create()));
92 RenderThemeChromiumDefault::RenderThemeChromiumDefault()
94 m_caretBlinkInterval = RenderTheme::caretBlinkInterval();
97 RenderThemeChromiumDefault::~RenderThemeChromiumDefault()
101 bool RenderThemeChromiumDefault::supportsFocusRing(const RenderStyle* style) const
103 if (useMockTheme()) {
104 // Don't use focus rings for buttons when mocking controls.
105 return style->appearance() == ButtonPart
106 || style->appearance() == PushButtonPart
107 || style->appearance() == SquareButtonPart;
110 return RenderThemeChromiumSkia::supportsFocusRing(style);
113 Color RenderThemeChromiumDefault::systemColor(CSSValueID cssValueId) const
115 static const Color defaultButtonGrayColor(0xffdddddd);
116 static const Color defaultMenuColor(0xfff7f7f7);
118 if (cssValueId == CSSValueButtonface) {
120 return Color(0xc0, 0xc0, 0xc0);
121 return defaultButtonGrayColor;
123 if (cssValueId == CSSValueMenu)
124 return defaultMenuColor;
125 return RenderTheme::systemColor(cssValueId);
128 String RenderThemeChromiumDefault::extraDefaultStyleSheet()
130 // FIXME: We should not have OS() branches here.
131 // We should have something like RenderThemeWin, RenderThemeLinux, or
132 // should concatenate UA stylesheets on build time.
134 return RenderThemeChromiumSkia::extraDefaultStyleSheet() +
136 String(themeInputMultipleFieldsCss, sizeof(themeInputMultipleFieldsCss)) +
138 String(themeChromiumLinuxCss, sizeof(themeChromiumLinuxCss));
140 return RenderThemeChromiumSkia::extraDefaultStyleSheet() +
141 String(themeInputMultipleFieldsCss, sizeof(themeInputMultipleFieldsCss));
145 Color RenderThemeChromiumDefault::activeListBoxSelectionBackgroundColor() const
147 return Color(0x28, 0x28, 0x28);
150 Color RenderThemeChromiumDefault::activeListBoxSelectionForegroundColor() const
155 Color RenderThemeChromiumDefault::inactiveListBoxSelectionBackgroundColor() const
157 return Color(0xc8, 0xc8, 0xc8);
160 Color RenderThemeChromiumDefault::inactiveListBoxSelectionForegroundColor() const
162 return Color(0x32, 0x32, 0x32);
165 Color RenderThemeChromiumDefault::platformActiveSelectionBackgroundColor() const
168 return Color(0x00, 0x00, 0xff); // Royal blue.
169 return m_activeSelectionBackgroundColor;
172 Color RenderThemeChromiumDefault::platformInactiveSelectionBackgroundColor() const
175 return Color(0x99, 0x99, 0x99); // Medium gray.
176 return m_inactiveSelectionBackgroundColor;
179 Color RenderThemeChromiumDefault::platformActiveSelectionForegroundColor() const
182 return Color(0xff, 0xff, 0xcc); // Pale yellow.
183 return m_activeSelectionForegroundColor;
186 Color RenderThemeChromiumDefault::platformInactiveSelectionForegroundColor() const
190 return m_inactiveSelectionForegroundColor;
193 IntSize RenderThemeChromiumDefault::sliderTickSize() const
196 return IntSize(1, 3);
197 return IntSize(1, 6);
200 int RenderThemeChromiumDefault::sliderTickOffsetFromTrackCenter() const
207 void RenderThemeChromiumDefault::adjustSliderThumbSize(RenderStyle* style, Element* element) const
209 IntSize size = Platform::current()->themeEngine()->getSize(WebThemeEngine::PartSliderThumb);
211 // FIXME: Mock theme doesn't handle zoomed sliders.
212 float zoomLevel = useMockTheme() ? 1 : style->effectiveZoom();
213 if (style->appearance() == SliderThumbHorizontalPart) {
214 style->setWidth(Length(size.width() * zoomLevel, Fixed));
215 style->setHeight(Length(size.height() * zoomLevel, Fixed));
216 } else if (style->appearance() == SliderThumbVerticalPart) {
217 style->setWidth(Length(size.height() * zoomLevel, Fixed));
218 style->setHeight(Length(size.width() * zoomLevel, Fixed));
220 RenderThemeChromiumSkia::adjustSliderThumbSize(style, element);
223 void RenderThemeChromiumDefault::setCaretBlinkInterval(double interval)
225 m_caretBlinkInterval = interval;
228 double RenderThemeChromiumDefault::caretBlinkIntervalInternal() const
230 return m_caretBlinkInterval;
233 void RenderThemeChromiumDefault::setSelectionColors(
234 unsigned activeBackgroundColor,
235 unsigned activeForegroundColor,
236 unsigned inactiveBackgroundColor,
237 unsigned inactiveForegroundColor)
239 m_activeSelectionBackgroundColor = activeBackgroundColor;
240 m_activeSelectionForegroundColor = activeForegroundColor;
241 m_inactiveSelectionBackgroundColor = inactiveBackgroundColor;
242 m_inactiveSelectionForegroundColor = inactiveForegroundColor;
245 bool RenderThemeChromiumDefault::paintCheckbox(RenderObject* o, const PaintInfo& i, const IntRect& rect)
247 WebThemeEngine::ExtraParams extraParams;
248 WebCanvas* canvas = i.context->canvas();
249 extraParams.button.checked = isChecked(o);
250 extraParams.button.indeterminate = isIndeterminate(o);
252 float zoomLevel = o->style()->effectiveZoom();
253 GraphicsContextStateSaver stateSaver(*i.context, false);
254 IntRect unzoomedRect = rect;
255 if (zoomLevel != 1) {
257 unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
258 unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
259 i.context->translate(unzoomedRect.x(), unzoomedRect.y());
260 i.context->scale(zoomLevel, zoomLevel);
261 i.context->translate(-unzoomedRect.x(), -unzoomedRect.y());
264 Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartCheckbox, getWebThemeState(this, o), WebRect(unzoomedRect), &extraParams);
268 void RenderThemeChromiumDefault::setCheckboxSize(RenderStyle* style) const
270 // If the width and height are both specified, then we have nothing to do.
271 if (!style->width().isIntrinsicOrAuto() && !style->height().isAuto())
274 IntSize size = Platform::current()->themeEngine()->getSize(WebThemeEngine::PartCheckbox);
275 float zoomLevel = style->effectiveZoom();
276 size.setWidth(size.width() * zoomLevel);
277 size.setHeight(size.height() * zoomLevel);
278 setSizeIfAuto(style, size);
281 bool RenderThemeChromiumDefault::paintRadio(RenderObject* o, const PaintInfo& i, const IntRect& rect)
283 WebThemeEngine::ExtraParams extraParams;
284 WebCanvas* canvas = i.context->canvas();
285 extraParams.button.checked = isChecked(o);
287 Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartRadio, getWebThemeState(this, o), WebRect(rect), &extraParams);
291 void RenderThemeChromiumDefault::setRadioSize(RenderStyle* style) const
293 // If the width and height are both specified, then we have nothing to do.
294 if (!style->width().isIntrinsicOrAuto() && !style->height().isAuto())
297 IntSize size = Platform::current()->themeEngine()->getSize(WebThemeEngine::PartRadio);
298 float zoomLevel = style->effectiveZoom();
299 size.setWidth(size.width() * zoomLevel);
300 size.setHeight(size.height() * zoomLevel);
301 setSizeIfAuto(style, size);
304 bool RenderThemeChromiumDefault::paintButton(RenderObject* o, const PaintInfo& i, const IntRect& rect)
306 WebThemeEngine::ExtraParams extraParams;
307 WebCanvas* canvas = i.context->canvas();
308 extraParams.button.hasBorder = true;
309 extraParams.button.backgroundColor = useMockTheme() ? 0xffc0c0c0 : defaultButtonBackgroundColor;
310 if (o->hasBackground())
311 extraParams.button.backgroundColor = o->resolveColor(CSSPropertyBackgroundColor).rgb();
313 Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartButton, getWebThemeState(this, o), WebRect(rect), &extraParams);
317 bool RenderThemeChromiumDefault::paintTextField(RenderObject* o, const PaintInfo& i, const IntRect& rect)
319 // WebThemeEngine does not handle border rounded corner and background image
320 // so return true to draw CSS border and background.
321 if (o->style()->hasBorderRadius() || o->style()->hasBackgroundImage())
324 ControlPart part = o->style()->appearance();
326 WebThemeEngine::ExtraParams extraParams;
327 extraParams.textField.isTextArea = part == TextAreaPart;
328 extraParams.textField.isListbox = part == ListboxPart;
330 WebCanvas* canvas = i.context->canvas();
332 Color backgroundColor = o->resolveColor(CSSPropertyBackgroundColor);
333 extraParams.textField.backgroundColor = backgroundColor.rgb();
335 Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartTextField, getWebThemeState(this, o), WebRect(rect), &extraParams);
339 bool RenderThemeChromiumDefault::paintMenuList(RenderObject* o, const PaintInfo& i, const IntRect& rect)
344 const int right = rect.x() + rect.width();
345 const int middle = rect.y() + rect.height() / 2;
347 WebThemeEngine::ExtraParams extraParams;
348 extraParams.menuList.arrowY = middle;
349 const RenderBox* box = toRenderBox(o);
350 // Match Chromium Win behaviour of showing all borders if any are shown.
351 extraParams.menuList.hasBorder = box->borderRight() || box->borderLeft() || box->borderTop() || box->borderBottom();
352 extraParams.menuList.hasBorderRadius = o->style()->hasBorderRadius();
353 // Fallback to transparent if the specified color object is invalid.
354 Color backgroundColor(Color::transparent);
355 if (o->hasBackground())
356 backgroundColor = o->resolveColor(CSSPropertyBackgroundColor);
357 extraParams.menuList.backgroundColor = backgroundColor.rgb();
359 // If we have a background image, don't fill the content area to expose the
360 // parent's background. Also, we shouldn't fill the content area if the
361 // alpha of the color is 0. The API of Windows GDI ignores the alpha.
362 // FIXME: the normal Aura theme doesn't care about this, so we should
363 // investigate if we really need fillContentArea.
364 extraParams.menuList.fillContentArea = !o->style()->hasBackgroundImage() && backgroundColor.alpha();
366 if (useMockTheme()) {
367 // The size and position of the drop-down button is different between
368 // the mock theme and the regular aura theme.
369 int spacingTop = box->borderTop() + box->paddingTop();
370 int spacingBottom = box->borderBottom() + box->paddingBottom();
371 int spacingRight = box->borderRight() + box->paddingRight();
372 extraParams.menuList.arrowX = (o->style()->direction() == RTL) ? rect.x() + 4 + spacingRight: right - 13 - spacingRight;
373 extraParams.menuList.arrowHeight = rect.height() - spacingBottom - spacingTop;
375 extraParams.menuList.arrowX = (o->style()->direction() == RTL) ? rect.x() + 7 : right - 13;
378 WebCanvas* canvas = i.context->canvas();
380 Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartMenuList, getWebThemeState(this, o), WebRect(rect), &extraParams);
384 bool RenderThemeChromiumDefault::paintMenuListButton(RenderObject* o, const PaintInfo& i, const IntRect& rect)
389 const int right = rect.x() + rect.width();
390 const int middle = rect.y() + rect.height() / 2;
392 WebThemeEngine::ExtraParams extraParams;
393 extraParams.menuList.arrowY = middle;
394 extraParams.menuList.hasBorder = false;
395 extraParams.menuList.hasBorderRadius = o->style()->hasBorderRadius();
396 extraParams.menuList.backgroundColor = Color::transparent;
397 extraParams.menuList.fillContentArea = false;
399 if (useMockTheme()) {
400 const RenderBox* box = toRenderBox(o);
401 // The size and position of the drop-down button is different between
402 // the mock theme and the regular aura theme.
403 int spacingTop = box->borderTop() + box->paddingTop();
404 int spacingBottom = box->borderBottom() + box->paddingBottom();
405 int spacingRight = box->borderRight() + box->paddingRight();
406 extraParams.menuList.arrowX = (o->style()->direction() == RTL) ? rect.x() + 4 + spacingRight: right - 13 - spacingRight;
407 extraParams.menuList.arrowHeight = rect.height() - spacingBottom - spacingTop;
409 extraParams.menuList.arrowX = (o->style()->direction() == RTL) ? rect.x() + 7 : right - 13;
412 WebCanvas* canvas = i.context->canvas();
414 Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartMenuList, getWebThemeState(this, o), WebRect(rect), &extraParams);
418 bool RenderThemeChromiumDefault::paintSliderTrack(RenderObject* o, const PaintInfo& i, const IntRect& rect)
420 WebThemeEngine::ExtraParams extraParams;
421 WebCanvas* canvas = i.context->canvas();
422 extraParams.slider.vertical = o->style()->appearance() == SliderVerticalPart;
424 paintSliderTicks(o, i, rect);
426 // FIXME: Mock theme doesn't handle zoomed sliders.
427 float zoomLevel = useMockTheme() ? 1 : o->style()->effectiveZoom();
428 GraphicsContextStateSaver stateSaver(*i.context, false);
429 IntRect unzoomedRect = rect;
430 if (zoomLevel != 1) {
432 unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
433 unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
434 i.context->translate(unzoomedRect.x(), unzoomedRect.y());
435 i.context->scale(zoomLevel, zoomLevel);
436 i.context->translate(-unzoomedRect.x(), -unzoomedRect.y());
439 Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartSliderTrack, getWebThemeState(this, o), WebRect(unzoomedRect), &extraParams);
444 bool RenderThemeChromiumDefault::paintSliderThumb(RenderObject* o, const PaintInfo& i, const IntRect& rect)
446 WebThemeEngine::ExtraParams extraParams;
447 WebCanvas* canvas = i.context->canvas();
448 extraParams.slider.vertical = o->style()->appearance() == SliderThumbVerticalPart;
449 extraParams.slider.inDrag = isPressed(o);
451 // FIXME: Mock theme doesn't handle zoomed sliders.
452 float zoomLevel = useMockTheme() ? 1 : o->style()->effectiveZoom();
453 GraphicsContextStateSaver stateSaver(*i.context, false);
454 IntRect unzoomedRect = rect;
455 if (zoomLevel != 1) {
457 unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
458 unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
459 i.context->translate(unzoomedRect.x(), unzoomedRect.y());
460 i.context->scale(zoomLevel, zoomLevel);
461 i.context->translate(-unzoomedRect.x(), -unzoomedRect.y());
464 Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartSliderThumb, getWebThemeState(this, o), WebRect(unzoomedRect), &extraParams);
468 void RenderThemeChromiumDefault::adjustInnerSpinButtonStyle(RenderStyle* style, Element*) const
470 IntSize size = Platform::current()->themeEngine()->getSize(WebThemeEngine::PartInnerSpinButton);
472 style->setWidth(Length(size.width(), Fixed));
473 style->setMinWidth(Length(size.width(), Fixed));
476 bool RenderThemeChromiumDefault::paintInnerSpinButton(RenderObject* o, const PaintInfo& i, const IntRect& rect)
478 WebThemeEngine::ExtraParams extraParams;
479 WebCanvas* canvas = i.context->canvas();
480 extraParams.innerSpin.spinUp = (controlStatesForRenderer(o) & SpinUpControlState);
481 extraParams.innerSpin.readOnly = isReadOnlyControl(o);
483 Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartInnerSpinButton, getWebThemeState(this, o), WebRect(rect), &extraParams);
487 bool RenderThemeChromiumDefault::paintProgressBar(RenderObject* o, const PaintInfo& i, const IntRect& rect)
489 if (!o->isProgress())
492 RenderProgress* renderProgress = toRenderProgress(o);
493 IntRect valueRect = progressValueRectFor(renderProgress, rect);
495 WebThemeEngine::ExtraParams extraParams;
496 extraParams.progressBar.determinate = renderProgress->isDeterminate();
497 extraParams.progressBar.valueRectX = valueRect.x();
498 extraParams.progressBar.valueRectY = valueRect.y();
499 extraParams.progressBar.valueRectWidth = valueRect.width();
500 extraParams.progressBar.valueRectHeight = valueRect.height();
502 DirectionFlippingScope scope(o, i, rect);
503 WebCanvas* canvas = i.context->canvas();
504 Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartProgressBar, getWebThemeState(this, o), WebRect(rect), &extraParams);
508 bool RenderThemeChromiumDefault::shouldOpenPickerWithF4Key() const
513 bool RenderThemeChromiumDefault::shouldUseFallbackTheme(RenderStyle* style) const
515 if (useMockTheme()) {
516 // The mock theme can't handle zoomed controls, so we fall back to the "fallback" theme.
517 ControlPart part = style->appearance();
518 if (part == CheckboxPart || part == RadioPart)
519 return style->effectiveZoom() != 1;
521 return RenderTheme::shouldUseFallbackTheme(style);