| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "app/gfx/canvas.h" | 11 #include "app/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 namespace views { | 15 namespace views { |
| 16 | 16 |
| 17 // Size of the divider in pixels. | 17 // Size of the divider in pixels. |
| 18 static const int kDividerSize = 4; | 18 static const int kDividerSize = 4; |
| 19 | 19 |
| 20 SingleSplitView::SingleSplitView(View* leading, View* trailing) | 20 SingleSplitView::SingleSplitView(View* leading, |
| 21 : divider_x_(-1) { | 21 View* trailing, |
| 22 Orientation orientation) |
| 23 : is_horizontal_(orientation == HORIZONTAL_SPLIT), |
| 24 divider_offset_(-1) { |
| 22 AddChildView(leading); | 25 AddChildView(leading); |
| 23 AddChildView(trailing); | 26 AddChildView(trailing); |
| 24 set_background( | 27 set_background( |
| 25 views::Background::CreateSolidBackground( | 28 views::Background::CreateSolidBackground( |
| 26 skia::COLORREFToSkColor(GetSysColor(COLOR_3DFACE)))); | 29 skia::COLORREFToSkColor(GetSysColor(COLOR_3DFACE)))); |
| 27 } | 30 } |
| 28 | 31 |
| 29 void SingleSplitView::Layout() { | 32 void SingleSplitView::Layout() { |
| 30 if (GetChildViewCount() != 2) | 33 if (GetChildViewCount() != 2) |
| 31 return; | 34 return; |
| 32 | 35 |
| 33 View* leading = GetChildViewAt(0); | 36 View* leading = GetChildViewAt(0); |
| 34 View* trailing = GetChildViewAt(1); | 37 View* trailing = GetChildViewAt(1); |
| 35 if (divider_x_ < 0) | 38 if (divider_offset_ < 0) |
| 36 divider_x_ = (width() - kDividerSize) / 2; | 39 divider_offset_ = (GetPrimaryAxisSize() - kDividerSize) / 2; |
| 37 else | 40 else |
| 38 divider_x_ = std::min(divider_x_, width() - kDividerSize); | 41 divider_offset_ = std::min(divider_offset_, |
| 39 leading->SetBounds(0, 0, divider_x_, height()); | 42 GetPrimaryAxisSize() - kDividerSize); |
| 40 trailing->SetBounds(divider_x_ + kDividerSize, 0, | 43 |
| 41 width() - divider_x_ - kDividerSize, height()); | 44 if (is_horizontal_) { |
| 45 leading->SetBounds(0, 0, divider_offset_, height()); |
| 46 trailing->SetBounds(divider_offset_ + kDividerSize, 0, |
| 47 width() - divider_offset_ - kDividerSize, height()); |
| 48 } else { |
| 49 leading->SetBounds(0, 0, width(), divider_offset_); |
| 50 trailing->SetBounds(0, divider_offset_ + kDividerSize, |
| 51 width(), height() - divider_offset_ - kDividerSize); |
| 52 } |
| 42 | 53 |
| 43 SchedulePaint(); | 54 SchedulePaint(); |
| 44 | 55 |
| 45 // Invoke super's implementation so that the children are layed out. | 56 // Invoke super's implementation so that the children are layed out. |
| 46 View::Layout(); | 57 View::Layout(); |
| 47 } | 58 } |
| 48 | 59 |
| 49 gfx::Size SingleSplitView::GetPreferredSize() { | 60 gfx::Size SingleSplitView::GetPreferredSize() { |
| 50 int width = 0; | 61 int width = 0; |
| 51 int height = 0; | 62 int height = 0; |
| 52 for (int i = 0; i < 2 && i < GetChildViewCount(); ++i) { | 63 for (int i = 0; i < 2 && i < GetChildViewCount(); ++i) { |
| 53 View* view = GetChildViewAt(i); | 64 View* view = GetChildViewAt(i); |
| 54 gfx::Size pref = view->GetPreferredSize(); | 65 gfx::Size pref = view->GetPreferredSize(); |
| 55 width += pref.width(); | 66 if (is_horizontal_) { |
| 56 height = std::max(height, pref.height()); | 67 width += pref.width(); |
| 68 height = std::max(height, pref.height()); |
| 69 } else { |
| 70 width = std::max(width, pref.width()); |
| 71 height += pref.height(); |
| 72 } |
| 57 } | 73 } |
| 58 width += kDividerSize; | 74 if (is_horizontal_) |
| 75 width += kDividerSize; |
| 76 else |
| 77 height += kDividerSize; |
| 59 return gfx::Size(width, height); | 78 return gfx::Size(width, height); |
| 60 } | 79 } |
| 61 | 80 |
| 62 gfx::NativeCursor SingleSplitView::GetCursorForPoint(Event::EventType event_type
, | 81 gfx::NativeCursor SingleSplitView::GetCursorForPoint(Event::EventType event_type
, |
| 63 int x, int y) { | 82 int x, int y) { |
| 64 if (IsPointInDivider(x)) { | 83 if (IsPointInDivider(x, y)) { |
| 65 #if defined(OS_WIN) | 84 #if defined(OS_WIN) |
| 66 static HCURSOR resize_cursor = LoadCursor(NULL, IDC_SIZEWE); | 85 static HCURSOR resize_cursor = LoadCursor(NULL, |
| 86 is_horizontal_ ? IDC_SIZEWE : IDC_SIZENS); |
| 67 return resize_cursor; | 87 return resize_cursor; |
| 68 #elif defined(OS_LINUX) | 88 #elif defined(OS_LINUX) |
| 69 return gdk_cursor_new(GDK_SB_H_DOUBLE_ARROW); | 89 return gdk_cursor_new(is_horizontal_ ? |
| 90 GDK_SB_H_DOUBLE_ARROW : |
| 91 GDK_SB_V_DOUBLE_ARROW); |
| 70 #endif | 92 #endif |
| 71 } | 93 } |
| 72 return NULL; | 94 return NULL; |
| 73 } | 95 } |
| 74 | 96 |
| 75 bool SingleSplitView::OnMousePressed(const MouseEvent& event) { | 97 bool SingleSplitView::OnMousePressed(const MouseEvent& event) { |
| 76 if (!IsPointInDivider(event.x())) | 98 if (!IsPointInDivider(event.x(), event.y())) |
| 77 return false; | 99 return false; |
| 78 drag_info_.initial_mouse_x = event.x(); | 100 drag_info_.initial_mouse_offset = GetPrimaryAxisSize(event.x(), event.y()); |
| 79 drag_info_.initial_divider_x = divider_x_; | 101 drag_info_.initial_divider_offset = divider_offset_; |
| 80 return true; | 102 return true; |
| 81 } | 103 } |
| 82 | 104 |
| 83 bool SingleSplitView::OnMouseDragged(const MouseEvent& event) { | 105 bool SingleSplitView::OnMouseDragged(const MouseEvent& event) { |
| 84 if (GetChildViewCount() < 2) | 106 if (GetChildViewCount() < 2) |
| 85 return false; | 107 return false; |
| 86 | 108 |
| 87 int delta_x = event.x() - drag_info_.initial_mouse_x; | 109 int delta_offset = GetPrimaryAxisSize(event.x(), event.y()) - |
| 88 if (UILayoutIsRightToLeft()) | 110 drag_info_.initial_mouse_offset; |
| 89 delta_x *= -1; | 111 if (is_horizontal_ && UILayoutIsRightToLeft()) |
| 112 delta_offset *= -1; |
| 90 // Honor the minimum size when resizing. | 113 // Honor the minimum size when resizing. |
| 91 int new_width = std::max(GetChildViewAt(0)->GetMinimumSize().width(), | 114 gfx::Size min = GetChildViewAt(0)->GetMinimumSize(); |
| 92 drag_info_.initial_divider_x + delta_x); | 115 int new_size = std::max(GetPrimaryAxisSize(min.width(), min.height()), |
| 116 drag_info_.initial_divider_offset + delta_offset); |
| 93 | 117 |
| 94 // And don't let the view get bigger than our width. | 118 // And don't let the view get bigger than our width. |
| 95 new_width = std::min(width() - kDividerSize, new_width); | 119 new_size = std::min(GetPrimaryAxisSize() - kDividerSize, new_size); |
| 96 | 120 |
| 97 if (new_width != divider_x_) { | 121 if (new_size != divider_offset_) { |
| 98 set_divider_x(new_width); | 122 set_divider_offset(new_size); |
| 99 Layout(); | 123 Layout(); |
| 100 } | 124 } |
| 101 return true; | 125 return true; |
| 102 } | 126 } |
| 103 | 127 |
| 104 void SingleSplitView::OnMouseReleased(const MouseEvent& event, bool canceled) { | 128 void SingleSplitView::OnMouseReleased(const MouseEvent& event, bool canceled) { |
| 105 if (GetChildViewCount() < 2) | 129 if (GetChildViewCount() < 2) |
| 106 return; | 130 return; |
| 107 | 131 |
| 108 if (canceled && drag_info_.initial_divider_x != divider_x_) { | 132 if (canceled && drag_info_.initial_divider_offset != divider_offset_) { |
| 109 set_divider_x(drag_info_.initial_divider_x); | 133 set_divider_offset(drag_info_.initial_divider_offset); |
| 110 Layout(); | 134 Layout(); |
| 111 } | 135 } |
| 112 } | 136 } |
| 113 | 137 |
| 114 bool SingleSplitView::IsPointInDivider(int x) { | 138 bool SingleSplitView::IsPointInDivider(int x, int y) { |
| 115 if (GetChildViewCount() < 2) | 139 if (GetChildViewCount() < 2) |
| 116 return false; | 140 return false; |
| 117 | 141 |
| 118 int divider_relative_x = | 142 int divider_relative_offset; |
| 119 x - GetChildViewAt(UILayoutIsRightToLeft() ? 1 : 0)->width(); | 143 if (is_horizontal_) { |
| 120 return (divider_relative_x >= 0 && divider_relative_x < kDividerSize); | 144 divider_relative_offset = |
| 145 x - GetChildViewAt(UILayoutIsRightToLeft() ? 1 : 0)->width(); |
| 146 } else { |
| 147 divider_relative_offset = y - GetChildViewAt(0)->height(); |
| 148 } |
| 149 return (divider_relative_offset >= 0 && |
| 150 divider_relative_offset < kDividerSize); |
| 121 } | 151 } |
| 122 | 152 |
| 123 } // namespace views | 153 } // namespace views |
| OLD | NEW |