1 // Copyright (c) 2012 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 "ui/views/controls/resize_area.h"
6
7 #include "base/logging.h"
8 #include "ui/accessibility/ax_view_state.h"
9 #include "ui/base/cursor/cursor.h"
10 #include "ui/views/controls/resize_area_delegate.h"
11 #include "ui/views/native_cursor.h"
12
13 namespace views {
14
15 const char ResizeArea::kViewClassName[] = "ResizeArea";
16
17 ////////////////////////////////////////////////////////////////////////////////
18 // ResizeArea
19
ResizeArea(ResizeAreaDelegate * delegate)20 ResizeArea::ResizeArea(ResizeAreaDelegate* delegate)
21 : delegate_(delegate),
22 initial_position_(0) {
23 }
24
~ResizeArea()25 ResizeArea::~ResizeArea() {
26 }
27
GetClassName() const28 const char* ResizeArea::GetClassName() const {
29 return kViewClassName;
30 }
31
GetCursor(const ui::MouseEvent & event)32 gfx::NativeCursor ResizeArea::GetCursor(const ui::MouseEvent& event) {
33 return enabled() ? GetNativeEastWestResizeCursor()
34 : gfx::kNullCursor;
35 }
36
OnMousePressed(const ui::MouseEvent & event)37 bool ResizeArea::OnMousePressed(const ui::MouseEvent& event) {
38 if (!event.IsOnlyLeftMouseButton())
39 return false;
40
41 // The resize area obviously will move once you start dragging so we need to
42 // convert coordinates to screen coordinates so that we don't lose our
43 // bearings.
44 gfx::Point point(event.x(), 0);
45 View::ConvertPointToScreen(this, &point);
46 initial_position_ = point.x();
47
48 return true;
49 }
50
OnMouseDragged(const ui::MouseEvent & event)51 bool ResizeArea::OnMouseDragged(const ui::MouseEvent& event) {
52 if (!event.IsLeftMouseButton())
53 return false;
54
55 ReportResizeAmount(event.x(), false);
56 return true;
57 }
58
OnMouseReleased(const ui::MouseEvent & event)59 void ResizeArea::OnMouseReleased(const ui::MouseEvent& event) {
60 ReportResizeAmount(event.x(), true);
61 }
62
OnMouseCaptureLost()63 void ResizeArea::OnMouseCaptureLost() {
64 ReportResizeAmount(initial_position_, true);
65 }
66
GetAccessibleState(ui::AXViewState * state)67 void ResizeArea::GetAccessibleState(ui::AXViewState* state) {
68 state->role = ui::AX_ROLE_SPLITTER;
69 }
70
ReportResizeAmount(int resize_amount,bool last_update)71 void ResizeArea::ReportResizeAmount(int resize_amount, bool last_update) {
72 gfx::Point point(resize_amount, 0);
73 View::ConvertPointToScreen(this, &point);
74 resize_amount = point.x() - initial_position_;
75 delegate_->OnResize(base::i18n::IsRTL() ? -resize_amount : resize_amount,
76 last_update);
77 }
78
79 } // namespace views
80