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 "ScrollbarThemeChromium.h"
29
30#include "ChromiumBridge.h"
31#include "PlatformMouseEvent.h"
32#include "Scrollbar.h"
33#include "ScrollbarClient.h"
34#include "ScrollbarThemeComposite.h"
35
36// -----------------------------------------------------------------------------
37// This file contains scrollbar theme code that is cross platform. Additional
38// members of ScrollbarThemeChromium can be found in the platform specific files
39// -----------------------------------------------------------------------------
40
41namespace WebCore {
42
43bool ScrollbarThemeChromium::hasThumb(Scrollbar* scrollbar)
44{
45    // This method is just called as a paint-time optimization to see if
46    // painting the thumb can be skipped.  We don't have to be exact here.
47    return thumbLength(scrollbar) > 0;
48}
49
50IntRect ScrollbarThemeChromium::backButtonRect(Scrollbar* scrollbar, ScrollbarPart part, bool)
51{
52    // Windows and Linux just have single arrows.
53    if (part == BackButtonEndPart)
54        return IntRect();
55
56    IntSize size = buttonSize(scrollbar);
57    return IntRect(scrollbar->x(), scrollbar->y(), size.width(), size.height());
58}
59
60IntRect ScrollbarThemeChromium::forwardButtonRect(Scrollbar* scrollbar, ScrollbarPart part, bool)
61{
62    // Windows and Linux just have single arrows.
63    if (part == ForwardButtonStartPart)
64        return IntRect();
65
66    IntSize size = buttonSize(scrollbar);
67    int x, y;
68    if (scrollbar->orientation() == HorizontalScrollbar) {
69        x = scrollbar->x() + scrollbar->width() - size.width();
70        y = scrollbar->y();
71    } else {
72        x = scrollbar->x();
73        y = scrollbar->y() + scrollbar->height() - size.height();
74    }
75    return IntRect(x, y, size.width(), size.height());
76}
77
78void ScrollbarThemeChromium::paintTrackBackground(GraphicsContext* context, Scrollbar* scrollbar, const IntRect& rect)
79{
80    // Just assume a forward track part.  We only paint the track as a single piece when there is no thumb.
81    if (!hasThumb(scrollbar))
82        paintTrackPiece(context, scrollbar, rect, ForwardTrackPart);
83}
84
85void ScrollbarThemeChromium::paintTickmarks(GraphicsContext* context, Scrollbar* scrollbar, const IntRect& rect)
86{
87    if (scrollbar->orientation() != VerticalScrollbar)
88        return;
89
90    if (rect.height() <= 0 || rect.width() <= 0)
91        return;  // nothing to draw on.
92
93    // Get the tickmarks for the frameview.
94    Vector<IntRect> tickmarks;
95    scrollbar->client()->getTickmarks(tickmarks);
96    if (!tickmarks.size())
97        return;
98
99    // Get the image for the tickmarks.
100    static RefPtr<Image> dash = Image::loadPlatformResource("tickmarkDash");
101    if (dash->isNull()) {
102        ASSERT_NOT_REACHED();
103        return;
104    }
105
106    context->save();
107
108    for (Vector<IntRect>::const_iterator i = tickmarks.begin(); i != tickmarks.end(); ++i) {
109        // Calculate how far down (in %) the tick-mark should appear.
110        const float percent = static_cast<float>(i->y()) / scrollbar->totalSize();
111
112        // Calculate how far down (in pixels) the tick-mark should appear.
113        const int yPos = rect.topLeft().y() + (rect.height() * percent);
114
115        IntPoint tick(scrollbar->x(), yPos);
116        context->drawImage(dash.get(), DeviceColorSpace, tick);
117    }
118
119    context->restore();
120}
121
122void ScrollbarThemeChromium::paintScrollCorner(ScrollView* view, GraphicsContext* context, const IntRect& cornerRect)
123{
124    // ScrollbarThemeComposite::paintScrollCorner incorrectly assumes that the
125    // ScrollView is a FrameView (see FramelessScrollView), so we cannot let
126    // that code run.  For FrameView's this is correct since we don't do custom
127    // scrollbar corner rendering, which ScrollbarThemeComposite supports.
128    ScrollbarTheme::paintScrollCorner(view, context, cornerRect);
129}
130
131} // namespace WebCore
132