4c41fb3c42f9dfcf5ed49fbb0c13e992ce283590
[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 LayoutUnit RenderTheme::baselinePosition(const RenderObject* o) const
617 {
618     if (!o->isBox())
619         return 0;
620
621     const RenderBox* box = toRenderBox(o);
622
623 #if USE(NEW_THEME)
624     return box->height() + box->marginTop() + m_theme->baselinePositionAdjustment(o->style()->appearance()) * o->style()->effectiveZoom();
625 #else
626     return box->height() + box->marginTop();
627 #endif
628 }
629
630 bool RenderTheme::isControlContainer(ControlPart appearance) const
631 {
632     // There are more leaves than this, but we'll patch this function as we add support for
633     // more controls.
634     return appearance != CheckboxPart && appearance != RadioPart;
635 }
636
637 bool RenderTheme::isControlStyled(const RenderStyle* style, const BorderData& border, const FillLayer& background,
638                                   const Color& backgroundColor) const
639 {
640     switch (style->appearance()) {
641         case PushButtonPart:
642         case SquareButtonPart:
643         case DefaultButtonPart:
644         case ButtonPart:
645         case ListboxPart:
646         case MenulistPart:
647         case ProgressBarPart:
648         case MeterPart:
649         case RelevancyLevelIndicatorPart:
650         case ContinuousCapacityLevelIndicatorPart:
651         case DiscreteCapacityLevelIndicatorPart:
652         case RatingLevelIndicatorPart:
653         // FIXME: Uncomment this when making search fields style-able.
654         // case SearchFieldPart:
655         case TextFieldPart:
656         case TextAreaPart:
657             // Test the style to see if the UA border and background match.
658             return (style->border() != border ||
659                     *style->backgroundLayers() != background ||
660                     style->visitedDependentColor(CSSPropertyBackgroundColor) != backgroundColor);
661         default:
662             return false;
663     }
664 }
665
666 void RenderTheme::adjustRepaintRect(const RenderObject* o, IntRect& r)
667 {
668 #if USE(NEW_THEME)
669     m_theme->inflateControlPaintRect(o->style()->appearance(), controlStatesForRenderer(o), r, o->style()->effectiveZoom());
670 #endif
671 }
672
673 bool RenderTheme::supportsFocusRing(const RenderStyle* style) const
674 {
675     return (style->hasAppearance() && style->appearance() != TextFieldPart && style->appearance() != TextAreaPart && style->appearance() != MenulistButtonPart && style->appearance() != ListboxPart);
676 }
677
678 bool RenderTheme::stateChanged(RenderObject* o, ControlState state) const
679 {
680     // Default implementation assumes the controls don't respond to changes in :hover state
681     if (state == HoverState && !supportsHover(o->style()))
682         return false;
683
684     // Assume pressed state is only responded to if the control is enabled.
685     if (state == PressedState && !isEnabled(o))
686         return false;
687
688     // Repaint the control.
689     o->repaint();
690     return true;
691 }
692
693 ControlStates RenderTheme::controlStatesForRenderer(const RenderObject* o) const
694 {
695     ControlStates result = 0;
696     if (isHovered(o)) {
697         result |= HoverState;
698         if (isSpinUpButtonPartHovered(o))
699             result |= SpinUpState;
700     }
701     if (isPressed(o)) {
702         result |= PressedState;
703         if (isSpinUpButtonPartPressed(o))
704             result |= SpinUpState;
705     }
706     if (isFocused(o) && o->style()->outlineStyleIsAuto())
707         result |= FocusState;
708     if (isEnabled(o))
709         result |= EnabledState;
710     if (isChecked(o))
711         result |= CheckedState;
712     if (isReadOnlyControl(o))
713         result |= ReadOnlyState;
714     if (isDefault(o))
715         result |= DefaultState;
716     if (!isActive(o))
717         result |= WindowInactiveState;
718     if (isIndeterminate(o))
719         result |= IndeterminateState;
720     return result;
721 }
722
723 bool RenderTheme::isActive(const RenderObject* o) const
724 {
725     Node* node = o->node();
726     if (!node)
727         return false;
728
729     Frame* frame = node->document()->frame();
730     if (!frame)
731         return false;
732
733     Page* page = frame->page();
734     if (!page)
735         return false;
736
737     return page->focusController()->isActive();
738 }
739
740 bool RenderTheme::isChecked(const RenderObject* o) const
741 {
742     if (!o->node())
743         return false;
744
745     HTMLInputElement* inputElement = o->node()->toInputElement();
746     if (!inputElement)
747         return false;
748
749     return inputElement->shouldAppearChecked();
750 }
751
752 bool RenderTheme::isIndeterminate(const RenderObject* o) const
753 {
754     if (!o->node())
755         return false;
756
757     HTMLInputElement* inputElement = o->node()->toInputElement();
758     if (!inputElement)
759         return false;
760
761     return inputElement->isIndeterminate();
762 }
763
764 bool RenderTheme::isEnabled(const RenderObject* o) const
765 {
766     Node* node = o->node();
767     if (!node || !node->isElementNode())
768         return true;
769     return static_cast<Element*>(node)->isEnabledFormControl();
770 }
771
772 bool RenderTheme::isFocused(const RenderObject* o) const
773 {
774     Node* node = o->node();
775     if (!node)
776         return false;
777
778     node = node->focusDelegate();
779     Document* document = node->document();
780     Frame* frame = document->frame();
781     return node == document->focusedNode() && frame && frame->selection()->isFocusedAndActive();
782 }
783
784 bool RenderTheme::isPressed(const RenderObject* o) const
785 {
786     if (!o->node())
787         return false;
788     return o->node()->active();
789 }
790
791 bool RenderTheme::isSpinUpButtonPartPressed(const RenderObject* o) const
792 {
793     Node* node = o->node();
794     if (!node || !node->active() || !node->isElementNode()
795         || !static_cast<Element*>(node)->isSpinButtonElement())
796         return false;
797     SpinButtonElement* element = static_cast<SpinButtonElement*>(node);
798     return element->upDownState() == SpinButtonElement::Up;
799 }
800
801 bool RenderTheme::isReadOnlyControl(const RenderObject* o) const
802 {
803     Node* node = o->node();
804     if (!node || !node->isElementNode())
805         return false;
806     return static_cast<Element*>(node)->isReadOnlyFormControl();
807 }
808
809 bool RenderTheme::isHovered(const RenderObject* o) const
810 {
811     Node* node = o->node();
812     if (!node)
813         return false;
814     if (!node->isElementNode() || !static_cast<Element*>(node)->isSpinButtonElement())
815         return node->hovered();
816     SpinButtonElement* element = static_cast<SpinButtonElement*>(node);
817     return element->hovered() && element->upDownState() != SpinButtonElement::Indeterminate;
818 }
819
820 bool RenderTheme::isSpinUpButtonPartHovered(const RenderObject* o) const
821 {
822     Node* node = o->node();
823     if (!node || !node->isElementNode() || !static_cast<Element*>(node)->isSpinButtonElement())
824         return false;
825     SpinButtonElement* element = static_cast<SpinButtonElement*>(node);
826     return element->upDownState() == SpinButtonElement::Up;
827 }
828
829 bool RenderTheme::isDefault(const RenderObject* o) const
830 {
831     // A button should only have the default appearance if the page is active
832     if (!isActive(o))
833         return false;
834
835     if (!o->document())
836         return false;
837
838     Settings* settings = o->document()->settings();
839     if (!settings || !settings->inApplicationChromeMode())
840         return false;
841     
842     return o->style()->appearance() == DefaultButtonPart;
843 }
844
845 #if !USE(NEW_THEME)
846
847 void RenderTheme::adjustCheckboxStyle(StyleResolver*, RenderStyle* style, Element*) const
848 {
849     // A summary of the rules for checkbox designed to match WinIE:
850     // width/height - honored (WinIE actually scales its control for small widths, but lets it overflow for small heights.)
851     // font-size - not honored (control has no text), but we use it to decide which control size to use.
852     setCheckboxSize(style);
853
854     // padding - not honored by WinIE, needs to be removed.
855     style->resetPadding();
856
857     // border - honored by WinIE, but looks terrible (just paints in the control box and turns off the Windows XP theme)
858     // for now, we will not honor it.
859     style->resetBorder();
860
861     style->setBoxShadow(nullptr);
862 }
863
864 void RenderTheme::adjustRadioStyle(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     setRadioSize(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::adjustButtonStyle(StyleResolver*, RenderStyle* style, Element*) const
882 {
883     // Most platforms will completely honor all CSS, and so we have no need to adjust the style
884     // at all by default.  We will still allow the theme a crack at setting up a desired vertical size.
885     setButtonSize(style);
886 }
887
888 void RenderTheme::adjustInnerSpinButtonStyle(StyleResolver*, RenderStyle*, Element*) const
889 {
890 }
891 #endif
892
893 void RenderTheme::adjustTextFieldStyle(StyleResolver*, RenderStyle*, Element*) const
894 {
895 }
896
897 void RenderTheme::adjustTextAreaStyle(StyleResolver*, RenderStyle*, Element*) const
898 {
899 }
900
901 void RenderTheme::adjustMenuListStyle(StyleResolver*, RenderStyle*, Element*) const
902 {
903 }
904
905 #if ENABLE(INPUT_SPEECH)
906 void RenderTheme::adjustInputFieldSpeechButtonStyle(StyleResolver* styleResolver, RenderStyle* style, Element* element) const
907 {
908     RenderInputSpeech::adjustInputFieldSpeechButtonStyle(styleResolver, style, element);
909 }
910
911 bool RenderTheme::paintInputFieldSpeechButton(RenderObject* object, const PaintInfo& paintInfo, const IntRect& rect)
912 {
913     return RenderInputSpeech::paintInputFieldSpeechButton(object, paintInfo, rect);
914 }
915 #endif
916
917 #if ENABLE(METER_TAG)
918 void RenderTheme::adjustMeterStyle(StyleResolver*, RenderStyle* style, Element*) const
919 {
920     style->setBoxShadow(nullptr);
921 }
922
923 IntSize RenderTheme::meterSizeForBounds(const RenderMeter*, const IntRect& bounds) const
924 {
925     return bounds.size();
926 }
927
928 bool RenderTheme::supportsMeter(ControlPart) const
929 {
930     return false;
931 }
932
933 bool RenderTheme::paintMeter(RenderObject*, const PaintInfo&, const IntRect&)
934 {
935     return true;
936 }
937
938 #endif
939
940 #if ENABLE(PROGRESS_TAG)
941 double RenderTheme::animationRepeatIntervalForProgressBar(RenderProgress*) const
942 {
943     return 0;
944 }
945
946 double RenderTheme::animationDurationForProgressBar(RenderProgress*) const
947 {
948     return 0;
949 }
950
951 void RenderTheme::adjustProgressBarStyle(StyleResolver*, RenderStyle*, Element*) const
952 {
953 }
954 #endif
955
956 bool RenderTheme::shouldHaveSpinButton(HTMLInputElement* inputElement) const
957 {
958     return inputElement->isSteppable() && !inputElement->isRangeControl();
959 }
960
961 void RenderTheme::adjustMenuListButtonStyle(StyleResolver*, RenderStyle*, Element*) const
962 {
963 }
964
965 void RenderTheme::adjustSliderTrackStyle(StyleResolver*, RenderStyle*, Element*) const
966 {
967 }
968
969 void RenderTheme::adjustSliderThumbStyle(StyleResolver*, RenderStyle* style, Element*) const
970 {
971     adjustSliderThumbSize(style);
972 }
973
974 void RenderTheme::adjustSliderThumbSize(RenderStyle*) const
975 {
976 }
977
978 void RenderTheme::adjustSearchFieldStyle(StyleResolver*, RenderStyle*, Element*) const
979 {
980 }
981
982 void RenderTheme::adjustSearchFieldCancelButtonStyle(StyleResolver*, RenderStyle*, Element*) const
983 {
984 }
985
986 void RenderTheme::adjustSearchFieldDecorationStyle(StyleResolver*, RenderStyle*, Element*) const
987 {
988 }
989
990 void RenderTheme::adjustSearchFieldResultsDecorationStyle(StyleResolver*, RenderStyle*, Element*) const
991 {
992 }
993
994 void RenderTheme::adjustSearchFieldResultsButtonStyle(StyleResolver*, RenderStyle*, Element*) const
995 {
996 }
997
998 void RenderTheme::platformColorsDidChange()
999 {
1000     m_activeSelectionForegroundColor = Color();
1001     m_inactiveSelectionForegroundColor = Color();
1002     m_activeSelectionBackgroundColor = Color();
1003     m_inactiveSelectionBackgroundColor = Color();
1004
1005     m_activeListBoxSelectionForegroundColor = Color();
1006     m_inactiveListBoxSelectionForegroundColor = Color();
1007     m_activeListBoxSelectionBackgroundColor = Color();
1008     m_inactiveListBoxSelectionForegroundColor = Color();
1009
1010     Page::scheduleForcedStyleRecalcForAllPages();
1011 }
1012
1013 Color RenderTheme::systemColor(int cssValueId) const
1014 {
1015     switch (cssValueId) {
1016         case CSSValueActiveborder:
1017             return 0xFFFFFFFF;
1018         case CSSValueActivecaption:
1019             return 0xFFCCCCCC;
1020         case CSSValueAppworkspace:
1021             return 0xFFFFFFFF;
1022         case CSSValueBackground:
1023             return 0xFF6363CE;
1024         case CSSValueButtonface:
1025             return 0xFFC0C0C0;
1026         case CSSValueButtonhighlight:
1027             return 0xFFDDDDDD;
1028         case CSSValueButtonshadow:
1029             return 0xFF888888;
1030         case CSSValueButtontext:
1031             return 0xFF000000;
1032         case CSSValueCaptiontext:
1033             return 0xFF000000;
1034         case CSSValueGraytext:
1035             return 0xFF808080;
1036         case CSSValueHighlight:
1037             return 0xFFB5D5FF;
1038         case CSSValueHighlighttext:
1039             return 0xFF000000;
1040         case CSSValueInactiveborder:
1041             return 0xFFFFFFFF;
1042         case CSSValueInactivecaption:
1043             return 0xFFFFFFFF;
1044         case CSSValueInactivecaptiontext:
1045             return 0xFF7F7F7F;
1046         case CSSValueInfobackground:
1047             return 0xFFFBFCC5;
1048         case CSSValueInfotext:
1049             return 0xFF000000;
1050         case CSSValueMenu:
1051             return 0xFFC0C0C0;
1052         case CSSValueMenutext:
1053             return 0xFF000000;
1054         case CSSValueScrollbar:
1055             return 0xFFFFFFFF;
1056         case CSSValueText:
1057             return 0xFF000000;
1058         case CSSValueThreeddarkshadow:
1059             return 0xFF666666;
1060         case CSSValueThreedface:
1061             return 0xFFC0C0C0;
1062         case CSSValueThreedhighlight:
1063             return 0xFFDDDDDD;
1064         case CSSValueThreedlightshadow:
1065             return 0xFFC0C0C0;
1066         case CSSValueThreedshadow:
1067             return 0xFF888888;
1068         case CSSValueWindow:
1069             return 0xFFFFFFFF;
1070         case CSSValueWindowframe:
1071             return 0xFFCCCCCC;
1072         case CSSValueWindowtext:
1073             return 0xFF000000;
1074     }
1075     return Color();
1076 }
1077
1078 Color RenderTheme::platformActiveTextSearchHighlightColor() const
1079 {
1080     return Color(255, 150, 50); // Orange.
1081 }
1082
1083 Color RenderTheme::platformInactiveTextSearchHighlightColor() const
1084 {
1085     return Color(255, 255, 0); // Yellow.
1086 }
1087
1088 #if ENABLE(TOUCH_EVENTS)
1089 Color RenderTheme::tapHighlightColor()
1090 {
1091     return defaultTheme()->platformTapHighlightColor();
1092 }
1093 #endif
1094
1095 // Value chosen by observation. This can be tweaked.
1096 static const int minColorContrastValue = 1300;
1097 // For transparent or translucent background color, use lightening.
1098 static const int minDisabledColorAlphaValue = 128;
1099
1100 Color RenderTheme::disabledTextColor(const Color& textColor, const Color& backgroundColor) const
1101 {
1102     // The explicit check for black is an optimization for the 99% case (black on white).
1103     // This also means that black on black will turn into grey on black when disabled.
1104     Color disabledColor;
1105     if (textColor.rgb() == Color::black || backgroundColor.alpha() < minDisabledColorAlphaValue || differenceSquared(textColor, Color::white) > differenceSquared(backgroundColor, Color::white))
1106         disabledColor = textColor.light();
1107     else
1108         disabledColor = textColor.dark();
1109     
1110     // If there's not very much contrast between the disabled color and the background color,
1111     // just leave the text color alone. We don't want to change a good contrast color scheme so that it has really bad contrast.
1112     // If the the contrast was already poor, then it doesn't do any good to change it to a different poor contrast color scheme.
1113     if (differenceSquared(disabledColor, backgroundColor) < minColorContrastValue)
1114         return textColor;
1115     
1116     return disabledColor;
1117 }
1118
1119 void RenderTheme::setCustomFocusRingColor(const Color& c)
1120 {
1121     customFocusRingColor() = c;
1122 }
1123
1124 Color RenderTheme::focusRingColor()
1125 {
1126     return customFocusRingColor().isValid() ? customFocusRingColor() : defaultTheme()->platformFocusRingColor();
1127 }
1128
1129 String RenderTheme::fileListDefaultLabel(bool multipleFilesAllowed) const
1130 {
1131     if (multipleFilesAllowed)
1132         return fileButtonNoFilesSelectedLabel();
1133     return fileButtonNoFileSelectedLabel();
1134 }
1135
1136 String RenderTheme::fileListNameForWidth(const FileList* fileList, const Font& font, int width, bool multipleFilesAllowed) const
1137 {
1138     if (width <= 0)
1139         return String();
1140
1141     String string;
1142     if (fileList->isEmpty())
1143         string = fileListDefaultLabel(multipleFilesAllowed);
1144     else if (fileList->length() == 1)
1145         string = fileList->item(0)->name();
1146     else
1147         return StringTruncator::rightTruncate(multipleFileUploadText(fileList->length()), width, font, StringTruncator::EnableRoundingHacks);
1148
1149     return StringTruncator::centerTruncate(string, width, font, StringTruncator::EnableRoundingHacks);
1150 }
1151
1152 } // namespace WebCore