OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef VIEWS_CONTROLS_SCROLLBAR_BITMAP_SCROLL_BAR_H_ | 5 #ifndef VIEWS_CONTROLS_SCROLLBAR_BASE_SCROLL_BAR_H_ |
6 #define VIEWS_CONTROLS_SCROLLBAR_BITMAP_SCROLL_BAR_H_ | 6 #define VIEWS_CONTROLS_SCROLLBAR_BASE_SCROLL_BAR_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "views/context_menu_controller.h" | 9 #include "views/context_menu_controller.h" |
10 #include "views/controls/button/image_button.h" | 10 #include "views/controls/button/image_button.h" |
11 #include "views/controls/menu/menu.h" | 11 #include "views/controls/menu/menu.h" |
12 #include "views/controls/scrollbar/scroll_bar.h" | 12 #include "views/controls/scrollbar/scroll_bar.h" |
13 #include "views/repeat_controller.h" | 13 #include "views/repeat_controller.h" |
14 | 14 |
15 namespace views { | 15 namespace views { |
16 | 16 |
17 namespace { | 17 class BaseScrollBarThumb; |
18 class BitmapScrollBarThumb; | |
19 } | |
20 | 18 |
21 /////////////////////////////////////////////////////////////////////////////// | 19 /////////////////////////////////////////////////////////////////////////////// |
22 // | 20 // |
23 // BitmapScrollBar | 21 // BaseScrollBar |
24 // | |
25 // A ScrollBar subclass that implements a scroll bar rendered using bitmaps | |
26 // that the user provides. There are bitmaps for the up and down buttons, as | |
27 // well as for the thumb and track. This is intended for creating UIs that | |
28 // have customized, non-native appearances, like floating HUDs etc. | |
29 // | |
30 // Maybe TODO(beng): (Cleanup) If we need to, we may want to factor rendering | |
31 // out of this altogether and have the user supply | |
32 // Background impls for each component, and just use those | |
33 // to render, so that for example we get native theme | |
34 // rendering. | |
35 // | 22 // |
36 /////////////////////////////////////////////////////////////////////////////// | 23 /////////////////////////////////////////////////////////////////////////////// |
37 class BitmapScrollBar : public ScrollBar, | 24 class VIEWS_EXPORT BaseScrollBar : public ScrollBar, |
38 public ButtonListener, | 25 public ContextMenuController, |
39 public ContextMenuController, | 26 public Menu::Delegate { |
40 public Menu::Delegate { | |
41 public: | 27 public: |
42 BitmapScrollBar(bool horizontal, bool show_scroll_buttons); | 28 BaseScrollBar(bool horizontal, BaseScrollBarThumb* thumb); |
43 virtual ~BitmapScrollBar() { } | 29 virtual ~BaseScrollBar() { } |
44 | 30 |
45 // Get the bounds of the "track" area that the thumb is free to slide within. | 31 // Get the bounds of the "track" area that the thumb is free to slide within. |
46 gfx::Rect GetTrackBounds() const; | 32 virtual gfx::Rect GetTrackBounds() const = 0; |
47 | |
48 // A list of parts that the user may supply bitmaps for. | |
49 enum ScrollBarPart { | |
50 // The button used to represent scrolling up/left by 1 line. | |
51 PREV_BUTTON = 0, | |
52 // The button used to represent scrolling down/right by 1 line. | |
53 // IMPORTANT: The code assumes the prev and next | |
54 // buttons have equal width and equal height. | |
55 NEXT_BUTTON, | |
56 // The top/left segment of the thumb on the scrollbar. | |
57 THUMB_START_CAP, | |
58 // The tiled background image of the thumb. | |
59 THUMB_MIDDLE, | |
60 // The bottom/right segment of the thumb on the scrollbar. | |
61 THUMB_END_CAP, | |
62 // The grippy that is rendered in the center of the thumb. | |
63 THUMB_GRIPPY, | |
64 // The tiled background image of the thumb track. | |
65 THUMB_TRACK, | |
66 PART_COUNT | |
67 }; | |
68 | |
69 // Sets the bitmap to be rendered for the specified part and state. | |
70 void SetImage(ScrollBarPart part, | |
71 CustomButton::ButtonState state, | |
72 SkBitmap* bitmap); | |
73 | 33 |
74 // An enumeration of different amounts of incremental scroll, representing | 34 // An enumeration of different amounts of incremental scroll, representing |
75 // events sent from different parts of the UI/keyboard. | 35 // events sent from different parts of the UI/keyboard. |
76 enum ScrollAmount { | 36 enum ScrollAmount { |
77 SCROLL_NONE = 0, | 37 SCROLL_NONE = 0, |
78 SCROLL_START, | 38 SCROLL_START, |
79 SCROLL_END, | 39 SCROLL_END, |
80 SCROLL_PREV_LINE, | 40 SCROLL_PREV_LINE, |
81 SCROLL_NEXT_LINE, | 41 SCROLL_NEXT_LINE, |
82 SCROLL_PREV_PAGE, | 42 SCROLL_PREV_PAGE, |
83 SCROLL_NEXT_PAGE, | 43 SCROLL_NEXT_PAGE, |
84 }; | 44 }; |
85 | 45 |
86 // Scroll the contents by the specified type (see ScrollAmount above). | 46 // Scroll the contents by the specified type (see ScrollAmount above). |
87 void ScrollByAmount(ScrollAmount amount); | 47 void ScrollByAmount(ScrollAmount amount); |
88 | 48 |
89 // Scroll the contents to the appropriate position given the supplied | 49 // Scroll the contents to the appropriate position given the supplied |
90 // position of the thumb (thumb track coordinates). If |scroll_to_middle| is | 50 // position of the thumb (thumb track coordinates). If |scroll_to_middle| is |
91 // true, then the conversion assumes |thumb_position| is in the middle of the | 51 // true, then the conversion assumes |thumb_position| is in the middle of the |
92 // thumb rather than the top. | 52 // thumb rather than the top. |
93 void ScrollToThumbPosition(int thumb_position, bool scroll_to_middle); | 53 void ScrollToThumbPosition(int thumb_position, bool scroll_to_middle); |
94 | 54 |
95 // Scroll the contents by the specified offset (contents coordinates). | 55 // Scroll the contents by the specified offset (contents coordinates). |
96 void ScrollByContentsOffset(int contents_offset); | 56 void ScrollByContentsOffset(int contents_offset); |
97 | 57 |
98 // View overrides: | 58 // View overrides: |
99 virtual gfx::Size GetPreferredSize() OVERRIDE; | 59 virtual gfx::Size GetPreferredSize() OVERRIDE = 0; |
100 virtual void Layout() OVERRIDE; | 60 virtual void Layout() OVERRIDE = 0; |
101 virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE; | 61 virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE; |
102 virtual void OnMouseReleased(const MouseEvent& event) OVERRIDE; | 62 virtual void OnMouseReleased(const MouseEvent& event) OVERRIDE; |
103 virtual void OnMouseCaptureLost() OVERRIDE; | 63 virtual void OnMouseCaptureLost() OVERRIDE; |
104 virtual bool OnKeyPressed(const KeyEvent& event) OVERRIDE; | 64 virtual bool OnKeyPressed(const KeyEvent& event) OVERRIDE; |
105 virtual bool OnMouseWheel(const MouseWheelEvent& event) OVERRIDE; | 65 virtual bool OnMouseWheel(const MouseWheelEvent& event) OVERRIDE; |
106 | 66 |
107 // BaseButton::ButtonListener overrides: | |
108 virtual void ButtonPressed(Button* sender, | |
109 const views::Event& event) OVERRIDE; | |
110 | |
111 // ScrollBar overrides: | 67 // ScrollBar overrides: |
112 virtual void Update(int viewport_size, | 68 virtual void Update(int viewport_size, |
113 int content_size, | 69 int content_size, |
114 int contents_scroll_offset) OVERRIDE; | 70 int contents_scroll_offset) OVERRIDE; |
115 virtual int GetLayoutSize() const OVERRIDE; | 71 virtual int GetLayoutSize() const OVERRIDE = 0; |
116 virtual int GetPosition() const OVERRIDE; | 72 virtual int GetPosition() const OVERRIDE; |
117 | 73 |
118 // ContextMenuController overrides. | 74 // ContextMenuController overrides. |
119 virtual void ShowContextMenuForView(View* source, | 75 virtual void ShowContextMenuForView(View* source, |
120 const gfx::Point& p, | 76 const gfx::Point& p, |
121 bool is_mouse_gesture) OVERRIDE; | 77 bool is_mouse_gesture) OVERRIDE; |
122 | 78 |
123 // Menu::Delegate overrides: | 79 // Menu::Delegate overrides: |
124 virtual std::wstring GetLabel(int id) const OVERRIDE; | 80 virtual std::wstring GetLabel(int id) const OVERRIDE; |
125 virtual bool IsCommandEnabled(int id) const OVERRIDE; | 81 virtual bool IsCommandEnabled(int id) const OVERRIDE; |
126 virtual void ExecuteCommand(int id) OVERRIDE; | 82 virtual void ExecuteCommand(int id) OVERRIDE; |
127 | 83 |
128 protected: | 84 protected: |
129 // View overrides: | 85 // View overrides: |
130 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | 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); |
131 | 99 |
132 private: | 100 private: |
133 // Called when the mouse is pressed down in the track area. | 101 // Called when the mouse is pressed down in the track area. |
134 void TrackClicked(); | 102 void TrackClicked(); |
135 | 103 |
136 // Responsible for scrolling the contents and also updating the UI to the | 104 // Responsible for scrolling the contents and also updating the UI to the |
137 // current value of the Scroll Offset. | 105 // current value of the Scroll Offset. |
138 void ScrollContentsToOffset(); | 106 void ScrollContentsToOffset(); |
139 | 107 |
140 // Returns the size (width or height) of the track area of the ScrollBar. | 108 // Returns the size (width or height) of the track area of the ScrollBar. |
141 int GetTrackSize() const; | 109 int GetTrackSize() const; |
142 | 110 |
143 // Calculate the position of the thumb within the track based on the | 111 // Calculate the position of the thumb within the track based on the |
144 // specified scroll offset of the contents. | 112 // specified scroll offset of the contents. |
145 int CalculateThumbPosition(int contents_scroll_offset) const; | 113 int CalculateThumbPosition(int contents_scroll_offset) const; |
146 | 114 |
147 // Calculates the current value of the contents offset (contents coordinates) | 115 // Calculates the current value of the contents offset (contents coordinates) |
148 // based on the current thumb position (thumb track coordinates). See | 116 // based on the current thumb position (thumb track coordinates). See |
149 // |ScrollToThumbPosition| for an explanation of |scroll_to_middle|. | 117 // |ScrollToThumbPosition| for an explanation of |scroll_to_middle|. |
150 int CalculateContentsOffset(int thumb_position, | 118 int CalculateContentsOffset(int thumb_position, |
151 bool scroll_to_middle) const; | 119 bool scroll_to_middle) const; |
152 | 120 |
153 // Called when the state of the thumb track changes (e.g. by the user | 121 // Called when the state of the thumb track changes (e.g. by the user |
154 // pressing the mouse button down in it). | 122 // pressing the mouse button down in it). |
155 void SetThumbTrackState(CustomButton::ButtonState state); | 123 void SetThumbTrackState(CustomButton::ButtonState state); |
156 | 124 |
157 // The thumb needs to be able to access the part images. | 125 BaseScrollBarThumb* thumb_; |
158 friend BitmapScrollBarThumb; | |
159 SkBitmap* images_[PART_COUNT][CustomButton::BS_COUNT]; | |
160 | 126 |
161 // The size of the scrolled contents, in pixels. | 127 // The size of the scrolled contents, in pixels. |
162 int contents_size_; | 128 int contents_size_; |
163 | 129 |
164 // The current amount the contents is offset by in the viewport. | 130 // The current amount the contents is offset by in the viewport. |
165 int contents_scroll_offset_; | 131 int contents_scroll_offset_; |
166 | 132 |
167 // Up/Down/Left/Right buttons and the Thumb. | |
168 ImageButton* prev_button_; | |
169 ImageButton* next_button_; | |
170 BitmapScrollBarThumb* thumb_; | |
171 | |
172 // The state of the scrollbar track. Typically, the track will highlight when | 133 // The state of the scrollbar track. Typically, the track will highlight when |
173 // the user presses the mouse on them (during page scrolling). | 134 // the user presses the mouse on them (during page scrolling). |
174 CustomButton::ButtonState thumb_track_state_; | 135 CustomButton::ButtonState thumb_track_state_; |
175 | 136 |
176 // The last amount of incremental scroll that this scrollbar performed. This | 137 // The last amount of incremental scroll that this scrollbar performed. This |
177 // is accessed by the callbacks for the auto-repeat up/down buttons to know | 138 // is accessed by the callbacks for the auto-repeat up/down buttons to know |
178 // what direction to repeatedly scroll in. | 139 // what direction to repeatedly scroll in. |
179 ScrollAmount last_scroll_amount_; | 140 ScrollAmount last_scroll_amount_; |
180 | 141 |
181 // An instance of a RepeatController which scrolls the scrollbar continuously | 142 // An instance of a RepeatController which scrolls the scrollbar continuously |
182 // as the user presses the mouse button down on the up/down buttons or the | 143 // as the user presses the mouse button down on the up/down buttons or the |
183 // track. | 144 // track. |
184 RepeatController repeater_; | 145 RepeatController repeater_; |
185 | 146 |
186 // The position of the mouse within the scroll bar when the context menu | 147 // The position of the mouse within the scroll bar when the context menu |
187 // was invoked. | 148 // was invoked. |
188 int context_menu_mouse_position_; | 149 int context_menu_mouse_position_; |
189 | 150 |
190 // True if the scroll buttons at each end of the scroll bar should be shown. | 151 DISALLOW_COPY_AND_ASSIGN(BaseScrollBar); |
191 bool show_scroll_buttons_; | |
192 | |
193 DISALLOW_COPY_AND_ASSIGN(BitmapScrollBar); | |
194 }; | 152 }; |
195 | 153 |
196 } // namespace views | 154 } // namespace views |
197 | 155 |
198 #endif // VIEWS_CONTROLS_SCROLLBAR_BITMAP_SCROLL_BAR_H_ | 156 #endif // VIEWS_CONTROLS_SCROLLBAR_BASE_SCROLL_BAR_H_ |
OLD | NEW |