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
41 namespace WebCore {
42
nativeTheme()43 ScrollbarTheme* ScrollbarTheme::nativeTheme()
44 {
45 static ScrollbarThemeChromium theme;
46 return &theme;
47 }
48
ScrollbarThemeChromium()49 ScrollbarThemeChromium::ScrollbarThemeChromium()
50 {
51 }
52
~ScrollbarThemeChromium()53 ScrollbarThemeChromium::~ScrollbarThemeChromium()
54 {
55 }
56
themeChanged()57 void ScrollbarThemeChromium::themeChanged()
58 {
59 }
60
hasThumb(Scrollbar * scrollbar)61 bool ScrollbarThemeChromium::hasThumb(Scrollbar* scrollbar)
62 {
63 // This method is just called as a paint-time optimization to see if
64 // painting the thumb can be skipped. We don't have to be exact here.
65 return thumbLength(scrollbar) > 0;
66 }
67
backButtonRect(Scrollbar * scrollbar,ScrollbarPart part,bool)68 IntRect ScrollbarThemeChromium::backButtonRect(Scrollbar* scrollbar, ScrollbarPart part, bool)
69 {
70 // Windows and Linux just have single arrows.
71 if (part == BackButtonEndPart)
72 return IntRect();
73
74 IntSize size = buttonSize(scrollbar);
75 return IntRect(scrollbar->x(), scrollbar->y(), size.width(), size.height());
76 }
77
forwardButtonRect(Scrollbar * scrollbar,ScrollbarPart part,bool)78 IntRect ScrollbarThemeChromium::forwardButtonRect(Scrollbar* scrollbar, ScrollbarPart part, bool)
79 {
80 // Windows and Linux just have single arrows.
81 if (part == ForwardButtonStartPart)
82 return IntRect();
83
84 IntSize size = buttonSize(scrollbar);
85 int x, y;
86 if (scrollbar->orientation() == HorizontalScrollbar) {
87 x = scrollbar->x() + scrollbar->width() - size.width();
88 y = scrollbar->y();
89 } else {
90 x = scrollbar->x();
91 y = scrollbar->y() + scrollbar->height() - size.height();
92 }
93 return IntRect(x, y, size.width(), size.height());
94 }
95
trackRect(Scrollbar * scrollbar,bool)96 IntRect ScrollbarThemeChromium::trackRect(Scrollbar* scrollbar, bool)
97 {
98 IntSize bs = buttonSize(scrollbar);
99 int thickness = scrollbarThickness();
100 if (scrollbar->orientation() == HorizontalScrollbar) {
101 if (scrollbar->width() < 2 * thickness)
102 return IntRect();
103 return IntRect(scrollbar->x() + bs.width(), scrollbar->y(), scrollbar->width() - 2 * bs.width(), thickness);
104 }
105 if (scrollbar->height() < 2 * thickness)
106 return IntRect();
107 return IntRect(scrollbar->x(), scrollbar->y() + bs.height(), thickness, scrollbar->height() - 2 * bs.height());
108 }
109
paintTrackBackground(GraphicsContext * context,Scrollbar * scrollbar,const IntRect & rect)110 void ScrollbarThemeChromium::paintTrackBackground(GraphicsContext* context, Scrollbar* scrollbar, const IntRect& rect)
111 {
112 // Just assume a forward track part. We only paint the track as a single piece when there is no thumb.
113 if (!hasThumb(scrollbar))
114 paintTrackPiece(context, scrollbar, rect, ForwardTrackPart);
115 }
116
paintTickmarks(GraphicsContext * context,Scrollbar * scrollbar,const IntRect & rect)117 void ScrollbarThemeChromium::paintTickmarks(GraphicsContext* context, Scrollbar* scrollbar, const IntRect& rect)
118 {
119 if (scrollbar->orientation() != VerticalScrollbar)
120 return;
121
122 if (rect.height() <= 0 || rect.width() <= 0)
123 return; // nothing to draw on.
124
125 // Get the tickmarks for the frameview.
126 Vector<IntRect> tickmarks;
127 scrollbar->client()->getTickmarks(tickmarks);
128 if (!tickmarks.size())
129 return;
130
131 // Get the image for the tickmarks.
132 static RefPtr<Image> dash = Image::loadPlatformResource("tickmarkDash");
133 if (dash->isNull()) {
134 ASSERT_NOT_REACHED();
135 return;
136 }
137
138 context->save();
139
140 for (Vector<IntRect>::const_iterator i = tickmarks.begin(); i != tickmarks.end(); ++i) {
141 // Calculate how far down (in %) the tick-mark should appear.
142 const float percent = static_cast<float>(i->y()) / scrollbar->totalSize();
143
144 // Calculate how far down (in pixels) the tick-mark should appear.
145 const int yPos = rect.topLeft().y() + (rect.height() * percent);
146
147 IntPoint tick(scrollbar->x(), yPos);
148 context->drawImage(dash.get(), tick);
149 }
150
151 context->restore();
152 }
153
paintScrollCorner(ScrollView * view,GraphicsContext * context,const IntRect & cornerRect)154 void ScrollbarThemeChromium::paintScrollCorner(ScrollView* view, GraphicsContext* context, const IntRect& cornerRect)
155 {
156 // ScrollbarThemeComposite::paintScrollCorner incorrectly assumes that the
157 // ScrollView is a FrameView (see FramelessScrollView), so we cannot let
158 // that code run. For FrameView's this is correct since we don't do custom
159 // scrollbar corner rendering, which ScrollbarThemeComposite supports.
160 ScrollbarTheme::paintScrollCorner(view, context, cornerRect);
161 }
162
shouldCenterOnThumb(Scrollbar *,const PlatformMouseEvent & evt)163 bool ScrollbarThemeChromium::shouldCenterOnThumb(Scrollbar*, const PlatformMouseEvent& evt)
164 {
165 return evt.shiftKey() && evt.button() == LeftButton;
166 }
167
buttonSize(Scrollbar * scrollbar)168 IntSize ScrollbarThemeChromium::buttonSize(Scrollbar* scrollbar)
169 {
170 // Our desired rect is essentially thickness by thickness.
171
172 // Our actual rect will shrink to half the available space when we have < 2
173 // times thickness pixels left. This allows the scrollbar to scale down
174 // and function even at tiny sizes.
175
176 int thickness = scrollbarThickness();
177
178 #if !defined(__linux__)
179 // In layout test mode, we force the button "girth" (i.e., the length of
180 // the button along the axis of the scrollbar) to be a fixed size.
181 // FIXME: This is retarded! scrollbarThickness is already fixed in layout
182 // test mode so that should be enough to result in repeatable results, but
183 // preserving this hack avoids having to rebaseline pixel tests.
184 const int kLayoutTestModeGirth = 17;
185
186 int girth = ChromiumBridge::layoutTestMode() ? kLayoutTestModeGirth : thickness;
187 #else
188 int girth = thickness;
189 #endif
190
191 if (scrollbar->orientation() == HorizontalScrollbar) {
192 int width = scrollbar->width() < 2 * girth ? scrollbar->width() / 2 : girth;
193 return IntSize(width, thickness);
194 }
195
196 int height = scrollbar->height() < 2 * girth ? scrollbar->height() / 2 : girth;
197 return IntSize(thickness, height);
198 }
199
200 } // namespace WebCore
201