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/resize_gripper.h" | 5 #include "views/controls/resize_area.h" |
6 | 6 |
7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "grit/app_resources.h" | |
10 | 9 |
11 namespace views { | 10 namespace views { |
12 | 11 |
13 const char ResizeGripper::kViewClassName[] = "views/ResizeGripper"; | 12 const char ResizeArea::kViewClassName[] = "views/ResizeArea"; |
14 | 13 |
15 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
16 static HCURSOR g_resize_cursor = NULL; | 15 static HCURSOR g_resize_cursor = NULL; |
17 #endif | 16 #endif |
18 | 17 |
19 //////////////////////////////////////////////////////////////////////////////// | 18 //////////////////////////////////////////////////////////////////////////////// |
20 // ResizeGripper | 19 // ResizeArea |
21 | 20 |
22 ResizeGripper::ResizeGripper(ResizeGripperDelegate* delegate) | 21 ResizeArea::ResizeArea(ResizeAreaDelegate* delegate) |
23 : delegate_(delegate), | 22 : delegate_(delegate), |
24 initial_position_(0), | 23 initial_position_(0) { |
25 gripper_visible_(false) { | |
26 ResourceBundle &rb = ResourceBundle::GetSharedInstance(); | |
27 SkBitmap* gripper_image = rb.GetBitmapNamed(IDR_RESIZE_GRIPPER); | |
28 // Explicitly set the image size so that the preferred size is fixed to that | |
29 // of the image. If we didn't do this the preferred size would change | |
30 // depending upon whether the gripper was visible. | |
31 SetImageSize(gfx::Size(gripper_image->width(), gripper_image->height())); | |
32 } | 24 } |
33 | 25 |
34 ResizeGripper::~ResizeGripper() { | 26 ResizeArea::~ResizeArea() { |
35 } | 27 } |
36 | 28 |
37 std::string ResizeGripper::GetClassName() const { | 29 std::string ResizeArea::GetClassName() const { |
38 return kViewClassName; | 30 return kViewClassName; |
39 } | 31 } |
40 | 32 |
41 gfx::NativeCursor ResizeGripper::GetCursorForPoint(Event::EventType event_type, | 33 gfx::NativeCursor ResizeArea::GetCursorForPoint(Event::EventType event_type, |
42 const gfx::Point& p) { | 34 const gfx::Point& p) { |
43 if (!enabled_) | 35 if (!enabled_) |
44 return NULL; | 36 return NULL; |
45 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
46 if (!g_resize_cursor) | 38 if (!g_resize_cursor) |
47 g_resize_cursor = LoadCursor(NULL, IDC_SIZEWE); | 39 g_resize_cursor = LoadCursor(NULL, IDC_SIZEWE); |
48 return g_resize_cursor; | 40 return g_resize_cursor; |
49 #elif defined(OS_LINUX) | 41 #elif defined(OS_LINUX) |
50 return gdk_cursor_new(GDK_SB_H_DOUBLE_ARROW); | 42 return gdk_cursor_new(GDK_SB_H_DOUBLE_ARROW); |
51 #endif | 43 #endif |
52 } | 44 } |
53 | 45 |
54 void ResizeGripper::OnMouseEntered(const views::MouseEvent& event) { | 46 bool ResizeArea::OnMousePressed(const views::MouseEvent& event) { |
55 SetGripperVisible(true); | |
56 } | |
57 | |
58 void ResizeGripper::OnMouseExited(const views::MouseEvent& event) { | |
59 SetGripperVisible(false); | |
60 } | |
61 | |
62 bool ResizeGripper::OnMousePressed(const views::MouseEvent& event) { | |
63 if (!event.IsOnlyLeftMouseButton()) | 47 if (!event.IsOnlyLeftMouseButton()) |
64 return false; | 48 return false; |
65 | 49 |
66 // The resize gripper obviously will move once you start dragging so we need | 50 // The resize area obviously will move once you start dragging so we need to |
67 // to convert coordinates to screen coordinates so that we don't loose our | 51 // convert coordinates to screen coordinates so that we don't lose our |
68 // bearings. | 52 // bearings. |
69 gfx::Point point(event.x(), 0); | 53 gfx::Point point(event.x(), 0); |
70 View::ConvertPointToScreen(this, &point); | 54 View::ConvertPointToScreen(this, &point); |
71 initial_position_ = point.x(); | 55 initial_position_ = point.x(); |
72 | 56 |
73 return true; | 57 return true; |
74 } | 58 } |
75 | 59 |
76 bool ResizeGripper::OnMouseDragged(const views::MouseEvent& event) { | 60 bool ResizeArea::OnMouseDragged(const views::MouseEvent& event) { |
77 if (!event.IsLeftMouseButton()) | 61 if (!event.IsLeftMouseButton()) |
78 return false; | 62 return false; |
79 | 63 |
80 ReportResizeAmount(event.x(), false); | 64 ReportResizeAmount(event.x(), false); |
81 return true; | 65 return true; |
82 } | 66 } |
83 | 67 |
84 void ResizeGripper::OnMouseReleased(const views::MouseEvent& event, | 68 void ResizeArea::OnMouseReleased(const views::MouseEvent& event, |
85 bool canceled) { | 69 bool canceled) { |
86 if (canceled) | 70 ReportResizeAmount(canceled ? initial_position_ : event.x(), true); |
87 ReportResizeAmount(initial_position_, true); | |
88 else | |
89 ReportResizeAmount(event.x(), true); | |
90 SetGripperVisible(HitTest(event.location())); | |
91 } | 71 } |
92 | 72 |
93 bool ResizeGripper::GetAccessibleRole(AccessibilityTypes::Role* role) { | 73 bool ResizeArea::GetAccessibleRole(AccessibilityTypes::Role* role) { |
94 DCHECK(role); | 74 DCHECK(role); |
95 *role = AccessibilityTypes::ROLE_SEPARATOR; | 75 *role = AccessibilityTypes::ROLE_SEPARATOR; |
96 return true; | 76 return true; |
97 } | 77 } |
98 | 78 |
99 void ResizeGripper::ReportResizeAmount(int resize_amount, bool last_update) { | 79 void ResizeArea::ReportResizeAmount(int resize_amount, bool last_update) { |
100 gfx::Point point(resize_amount, 0); | 80 gfx::Point point(resize_amount, 0); |
101 View::ConvertPointToScreen(this, &point); | 81 View::ConvertPointToScreen(this, &point); |
102 resize_amount = point.x() - initial_position_; | 82 resize_amount = point.x() - initial_position_; |
103 | 83 delegate_->OnResize(base::i18n::IsRTL() ? -resize_amount : resize_amount, |
104 if (base::i18n::IsRTL()) | 84 last_update); |
105 resize_amount = -1 * resize_amount; | |
106 delegate_->OnResize(resize_amount, last_update); | |
107 } | |
108 | |
109 void ResizeGripper::SetGripperVisible(bool visible) { | |
110 if (visible == gripper_visible_) | |
111 return; | |
112 | |
113 gripper_visible_ = visible; | |
114 if (gripper_visible_) { | |
115 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
116 SkBitmap* gripper_image = rb.GetBitmapNamed(IDR_RESIZE_GRIPPER); | |
117 SetImage(gripper_image); | |
118 } else { | |
119 SetImage(NULL); | |
120 } | |
121 } | 85 } |
122 | 86 |
123 } // namespace views | 87 } // namespace views |
OLD | NEW |