[email protected] | 3433825 | 2011-03-29 00:39:05 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 5 | #ifndef VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_ |
| 6 | #define VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 8 | |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 9 | #include "base/gtest_prod_util.h" |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 10 | #include "views/view.h" |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 11 | |
| 12 | namespace views { |
| 13 | |
[email protected] | 3d2fdb0 | 2011-10-25 18:42:25 | [diff] [blame] | 14 | class SingleSplitViewListener; |
| 15 | |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 16 | // SingleSplitView lays out two views next to each other, either horizontally |
| 17 | // or vertically. A splitter exists between the two views that the user can |
| 18 | // drag around to resize the views. |
[email protected] | 3d2fdb0 | 2011-10-25 18:42:25 | [diff] [blame] | 19 | // SingleSplitViewListener's SplitHandleMoved notification helps to monitor user |
| 20 | // initiated layout changes. |
[email protected] | 5036f18 | 2011-08-05 20:58:29 | [diff] [blame] | 21 | class VIEWS_EXPORT SingleSplitView : public View { |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 22 | public: |
[email protected] | ea36e32 | 2009-06-24 09:40:35 | [diff] [blame] | 23 | enum Orientation { |
| 24 | HORIZONTAL_SPLIT, |
| 25 | VERTICAL_SPLIT |
| 26 | }; |
| 27 | |
[email protected] | 6675395 | 2011-06-02 21:32:47 | [diff] [blame] | 28 | static const char kViewClassName[]; |
| 29 | |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 30 | SingleSplitView(View* leading, |
| 31 | View* trailing, |
| 32 | Orientation orientation, |
[email protected] | 3d2fdb0 | 2011-10-25 18:42:25 | [diff] [blame] | 33 | SingleSplitViewListener* listener); |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 34 | |
[email protected] | 8eb52a9a | 2011-03-09 16:52:00 | [diff] [blame] | 35 | virtual void Layout() OVERRIDE; |
[email protected] | 6675395 | 2011-06-02 21:32:47 | [diff] [blame] | 36 | virtual std::string GetClassName() const OVERRIDE; |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 37 | |
[email protected] | 79e549f | 2011-03-14 06:56:33 | [diff] [blame] | 38 | virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; |
[email protected] | 00854125 | 2010-03-16 00:24:20 | [diff] [blame] | 39 | |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 40 | // SingleSplitView's preferred size is the sum of the preferred widths |
| 41 | // and the max of the heights. |
[email protected] | 8eb52a9a | 2011-03-09 16:52:00 | [diff] [blame] | 42 | virtual gfx::Size GetPreferredSize() OVERRIDE; |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 43 | |
| 44 | // Overriden to return a resize cursor when over the divider. |
[email protected] | 3446c06 | 2011-05-03 21:48:00 | [diff] [blame] | 45 | virtual gfx::NativeCursor GetCursor(const MouseEvent& event) OVERRIDE; |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 46 | |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 47 | Orientation orientation() const { |
| 48 | return is_horizontal_ ? HORIZONTAL_SPLIT : VERTICAL_SPLIT; |
| 49 | } |
| 50 | |
[email protected] | ea36e32 | 2009-06-24 09:40:35 | [diff] [blame] | 51 | void set_divider_offset(int divider_offset) { |
| 52 | divider_offset_ = divider_offset; |
| 53 | } |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 54 | int divider_offset() const { return divider_offset_; } |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 55 | |
[email protected] | e46318e | 2009-07-09 14:13:08 | [diff] [blame] | 56 | // Sets whether the leading component is resized when the split views size |
| 57 | // changes. The default is true. A value of false results in the trailing |
| 58 | // component resizing on a bounds change. |
| 59 | void set_resize_leading_on_bounds_change(bool resize) { |
| 60 | resize_leading_on_bounds_change_ = resize; |
| 61 | } |
| 62 | |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 63 | // Calculates ideal leading and trailing view bounds according to the given |
| 64 | // split view |bounds|, current divider offset and children visiblity. |
| 65 | // Does not change children view bounds. |
| 66 | void CalculateChildrenBounds(const gfx::Rect& bounds, |
| 67 | gfx::Rect* leading_bounds, |
| 68 | gfx::Rect* trailing_bounds) const; |
| 69 | |
[email protected] | 79e549f | 2011-03-14 06:56:33 | [diff] [blame] | 70 | void SetAccessibleName(const string16& name); |
| 71 | |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 72 | protected: |
[email protected] | 8eb52a9a | 2011-03-09 16:52:00 | [diff] [blame] | 73 | // View overrides. |
| 74 | virtual bool OnMousePressed(const MouseEvent& event) OVERRIDE; |
| 75 | virtual bool OnMouseDragged(const MouseEvent& event) OVERRIDE; |
[email protected] | 3433825 | 2011-03-29 00:39:05 | [diff] [blame] | 76 | virtual void OnMouseCaptureLost() OVERRIDE; |
[email protected] | 8eb52a9a | 2011-03-09 16:52:00 | [diff] [blame] | 77 | virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE; |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 78 | |
| 79 | private: |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 80 | // This test calls OnMouse* functions. |
| 81 | FRIEND_TEST_ALL_PREFIXES(SingleSplitViewTest, MouseDrag); |
| 82 | |
[email protected] | ea36e32 | 2009-06-24 09:40:35 | [diff] [blame] | 83 | // Returns true if |x| or |y| is over the divider. |
[email protected] | 00854125 | 2010-03-16 00:24:20 | [diff] [blame] | 84 | bool IsPointInDivider(const gfx::Point& p); |
[email protected] | ea36e32 | 2009-06-24 09:40:35 | [diff] [blame] | 85 | |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 86 | // Calculates the new |divider_offset| based on the changes of split view |
| 87 | // bounds. |
[email protected] | 3d2fdb0 | 2011-10-25 18:42:25 | [diff] [blame] | 88 | int CalculateDividerOffset(int divider_offset, |
| 89 | const gfx::Rect& previous_bounds, |
| 90 | const gfx::Rect& new_bounds) const; |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 91 | |
| 92 | // Returns divider offset within primary axis size range for given split |
| 93 | // view |bounds|. |
| 94 | int NormalizeDividerOffset(int divider_offset, const gfx::Rect& bounds) const; |
| 95 | |
[email protected] | ea36e32 | 2009-06-24 09:40:35 | [diff] [blame] | 96 | // Returns width in case of horizontal split and height otherwise. |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 97 | int GetPrimaryAxisSize() const { |
[email protected] | ea36e32 | 2009-06-24 09:40:35 | [diff] [blame] | 98 | return GetPrimaryAxisSize(width(), height()); |
| 99 | } |
| 100 | |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 101 | int GetPrimaryAxisSize(int h, int v) const { |
[email protected] | ea36e32 | 2009-06-24 09:40:35 | [diff] [blame] | 102 | return is_horizontal_ ? h : v; |
| 103 | } |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 104 | |
| 105 | // Used to track drag info. |
| 106 | struct DragInfo { |
| 107 | // The initial coordinate of the mouse when the user started the drag. |
[email protected] | ea36e32 | 2009-06-24 09:40:35 | [diff] [blame] | 108 | int initial_mouse_offset; |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 109 | // The initial position of the divider when the user started the drag. |
[email protected] | ea36e32 | 2009-06-24 09:40:35 | [diff] [blame] | 110 | int initial_divider_offset; |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | DragInfo drag_info_; |
| 114 | |
[email protected] | ea36e32 | 2009-06-24 09:40:35 | [diff] [blame] | 115 | // Orientation of the split view. |
| 116 | bool is_horizontal_; |
| 117 | |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 118 | // Position of the divider. |
[email protected] | ea36e32 | 2009-06-24 09:40:35 | [diff] [blame] | 119 | int divider_offset_; |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 120 | |
[email protected] | e46318e | 2009-07-09 14:13:08 | [diff] [blame] | 121 | bool resize_leading_on_bounds_change_; |
| 122 | |
[email protected] | 3d2fdb0 | 2011-10-25 18:42:25 | [diff] [blame] | 123 | // Listener to notify about user initiated handle movements. Not owned. |
| 124 | SingleSplitViewListener* listener_; |
[email protected] | 1d4389b | 2011-01-13 00:12:35 | [diff] [blame] | 125 | |
[email protected] | 79e549f | 2011-03-14 06:56:33 | [diff] [blame] | 126 | // The accessible name of this view. |
| 127 | string16 accessible_name_; |
| 128 | |
[email protected] | 066e70a | 2008-10-24 01:55:03 | [diff] [blame] | 129 | DISALLOW_COPY_AND_ASSIGN(SingleSplitView); |
| 130 | }; |
| 131 | |
| 132 | } // namespace views |
| 133 | |
[email protected] | 2362e4f | 2009-05-08 00:34:05 | [diff] [blame] | 134 | #endif // VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_ |