2 * This file is part of the theme implementation for form controls in WebCore.
4 * Copyright (C) 2005 Apple Computer, Inc.
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.
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.
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.
23 #include "RenderTheme.h"
25 #include "CSSValueKeywords.h"
28 #include "GraphicsContext.h"
29 #include "HTMLInputElement.h"
30 #include "HTMLNames.h"
31 #include "RenderStyle.h"
33 // The methods in this file are shared by all themes on every platform.
37 using namespace HTMLNames;
39 void RenderTheme::adjustStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e,
40 bool UAHasAppearance, const BorderData& border, const BackgroundLayer& background, const Color& backgroundColor)
42 // Force inline and table display styles to be inline-block (except for table- which is block)
43 if (style->display() == INLINE || style->display() == INLINE_TABLE || style->display() == TABLE_ROW_GROUP ||
44 style->display() == TABLE_HEADER_GROUP || style->display() == TABLE_FOOTER_GROUP ||
45 style->display() == TABLE_ROW || style->display() == TABLE_COLUMN_GROUP || style->display() == TABLE_COLUMN ||
46 style->display() == TABLE_CELL || style->display() == TABLE_CAPTION)
47 style->setDisplay(INLINE_BLOCK);
48 else if (style->display() == COMPACT || style->display() == RUN_IN || style->display() == LIST_ITEM || style->display() == TABLE)
49 style->setDisplay(BLOCK);
51 if (UAHasAppearance && theme()->isControlStyled(style, border, background, backgroundColor)) {
52 if (style->appearance() == MenulistAppearance)
53 style->setAppearance(MenulistButtonAppearance);
55 style->setAppearance(NoAppearance);
58 // Call the appropriate style adjustment method based off the appearance value.
59 switch (style->appearance()) {
60 case CheckboxAppearance:
61 return adjustCheckboxStyle(selector, style, e);
63 return adjustRadioStyle(selector, style, e);
64 case PushButtonAppearance:
65 case SquareButtonAppearance:
66 case ButtonAppearance:
67 return adjustButtonStyle(selector, style, e);
68 case TextFieldAppearance:
69 return adjustTextFieldStyle(selector, style, e);
70 case TextAreaAppearance:
71 return adjustTextAreaStyle(selector, style, e);
72 case MenulistAppearance:
73 return adjustMenuListStyle(selector, style, e);
74 case MenulistButtonAppearance:
75 return adjustMenuListButtonStyle(selector, style, e);
76 case SliderHorizontalAppearance:
77 case SliderVerticalAppearance:
78 return adjustSliderTrackStyle(selector, style, e);
79 case SliderThumbHorizontalAppearance:
80 case SliderThumbVerticalAppearance:
81 return adjustSliderThumbStyle(selector, style, e);
82 case SearchFieldAppearance:
83 return adjustSearchFieldStyle(selector, style, e);
84 case SearchFieldCancelButtonAppearance:
85 return adjustSearchFieldCancelButtonStyle(selector, style, e);
86 case SearchFieldDecorationAppearance:
87 return adjustSearchFieldDecorationStyle(selector, style, e);
88 case SearchFieldResultsDecorationAppearance:
89 return adjustSearchFieldResultsDecorationStyle(selector, style, e);
90 case SearchFieldResultsButtonAppearance:
91 return adjustSearchFieldResultsButtonStyle(selector, style, e);
97 bool RenderTheme::paint(RenderObject* o, const RenderObject::PaintInfo& paintInfo, const IntRect& r)
99 // If painting is disabled, but we aren't updating control tints, then just bail.
100 // If we are updating control tints, just schedule a repaint if the theme supports tinting
102 if (paintInfo.context->updatingControlTints()) {
103 if (controlSupportsTints(o))
107 if (paintInfo.context->paintingDisabled())
110 // Call the appropriate paint method based off the appearance value.
111 switch (o->style()->appearance()) {
112 case CheckboxAppearance:
113 return paintCheckbox(o, paintInfo, r);
114 case RadioAppearance:
115 return paintRadio(o, paintInfo, r);
116 case PushButtonAppearance:
117 case SquareButtonAppearance:
118 case ButtonAppearance:
119 return paintButton(o, paintInfo, r);
120 case MenulistAppearance:
121 return paintMenuList(o, paintInfo, r);
122 case SliderHorizontalAppearance:
123 case SliderVerticalAppearance:
124 return paintSliderTrack(o, paintInfo, r);
125 case SliderThumbHorizontalAppearance:
126 case SliderThumbVerticalAppearance:
127 if (o->parent()->isSlider())
128 return paintSliderThumb(o, paintInfo, r);
129 // We don't support drawing a slider thumb without a parent slider
131 case MenulistButtonAppearance:
132 case TextFieldAppearance:
133 case TextAreaAppearance:
134 case ListboxAppearance:
136 case SearchFieldAppearance:
137 return paintSearchField(o, paintInfo, r);
138 case SearchFieldCancelButtonAppearance:
139 return paintSearchFieldCancelButton(o, paintInfo, r);
140 case SearchFieldDecorationAppearance:
141 return paintSearchFieldDecoration(o, paintInfo, r);
142 case SearchFieldResultsDecorationAppearance:
143 return paintSearchFieldResultsDecoration(o, paintInfo, r);
144 case SearchFieldResultsButtonAppearance:
145 return paintSearchFieldResultsButton(o, paintInfo, r);
150 return true; // We don't support the appearance, so let the normal background/border paint.
153 bool RenderTheme::paintBorderOnly(RenderObject* o, const RenderObject::PaintInfo& paintInfo, const IntRect& r)
155 if (paintInfo.context->paintingDisabled())
158 // Call the appropriate paint method based off the appearance value.
159 switch (o->style()->appearance()) {
160 case TextFieldAppearance:
161 return paintTextField(o, paintInfo, r);
162 case ListboxAppearance:
163 case TextAreaAppearance:
164 return paintTextArea(o, paintInfo, r);
165 case MenulistButtonAppearance:
167 case CheckboxAppearance:
168 case RadioAppearance:
169 case PushButtonAppearance:
170 case SquareButtonAppearance:
171 case ButtonAppearance:
172 case MenulistAppearance:
173 case SliderHorizontalAppearance:
174 case SliderVerticalAppearance:
175 case SliderThumbHorizontalAppearance:
176 case SliderThumbVerticalAppearance:
177 case SearchFieldAppearance:
178 case SearchFieldCancelButtonAppearance:
179 case SearchFieldDecorationAppearance:
180 case SearchFieldResultsDecorationAppearance:
181 case SearchFieldResultsButtonAppearance:
189 bool RenderTheme::paintDecorations(RenderObject* o, const RenderObject::PaintInfo& paintInfo, const IntRect& r)
191 if (paintInfo.context->paintingDisabled())
194 // Call the appropriate paint method based off the appearance value.
195 switch (o->style()->appearance()) {
196 case MenulistButtonAppearance:
197 return paintMenuListButton(o, paintInfo, r);
198 case TextFieldAppearance:
199 case TextAreaAppearance:
200 case ListboxAppearance:
201 case CheckboxAppearance:
202 case RadioAppearance:
203 case PushButtonAppearance:
204 case SquareButtonAppearance:
205 case ButtonAppearance:
206 case MenulistAppearance:
207 case SliderHorizontalAppearance:
208 case SliderVerticalAppearance:
209 case SliderThumbHorizontalAppearance:
210 case SliderThumbVerticalAppearance:
211 case SearchFieldAppearance:
212 case SearchFieldCancelButtonAppearance:
213 case SearchFieldDecorationAppearance:
214 case SearchFieldResultsDecorationAppearance:
215 case SearchFieldResultsButtonAppearance:
223 Color RenderTheme::activeSelectionBackgroundColor() const
225 if (!m_activeSelectionColor.isValid())
226 m_activeSelectionColor = platformActiveSelectionBackgroundColor().blendWithWhite();
227 return m_activeSelectionColor;
230 Color RenderTheme::inactiveSelectionBackgroundColor() const
232 if (!m_inactiveSelectionColor.isValid())
233 m_inactiveSelectionColor = platformInactiveSelectionBackgroundColor().blendWithWhite();
234 return m_inactiveSelectionColor;
237 Color RenderTheme::platformActiveSelectionBackgroundColor() const
239 // Use a blue color by default if the platform theme doesn't define anything.
240 return Color(0, 0, 255);
243 Color RenderTheme::platformInactiveSelectionBackgroundColor() const
245 // Use a grey color by default if the platform theme doesn't define anything.
246 return Color(128, 128, 128);
249 Color RenderTheme::platformActiveSelectionForegroundColor() const
254 Color RenderTheme::platformInactiveSelectionForegroundColor() const
259 Color RenderTheme::activeListBoxSelectionBackgroundColor() const
261 return activeSelectionBackgroundColor();
264 Color RenderTheme::activeListBoxSelectionForegroundColor() const
266 // Use a white color by default if the platform theme doesn't define anything.
267 return Color(255, 255, 255);
270 Color RenderTheme::inactiveListBoxSelectionBackgroundColor() const
272 return inactiveSelectionBackgroundColor();
275 Color RenderTheme::inactiveListBoxSelectionForegroundColor() const
277 // Use a black color by default if the platform theme doesn't define anything.
278 return Color(0, 0, 0);
281 short RenderTheme::baselinePosition(const RenderObject* o) const
283 return o->height() + o->marginTop();
286 bool RenderTheme::isControlContainer(EAppearance appearance) const
288 // There are more leaves than this, but we'll patch this function as we add support for
290 return appearance != CheckboxAppearance && appearance != RadioAppearance;
293 bool RenderTheme::isControlStyled(const RenderStyle* style, const BorderData& border, const BackgroundLayer& background,
294 const Color& backgroundColor) const
296 switch (style->appearance()) {
297 case PushButtonAppearance:
298 case SquareButtonAppearance:
299 case ButtonAppearance:
300 case ListboxAppearance:
301 case MenulistAppearance:
302 // FIXME: Uncomment this when making search fields style-able.
303 // case SearchFieldAppearance:
304 case TextFieldAppearance:
305 case TextAreaAppearance:
306 // Test the style to see if the UA border and background match.
307 return (style->border() != border ||
308 *style->backgroundLayers() != background ||
309 style->backgroundColor() != backgroundColor);
315 bool RenderTheme::supportsFocusRing(const RenderStyle* style) const
317 return (style->hasAppearance() && style->appearance() != TextFieldAppearance && style->appearance() != TextAreaAppearance && style->appearance() != MenulistButtonAppearance && style->appearance() != ListboxAppearance);
320 bool RenderTheme::stateChanged(RenderObject* o, ControlState state) const
322 // Default implementation assumes the controls dont respond to changes in :hover state
323 if (state == HoverState && !supportsHover(o->style()))
326 // Assume pressed state is only responded to if the control is enabled.
327 if (state == PressedState && !isEnabled(o))
330 // Repaint the control.
335 bool RenderTheme::isChecked(const RenderObject* o) const
339 return o->element()->isChecked();
342 bool RenderTheme::isIndeterminate(const RenderObject* o) const
346 return o->element()->isIndeterminate();
349 bool RenderTheme::isEnabled(const RenderObject* o) const
353 return o->element()->isEnabled();
356 bool RenderTheme::isFocused(const RenderObject* o) const
358 Node* node = o->element();
361 Document* document = node->document();
362 Frame* frame = document->frame();
363 return node == document->focusedNode() && frame && frame->isActive();
366 bool RenderTheme::isPressed(const RenderObject* o) const
370 return o->element()->active();
373 bool RenderTheme::isReadOnlyControl(const RenderObject* o) const
377 return o->element()->isReadOnlyControl();
380 bool RenderTheme::isHovered(const RenderObject* o) const
384 return o->element()->hovered();
387 void RenderTheme::adjustCheckboxStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
389 // A summary of the rules for checkbox designed to match WinIE:
390 // width/height - honored (WinIE actually scales its control for small widths, but lets it overflow for small heights.)
391 // font-size - not honored (control has no text), but we use it to decide which control size to use.
392 setCheckboxSize(style);
394 // padding - not honored by WinIE, needs to be removed.
395 style->resetPadding();
397 // border - honored by WinIE, but looks terrible (just paints in the control box and turns off the Windows XP theme)
398 // for now, we will not honor it.
399 style->resetBorder();
401 style->setBoxShadow(0);
404 void RenderTheme::adjustRadioStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
406 // A summary of the rules for checkbox designed to match WinIE:
407 // width/height - honored (WinIE actually scales its control for small widths, but lets it overflow for small heights.)
408 // font-size - not honored (control has no text), but we use it to decide which control size to use.
411 // padding - not honored by WinIE, needs to be removed.
412 style->resetPadding();
414 // border - honored by WinIE, but looks terrible (just paints in the control box and turns off the Windows XP theme)
415 // for now, we will not honor it.
416 style->resetBorder();
418 style->setBoxShadow(0);
421 void RenderTheme::adjustButtonStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
423 // Most platforms will completely honor all CSS, and so we have no need to adjust the style
424 // at all by default. We will still allow the theme a crack at setting up a desired vertical size.
425 setButtonSize(style);
428 void RenderTheme::adjustTextFieldStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
432 void RenderTheme::adjustTextAreaStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
436 void RenderTheme::adjustMenuListStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
440 void RenderTheme::adjustMenuListButtonStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
444 void RenderTheme::adjustSliderTrackStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
448 void RenderTheme::adjustSliderThumbStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
452 void RenderTheme::adjustSliderThumbSize(RenderObject*) const
456 void RenderTheme::adjustSearchFieldStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
460 void RenderTheme::adjustSearchFieldCancelButtonStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
464 void RenderTheme::adjustSearchFieldDecorationStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
468 void RenderTheme::adjustSearchFieldResultsDecorationStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
472 void RenderTheme::adjustSearchFieldResultsButtonStyle(CSSStyleSelector* selector, RenderStyle* style, Element* e) const
476 void RenderTheme::platformColorsDidChange()
478 m_activeSelectionColor = Color();
479 m_inactiveSelectionColor = Color();
482 Color RenderTheme::systemColor(int cssValueId) const
484 switch (cssValueId) {
485 case CSS_VAL_ACTIVEBORDER:
487 case CSS_VAL_ACTIVECAPTION:
489 case CSS_VAL_APPWORKSPACE:
491 case CSS_VAL_BACKGROUND:
493 case CSS_VAL_BUTTONFACE:
495 case CSS_VAL_BUTTONHIGHLIGHT:
497 case CSS_VAL_BUTTONSHADOW:
499 case CSS_VAL_BUTTONTEXT:
501 case CSS_VAL_CAPTIONTEXT:
503 case CSS_VAL_GRAYTEXT:
505 case CSS_VAL_HIGHLIGHT:
507 case CSS_VAL_HIGHLIGHTTEXT:
509 case CSS_VAL_INACTIVEBORDER:
511 case CSS_VAL_INACTIVECAPTION:
513 case CSS_VAL_INACTIVECAPTIONTEXT:
515 case CSS_VAL_INFOBACKGROUND:
517 case CSS_VAL_INFOTEXT:
521 case CSS_VAL_MENUTEXT:
523 case CSS_VAL_SCROLLBAR:
527 case CSS_VAL_THREEDDARKSHADOW:
529 case CSS_VAL_THREEDFACE:
531 case CSS_VAL_THREEDHIGHLIGHT:
533 case CSS_VAL_THREEDLIGHTSHADOW:
535 case CSS_VAL_THREEDSHADOW:
539 case CSS_VAL_WINDOWFRAME:
541 case CSS_VAL_WINDOWTEXT:
547 Color RenderTheme::platformTextSearchHighlightColor() const
549 return Color(255, 255, 0);
552 } // namespace WebCore