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