Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(222)

Side by Side Diff: views/controls/scrollbar/native_scroll_bar_views.cc

Issue 7669028: Adding a Views scrollbar implementation. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Refactored the bitmap code to be shared with the native_scroll_bar_views Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "views/controls/scrollbar/native_scroll_bar_views.h"
6
7 #include "base/logging.h"
8 #include "ui/base/keycodes/keyboard_codes.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/gfx/canvas_skia.h"
11 #include "ui/gfx/path.h"
12 #include "views/controls/button/custom_button.h"
13 #include "views/controls/focusable_border.h"
14 #include "views/controls/scrollbar/native_scroll_bar.h"
15 #include "views/controls/scrollbar/scroll_bar.h"
16 #include "views/controls/scrollbar/base_scroll_bar_button.h"
17 #include "views/controls/scrollbar/base_scroll_bar_thumb.h"
18
19 namespace views {
20
21 namespace {
22
23 // Wrapper for the scroll buttons.
24 class ScrollBarButton : public BaseScrollBarButton {
25 public:
26 enum Type {
27 UP,
28 DOWN,
29 LEFT,
30 RIGHT,
31 };
32
33 ScrollBarButton(ButtonListener* listener, Type type);
34 virtual ~ScrollBarButton();
35
36 virtual gfx::Size GetPreferredSize() OVERRIDE;
37
38 protected:
39 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
40
41 private:
42 gfx::NativeTheme::ExtraParams GetNativeThemeParams() const;
43 gfx::NativeTheme::Part GetNativeThemePart() const;
44 gfx::NativeTheme::State GetNativeThemeState() const;
45
46 Type type_;
47 };
48
49 // Wrapper for the scroll thumb
50 class ScrollBarThumb : public BaseScrollBarThumb {
51 public:
52 explicit ScrollBarThumb(BaseScrollBar* scroll_bar);
53 virtual ~ScrollBarThumb();
54
55 virtual gfx::Size GetPreferredSize() OVERRIDE;
56
57 protected:
58 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
59
60 private:
61 gfx::NativeTheme::ExtraParams GetNativeThemeParams() const;
62 gfx::NativeTheme::Part GetNativeThemePart() const;
63 gfx::NativeTheme::State GetNativeThemeState() const;
64
65 ScrollBar* scroll_bar_;
66 };
67
68 /////////////////////////////////////////////////////////////////////////////
69 // ScrollBarButton
70
71 ScrollBarButton::ScrollBarButton(
72 ButtonListener* listener,
73 Type type)
74 : BaseScrollBarButton(listener),
75 type_(type) {
76 }
77
78 ScrollBarButton::~ScrollBarButton() {
79 }
80
81 gfx::Size ScrollBarButton::GetPreferredSize() {
82 const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance();
83 return native_theme->GetPartSize(GetNativeThemePart(),
84 GetNativeThemeState(),
85 GetNativeThemeParams());
86 }
87
88 void ScrollBarButton::OnPaint(gfx::Canvas* canvas) {
89 const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance();
90 gfx::Rect bounds;
91 bounds.set_size(GetPreferredSize());
92
93 native_theme->Paint(canvas->AsCanvasSkia(),
94 GetNativeThemePart(),
95 GetNativeThemeState(),
96 bounds,
97 GetNativeThemeParams());
98 }
99
100 gfx::NativeTheme::ExtraParams
101 ScrollBarButton::GetNativeThemeParams() const {
102 gfx::NativeTheme::ExtraParams params;
103
104 switch (state_) {
105 case CustomButton::BS_HOT:
106 params.scrollbar_arrow.is_hovering = true;
107 break;
108 default:
109 params.scrollbar_arrow.is_hovering = false;
110 break;
111 }
112
113 return params;
114 }
115
116 gfx::NativeTheme::Part
117 ScrollBarButton::GetNativeThemePart() const {
118 switch (type_) {
119 case UP:
120 return gfx::NativeTheme::kScrollbarUpArrow;
121 case DOWN:
122 return gfx::NativeTheme::kScrollbarDownArrow;
123 case LEFT:
124 return gfx::NativeTheme::kScrollbarLeftArrow;
125 case RIGHT:
126 return gfx::NativeTheme::kScrollbarRightArrow;
127 }
128 }
129
130 gfx::NativeTheme::State
131 ScrollBarButton::GetNativeThemeState() const {
132 gfx::NativeTheme::State state;
133
134 switch (state_) {
135 case CustomButton::BS_HOT:
136 state = gfx::NativeTheme::kHovered;
137 break;
138 case CustomButton::BS_PUSHED:
139 state = gfx::NativeTheme::kPressed;
140 break;
141 case CustomButton::BS_DISABLED:
142 state = gfx::NativeTheme::kDisabled;
143 break;
144 case CustomButton::BS_NORMAL:
145 default:
146 state = gfx::NativeTheme::kNormal;
147 break;
148 }
149
150 return state;
151 }
152
153 /////////////////////////////////////////////////////////////////////////////
154 // ScrollBarThumb
155
156 ScrollBarThumb::ScrollBarThumb(BaseScrollBar* scroll_bar)
157 : BaseScrollBarThumb(scroll_bar),
158 scroll_bar_(scroll_bar) {
159 }
160
161 ScrollBarThumb::~ScrollBarThumb() {
162 }
163
164 gfx::Size ScrollBarThumb::GetPreferredSize() {
165 const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance();
166 return native_theme->GetPartSize(GetNativeThemePart(),
167 GetNativeThemeState(),
168 GetNativeThemeParams());
169 }
170
171 void ScrollBarThumb::OnPaint(gfx::Canvas* canvas) {
172 const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance();
173
174 native_theme->Paint(canvas->AsCanvasSkia(),
175 GetNativeThemePart(),
176 GetNativeThemeState(),
177 GetLocalBounds(),
178 GetNativeThemeParams());
179 }
180
181 gfx::NativeTheme::ExtraParams
182 ScrollBarThumb::GetNativeThemeParams() const {
183 gfx::NativeTheme::ExtraParams params;
184
185 switch (GetState()) {
186 case CustomButton::BS_HOT:
187 params.scrollbar_thumb.is_hovering = true;
188 break;
189 default:
190 params.scrollbar_thumb.is_hovering = false;
191 break;
192 }
193
194 return params;
195 }
196
197 gfx::NativeTheme::Part ScrollBarThumb::GetNativeThemePart() const {
198 if (scroll_bar_->IsHorizontal())
199 return gfx::NativeTheme::kScrollbarHorizontalThumb;
200 else
Ben Goodger (Google) 2011/08/23 16:05:15 no else after return
201 return gfx::NativeTheme::kScrollbarVerticalThumb;
202 }
203
204 gfx::NativeTheme::State ScrollBarThumb::GetNativeThemeState() const {
205 gfx::NativeTheme::State state;
206
207 switch (GetState()) {
208 case CustomButton::BS_HOT:
209 state = gfx::NativeTheme::kHovered;
210 break;
211 case CustomButton::BS_PUSHED:
212 state = gfx::NativeTheme::kPressed;
213 break;
214 case CustomButton::BS_DISABLED:
215 state = gfx::NativeTheme::kDisabled;
216 break;
217 case CustomButton::BS_NORMAL:
218 default:
219 state = gfx::NativeTheme::kNormal;
220 break;
221 }
222
223 return state;
224 }
225
226 } // namespace
227
228 ////////////////////////////////////////////////////////////////////////////////
229 // NativeScrollBarViews, public:
230
231 NativeScrollBarViews::NativeScrollBarViews(NativeScrollBar* scroll_bar)
232 : BaseScrollBar(scroll_bar->IsHorizontal(),
233 new ScrollBarThumb(this)),
234 native_scroll_bar_(scroll_bar) {
235 SetController(native_scroll_bar_->GetController());
236
237 if (native_scroll_bar_->IsHorizontal()) {
238 prev_button_ = new ScrollBarButton(this, ScrollBarButton::LEFT);
239 next_button_ = new ScrollBarButton(this, ScrollBarButton::RIGHT);
240
241 part_ = gfx::NativeTheme::kScrollbarHorizontalTrack;
242 } else {
243 prev_button_ = new ScrollBarButton(this, ScrollBarButton::UP);
244 next_button_ = new ScrollBarButton(this, ScrollBarButton::DOWN);
245
246 part_ = gfx::NativeTheme::kScrollbarVerticalTrack;
247 }
248
249 state_ = gfx::NativeTheme::kNormal;
250
251 AddChildView(prev_button_);
252 AddChildView(next_button_);
253 }
254
255 NativeScrollBarViews::~NativeScrollBarViews() {
256 }
257
258 ////////////////////////////////////////////////////////////////////////////////
259 // NativeScrollBarViews, View overrides:
260
261 void NativeScrollBarViews::Layout() {
262 SetBoundsRect(native_scroll_bar_->GetLocalBounds());
263
264 gfx::Size size = prev_button_->GetPreferredSize();
265 prev_button_->SetBounds(0, 0, size.width(), size.height());
266
267 if (native_scroll_bar_->IsHorizontal()) {
268 next_button_->SetBounds(width() - size.width(), 0,
269 size.width(), size.height());
270 } else {
271 next_button_->SetBounds(0, height() - size.height(),
272 size.width(), size.height());
273 }
274
275 GetThumb()->SetBoundsRect(GetTrackBounds());
276 }
277
278 gfx::Size NativeScrollBarViews::GetPreferredSize() {
279 if (native_scroll_bar_->IsHorizontal())
280 return gfx::Size(0, GetHorizontalScrollBarHeight());
281 return gfx::Size(GetVerticalScrollBarWidth(), 0);
282 }
283
284 int NativeScrollBarViews::GetLayoutSize() const {
285 gfx::Size size = prev_button_->GetPreferredSize();
286 return IsHorizontal() ? size.height() : size.width();
287 }
288
289 void NativeScrollBarViews::ScrollToPosition(int position) {
290 GetController()->ScrollToPosition(native_scroll_bar_, position);
291 }
292
293 int NativeScrollBarViews::GetScrollIncrement(bool is_page,
294 bool is_positive) {
295 return GetController()->GetScrollIncrement(native_scroll_bar_,
296 is_page,
297 is_positive);
298 }
299
300 void NativeScrollBarViews::OnPaint(gfx::Canvas* canvas) {
301 const gfx::NativeTheme* native_theme = gfx::NativeTheme::instance();
302 gfx::Rect bounds = GetTrackBounds();
303
304 params_.scrollbar_track.track_x = bounds.x();
305 params_.scrollbar_track.track_y = bounds.y();
306 params_.scrollbar_track.track_width = bounds.width();
307 params_.scrollbar_track.track_height = bounds.height();
308
309
310 native_theme->Paint(canvas->AsCanvasSkia(),
311 part_,
312 state_,
313 bounds,
314 params_);
315 }
316
317 //////////////////////////////////////////////////////////////////////////////
318 // BaseButton::ButtonListener overrides:
319
320 void NativeScrollBarViews::ButtonPressed(Button* sender,
321 const views::Event& event) {
322 if (sender == prev_button_) {
323 ScrollByAmount(SCROLL_PREV_LINE);
324 } else if (sender == next_button_) {
325 ScrollByAmount(SCROLL_NEXT_LINE);
326 }
327 }
328
329 ////////////////////////////////////////////////////////////////////////////////
330 // NativeScrollBarViews, NativeScrollBarWrapper overrides:
331
332 int NativeScrollBarViews::GetPosition() const {
333 return BaseScrollBar::GetPosition();
334 }
335
336 View* NativeScrollBarViews::GetView() {
337 return this;
338 }
339
340 void NativeScrollBarViews::Update(int viewport_size,
341 int content_size,
342 int current_pos) {
343 BaseScrollBar::Update(viewport_size, content_size, current_pos);
344 }
345
346 ////////////////////////////////////////////////////////////////////////////////
347 // NativeScrollBarViews, private:
348
349 gfx::Rect NativeScrollBarViews::GetTrackBounds() const {
350 gfx::Rect bounds = GetLocalBounds();
351 gfx::Size size = prev_button_->GetPreferredSize();
352 BaseScrollBarThumb* thumb = GetThumb();
353
354 if (native_scroll_bar_->IsHorizontal()) {
355 bounds.set_x(bounds.x() + size.width());
356 bounds.set_width(bounds.width() - 2 * size.width());
357 bounds.set_height(thumb->GetPreferredSize().height());
358 } else {
359 bounds.set_y(bounds.y() + size.height());
360 bounds.set_height(bounds.height() - 2 * size.height());
361 bounds.set_width(thumb->GetPreferredSize().width());
362 }
363
364 return bounds;
365 }
366
367 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698