| 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/context_menu_controller.h" | 9 #include "views/controls/scrollbar/base_scroll_bar.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 | 10 |
| 15 namespace views { | 11 namespace views { |
| 16 | 12 |
| 17 namespace { | 13 namespace { |
| 18 class BitmapScrollBarThumb; | 14 class BitmapScrollBarThumb; |
| 19 } | 15 } |
| 20 | 16 |
| 21 /////////////////////////////////////////////////////////////////////////////// | 17 /////////////////////////////////////////////////////////////////////////////// |
| 22 // | 18 // |
| 23 // BitmapScrollBar | 19 // BitmapScrollBar |
| 24 // | 20 // |
| 25 // A ScrollBar subclass that implements a scroll bar rendered using bitmaps | 21 // 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 | 22 // 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 | 23 // well as for the thumb and track. This is intended for creating UIs that |
| 28 // have customized, non-native appearances, like floating HUDs etc. | 24 // have customized, non-native appearances, like floating HUDs etc. |
| 29 // | 25 // |
| 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 // | |
| 36 /////////////////////////////////////////////////////////////////////////////// | 26 /////////////////////////////////////////////////////////////////////////////// |
| 37 class BitmapScrollBar : public ScrollBar, | 27 class VIEWS_EXPORT BitmapScrollBar : public BaseScrollBar, |
| 38 public ButtonListener, | 28 public ButtonListener { |
| 39 public ContextMenuController, | |
| 40 public Menu::Delegate { | |
| 41 public: | 29 public: |
| 42 BitmapScrollBar(bool horizontal, bool show_scroll_buttons); | 30 BitmapScrollBar(bool horizontal, bool show_scroll_buttons); |
| 43 virtual ~BitmapScrollBar() { } | 31 virtual ~BitmapScrollBar() { } |
| 44 | 32 |
| 45 // Get the bounds of the "track" area that the thumb is free to slide within. | |
| 46 gfx::Rect GetTrackBounds() const; | |
| 47 | |
| 48 // A list of parts that the user may supply bitmaps for. | 33 // A list of parts that the user may supply bitmaps for. |
| 49 enum ScrollBarPart { | 34 enum ScrollBarPart { |
| 50 // The button used to represent scrolling up/left by 1 line. | 35 // The button used to represent scrolling up/left by 1 line. |
| 51 PREV_BUTTON = 0, | 36 PREV_BUTTON = 0, |
| 52 // The button used to represent scrolling down/right by 1 line. | 37 // The button used to represent scrolling down/right by 1 line. |
| 53 // IMPORTANT: The code assumes the prev and next | 38 // IMPORTANT: The code assumes the prev and next |
| 54 // buttons have equal width and equal height. | 39 // buttons have equal width and equal height. |
| 55 NEXT_BUTTON, | 40 NEXT_BUTTON, |
| 56 // The top/left segment of the thumb on the scrollbar. | 41 // The top/left segment of the thumb on the scrollbar. |
| 57 THUMB_START_CAP, | 42 THUMB_START_CAP, |
| 58 // The tiled background image of the thumb. | 43 // The tiled background image of the thumb. |
| 59 THUMB_MIDDLE, | 44 THUMB_MIDDLE, |
| 60 // The bottom/right segment of the thumb on the scrollbar. | 45 // The bottom/right segment of the thumb on the scrollbar. |
| 61 THUMB_END_CAP, | 46 THUMB_END_CAP, |
| 62 // The grippy that is rendered in the center of the thumb. | 47 // The grippy that is rendered in the center of the thumb. |
| 63 THUMB_GRIPPY, | 48 THUMB_GRIPPY, |
| 64 // The tiled background image of the thumb track. | 49 // The tiled background image of the thumb track. |
| 65 THUMB_TRACK, | 50 THUMB_TRACK, |
| 66 PART_COUNT | 51 PART_COUNT |
| 67 }; | 52 }; |
| 68 | 53 |
| 69 // Sets the bitmap to be rendered for the specified part and state. | 54 // Sets the bitmap to be rendered for the specified part and state. |
| 70 void SetImage(ScrollBarPart part, | 55 void SetImage(ScrollBarPart part, |
| 71 CustomButton::ButtonState state, | 56 CustomButton::ButtonState state, |
| 72 SkBitmap* bitmap); | 57 SkBitmap* bitmap); |
| 73 | 58 |
| 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 }; | |
| 85 | 59 |
| 86 // Scroll the contents by the specified type (see ScrollAmount above). | 60 gfx::Rect GetTrackBounds() const; |
| 87 void ScrollByAmount(ScrollAmount amount); | |
| 88 | 61 |
| 89 // Scroll the contents to the appropriate position given the supplied | 62 protected: |
| 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 | |
| 98 // View overrides: | 63 // View overrides: |
| 99 virtual gfx::Size GetPreferredSize() OVERRIDE; | 64 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 100 virtual void Layout() OVERRIDE; | 65 virtual void Layout() OVERRIDE; |
| 101 virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE; | 66 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
| 102 virtual void OnMouseReleased(const MouseEvent& event) OVERRIDE; | 67 |
| 103 virtual void OnMouseCaptureLost() OVERRIDE; | 68 // ScrollBar overrides: |
| 104 virtual bool OnKeyPressed(const KeyEvent& event) OVERRIDE; | 69 virtual int GetLayoutSize() const OVERRIDE; |
| 105 virtual bool OnMouseWheel(const MouseWheelEvent& event) OVERRIDE; | |
| 106 | 70 |
| 107 // BaseButton::ButtonListener overrides: | 71 // BaseButton::ButtonListener overrides: |
| 108 virtual void ButtonPressed(Button* sender, | 72 virtual void ButtonPressed(Button* sender, |
| 109 const views::Event& event) OVERRIDE; | 73 const views::Event& event) OVERRIDE; |
| 110 | 74 |
| 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 | |
| 132 private: | 75 private: |
| 133 // Called when the mouse is pressed down in the track area. | 76 // Up/Down/Left/Right buttons. |
| 134 void TrackClicked(); | 77 ImageButton* prev_button_; |
| 135 | 78 ImageButton* next_button_; |
| 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); | |
| 156 | 79 |
| 157 // The thumb needs to be able to access the part images. | 80 // The thumb needs to be able to access the part images. |
| 158 friend BitmapScrollBarThumb; | 81 friend BitmapScrollBarThumb; |
| 159 SkBitmap* images_[PART_COUNT][CustomButton::BS_COUNT]; | 82 SkBitmap* images_[PART_COUNT][CustomButton::BS_COUNT]; |
| 160 | 83 |
| 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 | |
| 190 // True if the scroll buttons at each end of the scroll bar should be shown. | 84 // True if the scroll buttons at each end of the scroll bar should be shown. |
| 191 bool show_scroll_buttons_; | 85 bool show_scroll_buttons_; |
| 192 | 86 |
| 193 DISALLOW_COPY_AND_ASSIGN(BitmapScrollBar); | 87 DISALLOW_COPY_AND_ASSIGN(BitmapScrollBar); |
| 194 }; | 88 }; |
| 195 | 89 |
| 196 } // namespace views | 90 } // namespace views |
| 197 | 91 |
| 198 #endif // VIEWS_CONTROLS_SCROLLBAR_BITMAP_SCROLL_BAR_H_ | 92 #endif // VIEWS_CONTROLS_SCROLLBAR_BITMAP_SCROLL_BAR_H_ |
| OLD | NEW |