OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "ui/views/controls/scrollbar/native_scroll_bar_views.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "ui/events/keycodes/keyboard_codes.h" | |
9 #include "ui/gfx/canvas.h" | |
10 #include "ui/gfx/path.h" | |
11 #include "ui/views/controls/button/custom_button.h" | |
12 #include "ui/views/controls/focusable_border.h" | |
13 #include "ui/views/controls/scrollbar/base_scroll_bar_button.h" | |
14 #include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h" | |
15 #include "ui/views/controls/scrollbar/native_scroll_bar.h" | |
16 #include "ui/views/controls/scrollbar/scroll_bar.h" | |
17 | |
18 namespace views { | |
19 | |
20 namespace { | |
21 | |
22 // Wrapper for the scroll buttons. | |
23 class ScrollBarButton : public BaseScrollBarButton { | |
24 public: | |
25 enum Type { | |
26 UP, | |
27 DOWN, | |
28 LEFT, | |
29 RIGHT, | |
30 }; | |
31 | |
32 ScrollBarButton(ButtonListener* listener, Type type); | |
33 ~ScrollBarButton() override; | |
34 | |
35 gfx::Size GetPreferredSize() const override; | |
36 const char* GetClassName() const override { return "ScrollBarButton"; } | |
37 | |
38 protected: | |
39 void OnPaint(gfx::Canvas* canvas) override; | |
40 | |
41 private: | |
42 ui::NativeTheme::ExtraParams GetNativeThemeParams() const; | |
43 ui::NativeTheme::Part GetNativeThemePart() const; | |
44 ui::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 ~ScrollBarThumb() override; | |
54 | |
55 gfx::Size GetPreferredSize() const override; | |
56 const char* GetClassName() const override { return "ScrollBarThumb"; } | |
57 | |
58 protected: | |
59 void OnPaint(gfx::Canvas* canvas) override; | |
60 | |
61 private: | |
62 ui::NativeTheme::ExtraParams GetNativeThemeParams() const; | |
63 ui::NativeTheme::Part GetNativeThemePart() const; | |
64 ui::NativeTheme::State GetNativeThemeState() const; | |
65 | |
66 ScrollBar* scroll_bar_; | |
67 }; | |
68 | |
69 ///////////////////////////////////////////////////////////////////////////// | |
70 // ScrollBarButton | |
71 | |
72 ScrollBarButton::ScrollBarButton(ButtonListener* listener, Type type) | |
73 : BaseScrollBarButton(listener), | |
74 type_(type) { | |
75 SetFocusBehavior(FocusBehavior::NEVER); | |
76 } | |
77 | |
78 ScrollBarButton::~ScrollBarButton() { | |
79 } | |
80 | |
81 gfx::Size ScrollBarButton::GetPreferredSize() const { | |
82 return GetNativeTheme()->GetPartSize(GetNativeThemePart(), | |
83 GetNativeThemeState(), | |
84 GetNativeThemeParams()); | |
85 } | |
86 | |
87 void ScrollBarButton::OnPaint(gfx::Canvas* canvas) { | |
88 gfx::Rect bounds(GetPreferredSize()); | |
89 GetNativeTheme()->Paint(canvas->sk_canvas(), GetNativeThemePart(), | |
90 GetNativeThemeState(), bounds, | |
91 GetNativeThemeParams()); | |
92 } | |
93 | |
94 ui::NativeTheme::ExtraParams | |
95 ScrollBarButton::GetNativeThemeParams() const { | |
96 ui::NativeTheme::ExtraParams params; | |
97 | |
98 switch (state()) { | |
99 case CustomButton::STATE_HOVERED: | |
100 params.scrollbar_arrow.is_hovering = true; | |
101 break; | |
102 default: | |
103 params.scrollbar_arrow.is_hovering = false; | |
104 break; | |
105 } | |
106 | |
107 return params; | |
108 } | |
109 | |
110 ui::NativeTheme::Part | |
111 ScrollBarButton::GetNativeThemePart() const { | |
112 switch (type_) { | |
113 case UP: | |
114 return ui::NativeTheme::kScrollbarUpArrow; | |
115 case DOWN: | |
116 return ui::NativeTheme::kScrollbarDownArrow; | |
117 case LEFT: | |
118 return ui::NativeTheme::kScrollbarLeftArrow; | |
119 case RIGHT: | |
120 return ui::NativeTheme::kScrollbarRightArrow; | |
121 } | |
122 | |
123 NOTREACHED(); | |
124 return ui::NativeTheme::kScrollbarUpArrow; | |
125 } | |
126 | |
127 ui::NativeTheme::State | |
128 ScrollBarButton::GetNativeThemeState() const { | |
129 switch (state()) { | |
130 case CustomButton::STATE_HOVERED: | |
131 return ui::NativeTheme::kHovered; | |
132 case CustomButton::STATE_PRESSED: | |
133 return ui::NativeTheme::kPressed; | |
134 case CustomButton::STATE_DISABLED: | |
135 return ui::NativeTheme::kDisabled; | |
136 case CustomButton::STATE_NORMAL: | |
137 return ui::NativeTheme::kNormal; | |
138 case CustomButton::STATE_COUNT: | |
139 break; | |
140 } | |
141 | |
142 NOTREACHED(); | |
143 return ui::NativeTheme::kNormal; | |
144 } | |
145 | |
146 ///////////////////////////////////////////////////////////////////////////// | |
147 // ScrollBarThumb | |
148 | |
149 ScrollBarThumb::ScrollBarThumb(BaseScrollBar* scroll_bar) | |
150 : BaseScrollBarThumb(scroll_bar), | |
151 scroll_bar_(scroll_bar) { | |
152 } | |
153 | |
154 ScrollBarThumb::~ScrollBarThumb() { | |
155 } | |
156 | |
157 gfx::Size ScrollBarThumb::GetPreferredSize() const { | |
158 return GetNativeTheme()->GetPartSize(GetNativeThemePart(), | |
159 GetNativeThemeState(), | |
160 GetNativeThemeParams()); | |
161 } | |
162 | |
163 void ScrollBarThumb::OnPaint(gfx::Canvas* canvas) { | |
164 const gfx::Rect local_bounds(GetLocalBounds()); | |
165 const ui::NativeTheme::State theme_state = GetNativeThemeState(); | |
166 const ui::NativeTheme::ExtraParams extra_params(GetNativeThemeParams()); | |
167 GetNativeTheme()->Paint(canvas->sk_canvas(), | |
168 GetNativeThemePart(), | |
169 theme_state, | |
170 local_bounds, | |
171 extra_params); | |
172 const ui::NativeTheme::Part gripper_part = scroll_bar_->IsHorizontal() ? | |
173 ui::NativeTheme::kScrollbarHorizontalGripper : | |
174 ui::NativeTheme::kScrollbarVerticalGripper; | |
175 GetNativeTheme()->Paint(canvas->sk_canvas(), gripper_part, theme_state, | |
176 local_bounds, extra_params); | |
177 } | |
178 | |
179 ui::NativeTheme::ExtraParams ScrollBarThumb::GetNativeThemeParams() const { | |
180 // This gives the behavior we want. | |
181 ui::NativeTheme::ExtraParams params; | |
182 params.scrollbar_thumb.is_hovering = | |
183 (GetState() != CustomButton::STATE_HOVERED); | |
184 return params; | |
185 } | |
186 | |
187 ui::NativeTheme::Part ScrollBarThumb::GetNativeThemePart() const { | |
188 if (scroll_bar_->IsHorizontal()) | |
189 return ui::NativeTheme::kScrollbarHorizontalThumb; | |
190 return ui::NativeTheme::kScrollbarVerticalThumb; | |
191 } | |
192 | |
193 ui::NativeTheme::State ScrollBarThumb::GetNativeThemeState() const { | |
194 switch (GetState()) { | |
195 case CustomButton::STATE_HOVERED: | |
196 return ui::NativeTheme::kHovered; | |
197 case CustomButton::STATE_PRESSED: | |
198 return ui::NativeTheme::kPressed; | |
199 case CustomButton::STATE_DISABLED: | |
200 return ui::NativeTheme::kDisabled; | |
201 case CustomButton::STATE_NORMAL: | |
202 return ui::NativeTheme::kNormal; | |
203 case CustomButton::STATE_COUNT: | |
204 break; | |
205 } | |
206 | |
207 NOTREACHED(); | |
208 return ui::NativeTheme::kNormal; | |
209 } | |
210 | |
211 } // namespace | |
212 | |
213 //////////////////////////////////////////////////////////////////////////////// | |
214 // NativeScrollBarViews, public: | |
215 | |
216 const char NativeScrollBarViews::kViewClassName[] = "NativeScrollBarViews"; | |
217 | |
218 NativeScrollBarViews::NativeScrollBarViews(NativeScrollBar* scroll_bar) | |
219 : BaseScrollBar(scroll_bar->IsHorizontal(), | |
220 new ScrollBarThumb(this)), | |
221 native_scroll_bar_(scroll_bar) { | |
222 set_controller(native_scroll_bar_->controller()); | |
223 | |
224 if (native_scroll_bar_->IsHorizontal()) { | |
225 prev_button_ = new ScrollBarButton(this, ScrollBarButton::LEFT); | |
226 next_button_ = new ScrollBarButton(this, ScrollBarButton::RIGHT); | |
227 | |
228 part_ = ui::NativeTheme::kScrollbarHorizontalTrack; | |
229 } else { | |
230 prev_button_ = new ScrollBarButton(this, ScrollBarButton::UP); | |
231 next_button_ = new ScrollBarButton(this, ScrollBarButton::DOWN); | |
232 | |
233 part_ = ui::NativeTheme::kScrollbarVerticalTrack; | |
234 } | |
235 | |
236 state_ = ui::NativeTheme::kNormal; | |
237 | |
238 AddChildView(prev_button_); | |
239 AddChildView(next_button_); | |
240 | |
241 prev_button_->set_context_menu_controller(this); | |
242 next_button_->set_context_menu_controller(this); | |
243 } | |
244 | |
245 NativeScrollBarViews::~NativeScrollBarViews() { | |
246 } | |
247 | |
248 //////////////////////////////////////////////////////////////////////////////// | |
249 // NativeScrollBarViews, View overrides: | |
250 | |
251 void NativeScrollBarViews::Layout() { | |
252 gfx::Size size = prev_button_->GetPreferredSize(); | |
253 prev_button_->SetBounds(0, 0, size.width(), size.height()); | |
254 | |
255 if (native_scroll_bar_->IsHorizontal()) { | |
256 next_button_->SetBounds(width() - size.width(), 0, | |
257 size.width(), size.height()); | |
258 } else { | |
259 next_button_->SetBounds(0, height() - size.height(), | |
260 size.width(), size.height()); | |
261 } | |
262 | |
263 GetThumb()->SetBoundsRect(GetTrackBounds()); | |
264 } | |
265 | |
266 void NativeScrollBarViews::OnPaint(gfx::Canvas* canvas) { | |
267 gfx::Rect bounds = GetTrackBounds(); | |
268 | |
269 if (bounds.IsEmpty()) | |
270 return; | |
271 | |
272 params_.scrollbar_track.track_x = bounds.x(); | |
273 params_.scrollbar_track.track_y = bounds.y(); | |
274 params_.scrollbar_track.track_width = bounds.width(); | |
275 params_.scrollbar_track.track_height = bounds.height(); | |
276 params_.scrollbar_track.classic_state = 0; | |
277 | |
278 GetNativeTheme()->Paint(canvas->sk_canvas(), part_, state_, bounds, params_); | |
279 } | |
280 | |
281 gfx::Size NativeScrollBarViews::GetPreferredSize() const { | |
282 const ui::NativeTheme* theme = native_scroll_bar_->GetNativeTheme(); | |
283 if (native_scroll_bar_->IsHorizontal()) | |
284 return gfx::Size(0, GetHorizontalScrollBarHeight(theme)); | |
285 return gfx::Size(GetVerticalScrollBarWidth(theme), 0); | |
286 } | |
287 | |
288 const char* NativeScrollBarViews::GetClassName() const { | |
289 return kViewClassName; | |
290 } | |
291 | |
292 int NativeScrollBarViews::GetLayoutSize() const { | |
293 gfx::Size size = prev_button_->GetPreferredSize(); | |
294 return IsHorizontal() ? size.height() : size.width(); | |
295 } | |
296 | |
297 void NativeScrollBarViews::ScrollToPosition(int position) { | |
298 controller()->ScrollToPosition(native_scroll_bar_, position); | |
299 } | |
300 | |
301 int NativeScrollBarViews::GetScrollIncrement(bool is_page, bool is_positive) { | |
302 return controller()->GetScrollIncrement(native_scroll_bar_, | |
303 is_page, | |
304 is_positive); | |
305 } | |
306 | |
307 ////////////////////////////////////////////////////////////////////////////// | |
308 // BaseButton::ButtonListener overrides: | |
309 | |
310 void NativeScrollBarViews::ButtonPressed(Button* sender, | |
311 const ui::Event& event) { | |
312 if (sender == prev_button_) { | |
313 ScrollByAmount(SCROLL_PREV_LINE); | |
314 } else if (sender == next_button_) { | |
315 ScrollByAmount(SCROLL_NEXT_LINE); | |
316 } | |
317 } | |
318 | |
319 //////////////////////////////////////////////////////////////////////////////// | |
320 // NativeScrollBarViews, NativeScrollBarWrapper overrides: | |
321 | |
322 int NativeScrollBarViews::GetPosition() const { | |
323 return BaseScrollBar::GetPosition(); | |
324 } | |
325 | |
326 View* NativeScrollBarViews::GetView() { | |
327 return this; | |
328 } | |
329 | |
330 void NativeScrollBarViews::Update(int viewport_size, | |
331 int content_size, | |
332 int current_pos) { | |
333 BaseScrollBar::Update(viewport_size, content_size, current_pos); | |
334 } | |
335 | |
336 //////////////////////////////////////////////////////////////////////////////// | |
337 // NativeScrollBarViews, private: | |
338 | |
339 gfx::Rect NativeScrollBarViews::GetTrackBounds() const { | |
340 gfx::Rect bounds = GetLocalBounds(); | |
341 gfx::Size size = prev_button_->GetPreferredSize(); | |
342 BaseScrollBarThumb* thumb = GetThumb(); | |
343 | |
344 if (native_scroll_bar_->IsHorizontal()) { | |
345 bounds.set_x(bounds.x() + size.width()); | |
346 bounds.set_width(std::max(0, bounds.width() - 2 * size.width())); | |
347 bounds.set_height(thumb->GetPreferredSize().height()); | |
348 } else { | |
349 bounds.set_y(bounds.y() + size.height()); | |
350 bounds.set_height(std::max(0, bounds.height() - 2 * size.height())); | |
351 bounds.set_width(thumb->GetPreferredSize().width()); | |
352 } | |
353 | |
354 return bounds; | |
355 } | |
356 | |
357 //////////////////////////////////////////////////////////////////////////////// | |
358 // NativewScrollBarWrapper, public: | |
359 | |
360 // static | |
361 NativeScrollBarWrapper* NativeScrollBarWrapper::CreateWrapper( | |
362 NativeScrollBar* scroll_bar) { | |
363 return new NativeScrollBarViews(scroll_bar); | |
364 } | |
365 | |
366 // static | |
367 int NativeScrollBarWrapper::GetHorizontalScrollBarHeight( | |
368 const ui::NativeTheme* theme) { | |
369 ui::NativeTheme::ExtraParams button_params; | |
370 button_params.scrollbar_arrow.is_hovering = false; | |
371 gfx::Size button_size = theme->GetPartSize( | |
372 ui::NativeTheme::kScrollbarLeftArrow, | |
373 ui::NativeTheme::kNormal, | |
374 button_params); | |
375 | |
376 ui::NativeTheme::ExtraParams thumb_params; | |
377 thumb_params.scrollbar_thumb.is_hovering = false; | |
378 gfx::Size track_size = theme->GetPartSize( | |
379 ui::NativeTheme::kScrollbarHorizontalThumb, | |
380 ui::NativeTheme::kNormal, | |
381 thumb_params); | |
382 | |
383 return std::max(track_size.height(), button_size.height()); | |
384 } | |
385 | |
386 // static | |
387 int NativeScrollBarWrapper::GetVerticalScrollBarWidth( | |
388 const ui::NativeTheme* theme) { | |
389 ui::NativeTheme::ExtraParams button_params; | |
390 button_params.scrollbar_arrow.is_hovering = false; | |
391 gfx::Size button_size = theme->GetPartSize( | |
392 ui::NativeTheme::kScrollbarUpArrow, | |
393 ui::NativeTheme::kNormal, | |
394 button_params); | |
395 | |
396 ui::NativeTheme::ExtraParams thumb_params; | |
397 thumb_params.scrollbar_thumb.is_hovering = false; | |
398 gfx::Size track_size = theme->GetPartSize( | |
399 ui::NativeTheme::kScrollbarVerticalThumb, | |
400 ui::NativeTheme::kNormal, | |
401 thumb_params); | |
402 | |
403 return std::max(track_size.width(), button_size.width()); | |
404 } | |
405 | |
406 } // namespace views | |
OLD | NEW |