GN: Fix clean build of cloud_policy_proto_generated_compile_proto failing
[chromium-blink-merge.git] / ui / views / controls / scroll_view.h
blobc90005b3a4706bf488d0d7090d0fb347e9fc9d8b
1 // Copyright (c) 2012 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.
5 #ifndef UI_VIEWS_CONTROLS_SCROLL_VIEW_H_
6 #define UI_VIEWS_CONTROLS_SCROLL_VIEW_H_
8 #include <string>
10 #include "base/compiler_specific.h"
11 #include "ui/views/controls/scrollbar/scroll_bar.h"
13 namespace views {
15 /////////////////////////////////////////////////////////////////////////////
17 // ScrollView class
19 // A ScrollView is used to make any View scrollable. The view is added to
20 // a viewport which takes care of clipping.
22 // In this current implementation both horizontal and vertical scrollbars are
23 // added as needed.
25 // The scrollview supports keyboard UI and mousewheel.
27 /////////////////////////////////////////////////////////////////////////////
29 class VIEWS_EXPORT ScrollView : public View, public ScrollBarController {
30 public:
31 static const char kViewClassName[];
33 ScrollView();
35 ~ScrollView() override;
37 // Creates a ScrollView with a theme specific border.
38 static ScrollView* CreateScrollViewWithBorder();
40 // Set the contents. Any previous contents will be deleted. The contents
41 // is the view that needs to scroll.
42 void SetContents(View* a_view);
43 const View* contents() const { return contents_; }
44 View* contents() { return contents_; }
46 // Sets the header, deleting the previous header.
47 void SetHeader(View* header);
49 // Returns the visible region of the content View.
50 gfx::Rect GetVisibleRect() const;
52 void set_hide_horizontal_scrollbar(bool visible) {
53 hide_horizontal_scrollbar_ = visible;
56 // Turns this scroll view into a bounded scroll view, with a fixed height.
57 // By default, a ScrollView will stretch to fill its outer container.
58 void ClipHeightTo(int min_height, int max_height);
60 // Returns whether or not the ScrollView is bounded (as set by ClipHeightTo).
61 bool is_bounded() const { return max_height_ >= 0 && min_height_ >= 0; }
63 // Retrieves the width/height of scrollbars. These return 0 if the scrollbar
64 // has not yet been created.
65 int GetScrollBarWidth() const;
66 int GetScrollBarHeight() const;
68 // Returns the horizontal/vertical scrollbar. This may return NULL.
69 const ScrollBar* horizontal_scroll_bar() const { return horiz_sb_; }
70 const ScrollBar* vertical_scroll_bar() const { return vert_sb_; }
72 // Customize the scrollbar design. ScrollView takes the ownership of the
73 // specified ScrollBar. |horiz_sb| and |vert_sb| cannot be NULL.
74 void SetHorizontalScrollBar(ScrollBar* horiz_sb);
75 void SetVerticalScrollBar(ScrollBar* vert_sb);
77 // View overrides:
78 gfx::Size GetPreferredSize() const override;
79 int GetHeightForWidth(int width) const override;
80 void Layout() override;
81 bool OnKeyPressed(const ui::KeyEvent& event) override;
82 bool OnMouseWheel(const ui::MouseWheelEvent& e) override;
83 void OnMouseEntered(const ui::MouseEvent& event) override;
84 void OnMouseExited(const ui::MouseEvent& event) override;
85 void OnGestureEvent(ui::GestureEvent* event) override;
86 const char* GetClassName() const override;
88 // ScrollBarController overrides:
89 void ScrollToPosition(ScrollBar* source, int position) override;
90 int GetScrollIncrement(ScrollBar* source,
91 bool is_page,
92 bool is_positive) override;
94 private:
95 FRIEND_TEST_ALL_PREFIXES(ScrollViewTest, CornerViewVisibility);
96 class Viewport;
98 // Used internally by SetHeader() and SetContents() to reset the view. Sets
99 // |member| to |new_view|. If |new_view| is non-null it is added to |parent|.
100 void SetHeaderOrContents(View* parent, View* new_view, View** member);
102 // Scrolls the minimum amount necessary to make the specified rectangle
103 // visible, in the coordinates of the contents view. The specified rectangle
104 // is constrained by the bounds of the contents view. This has no effect if
105 // the contents have not been set.
106 void ScrollContentsRegionToBeVisible(const gfx::Rect& rect);
108 // Computes the visibility of both scrollbars, taking in account the view port
109 // and content sizes.
110 void ComputeScrollBarsVisibility(const gfx::Size& viewport_size,
111 const gfx::Size& content_size,
112 bool* horiz_is_shown,
113 bool* vert_is_shown) const;
115 // Shows or hides the scrollbar/corner_view based on the value of
116 // |should_show|.
117 void SetControlVisibility(View* control, bool should_show);
119 // Update the scrollbars positions given viewport and content sizes.
120 void UpdateScrollBarPositions();
122 // The current contents and its viewport. |contents_| is contained in
123 // |contents_viewport_|.
124 View* contents_;
125 View* contents_viewport_;
127 // The current header and its viewport. |header_| is contained in
128 // |header_viewport_|.
129 View* header_;
130 View* header_viewport_;
132 // Horizontal scrollbar.
133 ScrollBar* horiz_sb_;
135 // Vertical scrollbar.
136 ScrollBar* vert_sb_;
138 // Corner view.
139 View* corner_view_;
141 // The min and max height for the bounded scroll view. These are negative
142 // values if the view is not bounded.
143 int min_height_;
144 int max_height_;
146 // If true, never show the horizontal scrollbar (even if the contents is wider
147 // than the viewport).
148 bool hide_horizontal_scrollbar_;
150 DISALLOW_COPY_AND_ASSIGN(ScrollView);
153 // VariableRowHeightScrollHelper is intended for views that contain rows of
154 // varying height. To use a VariableRowHeightScrollHelper create one supplying
155 // a Controller and delegate GetPageScrollIncrement and GetLineScrollIncrement
156 // to the helper. VariableRowHeightScrollHelper calls back to the
157 // Controller to determine row boundaries.
158 class VariableRowHeightScrollHelper {
159 public:
160 // The origin and height of a row.
161 struct RowInfo {
162 RowInfo(int origin, int height) : origin(origin), height(height) {}
164 // Origin of the row.
165 int origin;
167 // Height of the row.
168 int height;
171 // Used to determine row boundaries.
172 class Controller {
173 public:
174 // Returns the origin and size of the row at the specified location.
175 virtual VariableRowHeightScrollHelper::RowInfo GetRowInfo(int y) = 0;
178 // Creates a new VariableRowHeightScrollHelper. Controller is
179 // NOT deleted by this VariableRowHeightScrollHelper.
180 explicit VariableRowHeightScrollHelper(Controller* controller);
181 virtual ~VariableRowHeightScrollHelper();
183 // Delegate the View methods of the same name to these. The scroll amount is
184 // determined by querying the Controller for the appropriate row to scroll
185 // to.
186 int GetPageScrollIncrement(ScrollView* scroll_view,
187 bool is_horizontal, bool is_positive);
188 int GetLineScrollIncrement(ScrollView* scroll_view,
189 bool is_horizontal, bool is_positive);
191 protected:
192 // Returns the row information for the row at the specified location. This
193 // calls through to the method of the same name on the controller.
194 virtual RowInfo GetRowInfo(int y);
196 private:
197 Controller* controller_;
199 DISALLOW_COPY_AND_ASSIGN(VariableRowHeightScrollHelper);
202 // FixedRowHeightScrollHelper is intended for views that contain fixed height
203 // height rows. To use a FixedRowHeightScrollHelper delegate
204 // GetPageScrollIncrement and GetLineScrollIncrement to it.
205 class FixedRowHeightScrollHelper : public VariableRowHeightScrollHelper {
206 public:
207 // Creates a FixedRowHeightScrollHelper. top_margin gives the distance from
208 // the top of the view to the first row, and may be 0. row_height gives the
209 // height of each row.
210 FixedRowHeightScrollHelper(int top_margin, int row_height);
212 protected:
213 // Calculates the bounds of the row from the top margin and row height.
214 RowInfo GetRowInfo(int y) override;
216 private:
217 int top_margin_;
218 int row_height_;
220 DISALLOW_COPY_AND_ASSIGN(FixedRowHeightScrollHelper);
223 } // namespace views
225 #endif // UI_VIEWS_CONTROLS_SCROLL_VIEW_H_