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 #include "views/controls/single_split_view.h" | 5 #include "views/controls/single_split_view.h" |
6 | 6 |
7 #if defined(OS_LINUX) | 7 #if defined(OS_LINUX) |
8 #include <gdk/gdk.h> | 8 #include <gdk/gdk.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include "gfx/canvas.h" | 11 #include "gfx/canvas.h" |
12 #include "skia/ext/skia_utils_win.h" | 12 #include "skia/ext/skia_utils_win.h" |
13 #include "views/background.h" | 13 #include "views/background.h" |
14 | 14 |
15 #if defined(OS_LINUX) | 15 #if defined(OS_LINUX) |
16 #include "gfx/gtk_util.h" | 16 #include "gfx/gtk_util.h" |
17 #endif | 17 #endif |
18 | 18 |
19 namespace views { | 19 namespace views { |
20 | 20 |
21 // Size of the divider in pixels. | 21 // Size of the divider in pixels. |
22 static const int kDividerSize = 4; | 22 static const int kDividerSize = 4; |
23 | 23 |
24 SingleSplitView::SingleSplitView(View* leading, | 24 SingleSplitView::SingleSplitView(View* leading, |
25 View* trailing, | 25 View* trailing, |
26 Orientation orientation, | 26 Orientation orientation) |
27 Observer* observer) | |
28 : is_horizontal_(orientation == HORIZONTAL_SPLIT), | 27 : is_horizontal_(orientation == HORIZONTAL_SPLIT), |
29 divider_offset_(-1), | 28 divider_offset_(-1), |
30 resize_leading_on_bounds_change_(true), | 29 resize_leading_on_bounds_change_(true) { |
31 observer_(observer) { | |
32 AddChildView(leading); | 30 AddChildView(leading); |
33 AddChildView(trailing); | 31 AddChildView(trailing); |
34 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
35 set_background( | 33 set_background( |
36 views::Background::CreateSolidBackground( | 34 views::Background::CreateSolidBackground( |
37 skia::COLORREFToSkColor(GetSysColor(COLOR_3DFACE)))); | 35 skia::COLORREFToSkColor(GetSysColor(COLOR_3DFACE)))); |
38 #endif | 36 #endif |
39 } | 37 } |
40 | 38 |
41 void SingleSplitView::DidChangeBounds(const gfx::Rect& previous, | 39 void SingleSplitView::DidChangeBounds(const gfx::Rect& previous, |
42 const gfx::Rect& current) { | 40 const gfx::Rect& current) { |
43 divider_offset_ = CalculateDividerOffset(divider_offset_, previous, current); | 41 if (resize_leading_on_bounds_change_) { |
| 42 // We do not update divider_offset_ on minimize (to zero) and on restore |
| 43 // (to largest value). As a result we get back to the original value upon |
| 44 // window restore. |
| 45 bool is_minimize_or_restore = |
| 46 previous.height() == 0 || current.height() == 0; |
| 47 if (!is_minimize_or_restore) { |
| 48 if (is_horizontal_) |
| 49 divider_offset_ += current.width() - previous.width(); |
| 50 else |
| 51 divider_offset_ += current.height() - previous.height(); |
| 52 |
| 53 if (divider_offset_ < 0) |
| 54 divider_offset_ = kDividerSize; |
| 55 } |
| 56 } |
44 View::DidChangeBounds(previous, current); | 57 View::DidChangeBounds(previous, current); |
45 } | 58 } |
46 | 59 |
47 void SingleSplitView::Layout() { | 60 void SingleSplitView::Layout() { |
48 gfx::Rect leading_bounds; | 61 if (GetChildViewCount() != 2) |
49 gfx::Rect trailing_bounds; | 62 return; |
50 CalculateChildrenBounds(bounds(), &leading_bounds, &trailing_bounds); | |
51 | 63 |
52 if (GetChildViewCount() > 0) { | 64 View* leading = GetChildViewAt(0); |
53 if (GetChildViewAt(0)->IsVisible()) | 65 View* trailing = GetChildViewAt(1); |
54 GetChildViewAt(0)->SetBounds(leading_bounds); | 66 |
55 if (GetChildViewCount() > 1) { | 67 if (!leading->IsVisible() && !trailing->IsVisible()) |
56 if (GetChildViewAt(1)->IsVisible()) | 68 return; |
57 GetChildViewAt(1)->SetBounds(trailing_bounds); | 69 |
| 70 if (width() == 0 || height() == 0) { |
| 71 // We are most likely minimized - do not touch divider offset. |
| 72 return; |
| 73 } else if (!trailing->IsVisible()) { |
| 74 leading->SetBounds(0, 0, width(), height()); |
| 75 } else if (!leading->IsVisible()) { |
| 76 trailing->SetBounds(0, 0, width(), height()); |
| 77 } else { |
| 78 if (divider_offset_ < 0) |
| 79 divider_offset_ = (GetPrimaryAxisSize() - kDividerSize) / 2; |
| 80 else |
| 81 divider_offset_ = std::min(divider_offset_, |
| 82 GetPrimaryAxisSize() - kDividerSize); |
| 83 |
| 84 if (is_horizontal_) { |
| 85 leading->SetBounds(0, 0, divider_offset_, height()); |
| 86 trailing->SetBounds(divider_offset_ + kDividerSize, 0, |
| 87 width() - divider_offset_ - kDividerSize, height()); |
| 88 } else { |
| 89 leading->SetBounds(0, 0, width(), divider_offset_); |
| 90 trailing->SetBounds(0, divider_offset_ + kDividerSize, |
| 91 width(), height() - divider_offset_ - kDividerSize); |
58 } | 92 } |
59 } | 93 } |
60 | 94 |
61 SchedulePaint(); | 95 SchedulePaint(); |
62 | 96 |
63 // Invoke super's implementation so that the children are layed out. | 97 // Invoke super's implementation so that the children are layed out. |
64 View::Layout(); | 98 View::Layout(); |
65 } | 99 } |
66 | 100 |
67 AccessibilityTypes::Role SingleSplitView::GetAccessibleRole() { | 101 AccessibilityTypes::Role SingleSplitView::GetAccessibleRole() { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 return is_horizontal_ ? we_resize_cursor : ns_resize_cursor; | 133 return is_horizontal_ ? we_resize_cursor : ns_resize_cursor; |
100 #elif defined(OS_LINUX) | 134 #elif defined(OS_LINUX) |
101 return gfx::GetCursor(is_horizontal_ ? | 135 return gfx::GetCursor(is_horizontal_ ? |
102 GDK_SB_H_DOUBLE_ARROW : | 136 GDK_SB_H_DOUBLE_ARROW : |
103 GDK_SB_V_DOUBLE_ARROW); | 137 GDK_SB_V_DOUBLE_ARROW); |
104 #endif | 138 #endif |
105 } | 139 } |
106 return NULL; | 140 return NULL; |
107 } | 141 } |
108 | 142 |
109 void SingleSplitView::CalculateChildrenBounds( | |
110 const gfx::Rect& bounds, | |
111 gfx::Rect* leading_bounds, | |
112 gfx::Rect* trailing_bounds) const { | |
113 bool is_leading_visible = | |
114 GetChildViewCount() > 0 && GetChildViewAt(0)->IsVisible(); | |
115 bool is_trailing_visible = | |
116 GetChildViewCount() > 1 && GetChildViewAt(1)->IsVisible(); | |
117 | |
118 if (!is_leading_visible && !is_trailing_visible) { | |
119 *leading_bounds = gfx::Rect(); | |
120 *trailing_bounds = gfx::Rect(); | |
121 return; | |
122 } | |
123 | |
124 int divider_at; | |
125 | |
126 if (!is_trailing_visible) { | |
127 divider_at = GetPrimaryAxisSize(bounds.width(), bounds.height()); | |
128 } else if (!is_leading_visible) { | |
129 divider_at = 0; | |
130 } else { | |
131 divider_at = | |
132 CalculateDividerOffset(divider_offset_, this->bounds(), bounds); | |
133 divider_at = NormalizeDividerOffset(divider_at, bounds); | |
134 } | |
135 | |
136 int divider_size = | |
137 !is_leading_visible || !is_trailing_visible ? 0 : kDividerSize; | |
138 | |
139 if (is_horizontal_) { | |
140 *leading_bounds = gfx::Rect(0, 0, divider_at, bounds.height()); | |
141 *trailing_bounds = | |
142 gfx::Rect(divider_at + divider_size, 0, | |
143 std::max(0, bounds.width() - divider_at - divider_size), | |
144 bounds.height()); | |
145 } else { | |
146 *leading_bounds = gfx::Rect(0, 0, bounds.width(), divider_at); | |
147 *trailing_bounds = | |
148 gfx::Rect(0, divider_at + divider_size, bounds.width(), | |
149 std::max(0, bounds.height() - divider_at - divider_size)); | |
150 } | |
151 } | |
152 | |
153 bool SingleSplitView::OnMousePressed(const MouseEvent& event) { | 143 bool SingleSplitView::OnMousePressed(const MouseEvent& event) { |
154 if (!IsPointInDivider(event.location())) | 144 if (!IsPointInDivider(event.location())) |
155 return false; | 145 return false; |
156 drag_info_.initial_mouse_offset = GetPrimaryAxisSize(event.x(), event.y()); | 146 drag_info_.initial_mouse_offset = GetPrimaryAxisSize(event.x(), event.y()); |
157 drag_info_.initial_divider_offset = | 147 drag_info_.initial_divider_offset = divider_offset_; |
158 NormalizeDividerOffset(divider_offset_, bounds()); | |
159 return true; | 148 return true; |
160 } | 149 } |
161 | 150 |
162 bool SingleSplitView::OnMouseDragged(const MouseEvent& event) { | 151 bool SingleSplitView::OnMouseDragged(const MouseEvent& event) { |
163 if (GetChildViewCount() < 2) | 152 if (GetChildViewCount() < 2) |
164 return false; | 153 return false; |
165 | 154 |
166 int delta_offset = GetPrimaryAxisSize(event.x(), event.y()) - | 155 int delta_offset = GetPrimaryAxisSize(event.x(), event.y()) - |
167 drag_info_.initial_mouse_offset; | 156 drag_info_.initial_mouse_offset; |
168 if (is_horizontal_ && base::i18n::IsRTL()) | 157 if (is_horizontal_ && base::i18n::IsRTL()) |
169 delta_offset *= -1; | 158 delta_offset *= -1; |
170 // Honor the minimum size when resizing. | 159 // Honor the minimum size when resizing. |
171 gfx::Size min = GetChildViewAt(0)->GetMinimumSize(); | 160 gfx::Size min = GetChildViewAt(0)->GetMinimumSize(); |
172 int new_size = std::max(GetPrimaryAxisSize(min.width(), min.height()), | 161 int new_size = std::max(GetPrimaryAxisSize(min.width(), min.height()), |
173 drag_info_.initial_divider_offset + delta_offset); | 162 drag_info_.initial_divider_offset + delta_offset); |
174 | 163 |
175 // And don't let the view get bigger than our width. | 164 // And don't let the view get bigger than our width. |
176 new_size = std::min(GetPrimaryAxisSize() - kDividerSize, new_size); | 165 new_size = std::min(GetPrimaryAxisSize() - kDividerSize, new_size); |
177 | 166 |
178 if (new_size != divider_offset_) { | 167 if (new_size != divider_offset_) { |
179 set_divider_offset(new_size); | 168 set_divider_offset(new_size); |
180 if (!observer_ || observer_->SplitHandleMoved(this)) | 169 Layout(); |
181 Layout(); | |
182 } | 170 } |
183 return true; | 171 return true; |
184 } | 172 } |
185 | 173 |
186 void SingleSplitView::OnMouseReleased(const MouseEvent& event, bool canceled) { | 174 void SingleSplitView::OnMouseReleased(const MouseEvent& event, bool canceled) { |
187 if (GetChildViewCount() < 2) | 175 if (GetChildViewCount() < 2) |
188 return; | 176 return; |
189 | 177 |
190 if (canceled && drag_info_.initial_divider_offset != divider_offset_) { | 178 if (canceled && drag_info_.initial_divider_offset != divider_offset_) { |
191 set_divider_offset(drag_info_.initial_divider_offset); | 179 set_divider_offset(drag_info_.initial_divider_offset); |
192 if (!observer_ || observer_->SplitHandleMoved(this)) | 180 Layout(); |
193 Layout(); | |
194 } | 181 } |
195 } | 182 } |
196 | 183 |
197 bool SingleSplitView::IsPointInDivider(const gfx::Point& p) { | 184 bool SingleSplitView::IsPointInDivider(const gfx::Point& p) { |
198 if (GetChildViewCount() < 2) | 185 if (GetChildViewCount() < 2) |
199 return false; | 186 return false; |
200 | 187 |
201 if (!GetChildViewAt(0)->IsVisible() || !GetChildViewAt(1)->IsVisible()) | 188 if (!GetChildViewAt(0)->IsVisible() || !GetChildViewAt(1)->IsVisible()) |
202 return false; | 189 return false; |
203 | 190 |
204 int divider_relative_offset; | 191 int divider_relative_offset; |
205 if (is_horizontal_) { | 192 if (is_horizontal_) { |
206 divider_relative_offset = | 193 divider_relative_offset = |
207 p.x() - GetChildViewAt(base::i18n::IsRTL() ? 1 : 0)->width(); | 194 p.x() - GetChildViewAt(base::i18n::IsRTL() ? 1 : 0)->width(); |
208 } else { | 195 } else { |
209 divider_relative_offset = p.y() - GetChildViewAt(0)->height(); | 196 divider_relative_offset = p.y() - GetChildViewAt(0)->height(); |
210 } | 197 } |
211 return (divider_relative_offset >= 0 && | 198 return (divider_relative_offset >= 0 && |
212 divider_relative_offset < kDividerSize); | 199 divider_relative_offset < kDividerSize); |
213 } | 200 } |
214 | 201 |
215 int SingleSplitView::CalculateDividerOffset( | |
216 int divider_offset, | |
217 const gfx::Rect& previous_bounds, | |
218 const gfx::Rect& new_bounds) const { | |
219 if (resize_leading_on_bounds_change_ && divider_offset != -1) { | |
220 // We do not update divider_offset on minimize (to zero) and on restore | |
221 // (to largest value). As a result we get back to the original value upon | |
222 // window restore. | |
223 bool is_minimize_or_restore = | |
224 previous_bounds.height() == 0 || new_bounds.height() == 0; | |
225 if (!is_minimize_or_restore) { | |
226 if (is_horizontal_) | |
227 divider_offset += new_bounds.width() - previous_bounds.width(); | |
228 else | |
229 divider_offset += new_bounds.height() - previous_bounds.height(); | |
230 | |
231 if (divider_offset < 0) | |
232 divider_offset = kDividerSize; | |
233 } | |
234 } | |
235 return divider_offset; | |
236 } | |
237 | |
238 int SingleSplitView::NormalizeDividerOffset(int divider_offset, | |
239 const gfx::Rect& bounds) const { | |
240 int primary_axis_size = GetPrimaryAxisSize(bounds.width(), bounds.height()); | |
241 if (divider_offset < 0) | |
242 return (primary_axis_size - kDividerSize) / 2; | |
243 return std::min(divider_offset, | |
244 std::max(primary_axis_size - kDividerSize, 0)); | |
245 } | |
246 | |
247 } // namespace views | 202 } // namespace views |
OLD | NEW |