summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/core/paint/ThemePainterDefault.cpp
blob: 7191690bf02b5491a83dfc4a43c5fde14f43c4bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 * Copyright (C) 2007 Apple Inc.
 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
 * Copyright (C) 2008 Collabora Ltd.
 * Copyright (C) 2008, 2009 Google Inc.
 * Copyright (C) 2009 Kenneth Rohde Christiansen
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#include "core/paint/ThemePainterDefault.h"

#include "core/layout/LayoutObject.h"
#include "core/layout/LayoutProgress.h"
#include "core/layout/LayoutTheme.h"
#include "core/paint/MediaControlsPainter.h"
#include "core/paint/PaintInfo.h"
#include "platform/LayoutTestSupport.h"
#include "platform/graphics/Color.h"
#include "platform/graphics/GraphicsContext.h"
#include "platform/graphics/GraphicsContextStateSaver.h"
#include "public/platform/Platform.h"
#include "public/platform/WebRect.h"
#include "public/platform/WebThemeEngine.h"

namespace blink {

namespace {

const unsigned defaultButtonBackgroundColor = 0xffdddddd;

bool useMockTheme()
{
    return LayoutTestSupport::isMockThemeEnabledForTest();
}

WebThemeEngine::State getWebThemeState(const LayoutObject& o)
{
    if (!LayoutTheme::isEnabled(o))
        return WebThemeEngine::StateDisabled;
    if (useMockTheme() && LayoutTheme::isReadOnlyControl(o))
        return WebThemeEngine::StateReadonly;
    if (LayoutTheme::isPressed(o))
        return WebThemeEngine::StatePressed;
    if (useMockTheme() && LayoutTheme::isFocused(o))
        return WebThemeEngine::StateFocused;
    if (LayoutTheme::isHovered(o))
        return WebThemeEngine::StateHover;

    return WebThemeEngine::StateNormal;
}

class DirectionFlippingScope {
public:
    DirectionFlippingScope(const LayoutObject&, const PaintInfo&, const IntRect&);
    ~DirectionFlippingScope();

private:
    bool m_needsFlipping;
    const PaintInfo& m_paintInfo;
};

DirectionFlippingScope::DirectionFlippingScope(const LayoutObject& layoutObject, const PaintInfo& paintInfo, const IntRect& rect)
    : m_needsFlipping(!layoutObject.styleRef().isLeftToRightDirection())
    , m_paintInfo(paintInfo)
{
    if (!m_needsFlipping)
        return;
    m_paintInfo.context.save();
    m_paintInfo.context.translate(2 * rect.x() + rect.width(), 0);
    m_paintInfo.context.scale(-1, 1);
}

DirectionFlippingScope::~DirectionFlippingScope()
{
    if (!m_needsFlipping)
        return;
    m_paintInfo.context.restore();
}

IntRect determinateProgressValueRectFor(const LayoutProgress& layoutProgress, const IntRect& rect)
{
    int dx = rect.width() * layoutProgress.position();
    return IntRect(rect.x(), rect.y(), dx, rect.height());
}

IntRect indeterminateProgressValueRectFor(const LayoutProgress& layoutProgress, const IntRect& rect)
{
    // Value comes from default of GTK+.
    static const int progressActivityBlocks = 5;

    int valueWidth = rect.width() / progressActivityBlocks;
    int movableWidth = rect.width() - valueWidth;
    if (movableWidth <= 0)
        return IntRect();

    double progress = layoutProgress.animationProgress();
    if (progress < 0.5)
        return IntRect(rect.x() + progress * 2 * movableWidth, rect.y(), valueWidth, rect.height());
    return IntRect(rect.x() + (1.0 - progress) * 2 * movableWidth, rect.y(), valueWidth, rect.height());
}

IntRect progressValueRectFor(const LayoutProgress& layoutProgress, const IntRect& rect)
{
    return layoutProgress.isDeterminate() ? determinateProgressValueRectFor(layoutProgress, rect) : indeterminateProgressValueRectFor(layoutProgress, rect);
}

IntRect convertToPaintingRect(const LayoutObject& inputLayoutObject, const LayoutObject& partLayoutObject, LayoutRect partRect, const IntRect& localOffset)
{
    // Compute an offset between the partLayoutObject and the inputLayoutObject.
    LayoutSize offsetFromInputLayoutObject = -partLayoutObject.offsetFromAncestorContainer(&inputLayoutObject);
    // Move the rect into partLayoutObject's coords.
    partRect.move(offsetFromInputLayoutObject);
    // Account for the local drawing offset.
    partRect.move(localOffset.x(), localOffset.y());

    return pixelSnappedIntRect(partRect);
}

} // namespace

ThemePainterDefault::ThemePainterDefault()
    : ThemePainter(nullptr)
{
}

bool ThemePainterDefault::paintCheckbox(const LayoutObject& o, const PaintInfo& i, const IntRect& rect)
{
    WebThemeEngine::ExtraParams extraParams;
    WebCanvas* canvas = i.context.canvas();
    extraParams.button.checked = LayoutTheme::isChecked(o);
    extraParams.button.indeterminate = LayoutTheme::isIndeterminate(o);

    float zoomLevel = o.styleRef().effectiveZoom();
    GraphicsContextStateSaver stateSaver(i.context, false);
    IntRect unzoomedRect = rect;
    if (zoomLevel != 1) {
        stateSaver.save();
        unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
        unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
        i.context.translate(unzoomedRect.x(), unzoomedRect.y());
        i.context.scale(zoomLevel, zoomLevel);
        i.context.translate(-unzoomedRect.x(), -unzoomedRect.y());
    }

    Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartCheckbox, getWebThemeState(o), WebRect(unzoomedRect), &extraParams);
    return false;
}

