1/*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2008, 2009 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "platform/scroll/ScrollbarThemeNonMacCommon.h"
29
30#include "platform/PlatformMouseEvent.h"
31#include "platform/graphics/GraphicsContextStateSaver.h"
32#include "platform/scroll/ScrollableArea.h"
33#include "platform/scroll/ScrollbarThemeClient.h"
34
35namespace blink {
36
37bool ScrollbarThemeNonMacCommon::hasThumb(ScrollbarThemeClient* scrollbar)
38{
39    // This method is just called as a paint-time optimization to see if
40    // painting the thumb can be skipped. We don't have to be exact here.
41    return thumbLength(scrollbar) > 0;
42}
43
44IntRect ScrollbarThemeNonMacCommon::backButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool)
45{
46    // Windows and Linux just have single arrows.
47    if (part == BackButtonEndPart)
48        return IntRect();
49
50    IntSize size = buttonSize(scrollbar);
51    return IntRect(scrollbar->x(), scrollbar->y(), size.width(), size.height());
52}
53
54IntRect ScrollbarThemeNonMacCommon::forwardButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool)
55{
56    // Windows and Linux just have single arrows.
57    if (part == ForwardButtonStartPart)
58        return IntRect();
59
60    IntSize size = buttonSize(scrollbar);
61    int x, y;
62    if (scrollbar->orientation() == HorizontalScrollbar) {
63        x = scrollbar->x() + scrollbar->width() - size.width();
64        y = scrollbar->y();
65    } else {
66        x = scrollbar->x();
67        y = scrollbar->y() + scrollbar->height() - size.height();
68    }
69    return IntRect(x, y, size.width(), size.height());
70}
71
72IntRect ScrollbarThemeNonMacCommon::trackRect(ScrollbarThemeClient* scrollbar, bool)
73{
74    IntSize bs = buttonSize(scrollbar);
75    int thickness = scrollbarThickness(scrollbar->controlSize());
76    if (scrollbar->orientation() == HorizontalScrollbar) {
77        // Once the scrollbar becomes smaller than the size of the
78        // two buttons with a 1 pixel gap, the track disappears.
79        if (scrollbar->width() <= 2 * bs.width() + 1)
80            return IntRect();
81        return IntRect(scrollbar->x() + bs.width(), scrollbar->y(), scrollbar->width() - 2 * bs.width(), thickness);
82    }
83    if (scrollbar->height() <= 2 * bs.height() + 1)
84        return IntRect();
85    return IntRect(scrollbar->x(), scrollbar->y() + bs.height(), thickness, scrollbar->height() - 2 * bs.height());
86}
87
88void ScrollbarThemeNonMacCommon::paintTrackBackground(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
89{
90    // Just assume a forward track part. We only paint the track as a single piece when there is no thumb.
91    if (!hasThumb(scrollbar))
92        paintTrackPiece(context, scrollbar, rect, ForwardTrackPart);
93}
94
95void ScrollbarThemeNonMacCommon::paintTickmarks(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
96{
97    if (scrollbar->orientation() != VerticalScrollbar)
98        return;
99
100    if (rect.height() <= 0 || rect.width() <= 0)
101        return;
102
103    // Get the tickmarks for the frameview.
104    Vector<IntRect> tickmarks;
105    scrollbar->getTickmarks(tickmarks);
106    if (!tickmarks.size())
107        return;
108
109    GraphicsContextStateSaver stateSaver(*context);
110    context->setShouldAntialias(false);
111
112    for (Vector<IntRect>::const_iterator i = tickmarks.begin(); i != tickmarks.end(); ++i) {
113        // Calculate how far down (in %) the tick-mark should appear.
114        const float percent = static_cast<float>(i->y()) / scrollbar->totalSize();
115
116        // Calculate how far down (in pixels) the tick-mark should appear.
117        const int yPos = rect.y() + (rect.height() * percent);
118
119        context->setFillColor(Color(0xCC, 0xAA, 0x00, 0xFF));
120        FloatRect tickRect(rect.x(), yPos, rect.width(), 3);
121        context->fillRect(tickRect);
122
123        context->setFillColor(Color(0xFF, 0xDD, 0x00, 0xFF));
124        FloatRect tickStroke(rect.x(), yPos + 1, rect.width(), 1);
125        context->fillRect(tickStroke);
126    }
127}
128
129} // namespace blink
130