summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/Source/core/layout/LayoutScrollbar.cpp
blob: 8726d9365842b2dc197d205f62dcf06882482537 (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
/*
 * Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "core/layout/LayoutScrollbar.h"

#include "core/css/PseudoStyleRequest.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/layout/LayoutPart.h"
#include "core/layout/LayoutScrollbarPart.h"
#include "core/layout/LayoutScrollbarTheme.h"
#include "core/layout/LayoutView.h"
#include "platform/graphics/GraphicsContext.h"

namespace blink {

PassRefPtrWillBeRawPtr<Scrollbar> LayoutScrollbar::createCustomScrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, Node* ownerNode, LocalFrame* owningFrame)
{
    return adoptRefWillBeNoop(new LayoutScrollbar(scrollableArea, orientation, ownerNode, owningFrame));
}

LayoutScrollbar::LayoutScrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, Node* ownerNode, LocalFrame* owningFrame)
    : Scrollbar(scrollableArea, orientation, RegularScrollbar, nullptr, LayoutScrollbarTheme::layoutScrollbarTheme())
    , m_owner(ownerNode)
    , m_owningFrame(owningFrame)
{
    ASSERT(ownerNode || owningFrame);

    // FIXME: We need to do this because LayoutScrollbar::styleChanged is called as soon as the scrollbar is created.

    // Update the scrollbar size.
    IntRect rect(0, 0, 0, 0);
    updateScrollbarPart(ScrollbarBGPart);
    if (LayoutScrollbarPart* part = m_parts.get(ScrollbarBGPart)) {
        part->layout();
        rect.setSize(flooredIntSize(part->size()));
    } else if (this->orientation() == HorizontalScrollbar) {
        rect.setWidth(this->width());
    } else {
        rect.setHeight(this->height());
    }

    setFrameRect(rect);

}

LayoutScrollbar::~LayoutScrollbar()
{
    if (m_parts.isEmpty())
        return;

    // When a scrollbar is detached from its parent (causing all parts removal) and
    // ready to be destroyed, its destruction can be delayed because of RefPtr
    // maintained in other classes such as EventHandler (m_lastScrollbarUnderMouse).
    // Meanwhile, we can have a call to updateScrollbarPart which recreates the
    // scrollbar part. So, we need to destroy these parts since we don't want them
    // to call on a destroyed scrollbar. See webkit bug 68009.
    updateScrollbarParts(true);
}

DEFINE_TRACE(LayoutScrollbar)
{
#if ENABLE(OILPAN)
    visitor->trace(m_owner);
    visitor->trace(m_owningFrame);
#endif
    Scrollbar::trace(visitor);
}

LayoutBox* LayoutScrollbar::owningLayoutObject() const
{
    if (m_owningFrame)
        return m_owningFrame->ownerLayoutObject();
    return m_owner && m_owner->layoutObject() ? m_owner->layoutObject()->enclosingBox() : 0;
}

LayoutBox* LayoutScrollbar::owningLayoutObjectWithinFrame() const
{
    if (m_owningFrame)
        return m_owningFrame->contentLayoutObject();
    return owningLayoutObject();
}

void LayoutScrollbar::setParent(Widget* parent)
{
    Scrollbar::setParent(parent);
    if (!parent) {
        // Destroy all of the scrollbar's LayoutBoxes.
        updateScrollbarParts(true);
    }
}

void LayoutScrollbar::setEnabled(bool e)
{
    bool wasEnabled = enabled();
    Scrollbar::setEnabled(e);
    if (wasEnabled != e)
        updateScrollbarParts();
}

void LayoutScrollbar::styleChanged()
{
    updateScrollbarParts();
}

void LayoutScrollbar::setHoveredPart(ScrollbarPart part)
{
    if (part == m_hoveredPart)
        return;

    ScrollbarPart oldPart = m_hoveredPart;
    m_hoveredPart = part;

    updateScrollbarPart(oldPart);
    updateScrollbarPart(m_hoveredPart);

    updateScrollbarPart(ScrollbarBGPart);
    updateScrollbarPart(TrackBGPart);
}

void LayoutScrollbar::setPressedPart(ScrollbarPart part)
{
    ScrollbarPart oldPart = m_pressedPart;
    Scrollbar::setPressedPart(part);

    updateScrollbarPart(oldPart);
    updateScrollbarPart(part);

    updateScrollbarPart(ScrollbarBGPart);
    updateScrollbarPart(TrackBGPart);
}

PassRefPtr<ComputedStyle> LayoutScrollbar::getScrollbarPseudoStyle(ScrollbarPart partType, PseudoId pseudoId)
{
    if (!owningLayoutObject())
        return nullptr;

    RefPtr<ComputedStyle> result = owningLayoutObject()->getUncachedPseudoStyle(PseudoStyleRequest(pseudoId, this, partType), owningLayoutObject()->style());
    // Scrollbars for root frames should always have background color
    // unless explicitly specified as transparent. So we force it.
    // This is because WebKit assumes scrollbar to be always painted and missing background
    // causes visual artifact like non-paint invalidated dirty region.
    if (result && m_owningFrame && m_owningFrame->view() && !m_owningFrame->view()->isTransparent() && !result->hasBackground())
        result->setBackgroundColor(StyleColor(Color::white));

    return result;
}

void LayoutScrollbar::updateScrollbarParts(bool destroy)
{
    updateScrollbarPart(ScrollbarBGPart, destroy);
    updateScrollbarPart(BackButtonStartPart, destroy);
    updateScrollbarPart(ForwardButtonStartPart, destroy);
    updateScrollbarPart(BackTrackPart, destroy);
    updateScrollbarPart(ThumbPart, destroy);
    updateScrollbarPart(ForwardTrackPart, destroy);
    updateScrollbarPart(BackButtonEndPart, destroy);
    updateScrollbarPart(ForwardButtonEndPart, destroy);
    updateScrollbarPart(TrackBGPart, destroy);

    if (destroy)
        return;

    // See if the scrollbar's thickness changed.  If so, we need to mark our owning object as needing a layout.
    bool isHorizontal = orientation() == HorizontalScrollbar;
    int oldThickness = isHorizontal ? height() : width();
    int newThickness = 0;
    LayoutScrollbarPart* part = m_parts.get(ScrollbarBGPart);
    if (part) {
        part->layout();
        newThickness = isHorizontal ? part->size().height() : part->size().width();
    }

    if (newThickness != oldThickness) {
        setFrameRect(IntRect(location(), IntSize(isHorizontal ? width() : newThickness, isHorizontal ? newThickness : height())));
        if (LayoutBox* box = owningLayoutObjectWithinFrame()) {
            if (box->isLayoutBlock())
                toLayoutBlock(box)->notifyScrollbarThicknessChanged();
            box->setChildNeedsLayout();
            if (m_scrollableArea)
                m_scrollableArea->setScrollCornerNeedsPaintInvalidation();
        }
    }
}

static PseudoId pseudoForScrollbarPart(ScrollbarPart part)
{
    switch (part) {
    case BackButtonStartPart:
    case ForwardButtonStartPart:
    case BackButtonEndPart:
    case ForwardButtonEndPart:
        return PseudoIdScrollbarButton;
    case BackTrackPart:
    case ForwardTrackPart:
        return PseudoIdScrollbarTrackPiece;
    case ThumbPart:
        return PseudoIdScrollbarThumb;
    case TrackBGPart:
        return PseudoIdScrollbarTrack;
    case ScrollbarBGPart:
        return PseudoIdScrollbar;
    case NoPart:
    case AllParts:
        break;
    }
    ASSERT_NOT_REACHED();
    return PseudoIdScrollbar;
}

void LayoutScrollbar::updateScrollbarPart(ScrollbarPart partType, bool destroy)
{
    if (partType == NoPart)
        return;

    RefPtr<ComputedStyle> partStyle = !destroy ? getScrollbarPseudoStyle(partType,  pseudoForScrollbarPart(partType)) : PassRefPtr<ComputedStyle>(nullptr);

    bool needLayoutObject = !destroy && partStyle && partStyle->display() != NONE;

    if (needLayoutObject && partStyle->display() != BLOCK) {
        // See if we are a button that should not be visible according to OS settings.
        WebScrollbarButtonsPlacement buttonsPlacement = theme().buttonsPlacement();
        switch (partType) {
        case BackButtonStartPart:
            needLayoutObject = (buttonsPlacement == WebScrollbarButtonsPlacementSingle || buttonsPlacement == WebScrollbarButtonsPlacementDoubleStart
                || buttonsPlacement == WebScrollbarButtonsPlacementDoubleBoth);
            break;
        case ForwardButtonStartPart:
            needLayoutObject = (buttonsPlacement == WebScrollbarButtonsPlacementDoubleStart || buttonsPlacement == WebScrollbarButtonsPlacementDoubleBoth);
            break;
        case BackButtonEndPart:
            needLayoutObject = (buttonsPlacement == WebScrollbarButtonsPlacementDoubleEnd || buttonsPlacement == WebScrollbarButtonsPlacementDoubleBoth);
            break;
        case ForwardButtonEndPart:
            needLayoutObject = (buttonsPlacement == WebScrollbarButtonsPlacementSingle || buttonsPlacement == WebScrollbarButtonsPlacementDoubleEnd
                || buttonsPlacement == WebScrollbarButtonsPlacementDoubleBoth);
            break;
        default:
            break;
        }
    }

    LayoutScrollbarPart* partLayoutObject = m_parts.get(partType);
    if (!partLayoutObject && needLayoutObject) {
        partLayoutObject = LayoutScrollbarPart::createAnonymous(&owningLayoutObject()->document(), this, partType);
        m_parts.set(partType, partLayoutObject);
    } else if (partLayoutObject && !needLayoutObject) {
        m_parts.remove(partType);
        partLayoutObject->destroy();
        partLayoutObject = 0;
    }

    if (partLayoutObject)
        partLayoutObject->setStyleWithWritingModeOfParent(partStyle.release());
}

IntRect LayoutScrollbar::buttonRect(ScrollbarPart partType) const
{
    LayoutScrollbarPart* partLayoutObject = m_parts.get(partType);
    if (!partLayoutObject)
        return IntRect();

    partLayoutObject->layout();

    bool isHorizontal = orientation() == HorizontalScrollbar;
    if (partType == BackButtonStartPart)
        return IntRect(location(), IntSize(isHorizontal ? partLayoutObject->pixelSnappedWidth() : width(), isHorizontal ? height() : partLayoutObject->pixelSnappedHeight()));
    if (partType == ForwardButtonEndPart) {
        return IntRect(isHorizontal ? x() + width() - partLayoutObject->pixelSnappedWidth() : x(),
            isHorizontal ? y() : y() + height() - partLayoutObject->pixelSnappedHeight(),
            isHorizontal ? partLayoutObject->pixelSnappedWidth() : width(),
            isHorizontal ? height() : partLayoutObject->pixelSnappedHeight());
    }

    if (partType == ForwardButtonStartPart) {
        IntRect previousButton = buttonRect(BackButtonStartPart);
        return IntRect(isHorizontal ? x() + previousButton.width() : x(),
            isHorizontal ? y() : y() + previousButton.height(),
            isHorizontal ? partLayoutObject->pixelSnappedWidth() : width(),
            isHorizontal ? height() : partLayoutObject->pixelSnappedHeight());
    }

    IntRect followingButton = buttonRect(ForwardButtonEndPart);
    return IntRect(isHorizontal ? x() + width() - followingButton.width() - partLayoutObject->pixelSnappedWidth() : x(),
        isHorizontal ? y() : y() + height() - followingButton.height() - partLayoutObject->pixelSnappedHeight(),
        isHorizontal ? partLayoutObject->pixelSnappedWidth() : width(),
        isHorizontal ? height() : partLayoutObject->pixelSnappedHeight());
}

IntRect LayoutScrollbar::trackRect(int startLength, int endLength) const
{
    LayoutScrollbarPart* part = m_parts.get(TrackBGPart);
    if (part)
        part->layout();

    if (orientation() == HorizontalScrollbar) {
        int marginLeft = part ? static_cast<int>(part->marginLeft()) : 0;
        int marginRight = part ? static_cast<int>(part->marginRight()) : 0;
        startLength += marginLeft;
        endLength += marginRight;
        int totalLength = startLength + endLength;
        return IntRect(x() + startLength, y(), width() - totalLength, height());
    }

    int marginTop = part ? static_cast<int>(part->marginTop()) : 0;
    int marginBottom = part ? static_cast<int>(part->marginBottom()) : 0;
    startLength += marginTop;
    endLength += marginBottom;
    int totalLength = startLength + endLength;

    return IntRect(x(), y() + startLength, width(), height() - totalLength);
}

IntRect LayoutScrollbar::trackPieceRectWithMargins(ScrollbarPart partType, const IntRect& oldRect) const
{
    LayoutScrollbarPart* partLayoutObject = m_parts.get(partType);
    if (!partLayoutObject)
        return oldRect;

    partLayoutObject->layout();

    IntRect rect = oldRect;
    if (orientation() == HorizontalScrollbar) {
        rect.setX(rect.x() + partLayoutObject->marginLeft());
        rect.setWidth(rect.width() - partLayoutObject->marginWidth());
    } else {
        rect.setY(rect.y() + partLayoutObject->marginTop());
        rect.setHeight(rect.height() - partLayoutObject->marginHeight());
    }
    return rect;
}

int LayoutScrollbar::minimumThumbLength() const
{
    LayoutScrollbarPart* partLayoutObject = m_parts.get(ThumbPart);
    if (!partLayoutObject)
        return 0;
    partLayoutObject->layout();
    return orientation() == HorizontalScrollbar ? partLayoutObject->size().width() : partLayoutObject->size().height();
}

void LayoutScrollbar::invalidateDisplayItemClientsOfScrollbarParts(const LayoutBoxModelObject& paintInvalidationContainer)
{
    for (auto& part : m_parts)
        part.value->invalidateDisplayItemClientsIncludingNonCompositingDescendants(&paintInvalidationContainer, PaintInvalidationScroll);
}

} // namespace blink