RenderScrollbar.cpp 11.8 KB
Newer Older
hyatt@apple.com's avatar
hyatt@apple.com committed
1
/*
2
 * Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved.
hyatt@apple.com's avatar
hyatt@apple.com committed
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
 *
 * 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 "config.h"
#include "RenderScrollbar.h"
28

hyatt@apple.com's avatar
hyatt@apple.com committed
29 30 31 32 33
#include "RenderScrollbarPart.h"
#include "RenderScrollbarTheme.h"

namespace WebCore {

hyatt@apple.com's avatar
hyatt@apple.com committed
34
PassRefPtr<Scrollbar> RenderScrollbar::createCustomScrollbar(ScrollbarClient* client, ScrollbarOrientation orientation, RenderBox* renderer)
hyatt@apple.com's avatar
hyatt@apple.com committed
35
{
36
    return adoptRef(new RenderScrollbar(client, orientation, renderer));
hyatt@apple.com's avatar
hyatt@apple.com committed
37 38
}

hyatt@apple.com's avatar
hyatt@apple.com committed
39
RenderScrollbar::RenderScrollbar(ScrollbarClient* client, ScrollbarOrientation orientation, RenderBox* renderer)
hyatt@apple.com's avatar
hyatt@apple.com committed
40 41 42
    : Scrollbar(client, orientation, RegularScrollbar, RenderScrollbarTheme::renderScrollbarTheme())
    , m_owner(renderer)
{
43 44 45 46 47 48 49 50 51 52
    // FIXME: We need to do this because RenderScrollbar::styleChanged is called as soon as the scrollbar is created.
    
    // Update the scrollbar size.
    updateScrollbarPart(ScrollbarBGPart);
    RenderScrollbarPart* part = m_parts.get(ScrollbarBGPart);
    if (!part)
        return;

    part->layout();
    setFrameRect(IntRect(0, 0, part->width(), part->height()));
hyatt@apple.com's avatar
hyatt@apple.com committed
53 54 55 56
}

RenderScrollbar::~RenderScrollbar()
{
57 58 59 60 61 62 63
    ASSERT(m_parts.isEmpty());
}

void RenderScrollbar::setParent(ScrollView* parent)
{
    Scrollbar::setParent(parent);
    if (!parent) {
hyatt@apple.com's avatar
hyatt@apple.com committed
64
        // Destroy all of the scrollbar's RenderBoxes.
65
        updateScrollbarParts(true);
66
    }
hyatt@apple.com's avatar
hyatt@apple.com committed
67 68
}

hyatt@apple.com's avatar
hyatt@apple.com committed
69 70 71 72 73 74 75 76
void RenderScrollbar::setEnabled(bool e)
{
    bool wasEnabled = enabled();
    Scrollbar::setEnabled(e);
    if (wasEnabled != e)
        updateScrollbarParts();
}

77 78 79 80 81
void RenderScrollbar::styleChanged()
{
    updateScrollbarParts();
}

hyatt@apple.com's avatar
hyatt@apple.com committed
82 83
void RenderScrollbar::paint(GraphicsContext* context, const IntRect& damageRect)
{
84
    if (context->updatingControlTints()) {
hyatt@apple.com's avatar
hyatt@apple.com committed
85
        updateScrollbarParts();
86 87
        return;
    }
hyatt@apple.com's avatar
hyatt@apple.com committed
88 89 90
    Scrollbar::paint(context, damageRect);
}

91 92 93 94 95 96
void RenderScrollbar::setHoveredPart(ScrollbarPart part)
{
    if (part == m_hoveredPart)
        return;

    ScrollbarPart oldPart = m_hoveredPart;
97
    m_hoveredPart = part;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

    updateScrollbarPart(oldPart);
    updateScrollbarPart(m_hoveredPart);

    updateScrollbarPart(ScrollbarBGPart);
    updateScrollbarPart(TrackBGPart);
}

void RenderScrollbar::setPressedPart(ScrollbarPart part)
{
    ScrollbarPart oldPart = m_pressedPart;
    Scrollbar::setPressedPart(part);
    
    updateScrollbarPart(oldPart);
    updateScrollbarPart(part);
    
    updateScrollbarPart(ScrollbarBGPart);
    updateScrollbarPart(TrackBGPart);
}

hyatt@apple.com's avatar
hyatt@apple.com committed
118 119 120 121 122 123 124
static ScrollbarPart s_styleResolvePart;
static RenderScrollbar* s_styleResolveScrollbar;

RenderScrollbar* RenderScrollbar::scrollbarForStyleResolve()
{
    return s_styleResolveScrollbar;
}
hyatt@apple.com's avatar
hyatt@apple.com committed
125

hyatt@apple.com's avatar
hyatt@apple.com committed
126 127 128 129 130
ScrollbarPart RenderScrollbar::partForStyleResolve()
{
    return s_styleResolvePart;
}

131
PassRefPtr<RenderStyle> RenderScrollbar::getScrollbarPseudoStyle(ScrollbarPart partType, PseudoId pseudoId)
hyatt@apple.com's avatar
hyatt@apple.com committed
132
{
133 134 135
    if (!m_owner)
        return 0;

hyatt@apple.com's avatar
hyatt@apple.com committed
136 137
    s_styleResolvePart = partType;
    s_styleResolveScrollbar = this;
138
    RefPtr<RenderStyle> result = m_owner->getUncachedPseudoStyle(pseudoId, m_owner->style());
hyatt@apple.com's avatar
hyatt@apple.com committed
139 140
    s_styleResolvePart = NoPart;
    s_styleResolveScrollbar = 0;
hyatt@apple.com's avatar
hyatt@apple.com committed
141 142 143
    return result;
}

144
void RenderScrollbar::updateScrollbarParts(bool destroy)
hyatt@apple.com's avatar
hyatt@apple.com committed
145
{
146 147 148 149 150 151 152 153 154
    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);
hyatt@apple.com's avatar
hyatt@apple.com committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    
    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;
    RenderScrollbarPart* part = m_parts.get(ScrollbarBGPart);
    if (part) {
        part->layout();
        newThickness = isHorizontal ? part->height() : part->width();
    }
    
    if (newThickness != oldThickness) {
        setFrameRect(IntRect(x(), y(), isHorizontal ? width() : newThickness, isHorizontal ? newThickness : height()));
        m_owner->setChildNeedsLayout(true);
    }
}

175
static PseudoId pseudoForScrollbarPart(ScrollbarPart part)
hyatt@apple.com's avatar
hyatt@apple.com committed
176
{
177 178 179 180 181
    switch (part) {
        case BackButtonStartPart:
        case ForwardButtonStartPart:
        case BackButtonEndPart:
        case ForwardButtonEndPart:
182
            return SCROLLBAR_BUTTON;
183 184
        case BackTrackPart:
        case ForwardTrackPart:
185
            return SCROLLBAR_TRACK_PIECE;
186
        case ThumbPart:
187
            return SCROLLBAR_THUMB;
188
        case TrackBGPart:
189
            return SCROLLBAR_TRACK;
190
        case ScrollbarBGPart:
191
            return SCROLLBAR;
192 193 194
        case NoPart:
        case AllParts:
            break;
195
    }
196 197
    ASSERT_NOT_REACHED();
    return SCROLLBAR;
198 199
}

200
void RenderScrollbar::updateScrollbarPart(ScrollbarPart partType, bool destroy)
201 202 203 204
{
    if (partType == NoPart)
        return;

205
    RefPtr<RenderStyle> partStyle = !destroy ? getScrollbarPseudoStyle(partType,  pseudoForScrollbarPart(partType)) : 0;
hyatt@apple.com's avatar
hyatt@apple.com committed
206 207 208
    
    bool needRenderer = !destroy && partStyle && partStyle->display() != NONE && partStyle->visibility() == VISIBLE;
    
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    if (needRenderer && partStyle->display() != BLOCK) {
        // See if we are a button that should not be visible according to OS settings.
        ScrollbarButtonsPlacement buttonsPlacement = theme()->buttonsPlacement();
        switch (partType) {
            case BackButtonStartPart:
                needRenderer = (buttonsPlacement == ScrollbarButtonsSingle || buttonsPlacement == ScrollbarButtonsDoubleStart ||
                                buttonsPlacement == ScrollbarButtonsDoubleBoth);
                break;
            case ForwardButtonStartPart:
                needRenderer = (buttonsPlacement == ScrollbarButtonsDoubleStart || buttonsPlacement == ScrollbarButtonsDoubleBoth);
                break;
            case BackButtonEndPart:
                needRenderer = (buttonsPlacement == ScrollbarButtonsDoubleEnd || buttonsPlacement == ScrollbarButtonsDoubleBoth);
                break;
            case ForwardButtonEndPart:
                needRenderer = (buttonsPlacement == ScrollbarButtonsSingle || buttonsPlacement == ScrollbarButtonsDoubleEnd ||
                                buttonsPlacement == ScrollbarButtonsDoubleBoth);
                break;
            default:
                break;
        }
    }
    
hyatt@apple.com's avatar
hyatt@apple.com committed
232 233
    RenderScrollbarPart* partRenderer = m_parts.get(partType);
    if (!partRenderer && needRenderer) {
234
        partRenderer = new (m_owner->renderArena()) RenderScrollbarPart(m_owner->document(), this, partType);
hyatt@apple.com's avatar
hyatt@apple.com committed
235 236 237 238 239 240 241 242
        m_parts.set(partType, partRenderer);
    } else if (partRenderer && !needRenderer) {
        m_parts.remove(partType);
        partRenderer->destroy();
        partRenderer = 0;
    }
    
    if (partRenderer)
243
        partRenderer->setStyle(partStyle.release());
hyatt@apple.com's avatar
hyatt@apple.com committed
244 245 246 247 248 249 250
}

void RenderScrollbar::paintPart(GraphicsContext* graphicsContext, ScrollbarPart partType, const IntRect& rect)
{
    RenderScrollbarPart* partRenderer = m_parts.get(partType);
    if (!partRenderer)
        return;
251
    partRenderer->paintIntoRect(graphicsContext, x(), y(), rect);
hyatt@apple.com's avatar
hyatt@apple.com committed
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
}

IntRect RenderScrollbar::buttonRect(ScrollbarPart partType)
{
    RenderScrollbarPart* partRenderer = m_parts.get(partType);
    if (!partRenderer)
        return IntRect();
        
    partRenderer->layout();
    
    bool isHorizontal = orientation() == HorizontalScrollbar;
    if (partType == BackButtonStartPart)
        return IntRect(x(), y(), isHorizontal ? partRenderer->width() : width(), isHorizontal ? height() : partRenderer->height());
    if (partType == ForwardButtonEndPart)
        return IntRect(isHorizontal ? x() + width() - partRenderer->width() : x(),
        
                       isHorizontal ? y() : y() + height() - partRenderer->height(),
                       isHorizontal ? partRenderer->width() : width(),
                       isHorizontal ? height() : partRenderer->height());
    
    if (partType == ForwardButtonStartPart) {
        IntRect previousButton = buttonRect(BackButtonStartPart);
        return IntRect(isHorizontal ? x() + previousButton.width() : x(),
                       isHorizontal ? y() : y() + previousButton.height(),
                       isHorizontal ? partRenderer->width() : width(),
                       isHorizontal ? height() : partRenderer->height());
    }
    
    IntRect followingButton = buttonRect(ForwardButtonEndPart);
    return IntRect(isHorizontal ? x() + width() - followingButton.width() - partRenderer->width() : x(),
                   isHorizontal ? y() : y() + height() - followingButton.height() - partRenderer->height(),
                   isHorizontal ? partRenderer->width() : width(),
                   isHorizontal ? height() : partRenderer->height());
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
IntRect RenderScrollbar::trackRect(int startLength, int endLength)
{
    RenderScrollbarPart* part = m_parts.get(TrackBGPart);
    if (part)
        part->layout();

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

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

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
IntRect RenderScrollbar::trackPieceRectWithMargins(ScrollbarPart partType, const IntRect& oldRect)
{
    RenderScrollbarPart* partRenderer = m_parts.get(partType);
    if (!partRenderer)
        return oldRect;
    
    partRenderer->layout();
    
    IntRect rect = oldRect;
    if (orientation() == HorizontalScrollbar) {
        rect.setX(rect.x() + partRenderer->marginLeft());
        rect.setWidth(rect.width() - (partRenderer->marginLeft() + partRenderer->marginRight()));
    } else {
        rect.setY(rect.y() + partRenderer->marginTop());
        rect.setHeight(rect.height() - (partRenderer->marginTop() + partRenderer->marginBottom()));
    }
    return rect;
}

hyatt@apple.com's avatar
hyatt@apple.com committed
330 331 332 333 334 335 336 337 338 339
int RenderScrollbar::minimumThumbLength()
{
    RenderScrollbarPart* partRenderer = m_parts.get(ThumbPart);
    if (!partRenderer)
        return 0;    
    partRenderer->layout();
    return orientation() == HorizontalScrollbar ? partRenderer->width() : partRenderer->height();
}

}