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.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
25 #include "RenderThemeChromiumSkia.h"
27 #include "CSSValueKeywords.h"
28 #include "CurrentTime.h"
29 #include "GraphicsContext.h"
30 #include "HTMLMediaElement.h"
31 #include "HTMLNames.h"
33 #include "MediaControlElements.h"
34 #include "PaintInfo.h"
35 #include "PlatformSupport.h"
36 #include "PlatformContextSkia.h"
37 #include "RenderBox.h"
38 #include "RenderMediaControlsChromium.h"
39 #include "RenderObject.h"
40 #include "RenderProgress.h"
41 #include "RenderSlider.h"
42 #include "ScrollbarTheme.h"
43 #include "TimeRanges.h"
44 #include "TransformationMatrix.h"
45 #include "UserAgentStyleSheets.h"
48 #include "SkGradientShader.h"
59 static const int styledMenuListInternalPadding[4] = { 1, 4, 1, 4 };
61 // These values all match Safari/Win.
62 static const float defaultControlFontPixelSize = 13;
63 static const float defaultCancelButtonSize = 9;
64 static const float minCancelButtonSize = 5;
65 static const float maxCancelButtonSize = 21;
66 static const float defaultSearchFieldResultsDecorationSize = 13;
67 static const float minSearchFieldResultsDecorationSize = 9;
68 static const float maxSearchFieldResultsDecorationSize = 30;
69 static const float defaultSearchFieldResultsButtonWidth = 18;
71 // We aim to match IE here.
72 // -IE uses a font based on the encoding as the default font for form controls.
73 // -Gecko uses MS Shell Dlg (actually calls GetStockObject(DEFAULT_GUI_FONT),
74 // which returns MS Shell Dlg)
75 // -Safari uses Lucida Grande.
77 // FIXME: The only case where we know we don't match IE is for ANSI encodings.
78 // IE uses MS Shell Dlg there, which we render incorrectly at certain pixel
79 // sizes (e.g. 15px). So, for now we just use Arial.
80 const String& RenderThemeChromiumSkia::defaultGUIFont()
82 DEFINE_STATIC_LOCAL(String, fontFace, ("Arial"));
86 float RenderThemeChromiumSkia::defaultFontSize = 16.0;
88 RenderThemeChromiumSkia::RenderThemeChromiumSkia()
92 RenderThemeChromiumSkia::~RenderThemeChromiumSkia()
96 // Use the Windows style sheets to match their metrics.
97 String RenderThemeChromiumSkia::extraDefaultStyleSheet()
99 return String(themeWinUserAgentStyleSheet, sizeof(themeWinUserAgentStyleSheet)) +
100 String(themeChromiumSkiaUserAgentStyleSheet, sizeof(themeChromiumSkiaUserAgentStyleSheet)) +
101 String(themeChromiumUserAgentStyleSheet, sizeof(themeChromiumUserAgentStyleSheet));
104 String RenderThemeChromiumSkia::extraQuirksStyleSheet()
106 return String(themeWinQuirksUserAgentStyleSheet, sizeof(themeWinQuirksUserAgentStyleSheet));
110 String RenderThemeChromiumSkia::extraMediaControlsStyleSheet()
112 return String(mediaControlsChromiumUserAgentStyleSheet, sizeof(mediaControlsChromiumUserAgentStyleSheet));
116 bool RenderThemeChromiumSkia::supportsHover(const RenderStyle* style) const
121 bool RenderThemeChromiumSkia::supportsFocusRing(const RenderStyle* style) const
123 // This causes WebKit to draw the focus rings for us.
127 Color RenderThemeChromiumSkia::platformActiveSelectionBackgroundColor() const
129 return Color(0x1e, 0x90, 0xff);
132 Color RenderThemeChromiumSkia::platformInactiveSelectionBackgroundColor() const
134 return Color(0xc8, 0xc8, 0xc8);
137 Color RenderThemeChromiumSkia::platformActiveSelectionForegroundColor() const
142 Color RenderThemeChromiumSkia::platformInactiveSelectionForegroundColor() const
144 return Color(0x32, 0x32, 0x32);
147 Color RenderThemeChromiumSkia::platformFocusRingColor() const
149 static Color focusRingColor(229, 151, 0, 255);
150 return focusRingColor;
153 double RenderThemeChromiumSkia::caretBlinkInterval() const
155 // Disable the blinking caret in layout test mode, as it introduces
156 // a race condition for the pixel tests. http://b/1198440
157 if (PlatformSupport::layoutTestMode())
160 return caretBlinkIntervalInternal();
163 void RenderThemeChromiumSkia::systemFont(int propId, FontDescription& fontDescription) const
165 float fontSize = defaultFontSize;
168 case CSSValueWebkitMiniControl:
169 case CSSValueWebkitSmallControl:
170 case CSSValueWebkitControl:
171 // Why 2 points smaller? Because that's what Gecko does. Note that we
172 // are assuming a 96dpi screen, which is the default that we use on
174 static const float pointsPerInch = 72.0f;
175 static const float pixelsPerInch = 96.0f;
176 fontSize -= (2.0f / pointsPerInch) * pixelsPerInch;
180 fontDescription.firstFamily().setFamily(defaultGUIFont());
181 fontDescription.setSpecifiedSize(fontSize);
182 fontDescription.setIsAbsoluteSize(true);
183 fontDescription.setGenericFamily(FontDescription::NoFamily);
184 fontDescription.setWeight(FontWeightNormal);
185 fontDescription.setItalic(false);
188 int RenderThemeChromiumSkia::minimumMenuListSize(RenderStyle* style) const
193 // These are the default dimensions of radio buttons and checkboxes.
194 static const int widgetStandardWidth = 13;
195 static const int widgetStandardHeight = 13;
197 // Return a rectangle that has the same center point as |original|, but with a
198 // size capped at |width| by |height|.
199 IntRect center(const IntRect& original, int width, int height)
201 width = std::min(original.width(), width);
202 height = std::min(original.height(), height);
203 int x = original.x() + (original.width() - width) / 2;
204 int y = original.y() + (original.height() - height) / 2;
206 return IntRect(x, y, width, height);
209 void RenderThemeChromiumSkia::setCheckboxSize(RenderStyle* style) const
211 // If the width and height are both specified, then we have nothing to do.
212 if (!style->width().isIntrinsicOrAuto() && !style->height().isAuto())
215 // FIXME: A hard-coded size of 13 is used. This is wrong but necessary
216 // for now. It matches Firefox. At different DPI settings on Windows,
217 // querying the theme gives you a larger size that accounts for the higher
218 // DPI. Until our entire engine honors a DPI setting other than 96, we
219 // can't rely on the theme's metrics.
220 const IntSize size(widgetStandardWidth, widgetStandardHeight);
221 setSizeIfAuto(style, size);
224 void RenderThemeChromiumSkia::setRadioSize(RenderStyle* style) const
226 // Use same sizing for radio box as checkbox.
227 setCheckboxSize(style);
230 void RenderThemeChromiumSkia::adjustButtonStyle(CSSStyleSelector*, RenderStyle* style, Element*) const
232 if (style->appearance() == PushButtonPart) {
233 // Ignore line-height.
234 style->setLineHeight(RenderStyle::initialLineHeight());
238 bool RenderThemeChromiumSkia::paintTextArea(RenderObject* o, const PaintInfo& i, const IntRect& r)
240 return paintTextField(o, i, r);
243 void RenderThemeChromiumSkia::adjustSearchFieldStyle(CSSStyleSelector*, RenderStyle* style, Element*) const
245 // Ignore line-height.
246 style->setLineHeight(RenderStyle::initialLineHeight());
249 bool RenderThemeChromiumSkia::paintSearchField(RenderObject* o, const PaintInfo& i, const IntRect& r)
251 return paintTextField(o, i, r);
254 void RenderThemeChromiumSkia::adjustSearchFieldCancelButtonStyle(CSSStyleSelector*, RenderStyle* style, Element*) const
256 // Scale the button size based on the font size
257 float fontScale = style->fontSize() / defaultControlFontPixelSize;
258 int cancelButtonSize = lroundf(std::min(std::max(minCancelButtonSize, defaultCancelButtonSize * fontScale), maxCancelButtonSize));
259 style->setWidth(Length(cancelButtonSize, Fixed));
260 style->setHeight(Length(cancelButtonSize, Fixed));
263 IntRect RenderThemeChromiumSkia::convertToPaintingRect(RenderObject* inputRenderer, const RenderObject* partRenderer, IntRect partRect, const IntRect& localOffset) const
265 // Compute an offset between the part renderer and the input renderer.
266 IntSize offsetFromInputRenderer = -(partRenderer->offsetFromAncestorContainer(inputRenderer));
267 // Move the rect into partRenderer's coords.
268 partRect.move(offsetFromInputRenderer);
269 // Account for the local drawing offset.
270 partRect.move(localOffset.x(), localOffset.y());
275 bool RenderThemeChromiumSkia::paintSearchFieldCancelButton(RenderObject* cancelButtonObject, const PaintInfo& paintInfo, const IntRect& r)
277 // Get the renderer of <input> element.
278 Node* input = cancelButtonObject->node()->shadowAncestorNode();
279 if (!input->renderer()->isBox())
281 RenderBox* inputRenderBox = toRenderBox(input->renderer());
282 IntRect inputContentBox = inputRenderBox->contentBoxRect();
284 // Make sure the scaled button stays square and will fit in its parent's box.
285 int cancelButtonSize = std::min(inputContentBox.width(), std::min(inputContentBox.height(), r.height()));
286 // Calculate cancel button's coordinates relative to the input element.
287 // Center the button vertically. Round up though, so if it has to be one pixel off-center, it will
288 // be one pixel closer to the bottom of the field. This tends to look better with the text.
289 IntRect cancelButtonRect(cancelButtonObject->offsetFromAncestorContainer(inputRenderBox).width(),
290 inputContentBox.y() + (inputContentBox.height() - cancelButtonSize + 1) / 2,
291 cancelButtonSize, cancelButtonSize);
292 IntRect paintingRect = convertToPaintingRect(inputRenderBox, cancelButtonObject, cancelButtonRect, r);
294 static Image* cancelImage = Image::loadPlatformResource("searchCancel").releaseRef();
295 static Image* cancelPressedImage = Image::loadPlatformResource("searchCancelPressed").releaseRef();
296 paintInfo.context->drawImage(isPressed(cancelButtonObject) ? cancelPressedImage : cancelImage,
297 cancelButtonObject->style()->colorSpace(), paintingRect);
301 void RenderThemeChromiumSkia::adjustSearchFieldDecorationStyle(CSSStyleSelector*, RenderStyle* style, Element*) const
303 IntSize emptySize(1, 11);
304 style->setWidth(Length(emptySize.width(), Fixed));
305 style->setHeight(Length(emptySize.height(), Fixed));
308 void RenderThemeChromiumSkia::adjustSearchFieldResultsDecorationStyle(CSSStyleSelector*, RenderStyle* style, Element*) const
310 // Scale the decoration size based on the font size
311 float fontScale = style->fontSize() / defaultControlFontPixelSize;
312 int magnifierSize = lroundf(std::min(std::max(minSearchFieldResultsDecorationSize, defaultSearchFieldResultsDecorationSize * fontScale),
313 maxSearchFieldResultsDecorationSize));
314 style->setWidth(Length(magnifierSize, Fixed));
315 style->setHeight(Length(magnifierSize, Fixed));
318 bool RenderThemeChromiumSkia::paintSearchFieldResultsDecoration(RenderObject* magnifierObject, const PaintInfo& paintInfo, const IntRect& r)
320 // Get the renderer of <input> element.
321 Node* input = magnifierObject->node()->shadowAncestorNode();
322 if (!input->renderer()->isBox())
324 RenderBox* inputRenderBox = toRenderBox(input->renderer());
325 IntRect inputContentBox = inputRenderBox->contentBoxRect();
327 // Make sure the scaled decoration stays square and will fit in its parent's box.
328 int magnifierSize = std::min(inputContentBox.width(), std::min(inputContentBox.height(), r.height()));
329 // Calculate decoration's coordinates relative to the input element.
330 // Center the decoration vertically. Round up though, so if it has to be one pixel off-center, it will
331 // be one pixel closer to the bottom of the field. This tends to look better with the text.
332 IntRect magnifierRect(magnifierObject->offsetFromAncestorContainer(inputRenderBox).width(),
333 inputContentBox.y() + (inputContentBox.height() - magnifierSize + 1) / 2,
334 magnifierSize, magnifierSize);
335 IntRect paintingRect = convertToPaintingRect(inputRenderBox, magnifierObject, magnifierRect, r);
337 static Image* magnifierImage = Image::loadPlatformResource("searchMagnifier").releaseRef();
338 paintInfo.context->drawImage(magnifierImage, magnifierObject->style()->colorSpace(), paintingRect);
342 void RenderThemeChromiumSkia::adjustSearchFieldResultsButtonStyle(CSSStyleSelector*, RenderStyle* style, Element*) const
344 // Scale the button size based on the font size
345 float fontScale = style->fontSize() / defaultControlFontPixelSize;
346 int magnifierHeight = lroundf(std::min(std::max(minSearchFieldResultsDecorationSize, defaultSearchFieldResultsDecorationSize * fontScale),
347 maxSearchFieldResultsDecorationSize));
348 int magnifierWidth = lroundf(magnifierHeight * defaultSearchFieldResultsButtonWidth / defaultSearchFieldResultsDecorationSize);
349 style->setWidth(Length(magnifierWidth, Fixed));
350 style->setHeight(Length(magnifierHeight, Fixed));
353 bool RenderThemeChromiumSkia::paintSearchFieldResultsButton(RenderObject* magnifierObject, const PaintInfo& paintInfo, const IntRect& r)
355 // Get the renderer of <input> element.
356 Node* input = magnifierObject->node()->shadowAncestorNode();
357 if (!input->renderer()->isBox())
359 RenderBox* inputRenderBox = toRenderBox(input->renderer());
360 IntRect inputContentBox = inputRenderBox->contentBoxRect();
362 // Make sure the scaled decoration will fit in its parent's box.
363 int magnifierHeight = std::min(inputContentBox.height(), r.height());
364 int magnifierWidth = std::min(inputContentBox.width(), static_cast<int>(magnifierHeight * defaultSearchFieldResultsButtonWidth / defaultSearchFieldResultsDecorationSize));
365 IntRect magnifierRect(magnifierObject->offsetFromAncestorContainer(inputRenderBox).width(),
366 inputContentBox.y() + (inputContentBox.height() - magnifierHeight + 1) / 2,
367 magnifierWidth, magnifierHeight);
368 IntRect paintingRect = convertToPaintingRect(inputRenderBox, magnifierObject, magnifierRect, r);
370 static Image* magnifierImage = Image::loadPlatformResource("searchMagnifierResults").releaseRef();
371 paintInfo.context->drawImage(magnifierImage, magnifierObject->style()->colorSpace(), paintingRect);
375 bool RenderThemeChromiumSkia::paintMediaControlsBackground(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
378 return RenderMediaControlsChromium::paintMediaControlsPart(MediaTimelineContainer, object, paintInfo, rect);
380 UNUSED_PARAM(object);
381 UNUSED_PARAM(paintInfo);
387 bool RenderThemeChromiumSkia::paintMediaSliderTrack(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
390 return RenderMediaControlsChromium::paintMediaControlsPart(MediaSlider, object, paintInfo, rect);
392 UNUSED_PARAM(object);
393 UNUSED_PARAM(paintInfo);
399 bool RenderThemeChromiumSkia::paintMediaVolumeSliderTrack(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
402 return RenderMediaControlsChromium::paintMediaControlsPart(MediaVolumeSlider, object, paintInfo, rect);
404 UNUSED_PARAM(object);
405 UNUSED_PARAM(paintInfo);
411 void RenderThemeChromiumSkia::adjustSliderThumbSize(RenderStyle* style) const
414 RenderMediaControlsChromium::adjustMediaSliderThumbSize(style);
420 bool RenderThemeChromiumSkia::paintMediaSliderThumb(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
423 return RenderMediaControlsChromium::paintMediaControlsPart(MediaSliderThumb, object, paintInfo, rect);
425 UNUSED_PARAM(object);
426 UNUSED_PARAM(paintInfo);
432 bool RenderThemeChromiumSkia::paintMediaVolumeSliderThumb(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
435 return RenderMediaControlsChromium::paintMediaControlsPart(MediaVolumeSliderThumb, object, paintInfo, rect);
437 UNUSED_PARAM(object);
438 UNUSED_PARAM(paintInfo);
444 bool RenderThemeChromiumSkia::paintMediaPlayButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
447 return RenderMediaControlsChromium::paintMediaControlsPart(MediaPlayButton, object, paintInfo, rect);
449 UNUSED_PARAM(object);
450 UNUSED_PARAM(paintInfo);
456 bool RenderThemeChromiumSkia::paintMediaMuteButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
459 return RenderMediaControlsChromium::paintMediaControlsPart(MediaMuteButton, object, paintInfo, rect);
461 UNUSED_PARAM(object);
462 UNUSED_PARAM(paintInfo);
468 void RenderThemeChromiumSkia::adjustMenuListStyle(CSSStyleSelector* selector, RenderStyle* style, WebCore::Element* e) const
470 // Height is locked to auto on all browsers.
471 style->setLineHeight(RenderStyle::initialLineHeight());
474 void RenderThemeChromiumSkia::adjustMenuListButtonStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
476 adjustMenuListStyle(selector, style, e);
479 // Used to paint styled menulists (i.e. with a non-default border)
480 bool RenderThemeChromiumSkia::paintMenuListButton(RenderObject* o, const PaintInfo& i, const IntRect& rect)
482 return paintMenuList(o, i, rect);
485 int RenderThemeChromiumSkia::popupInternalPaddingLeft(RenderStyle* style) const
487 return menuListInternalPadding(style, LeftPadding);
490 int RenderThemeChromiumSkia::popupInternalPaddingRight(RenderStyle* style) const
492 return menuListInternalPadding(style, RightPadding);
495 int RenderThemeChromiumSkia::popupInternalPaddingTop(RenderStyle* style) const
497 return menuListInternalPadding(style, TopPadding);
500 int RenderThemeChromiumSkia::popupInternalPaddingBottom(RenderStyle* style) const
502 return menuListInternalPadding(style, BottomPadding);
506 void RenderThemeChromiumSkia::setDefaultFontSize(int fontSize)
508 defaultFontSize = static_cast<float>(fontSize);
511 double RenderThemeChromiumSkia::caretBlinkIntervalInternal() const
513 return RenderTheme::caretBlinkInterval();
517 void RenderThemeChromiumSkia::setSizeIfAuto(RenderStyle* style, const IntSize& size)
519 if (style->width().isIntrinsicOrAuto())
520 style->setWidth(Length(size.width(), Fixed));
521 if (style->height().isAuto())
522 style->setHeight(Length(size.height(), Fixed));
525 int RenderThemeChromiumSkia::menuListInternalPadding(RenderStyle* style, int paddingType) const
527 // This internal padding is in addition to the user-supplied padding.
528 // Matches the FF behavior.
529 int padding = styledMenuListInternalPadding[paddingType];
531 // Reserve the space for right arrow here. The rest of the padding is
532 // set by adjustMenuListStyle, since PopMenuWin.cpp uses the padding from
533 // RenderMenuList to lay out the individual items in the popup.
534 // If the MenuList actually has appearance "NoAppearance", then that means
535 // we don't draw a button, so don't reserve space for it.
536 const int barType = style->direction() == LTR ? RightPadding : LeftPadding;
537 if (paddingType == barType && style->appearance() != NoControlPart)
538 padding += ScrollbarTheme::nativeTheme()->scrollbarThickness();
543 #if ENABLE(PROGRESS_TAG)
546 // Following values are come from default of GTK+
548 static const int progressDeltaPixelsPerSecond = 100;
549 static const int progressActivityBlocks = 5;
550 static const int progressAnimationFrmaes = 10;
551 static const double progressAnimationInterval = 0.125;
553 IntRect RenderThemeChromiumSkia::determinateProgressValueRectFor(RenderProgress* renderProgress, const IntRect& rect) const
555 int dx = rect.width() * renderProgress->position();
556 if (renderProgress->style()->direction() == RTL)
557 return IntRect(rect.x() + rect.width() - dx, rect.y(), dx, rect.height());
558 return IntRect(rect.x(), rect.y(), dx, rect.height());
561 IntRect RenderThemeChromiumSkia::indeterminateProgressValueRectFor(RenderProgress* renderProgress, const IntRect& rect) const
564 int valueWidth = rect.width() / progressActivityBlocks;
565 int movableWidth = rect.width() - valueWidth;
566 if (movableWidth <= 0)
569 double progress = renderProgress->animationProgress();
571 return IntRect(rect.x() + progress * 2 * movableWidth, rect.y(), valueWidth, rect.height());
572 return IntRect(rect.x() + (1.0 - progress) * 2 * movableWidth, rect.y(), valueWidth, rect.height());
575 double RenderThemeChromiumSkia::animationRepeatIntervalForProgressBar(RenderProgress*) const
577 return progressAnimationInterval;
580 double RenderThemeChromiumSkia::animationDurationForProgressBar(RenderProgress* renderProgress) const
582 return progressAnimationInterval * progressAnimationFrmaes * 2; // "2" for back and forth
585 IntRect RenderThemeChromiumSkia::progressValueRectFor(RenderProgress* renderProgress, const IntRect& rect) const
587 return renderProgress->isDeterminate() ? determinateProgressValueRectFor(renderProgress, rect) : indeterminateProgressValueRectFor(renderProgress, rect);
592 } // namespace WebCore