OLD | NEW |
| (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 #ifndef VIEWS_CONTROLS_SCROLLBAR_BASE_SCROLL_BAR_H_ | |
6 #define VIEWS_CONTROLS_SCROLLBAR_BASE_SCROLL_BAR_H_ | |
7 #pragma once | |
8 | |
9 #include "views/context_menu_controller.h" | |
10 #include "views/controls/button/image_button.h" | |
11 #include "views/controls/menu/menu.h" | |
12 #include "views/controls/scrollbar/scroll_bar.h" | |
13 #include "views/repeat_controller.h" | |
14 | |
15 namespace views { | |
16 | |
17 class BaseScrollBarThumb; | |
18 | |
19 /////////////////////////////////////////////////////////////////////////////// | |
20 // | |
21 // BaseScrollBar | |
22 // | |
23 /////////////////////////////////////////////////////////////////////////////// | |
24 class VIEWS_EXPORT BaseScrollBar : public ScrollBar, | |
25 public ContextMenuController, | |
26 public Menu::Delegate { | |
27 public: | |
28 BaseScrollBar(bool horizontal, BaseScrollBarThumb* thumb); | |
29 virtual ~BaseScrollBar() { } | |
30 | |
31 // Get the bounds of the "track" area that the thumb is free to slide within. | |
32 virtual gfx::Rect GetTrackBounds() const = 0; | |
33 | |
34 // An enumeration of different amounts of incremental scroll, representing | |
35 // events sent from different parts of the UI/keyboard. | |
36 enum ScrollAmount { | |
37 SCROLL_NONE = 0, | |
38 SCROLL_START, | |
39 SCROLL_END, | |
40 SCROLL_PREV_LINE, | |
41 SCROLL_NEXT_LINE, | |
42 SCROLL_PREV_PAGE, | |
43 SCROLL_NEXT_PAGE, | |
44 }; | |
45 | |
46 // Scroll the contents by the specified type (see ScrollAmount above). | |
47 void ScrollByAmount(ScrollAmount amount); | |
48 | |
49 // Scroll the contents to the appropriate position given the supplied | |
50 // position of the thumb (thumb track coordinates). If |scroll_to_middle| is | |
51 // true, then the conversion assumes |thumb_position| is in the middle of the | |
52 // thumb rather than the top. | |
53 void ScrollToThumbPosition(int thumb_position, bool scroll_to_middle); | |
54 | |
55 // Scroll the contents by the specified offset (contents coordinates). | |
56 void ScrollByContentsOffset(int contents_offset); | |
57 | |
58 // View overrides: | |
59 virtual gfx::Size GetPreferredSize() OVERRIDE = 0; | |
60 virtual void Layout() OVERRIDE = 0; | |
61 virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE; | |
62 virtual void OnMouseReleased(const MouseEvent& event) OVERRIDE; | |
63 virtual void OnMouseCaptureLost() OVERRIDE; | |
64 virtual bool OnKeyPressed(const KeyEvent& event) OVERRIDE; | |
65 virtual bool OnMouseWheel(const MouseWheelEvent& event) OVERRIDE; | |
66 | |
67 // ScrollBar overrides: | |
68 virtual void Update(int viewport_size, | |
69 int content_size, | |
70 int contents_scroll_offset) OVERRIDE; | |
71 virtual int GetLayoutSize() const OVERRIDE = 0; | |
72 virtual int GetPosition() const OVERRIDE; | |
73 | |
74 // ContextMenuController overrides. | |
75 virtual void ShowContextMenuForView(View* source, | |
76 const gfx::Point& p, | |
77 bool is_mouse_gesture) OVERRIDE; | |
78 | |
79 // Menu::Delegate overrides: | |
80 virtual std::wstring GetLabel(int id) const OVERRIDE; | |
81 virtual bool IsCommandEnabled(int id) const OVERRIDE; | |
82 virtual void ExecuteCommand(int id) OVERRIDE; | |
83 | |
84 protected: | |
85 // View overrides: | |
86 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE = 0; | |
87 | |
88 BaseScrollBarThumb* GetThumb() const; | |
89 | |
90 CustomButton::ButtonState GetThumbTrackState() const; | |
91 | |
92 // Wrapper functions that calls the controller. We need this since native | |
93 // scrollbars wrap around a different scrollbar. When calling the controller | |
94 // we need to pass in the appropriate scrollbar. For normal scrollbars it's | |
95 // the |this| scrollbar, for native scrollbars it's the native scrollbar used | |
96 // to create this. | |
97 virtual void ScrollToPosition(int position); | |
98 virtual int GetScrollIncrement(bool is_page, bool is_positive); | |
99 | |
100 private: | |
101 // Called when the mouse is pressed down in the track area. | |
102 void TrackClicked(); | |
103 | |
104 // Responsible for scrolling the contents and also updating the UI to the | |
105 // current value of the Scroll Offset. | |
106 void ScrollContentsToOffset(); | |
107 | |
108 // Returns the size (width or height) of the track area of the ScrollBar. | |
109 int GetTrackSize() const; | |
110 | |
111 // Calculate the position of the thumb within the track based on the | |
112 // specified scroll offset of the contents. | |
113 int CalculateThumbPosition(int contents_scroll_offset) const; | |
114 | |
115 // Calculates the current value of the contents offset (contents coordinates) | |
116 // based on the current thumb position (thumb track coordinates). See | |
117 // |ScrollToThumbPosition| for an explanation of |scroll_to_middle|. | |
118 int CalculateContentsOffset(int thumb_position, | |
119 bool scroll_to_middle) const; | |
120 | |
121 // Called when the state of the thumb track changes (e.g. by the user | |
122 // pressing the mouse button down in it). | |
123 void SetThumbTrackState(CustomButton::ButtonState state); | |
124 | |
125 BaseScrollBarThumb* thumb_; | |
126 | |
127 // The size of the scrolled contents, in pixels. | |
128 int contents_size_; | |
129 | |
130 // The current amount the contents is offset by in the viewport. | |
131 int contents_scroll_offset_; | |
132 | |
133 // The state of the scrollbar track. Typically, the track will highlight when | |
134 // the user presses the mouse on them (during page scrolling). | |
135 CustomButton::ButtonState thumb_track_state_; | |
136 | |
137 // The last amount of incremental scroll that this scrollbar performed. This | |
138 // is accessed by the callbacks for the auto-repeat up/down buttons to know | |
139 // what direction to repeatedly scroll in. | |
140 ScrollAmount last_scroll_amount_; | |
141 | |
142 // An instance of a RepeatController which scrolls the scrollbar continuously | |
143 // as the user presses the mouse button down on the up/down buttons or the | |
144 // track. | |
145 RepeatController repeater_; | |
146 | |
147 // The position of the mouse within the scroll bar when the context menu | |
148 // was invoked. | |
149 int context_menu_mouse_position_; | |
150 | |
151 DISALLOW_COPY_AND_ASSIGN(BaseScrollBar); | |
152 }; | |
153 | |
154 } // namespace views | |
155 | |
156 #endif // VIEWS_CONTROLS_SCROLLBAR_BASE_SCROLL_BAR_H_ | |
OLD | NEW |