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 "base/logging.h" | |
11 #include "gfx/canvas.h" | 12 #include "gfx/canvas.h" |
12 #include "skia/ext/skia_utils_win.h" | 13 #include "skia/ext/skia_utils_win.h" |
13 #include "views/background.h" | 14 #include "views/background.h" |
14 | 15 |
15 #if defined(OS_LINUX) | 16 #if defined(OS_LINUX) |
16 #include "gfx/gtk_util.h" | 17 #include "gfx/gtk_util.h" |
17 #endif | 18 #endif |
18 | 19 |
19 namespace views { | 20 namespace views { |
20 | 21 |
21 // Size of the divider in pixels. | 22 // Size of the divider in pixels. |
22 static const int kDividerSize = 4; | 23 static const int kDividerSize = 4; |
23 | 24 |
24 SingleSplitView::SingleSplitView(View* leading, | 25 SingleSplitView::SingleSplitView(View* leading, |
25 View* trailing, | 26 View* trailing, |
26 Orientation orientation) | 27 Orientation orientation, |
28 Observer* observer) | |
27 : is_horizontal_(orientation == HORIZONTAL_SPLIT), | 29 : is_horizontal_(orientation == HORIZONTAL_SPLIT), |
28 divider_offset_(-1), | 30 divider_offset_(-1), |
29 resize_leading_on_bounds_change_(true) { | 31 resize_leading_on_bounds_change_(true), |
32 observer_(observer) { | |
33 DCHECK(observer_); | |
30 AddChildView(leading); | 34 AddChildView(leading); |
31 AddChildView(trailing); | 35 AddChildView(trailing); |
32 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
33 set_background( | 37 set_background( |
34 views::Background::CreateSolidBackground( | 38 views::Background::CreateSolidBackground( |
35 skia::COLORREFToSkColor(GetSysColor(COLOR_3DFACE)))); | 39 skia::COLORREFToSkColor(GetSysColor(COLOR_3DFACE)))); |
36 #endif | 40 #endif |
37 } | 41 } |
38 | 42 |
39 void SingleSplitView::DidChangeBounds(const gfx::Rect& previous, | 43 void SingleSplitView::DidChangeBounds(const gfx::Rect& previous, |
(...skipping 11 matching lines...) Expand all Loading... | |
51 divider_offset_ += current.height() - previous.height(); | 55 divider_offset_ += current.height() - previous.height(); |
52 | 56 |
53 if (divider_offset_ < 0) | 57 if (divider_offset_ < 0) |
54 divider_offset_ = kDividerSize; | 58 divider_offset_ = kDividerSize; |
55 } | 59 } |
56 } | 60 } |
57 View::DidChangeBounds(previous, current); | 61 View::DidChangeBounds(previous, current); |
58 } | 62 } |
59 | 63 |
60 void SingleSplitView::Layout() { | 64 void SingleSplitView::Layout() { |
61 if (GetChildViewCount() != 2) | 65 if (GetChildViewCount() != 2) { |
sky
2011/01/06 17:55:02
This code is fairly finicky. Please write some tes
Aleksey Shlyapnikov
2011/01/07 19:07:23
Reworked this code to reduce finickiness.
On 2011
| |
66 // Special case, if there's only one view and it's visible, maximize it. | |
67 // Otherwise don't touch these rects to avoid unnecessary resizes. | |
sky
2011/01/06 17:55:02
I don't understand this comment. Shouldn't you alw
Aleksey Shlyapnikov
2011/01/07 19:07:23
Done.
| |
68 if (GetChildViewCount() == 1 && GetChildViewAt(0)->IsVisible()) | |
69 leading_view_rect_ = gfx::Rect(0, 0, width(), height()); | |
62 return; | 70 return; |
71 } | |
63 | 72 |
64 View* leading = GetChildViewAt(0); | 73 View* leading = GetChildViewAt(0); |
65 View* trailing = GetChildViewAt(1); | 74 View* trailing = GetChildViewAt(1); |
66 | 75 |
67 if (!leading->IsVisible() && !trailing->IsVisible()) | 76 if (!leading->IsVisible() && !trailing->IsVisible()) |
68 return; | 77 return; |
69 | 78 |
70 if (width() == 0 || height() == 0) { | 79 if (width() == 0 || height() == 0) { |
71 // We are most likely minimized - do not touch divider offset. | 80 // We are most likely minimized - do not touch divider offset. |
72 return; | 81 return; |
73 } else if (!trailing->IsVisible()) { | 82 } |
74 leading->SetBounds(0, 0, width(), height()); | 83 |
84 int divide_at; | |
sky
2011/01/06 17:55:02
dividier_at
Aleksey Shlyapnikov
2011/01/07 19:07:23
Done.
| |
85 int divider_size = | |
86 !leading->IsVisible() || !trailing->IsVisible() ? 0 : kDividerSize; | |
87 | |
88 if (!trailing->IsVisible()) { | |
89 divide_at = GetPrimaryAxisSize(); | |
75 } else if (!leading->IsVisible()) { | 90 } else if (!leading->IsVisible()) { |
76 trailing->SetBounds(0, 0, width(), height()); | 91 divide_at = 0; |
77 } else { | 92 } else { |
78 if (divider_offset_ < 0) | 93 if (divider_offset_ < 0) { |
79 divider_offset_ = (GetPrimaryAxisSize() - kDividerSize) / 2; | 94 divider_offset_ = (GetPrimaryAxisSize() - kDividerSize) / 2; |
80 else | 95 } else { |
81 divider_offset_ = std::min(divider_offset_, | 96 divider_offset_ = std::min(divider_offset_, |
82 GetPrimaryAxisSize() - kDividerSize); | 97 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); | |
92 } | 98 } |
99 divide_at = divider_offset_; | |
93 } | 100 } |
94 | 101 |
95 SchedulePaint(); | 102 if (is_horizontal_) { |
103 leading_view_rect_ = gfx::Rect(0, 0, divide_at, height()); | |
104 trailing_view_rect_ = | |
105 gfx::Rect(divide_at + divider_size, 0, | |
106 width() - divide_at - divider_size, height()); | |
107 } else { | |
108 leading_view_rect_ = gfx::Rect(0, 0, width(), divide_at); | |
109 trailing_view_rect_ = | |
110 gfx::Rect(0, divide_at + divider_size, | |
111 width(), height() - divide_at - divider_size); | |
112 } | |
96 | 113 |
97 // Invoke super's implementation so that the children are layed out. | 114 // Invoke super's implementation so that the children are layed out. |
sky
2011/01/06 17:55:02
If Layout was invoked because the bounds of the si
Aleksey Shlyapnikov
2011/01/07 19:07:23
Done.
| |
98 View::Layout(); | 115 View::Layout(); |
99 } | 116 } |
100 | 117 |
101 AccessibilityTypes::Role SingleSplitView::GetAccessibleRole() { | 118 AccessibilityTypes::Role SingleSplitView::GetAccessibleRole() { |
102 return AccessibilityTypes::ROLE_GROUPING; | 119 return AccessibilityTypes::ROLE_GROUPING; |
103 } | 120 } |
104 | 121 |
105 gfx::Size SingleSplitView::GetPreferredSize() { | 122 gfx::Size SingleSplitView::GetPreferredSize() { |
106 int width = 0; | 123 int width = 0; |
107 int height = 0; | 124 int height = 0; |
(...skipping 25 matching lines...) Expand all Loading... | |
133 return is_horizontal_ ? we_resize_cursor : ns_resize_cursor; | 150 return is_horizontal_ ? we_resize_cursor : ns_resize_cursor; |
134 #elif defined(OS_LINUX) | 151 #elif defined(OS_LINUX) |
135 return gfx::GetCursor(is_horizontal_ ? | 152 return gfx::GetCursor(is_horizontal_ ? |
136 GDK_SB_H_DOUBLE_ARROW : | 153 GDK_SB_H_DOUBLE_ARROW : |
137 GDK_SB_V_DOUBLE_ARROW); | 154 GDK_SB_V_DOUBLE_ARROW); |
138 #endif | 155 #endif |
139 } | 156 } |
140 return NULL; | 157 return NULL; |
141 } | 158 } |
142 | 159 |
160 void SingleSplitView::ResizeViews() { | |
161 if (GetChildViewCount() > 0) { | |
162 GetChildViewAt(0)->SetBounds(leading_view_rect()); | |
sky
2011/01/06 17:55:02
Only do this if the views are visible.
Aleksey Shlyapnikov
2011/01/07 19:07:23
Done.
| |
163 if (GetChildViewCount() > 1) | |
164 GetChildViewAt(1)->SetBounds(trailing_view_rect()); | |
165 } | |
166 } | |
167 | |
143 bool SingleSplitView::OnMousePressed(const MouseEvent& event) { | 168 bool SingleSplitView::OnMousePressed(const MouseEvent& event) { |
144 if (!IsPointInDivider(event.location())) | 169 if (!IsPointInDivider(event.location())) |
145 return false; | 170 return false; |
146 drag_info_.initial_mouse_offset = GetPrimaryAxisSize(event.x(), event.y()); | 171 drag_info_.initial_mouse_offset = GetPrimaryAxisSize(event.x(), event.y()); |
147 drag_info_.initial_divider_offset = divider_offset_; | 172 drag_info_.initial_divider_offset = divider_offset_; |
148 return true; | 173 return true; |
149 } | 174 } |
150 | 175 |
151 bool SingleSplitView::OnMouseDragged(const MouseEvent& event) { | 176 bool SingleSplitView::OnMouseDragged(const MouseEvent& event) { |
152 if (GetChildViewCount() < 2) | 177 if (GetChildViewCount() < 2) |
153 return false; | 178 return false; |
154 | 179 |
155 int delta_offset = GetPrimaryAxisSize(event.x(), event.y()) - | 180 int delta_offset = GetPrimaryAxisSize(event.x(), event.y()) - |
156 drag_info_.initial_mouse_offset; | 181 drag_info_.initial_mouse_offset; |
157 if (is_horizontal_ && base::i18n::IsRTL()) | 182 if (is_horizontal_ && base::i18n::IsRTL()) |
158 delta_offset *= -1; | 183 delta_offset *= -1; |
159 // Honor the minimum size when resizing. | 184 // Honor the minimum size when resizing. |
160 gfx::Size min = GetChildViewAt(0)->GetMinimumSize(); | 185 gfx::Size min = GetChildViewAt(0)->GetMinimumSize(); |
161 int new_size = std::max(GetPrimaryAxisSize(min.width(), min.height()), | 186 int new_size = std::max(GetPrimaryAxisSize(min.width(), min.height()), |
162 drag_info_.initial_divider_offset + delta_offset); | 187 drag_info_.initial_divider_offset + delta_offset); |
163 | 188 |
164 // And don't let the view get bigger than our width. | 189 // And don't let the view get bigger than our width. |
165 new_size = std::min(GetPrimaryAxisSize() - kDividerSize, new_size); | 190 new_size = std::min(GetPrimaryAxisSize() - kDividerSize, new_size); |
166 | 191 |
167 if (new_size != divider_offset_) { | 192 if (new_size != divider_offset_) { |
168 set_divider_offset(new_size); | 193 set_divider_offset(new_size); |
169 Layout(); | 194 Layout(); |
195 observer_->SplitHandleMoved(this); | |
170 } | 196 } |
171 return true; | 197 return true; |
172 } | 198 } |
173 | 199 |
174 void SingleSplitView::OnMouseReleased(const MouseEvent& event, bool canceled) { | 200 void SingleSplitView::OnMouseReleased(const MouseEvent& event, bool canceled) { |
175 if (GetChildViewCount() < 2) | 201 if (GetChildViewCount() < 2) |
176 return; | 202 return; |
177 | 203 |
178 if (canceled && drag_info_.initial_divider_offset != divider_offset_) { | 204 if (canceled && drag_info_.initial_divider_offset != divider_offset_) { |
179 set_divider_offset(drag_info_.initial_divider_offset); | 205 set_divider_offset(drag_info_.initial_divider_offset); |
180 Layout(); | 206 Layout(); |
207 observer_->SplitHandleMoved(this); | |
181 } | 208 } |
182 } | 209 } |
183 | 210 |
184 bool SingleSplitView::IsPointInDivider(const gfx::Point& p) { | 211 bool SingleSplitView::IsPointInDivider(const gfx::Point& p) { |
185 if (GetChildViewCount() < 2) | 212 if (GetChildViewCount() < 2) |
186 return false; | 213 return false; |
187 | 214 |
188 if (!GetChildViewAt(0)->IsVisible() || !GetChildViewAt(1)->IsVisible()) | 215 if (!GetChildViewAt(0)->IsVisible() || !GetChildViewAt(1)->IsVisible()) |
189 return false; | 216 return false; |
190 | 217 |
191 int divider_relative_offset; | 218 int divider_relative_offset; |
192 if (is_horizontal_) { | 219 if (is_horizontal_) { |
193 divider_relative_offset = | 220 divider_relative_offset = |
194 p.x() - GetChildViewAt(base::i18n::IsRTL() ? 1 : 0)->width(); | 221 p.x() - GetChildViewAt(base::i18n::IsRTL() ? 1 : 0)->width(); |
195 } else { | 222 } else { |
196 divider_relative_offset = p.y() - GetChildViewAt(0)->height(); | 223 divider_relative_offset = p.y() - GetChildViewAt(0)->height(); |
197 } | 224 } |
198 return (divider_relative_offset >= 0 && | 225 return (divider_relative_offset >= 0 && |
199 divider_relative_offset < kDividerSize); | 226 divider_relative_offset < kDividerSize); |
200 } | 227 } |
201 | 228 |
202 } // namespace views | 229 } // namespace views |
OLD | NEW |