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