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