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