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