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/button/menu_button.h" 6 7 #include "base/strings/utf_string_conversions.h" 8 #include "grit/ui_resources.h" 9 #include "grit/ui_strings.h" 10 #include "ui/base/accessibility/accessible_view_state.h" 11 #include "ui/base/dragdrop/drag_drop_types.h" 12 #include "ui/base/events/event.h" 13 #include "ui/base/events/event_constants.h" 14 #include "ui/base/l10n/l10n_util.h" 15 #include "ui/base/resource/resource_bundle.h" 16 #include "ui/gfx/canvas.h" 17 #include "ui/gfx/image/image.h" 18 #include "ui/gfx/screen.h" 19 #include "ui/views/controls/button/button.h" 20 #include "ui/views/controls/button/menu_button_listener.h" 21 #include "ui/views/widget/root_view.h" 22 #include "ui/views/widget/widget.h" 23 24 using base::Time; 25 using base::TimeDelta; 26 27 namespace views { 28 29 // Default menu offset. 30 static const int kDefaultMenuOffsetX = -2; 31 static const int kDefaultMenuOffsetY = -4; 32 33 // static 34 const char MenuButton::kViewClassName[] = "MenuButton"; 35 const int64 MenuButton::kMinimumTimeBetweenButtonClicks = 100; 36 const int MenuButton::kMenuMarkerPaddingLeft = 3; 37 const int MenuButton::kMenuMarkerPaddingRight = -1; 38 39 //////////////////////////////////////////////////////////////////////////////// 40 // 41 // MenuButton - constructors, destructors, initialization 42 // 43 //////////////////////////////////////////////////////////////////////////////// 44 45 MenuButton::MenuButton(ButtonListener* listener, 46 const string16& text, 47 MenuButtonListener* menu_button_listener, 48 bool show_menu_marker) 49 : TextButton(listener, text), 50 menu_visible_(false), 51 menu_offset_(kDefaultMenuOffsetX, kDefaultMenuOffsetY), 52 listener_(menu_button_listener), 53 show_menu_marker_(show_menu_marker), 54 menu_marker_(ui::ResourceBundle::GetSharedInstance().GetImageNamed( 55 IDR_MENU_DROPARROW).ToImageSkia()), 56 destroyed_flag_(NULL) { 57 set_alignment(TextButton::ALIGN_LEFT); 58 } 59 60 MenuButton::~MenuButton() { 61 if (destroyed_flag_) 62 *destroyed_flag_ = true; 63 } 64 65 //////////////////////////////////////////////////////////////////////////////// 66 // 67 // MenuButton - Public APIs 68 // 69 //////////////////////////////////////////////////////////////////////////////// 70 71 bool MenuButton::Activate() { 72 SetState(STATE_PRESSED); 73 if (listener_) { 74 gfx::Rect lb = GetLocalBounds(); 75 76 // The position of the menu depends on whether or not the locale is 77 // right-to-left. 78 gfx::Point menu_position(lb.right(), lb.bottom()); 79 if (base::i18n::IsRTL()) 80 menu_position.set_x(lb.x()); 81 82 View::ConvertPointToScreen(this, &menu_position); 83 if (base::i18n::IsRTL()) 84 menu_position.Offset(-menu_offset_.x(), menu_offset_.y()); 85 else 86 menu_position.Offset(menu_offset_.x(), menu_offset_.y()); 87 88 int max_x_coordinate = GetMaximumScreenXCoordinate(); 89 if (max_x_coordinate && max_x_coordinate <= menu_position.x()) 90 menu_position.set_x(max_x_coordinate - 1); 91 92 // We're about to show the menu from a mouse press. By showing from the 93 // mouse press event we block RootView in mouse dispatching. This also 94 // appears to cause RootView to get a mouse pressed BEFORE the mouse 95 // release is seen, which means RootView sends us another mouse press no 96 // matter where the user pressed. To force RootView to recalculate the 97 // mouse target during the mouse press we explicitly set the mouse handler 98 // to NULL. 99 static_cast<internal::RootView*>(GetWidget()->GetRootView())-> 100 SetMouseHandler(NULL); 101 102 menu_visible_ = true; 103 104 bool destroyed = false; 105 destroyed_flag_ = &destroyed; 106 107 listener_->OnMenuButtonClicked(this, menu_position); 108 109 if (destroyed) { 110 // The menu was deleted while showing. Don't attempt any processing. 111 return false; 112 } 113 114 destroyed_flag_ = NULL; 115 116 menu_visible_ = false; 117 menu_closed_time_ = Time::Now(); 118 119 // Now that the menu has closed, we need to manually reset state to 120 // "normal" since the menu modal loop will have prevented normal 121 // mouse move messages from getting to this View. We set "normal" 122 // and not "hot" because the likelihood is that the mouse is now 123 // somewhere else (user clicked elsewhere on screen to close the menu 124 // or selected an item) and we will inevitably refresh the hot state 125 // in the event the mouse _is_ over the view. 126 SetState(STATE_NORMAL); 127 128 // 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 // target. 131 return false; 132 } 133 return true; 134 } 135 136 void MenuButton::PaintButton(gfx::Canvas* canvas, PaintButtonMode mode) { 137 TextButton::PaintButton(canvas, mode); 138 139 if (show_menu_marker_) { 140 gfx::Insets insets = GetInsets(); 141 142 // We can not use the views' mirroring infrastructure for mirroring a 143 // MenuButton control (see TextButton::OnPaint() for a detailed explanation 144 // regarding why we can not flip the canvas). Therefore, we need to 145 // manually mirror the position of the down arrow. 146 gfx::Rect arrow_bounds(width() - insets.right() - 147 menu_marker_->width() - kMenuMarkerPaddingRight, 148 height() / 2 - menu_marker_->height() / 2, 149 menu_marker_->width(), 150 menu_marker_->height()); 151 arrow_bounds.set_x(GetMirroredXForRect(arrow_bounds)); 152 canvas->DrawImageInt(*menu_marker_, arrow_bounds.x(), arrow_bounds.y()); 153 } 154 } 155 156 //////////////////////////////////////////////////////////////////////////////// 157 // 158 // MenuButton - Events 159 // 160 //////////////////////////////////////////////////////////////////////////////// 161 162 gfx::Size MenuButton::GetPreferredSize() { 163 gfx::Size prefsize = TextButton::GetPreferredSize(); 164 if (show_menu_marker_) { 165 prefsize.Enlarge(menu_marker_->width() + kMenuMarkerPaddingLeft + 166 kMenuMarkerPaddingRight, 167 0); 168 } 169 return prefsize; 170 } 171 172 const char* MenuButton::GetClassName() const { 173 return kViewClassName; 174 } 175 176 bool MenuButton::OnMousePressed(const ui::MouseEvent& event) { 177 RequestFocus(); 178 if (state() != STATE_DISABLED) { 179 // If we're draggable (GetDragOperations returns a non-zero value), then 180 // don't pop on press, instead wait for release. 181 if (event.IsOnlyLeftMouseButton() && 182 HitTestPoint(event.location()) && 183 GetDragOperations(event.location()) == ui::DragDropTypes::DRAG_NONE) { 184 TimeDelta delta = Time::Now() - menu_closed_time_; 185 int64 delta_in_milliseconds = delta.InMilliseconds(); 186 if (delta_in_milliseconds > kMinimumTimeBetweenButtonClicks) { 187 return Activate(); 188 } 189 } 190 } 191 return true; 192 } 193 194 void MenuButton::OnMouseReleased(const ui::MouseEvent& event) { 195 // Explicitly test for left mouse button to show the menu. If we tested for 196 // !IsTriggerableEvent it could lead to a situation where we end up showing 197 // the menu and context menu (this would happen if the right button is not 198 // triggerable and there's a context menu). 199 if (GetDragOperations(event.location()) != ui::DragDropTypes::DRAG_NONE && 200 state() != STATE_DISABLED && !InDrag() && event.IsOnlyLeftMouseButton() && 201 HitTestPoint(event.location())) { 202 Activate(); 203 } else { 204 TextButton::OnMouseReleased(event); 205 } 206 } 207 208 // The reason we override View::OnMouseExited is because we get this event when 209 // we display the menu. If we don't override this method then 210 // BaseButton::OnMouseExited will get the event and will set the button's state 211 // to STATE_NORMAL instead of keeping the state BM_PUSHED. This, in turn, will 212 // cause the button to appear depressed while the menu is displayed. 213 void MenuButton::OnMouseExited(const ui::MouseEvent& event) { 214 if ((state_ != STATE_DISABLED) && (!menu_visible_) && (!InDrag())) { 215 SetState(STATE_NORMAL); 216 } 217 } 218 219 void MenuButton::OnGestureEvent(ui::GestureEvent* event) { 220 if (state() != STATE_DISABLED && event->type() == ui::ET_GESTURE_TAP) { 221 if (Activate()) 222 event->StopPropagation(); 223 return; 224 } 225 TextButton::OnGestureEvent(event); 226 } 227 228 bool MenuButton::OnKeyPressed(const ui::KeyEvent& event) { 229 switch (event.key_code()) { 230 case ui::VKEY_SPACE: 231 // Alt-space on windows should show the window menu. 232 if (event.IsAltDown()) 233 break; 234 case ui::VKEY_RETURN: 235 case ui::VKEY_UP: 236 case ui::VKEY_DOWN: { 237 // WARNING: we may have been deleted by the time Activate returns. 238 bool ret = Activate(); 239 #if defined(USE_AURA) 240 // This is to prevent the keyboard event from being dispatched twice. 241 // The Activate function returns false in most cases. In AURA if the 242 // keyboard event is not handled, we pass it to the default handler 243 // which dispatches the event back to us causing the menu to get 244 // displayed again. 245 ret = true; 246 #endif 247 return ret; 248 } 249 default: 250 break; 251 } 252 return false; 253 } 254 255 bool MenuButton::OnKeyReleased(const ui::KeyEvent& event) { 256 // Override CustomButton's implementation, which presses the button when 257 // you press space and clicks it when you release space. For a MenuButton 258 // we always activate the menu on key press. 259 return false; 260 } 261 262 void MenuButton::GetAccessibleState(ui::AccessibleViewState* state) { 263 CustomButton::GetAccessibleState(state); 264 state->role = ui::AccessibilityTypes::ROLE_BUTTONMENU; 265 state->default_action = l10n_util::GetStringUTF16(IDS_APP_ACCACTION_PRESS); 266 state->state = ui::AccessibilityTypes::STATE_HASPOPUP; 267 } 268 269 int MenuButton::GetMaximumScreenXCoordinate() { 270 if (!GetWidget()) { 271 NOTREACHED(); 272 return 0; 273 } 274 275 gfx::Rect monitor_bounds = GetWidget()->GetWorkAreaBoundsInScreen(); 276 return monitor_bounds.right() - 1; 277 } 278 279 } // namespace views 280