2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2008, 2009 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "platform/scroll/ScrollbarThemeNonMacCommon.h"
30 #include "platform/PlatformMouseEvent.h"
31 #include "platform/graphics/GraphicsContextStateSaver.h"
32 #include "platform/scroll/ScrollableArea.h"
33 #include "platform/scroll/ScrollbarThemeClient.h"
37 bool ScrollbarThemeNonMacCommon::hasThumb(ScrollbarThemeClient* scrollbar)
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;
44 IntRect ScrollbarThemeNonMacCommon::backButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool)
46 // Windows and Linux just have single arrows.
47 if (part == BackButtonEndPart)
50 IntSize size = buttonSize(scrollbar);
51 return IntRect(scrollbar->x(), scrollbar->y(), size.width(), size.height());
54 IntRect ScrollbarThemeNonMacCommon::forwardButtonRect(ScrollbarThemeClient* scrollbar, ScrollbarPart part, bool)
56 // Windows and Linux just have single arrows.
57 if (part == ForwardButtonStartPart)
60 IntSize size = buttonSize(scrollbar);
62 if (scrollbar->orientation() == HorizontalScrollbar) {
63 x = scrollbar->x() + scrollbar->width() - size.width();
67 y = scrollbar->y() + scrollbar->height() - size.height();
69 return IntRect(x, y, size.width(), size.height());
72 IntRect ScrollbarThemeNonMacCommon::trackRect(ScrollbarThemeClient* scrollbar, bool)
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)
81 return IntRect(scrollbar->x() + bs.width(), scrollbar->y(), scrollbar->width() - 2 * bs.width(), thickness);
83 if (scrollbar->height() <= 2 * bs.height() + 1)
85 return IntRect(scrollbar->x(), scrollbar->y() + bs.height(), thickness, scrollbar->height() - 2 * bs.height());
88 void ScrollbarThemeNonMacCommon::paintTrackBackground(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
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);
95 void ScrollbarThemeNonMacCommon::paintTickmarks(GraphicsContext* context, ScrollbarThemeClient* scrollbar, const IntRect& rect)
97 if (scrollbar->orientation() != VerticalScrollbar)
100 if (rect.height() <= 0 || rect.width() <= 0)
103 // Get the tickmarks for the frameview.
104 Vector<IntRect> tickmarks;
105 scrollbar->getTickmarks(tickmarks);
106 if (!tickmarks.size())
109 GraphicsContextStateSaver stateSaver(*context);
110 context->setShouldAntialias(false);
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();
116 // Calculate how far down (in pixels) the tick-mark should appear.
117 const int yPos = rect.y() + (rect.height() * percent);
119 FloatRect tickRect(rect.x(), yPos, rect.width(), 3);
120 context->fillRect(tickRect, Color(0xCC, 0xAA, 0x00, 0xFF));
122 FloatRect tickStroke(rect.x(), yPos + 1, rect.width(), 1);
123 context->fillRect(tickStroke, Color(0xFF, 0xDD, 0x00, 0xFF));