OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ui/views/controls/button/menu_button.h" | 5 #include "ui/views/controls/button/menu_button.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "grit/ui_resources.h" | 8 #include "grit/ui_resources.h" |
9 #include "grit/ui_strings.h" | 9 #include "grit/ui_strings.h" |
10 #include "ui/accessibility/ax_view_state.h" | 10 #include "ui/accessibility/ax_view_state.h" |
11 #include "ui/base/dragdrop/drag_drop_types.h" | 11 #include "ui/base/dragdrop/drag_drop_types.h" |
12 #include "ui/base/l10n/l10n_util.h" | 12 #include "ui/base/l10n/l10n_util.h" |
13 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
14 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
15 #include "ui/events/event_constants.h" | 15 #include "ui/events/event_constants.h" |
16 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
17 #include "ui/gfx/image/image.h" | 17 #include "ui/gfx/image/image.h" |
18 #include "ui/gfx/screen.h" | 18 #include "ui/gfx/screen.h" |
19 #include "ui/gfx/text_constants.h" | |
20 #include "ui/views/controls/button/button.h" | 19 #include "ui/views/controls/button/button.h" |
21 #include "ui/views/controls/button/menu_button_listener.h" | 20 #include "ui/views/controls/button/menu_button_listener.h" |
22 #include "ui/views/mouse_constants.h" | 21 #include "ui/views/mouse_constants.h" |
23 #include "ui/views/widget/root_view.h" | 22 #include "ui/views/widget/root_view.h" |
24 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
25 | 24 |
26 using base::TimeTicks; | 25 using base::TimeTicks; |
27 using base::TimeDelta; | 26 using base::TimeDelta; |
28 | 27 |
29 namespace views { | 28 namespace views { |
(...skipping 10 matching lines...) Expand all Loading... |
40 //////////////////////////////////////////////////////////////////////////////// | 39 //////////////////////////////////////////////////////////////////////////////// |
41 // | 40 // |
42 // MenuButton - constructors, destructors, initialization | 41 // MenuButton - constructors, destructors, initialization |
43 // | 42 // |
44 //////////////////////////////////////////////////////////////////////////////// | 43 //////////////////////////////////////////////////////////////////////////////// |
45 | 44 |
46 MenuButton::MenuButton(ButtonListener* listener, | 45 MenuButton::MenuButton(ButtonListener* listener, |
47 const base::string16& text, | 46 const base::string16& text, |
48 MenuButtonListener* menu_button_listener, | 47 MenuButtonListener* menu_button_listener, |
49 bool show_menu_marker) | 48 bool show_menu_marker) |
50 : LabelButton(listener, text), | 49 : TextButton(listener, text), |
51 menu_visible_(false), | 50 menu_visible_(false), |
52 menu_offset_(kDefaultMenuOffsetX, kDefaultMenuOffsetY), | 51 menu_offset_(kDefaultMenuOffsetX, kDefaultMenuOffsetY), |
53 listener_(menu_button_listener), | 52 listener_(menu_button_listener), |
54 show_menu_marker_(show_menu_marker), | 53 show_menu_marker_(show_menu_marker), |
55 menu_marker_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 54 menu_marker_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
56 IDR_MENU_DROPARROW).ToImageSkia()), | 55 IDR_MENU_DROPARROW).ToImageSkia()), |
57 destroyed_flag_(NULL) { | 56 destroyed_flag_(NULL) { |
58 SetHorizontalAlignment(gfx::ALIGN_LEFT); | 57 set_alignment(TextButton::ALIGN_LEFT); |
59 } | 58 } |
60 | 59 |
61 MenuButton::~MenuButton() { | 60 MenuButton::~MenuButton() { |
62 if (destroyed_flag_) | 61 if (destroyed_flag_) |
63 *destroyed_flag_ = true; | 62 *destroyed_flag_ = true; |
64 } | 63 } |
65 | 64 |
66 //////////////////////////////////////////////////////////////////////////////// | 65 //////////////////////////////////////////////////////////////////////////////// |
67 // | 66 // |
68 // MenuButton - Public APIs | 67 // MenuButton - Public APIs |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 SetState(STATE_NORMAL); | 126 SetState(STATE_NORMAL); |
128 | 127 |
129 // We must return false here so that the RootView does not get stuck | 128 // We must return false here so that the RootView does not get stuck |
130 // sending all mouse pressed events to us instead of the appropriate | 129 // sending all mouse pressed events to us instead of the appropriate |
131 // target. | 130 // target. |
132 return false; | 131 return false; |
133 } | 132 } |
134 return true; | 133 return true; |
135 } | 134 } |
136 | 135 |
137 void MenuButton::OnPaint(gfx::Canvas* canvas) { | 136 void MenuButton::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { |
138 LabelButton::OnPaint(canvas); | 137 TextButton::PaintButton(canvas, mode); |
139 | 138 |
140 if (show_menu_marker_) | 139 if (show_menu_marker_) |
141 PaintMenuMarker(canvas); | 140 PaintMenuMarker(canvas); |
142 } | 141 } |
143 | 142 |
144 //////////////////////////////////////////////////////////////////////////////// | 143 //////////////////////////////////////////////////////////////////////////////// |
145 // | 144 // |
146 // MenuButton - Events | 145 // MenuButton - Events |
147 // | 146 // |
148 //////////////////////////////////////////////////////////////////////////////// | 147 //////////////////////////////////////////////////////////////////////////////// |
149 | 148 |
150 gfx::Size MenuButton::GetPreferredSize() const { | 149 gfx::Size MenuButton::GetPreferredSize() const { |
151 gfx::Size prefsize = LabelButton::GetPreferredSize(); | 150 gfx::Size prefsize = TextButton::GetPreferredSize(); |
152 if (show_menu_marker_) { | 151 if (show_menu_marker_) { |
153 prefsize.Enlarge(menu_marker_->width() + kMenuMarkerPaddingLeft + | 152 prefsize.Enlarge(menu_marker_->width() + kMenuMarkerPaddingLeft + |
154 kMenuMarkerPaddingRight, | 153 kMenuMarkerPaddingRight, |
155 0); | 154 0); |
156 } | 155 } |
157 return prefsize; | 156 return prefsize; |
158 } | 157 } |
159 | 158 |
160 const char* MenuButton::GetClassName() const { | 159 const char* MenuButton::GetClassName() const { |
161 return kViewClassName; | 160 return kViewClassName; |
(...skipping 18 matching lines...) Expand all Loading... |
180 void MenuButton::OnMouseReleased(const ui::MouseEvent& event) { | 179 void MenuButton::OnMouseReleased(const ui::MouseEvent& event) { |
181 // Explicitly test for left mouse button to show the menu. If we tested for | 180 // Explicitly test for left mouse button to show the menu. If we tested for |
182 // !IsTriggerableEvent it could lead to a situation where we end up showing | 181 // !IsTriggerableEvent it could lead to a situation where we end up showing |
183 // the menu and context menu (this would happen if the right button is not | 182 // the menu and context menu (this would happen if the right button is not |
184 // triggerable and there's a context menu). | 183 // triggerable and there's a context menu). |
185 if (GetDragOperations(event.location()) != ui::DragDropTypes::DRAG_NONE && | 184 if (GetDragOperations(event.location()) != ui::DragDropTypes::DRAG_NONE && |
186 state() != STATE_DISABLED && !InDrag() && event.IsOnlyLeftMouseButton() && | 185 state() != STATE_DISABLED && !InDrag() && event.IsOnlyLeftMouseButton() && |
187 HitTestPoint(event.location())) { | 186 HitTestPoint(event.location())) { |
188 Activate(); | 187 Activate(); |
189 } else { | 188 } else { |
190 LabelButton::OnMouseReleased(event); | 189 TextButton::OnMouseReleased(event); |
191 } | 190 } |
192 } | 191 } |
193 | 192 |
194 // The reason we override View::OnMouseExited is because we get this event when | 193 // The reason we override View::OnMouseExited is because we get this event when |
195 // we display the menu. If we don't override this method then | 194 // we display the menu. If we don't override this method then |
196 // BaseButton::OnMouseExited will get the event and will set the button's state | 195 // BaseButton::OnMouseExited will get the event and will set the button's state |
197 // to STATE_NORMAL instead of keeping the state BM_PUSHED. This, in turn, will | 196 // to STATE_NORMAL instead of keeping the state BM_PUSHED. This, in turn, will |
198 // cause the button to appear depressed while the menu is displayed. | 197 // cause the button to appear depressed while the menu is displayed. |
199 void MenuButton::OnMouseExited(const ui::MouseEvent& event) { | 198 void MenuButton::OnMouseExited(const ui::MouseEvent& event) { |
200 if ((state_ != STATE_DISABLED) && (!menu_visible_) && (!InDrag())) { | 199 if ((state_ != STATE_DISABLED) && (!menu_visible_) && (!InDrag())) { |
201 SetState(STATE_NORMAL); | 200 SetState(STATE_NORMAL); |
202 } | 201 } |
203 } | 202 } |
204 | 203 |
205 void MenuButton::OnGestureEvent(ui::GestureEvent* event) { | 204 void MenuButton::OnGestureEvent(ui::GestureEvent* event) { |
206 if (state() != STATE_DISABLED && event->type() == ui::ET_GESTURE_TAP && | 205 if (state() != STATE_DISABLED && event->type() == ui::ET_GESTURE_TAP && |
207 !Activate()) { | 206 !Activate()) { |
208 // When |Activate()| returns |false|, it means that a menu is shown and | 207 // When |Activate()| returns |false|, it means that a menu is shown and |
209 // has handled the gesture event. So, there is no need to further process | 208 // has handled the gesture event. So, there is no need to further process |
210 // the gesture event here. | 209 // the gesture event here. |
211 return; | 210 return; |
212 } | 211 } |
213 LabelButton::OnGestureEvent(event); | 212 TextButton::OnGestureEvent(event); |
214 } | 213 } |
215 | 214 |
216 bool MenuButton::OnKeyPressed(const ui::KeyEvent& event) { | 215 bool MenuButton::OnKeyPressed(const ui::KeyEvent& event) { |
217 switch (event.key_code()) { | 216 switch (event.key_code()) { |
218 case ui::VKEY_SPACE: | 217 case ui::VKEY_SPACE: |
219 // Alt-space on windows should show the window menu. | 218 // Alt-space on windows should show the window menu. |
220 if (event.IsAltDown()) | 219 if (event.IsAltDown()) |
221 break; | 220 break; |
222 case ui::VKEY_RETURN: | 221 case ui::VKEY_RETURN: |
223 case ui::VKEY_UP: | 222 case ui::VKEY_UP: |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 // manually mirror the position of the down arrow. | 258 // manually mirror the position of the down arrow. |
260 gfx::Rect arrow_bounds(width() - insets.right() - | 259 gfx::Rect arrow_bounds(width() - insets.right() - |
261 menu_marker_->width() - kMenuMarkerPaddingRight, | 260 menu_marker_->width() - kMenuMarkerPaddingRight, |
262 height() / 2 - menu_marker_->height() / 2, | 261 height() / 2 - menu_marker_->height() / 2, |
263 menu_marker_->width(), | 262 menu_marker_->width(), |
264 menu_marker_->height()); | 263 menu_marker_->height()); |
265 arrow_bounds.set_x(GetMirroredXForRect(arrow_bounds)); | 264 arrow_bounds.set_x(GetMirroredXForRect(arrow_bounds)); |
266 canvas->DrawImageInt(*menu_marker_, arrow_bounds.x(), arrow_bounds.y()); | 265 canvas->DrawImageInt(*menu_marker_, arrow_bounds.x(), arrow_bounds.y()); |
267 } | 266 } |
268 | 267 |
269 gfx::Rect MenuButton::GetChildAreaBounds() { | |
270 gfx::Size s = size(); | |
271 | |
272 if (show_menu_marker_) { | |
273 s.set_width(s.width() - menu_marker_->width() - kMenuMarkerPaddingLeft - | |
274 kMenuMarkerPaddingRight); | |
275 } | |
276 | |
277 return gfx::Rect(s); | |
278 } | |
279 | |
280 int MenuButton::GetMaximumScreenXCoordinate() { | 268 int MenuButton::GetMaximumScreenXCoordinate() { |
281 if (!GetWidget()) { | 269 if (!GetWidget()) { |
282 NOTREACHED(); | 270 NOTREACHED(); |
283 return 0; | 271 return 0; |
284 } | 272 } |
285 | 273 |
286 gfx::Rect monitor_bounds = GetWidget()->GetWorkAreaBoundsInScreen(); | 274 gfx::Rect monitor_bounds = GetWidget()->GetWorkAreaBoundsInScreen(); |
287 return monitor_bounds.right() - 1; | 275 return monitor_bounds.right() - 1; |
288 } | 276 } |
289 | 277 |
290 } // namespace views | 278 } // namespace views |
OLD | NEW |