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