Chromium Code Reviews| 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 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 virtual void ScrollToPosition(int position); | |
|
Ben Goodger (Google)
2011/08/23 16:05:15
Are these overrides or introduced here? If the lat
| |
| 91 virtual int GetScrollIncrement(bool is_page, bool is_positive); | |
| 131 | 92 |
| 132 private: | 93 private: |
| 133 // Called when the mouse is pressed down in the track area. | 94 // Called when the mouse is pressed down in the track area. |
| 134 void TrackClicked(); | 95 void TrackClicked(); |
| 135 | 96 |
| 136 // Responsible for scrolling the contents and also updating the UI to the | 97 // Responsible for scrolling the contents and also updating the UI to the |
| 137 // current value of the Scroll Offset. | 98 // current value of the Scroll Offset. |
| 138 void ScrollContentsToOffset(); | 99 void ScrollContentsToOffset(); |
| 139 | 100 |
| 140 // Returns the size (width or height) of the track area of the ScrollBar. | 101 // Returns the size (width or height) of the track area of the ScrollBar. |
| 141 int GetTrackSize() const; | 102 int GetTrackSize() const; |
| 142 | 103 |
| 143 // Calculate the position of the thumb within the track based on the | 104 // Calculate the position of the thumb within the track based on the |
| 144 // specified scroll offset of the contents. | 105 // specified scroll offset of the contents. |
| 145 int CalculateThumbPosition(int contents_scroll_offset) const; | 106 int CalculateThumbPosition(int contents_scroll_offset) const; |
| 146 | 107 |
| 147 // Calculates the current value of the contents offset (contents coordinates) | 108 // Calculates the current value of the contents offset (contents coordinates) |
| 148 // based on the current thumb position (thumb track coordinates). See | 109 // based on the current thumb position (thumb track coordinates). See |
| 149 // |ScrollToThumbPosition| for an explanation of |scroll_to_middle|. | 110 // |ScrollToThumbPosition| for an explanation of |scroll_to_middle|. |
| 150 int CalculateContentsOffset(int thumb_position, | 111 int CalculateContentsOffset(int thumb_position, |
| 151 bool scroll_to_middle) const; | 112 bool scroll_to_middle) const; |
| 152 | 113 |
| 153 // Called when the state of the thumb track changes (e.g. by the user | 114 // Called when the state of the thumb track changes (e.g. by the user |
| 154 // pressing the mouse button down in it). | 115 // pressing the mouse button down in it). |
| 155 void SetThumbTrackState(CustomButton::ButtonState state); | 116 void SetThumbTrackState(CustomButton::ButtonState state); |
| 156 | 117 |
| 157 // The thumb needs to be able to access the part images. | 118 BaseScrollBarThumb* thumb_; |
| 158 friend BitmapScrollBarThumb; | |
| 159 SkBitmap* images_[PART_COUNT][CustomButton::BS_COUNT]; | |
| 160 | 119 |
| 161 // The size of the scrolled contents, in pixels. | 120 // The size of the scrolled contents, in pixels. |
| 162 int contents_size_; | 121 int contents_size_; |
| 163 | 122 |
| 164 // The current amount the contents is offset by in the viewport. | 123 // The current amount the contents is offset by in the viewport. |
| 165 int contents_scroll_offset_; | 124 int contents_scroll_offset_; |
| 166 | 125 |
| 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 | 126 // The state of the scrollbar track. Typically, the track will highlight when |
| 173 // the user presses the mouse on them (during page scrolling). | 127 // the user presses the mouse on them (during page scrolling). |
| 174 CustomButton::ButtonState thumb_track_state_; | 128 CustomButton::ButtonState thumb_track_state_; |
| 175 | 129 |
| 176 // The last amount of incremental scroll that this scrollbar performed. This | 130 // 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 | 131 // is accessed by the callbacks for the auto-repeat up/down buttons to know |
| 178 // what direction to repeatedly scroll in. | 132 // what direction to repeatedly scroll in. |
| 179 ScrollAmount last_scroll_amount_; | 133 ScrollAmount last_scroll_amount_; |
| 180 | 134 |
| 181 // An instance of a RepeatController which scrolls the scrollbar continuously | 135 // 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 | 136 // as the user presses the mouse button down on the up/down buttons or the |
| 183 // track. | 137 // track. |
| 184 RepeatController repeater_; | 138 RepeatController repeater_; |
| 185 | 139 |
| 186 // The position of the mouse within the scroll bar when the context menu | 140 // The position of the mouse within the scroll bar when the context menu |
| 187 // was invoked. | 141 // was invoked. |
| 188 int context_menu_mouse_position_; | 142 int context_menu_mouse_position_; |
| 189 | 143 |
| 190 // True if the scroll buttons at each end of the scroll bar should be shown. | 144 DISALLOW_COPY_AND_ASSIGN(BaseScrollBar); |
| 191 bool show_scroll_buttons_; | |
| 192 | |
| 193 DISALLOW_COPY_AND_ASSIGN(BitmapScrollBar); | |
| 194 }; | 145 }; |
| 195 | 146 |
| 196 } // namespace views | 147 } // namespace views |
| 197 | 148 |
| 198 #endif // VIEWS_CONTROLS_SCROLLBAR_BITMAP_SCROLL_BAR_H_ | 149 #endif // VIEWS_CONTROLS_SCROLLBAR_BASE_SCROLL_BAR_H_ |
| OLD | NEW |