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