OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ui/views/controls/single_split_view.h" | |
6 | |
7 #include "build/build_config.h" | |
8 #include "ui/accessibility/ax_view_state.h" | |
9 #include "ui/base/cursor/cursor.h" | |
10 #include "ui/gfx/canvas.h" | |
11 #include "ui/views/background.h" | |
12 #include "ui/views/border.h" | |
13 #include "ui/views/controls/single_split_view_listener.h" | |
14 #include "ui/views/native_cursor.h" | |
15 | |
16 #if defined(OS_WIN) | |
17 #include "skia/ext/skia_utils_win.h" | |
18 #endif | |
19 | |
20 namespace views { | |
21 | |
22 // static | |
23 const char SingleSplitView::kViewClassName[] = "SingleSplitView"; | |
24 | |
25 // Size of the divider in pixels. | |
26 static const int kDividerSize = 4; | |
27 | |
28 SingleSplitView::SingleSplitView(View* leading, | |
29 View* trailing, | |
30 Orientation orientation, | |
31 SingleSplitViewListener* listener) | |
32 : is_horizontal_(orientation == HORIZONTAL_SPLIT), | |
33 divider_offset_(-1), | |
34 resize_leading_on_bounds_change_(true), | |
35 resize_disabled_(false), | |
36 listener_(listener) { | |
37 AddChildView(leading); | |
38 AddChildView(trailing); | |
39 #if defined(OS_WIN) | |
40 set_background( | |
41 views::Background::CreateSolidBackground( | |
42 skia::COLORREFToSkColor(GetSysColor(COLOR_3DFACE)))); | |
43 #endif | |
44 } | |
45 | |
46 void SingleSplitView::Layout() { | |
47 gfx::Rect leading_bounds; | |
48 gfx::Rect trailing_bounds; | |
49 CalculateChildrenBounds(GetContentsBounds(), &leading_bounds, | |
50 &trailing_bounds); | |
51 | |
52 if (has_children()) { | |
53 if (child_at(0)->visible()) | |
54 child_at(0)->SetBoundsRect(leading_bounds); | |
55 if (child_count() > 1) { | |
56 if (child_at(1)->visible()) | |
57 child_at(1)->SetBoundsRect(trailing_bounds); | |
58 } | |
59 } | |
60 | |
61 // Invoke super's implementation so that the children are layed out. | |
62 View::Layout(); | |
63 } | |
64 | |
65 const char* SingleSplitView::GetClassName() const { | |
66 return kViewClassName; | |
67 } | |
68 | |
69 void SingleSplitView::GetAccessibleState(ui::AXViewState* state) { | |
70 state->role = ui::AX_ROLE_GROUP; | |
71 state->name = accessible_name_; | |
72 } | |
73 | |
74 gfx::Size SingleSplitView::GetPreferredSize() const { | |
75 int width = 0; | |
76 int height = 0; | |
77 for (int i = 0; i < 2 && i < child_count(); ++i) { | |
78 const View* view = child_at(i); | |
79 gfx::Size pref = view->GetPreferredSize(); | |
80 if (is_horizontal_) { | |
81 width += pref.width(); | |
82 height = std::max(height, pref.height()); | |
83 } else { | |
84 width = std::max(width, pref.width()); | |
85 height += pref.height(); | |
86 } | |
87 } | |
88 if (is_horizontal_) | |
89 width += GetDividerSize(); | |
90 else | |
91 height += GetDividerSize(); | |
92 width += GetInsets().width(); | |
93 height += GetInsets().height(); | |
94 return gfx::Size(width, height); | |
95 } | |
96 | |
97 gfx::NativeCursor SingleSplitView::GetCursor(const ui::MouseEvent& event) { | |
98 if (!IsPointInDivider(event.location())) | |
99 return gfx::kNullCursor; | |
100 return is_horizontal_ ? GetNativeEastWestResizeCursor() | |
101 : GetNativeNorthSouthResizeCursor(); | |
102 } | |
103 | |
104 int SingleSplitView::GetDividerSize() const { | |
105 bool both_visible = child_count() > 1 && child_at(0)->visible() && | |
106 child_at(1)->visible(); | |
107 return both_visible && !resize_disabled_ ? kDividerSize : 0; | |
108 } | |
109 | |
110 void SingleSplitView::CalculateChildrenBounds( | |
111 const gfx::Rect& bounds, | |
112 gfx::Rect* leading_bounds, | |
113 gfx::Rect* trailing_bounds) const { | |
114 bool is_leading_visible = has_children() && child_at(0)->visible(); | |
115 bool is_trailing_visible = child_count() > 1 && child_at(1)->visible(); | |
116 | |
117 if (!is_leading_visible && !is_trailing_visible) { | |
118 *leading_bounds = gfx::Rect(); | |
119 *trailing_bounds = gfx::Rect(); | |
120 return; | |
121 } | |
122 | |
123 int divider_at; | |
124 | |
125 if (!is_trailing_visible) { | |
126 divider_at = GetPrimaryAxisSize(bounds.width(), bounds.height()); | |
127 } else if (!is_leading_visible) { | |
128 divider_at = 0; | |
129 } else { | |
130 divider_at = CalculateDividerOffset(divider_offset_, bounds, bounds); | |
131 divider_at = NormalizeDividerOffset(divider_at, bounds); | |
132 } | |
133 | |
134 int divider_size = GetDividerSize(); | |
135 | |
136 if (is_horizontal_) { | |
137 *leading_bounds = | |
138 gfx::Rect(bounds.x(), bounds.y(), divider_at, bounds.height()); | |
139 *trailing_bounds = | |
140 gfx::Rect(divider_at + divider_size + bounds.x(), bounds.y(), | |
141 std::max(0, bounds.width() - divider_at - divider_size), | |
142 bounds.height()); | |
143 } else { | |
144 *leading_bounds = | |
145 gfx::Rect(bounds.x(), bounds.y(), bounds.width(), divider_at); | |
146 *trailing_bounds = gfx::Rect( | |
147 bounds.x(), divider_at + divider_size + bounds.y(), bounds.width(), | |
148 std::max(0, bounds.height() - divider_at - divider_size)); | |
149 } | |
150 } | |
151 | |
152 void SingleSplitView::SetAccessibleName(const base::string16& name) { | |
153 accessible_name_ = name; | |
154 } | |
155 | |
156 bool SingleSplitView::OnMousePressed(const ui::MouseEvent& event) { | |
157 if (!IsPointInDivider(event.location())) | |
158 return false; | |
159 drag_info_.initial_mouse_offset = GetPrimaryAxisSize(event.x(), event.y()); | |
160 drag_info_.initial_divider_offset = | |
161 NormalizeDividerOffset(divider_offset_, GetContentsBounds()); | |
162 return true; | |
163 } | |
164 | |
165 bool SingleSplitView::OnMouseDragged(const ui::MouseEvent& event) { | |
166 if (child_count() < 2) | |
167 return false; | |
168 | |
169 int delta_offset = GetPrimaryAxisSize(event.x(), event.y()) - | |
170 drag_info_.initial_mouse_offset; | |
171 if (is_horizontal_ && base::i18n::IsRTL()) | |
172 delta_offset *= -1; | |
173 // Honor the first child's minimum size when resizing. | |
174 gfx::Size min = child_at(0)->GetMinimumSize(); | |
175 int new_size = std::max(GetPrimaryAxisSize(min.width(), min.height()), | |
176 drag_info_.initial_divider_offset + delta_offset); | |
177 | |
178 // Honor the second child's minimum size, and don't let the view | |
179 // get bigger than our width. | |
180 min = child_at(1)->GetMinimumSize(); | |
181 new_size = std::min(GetPrimaryAxisSize() - kDividerSize - | |
182 GetPrimaryAxisSize(min.width(), min.height()), new_size); | |
183 | |
184 if (new_size != divider_offset_) { | |
185 set_divider_offset(new_size); | |
186 if (!listener_ || listener_->SplitHandleMoved(this)) | |
187 Layout(); | |
188 } | |
189 return true; | |
190 } | |
191 | |
192 void SingleSplitView::OnMouseCaptureLost() { | |
193 if (child_count() < 2) | |
194 return; | |
195 | |
196 if (drag_info_.initial_divider_offset != divider_offset_) { | |
197 set_divider_offset(drag_info_.initial_divider_offset); | |
198 if (!listener_ || listener_->SplitHandleMoved(this)) | |
199 Layout(); | |
200 } | |
201 } | |
202 | |
203 void SingleSplitView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
204 gfx::Rect previous_content_bounds = previous_bounds; | |
205 previous_content_bounds.Inset(GetInsets()); | |
206 divider_offset_ = CalculateDividerOffset( | |
207 divider_offset_, previous_content_bounds, GetContentsBounds()); | |
208 } | |
209 | |
210 bool SingleSplitView::IsPointInDivider(const gfx::Point& p) { | |
211 if (resize_disabled_) | |
212 return false; | |
213 | |
214 if (child_count() < 2) | |
215 return false; | |
216 | |
217 if (!child_at(0)->visible() || !child_at(1)->visible()) | |
218 return false; | |
219 | |
220 int divider_relative_offset; | |
221 if (is_horizontal_) { | |
222 divider_relative_offset = | |
223 p.x() - child_at(base::i18n::IsRTL() ? 1 : 0)->width(); | |
224 } else { | |
225 divider_relative_offset = p.y() - child_at(0)->height(); | |
226 } | |
227 return (divider_relative_offset >= 0 && | |
228 divider_relative_offset < GetDividerSize()); | |
229 } | |
230 | |
231 int SingleSplitView::CalculateDividerOffset( | |
232 int divider_offset, | |
233 const gfx::Rect& previous_bounds, | |
234 const gfx::Rect& new_bounds) const { | |
235 if (resize_leading_on_bounds_change_ && divider_offset != -1) { | |
236 // We do not update divider_offset on minimize (to zero) and on restore | |
237 // (to largest value). As a result we get back to the original value upon | |
238 // window restore. | |
239 bool is_minimize_or_restore = | |
240 previous_bounds.height() == 0 || new_bounds.height() == 0; | |
241 if (!is_minimize_or_restore) { | |
242 if (is_horizontal_) | |
243 divider_offset += new_bounds.width() - previous_bounds.width(); | |
244 else | |
245 divider_offset += new_bounds.height() - previous_bounds.height(); | |
246 | |
247 if (divider_offset < 0) | |
248 divider_offset = GetDividerSize(); | |
249 } | |
250 } | |
251 return divider_offset; | |
252 } | |
253 | |
254 int SingleSplitView::NormalizeDividerOffset(int divider_offset, | |
255 const gfx::Rect& bounds) const { | |
256 int primary_axis_size = GetPrimaryAxisSize(bounds.width(), bounds.height()); | |
257 if (divider_offset < 0) | |
258 // primary_axis_size may < GetDividerSize during initial layout. | |
259 return std::max(0, (primary_axis_size - GetDividerSize()) / 2); | |
260 return std::min(divider_offset, | |
261 std::max(primary_axis_size - GetDividerSize(), 0)); | |
262 } | |
263 | |
264 } // namespace views | |
OLD | NEW |