| 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_SCROLL_VIEW_H_ | |
| 6 #define VIEWS_CONTROLS_SCROLL_VIEW_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "ui/views/controls/scrollbar/scroll_bar.h" | |
| 13 | |
| 14 namespace views { | |
| 15 | |
| 16 ///////////////////////////////////////////////////////////////////////////// | |
| 17 // | |
| 18 // ScrollView class | |
| 19 // | |
| 20 // A ScrollView is used to make any View scrollable. The view is added to | |
| 21 // a viewport which takes care of clipping. | |
| 22 // | |
| 23 // In this current implementation both horizontal and vertical scrollbars are | |
| 24 // added as needed. | |
| 25 // | |
| 26 // The scrollview supports keyboard UI and mousewheel. | |
| 27 // | |
| 28 ///////////////////////////////////////////////////////////////////////////// | |
| 29 | |
| 30 class VIEWS_EXPORT ScrollView : public View, public ScrollBarController { | |
| 31 public: | |
| 32 static const char* const kViewClassName; | |
| 33 | |
| 34 ScrollView(); | |
| 35 // Initialize with specific views. resize_corner is optional. | |
| 36 ScrollView(ScrollBar* horizontal_scrollbar, | |
| 37 ScrollBar* vertical_scrollbar, | |
| 38 View* resize_corner); | |
| 39 virtual ~ScrollView(); | |
| 40 | |
| 41 // Set the contents. Any previous contents will be deleted. The contents | |
| 42 // is the view that needs to scroll. | |
| 43 void SetContents(View* a_view); | |
| 44 View* GetContents() const; | |
| 45 | |
| 46 // Overridden to layout the viewport and scrollbars. | |
| 47 virtual void Layout() OVERRIDE; | |
| 48 | |
| 49 // Returns the visible region of the content View. | |
| 50 gfx::Rect GetVisibleRect() const; | |
| 51 | |
| 52 // Scrolls the minimum amount necessary to make the specified rectangle | |
| 53 // visible, in the coordinates of the contents view. The specified rectangle | |
| 54 // is constrained by the bounds of the contents view. This has no effect if | |
| 55 // the contents have not been set. | |
| 56 // | |
| 57 // Client code should use ScrollRectToVisible, which invokes this | |
| 58 // appropriately. | |
| 59 void ScrollContentsRegionToBeVisible(const gfx::Rect& rect); | |
| 60 | |
| 61 // ScrollBarController. | |
| 62 // NOTE: this is intended to be invoked by the ScrollBar, and NOT general | |
| 63 // client code. | |
| 64 // See also ScrollRectToVisible. | |
| 65 virtual void ScrollToPosition(ScrollBar* source, int position) OVERRIDE; | |
| 66 | |
| 67 // Returns the amount to scroll relative to the visible bounds. This invokes | |
| 68 // either GetPageScrollIncrement or GetLineScrollIncrement to determine the | |
| 69 // amount to scroll. If the view returns 0 (or a negative value) a default | |
| 70 // value is used. | |
| 71 virtual int GetScrollIncrement(ScrollBar* source, | |
| 72 bool is_page, | |
| 73 bool is_positive) OVERRIDE; | |
| 74 | |
| 75 // Keyboard events | |
| 76 virtual bool OnKeyPressed(const KeyEvent& event) OVERRIDE; | |
| 77 virtual bool OnMouseWheel(const MouseWheelEvent& e) OVERRIDE; | |
| 78 | |
| 79 virtual std::string GetClassName() const OVERRIDE; | |
| 80 | |
| 81 // Retrieves the vertical scrollbar width. | |
| 82 int GetScrollBarWidth() const; | |
| 83 | |
| 84 // Retrieves the horizontal scrollbar height. | |
| 85 int GetScrollBarHeight() const; | |
| 86 | |
| 87 // Computes the visibility of both scrollbars, taking in account the view port | |
| 88 // and content sizes. | |
| 89 void ComputeScrollBarsVisibility(const gfx::Size& viewport_size, | |
| 90 const gfx::Size& content_size, | |
| 91 bool* horiz_is_shown, | |
| 92 bool* vert_is_shown) const; | |
| 93 | |
| 94 ScrollBar* horizontal_scroll_bar() const { return horiz_sb_; } | |
| 95 | |
| 96 ScrollBar* vertical_scroll_bar() const { return vert_sb_; } | |
| 97 | |
| 98 private: | |
| 99 // Initialize the ScrollView. resize_corner is optional. | |
| 100 void Init(ScrollBar* horizontal_scrollbar, | |
| 101 ScrollBar* vertical_scrollbar, | |
| 102 View* resize_corner); | |
| 103 | |
| 104 // Shows or hides the scrollbar/resize_corner based on the value of | |
| 105 // |should_show|. | |
| 106 void SetControlVisibility(View* control, bool should_show); | |
| 107 | |
| 108 // Update the scrollbars positions given viewport and content sizes. | |
| 109 void UpdateScrollBarPositions(); | |
| 110 | |
| 111 // Make sure the content is not scrolled out of bounds | |
| 112 void CheckScrollBounds(); | |
| 113 | |
| 114 // Make sure the content is not scrolled out of bounds in one dimension | |
| 115 int CheckScrollBounds(int viewport_size, int content_size, int current_pos); | |
| 116 | |
| 117 // The clipping viewport. Content is added to that view. | |
| 118 View* viewport_; | |
| 119 | |
| 120 // The current contents | |
| 121 View* contents_; | |
| 122 | |
| 123 // Horizontal scrollbar. | |
| 124 ScrollBar* horiz_sb_; | |
| 125 | |
| 126 // Vertical scrollbar. | |
| 127 ScrollBar* vert_sb_; | |
| 128 | |
| 129 // Resize corner. | |
| 130 View* resize_corner_; | |
| 131 | |
| 132 DISALLOW_COPY_AND_ASSIGN(ScrollView); | |
| 133 }; | |
| 134 | |
| 135 // VariableRowHeightScrollHelper is intended for views that contain rows of | |
| 136 // varying height. To use a VariableRowHeightScrollHelper create one supplying | |
| 137 // a Controller and delegate GetPageScrollIncrement and GetLineScrollIncrement | |
| 138 // to the helper. VariableRowHeightScrollHelper calls back to the | |
| 139 // Controller to determine row boundaries. | |
| 140 class VariableRowHeightScrollHelper { | |
| 141 public: | |
| 142 // The origin and height of a row. | |
| 143 struct RowInfo { | |
| 144 RowInfo(int origin, int height) : origin(origin), height(height) {} | |
| 145 | |
| 146 // Origin of the row. | |
| 147 int origin; | |
| 148 | |
| 149 // Height of the row. | |
| 150 int height; | |
| 151 }; | |
| 152 | |
| 153 // Used to determine row boundaries. | |
| 154 class Controller { | |
| 155 public: | |
| 156 // Returns the origin and size of the row at the specified location. | |
| 157 virtual VariableRowHeightScrollHelper::RowInfo GetRowInfo(int y) = 0; | |
| 158 }; | |
| 159 | |
| 160 // Creates a new VariableRowHeightScrollHelper. Controller is | |
| 161 // NOT deleted by this VariableRowHeightScrollHelper. | |
| 162 explicit VariableRowHeightScrollHelper(Controller* controller); | |
| 163 virtual ~VariableRowHeightScrollHelper(); | |
| 164 | |
| 165 // Delegate the View methods of the same name to these. The scroll amount is | |
| 166 // determined by querying the Controller for the appropriate row to scroll | |
| 167 // to. | |
| 168 int GetPageScrollIncrement(ScrollView* scroll_view, | |
| 169 bool is_horizontal, bool is_positive); | |
| 170 int GetLineScrollIncrement(ScrollView* scroll_view, | |
| 171 bool is_horizontal, bool is_positive); | |
| 172 | |
| 173 protected: | |
| 174 // Returns the row information for the row at the specified location. This | |
| 175 // calls through to the method of the same name on the controller. | |
| 176 virtual RowInfo GetRowInfo(int y); | |
| 177 | |
| 178 private: | |
| 179 Controller* controller_; | |
| 180 | |
| 181 DISALLOW_COPY_AND_ASSIGN(VariableRowHeightScrollHelper); | |
| 182 }; | |
| 183 | |
| 184 // FixedRowHeightScrollHelper is intended for views that contain fixed height | |
| 185 // height rows. To use a FixedRowHeightScrollHelper delegate | |
| 186 // GetPageScrollIncrement and GetLineScrollIncrement to it. | |
| 187 class FixedRowHeightScrollHelper : public VariableRowHeightScrollHelper { | |
| 188 public: | |
| 189 // Creates a FixedRowHeightScrollHelper. top_margin gives the distance from | |
| 190 // the top of the view to the first row, and may be 0. row_height gives the | |
| 191 // height of each row. | |
| 192 FixedRowHeightScrollHelper(int top_margin, int row_height); | |
| 193 | |
| 194 protected: | |
| 195 // Calculates the bounds of the row from the top margin and row height. | |
| 196 virtual RowInfo GetRowInfo(int y) OVERRIDE; | |
| 197 | |
| 198 private: | |
| 199 int top_margin_; | |
| 200 int row_height_; | |
| 201 | |
| 202 DISALLOW_COPY_AND_ASSIGN(FixedRowHeightScrollHelper); | |
| 203 }; | |
| 204 | |
| 205 } // namespace views | |
| 206 | |
| 207 #endif // VIEWS_CONTROLS_SCROLL_VIEW_H_ | |
| OLD | NEW |