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