bool ThemePainterDefault::paintRadio(const LayoutObject& o, const PaintInfo& i, const IntRect& rect)
{
    WebThemeEngine::ExtraParams extraParams;
    WebCanvas* canvas = i.context.canvas();
    extraParams.button.checked = LayoutTheme::isChecked(o);

    Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartRadio, getWebThemeState(o), WebRect(rect), &extraParams);
    return false;
}

bool ThemePainterDefault::paintButton(const LayoutObject& o, const PaintInfo& i, const IntRect& rect)
{
    WebThemeEngine::ExtraParams extraParams;
    WebCanvas* canvas = i.context.canvas();
    extraParams.button.hasBorder = true;
    extraParams.button.backgroundColor = useMockTheme() ? 0xffc0c0c0 : defaultButtonBackgroundColor;
    if (o.hasBackground())
        extraParams.button.backgroundColor = o.resolveColor(CSSPropertyBackgroundColor).rgb();

    Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartButton, getWebThemeState(o), WebRect(rect), &extraParams);
    return false;
}

bool ThemePainterDefault::paintTextField(const LayoutObject& o, const PaintInfo& i, const IntRect& rect)
{
    // WebThemeEngine does not handle border rounded corner and background image
    // so return true to draw CSS border and background.
    if (o.styleRef().hasBorderRadius() || o.styleRef().hasBackgroundImage())
        return true;

    ControlPart part = o.styleRef().appearance();

    WebThemeEngine::ExtraParams extraParams;
    extraParams.textField.isTextArea = part == TextAreaPart;
    extraParams.textField.isListbox = part == ListboxPart;

    WebCanvas* canvas = i.context.canvas();

    Color backgroundColor = o.resolveColor(CSSPropertyBackgroundColor);
    extraParams.textField.backgroundColor = backgroundColor.rgb();

    Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartTextField, getWebThemeState(o), WebRect(rect), &extraParams);
    return false;
}

bool ThemePainterDefault::paintMenuList(const LayoutObject& o, const PaintInfo& i, const IntRect& rect)
{
    if (!o.isBox())
        return false;

    WebThemeEngine::ExtraParams extraParams;
    const LayoutBox& box = toLayoutBox(o);
    // Match Chromium Win behaviour of showing all borders if any are shown.
    extraParams.menuList.hasBorder = box.borderRight() || box.borderLeft() || box.borderTop() || box.borderBottom();
    extraParams.menuList.hasBorderRadius = o.styleRef().hasBorderRadius();
    // Fallback to transparent if the specified color object is invalid.
    Color backgroundColor(Color::transparent);
    if (o.hasBackground())
        backgroundColor = o.resolveColor(CSSPropertyBackgroundColor);
    extraParams.menuList.backgroundColor = backgroundColor.rgb();

    // If we have a background image, don't fill the content area to expose the
    // parent's background. Also, we shouldn't fill the content area if the
    // alpha of the color is 0. The API of Windows GDI ignores the alpha.
    // FIXME: the normal Aura theme doesn't care about this, so we should
    // investigate if we really need fillContentArea.
    extraParams.menuList.fillContentArea = !o.styleRef().hasBackgroundImage() && backgroundColor.alpha();

    setupMenuListArrow(box, rect, extraParams);

    WebCanvas* canvas = i.context.canvas();
    Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartMenuList, getWebThemeState(o), WebRect(rect), &extraParams);
    return false;
}

bool ThemePainterDefault::paintMenuListButton(const LayoutObject& o, const PaintInfo& i, const IntRect& rect)
{
    if (!o.isBox())
        return false;

    WebThemeEngine::ExtraParams extraParams;
    extraParams.menuList.hasBorder = false;
    extraParams.menuList.hasBorderRadius = o.styleRef().hasBorderRadius();
    extraParams.menuList.backgroundColor = Color::transparent;
    extraParams.menuList.fillContentArea = false;
    setupMenuListArrow(toLayoutBox(o), rect, extraParams);

    WebCanvas* canvas = i.context.canvas();
    Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartMenuList, getWebThemeState(o), WebRect(rect), &extraParams);
    return false;
}

