summaryrefslogtreecommitdiffstats
path: root/views/controls/single_split_view.cc
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-08 00:34:05 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-05-08 00:34:05 +0000
commit2362e4fe2905ab75d3230ebc3e307ae53e2b8362 (patch)
treee6d88357a2021811e0e354f618247217be8bb3da /views/controls/single_split_view.cc
parentdb23ac3e713dc17509b2b15d3ee634968da45715 (diff)
downloadchromium_src-2362e4fe2905ab75d3230ebc3e307ae53e2b8362.zip
chromium_src-2362e4fe2905ab75d3230ebc3e307ae53e2b8362.tar.gz
chromium_src-2362e4fe2905ab75d3230ebc3e307ae53e2b8362.tar.bz2
Move src/chrome/views to src/views. RS=darin http://crbug.com/11387
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15604 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views/controls/single_split_view.cc')
-rw-r--r--views/controls/single_split_view.cc116
1 files changed, 116 insertions, 0 deletions
diff --git a/views/controls/single_split_view.cc b/views/controls/single_split_view.cc
new file mode 100644
index 0000000..4558ffd
--- /dev/null
+++ b/views/controls/single_split_view.cc
@@ -0,0 +1,116 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "views/controls/single_split_view.h"
+
+#include "app/gfx/chrome_canvas.h"
+#include "skia/ext/skia_utils_win.h"
+#include "views/background.h"
+
+namespace views {
+
+// Size of the divider in pixels.
+static const int kDividerSize = 4;
+
+SingleSplitView::SingleSplitView(View* leading, View* trailing)
+ : divider_x_(-1) {
+ AddChildView(leading);
+ AddChildView(trailing);
+ set_background(
+ views::Background::CreateSolidBackground(
+ skia::COLORREFToSkColor(GetSysColor(COLOR_3DFACE))));
+}
+
+void SingleSplitView::Layout() {
+ if (GetChildViewCount() != 2)
+ return;
+
+ View* leading = GetChildViewAt(0);
+ View* trailing = GetChildViewAt(1);
+ if (divider_x_ < 0)
+ divider_x_ = (width() - kDividerSize) / 2;
+ else
+ divider_x_ = std::min(divider_x_, width() - kDividerSize);
+ leading->SetBounds(0, 0, divider_x_, height());
+ trailing->SetBounds(divider_x_ + kDividerSize, 0,
+ width() - divider_x_ - kDividerSize, height());
+
+ SchedulePaint();
+
+ // Invoke super's implementation so that the children are layed out.
+ View::Layout();
+}
+
+gfx::Size SingleSplitView::GetPreferredSize() {
+ int width = 0;
+ int height = 0;
+ for (int i = 0; i < 2 && i < GetChildViewCount(); ++i) {
+ View* view = GetChildViewAt(i);
+ gfx::Size pref = view->GetPreferredSize();
+ width += pref.width();
+ height = std::max(height, pref.height());
+ }
+ width += kDividerSize;
+ return gfx::Size(width, height);
+}
+
+HCURSOR SingleSplitView::GetCursorForPoint(Event::EventType event_type,
+ int x,
+ int y) {
+ if (IsPointInDivider(x)) {
+ static HCURSOR resize_cursor = LoadCursor(NULL, IDC_SIZEWE);
+ return resize_cursor;
+ }
+ return NULL;
+}
+
+bool SingleSplitView::OnMousePressed(const MouseEvent& event) {
+ if (!IsPointInDivider(event.x()))
+ return false;
+ drag_info_.initial_mouse_x = event.x();
+ drag_info_.initial_divider_x = divider_x_;
+ return true;
+}
+
+bool SingleSplitView::OnMouseDragged(const MouseEvent& event) {
+ if (GetChildViewCount() < 2)
+ return false;
+
+ int delta_x = event.x() - drag_info_.initial_mouse_x;
+ if (UILayoutIsRightToLeft())
+ delta_x *= -1;
+ // Honor the minimum size when resizing.
+ int new_width = std::max(GetChildViewAt(0)->GetMinimumSize().width(),
+ drag_info_.initial_divider_x + delta_x);
+
+ // And don't let the view get bigger than our width.
+ new_width = std::min(width() - kDividerSize, new_width);
+
+ if (new_width != divider_x_) {
+ set_divider_x(new_width);
+ Layout();
+ }
+ return true;
+}
+
+void SingleSplitView::OnMouseReleased(const MouseEvent& event, bool canceled) {
+ if (GetChildViewCount() < 2)
+ return;
+
+ if (canceled && drag_info_.initial_divider_x != divider_x_) {
+ set_divider_x(drag_info_.initial_divider_x);
+ Layout();
+ }
+}
+
+bool SingleSplitView::IsPointInDivider(int x) {
+ if (GetChildViewCount() < 2)
+ return false;
+
+ int divider_relative_x =
+ x - GetChildViewAt(UILayoutIsRightToLeft() ? 1 : 0)->width();
+ return (divider_relative_x >= 0 && divider_relative_x < kDividerSize);
+}
+
+} // namespace views