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 #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) | |
| 27 : is_horizontal_(orientation == HORIZONTAL_SPLIT), | 28 : is_horizontal_(orientation == HORIZONTAL_SPLIT), |
| 28 divider_offset_(-1), | 29 divider_offset_(-1), |
| 29 resize_leading_on_bounds_change_(true) { | 30 resize_leading_on_bounds_change_(true), |
| 31 observer_(observer) { | |
| 30 AddChildView(leading); | 32 AddChildView(leading); |
| 31 AddChildView(trailing); | 33 AddChildView(trailing); |
| 32 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 33 set_background( | 35 set_background( |
| 34 views::Background::CreateSolidBackground( | 36 views::Background::CreateSolidBackground( |
| 35 skia::COLORREFToSkColor(GetSysColor(COLOR_3DFACE)))); | 37 skia::COLORREFToSkColor(GetSysColor(COLOR_3DFACE)))); |
| 36 #endif | 38 #endif |
| 37 } | 39 } |
| 38 | 40 |
| 39 void SingleSplitView::DidChangeBounds(const gfx::Rect& previous, | 41 void SingleSplitView::DidChangeBounds(const gfx::Rect& previous, |
| 40 const gfx::Rect& current) { | 42 const gfx::Rect& current) { |
| 41 if (resize_leading_on_bounds_change_) { | 43 if (resize_leading_on_bounds_change_) { |
| 42 // We do not update divider_offset_ on minimize (to zero) and on restore | 44 // 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 | 45 // (to largest value). As a result we get back to the original value upon |
| 44 // window restore. | 46 // window restore. |
| 45 bool is_minimize_or_restore = | 47 bool is_minimize_or_restore = |
| 46 previous.height() == 0 || current.height() == 0; | 48 previous.height() == 0 || current.height() == 0; |
| 47 if (!is_minimize_or_restore) { | 49 if (!is_minimize_or_restore) { |
| 50 divider_offset_ = NormalizeDividerOffset(divider_offset_); | |
|
sky
2011/01/07 21:35:40
I think this'll do the wrong thing on a large cont
Aleksey Shlyapnikov
2011/01/07 23:06:00
Thanks for catching this, fixed.
| |
| 48 if (is_horizontal_) | 51 if (is_horizontal_) |
| 49 divider_offset_ += current.width() - previous.width(); | 52 divider_offset_ += current.width() - previous.width(); |
| 50 else | 53 else |
| 51 divider_offset_ += current.height() - previous.height(); | 54 divider_offset_ += current.height() - previous.height(); |
| 52 | 55 |
| 53 if (divider_offset_ < 0) | 56 if (divider_offset_ < 0) |
| 54 divider_offset_ = kDividerSize; | 57 divider_offset_ = kDividerSize; |
| 55 } | 58 } |
| 56 } | 59 } |
| 57 View::DidChangeBounds(previous, current); | 60 View::DidChangeBounds(previous, current); |
| 58 } | 61 } |
| 59 | 62 |
| 60 void SingleSplitView::Layout() { | 63 void SingleSplitView::Layout() { |
| 61 if (GetChildViewCount() != 2) | 64 // When provided, it's an observer's responsiblity to layout |
| 65 // children views, SingleSplitView should not interfere in this case. | |
| 66 if (observer_) | |
|
sky
2011/01/07 21:35:40
I'm confused by this. Why isn't layout always doin
Aleksey Shlyapnikov
2011/01/07 23:06:00
Correct, but it's a bit more complex than that. Br
Aleksey Shlyapnikov
2011/01/08 23:54:41
Please give it another look, I uploaded the altern
| |
| 62 return; | 67 return; |
| 63 | 68 |
| 64 View* leading = GetChildViewAt(0); | 69 gfx::Rect leading_bounds; |
| 65 View* trailing = GetChildViewAt(1); | 70 gfx::Rect trailing_bounds; |
| 71 CalculateChildrenBounds(&leading_bounds, &trailing_bounds); | |
| 66 | 72 |
| 67 if (!leading->IsVisible() && !trailing->IsVisible()) | 73 ResizeChildren(leading_bounds, trailing_bounds); |
| 68 return; | |
| 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); | |
| 92 } | |
| 93 } | |
| 94 | 74 |
| 95 SchedulePaint(); | 75 SchedulePaint(); |
| 96 | 76 |
| 97 // Invoke super's implementation so that the children are layed out. | 77 // Invoke super's implementation so that the children are layed out. |
| 98 View::Layout(); | 78 View::Layout(); |
| 99 } | 79 } |
| 100 | 80 |
| 101 AccessibilityTypes::Role SingleSplitView::GetAccessibleRole() { | 81 AccessibilityTypes::Role SingleSplitView::GetAccessibleRole() { |
| 102 return AccessibilityTypes::ROLE_GROUPING; | 82 return AccessibilityTypes::ROLE_GROUPING; |
| 103 } | 83 } |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 133 return is_horizontal_ ? we_resize_cursor : ns_resize_cursor; | 113 return is_horizontal_ ? we_resize_cursor : ns_resize_cursor; |
| 134 #elif defined(OS_LINUX) | 114 #elif defined(OS_LINUX) |
| 135 return gfx::GetCursor(is_horizontal_ ? | 115 return gfx::GetCursor(is_horizontal_ ? |
| 136 GDK_SB_H_DOUBLE_ARROW : | 116 GDK_SB_H_DOUBLE_ARROW : |
| 137 GDK_SB_V_DOUBLE_ARROW); | 117 GDK_SB_V_DOUBLE_ARROW); |
| 138 #endif | 118 #endif |
| 139 } | 119 } |
| 140 return NULL; | 120 return NULL; |
| 141 } | 121 } |
| 142 | 122 |
| 123 void SingleSplitView::CalculateChildrenBounds( | |
| 124 gfx::Rect* leading_bounds, gfx::Rect* trailing_bounds) const { | |
| 125 bool is_leading_visible = | |
| 126 GetChildViewCount() > 0 && GetChildViewAt(0)->IsVisible(); | |
| 127 bool is_trailing_visible = | |
| 128 GetChildViewCount() > 1 && GetChildViewAt(1)->IsVisible(); | |
| 129 | |
| 130 if (!is_leading_visible && !is_trailing_visible) { | |
| 131 *leading_bounds = gfx::Rect(); | |
|
sky
2011/01/07 21:35:40
nit: set the bounds to empty first thing. That way
Aleksey Shlyapnikov
2011/01/07 23:06:00
This is a very rare case, why should we penalize t
| |
| 132 *trailing_bounds = gfx::Rect(); | |
| 133 return; | |
| 134 } | |
| 135 | |
| 136 int divider_at; | |
| 137 int divider_size = | |
| 138 !is_leading_visible || !is_trailing_visible ? 0 : kDividerSize; | |
| 139 | |
| 140 if (!is_trailing_visible) { | |
| 141 divider_at = GetPrimaryAxisSize(); | |
| 142 } else if (!is_leading_visible) { | |
| 143 divider_at = 0; | |
| 144 } else { | |
| 145 divider_at = NormalizeDividerOffset(divider_offset_); | |
| 146 } | |
| 147 | |
| 148 if (is_horizontal_) { | |
| 149 *leading_bounds = gfx::Rect(0, 0, divider_at, height()); | |
| 150 *trailing_bounds = | |
| 151 gfx::Rect(divider_at + divider_size, 0, | |
| 152 width() - divider_at - divider_size, height()); | |
| 153 } else { | |
| 154 *leading_bounds = gfx::Rect(0, 0, width(), divider_at); | |
| 155 *trailing_bounds = | |
| 156 gfx::Rect(0, divider_at + divider_size, | |
| 157 width(), height() - divider_at - divider_size); | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 void SingleSplitView::ResizeChildren(const gfx::Rect& leading_bounds, | |
| 162 const gfx::Rect& trailing_bounds) const { | |
| 163 if (GetChildViewCount() > 0) { | |
| 164 if (GetChildViewAt(0)->IsVisible()) | |
| 165 GetChildViewAt(0)->SetBounds(leading_bounds); | |
| 166 if (GetChildViewCount() > 1) { | |
| 167 if (GetChildViewAt(1)->IsVisible()) | |
| 168 GetChildViewAt(1)->SetBounds(trailing_bounds); | |
| 169 } | |
| 170 } | |
| 171 } | |
| 172 | |
| 143 bool SingleSplitView::OnMousePressed(const MouseEvent& event) { | 173 bool SingleSplitView::OnMousePressed(const MouseEvent& event) { |
| 144 if (!IsPointInDivider(event.location())) | 174 if (!IsPointInDivider(event.location())) |
| 145 return false; | 175 return false; |
| 146 drag_info_.initial_mouse_offset = GetPrimaryAxisSize(event.x(), event.y()); | 176 drag_info_.initial_mouse_offset = GetPrimaryAxisSize(event.x(), event.y()); |
| 147 drag_info_.initial_divider_offset = divider_offset_; | 177 drag_info_.initial_divider_offset = NormalizeDividerOffset(divider_offset_); |
| 148 return true; | 178 return true; |
| 149 } | 179 } |
| 150 | 180 |
| 151 bool SingleSplitView::OnMouseDragged(const MouseEvent& event) { | 181 bool SingleSplitView::OnMouseDragged(const MouseEvent& event) { |
| 152 if (GetChildViewCount() < 2) | 182 if (GetChildViewCount() < 2) |
| 153 return false; | 183 return false; |
| 154 | 184 |
| 155 int delta_offset = GetPrimaryAxisSize(event.x(), event.y()) - | 185 int delta_offset = GetPrimaryAxisSize(event.x(), event.y()) - |
| 156 drag_info_.initial_mouse_offset; | 186 drag_info_.initial_mouse_offset; |
| 157 if (is_horizontal_ && base::i18n::IsRTL()) | 187 if (is_horizontal_ && base::i18n::IsRTL()) |
| 158 delta_offset *= -1; | 188 delta_offset *= -1; |
| 159 // Honor the minimum size when resizing. | 189 // Honor the minimum size when resizing. |
| 160 gfx::Size min = GetChildViewAt(0)->GetMinimumSize(); | 190 gfx::Size min = GetChildViewAt(0)->GetMinimumSize(); |
| 161 int new_size = std::max(GetPrimaryAxisSize(min.width(), min.height()), | 191 int new_size = std::max(GetPrimaryAxisSize(min.width(), min.height()), |
| 162 drag_info_.initial_divider_offset + delta_offset); | 192 drag_info_.initial_divider_offset + delta_offset); |
| 163 | 193 |
| 164 // And don't let the view get bigger than our width. | 194 // And don't let the view get bigger than our width. |
| 165 new_size = std::min(GetPrimaryAxisSize() - kDividerSize, new_size); | 195 new_size = std::min(GetPrimaryAxisSize() - kDividerSize, new_size); |
| 166 | 196 |
| 167 if (new_size != divider_offset_) { | 197 if (new_size != divider_offset_) { |
| 168 set_divider_offset(new_size); | 198 set_divider_offset(new_size); |
| 169 Layout(); | 199 if (!observer_) |
| 200 Layout(); | |
| 201 else | |
| 202 observer_->SplitHandleMoved(this); | |
| 170 } | 203 } |
| 171 return true; | 204 return true; |
| 172 } | 205 } |
| 173 | 206 |
| 174 void SingleSplitView::OnMouseReleased(const MouseEvent& event, bool canceled) { | 207 void SingleSplitView::OnMouseReleased(const MouseEvent& event, bool canceled) { |
| 175 if (GetChildViewCount() < 2) | 208 if (GetChildViewCount() < 2) |
| 176 return; | 209 return; |
| 177 | 210 |
| 178 if (canceled && drag_info_.initial_divider_offset != divider_offset_) { | 211 if (canceled && drag_info_.initial_divider_offset != divider_offset_) { |
| 179 set_divider_offset(drag_info_.initial_divider_offset); | 212 set_divider_offset(drag_info_.initial_divider_offset); |
| 180 Layout(); | 213 if (!observer_) |
| 214 Layout(); | |
| 215 else | |
| 216 observer_->SplitHandleMoved(this); | |
| 181 } | 217 } |
| 182 } | 218 } |
| 183 | 219 |
| 184 bool SingleSplitView::IsPointInDivider(const gfx::Point& p) { | 220 bool SingleSplitView::IsPointInDivider(const gfx::Point& p) { |
| 185 if (GetChildViewCount() < 2) | 221 if (GetChildViewCount() < 2) |
| 186 return false; | 222 return false; |
| 187 | 223 |
| 188 if (!GetChildViewAt(0)->IsVisible() || !GetChildViewAt(1)->IsVisible()) | 224 if (!GetChildViewAt(0)->IsVisible() || !GetChildViewAt(1)->IsVisible()) |
| 189 return false; | 225 return false; |
| 190 | 226 |
| 191 int divider_relative_offset; | 227 int divider_relative_offset; |
| 192 if (is_horizontal_) { | 228 if (is_horizontal_) { |
| 193 divider_relative_offset = | 229 divider_relative_offset = |
| 194 p.x() - GetChildViewAt(base::i18n::IsRTL() ? 1 : 0)->width(); | 230 p.x() - GetChildViewAt(base::i18n::IsRTL() ? 1 : 0)->width(); |
| 195 } else { | 231 } else { |
| 196 divider_relative_offset = p.y() - GetChildViewAt(0)->height(); | 232 divider_relative_offset = p.y() - GetChildViewAt(0)->height(); |
| 197 } | 233 } |
| 198 return (divider_relative_offset >= 0 && | 234 return (divider_relative_offset >= 0 && |
| 199 divider_relative_offset < kDividerSize); | 235 divider_relative_offset < kDividerSize); |
| 200 } | 236 } |
| 201 | 237 |
| 238 int SingleSplitView::NormalizeDividerOffset(int divider_offset) const { | |
| 239 if (divider_offset < 0) | |
| 240 return (GetPrimaryAxisSize() - kDividerSize) / 2; | |
| 241 return std::min(divider_offset, | |
| 242 std::max(GetPrimaryAxisSize() - kDividerSize, 0)); | |
| 243 } | |
| 244 | |
| 202 } // namespace views | 245 } // namespace views |
| OLD | NEW |