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