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
23#ifndef RenderTheme_h
24#define RenderTheme_h
25
26#include "core/platform/ScrollTypes.h"
27#if USE(NEW_THEME)
28#include "core/platform/Theme.h"
29#else
30#include "core/platform/ThemeTypes.h"
31#endif
32#include "core/rendering/RenderObject.h"
33#include "core/rendering/style/CachedUAStyle.h"
34#include "wtf/PassRefPtr.h"
35#include "wtf/RefCounted.h"
36#include "wtf/text/WTFString.h"
37
38namespace WebCore {
39
40class CSSStyleSheet;
41class Element;
42class FileList;
43class HTMLInputElement;
44class PopupMenu;
45class RenderMenuList;
46class RenderMeter;
47class RenderProgress;
48
49
50class RenderTheme : public RefCounted<RenderTheme> {
51protected:
52    RenderTheme();
53
54public:
55    virtual ~RenderTheme() { }
56
57    // This function is to be implemented in your platform-specific theme implementation to hand back the
58    // appropriate platform theme. When the theme is needed in non-page dependent code, a default theme is
59    // used as fallback, which is returned for a nulled page, so the platform code needs to account for this.
60    static PassRefPtr<RenderTheme> themeForPage(Page* page);
61
62    // When the theme is needed in non-page dependent code, the defaultTheme() is used as fallback.
63    static inline PassRefPtr<RenderTheme> defaultTheme()
64    {
65        return themeForPage(0);
66    };
67
68    static void setSizeIfAuto(RenderStyle*, const IntSize&);
69
70    // This method is called whenever style has been computed for an element and the appearance
71    // property has been set to a value other than "none".  The theme should map in all of the appropriate
72    // metrics and defaults given the contents of the style.  This includes sophisticated operations like
73    // selection of control size based off the font, the disabling of appearance when certain other properties like
74    // "border" are set, or if the appearance is not supported by the theme.
75    void adjustStyle(RenderStyle*, Element*,  const CachedUAStyle&);
76
77    // This method is called to paint the widget as a background of the RenderObject.  A widget's foreground, e.g., the
78    // text of a button, is always rendered by the engine itself.  The boolean return value indicates
79    // whether the CSS border/background should also be painted.
80    bool paint(RenderObject*, const PaintInfo&, const IntRect&);
81    bool paintBorderOnly(RenderObject*, const PaintInfo&, const IntRect&);
82    bool paintDecorations(RenderObject*, const PaintInfo&, const IntRect&);
83
84    // The remaining methods should be implemented by the platform-specific portion of the theme, e.g.,
85    // RenderThemeMac.cpp for Mac OS X.
86
87    // These methods return the theme's extra style sheets rules, to let each platform
88    // adjust the default CSS rules in html.css, quirks.css or mediaControls.css.
89    virtual String extraDefaultStyleSheet();
90    virtual String extraQuirksStyleSheet() { return String(); }
91    virtual String extraMediaControlsStyleSheet() { return String(); }
92    virtual String extraFullScreenStyleSheet() { return String(); }
93
94    // A method to obtain the baseline position for a "leaf" control.  This will only be used if a baseline
95    // position cannot be determined by examining child content. Checkboxes and radio buttons are examples of
96    // controls that need to do this.
97    virtual int baselinePosition(const RenderObject*) const;
98
99    // A method for asking if a control is a container or not.  Leaf controls have to have some special behavior (like
100    // the baseline position API above).
101    bool isControlContainer(ControlPart) const;
102
103    // A method asking if the control changes its tint when the window has focus or not.
104    virtual bool controlSupportsTints(const RenderObject*) const { return false; }
105
106    // Whether or not the control has been styled enough by the author to disable the native appearance.
107    virtual bool isControlStyled(const RenderStyle*, const CachedUAStyle&) const;
108
109    // A general method asking if any control tinting is supported at all.
110    virtual bool supportsControlTints() const { return false; }
111
112    // Some controls may spill out of their containers (e.g., the check on an OS X checkbox).  When these controls repaint,
113    // the theme needs to communicate this inflated rect to the engine so that it can invalidate the whole control.
114    virtual void adjustRepaintRect(const RenderObject*, IntRect&);
115
116    // This method is called whenever a relevant state changes on a particular themed object, e.g., the mouse becomes pressed
117    // or a control becomes disabled.
118    virtual bool stateChanged(RenderObject*, ControlState) const;
119
120    // This method is called whenever the theme changes on the system in order to flush cached resources from the
121    // old theme.
122    virtual void themeChanged() { }
123
124    bool shouldDrawDefaultFocusRing(RenderObject*) const;
125
126    // A method asking if the theme's controls actually care about redrawing when hovered.
127    virtual bool supportsHover(const RenderStyle*) const { return false; }
128
129    // A method asking if the platform is able to show datalist suggestions for a given input type.
130    virtual bool supportsDataListUI(const AtomicString&) const;
131
132#if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
133    // A method asking if the platform is able to show a calendar picker for a given input type.
134    virtual bool supportsCalendarPicker(const AtomicString&) const;
135#endif
136
137    // Text selection colors.
138    Color activeSelectionBackgroundColor() const;
139    Color inactiveSelectionBackgroundColor() const;
140    Color activeSelectionForegroundColor() const;
141    Color inactiveSelectionForegroundColor() const;
142
143    // List box selection colors
144    Color activeListBoxSelectionBackgroundColor() const;
145    Color activeListBoxSelectionForegroundColor() const;
146    Color inactiveListBoxSelectionBackgroundColor() const;
147    Color inactiveListBoxSelectionForegroundColor() const;
148
149    // Highlighting colors for TextMatches.
150    virtual Color platformActiveTextSearchHighlightColor() const;
151    virtual Color platformInactiveTextSearchHighlightColor() const;
152
153    static Color focusRingColor();
154    virtual Color platformFocusRingColor() const { return Color(0, 0, 0); }
155    static void setCustomFocusRingColor(const Color&);
156    static Color tapHighlightColor();
157    virtual Color platformTapHighlightColor() const { return RenderTheme::defaultTapHighlightColor; }
158    virtual void platformColorsDidChange();
159
160    virtual double caretBlinkInterval() const { return 0.5; }
161
162    // System fonts and colors for CSS.
163    virtual void systemFont(CSSValueID, FontDescription&) const = 0;
164    virtual Color systemColor(CSSValueID) const;
165
166    virtual int minimumMenuListSize(RenderStyle*) const { return 0; }
167
168    virtual void adjustSliderThumbSize(RenderStyle*, Element*) const;
169
170    virtual int popupInternalPaddingLeft(RenderStyle*) const { return 0; }
171    virtual int popupInternalPaddingRight(RenderStyle*) const { return 0; }
172    virtual int popupInternalPaddingTop(RenderStyle*) const { return 0; }
173    virtual int popupInternalPaddingBottom(RenderStyle*) const { return 0; }
174    virtual bool popupOptionSupportsTextIndent() const { return false; }
175
176    virtual ScrollbarControlSize scrollbarControlSizeForPart(ControlPart) { return RegularScrollbar; }
177
178    // Method for painting the caps lock indicator
179    virtual bool paintCapsLockIndicator(RenderObject*, const PaintInfo&, const IntRect&) { return 0; };
180
181    // Returns the repeat interval of the animation for the progress bar.
182    virtual double animationRepeatIntervalForProgressBar(RenderProgress*) const;
183    // Returns the duration of the animation for the progress bar.
184    virtual double animationDurationForProgressBar(RenderProgress*) const;
185
186    // Media controls
187    virtual bool supportsClosedCaptioning() const { return false; }
188    virtual bool hasOwnDisabledStateHandlingFor(ControlPart) const { return false; }
189    virtual bool usesMediaControlStatusDisplay() { return false; }
190    virtual bool usesMediaControlVolumeSlider() const { return true; }
191    virtual bool usesVerticalVolumeSlider() const { return true; }
192    virtual double mediaControlsFadeInDuration() { return 0.1; }
193    virtual double mediaControlsFadeOutDuration() { return 0.3; }
194    virtual String formatMediaControlsTime(float time) const;
195    virtual String formatMediaControlsCurrentTime(float currentTime, float duration) const;
196
197    virtual IntSize meterSizeForBounds(const RenderMeter*, const IntRect&) const;
198    virtual bool supportsMeter(ControlPart) const;
199
200    // Returns the threshold distance for snapping to a slider tick mark.
201    virtual LayoutUnit sliderTickSnappingThreshold() const;
202    // Returns size of one slider tick mark for a horizontal track.
203    // For vertical tracks we rotate it and use it. i.e. Width is always length along the track.
204    virtual IntSize sliderTickSize() const = 0;
205    // Returns the distance of slider tick origin from the slider track center.
206    virtual int sliderTickOffsetFromTrackCenter() const = 0;
207    void paintSliderTicks(RenderObject*, const PaintInfo&, const IntRect&);
208
209    virtual bool shouldShowPlaceholderWhenFocused() const { return false; }
210    virtual bool shouldHaveSpinButton(HTMLInputElement*) const;
211
212    // Functions for <select> elements.
213    virtual bool delegatesMenuListRendering() const { return false; }
214    virtual bool popsMenuByArrowKeys() const { return false; }
215    virtual bool popsMenuBySpaceOrReturn() const { return false; }
216
217    virtual String fileListDefaultLabel(bool multipleFilesAllowed) const;
218    virtual String fileListNameForWidth(const FileList*, const Font&, int width, bool multipleFilesAllowed) const;
219
220    virtual bool shouldOpenPickerWithF4Key() const;
221
222protected:
223    // The platform selection color.
224    virtual Color platformActiveSelectionBackgroundColor() const;
225    virtual Color platformInactiveSelectionBackgroundColor() const;
226    virtual Color platformActiveSelectionForegroundColor() const;
227    virtual Color platformInactiveSelectionForegroundColor() const;
228
229    virtual Color platformActiveListBoxSelectionBackgroundColor() const;
230    virtual Color platformInactiveListBoxSelectionBackgroundColor() const;
231    virtual Color platformActiveListBoxSelectionForegroundColor() const;
232    virtual Color platformInactiveListBoxSelectionForegroundColor() const;
233
234    // A method asking if the theme is able to draw the focus ring.
235    virtual bool supportsFocusRing(const RenderStyle*) const;
236    virtual bool supportsSelectionForegroundColors() const { return true; }
237    virtual bool supportsListBoxSelectionForegroundColors() const { return true; }
238
239#if !USE(NEW_THEME)
240    // Methods for each appearance value.
241    virtual void adjustCheckboxStyle(RenderStyle*, Element*) const;
242    virtual bool paintCheckbox(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
243    virtual void setCheckboxSize(RenderStyle*) const { }
244
245    virtual void adjustRadioStyle(RenderStyle*, Element*) const;
246    virtual bool paintRadio(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
247    virtual void setRadioSize(RenderStyle*) const { }
248
249    virtual void adjustButtonStyle(RenderStyle*, Element*) const;
250    virtual bool paintButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
251    virtual void setButtonSize(RenderStyle*) const { }
252
253    virtual void adjustInnerSpinButtonStyle(RenderStyle*, Element*) const;
254    virtual bool paintInnerSpinButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
255#endif
256
257    virtual void adjustTextFieldStyle(RenderStyle*, Element*) const;
258    virtual bool paintTextField(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
259
260    virtual void adjustTextAreaStyle(RenderStyle*, Element*) const;
261    virtual bool paintTextArea(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
262
263    virtual void adjustMenuListStyle(RenderStyle*, Element*) const;
264    virtual bool paintMenuList(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
265
266    virtual void adjustMenuListButtonStyle(RenderStyle*, Element*) const;
267    virtual bool paintMenuListButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
268
269    virtual void adjustMeterStyle(RenderStyle*, Element*) const;
270    virtual bool paintMeter(RenderObject*, const PaintInfo&, const IntRect&);
271
272    virtual void adjustProgressBarStyle(RenderStyle*, Element*) const;
273    virtual bool paintProgressBar(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
274
275#if ENABLE(INPUT_SPEECH)
276    virtual void adjustInputFieldSpeechButtonStyle(RenderStyle*, Element*) const;
277    virtual bool paintInputFieldSpeechButton(RenderObject*, const PaintInfo&, const IntRect&);
278#endif
279
280    virtual void adjustSliderTrackStyle(RenderStyle*, Element*) const;
281    virtual bool paintSliderTrack(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
282
283    virtual void adjustSliderThumbStyle(RenderStyle*, Element*) const;
284    virtual bool paintSliderThumb(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
285
286    virtual void adjustSearchFieldStyle(RenderStyle*, Element*) const;
287    virtual bool paintSearchField(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
288
289    virtual void adjustSearchFieldCancelButtonStyle(RenderStyle*, Element*) const;
290    virtual bool paintSearchFieldCancelButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
291
292    virtual void adjustSearchFieldDecorationStyle(RenderStyle*, Element*) const;
293    virtual bool paintSearchFieldDecoration(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
294
295    virtual void adjustSearchFieldResultsDecorationStyle(RenderStyle*, Element*) const;
296    virtual bool paintSearchFieldResultsDecoration(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
297
298    virtual void adjustMediaControlStyle(RenderStyle*, Element*) const;
299    virtual bool paintMediaFullscreenButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
300    virtual bool paintMediaPlayButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
301    virtual bool paintMediaOverlayPlayButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
302    virtual bool paintMediaMuteButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
303    virtual bool paintMediaSeekBackButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
304    virtual bool paintMediaSeekForwardButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
305    virtual bool paintMediaSliderTrack(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
306    virtual bool paintMediaSliderThumb(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
307    virtual bool paintMediaVolumeSliderContainer(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
308    virtual bool paintMediaVolumeSliderTrack(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
309    virtual bool paintMediaVolumeSliderThumb(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
310    virtual bool paintMediaRewindButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
311    virtual bool paintMediaReturnToRealtimeButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
312    virtual bool paintMediaToggleClosedCaptionsButton(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
313    virtual bool paintMediaControlsBackground(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
314    virtual bool paintMediaCurrentTime(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
315    virtual bool paintMediaTimeRemaining(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
316    virtual bool paintMediaFullScreenVolumeSliderTrack(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
317    virtual bool paintMediaFullScreenVolumeSliderThumb(RenderObject*, const PaintInfo&, const IntRect&) { return true; }
318
319    virtual bool shouldUseFallbackTheme(RenderStyle*) const;
320    void adjustStyleUsingFallbackTheme(RenderStyle*, Element*);
321    bool paintUsingFallbackTheme(RenderObject*, const PaintInfo&, const IntRect&);
322    void adjustCheckboxStyleUsingFallbackTheme(RenderStyle*, Element*) const;
323    bool paintCheckboxUsingFallbackTheme(RenderObject*, const PaintInfo&, const IntRect&);
324    void adjustRadioStyleUsingFallbackTheme(RenderStyle*, Element*) const;
325    bool paintRadioUsingFallbackTheme(RenderObject*, const PaintInfo&, const IntRect&);
326
327public:
328    // Methods for state querying
329    ControlStates controlStatesForRenderer(const RenderObject* o) const;
330    bool isActive(const RenderObject*) const;
331    bool isChecked(const RenderObject*) const;
332    bool isIndeterminate(const RenderObject*) const;
333    bool isEnabled(const RenderObject*) const;
334    bool isFocused(const RenderObject*) const;
335    bool isPressed(const RenderObject*) const;
336    bool isSpinUpButtonPartPressed(const RenderObject*) const;
337    bool isHovered(const RenderObject*) const;
338    bool isSpinUpButtonPartHovered(const RenderObject*) const;
339    bool isReadOnlyControl(const RenderObject*) const;
340
341private:
342    mutable Color m_activeSelectionBackgroundColor;
343    mutable Color m_inactiveSelectionBackgroundColor;
344    mutable Color m_activeSelectionForegroundColor;
345    mutable Color m_inactiveSelectionForegroundColor;
346
347    mutable Color m_activeListBoxSelectionBackgroundColor;
348    mutable Color m_inactiveListBoxSelectionBackgroundColor;
349    mutable Color m_activeListBoxSelectionForegroundColor;
350    mutable Color m_inactiveListBoxSelectionForegroundColor;
351
352    mutable unsigned m_selectionColorsValid;
353
354    // This color is expected to be drawn on a semi-transparent overlay,
355    // making it more transparent than its alpha value indicates.
356    static const RGBA32 defaultTapHighlightColor = 0x66000000;
357
358#if USE(NEW_THEME)
359    Theme* m_theme; // The platform-specific theme.
360#endif
361};
362
363} // namespace WebCore
364
365#endif // RenderTheme_h
366