Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_SINGLE_SPLIT_VIEW_H_ | 5 #ifndef VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_ |
| 6 #define VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_ | 6 #define VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "views/view.h" | 9 #include "views/view.h" |
| 10 | 10 |
| 11 namespace views { | 11 namespace views { |
| 12 | 12 |
| 13 // SingleSplitView lays out two views horizontally. A splitter exists between | 13 // SingleSplitView lays out two views next to each other, either horizontally |
| 14 // the two views that the user can drag around to resize the views. | 14 // or vertically. A splitter exists between the two views that the user can |
| 15 // drag around to resize the views. | |
| 16 // When non-NULL observer is provided at the construction time, SingleSplitView | |
| 17 // expects the entity resizing the SingleSplitView itself to resize its children | |
| 18 // views (CalculateChildrenBounds and ResizeChildren functions are handy for | |
| 19 // this). Observer's SplitHandleMoved notification helps to handle user | |
| 20 // initiated layout changes. | |
| 21 // When observer is NULL, SingleSplitView resizes children views automatically. | |
| 15 class SingleSplitView : public views::View { | 22 class SingleSplitView : public views::View { |
| 16 public: | 23 public: |
| 17 enum Orientation { | 24 enum Orientation { |
| 18 HORIZONTAL_SPLIT, | 25 HORIZONTAL_SPLIT, |
| 19 VERTICAL_SPLIT | 26 VERTICAL_SPLIT |
| 20 }; | 27 }; |
| 21 | 28 |
| 22 SingleSplitView(View* leading, View* trailing, Orientation orientation); | 29 class Observer { |
| 30 public: | |
| 31 // Invoked when split handle is moved by the user. | |
| 32 virtual void SplitHandleMoved(SingleSplitView* source) = 0; | |
| 33 protected: | |
| 34 virtual ~Observer() {} | |
| 35 }; | |
| 36 | |
| 37 SingleSplitView(View* leading, View* trailing, Orientation orientation, | |
|
sky
2011/01/07 21:35:40
each arg on its own line.
Aleksey Shlyapnikov
2011/01/07 23:06:00
Done.
| |
| 38 Observer* observer); | |
| 23 | 39 |
| 24 virtual void DidChangeBounds(const gfx::Rect& previous, | 40 virtual void DidChangeBounds(const gfx::Rect& previous, |
| 25 const gfx::Rect& current); | 41 const gfx::Rect& current); |
| 26 | 42 |
| 27 virtual void Layout(); | 43 virtual void Layout(); |
| 28 | 44 |
| 29 virtual AccessibilityTypes::Role GetAccessibleRole(); | 45 virtual AccessibilityTypes::Role GetAccessibleRole(); |
| 30 | 46 |
| 31 // SingleSplitView's preferred size is the sum of the preferred widths | 47 // SingleSplitView's preferred size is the sum of the preferred widths |
| 32 // and the max of the heights. | 48 // and the max of the heights. |
| 33 virtual gfx::Size GetPreferredSize(); | 49 virtual gfx::Size GetPreferredSize(); |
| 34 | 50 |
| 35 // Overriden to return a resize cursor when over the divider. | 51 // Overriden to return a resize cursor when over the divider. |
| 36 virtual gfx::NativeCursor GetCursorForPoint(Event::EventType event_type, | 52 virtual gfx::NativeCursor GetCursorForPoint(Event::EventType event_type, |
| 37 const gfx::Point& p); | 53 const gfx::Point& p); |
| 38 | 54 |
| 39 void set_divider_offset(int divider_offset) { | 55 void set_divider_offset(int divider_offset) { |
| 40 divider_offset_ = divider_offset; | 56 divider_offset_ = divider_offset; |
| 41 } | 57 } |
| 42 int divider_offset() { return divider_offset_; } | 58 int divider_offset() const { return divider_offset_; } |
| 43 | 59 |
| 44 // Sets whether the leading component is resized when the split views size | 60 // Sets whether the leading component is resized when the split views size |
| 45 // changes. The default is true. A value of false results in the trailing | 61 // changes. The default is true. A value of false results in the trailing |
| 46 // component resizing on a bounds change. | 62 // component resizing on a bounds change. |
| 47 void set_resize_leading_on_bounds_change(bool resize) { | 63 void set_resize_leading_on_bounds_change(bool resize) { |
| 48 resize_leading_on_bounds_change_ = resize; | 64 resize_leading_on_bounds_change_ = resize; |
| 49 } | 65 } |
| 50 | 66 |
| 67 // Calculates ideal leading and trailing view bounds according to the current | |
| 68 // split view bounds, divider offset and children visiblity. | |
| 69 // Does not change children view bounds. | |
| 70 void CalculateChildrenBounds(gfx::Rect* leading_bounds, | |
| 71 gfx::Rect* trailing_bounds) const; | |
| 72 | |
| 73 // Resizes children views to the leading_bounds and trailing_bounds. | |
| 74 void ResizeChildren(const gfx::Rect& leading_bounds, | |
| 75 const gfx::Rect& trailing_bounds) const; | |
| 76 | |
| 51 protected: | 77 protected: |
| 52 virtual bool OnMousePressed(const MouseEvent& event); | 78 virtual bool OnMousePressed(const MouseEvent& event); |
| 53 virtual bool OnMouseDragged(const MouseEvent& event); | 79 virtual bool OnMouseDragged(const MouseEvent& event); |
| 54 virtual void OnMouseReleased(const MouseEvent& event, bool canceled); | 80 virtual void OnMouseReleased(const MouseEvent& event, bool canceled); |
| 55 | 81 |
| 56 private: | 82 private: |
| 57 // Returns true if |x| or |y| is over the divider. | 83 // Returns true if |x| or |y| is over the divider. |
| 58 bool IsPointInDivider(const gfx::Point& p); | 84 bool IsPointInDivider(const gfx::Point& p); |
| 59 | 85 |
| 86 // Returns divider offset within primary axis size range. | |
| 87 int NormalizeDividerOffset(int divider_offset) const; | |
| 88 | |
| 60 // Returns width in case of horizontal split and height otherwise. | 89 // Returns width in case of horizontal split and height otherwise. |
| 61 int GetPrimaryAxisSize() { | 90 int GetPrimaryAxisSize() const { |
| 62 return GetPrimaryAxisSize(width(), height()); | 91 return GetPrimaryAxisSize(width(), height()); |
| 63 } | 92 } |
| 64 | 93 |
| 65 int GetPrimaryAxisSize(int h, int v) { | 94 int GetPrimaryAxisSize(int h, int v) const { |
| 66 return is_horizontal_ ? h : v; | 95 return is_horizontal_ ? h : v; |
| 67 } | 96 } |
| 68 | 97 |
| 69 // Used to track drag info. | 98 // Used to track drag info. |
| 70 struct DragInfo { | 99 struct DragInfo { |
| 71 // The initial coordinate of the mouse when the user started the drag. | 100 // The initial coordinate of the mouse when the user started the drag. |
| 72 int initial_mouse_offset; | 101 int initial_mouse_offset; |
| 73 // The initial position of the divider when the user started the drag. | 102 // The initial position of the divider when the user started the drag. |
| 74 int initial_divider_offset; | 103 int initial_divider_offset; |
| 75 }; | 104 }; |
| 76 | 105 |
| 77 DragInfo drag_info_; | 106 DragInfo drag_info_; |
| 78 | 107 |
| 79 // Orientation of the split view. | 108 // Orientation of the split view. |
| 80 bool is_horizontal_; | 109 bool is_horizontal_; |
| 81 | 110 |
| 82 // Position of the divider. | 111 // Position of the divider. |
| 83 int divider_offset_; | 112 int divider_offset_; |
| 84 | 113 |
| 85 bool resize_leading_on_bounds_change_; | 114 bool resize_leading_on_bounds_change_; |
| 86 | 115 |
| 116 // Observer to notify about user initiated handle movements. Not own by us. | |
| 117 Observer* observer_; | |
| 118 | |
| 87 DISALLOW_COPY_AND_ASSIGN(SingleSplitView); | 119 DISALLOW_COPY_AND_ASSIGN(SingleSplitView); |
| 88 }; | 120 }; |
| 89 | 121 |
| 90 } // namespace views | 122 } // namespace views |
| 91 | 123 |
| 92 #endif // VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_ | 124 #endif // VIEWS_CONTROLS_SINGLE_SPLIT_VIEW_H_ |
| OLD | NEW |