cdd01e34f72b7af79d8b63bdbaf9a26da0d61657
[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     static NeverDestroyed<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, const Element* element, 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         if (auto themeFont = m_theme->controlFont(part, style.fontCascade(), style.effectiveZoom())) {
174             // If overriding the specified font with the theme font, also override the line height with the standard line height.
175             style.setLineHeight(RenderStyle::initialLineHeight());
176             if (style.setFontDescription(themeFont.value()))
177                 style.fontCascade().update(nullptr);
178         }
179
180         // Special style that tells enabled default buttons in active windows to use the ActiveButtonText color.
181         // The active window part of the test has to be done at paint time since it's not triggered by a style change.
182         style.setInsideDefaultButton(part == DefaultButtonPart && element && !element->isDisabledFormControl());
183         break;
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, element);
195     case RadioPart:
196         return adjustRadioStyle(styleResolver, style, element);
197     case PushButtonPart:
198     case SquareButtonPart:
199     case DefaultButtonPart:
200     case ButtonPart:
201         return adjustButtonStyle(styleResolver, style, element);
202     case InnerSpinButtonPart:
203         return adjustInnerSpinButtonStyle(styleResolver, style, element);
204 #endif
205     case TextFieldPart:
206         return adjustTextFieldStyle(styleResolver, style, element);
207     case TextAreaPart:
208         return adjustTextAreaStyle(styleResolver, style, element);
209     case MenulistPart:
210         return adjustMenuListStyle(styleResolver, style, element);
211     case MenulistButtonPart:
212         return adjustMenuListButtonStyle(styleResolver, style, element);
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, element);
221     case MediaSliderPart:
222     case MediaVolumeSliderPart:
223     case MediaFullScreenVolumeSliderPart:
224     case SliderHorizontalPart:
225     case SliderVerticalPart:
226         return adjustSliderTrackStyle(styleResolver, style, element);
227     case SliderThumbHorizontalPart:
228     case SliderThumbVerticalPart:
229         return adjustSliderThumbStyle(styleResolver, style, element);
230     case SearchFieldPart:
231         return adjustSearchFieldStyle(styleResolver, style, element);
232     case SearchFieldCancelButtonPart:
233         return adjustSearchFieldCancelButtonStyle(styleResolver, style, element);
234     case SearchFieldDecorationPart:
235         return adjustSearchFieldDecorationPartStyle(styleResolver, style, element);
236     case SearchFieldResultsDecorationPart:
237         return adjustSearchFieldResultsDecorationPartStyle(styleResolver, style, element);
238     case SearchFieldResultsButtonPart:
239         return adjustSearchFieldResultsButtonStyle(styleResolver, style, element);
240     case ProgressBarPart:
241         return adjustProgressBarStyle(styleResolver, style, element);
242 #if ENABLE(METER_ELEMENT)
243     case MeterPart:
244     case RelevancyLevelIndicatorPart:
245     case ContinuousCapacityLevelIndicatorPart:
246     case DiscreteCapacityLevelIndicatorPart:
247     case RatingLevelIndicatorPart:
248         return adjustMeterStyle(styleResolver, style, element);
249 #endif
250 #if ENABLE(SERVICE_CONTROLS)
251     case ImageControlsButtonPart:
252         break;
253 #endif
254     case CapsLockIndicatorPart:
255         return adjustCapsLockIndicatorStyle(styleResolver, style, element);
256 #if ENABLE(APPLE_PAY)
257     case ApplePayButtonPart:
258         return adjustApplePayButtonStyle(styleResolver, style, element);
259 #endif
260 #if ENABLE(ATTACHMENT_ELEMENT)
261     case AttachmentPart:
262         return adjustAttachmentStyle(styleResolver, style, element);
263 #endif
264     default:
265         break;
266     }
267 }
268
269 bool RenderTheme::paint(const RenderBox& box, ControlStates& controlStates, const PaintInfo& paintInfo, const LayoutRect& rect)
270 {
271     // If painting is disabled, but we aren't updating control tints, then just bail.
272     // If we are updating control tints, just schedule a repaint if the theme supports tinting
273     // for that control.
274     if (paintInfo.context().updatingControlTints()) {
275         if (controlSupportsTints(box))
276             box.repaint();
277         return false;
278     }
279     if (paintInfo.context().paintingDisabled())
280         return false;
281
282     if (UNLIKELY(paintInfo.context().isRecording()))
283         return false;
284
285     ControlPart part = box.style().appearance();
286     IntRect integralSnappedRect = snappedIntRect(rect);
287     float deviceScaleFactor = box.document().deviceScaleFactor();
288     FloatRect devicePixelSnappedRect = snapRectToDevicePixels(rect, deviceScaleFactor);
289
290 #if USE(NEW_THEME)
291     float pageScaleFactor = box.document().page() ? box.document().page()->pageScaleFactor() : 1.0f;
292     
293     switch (part) {
294     case CheckboxPart:
295     case RadioPart:
296     case PushButtonPart:
297     case SquareButtonPart:
298     case DefaultButtonPart:
299     case ButtonPart:
300     case InnerSpinButtonPart:
301         updateControlStatesForRenderer(box, controlStates);
302         m_theme->paint(part, controlStates, paintInfo.context(), devicePixelSnappedRect, box.style().effectiveZoom(), &box.view().frameView(), deviceScaleFactor, pageScaleFactor);
303         return false;
304     default:
305         break;
306     }
307 #else
308     UNUSED_PARAM(controlStates);
309 #endif
310
311     // Call the appropriate paint method based off the appearance value.
312     switch (part) {
313 #if !USE(NEW_THEME)
314     case CheckboxPart:
315         return paintCheckbox(box, paintInfo, integralSnappedRect);
316     case RadioPart:
317         return paintRadio(box, paintInfo, integralSnappedRect);
318     case PushButtonPart:
319     case SquareButtonPart:
320     case DefaultButtonPart:
321     case ButtonPart:
322         return paintButton(box, paintInfo, integralSnappedRect);
323     case InnerSpinButtonPart:
324         return paintInnerSpinButton(box, paintInfo, integralSnappedRect);
325 #endif
326     case MenulistPart:
327         return paintMenuList(box, paintInfo, devicePixelSnappedRect);
328 #if ENABLE(METER_ELEMENT)
329     case MeterPart:
330     case RelevancyLevelIndicatorPart:
331     case ContinuousCapacityLevelIndicatorPart:
332     case DiscreteCapacityLevelIndicatorPart:
333     case RatingLevelIndicatorPart:
334         return paintMeter(box, paintInfo, integralSnappedRect);
335 #endif
336     case ProgressBarPart:
337         return paintProgressBar(box, paintInfo, integralSnappedRect);
338     case SliderHorizontalPart:
339     case SliderVerticalPart:
340         return paintSliderTrack(box, paintInfo, integralSnappedRect);
341     case SliderThumbHorizontalPart:
342     case SliderThumbVerticalPart:
343         return paintSliderThumb(box, paintInfo, integralSnappedRect);
344     case MediaEnterFullscreenButtonPart:
345     case MediaExitFullscreenButtonPart:
346         return paintMediaFullscreenButton(box, paintInfo, integralSnappedRect);
347     case MediaPlayButtonPart:
348         return paintMediaPlayButton(box, paintInfo, integralSnappedRect);
349     case MediaOverlayPlayButtonPart:
350         return paintMediaOverlayPlayButton(box, paintInfo, integralSnappedRect);
351     case MediaMuteButtonPart:
352         return paintMediaMuteButton(box, paintInfo, integralSnappedRect);
353     case MediaSeekBackButtonPart:
354         return paintMediaSeekBackButton(box, paintInfo, integralSnappedRect);
355     case MediaSeekForwardButtonPart:
356         return paintMediaSeekForwardButton(box, paintInfo, integralSnappedRect);
357     case MediaRewindButtonPart:
358         return paintMediaRewindButton(box, paintInfo, integralSnappedRect);
359     case MediaReturnToRealtimeButtonPart:
360         return paintMediaReturnToRealtimeButton(box, paintInfo, integralSnappedRect);
361     case MediaToggleClosedCaptionsButtonPart:
362         return paintMediaToggleClosedCaptionsButton(box, paintInfo, integralSnappedRect);
363     case MediaSliderPart:
364         return paintMediaSliderTrack(box, paintInfo, integralSnappedRect);
365     case MediaSliderThumbPart:
366         return paintMediaSliderThumb(box, paintInfo, integralSnappedRect);
367     case MediaVolumeSliderMuteButtonPart:
368         return paintMediaMuteButton(box, paintInfo, integralSnappedRect);
369     case MediaVolumeSliderContainerPart:
370         return paintMediaVolumeSliderContainer(box, paintInfo, integralSnappedRect);
371     case MediaVolumeSliderPart:
372         return paintMediaVolumeSliderTrack(box, paintInfo, integralSnappedRect);
373     case MediaVolumeSliderThumbPart:
374         return paintMediaVolumeSliderThumb(box, paintInfo, integralSnappedRect);
375     case MediaFullScreenVolumeSliderPart:
376         return paintMediaFullScreenVolumeSliderTrack(box, paintInfo, integralSnappedRect);
377     case MediaFullScreenVolumeSliderThumbPart:
378         return paintMediaFullScreenVolumeSliderThumb(box, paintInfo, integralSnappedRect);
379     case MediaTimeRemainingPart:
380         return paintMediaTimeRemaining(box, paintInfo, integralSnappedRect);
381     case MediaCurrentTimePart:
382         return paintMediaCurrentTime(box, paintInfo, integralSnappedRect);
383     case MediaControlsBackgroundPart:
384         return paintMediaControlsBackground(box, paintInfo, integralSnappedRect);
385     case MenulistButtonPart:
386     case TextFieldPart:
387     case TextAreaPart:
388     case ListboxPart:
389         return true;
390     case SearchFieldPart:
391         return paintSearchField(box, paintInfo, integralSnappedRect);
392     case SearchFieldCancelButtonPart:
393         return paintSearchFieldCancelButton(box, paintInfo, integralSnappedRect);
394     case SearchFieldDecorationPart:
395         return paintSearchFieldDecorationPart(box, paintInfo, integralSnappedRect);
396     case SearchFieldResultsDecorationPart:
397         return paintSearchFieldResultsDecorationPart(box, paintInfo, integralSnappedRect);
398     case SearchFieldResultsButtonPart:
399         return paintSearchFieldResultsButton(box, paintInfo, integralSnappedRect);
400     case SnapshottedPluginOverlayPart:
401         return paintSnapshottedPluginOverlay(box, paintInfo, integralSnappedRect);
402 #if ENABLE(SERVICE_CONTROLS)
403     case ImageControlsButtonPart:
404         return paintImageControlsButton(box, paintInfo, integralSnappedRect);
405 #endif
406     case CapsLockIndicatorPart:
407         return paintCapsLockIndicator(box, paintInfo, integralSnappedRect);
408 #if ENABLE(APPLE_PAY)
409     case ApplePayButtonPart:
410         return paintApplePayButton(box, paintInfo, integralSnappedRect);
411 #endif
412 #if ENABLE(ATTACHMENT_ELEMENT)
413     case AttachmentPart:
414         return paintAttachment(box, paintInfo, integralSnappedRect);
415 #endif
416     default:
417         break;
418     }
419
420     return true; // We don't support the appearance, so let the normal background/border paint.
421 }
422
423 bool RenderTheme::paintBorderOnly(const RenderBox& box, const PaintInfo& paintInfo, const LayoutRect& rect)
424 {
425     if (paintInfo.context().paintingDisabled())
426         return false;
427
428 #if PLATFORM(IOS)
429     UNUSED_PARAM(rect);
430     return box.style().appearance() != NoControlPart;
431 #else
432     FloatRect devicePixelSnappedRect = snapRectToDevicePixels(rect, box.document().deviceScaleFactor());
433     // Call the appropriate paint method based off the appearance value.
434     switch (box.style().appearance()) {
435     case TextFieldPart:
436         return paintTextField(box, paintInfo, devicePixelSnappedRect);
437     case ListboxPart:
438     case TextAreaPart:
439         return paintTextArea(box, paintInfo, devicePixelSnappedRect);
440     case MenulistButtonPart:
441     case SearchFieldPart:
442         return true;
443     case CheckboxPart:
444     case RadioPart:
445     case PushButtonPart:
446     case SquareButtonPart:
447     case DefaultButtonPart:
448     case ButtonPart:
449     case MenulistPart:
450 #if ENABLE(METER_ELEMENT)
451     case MeterPart:
452     case RelevancyLevelIndicatorPart:
453     case ContinuousCapacityLevelIndicatorPart:
454     case DiscreteCapacityLevelIndicatorPart:
455     case RatingLevelIndicatorPart:
456 #endif
457     case ProgressBarPart:
458     case SliderHorizontalPart:
459     case SliderVerticalPart:
460     case SliderThumbHorizontalPart:
461     case SliderThumbVerticalPart:
462     case SearchFieldCancelButtonPart:
463     case SearchFieldDecorationPart:
464     case SearchFieldResultsDecorationPart:
465     case SearchFieldResultsButtonPart:
466 #if ENABLE(SERVICE_CONTROLS)
467     case ImageControlsButtonPart:
468 #endif
469     default:
470         break;
471     }
472
473     return false;
474 #endif
475 }
476
477 bool RenderTheme::paintDecorations(const RenderBox& box, const PaintInfo& paintInfo, const LayoutRect& rect)
478 {
479     if (paintInfo.context().paintingDisabled())
480         return false;
481
482     IntRect integralSnappedRect = snappedIntRect(rect);
483     FloatRect devicePixelSnappedRect = snapRectToDevicePixels(rect, box.document().deviceScaleFactor());
484
485     // Call the appropriate paint method based off the appearance value.
486     switch (box.style().appearance()) {
487     case MenulistButtonPart:
488         return paintMenuListButtonDecorations(box, paintInfo, devicePixelSnappedRect);
489     case TextFieldPart:
490         return paintTextFieldDecorations(box, paintInfo, devicePixelSnappedRect);
491     case TextAreaPart:
492         return paintTextAreaDecorations(box, paintInfo, devicePixelSnappedRect);
493     case CheckboxPart:
494         return paintCheckboxDecorations(box, paintInfo, integralSnappedRect);
495     case RadioPart:
496         return paintRadioDecorations(box, paintInfo, integralSnappedRect);
497     case PushButtonPart:
498         return paintPushButtonDecorations(box, paintInfo, integralSnappedRect);
499     case SquareButtonPart:
500         return paintSquareButtonDecorations(box, paintInfo, integralSnappedRect);
501     case ButtonPart:
502         return paintButtonDecorations(box, paintInfo, integralSnappedRect);
503     case MenulistPart:
504         return paintMenuListDecorations(box, paintInfo, integralSnappedRect);
505     case SliderThumbHorizontalPart:
506     case SliderThumbVerticalPart:
507         return paintSliderThumbDecorations(box, paintInfo, integralSnappedRect);
508     case SearchFieldPart:
509         return paintSearchFieldDecorations(box, paintInfo, integralSnappedRect);
510 #if ENABLE(METER_ELEMENT)
511     case MeterPart:
512     case RelevancyLevelIndicatorPart:
513     case ContinuousCapacityLevelIndicatorPart:
514     case DiscreteCapacityLevelIndicatorPart:
515     case RatingLevelIndicatorPart:
516 #endif
517     case ProgressBarPart:
518     case SliderHorizontalPart:
519     case SliderVerticalPart:
520     case ListboxPart:
521     case DefaultButtonPart:
522     case SearchFieldCancelButtonPart:
523     case SearchFieldDecorationPart:
524     case SearchFieldResultsDecorationPart:
525     case SearchFieldResultsButtonPart:
526 #if ENABLE(SERVICE_CONTROLS)
527     case ImageControlsButtonPart:
528 #endif
529     default:
530         break;
531     }
532
533     return false;
534 }
535
536 #if ENABLE(VIDEO)
537
538 String RenderTheme::formatMediaControlsTime(float time) const
539 {
540     if (!std::isfinite(time))
541         time = 0;
542     int seconds = (int)fabsf(time);
543     int hours = seconds / (60 * 60);
544     int minutes = (seconds / 60) % 60;
545     seconds %= 60;
546     if (hours) {
547         if (hours > 9)
548             return String::format("%s%02d:%02d:%02d", (time < 0 ? "-" : ""), hours, minutes, seconds);
549
550         return String::format("%s%01d:%02d:%02d", (time < 0 ? "-" : ""), hours, minutes, seconds);
551     }
552
553     return String::format("%s%02d:%02d", (time < 0 ? "-" : ""), minutes, seconds);
554 }
555
556 String RenderTheme::formatMediaControlsCurrentTime(float currentTime, float /*duration*/) const
557 {
558     return formatMediaControlsTime(currentTime);
559 }
560
561 String RenderTheme::formatMediaControlsRemainingTime(float currentTime, float duration) const
562 {
563     return formatMediaControlsTime(currentTime - duration);
564 }
565
566 LayoutPoint RenderTheme::volumeSliderOffsetFromMuteButton(const RenderBox& muteButtonBox, const LayoutSize& size) const
567 {
568     LayoutUnit y = -size.height();
569     FloatPoint absPoint = muteButtonBox.localToAbsolute(FloatPoint(muteButtonBox.offsetLeft(), y), IsFixed | UseTransforms);
570     if (absPoint.y() < 0)
571         y = muteButtonBox.height();
572     return LayoutPoint(0, y);
573 }
574
575 #endif
576
577 Color RenderTheme::activeSelectionBackgroundColor() const
578 {
579     if (!m_activeSelectionBackgroundColor.isValid())
580         m_activeSelectionBackgroundColor = platformActiveSelectionBackgroundColor().blendWithWhite();
581     return m_activeSelectionBackgroundColor;
582 }
583
584 Color RenderTheme::inactiveSelectionBackgroundColor() const
585 {
586     if (!m_inactiveSelectionBackgroundColor.isValid())
587         m_inactiveSelectionBackgroundColor = platformInactiveSelectionBackgroundColor().blendWithWhite();
588     return m_inactiveSelectionBackgroundColor;
589 }
590
591 Color RenderTheme::activeSelectionForegroundColor() const
592 {
593     if (!m_activeSelectionForegroundColor.isValid() && supportsSelectionForegroundColors())
594         m_activeSelectionForegroundColor = platformActiveSelectionForegroundColor();
595     return m_activeSelectionForegroundColor;
596 }
597
598 Color RenderTheme::inactiveSelectionForegroundColor() const
599 {
600     if (!m_inactiveSelectionForegroundColor.isValid() && supportsSelectionForegroundColors())
601         m_inactiveSelectionForegroundColor = platformInactiveSelectionForegroundColor();
602     return m_inactiveSelectionForegroundColor;
603 }
604
605 Color RenderTheme::activeListBoxSelectionBackgroundColor() const
606 {
607     if (!m_activeListBoxSelectionBackgroundColor.isValid())
608         m_activeListBoxSelectionBackgroundColor = platformActiveListBoxSelectionBackgroundColor();
609     return m_activeListBoxSelectionBackgroundColor;
610 }
611
612 Color RenderTheme::inactiveListBoxSelectionBackgroundColor() const
613 {
614     if (!m_inactiveListBoxSelectionBackgroundColor.isValid())
615         m_inactiveListBoxSelectionBackgroundColor = platformInactiveListBoxSelectionBackgroundColor();
616     return m_inactiveListBoxSelectionBackgroundColor;
617 }
618
619 Color RenderTheme::activeListBoxSelectionForegroundColor() const
620 {
621     if (!m_activeListBoxSelectionForegroundColor.isValid() && supportsListBoxSelectionForegroundColors())
622         m_activeListBoxSelectionForegroundColor = platformActiveListBoxSelectionForegroundColor();
623     return m_activeListBoxSelectionForegroundColor;
624 }
625
626 Color RenderTheme::inactiveListBoxSelectionForegroundColor() const
627 {
628     if (!m_inactiveListBoxSelectionForegroundColor.isValid() && supportsListBoxSelectionForegroundColors())
629         m_inactiveListBoxSelectionForegroundColor = platformInactiveListBoxSelectionForegroundColor();
630     return m_inactiveListBoxSelectionForegroundColor;
631 }
632
633 Color RenderTheme::platformActiveSelectionBackgroundColor() const
634 {
635     // Use a blue color by default if the platform theme doesn't define anything.
636     return Color(0, 0, 255);
637 }
638
639 Color RenderTheme::platformActiveSelectionForegroundColor() const
640 {
641     // Use a white color by default if the platform theme doesn't define anything.
642     return Color::white;
643 }
644
645 Color RenderTheme::platformInactiveSelectionBackgroundColor() const
646 {
647     // Use a grey color by default if the platform theme doesn't define anything.
648     // This color matches Firefox's inactive color.
649     return Color(176, 176, 176);
650 }
651
652 Color RenderTheme::platformInactiveSelectionForegroundColor() const
653 {
654     // Use a black color by default.
655     return Color::black;
656 }
657
658 Color RenderTheme::platformActiveListBoxSelectionBackgroundColor() const
659 {
660     return platformActiveSelectionBackgroundColor();
661 }
662
663 Color RenderTheme::platformActiveListBoxSelectionForegroundColor() const
664 {
665     return platformActiveSelectionForegroundColor();
666 }
667
668 Color RenderTheme::platformInactiveListBoxSelectionBackgroundColor() const
669 {
670     return platformInactiveSelectionBackgroundColor();
671 }
672
673 Color RenderTheme::platformInactiveListBoxSelectionForegroundColor() const
674 {
675     return platformInactiveSelectionForegroundColor();
676 }
677
678 int RenderTheme::baselinePosition(const RenderBox& box) const
679 {
680 #if USE(NEW_THEME)
681     return box.height() + box.marginTop() + m_theme->baselinePositionAdjustment(box.style().appearance()) * box.style().effectiveZoom();
682 #else
683     return box.height() + box.marginTop();
684 #endif
685 }
686
687 bool RenderTheme::isControlContainer(ControlPart appearance) const
688 {
689     // There are more leaves than this, but we'll patch this function as we add support for
690     // more controls.
691     return appearance != CheckboxPart && appearance != RadioPart;
692 }
693
694 bool RenderTheme::isControlStyled(const RenderStyle& style, const BorderData& border, const FillLayer& background, const Color& backgroundColor) const
695 {
696     switch (style.appearance()) {
697     case PushButtonPart:
698     case SquareButtonPart:
699     case DefaultButtonPart:
700     case ButtonPart:
701     case ListboxPart:
702     case MenulistPart:
703     case ProgressBarPart:
704     case MeterPart:
705     case RelevancyLevelIndicatorPart:
706     case ContinuousCapacityLevelIndicatorPart:
707     case DiscreteCapacityLevelIndicatorPart:
708     case RatingLevelIndicatorPart:
709     // FIXME: SearchFieldPart should be included here when making search fields style-able.
710     case TextFieldPart:
711     case TextAreaPart:
712         // Test the style to see if the UA border and background match.
713         return (style.border() != border
714             || *style.backgroundLayers() != background
715             || !style.backgroundColorEqualsToColorIgnoringVisited(backgroundColor));
716     default:
717         return false;
718     }
719 }
720
721 void RenderTheme::adjustRepaintRect(const RenderObject& renderer, FloatRect& rect)
722 {
723 #if USE(NEW_THEME)
724     ControlStates states(extractControlStatesForRenderer(renderer));
725     m_theme->inflateControlPaintRect(renderer.style().appearance(), states, rect, renderer.style().effectiveZoom());
726 #else
727     UNUSED_PARAM(renderer);
728     UNUSED_PARAM(rect);
729 #endif
730 }
731
732 bool RenderTheme::supportsFocusRing(const RenderStyle& style) const
733 {
734     return (style.hasAppearance() && style.appearance() != TextFieldPart && style.appearance() != TextAreaPart && style.appearance() != MenulistButtonPart && style.appearance() != ListboxPart);
735 }
736
737 bool RenderTheme::stateChanged(const RenderObject& o, ControlStates::States state) const
738 {
739     // Default implementation assumes the controls don't respond to changes in :hover state
740     if (state == ControlStates::HoverState && !supportsHover(o.style()))
741         return false;
742
743     // Assume pressed state is only responded to if the control is enabled.
744     if (state == ControlStates::PressedState && !isEnabled(o))
745         return false;
746
747     // Repaint the control.
748     o.repaint();
749     return true;
750 }
751
752 void RenderTheme::updateControlStatesForRenderer(const RenderBox& box, ControlStates& controlStates) const
753 {
754     ControlStates newStates = extractControlStatesForRenderer(box);
755     controlStates.setStates(newStates.states());
756     if (isFocused(box))
757         controlStates.setTimeSinceControlWasFocused(box.document().page()->focusController().timeSinceFocusWasSet());
758 }
759
760 ControlStates::States RenderTheme::extractControlStatesForRenderer(const RenderObject& o) const
761 {
762     ControlStates::States states = 0;
763     if (isHovered(o)) {
764         states |= ControlStates::HoverState;
765         if (isSpinUpButtonPartHovered(o))
766             states |= ControlStates::SpinUpState;
767     }
768     if (isPressed(o)) {
769         states |= ControlStates::PressedState;
770         if (isSpinUpButtonPartPressed(o))
771             states |= ControlStates::SpinUpState;
772     }
773     if (isFocused(o) && o.style().outlineStyleIsAuto())
774         states |= ControlStates::FocusState;
775     if (isEnabled(o))
776         states |= ControlStates::EnabledState;
777     if (isChecked(o))
778         states |= ControlStates::CheckedState;
779     if (isDefault(o))
780         states |= ControlStates::DefaultState;
781     if (!isActive(o))
782         states |= ControlStates::WindowInactiveState;
783     if (isIndeterminate(o))
784         states |= ControlStates::IndeterminateState;
785     return states;
786 }
787
788 bool RenderTheme::isActive(const RenderObject& o) const
789 {
790     Page* page = o.document().page();
791     if (!page)
792         return false;
793
794     return page->focusController().isActive();
795 }
796
797 bool RenderTheme::isChecked(const RenderObject& o) const
798 {
799     return is<HTMLInputElement>(o.node()) && downcast<HTMLInputElement>(*o.node()).shouldAppearChecked();
800 }
801
802 bool RenderTheme::isIndeterminate(const RenderObject& o) const
803 {
804     return is<HTMLInputElement>(o.node()) && downcast<HTMLInputElement>(*o.node()).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, const 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, const 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, const 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&, const Element*) const
927 {
928 }
929 #endif
930
931 void RenderTheme::adjustTextFieldStyle(StyleResolver&, RenderStyle&, const Element*) const
932 {
933 }
934
935 void RenderTheme::adjustTextAreaStyle(StyleResolver&, RenderStyle&, const Element*) const
936 {
937 }
938
939 void RenderTheme::adjustMenuListStyle(StyleResolver&, RenderStyle&, const Element*) const
940 {
941 }
942
943 #if ENABLE(METER_ELEMENT)
944 void RenderTheme::adjustMeterStyle(StyleResolver&, RenderStyle& style, const 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 #endif
964
965 void RenderTheme::adjustCapsLockIndicatorStyle(StyleResolver&, RenderStyle&, const Element*) const
966 {
967 }
968
969 bool RenderTheme::paintCapsLockIndicator(const RenderObject&, const PaintInfo&, const IntRect&)
970 {
971     return false;
972 }
973
974 #if ENABLE(ATTACHMENT_ELEMENT)
975 void RenderTheme::adjustAttachmentStyle(StyleResolver&, RenderStyle&, const Element*) const
976 {
977 }
978
979 bool RenderTheme::paintAttachment(const RenderObject&, const PaintInfo&, const IntRect&)
980 {
981     return false;
982 }
983 #endif
984
985 #if ENABLE(DATALIST_ELEMENT)
986 LayoutUnit RenderTheme::sliderTickSnappingThreshold() const
987 {
988     return 0;
989 }
990
991 void RenderTheme::paintSliderTicks(const RenderObject& o, const PaintInfo& paintInfo, const IntRect& rect)
992 {
993     if (!is<HTMLInputElement>(o.node()))
994         return;
995
996     auto& input = downcast<HTMLInputElement>(*o.node());
997     auto* dataList = downcast<HTMLDataListElement>(input.list());
998     if (!dataList)
999         return;
1000
1001     double min = input.minimum();
1002     double max = input.maximum();
1003     ControlPart part = o.style().appearance();
1004     // We don't support ticks on alternate sliders like MediaVolumeSliders.
1005     if (part !=  SliderHorizontalPart && part != SliderVerticalPart)
1006         return;
1007     bool isHorizontal = part ==  SliderHorizontalPart;
1008
1009     IntSize thumbSize;
1010     const RenderObject* thumbRenderer = input.sliderThumbElement()->renderer();
1011     if (thumbRenderer) {
1012         const RenderStyle& thumbStyle = thumbRenderer->style();
1013         int thumbWidth = thumbStyle.width().intValue();
1014         int thumbHeight = thumbStyle.height().intValue();
1015         thumbSize.setWidth(isHorizontal ? thumbWidth : thumbHeight);
1016         thumbSize.setHeight(isHorizontal ? thumbHeight : thumbWidth);
1017     }
1018
1019     IntSize tickSize = sliderTickSize();
1020     float zoomFactor = o.style().effectiveZoom();
1021     FloatRect tickRect;
1022     int tickRegionSideMargin = 0;
1023     int tickRegionWidth = 0;
1024     IntRect trackBounds;
1025     RenderObject* trackRenderer = input.sliderTrackElement()->renderer();
1026     // We can ignoring transforms because transform is handled by the graphics context.
1027     if (trackRenderer)
1028         trackBounds = trackRenderer->absoluteBoundingBoxRectIgnoringTransforms();
1029     IntRect sliderBounds = o.absoluteBoundingBoxRectIgnoringTransforms();
1030
1031     // Make position relative to the transformed ancestor element.
1032     trackBounds.setX(trackBounds.x() - sliderBounds.x() + rect.x());
1033     trackBounds.setY(trackBounds.y() - sliderBounds.y() + rect.y());
1034
1035     if (isHorizontal) {
1036         tickRect.setWidth(floor(tickSize.width() * zoomFactor));
1037         tickRect.setHeight(floor(tickSize.height() * zoomFactor));
1038         tickRect.setY(floor(rect.y() + rect.height() / 2.0 + sliderTickOffsetFromTrackCenter() * zoomFactor));
1039         tickRegionSideMargin = trackBounds.x() + (thumbSize.width() - tickSize.width() * zoomFactor) / 2.0;
1040         tickRegionWidth = trackBounds.width() - thumbSize.width();
1041     } else {
1042         tickRect.setWidth(floor(tickSize.height() * zoomFactor));
1043         tickRect.setHeight(floor(tickSize.width() * zoomFactor));
1044         tickRect.setX(floor(rect.x() + rect.width() / 2.0 + sliderTickOffsetFromTrackCenter() * zoomFactor));
1045         tickRegionSideMargin = trackBounds.y() + (thumbSize.width() - tickSize.width() * zoomFactor) / 2.0;
1046         tickRegionWidth = trackBounds.height() - thumbSize.width();
1047     }
1048     Ref<HTMLCollection> options = dataList->options();
1049     GraphicsContextStateSaver stateSaver(paintInfo.context());
1050     paintInfo.context().setFillColor(o.style().visitedDependentColor(CSSPropertyColor));
1051     for (unsigned i = 0; Node* node = options->item(i); i++) {
1052         ASSERT(is<HTMLOptionElement>(*node));
1053         HTMLOptionElement& optionElement = downcast<HTMLOptionElement>(*node);
1054         String value = optionElement.value();
1055         if (!input.isValidValue(value))
1056             continue;
1057         double parsedValue = parseToDoubleForNumberType(input.sanitizeValue(value));
1058         double tickFraction = (parsedValue - min) / (max - min);
1059         double tickRatio = isHorizontal && o.style().isLeftToRightDirection() ? tickFraction : 1.0 - tickFraction;
1060         double tickPosition = round(tickRegionSideMargin + tickRegionWidth * tickRatio);
1061         if (isHorizontal)
1062             tickRect.setX(tickPosition);
1063         else
1064             tickRect.setY(tickPosition);
1065         paintInfo.context().fillRect(tickRect);
1066     }
1067 }
1068 #endif
1069
1070 double RenderTheme::animationRepeatIntervalForProgressBar(RenderProgress&) const
1071 {
1072     return 0;
1073 }
1074
1075 double RenderTheme::animationDurationForProgressBar(RenderProgress&) const
1076 {
1077     return 0;
1078 }
1079
1080 void RenderTheme::adjustProgressBarStyle(StyleResolver&, RenderStyle&, const Element*) const
1081 {
1082 }
1083
1084 IntRect RenderTheme::progressBarRectForBounds(const RenderObject&, const IntRect& bounds) const
1085 {
1086     return bounds;
1087 }
1088
1089 bool RenderTheme::shouldHaveSpinButton(const HTMLInputElement& inputElement) const
1090 {
1091     return inputElement.isSteppable() && !inputElement.isRangeControl();
1092 }
1093
1094 bool RenderTheme::shouldHaveCapsLockIndicator(const HTMLInputElement&) const
1095 {
1096     return false;
1097 }
1098
1099 void RenderTheme::adjustMenuListButtonStyle(StyleResolver&, RenderStyle&, const Element*) const
1100 {
1101 }
1102
1103 void RenderTheme::adjustMediaControlStyle(StyleResolver&, RenderStyle&, const Element*) const
1104 {
1105 }
1106
1107 void RenderTheme::adjustSliderTrackStyle(StyleResolver&, RenderStyle&, const Element*) const
1108 {
1109 }
1110
1111 void RenderTheme::adjustSliderThumbStyle(StyleResolver&, RenderStyle& style, const Element* element) const
1112 {
1113     adjustSliderThumbSize(style, element);
1114 }
1115
1116 void RenderTheme::adjustSliderThumbSize(RenderStyle&, const Element*) const
1117 {
1118 }
1119
1120 void RenderTheme::adjustSearchFieldStyle(StyleResolver&, RenderStyle&, const Element*) const
1121 {
1122 }
1123
1124 void RenderTheme::adjustSearchFieldCancelButtonStyle(StyleResolver&, RenderStyle&, const Element*) const
1125 {
1126 }
1127
1128 void RenderTheme::adjustSearchFieldDecorationPartStyle(StyleResolver&, RenderStyle&, const Element*) const
1129 {
1130 }
1131
1132 void RenderTheme::adjustSearchFieldResultsDecorationPartStyle(StyleResolver&, RenderStyle&, const Element*) const
1133 {
1134 }
1135
1136 void RenderTheme::adjustSearchFieldResultsButtonStyle(StyleResolver&, RenderStyle&, const Element*) const
1137 {
1138 }
1139
1140 void RenderTheme::platformColorsDidChange()
1141 {
1142     m_activeSelectionForegroundColor = Color();
1143     m_inactiveSelectionForegroundColor = Color();
1144     m_activeSelectionBackgroundColor = Color();
1145     m_inactiveSelectionBackgroundColor = Color();
1146
1147     m_activeListBoxSelectionForegroundColor = Color();
1148     m_inactiveListBoxSelectionForegroundColor = Color();
1149     m_activeListBoxSelectionBackgroundColor = Color();
1150     m_inactiveListBoxSelectionForegroundColor = Color();
1151
1152     Page::updateStyleForAllPagesAfterGlobalChangeInEnvironment();
1153 }
1154
1155 FontCascadeDescription& RenderTheme::cachedSystemFontDescription(CSSValueID systemFontID) const
1156 {
1157     static NeverDestroyed<FontCascadeDescription> caption;
1158     static NeverDestroyed<FontCascadeDescription> icon;
1159     static NeverDestroyed<FontCascadeDescription> menu;
1160     static NeverDestroyed<FontCascadeDescription> messageBox;
1161     static NeverDestroyed<FontCascadeDescription> smallCaption;
1162     static NeverDestroyed<FontCascadeDescription> statusBar;
1163     static NeverDestroyed<FontCascadeDescription> webkitMiniControl;
1164     static NeverDestroyed<FontCascadeDescription> webkitSmallControl;
1165     static NeverDestroyed<FontCascadeDescription> webkitControl;
1166     static NeverDestroyed<FontCascadeDescription> defaultDescription;
1167
1168     switch (systemFontID) {
1169     case CSSValueCaption:
1170         return caption;
1171     case CSSValueIcon:
1172         return icon;
1173     case CSSValueMenu:
1174         return menu;
1175     case CSSValueMessageBox:
1176         return messageBox;
1177     case CSSValueSmallCaption:
1178         return smallCaption;
1179     case CSSValueStatusBar:
1180         return statusBar;
1181     case CSSValueWebkitMiniControl:
1182         return webkitMiniControl;
1183     case CSSValueWebkitSmallControl:
1184         return webkitSmallControl;
1185     case CSSValueWebkitControl:
1186         return webkitControl;
1187     case CSSValueNone:
1188         return defaultDescription;
1189     default:
1190         ASSERT_NOT_REACHED();
1191         return defaultDescription;
1192     }
1193 }
1194
1195 void RenderTheme::systemFont(CSSValueID systemFontID, FontCascadeDescription& fontDescription) const
1196 {
1197     fontDescription = cachedSystemFontDescription(systemFontID);
1198     if (fontDescription.isAbsoluteSize())
1199         return;
1200
1201     updateCachedSystemFontDescription(systemFontID, fontDescription);
1202 }
1203
1204 Color RenderTheme::systemColor(CSSValueID cssValueId) const
1205 {
1206     switch (cssValueId) {
1207     case CSSValueActiveborder:
1208         return 0xFFFFFFFF;
1209     case CSSValueActivebuttontext:
1210         return 0xFF000000;
1211     case CSSValueActivecaption:
1212         return 0xFFCCCCCC;
1213     case CSSValueAppworkspace:
1214         return 0xFFFFFFFF;
1215     case CSSValueBackground:
1216         return 0xFF6363CE;
1217     case CSSValueButtonface:
1218         return 0xFFC0C0C0;
1219     case CSSValueButtonhighlight:
1220         return 0xFFDDDDDD;
1221     case CSSValueButtonshadow:
1222         return 0xFF888888;
1223     case CSSValueButtontext:
1224         return 0xFF000000;
1225     case CSSValueCaptiontext:
1226         return 0xFF000000;
1227     case CSSValueGraytext:
1228         return 0xFF808080;
1229     case CSSValueHighlight:
1230         return 0xFFB5D5FF;
1231     case CSSValueHighlighttext:
1232         return 0xFF000000;
1233     case CSSValueInactiveborder:
1234         return 0xFFFFFFFF;
1235     case CSSValueInactivecaption:
1236         return 0xFFFFFFFF;
1237     case CSSValueInactivecaptiontext:
1238         return 0xFF7F7F7F;
1239     case CSSValueInfobackground:
1240         return 0xFFFBFCC5;
1241     case CSSValueInfotext:
1242         return 0xFF000000;
1243     case CSSValueMenu:
1244         return 0xFFC0C0C0;
1245     case CSSValueMenutext:
1246         return 0xFF000000;
1247     case CSSValueScrollbar:
1248         return 0xFFFFFFFF;
1249     case CSSValueText:
1250         return 0xFF000000;
1251     case CSSValueThreeddarkshadow:
1252         return 0xFF666666;
1253     case CSSValueThreedface:
1254         return 0xFFC0C0C0;
1255     case CSSValueThreedhighlight:
1256         return 0xFFDDDDDD;
1257     case CSSValueThreedlightshadow:
1258         return 0xFFC0C0C0;
1259     case CSSValueThreedshadow:
1260         return 0xFF888888;
1261     case CSSValueWindow:
1262         return 0xFFFFFFFF;
1263     case CSSValueWindowframe:
1264         return 0xFFCCCCCC;
1265     case CSSValueWindowtext:
1266         return 0xFF000000;
1267     default:
1268         break;
1269     }
1270     return Color();
1271 }
1272
1273 Color RenderTheme::platformActiveTextSearchHighlightColor() const
1274 {
1275     return Color(255, 150, 50); // Orange.
1276 }
1277
1278 Color RenderTheme::platformInactiveTextSearchHighlightColor() const
1279 {
1280     return Color(255, 255, 0); // Yellow.
1281 }
1282
1283 #if ENABLE(TOUCH_EVENTS)
1284 Color RenderTheme::tapHighlightColor()
1285 {
1286     return defaultTheme()->platformTapHighlightColor();
1287 }
1288 #endif
1289
1290 // Value chosen by observation. This can be tweaked.
1291 static const int minColorContrastValue = 1300;
1292 // For transparent or translucent background color, use lightening.
1293 static const int minDisabledColorAlphaValue = 128;
1294
1295 Color RenderTheme::disabledTextColor(const Color& textColor, const Color& backgroundColor) const
1296 {
1297     // The explicit check for black is an optimization for the 99% case (black on white).
1298     // This also means that black on black will turn into grey on black when disabled.
1299     Color disabledColor;
1300     if (textColor.rgb() == Color::black || backgroundColor.alpha() < minDisabledColorAlphaValue || differenceSquared(textColor, Color::white) > differenceSquared(backgroundColor, Color::white))
1301         disabledColor = textColor.light();
1302     else
1303         disabledColor = textColor.dark();
1304     
1305     // If there's not very much contrast between the disabled color and the background color,
1306     // just leave the text color alone. We don't want to change a good contrast color scheme so that it has really bad contrast.
1307     // If the the contrast was already poor, then it doesn't do any good to change it to a different poor contrast color scheme.
1308     if (differenceSquared(disabledColor, backgroundColor) < minColorContrastValue)
1309         return textColor;
1310     
1311     return disabledColor;
1312 }
1313
1314 void RenderTheme::setCustomFocusRingColor(const Color& c)
1315 {
1316     customFocusRingColor() = c;
1317 }
1318
1319 Color RenderTheme::focusRingColor()
1320 {
1321     return customFocusRingColor().isValid() ? customFocusRingColor() : defaultTheme()->platformFocusRingColor();
1322 }
1323
1324 String RenderTheme::fileListDefaultLabel(bool multipleFilesAllowed) const
1325 {
1326     if (multipleFilesAllowed)
1327         return fileButtonNoFilesSelectedLabel();
1328     return fileButtonNoFileSelectedLabel();
1329 }
1330
1331 String RenderTheme::fileListNameForWidth(const FileList* fileList, const FontCascade& font, int width, bool multipleFilesAllowed) const
1332 {
1333     if (width <= 0)
1334         return String();
1335
1336     String string;
1337     if (fileList->isEmpty())
1338         string = fileListDefaultLabel(multipleFilesAllowed);
1339     else if (fileList->length() == 1)
1340         string = fileList->item(0)->name();
1341     else
1342         return StringTruncator::rightTruncate(multipleFileUploadText(fileList->length()), width, font);
1343
1344     return StringTruncator::centerTruncate(string, width, font);
1345 }
1346
1347 #if ENABLE(TOUCH_EVENTS)
1348 Color RenderTheme::platformTapHighlightColor() const
1349 {
1350     // This color is expected to be drawn on a semi-transparent overlay,
1351     // making it more transparent than its alpha value indicates.
1352     static NeverDestroyed<const Color> defaultTapHighlightColor = Color(0, 0, 0, 102);
1353     return defaultTapHighlightColor;
1354 }
1355 #endif
1356
1357 } // namespace WebCore