1 // Copyright (c) 2011 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 #ifndef UI_VIEWS_CONTROLS_MENU_MENU_2_H_ 6 #define UI_VIEWS_CONTROLS_MENU_MENU_2_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "ui/gfx/native_widget_types.h" 11 #include "ui/views/controls/menu/menu_wrapper.h" 12 #include "ui/views/views_export.h" 13 14 namespace gfx { 15 class Point; 16 } 17 18 namespace ui { 19 class MenuModel; 20 } 21 22 namespace views { 23 24 // A menu. Populated from a model, and relies on a delegate to execute commands. 25 // 26 // WARNING: do NOT create and use Menu2 on the stack. Menu2 notifies the model 27 // of selection AFTER a delay. This means that if use a Menu2 on the stack 28 // ActivatedAt is never invoked. 29 class VIEWS_EXPORT Menu2 { 30 public: 31 // How the menu is aligned relative to the point it is shown at. 32 // The alignment is reversed by menu if text direction is right to left. 33 enum Alignment { 34 ALIGN_TOPLEFT, 35 ALIGN_TOPRIGHT 36 }; 37 38 // Creates a new menu populated with the contents of |model|. 39 // WARNING: this populates the menu on construction by invoking methods on 40 // the model. As such, it is typically not safe to use this as the model 41 // from the constructor. EG: 42 // MyClass : menu_(this) {} 43 // is likely to have problems. 44 explicit Menu2(ui::MenuModel* model); 45 virtual ~Menu2(); 46 47 // Runs the menu at the specified point. This method blocks until done. 48 // RunContextMenuAt is the same, but the alignment is the default for a 49 // context menu. 50 void RunMenuAt(const gfx::Point& point, Alignment alignment); 51 void RunContextMenuAt(const gfx::Point& point); 52 53 // Cancels the active menu. 54 void CancelMenu(); 55 56 // Called when the model supplying data to this menu has changed, and the menu 57 // must be rebuilt. 58 void Rebuild(); 59 60 // Called when the states of the menu items in the menu should be refreshed 61 // from the model. 62 void UpdateStates(); 63 64 // For submenus. 65 HMENU GetNativeMenu() const; 66 67 // Get the result of the last call to RunMenuAt to determine whether an 68 // item was selected, the user navigated to a next or previous menu, or 69 // nothing. 70 MenuWrapper::MenuAction GetMenuAction() const; 71 72 // Add a listener to receive a callback when the menu opens. 73 void AddMenuListener(MenuListener* listener); 74 75 // Remove a menu listener. 76 void RemoveMenuListener(MenuListener* listener); 77 78 // Accessors. 79 ui::MenuModel* model() const { return model_; } 80 81 // Sets the minimum width of the menu. 82 void SetMinimumWidth(int width); 83 84 private: 85 86 ui::MenuModel* model_; 87 88 // The object that actually implements the menu. 89 scoped_ptr<MenuWrapper> wrapper_; 90 91 DISALLOW_COPY_AND_ASSIGN(Menu2); 92 }; 93 94 } // namespace views 95 96 #endif // UI_VIEWS_CONTROLS_MENU_MENU_2_H_ 97