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