void ThemePainterDefault::setupMenuListArrow(const LayoutBox& box, const IntRect& rect, WebThemeEngine::ExtraParams& extraParams)
{
    const int right = rect.x() + rect.width();
    const int middle = rect.y() + rect.height() / 2;

    extraParams.menuList.arrowY = middle;
    if (useMockTheme()) {
        // The size and position of the drop-down button is different between
        // the mock theme and the regular aura theme.
        int spacingTop = box.borderTop() + box.paddingTop();
        int spacingBottom = box.borderBottom() + box.paddingBottom();
        int spacingRight = box.borderRight() + box.paddingRight();
        extraParams.menuList.arrowX = (box.styleRef().direction() == RTL) ? rect.x() + 4 + spacingRight: right - 10 - spacingRight;
        extraParams.menuList.arrowSize = rect.height() - spacingBottom - spacingTop;
    } else {
        const int arrowSize = 6;
        const int arrowPadding = 7;
        extraParams.menuList.arrowX = (box.styleRef().direction() == RTL)
            ? rect.x() + arrowPadding * box.styleRef().effectiveZoom()
            : right - (arrowSize + arrowPadding) * box.styleRef().effectiveZoom();
        extraParams.menuList.arrowSize = arrowSize * box.styleRef().effectiveZoom();
    }
    extraParams.menuList.arrowColor = box.resolveColor(CSSPropertyColor).rgb();
}

bool ThemePainterDefault::paintSliderTrack(const LayoutObject& o, const PaintInfo& i, const IntRect& rect)
{
    WebThemeEngine::ExtraParams extraParams;
    WebCanvas* canvas = i.context.canvas();
    extraParams.slider.vertical = o.styleRef().appearance() == SliderVerticalPart;

    paintSliderTicks(o, i, rect);

    // FIXME: Mock theme doesn't handle zoomed sliders.
    float zoomLevel = useMockTheme() ? 1 : o.styleRef().effectiveZoom();
    GraphicsContextStateSaver stateSaver(i.context, false);
    IntRect unzoomedRect = rect;
    if (zoomLevel != 1) {
        stateSaver.save();
        unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
        unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
        i.context.translate(unzoomedRect.x(), unzoomedRect.y());
        i.context.scale(zoomLevel, zoomLevel);
        i.context.translate(-unzoomedRect.x(), -unzoomedRect.y());
    }

    Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartSliderTrack, getWebThemeState(o), WebRect(unzoomedRect), &extraParams);

    return false;
}

bool ThemePainterDefault::paintSliderThumb(const LayoutObject& o, const PaintInfo& i, const IntRect& rect)
{
    WebThemeEngine::ExtraParams extraParams;
    WebCanvas* canvas = i.context.canvas();
    extraParams.slider.vertical = o.styleRef().appearance() == SliderThumbVerticalPart;
    extraParams.slider.inDrag = LayoutTheme::isPressed(o);

    // FIXME: Mock theme doesn't handle zoomed sliders.
    float zoomLevel = useMockTheme() ? 1 : o.styleRef().effectiveZoom();
    GraphicsContextStateSaver stateSaver(i.context, false);
    IntRect unzoomedRect = rect;
    if (zoomLevel != 1) {
        stateSaver.save();
        unzoomedRect.setWidth(unzoomedRect.width() / zoomLevel);
        unzoomedRect.setHeight(unzoomedRect.height() / zoomLevel);
        i.context.translate(unzoomedRect.x(), unzoomedRect.y());
        i.context.scale(zoomLevel, zoomLevel);
        i.context.translate(-unzoomedRect.x(), -unzoomedRect.y());
    }

    Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartSliderThumb, getWebThemeState(o), WebRect(unzoomedRect), &extraParams);
    return false;
}

bool ThemePainterDefault::paintInnerSpinButton(const LayoutObject& o, const PaintInfo& i, const IntRect& rect)
{
    WebThemeEngine::ExtraParams extraParams;
    WebCanvas* canvas = i.context.canvas();
    extraParams.innerSpin.spinUp = (LayoutTheme::controlStatesForLayoutObject(o) & SpinUpControlState);
    extraParams.innerSpin.readOnly = LayoutTheme::isReadOnlyControl(o);

    Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartInnerSpinButton, getWebThemeState(o), WebRect(rect), &extraParams);
    return false;
}

