Stop placing std::isfinite and std::signbit inside the global scope
[WebKit-https.git] / Source / WebCore / rendering / RenderTheme.cpp
1 /**
2  * This file is part of the theme implementation for form controls in WebCore.
3  *
4  * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Computer, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include "config.h"
23 #include "RenderTheme.h"
24
25 #include "CSSValueKeywords.h"
26 #include "Document.h"
27 #include "FileList.h"
28 #include "FileSystem.h"
29 #include "FloatConversion.h"
30 #include "FocusController.h"
31 #include "FontSelector.h"
32 #include "Frame.h"
33 #include "FrameSelection.h"
34 #include "GraphicsContext.h"
35 #include "HTMLInputElement.h"
36 #include "HTMLNames.h"
37 #include "LocalizedStrings.h"
38 #include "MediaControlElements.h"
39 #include "Page.h"
40 #include "PaintInfo.h"
41 #include "RenderStyle.h"
42 #include "RenderView.h"
43 #include "Settings.h"
44 #include "SpinButtonElement.h"
45 #include "StringTruncator.h"
46 #include "TextControlInnerElements.h"
47
48 #if ENABLE(METER_ELEMENT)
49 #include "HTMLMeterElement.h"
50 #include "RenderMeter.h"
51 #endif
52
53 #if ENABLE(INPUT_SPEECH)
54 #include "RenderInputSpeech.h"
55 #endif
56
57 #if ENABLE(DATALIST_ELEMENT)
58 #include "ElementShadow.h"
59 #include "HTMLCollection.h"
60 #include "HTMLDataListElement.h"
61 #include "HTMLOptionElement.h"
62 #include "HTMLParserIdioms.h"
63 #endif
64
65 // The methods in this file are shared by all themes on every platform.
66
67 namespace WebCore {
68
69 using namespace HTMLNames;
70
71 static Color& customFocusRingColor()
72 {
73     DEFINE_STATIC_LOCAL(Color, color, ());
74     return color;
75 }
76
77 RenderTheme::RenderTheme()
78 #if USE(NEW_THEME)
79     : m_theme(platformTheme())
80 #endif
81 {
82 }
83
84 void RenderTheme::adjustStyle(StyleResolver* styleResolver, RenderStyle* style, Element* e, bool UAHasAppearance, const BorderData& border, const FillLayer& background, const Color& backgroundColor)
85 {
86     // Force inline and table display styles to be inline-block (except for table- which is block)
87     ControlPart part = style->appearance();
88     if (style->display() == INLINE || style->display() == INLINE_TABLE || style->display() == TABLE_ROW_GROUP
89         || style->display() == TABLE_HEADER_GROUP || style->display() == TABLE_FOOTER_GROUP
90         || style->display() == TABLE_ROW || style->display() == TABLE_COLUMN_GROUP || style->display() == TABLE_COLUMN
91         || style->display() == TABLE_CELL || style->display() == TABLE_CAPTION)
92         style->setDisplay(INLINE_BLOCK);
93     else if (style->display() == COMPACT || style->display() == RUN_IN || style->display() == LIST_ITEM || style->display() == TABLE)
94         style->setDisplay(BLOCK);
95
96     if (UAHasAppearance && isControlStyled(style, border, background, backgroundColor)) {
97         if (part == MenulistPart) {
98             style->setAppearance(MenulistButtonPart);
99             part = MenulistButtonPart;
100         } else
101             style->setAppearance(NoControlPart);
102     }
103
104     if (!style->hasAppearance())
105         return;
106
107     // Never support box-shadow on native controls.
108     style->setBoxShadow(nullptr);
109     
110 #if USE(NEW_THEME)
111     switch (part) {
112     case CheckboxPart:
113     case InnerSpinButtonPart:
114     case RadioPart:
115     case PushButtonPart:
116     case SquareButtonPart:
117     case DefaultButtonPart:
118     case ButtonPart: {
119         // Border
120         LengthBox borderBox(style->borderTopWidth(), style->borderRightWidth(), style->borderBottomWidth(), style->borderLeftWidth());
121         borderBox = m_theme->controlBorder(part, style->font(), borderBox, style->effectiveZoom());
122         if (borderBox.top().value() != static_cast<int>(style->borderTopWidth())) {
123             if (borderBox.top().value())
124                 style->setBorderTopWidth(borderBox.top().value());
125             else
126                 style->resetBorderTop();
127         }
128         if (borderBox.right().value() != static_cast<int>(style->borderRightWidth())) {
129             if (borderBox.right().value())
130                 style->setBorderRightWidth(borderBox.right().value());
131             else
132                 style->resetBorderRight();
133         }
134         if (borderBox.bottom().value() != static_cast<int>(style->borderBottomWidth())) {
135             style->setBorderBottomWidth(borderBox.bottom().value());
136             if (borderBox.bottom().value())
137                 style->setBorderBottomWidth(borderBox.bottom().value());
138             else
139                 style->resetBorderBottom();
140         }
141         if (borderBox.left().value() != static_cast<int>(style->borderLeftWidth())) {
142             style->setBorderLeftWidth(borderBox.left().value());
143             if (borderBox.left().value())
144                 style->setBorderLeftWidth(borderBox.left().value());
145             else
146                 style->resetBorderLeft();
147         }
148
149         // Padding
150         LengthBox paddingBox = m_theme->controlPadding(part, style->font(), style->paddingBox(), style->effectiveZoom());
151         if (paddingBox != style->paddingBox())
152             style->setPaddingBox(paddingBox);
153
154         // Whitespace
155         if (m_theme->controlRequiresPreWhiteSpace(part))
156             style->setWhiteSpace(PRE);
157             
158         // Width / Height
159         // The width and height here are affected by the zoom.
160         // FIXME: Check is flawed, since it doesn't take min-width/max-width into account.
161         LengthSize controlSize = m_theme->controlSize(part, style->font(), LengthSize(style->width(), style->height()), style->effectiveZoom());
162         if (controlSize.width() != style->width())
163             style->setWidth(controlSize.width());
164         if (controlSize.height() != style->height())
165             style->setHeight(controlSize.height());
166                 
167         // Min-Width / Min-Height
168         LengthSize minControlSize = m_theme->minimumControlSize(part, style->font(), style->effectiveZoom());
169         if (minControlSize.width() != style->minWidth())
170             style->setMinWidth(minControlSize.width());
171         if (minControlSize.height() != style->minHeight())
172             style->setMinHeight(minControlSize.height());
173                 
174         // Font
175         FontDescription controlFont = m_theme->controlFont(part, style->font(), style->effectiveZoom());
176         if (controlFont != style->font().fontDescription()) {
177             // Reset our line-height
178             style->setLineHeight(RenderStyle::initialLineHeight());
179
180             // Now update our font.
181             if (style->setFontDescription(controlFont))
182                 style->font().update(0);
183         }
184     }
185     default:
186         break;
187     }
188 #endif
189
190     // Call the appropriate style adjustment method based off the appearance value.
191     switch (style->appearance()) {
192 #if !USE(NEW_THEME)
193     case CheckboxPart:
194         return adjustCheckboxStyle(styleResolver, style, e);
195     case RadioPart:
196         return adjustRadioStyle(styleResolver, style, e);
197     case PushButtonPart:
198     case SquareButtonPart:
199     case DefaultButtonPart:
200     case ButtonPart:
201         return adjustButtonStyle(styleResolver, style, e);
202     case InnerSpinButtonPart:
203         return adjustInnerSpinButtonStyle(styleResolver, style, e);
204 #endif
205     case TextFieldPart:
206         return adjustTextFieldStyle(styleResolver, style, e);
207     case TextAreaPart:
208         return adjustTextAreaStyle(styleResolver, style, e);
209     case MenulistPart:
210         return adjustMenuListStyle(styleResolver, style, e);
211     case MenulistButtonPart:
212         return adjustMenuListButtonStyle(styleResolver, style, e);
213     case MediaPlayButtonPart:
214     case MediaCurrentTimePart:
215     case MediaTimeRemainingPart:
216     case MediaEnterFullscreenButtonPart:
217     case MediaExitFullscreenButtonPart:
218     case MediaMuteButtonPart:
219     case MediaVolumeSliderContainerPart:
220         return adjustMediaControlStyle(styleResolver, style, e);
221     case MediaSliderPart:
222     case MediaVolumeSliderPart:
223     case MediaFullScreenVolumeSliderPart:
224     case SliderHorizontalPart:
225     case SliderVerticalPart:
226         return adjustSliderTrackStyle(styleResolver, style, e);
227     case SliderThumbHorizontalPart:
228     case SliderThumbVerticalPart:
229         return adjustSliderThumbStyle(styleResolver, style, e);
230     case SearchFieldPart:
231         return adjustSearchFieldStyle(styleResolver, style, e);
232     case SearchFieldCancelButtonPart:
233         return adjustSearchFieldCancelButtonStyle(styleResolver, style, e);
234     case SearchFieldDecorationPart:
235         return adjustSearchFieldDecorationStyle(styleResolver, style, e);
236     case SearchFieldResultsDecorationPart:
237         return adjustSearchFieldResultsDecorationStyle(styleResolver, style, e);
238     case SearchFieldResultsButtonPart:
239         return adjustSearchFieldResultsButtonStyle(styleResolver, style, e);
240 #if ENABLE(PROGRESS_ELEMENT)
241     case ProgressBarPart:
242         return adjustProgressBarStyle(styleResolver, style, e);
243 #endif
244 #if ENABLE(METER_ELEMENT)
245     case MeterPart:
246     case RelevancyLevelIndicatorPart:
247     case ContinuousCapacityLevelIndicatorPart:
248     case DiscreteCapacityLevelIndicatorPart:
249     case RatingLevelIndicatorPart:
250         return adjustMeterStyle(styleResolver, style, e);
251 #endif
252 #if ENABLE(INPUT_SPEECH)
253     case InputSpeechButtonPart:
254         return adjustInputFieldSpeechButtonStyle(styleResolver, style, e);
255 #endif
256     default:
257         break;
258     }
259 }
260
261 bool RenderTheme::paint(RenderObject* o, const PaintInfo& paintInfo, const IntRect& r)
262 {
263     // If painting is disabled, but we aren't updating control tints, then just bail.
264     // If we are updating control tints, just schedule a repaint if the theme supports tinting
265     // for that control.
266     if (paintInfo.context->updatingControlTints()) {
267         if (controlSupportsTints(o))
268             o->repaint();
269         return false;
270     }
271     if (paintInfo.context->paintingDisabled())
272         return false;
273
274     ControlPart part = o->style()->appearance();
275
276 #if USE(NEW_THEME)
277     switch (part) {
278     case CheckboxPart:
279     case RadioPart:
280     case PushButtonPart:
281     case SquareButtonPart:
282     case DefaultButtonPart:
283     case ButtonPart:
284     case InnerSpinButtonPart:
285         m_theme->paint(part, controlStatesForRenderer(o), const_cast<GraphicsContext*>(paintInfo.context), r, o->style()->effectiveZoom(), o->view()->frameView());
286         return false;
287     default:
288         break;
289     }
290 #endif
291
292     // Call the appropriate paint method based off the appearance value.
293     switch (part) {
294 #if !USE(NEW_THEME)
295     case CheckboxPart:
296         return paintCheckbox(o, paintInfo, r);
297     case RadioPart:
298         return paintRadio(o, paintInfo, r);
299     case PushButtonPart:
300     case SquareButtonPart:
301     case DefaultButtonPart:
302     case ButtonPart:
303         return paintButton(o, paintInfo, r);
304     case InnerSpinButtonPart:
305         return paintInnerSpinButton(o, paintInfo, r);
306 #endif
307     case MenulistPart:
308         return paintMenuList(o, paintInfo, r);
309 #if ENABLE(METER_ELEMENT)
310     case MeterPart:
311     case RelevancyLevelIndicatorPart:
312     case ContinuousCapacityLevelIndicatorPart:
313     case DiscreteCapacityLevelIndicatorPart:
314     case RatingLevelIndicatorPart:
315         return paintMeter(o, paintInfo, r);
316 #endif
317 #if ENABLE(PROGRESS_ELEMENT)
318     case ProgressBarPart:
319         return paintProgressBar(o, paintInfo, r);
320 #endif
321     case SliderHorizontalPart:
322     case SliderVerticalPart:
323         return paintSliderTrack(o, paintInfo, r);
324     case SliderThumbHorizontalPart:
325     case SliderThumbVerticalPart:
326         return paintSliderThumb(o, paintInfo, r);
327     case MediaEnterFullscreenButtonPart:
328     case MediaExitFullscreenButtonPart:
329         return paintMediaFullscreenButton(o, paintInfo, r);
330     case MediaPlayButtonPart:
331         return paintMediaPlayButton(o, paintInfo, r);
332     case MediaOverlayPlayButtonPart:
333         return paintMediaOverlayPlayButton(o, paintInfo, r);
334     case MediaMuteButtonPart:
335         return paintMediaMuteButton(o, paintInfo, r);
336     case MediaSeekBackButtonPart:
337         return paintMediaSeekBackButton(o, paintInfo, r);
338     case MediaSeekForwardButtonPart:
339         return paintMediaSeekForwardButton(o, paintInfo, r);
340     case MediaRewindButtonPart:
341         return paintMediaRewindButton(o, paintInfo, r);
342     case MediaReturnToRealtimeButtonPart:
343         return paintMediaReturnToRealtimeButton(o, paintInfo, r);
344     case MediaSliderPart:
345         return paintMediaSliderTrack(o, paintInfo, r);
346     case MediaSliderThumbPart:
347         return paintMediaSliderThumb(o, paintInfo, r);
348     case MediaVolumeSliderMuteButtonPart:
349         return paintMediaMuteButton(o, paintInfo, r);
350     case MediaVolumeSliderContainerPart:
351         return paintMediaVolumeSliderContainer(o, paintInfo, r);
352     case MediaVolumeSliderPart:
353         return paintMediaVolumeSliderTrack(o, paintInfo, r);
354     case MediaVolumeSliderThumbPart:
355         return paintMediaVolumeSliderThumb(o, paintInfo, r);
356     case MediaFullScreenVolumeSliderPart:
357         return paintMediaFullScreenVolumeSliderTrack(o, paintInfo, r);
358     case MediaFullScreenVolumeSliderThumbPart:
359         return paintMediaFullScreenVolumeSliderThumb(o, paintInfo, r);
360     case MediaTimeRemainingPart:
361         return paintMediaTimeRemaining(o, paintInfo, r);
362     case MediaCurrentTimePart:
363         return paintMediaCurrentTime(o, paintInfo, r);
364     case MediaControlsBackgroundPart:
365         return paintMediaControlsBackground(o, paintInfo, r);
366     case MenulistButtonPart:
367     case TextFieldPart:
368     case TextAreaPart:
369     case ListboxPart:
370         return true;
371     case SearchFieldPart:
372         return paintSearchField(o, paintInfo, r);
373     case SearchFieldCancelButtonPart:
374         return paintSearchFieldCancelButton(o, paintInfo, r);
375     case SearchFieldDecorationPart:
376         return paintSearchFieldDecoration(o, paintInfo, r);
377     case SearchFieldResultsDecorationPart:
378         return paintSearchFieldResultsDecoration(o, paintInfo, r);
379     case SearchFieldResultsButtonPart:
380         return paintSearchFieldResultsButton(o, paintInfo, r);
381 #if ENABLE(INPUT_SPEECH)
382     case InputSpeechButtonPart:
383         return paintInputFieldSpeechButton(o, paintInfo, r);
384 #endif
385     default:
386         break;
387     }
388
389     return true; // We don't support the appearance, so let the normal background/border paint.
390 }
391
392 bool RenderTheme::paintBorderOnly(RenderObject* o, const PaintInfo& paintInfo, const IntRect& r)
393 {
394     if (paintInfo.context->paintingDisabled())
395         return false;
396
397     // Call the appropriate paint method based off the appearance value.
398     switch (o->style()->appearance()) {
399     case TextFieldPart:
400         return paintTextField(o, paintInfo, r);
401     case ListboxPart:
402     case TextAreaPart:
403         return paintTextArea(o, paintInfo, r);
404     case MenulistButtonPart:
405     case SearchFieldPart:
406         return true;
407     case CheckboxPart:
408     case RadioPart:
409     case PushButtonPart:
410     case SquareButtonPart:
411     case DefaultButtonPart:
412     case ButtonPart:
413     case MenulistPart:
414 #if ENABLE(METER_ELEMENT)
415     case MeterPart:
416     case RelevancyLevelIndicatorPart:
417     case ContinuousCapacityLevelIndicatorPart:
418     case DiscreteCapacityLevelIndicatorPart:
419     case RatingLevelIndicatorPart:
420 #endif
421 #if ENABLE(PROGRESS_ELEMENT)
422     case ProgressBarPart:
423 #endif
424     case SliderHorizontalPart:
425     case SliderVerticalPart:
426     case SliderThumbHorizontalPart:
427     case SliderThumbVerticalPart:
428     case SearchFieldCancelButtonPart:
429     case SearchFieldDecorationPart:
430     case SearchFieldResultsDecorationPart:
431     case SearchFieldResultsButtonPart:
432 #if ENABLE(INPUT_SPEECH)
433     case InputSpeechButtonPart:
434 #endif
435     default:
436         break;
437     }
438
439     return false;
440 }
441
442 bool RenderTheme::paintDecorations(RenderObject* o, const PaintInfo& paintInfo, const IntRect& r)
443 {
444     if (paintInfo.context->paintingDisabled())
445         return false;
446
447     // Call the appropriate paint method based off the appearance value.
448     switch (o->style()->appearance()) {
449     case MenulistButtonPart:
450         return paintMenuListButton(o, paintInfo, r);
451     case TextFieldPart:
452     case TextAreaPart:
453     case ListboxPart:
454     case CheckboxPart:
455     case RadioPart:
456     case PushButtonPart:
457     case SquareButtonPart:
458     case DefaultButtonPart:
459     case ButtonPart:
460     case MenulistPart:
461 #if ENABLE(METER_ELEMENT)
462     case MeterPart:
463     case RelevancyLevelIndicatorPart:
464     case ContinuousCapacityLevelIndicatorPart:
465     case DiscreteCapacityLevelIndicatorPart:
466     case RatingLevelIndicatorPart:
467 #endif
468 #if ENABLE(PROGRESS_ELEMENT)
469     case ProgressBarPart:
470 #endif
471     case SliderHorizontalPart:
472     case SliderVerticalPart:
473     case SliderThumbHorizontalPart:
474     case SliderThumbVerticalPart:
475     case SearchFieldPart:
476     case SearchFieldCancelButtonPart:
477     case SearchFieldDecorationPart:
478     case SearchFieldResultsDecorationPart:
479     case SearchFieldResultsButtonPart:
480 #if ENABLE(INPUT_SPEECH)
481     case InputSpeechButtonPart:
482 #endif
483     default:
484         break;
485     }
486
487     return false;
488 }
489
490 #if ENABLE(VIDEO)
491
492 String RenderTheme::formatMediaControlsTime(float time) const
493 {
494     if (!std::isfinite(time))
495         time = 0;
496     int seconds = (int)fabsf(time);
497     int hours = seconds / (60 * 60);
498     int minutes = (seconds / 60) % 60;
499     seconds %= 60;
500     if (hours) {
501         if (hours > 9)
502             return String::format("%s%02d:%02d:%02d", (time < 0 ? "-" : ""), hours, minutes, seconds);
503
504         return String::format("%s%01d:%02d:%02d", (time < 0 ? "-" : ""), hours, minutes, seconds);
505     }
506
507     return String::format("%s%02d:%02d", (time < 0 ? "-" : ""), minutes, seconds);
508 }
509
510 String RenderTheme::formatMediaControlsCurrentTime(float currentTime, float /*duration*/) const
511 {
512     return formatMediaControlsTime(currentTime);
513 }
514
515 String RenderTheme::formatMediaControlsRemainingTime(float currentTime, float duration) const
516 {
517     return formatMediaControlsTime(currentTime - duration);
518 }
519
520 IntPoint RenderTheme::volumeSliderOffsetFromMuteButton(RenderBox* muteButtonBox, const IntSize& size) const
521 {
522     int y = -size.height();
523     FloatPoint absPoint = muteButtonBox->localToAbsolute(FloatPoint(muteButtonBox->pixelSnappedOffsetLeft(), y), IsFixed | UseTransforms);
524     if (absPoint.y() < 0)
525         y = muteButtonBox->height();
526     return IntPoint(0, y);
527 }
528
529 #endif
530
531 Color RenderTheme::activeSelectionBackgroundColor() const
532 {
533     if (!m_activeSelectionBackgroundColor.isValid())
534         m_activeSelectionBackgroundColor = platformActiveSelectionBackgroundColor().blendWithWhite();
535     return m_activeSelectionBackgroundColor;
536 }
537
538 Color RenderTheme::inactiveSelectionBackgroundColor() const
539 {
540     if (!m_inactiveSelectionBackgroundColor.isValid())
541         m_inactiveSelectionBackgroundColor = platformInactiveSelectionBackgroundColor().blendWithWhite();
542     return m_inactiveSelectionBackgroundColor;
543 }
544
545 Color RenderTheme::activeSelectionForegroundColor() const
546 {
547     if (!m_activeSelectionForegroundColor.isValid() && supportsSelectionForegroundColors())
548         m_activeSelectionForegroundColor = platformActiveSelectionForegroundColor();
549     return m_activeSelectionForegroundColor;
550 }
551
552 Color RenderTheme::inactiveSelectionForegroundColor() const
553 {
554     if (!m_inactiveSelectionForegroundColor.isValid() && supportsSelectionForegroundColors())
555         m_inactiveSelectionForegroundColor = platformInactiveSelectionForegroundColor();
556     return m_inactiveSelectionForegroundColor;
557 }
558
559 Color RenderTheme::activeListBoxSelectionBackgroundColor() const
560 {
561     if (!m_activeListBoxSelectionBackgroundColor.isValid())
562         m_activeListBoxSelectionBackgroundColor = platformActiveListBoxSelectionBackgroundColor();
563     return m_activeListBoxSelectionBackgroundColor;
564 }
565
566 Color RenderTheme::inactiveListBoxSelectionBackgroundColor() const
567 {
568     if (!m_inactiveListBoxSelectionBackgroundColor.isValid())
569         m_inactiveListBoxSelectionBackgroundColor = platformInactiveListBoxSelectionBackgroundColor();
570     return m_inactiveListBoxSelectionBackgroundColor;
571 }
572
573 Color RenderTheme::activeListBoxSelectionForegroundColor() const
574 {
575     if (!m_activeListBoxSelectionForegroundColor.isValid() && supportsListBoxSelectionForegroundColors())
576         m_activeListBoxSelectionForegroundColor = platformActiveListBoxSelectionForegroundColor();
577     return m_activeListBoxSelectionForegroundColor;
578 }
579
580 Color RenderTheme::inactiveListBoxSelectionForegroundColor() const
581 {
582     if (!m_inactiveListBoxSelectionForegroundColor.isValid() && supportsListBoxSelectionForegroundColors())
583         m_inactiveListBoxSelectionForegroundColor = platformInactiveListBoxSelectionForegroundColor();
584     return m_inactiveListBoxSelectionForegroundColor;
585 }
586
587 Color RenderTheme::platformActiveSelectionBackgroundColor() const
588 {
589     // Use a blue color by default if the platform theme doesn't define anything.
590     return Color(0, 0, 255);
591 }
592
593 Color RenderTheme::platformActiveSelectionForegroundColor() const
594 {
595     // Use a white color by default if the platform theme doesn't define anything.
596     return Color::white;
597 }
598
599 Color RenderTheme::platformInactiveSelectionBackgroundColor() const
600 {
601     // Use a grey color by default if the platform theme doesn't define anything.
602     // This color matches Firefox's inactive color.
603     return Color(176, 176, 176);
604 }
605
606 Color RenderTheme::platformInactiveSelectionForegroundColor() const
607 {
608     // Use a black color by default.
609     return Color::black;
610 }
611
612 Color RenderTheme::platformActiveListBoxSelectionBackgroundColor() const
613 {
614     return platformActiveSelectionBackgroundColor();
615 }
616
617 Color RenderTheme::platformActiveListBoxSelectionForegroundColor() const
618 {
619     return platformActiveSelectionForegroundColor();
620 }
621
622 Color RenderTheme::platformInactiveListBoxSelectionBackgroundColor() const
623 {
624     return platformInactiveSelectionBackgroundColor();
625 }
626
627 Color RenderTheme::platformInactiveListBoxSelectionForegroundColor() const
628 {
629     return platformInactiveSelectionForegroundColor();
630 }
631
632 int RenderTheme::baselinePosition(const RenderObject* o) const
633 {
634     if (!o->isBox())
635         return 0;
636
637     const RenderBox* box = toRenderBox(o);
638
639 #if USE(NEW_THEME)
640     return box->height() + box->marginTop() + m_theme->baselinePositionAdjustment(o->style()->appearance()) * o->style()->effectiveZoom();
641 #else
642     return box->height() + box->marginTop();
643 #endif
644 }
645
646 bool RenderTheme::isControlContainer(ControlPart appearance) const
647 {
648     // There are more leaves than this, but we'll patch this function as we add support for
649     // more controls.
650     return appearance != CheckboxPart && appearance != RadioPart;
651 }
652
653 bool RenderTheme::isControlStyled(const RenderStyle* style, const BorderData& border, const FillLayer& background, const Color& backgroundColor) const
654 {
655     switch (style->appearance()) {
656     case PushButtonPart:
657     case SquareButtonPart:
658     case DefaultButtonPart:
659     case ButtonPart:
660     case ListboxPart:
661     case MenulistPart:
662     case ProgressBarPart:
663     case MeterPart:
664     case RelevancyLevelIndicatorPart:
665     case ContinuousCapacityLevelIndicatorPart:
666     case DiscreteCapacityLevelIndicatorPart:
667     case RatingLevelIndicatorPart:
668     // FIXME: Uncomment this when making search fields style-able.
669     // case SearchFieldPart:
670     case TextFieldPart:
671     case TextAreaPart:
672         // Test the style to see if the UA border and background match.
673         return (style->border() != border
674             || *style->backgroundLayers() != background
675             || style->visitedDependentColor(CSSPropertyBackgroundColor) != backgroundColor);
676     default:
677         return false;
678     }
679 }
680
681 void RenderTheme::adjustRepaintRect(const RenderObject* o, IntRect& r)
682 {
683 #if USE(NEW_THEME)
684     m_theme->inflateControlPaintRect(o->style()->appearance(), controlStatesForRenderer(o), r, o->style()->effectiveZoom());
685 #else
686     UNUSED_PARAM(o);
687     UNUSED_PARAM(r);
688 #endif
689 }
690
691 bool RenderTheme::supportsFocusRing(const RenderStyle* style) const
692 {
693     return (style->hasAppearance() && style->appearance() != TextFieldPart && style->appearance() != TextAreaPart && style->appearance() != MenulistButtonPart && style->appearance() != ListboxPart);
694 }
695
696 bool RenderTheme::stateChanged(RenderObject* o, ControlState state) const
697 {
698     // Default implementation assumes the controls don't respond to changes in :hover state
699     if (state == HoverState && !supportsHover(o->style()))
700         return false;
701
702     // Assume pressed state is only responded to if the control is enabled.
703     if (state == PressedState && !isEnabled(o))
704         return false;
705
706     // Repaint the control.
707     o->repaint();
708     return true;
709 }
710
711 ControlStates RenderTheme::controlStatesForRenderer(const RenderObject* o) const
712 {
713     ControlStates result = 0;
714     if (isHovered(o)) {
715         result |= HoverState;
716         if (isSpinUpButtonPartHovered(o))
717             result |= SpinUpState;
718     }
719     if (isPressed(o)) {
720         result |= PressedState;
721         if (isSpinUpButtonPartPressed(o))
722             result |= SpinUpState;
723     }
724     if (isFocused(o) && o->style()->outlineStyleIsAuto())
725         result |= FocusState;
726     if (isEnabled(o))
727         result |= EnabledState;
728     if (isChecked(o))
729         result |= CheckedState;
730     if (isReadOnlyControl(o))
731         result |= ReadOnlyState;
732     if (isDefault(o))
733         result |= DefaultState;
734     if (!isActive(o))
735         result |= WindowInactiveState;
736     if (isIndeterminate(o))
737         result |= IndeterminateState;
738     return result;
739 }
740
741 bool RenderTheme::isActive(const RenderObject* o) const
742 {
743     Node* node = o->node();
744     if (!node)
745         return false;
746
747     Frame* frame = node->document()->frame();
748     if (!frame)
749         return false;
750
751     Page* page = frame->page();
752     if (!page)
753         return false;
754
755     return page->focusController()->isActive();
756 }
757
758 bool RenderTheme::isChecked(const RenderObject* o) const
759 {
760     if (!o->node())
761         return false;
762
763     HTMLInputElement* inputElement = o->node()->toInputElement();
764     if (!inputElement)
765         return false;
766
767     return inputElement->shouldAppearChecked();
768 }
769
770 bool RenderTheme::isIndeterminate(const RenderObject* o) const
771 {
772     if (!o->node())
773         return false;
774
775     HTMLInputElement* inputElement = o->node()->toInputElement();
776     if (!inputElement)
777         return false;
778
779     return inputElement->isIndeterminate();
780 }
781
782 bool RenderTheme::isEnabled(const RenderObject* o) const
783 {
784     Node* node = o->node();
785     if (!node || !node->isElementNode())
786         return true;
787     return static_cast<Element*>(node)->isEnabledFormControl();
788 }
789
790 bool RenderTheme::isFocused(const RenderObject* o) const
791 {
792     Node* node = o->node();
793     if (!node)
794         return false;
795
796     node = node->focusDelegate();
797     Document* document = node->document();
798     Frame* frame = document->frame();
799     return node == document->focusedNode() && frame && frame->selection()->isFocusedAndActive();
800 }
801
802 bool RenderTheme::isPressed(const RenderObject* o) const
803 {
804     if (!o->node())
805         return false;
806     return o->node()->active();
807 }
808
809 bool RenderTheme::isSpinUpButtonPartPressed(const RenderObject* o) const
810 {
811     Node* node = o->node();
812     if (!node || !node->active() || !node->isElementNode()
813         || !static_cast<Element*>(node)->isSpinButtonElement())
814         return false;
815     SpinButtonElement* element = static_cast<SpinButtonElement*>(node);
816     return element->upDownState() == SpinButtonElement::Up;
817 }
818
819 bool RenderTheme::isReadOnlyControl(const RenderObject* o) const
820 {
821     Node* node = o->node();
822     if (!node || !node->isElementNode())
823         return false;
824     return toElement(node)->matchesReadOnlyPseudoClass();
825 }
826
827 bool RenderTheme::isHovered(const RenderObject* o) const
828 {
829     Node* node = o->node();
830     if (!node)
831         return false;
832     if (!node->isElementNode() || !static_cast<Element*>(node)->isSpinButtonElement())
833         return node->hovered();
834     SpinButtonElement* element = static_cast<SpinButtonElement*>(node);
835     return element->hovered() && element->upDownState() != SpinButtonElement::Indeterminate;
836 }
837
838 bool RenderTheme::isSpinUpButtonPartHovered(const RenderObject* o) const
839 {
840     Node* node = o->node();
841     if (!node || !node->isElementNode() || !static_cast<Element*>(node)->isSpinButtonElement())
842         return false;
843     SpinButtonElement* element = static_cast<SpinButtonElement*>(node);
844     return element->upDownState() == SpinButtonElement::Up;
845 }
846
847 bool RenderTheme::isDefault(const RenderObject* o) const
848 {
849     // A button should only have the default appearance if the page is active
850     if (!isActive(o))
851         return false;
852
853     if (!o->document())
854         return false;
855
856     Settings* settings = o->document()->settings();
857     if (!settings || !settings->inApplicationChromeMode())
858         return false;
859     
860     return o->style()->appearance() == DefaultButtonPart;
861 }
862
863 #if !USE(NEW_THEME)
864
865 void RenderTheme::adjustCheckboxStyle(StyleResolver*, RenderStyle* style, Element*) const
866 {
867     // A summary of the rules for checkbox designed to match WinIE:
868     // width/height - honored (WinIE actually scales its control for small widths, but lets it overflow for small heights.)
869     // font-size - not honored (control has no text), but we use it to decide which control size to use.
870     setCheckboxSize(style);
871
872     // padding - not honored by WinIE, needs to be removed.
873     style->resetPadding();
874
875     // border - honored by WinIE, but looks terrible (just paints in the control box and turns off the Windows XP theme)
876     // for now, we will not honor it.
877     style->resetBorder();
878
879     style->setBoxShadow(nullptr);
880 }
881
882 void RenderTheme::adjustRadioStyle(StyleResolver*, RenderStyle* style, Element*) const
883 {
884     // A summary of the rules for checkbox designed to match WinIE:
885     // width/height - honored (WinIE actually scales its control for small widths, but lets it overflow for small heights.)
886     // font-size - not honored (control has no text), but we use it to decide which control size to use.
887     setRadioSize(style);
888
889     // padding - not honored by WinIE, needs to be removed.
890     style->resetPadding();
891
892     // border - honored by WinIE, but looks terrible (just paints in the control box and turns off the Windows XP theme)
893     // for now, we will not honor it.
894     style->resetBorder();
895
896     style->setBoxShadow(nullptr);
897 }
898
899 void RenderTheme::adjustButtonStyle(StyleResolver*, RenderStyle* style, Element*) const
900 {
901     // Most platforms will completely honor all CSS, and so we have no need to
902     // adjust the style at all by default. We will still allow the theme a crack
903     // at setting up a desired vertical size.
904     setButtonSize(style);
905 }
906
907 void RenderTheme::adjustInnerSpinButtonStyle(StyleResolver*, RenderStyle*, Element*) const
908 {
909 }
910 #endif
911
912 void RenderTheme::adjustTextFieldStyle(StyleResolver*, RenderStyle*, Element*) const
913 {
914 }
915
916 void RenderTheme::adjustTextAreaStyle(StyleResolver*, RenderStyle*, Element*) const
917 {
918 }
919
920 void RenderTheme::adjustMenuListStyle(StyleResolver*, RenderStyle*, Element*) const
921 {
922 }
923
924 #if ENABLE(INPUT_SPEECH)
925 void RenderTheme::adjustInputFieldSpeechButtonStyle(StyleResolver* styleResolver, RenderStyle* style, Element* element) const
926 {
927     RenderInputSpeech::adjustInputFieldSpeechButtonStyle(styleResolver, style, element);
928 }
929
930 bool RenderTheme::paintInputFieldSpeechButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
931 {
932     return RenderInputSpeech::paintInputFieldSpeechButton(object, paintInfo, rect);
933 }
934 #endif
935
936 #if ENABLE(METER_ELEMENT)
937 void RenderTheme::adjustMeterStyle(StyleResolver*, RenderStyle* style, Element*) const
938 {
939     style->setBoxShadow(nullptr);
940 }
941
942 IntSize RenderTheme::meterSizeForBounds(const RenderMeter*, const IntRect& bounds) const
943 {
944     return bounds.size();
945 }
946
947 bool RenderTheme::supportsMeter(ControlPart) const
948 {
949     return false;
950 }
951
952 bool RenderTheme::paintMeter(RenderObject*, const PaintInfo&, const IntRect&)
953 {
954     return true;
955 }
956
957 #endif
958
959 #if ENABLE(DATALIST_ELEMENT)
960 LayoutUnit RenderTheme::sliderTickSnappingThreshold() const
961 {
962     return 0;
963 }
964
965 void RenderTheme::paintSliderTicks(RenderObject* o, const PaintInfo& paintInfo, const IntRect& rect)
966 {
967     Node* node = o->node();
968     if (!node)
969         return;
970
971     HTMLInputElement* input = node->toInputElement();
972     if (!input)
973         return;
974
975     HTMLDataListElement* dataList = static_cast<HTMLDataListElement*>(input->list());
976     if (!dataList)
977         return;
978
979     double min = input->minimum();
980     double max = input->maximum();
981     ControlPart part = o->style()->appearance();
982     // We don't support ticks on alternate sliders like MediaVolumeSliders.
983     if (part !=  SliderHorizontalPart && part != SliderVerticalPart)
984         return;
985     bool isHorizontal = part ==  SliderHorizontalPart;
986
987     IntSize thumbSize;
988     RenderObject* thumbRenderer = input->sliderThumbElement()->renderer();
989     if (thumbRenderer) {
990         RenderStyle* thumbStyle = thumbRenderer->style();
991         int thumbWidth = thumbStyle->width().intValue();
992         int thumbHeight = thumbStyle->height().intValue();
993         thumbSize.setWidth(isHorizontal ? thumbWidth : thumbHeight);
994         thumbSize.setHeight(isHorizontal ? thumbHeight : thumbWidth);
995     }
996
997     IntSize tickSize = sliderTickSize();
998     float zoomFactor = o->style()->effectiveZoom();
999     FloatRect tickRect;
1000     int tickRegionSideMargin = 0;
1001     int tickRegionWidth = 0;
1002     IntRect trackBounds;
1003     RenderObject* trackRenderer = input->sliderTrackElement()->renderer();
1004     // We can ignoring transforms because transform is handled by the graphics context.
1005     if (trackRenderer)
1006         trackBounds = trackRenderer->absoluteBoundingBoxRectIgnoringTransforms();
1007     IntRect sliderBounds = o->absoluteBoundingBoxRectIgnoringTransforms();
1008
1009     // Make position relative to the transformed ancestor element.
1010     trackBounds.setX(trackBounds.x() - sliderBounds.x() + rect.x());
1011     trackBounds.setY(trackBounds.y() - sliderBounds.y() + rect.y());
1012
1013     if (isHorizontal) {
1014         tickRect.setWidth(floor(tickSize.width() * zoomFactor));
1015         tickRect.setHeight(floor(tickSize.height() * zoomFactor));
1016         tickRect.setY(floor(rect.y() + rect.height() / 2.0 + sliderTickOffsetFromTrackCenter() * zoomFactor));
1017         tickRegionSideMargin = trackBounds.x() + (thumbSize.width() - tickSize.width() * zoomFactor) / 2.0;
1018         tickRegionWidth = trackBounds.width() - thumbSize.width();
1019     } else {
1020         tickRect.setWidth(floor(tickSize.height() * zoomFactor));
1021         tickRect.setHeight(floor(tickSize.width() * zoomFactor));
1022         tickRect.setX(floor(rect.x() + rect.width() / 2.0 + sliderTickOffsetFromTrackCenter() * zoomFactor));
1023         tickRegionSideMargin = trackBounds.y() + (thumbSize.width() - tickSize.width() * zoomFactor) / 2.0;
1024         tickRegionWidth = trackBounds.height() - thumbSize.width();
1025     }
1026     RefPtr<HTMLCollection> options = dataList->options();
1027     GraphicsContextStateSaver stateSaver(*paintInfo.context);
1028     paintInfo.context->setFillColor(o->style()->visitedDependentColor(CSSPropertyColor), ColorSpaceDeviceRGB);
1029     for (unsigned i = 0; Node* node = options->item(i); i++) {
1030         ASSERT(node->hasTagName(optionTag));
1031         HTMLOptionElement* optionElement = static_cast<HTMLOptionElement*>(node);
1032         String value = optionElement->value();
1033         if (!input->isValidValue(value))
1034             continue;
1035         double parsedValue = parseToDoubleForNumberType(input->sanitizeValue(value));
1036         double tickFraction = (parsedValue - min) / (max - min);
1037         double tickRatio = isHorizontal && o->style()->isLeftToRightDirection() ? tickFraction : 1.0 - tickFraction;
1038         double tickPosition = round(tickRegionSideMargin + tickRegionWidth * tickRatio);
1039         if (isHorizontal)
1040             tickRect.setX(tickPosition);
1041         else
1042             tickRect.setY(tickPosition);
1043         paintInfo.context->fillRect(tickRect);
1044     }
1045 }
1046 #endif
1047
1048 #if ENABLE(PROGRESS_ELEMENT)
1049 double RenderTheme::animationRepeatIntervalForProgressBar(RenderProgress*) const
1050 {
1051     return 0;
1052 }
1053
1054 double RenderTheme::animationDurationForProgressBar(RenderProgress*) const
1055 {
1056     return 0;
1057 }
1058
1059 void RenderTheme::adjustProgressBarStyle(StyleResolver*, RenderStyle*, Element*) const
1060 {
1061 }
1062 #endif
1063
1064 bool RenderTheme::shouldHaveSpinButton(HTMLInputElement* inputElement) const
1065 {
1066     return inputElement->isSteppable() && !inputElement->isRangeControl();
1067 }
1068
1069 void RenderTheme::adjustMenuListButtonStyle(StyleResolver*, RenderStyle*, Element*) const
1070 {
1071 }
1072
1073 void RenderTheme::adjustMediaControlStyle(StyleResolver*, RenderStyle*, Element*) const
1074 {
1075 }
1076
1077 void RenderTheme::adjustSliderTrackStyle(StyleResolver*, RenderStyle*, Element*) const
1078 {
1079 }
1080
1081 void RenderTheme::adjustSliderThumbStyle(StyleResolver*, RenderStyle* style, Element* element) const
1082 {
1083     adjustSliderThumbSize(style, element);
1084 }
1085
1086 void RenderTheme::adjustSliderThumbSize(RenderStyle*, Element*) const
1087 {
1088 }
1089
1090 void RenderTheme::adjustSearchFieldStyle(StyleResolver*, RenderStyle*, Element*) const
1091 {
1092 }
1093
1094 void RenderTheme::adjustSearchFieldCancelButtonStyle(StyleResolver*, RenderStyle*, Element*) const
1095 {
1096 }
1097
1098 void RenderTheme::adjustSearchFieldDecorationStyle(StyleResolver*, RenderStyle*, Element*) const
1099 {
1100 }
1101
1102 void RenderTheme::adjustSearchFieldResultsDecorationStyle(StyleResolver*, RenderStyle*, Element*) const
1103 {
1104 }
1105
1106 void RenderTheme::adjustSearchFieldResultsButtonStyle(StyleResolver*, RenderStyle*, Element*) const
1107 {
1108 }
1109
1110 void RenderTheme::platformColorsDidChange()
1111 {
1112     m_activeSelectionForegroundColor = Color();
1113     m_inactiveSelectionForegroundColor = Color();
1114     m_activeSelectionBackgroundColor = Color();
1115     m_inactiveSelectionBackgroundColor = Color();
1116
1117     m_activeListBoxSelectionForegroundColor = Color();
1118     m_inactiveListBoxSelectionForegroundColor = Color();
1119     m_activeListBoxSelectionBackgroundColor = Color();
1120     m_inactiveListBoxSelectionForegroundColor = Color();
1121
1122     Page::scheduleForcedStyleRecalcForAllPages();
1123 }
1124
1125 Color RenderTheme::systemColor(int cssValueId) const
1126 {
1127     switch (cssValueId) {
1128     case CSSValueActiveborder:
1129         return 0xFFFFFFFF;
1130     case CSSValueActivecaption:
1131         return 0xFFCCCCCC;
1132     case CSSValueAppworkspace:
1133         return 0xFFFFFFFF;
1134     case CSSValueBackground:
1135         return 0xFF6363CE;
1136     case CSSValueButtonface:
1137         return 0xFFC0C0C0;
1138     case CSSValueButtonhighlight:
1139         return 0xFFDDDDDD;
1140     case CSSValueButtonshadow:
1141         return 0xFF888888;
1142     case CSSValueButtontext:
1143         return 0xFF000000;
1144     case CSSValueCaptiontext:
1145         return 0xFF000000;
1146     case CSSValueGraytext:
1147         return 0xFF808080;
1148     case CSSValueHighlight:
1149         return 0xFFB5D5FF;
1150     case CSSValueHighlighttext:
1151         return 0xFF000000;
1152     case CSSValueInactiveborder:
1153         return 0xFFFFFFFF;
1154     case CSSValueInactivecaption:
1155         return 0xFFFFFFFF;
1156     case CSSValueInactivecaptiontext:
1157         return 0xFF7F7F7F;
1158     case CSSValueInfobackground:
1159         return 0xFFFBFCC5;
1160     case CSSValueInfotext:
1161         return 0xFF000000;
1162     case CSSValueMenu:
1163         return 0xFFC0C0C0;
1164     case CSSValueMenutext:
1165         return 0xFF000000;
1166     case CSSValueScrollbar:
1167         return 0xFFFFFFFF;
1168     case CSSValueText:
1169         return 0xFF000000;
1170     case CSSValueThreeddarkshadow:
1171         return 0xFF666666;
1172     case CSSValueThreedface:
1173         return 0xFFC0C0C0;
1174     case CSSValueThreedhighlight:
1175         return 0xFFDDDDDD;
1176     case CSSValueThreedlightshadow:
1177         return 0xFFC0C0C0;
1178     case CSSValueThreedshadow:
1179         return 0xFF888888;
1180     case CSSValueWindow:
1181         return 0xFFFFFFFF;
1182     case CSSValueWindowframe:
1183         return 0xFFCCCCCC;
1184     case CSSValueWindowtext:
1185         return 0xFF000000;
1186     }
1187     return Color();
1188 }
1189
1190 Color RenderTheme::platformActiveTextSearchHighlightColor() const
1191 {
1192     return Color(255, 150, 50); // Orange.
1193 }
1194
1195 Color RenderTheme::platformInactiveTextSearchHighlightColor() const
1196 {
1197     return Color(255, 255, 0); // Yellow.
1198 }
1199
1200 #if ENABLE(TOUCH_EVENTS)
1201 Color RenderTheme::tapHighlightColor()
1202 {
1203     return defaultTheme()->platformTapHighlightColor();
1204 }
1205 #endif
1206
1207 // Value chosen by observation. This can be tweaked.
1208 static const int minColorContrastValue = 1300;
1209 // For transparent or translucent background color, use lightening.
1210 static const int minDisabledColorAlphaValue = 128;
1211
1212 Color RenderTheme::disabledTextColor(const Color& textColor, const Color& backgroundColor) const
1213 {
1214     // The explicit check for black is an optimization for the 99% case (black on white).
1215     // This also means that black on black will turn into grey on black when disabled.
1216     Color disabledColor;
1217     if (textColor.rgb() == Color::black || backgroundColor.alpha() < minDisabledColorAlphaValue || differenceSquared(textColor, Color::white) > differenceSquared(backgroundColor, Color::white))
1218         disabledColor = textColor.light();
1219     else
1220         disabledColor = textColor.dark();
1221     
1222     // If there's not very much contrast between the disabled color and the background color,
1223     // just leave the text color alone. We don't want to change a good contrast color scheme so that it has really bad contrast.
1224     // If the the contrast was already poor, then it doesn't do any good to change it to a different poor contrast color scheme.
1225     if (differenceSquared(disabledColor, backgroundColor) < minColorContrastValue)
1226         return textColor;
1227     
1228     return disabledColor;
1229 }
1230
1231 void RenderTheme::setCustomFocusRingColor(const Color& c)
1232 {
1233     customFocusRingColor() = c;
1234 }
1235
1236 Color RenderTheme::focusRingColor()
1237 {
1238     return customFocusRingColor().isValid() ? customFocusRingColor() : defaultTheme()->platformFocusRingColor();
1239 }
1240
1241 String RenderTheme::fileListDefaultLabel(bool multipleFilesAllowed) const
1242 {
1243     if (multipleFilesAllowed)
1244         return fileButtonNoFilesSelectedLabel();
1245     return fileButtonNoFileSelectedLabel();
1246 }
1247
1248 String RenderTheme::fileListNameForWidth(const FileList* fileList, const Font& font, int width, bool multipleFilesAllowed) const
1249 {
1250     if (width <= 0)
1251         return String();
1252
1253     String string;
1254     if (fileList->isEmpty())
1255         string = fileListDefaultLabel(multipleFilesAllowed);
1256     else if (fileList->length() == 1)
1257         string = fileList->item(0)->name();
1258     else
1259         return StringTruncator::rightTruncate(multipleFileUploadText(fileList->length()), width, font, StringTruncator::EnableRoundingHacks);
1260
1261     return StringTruncator::centerTruncate(string, width, font, StringTruncator::EnableRoundingHacks);
1262 }
1263
1264 bool RenderTheme::shouldOpenPickerWithF4Key() const
1265 {
1266     return false;
1267 }
1268
1269 } // namespace WebCore