bool ThemePainterDefault::paintProgressBar(const LayoutObject& o, const PaintInfo& i, const IntRect& rect)
{
    if (!o.isProgress())
        return true;

    const LayoutProgress& layoutProgress = toLayoutProgress(o);
    IntRect valueRect = progressValueRectFor(layoutProgress, rect);

    WebThemeEngine::ExtraParams extraParams;
    extraParams.progressBar.determinate = layoutProgress.isDeterminate();
    extraParams.progressBar.valueRectX = valueRect.x();
    extraParams.progressBar.valueRectY = valueRect.y();
    extraParams.progressBar.valueRectWidth = valueRect.width();
    extraParams.progressBar.valueRectHeight = valueRect.height();

    DirectionFlippingScope scope(o, i, rect);
    WebCanvas* canvas = i.context.canvas();
    Platform::current()->themeEngine()->paint(canvas, WebThemeEngine::PartProgressBar, getWebThemeState(o), WebRect(rect), &extraParams);
    return false;
}

bool ThemePainterDefault::paintTextArea(const LayoutObject& o, const PaintInfo& i, const IntRect& r)
{
    return paintTextField(o, i, r);
}

bool ThemePainterDefault::paintSearchField(const LayoutObject& o, const PaintInfo& i, const IntRect& r)
{
    return paintTextField(o, i, r);
}

bool ThemePainterDefault::paintSearchFieldCancelButton(const LayoutObject& cancelButtonObject, const PaintInfo& paintInfo, const IntRect& r)
{
    // Get the layoutObject of <input> element.
    if (!cancelButtonObject.node())
        return false;
    Node* input = cancelButtonObject.node()->shadowHost();
    const LayoutObject& baseLayoutObject = input ? *input->layoutObject() : cancelButtonObject;
    if (!baseLayoutObject.isBox())
        return false;
    const LayoutBox& inputLayoutBox = toLayoutBox(baseLayoutObject);
    LayoutRect inputContentBox = inputLayoutBox.contentBoxRect();

    // Make sure the scaled button stays square and will fit in its parent's box.
    LayoutUnit cancelButtonSize = std::min(inputContentBox.width(), std::min(inputContentBox.height(), LayoutUnit(r.height())));
    // Calculate cancel button's coordinates relative to the input element.
    // Center the button vertically.  Round up though, so if it has to be one pixel off-center, it will
    // be one pixel closer to the bottom of the field.  This tends to look better with the text.
    LayoutRect cancelButtonRect(cancelButtonObject.offsetFromAncestorContainer(&inputLayoutBox).width(),
        inputContentBox.y() + (inputContentBox.height() - cancelButtonSize + 1) / 2,
        cancelButtonSize, cancelButtonSize);
    IntRect paintingRect = convertToPaintingRect(inputLayoutBox, cancelButtonObject, cancelButtonRect, r);

    DEFINE_STATIC_REF(Image, cancelImage, (Image::loadPlatformResource("searchCancel")));
    DEFINE_STATIC_REF(Image, cancelPressedImage, (Image::loadPlatformResource("searchCancelPressed")));
    paintInfo.context.drawImage(LayoutTheme::isPressed(cancelButtonObject) ? cancelPressedImage : cancelImage, paintingRect);
    return false;
}

bool ThemePainterDefault::paintSearchFieldResultsDecoration(const LayoutObject& magnifierObject, const PaintInfo& paintInfo, const IntRect& r)
{
    // Get the layoutObject of <input> element.
    if (!magnifierObject.node())
        return false;
    Node* input = magnifierObject.node()->shadowHost();
    const LayoutObject& baseLayoutObject = input ? *input->layoutObject() : magnifierObject;
    if (!baseLayoutObject.isBox())
        return false;
    const LayoutBox& inputLayoutBox = toLayoutBox(baseLayoutObject);
    LayoutRect inputContentBox = inputLayoutBox.contentBoxRect();

    // Make sure the scaled decoration stays square and will fit in its parent's box.
    LayoutUnit magnifierSize = std::min(inputContentBox.width(), std::min(inputContentBox.height(), LayoutUnit(r.height())));
    // Calculate decoration's coordinates relative to the input element.
    // Center the decoration vertically.  Round up though, so if it has to be one pixel off-center, it will
    // be one pixel closer to the bottom of the field.  This tends to look better with the text.
    LayoutRect magnifierRect(magnifierObject.offsetFromAncestorContainer(&inputLayoutBox).width(),
        inputContentBox.y() + (inputContentBox.height() - magnifierSize + 1) / 2,
        magnifierSize, magnifierSize);
    IntRect paintingRect = convertToPaintingRect(inputLayoutBox, magnifierObject, magnifierRect, r);

    DEFINE_STATIC_REF(Image, magnifierImage, (Image::loadPlatformResource("searchMagnifier")));
    paintInfo.context.drawImage(magnifierImage, paintingRect);
    return false;
}

} // namespace blink