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 CEF_LIBCEF_BROWSER_NATIVE_MENU_2_H_ 6 #define CEF_LIBCEF_BROWSER_NATIVE_MENU_2_H_ 7 8 #include <memory> 9 10 #include "libcef/browser/native/menu_wrapper.h" 11 12 #include "ui/gfx/native_widget_types.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 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 { ALIGN_TOPLEFT, ALIGN_TOPRIGHT }; 34 35 // Creates a new menu populated with the contents of |model|. 36 // WARNING: this populates the menu on construction by invoking methods on 37 // the model. As such, it is typically not safe to use this as the model 38 // from the constructor. EG: 39 // MyClass : menu_(this) {} 40 // is likely to have problems. 41 explicit Menu2(ui::MenuModel* model); 42 43 Menu2(const Menu2&) = delete; 44 Menu2& operator=(const Menu2&) = delete; 45 46 virtual ~Menu2(); 47 48 // Runs the menu at the specified point. This method blocks until done. 49 // RunContextMenuAt is the same, but the alignment is the default for a 50 // context menu. 51 void RunMenuAt(const gfx::Point& point, Alignment alignment); 52 void RunContextMenuAt(const gfx::Point& point); 53 54 // Cancels the active menu. 55 void CancelMenu(); 56 57 // Called when the model supplying data to this menu has changed, and the menu 58 // must be rebuilt. 59 void Rebuild(); 60 61 // Called when the states of the menu items in the menu should be refreshed 62 // from the model. 63 void UpdateStates(); 64 65 // For submenus. 66 HMENU GetNativeMenu() const; 67 68 // Get the result of the last call to RunMenuAt to determine whether an 69 // item was selected, the user navigated to a next or previous menu, or 70 // nothing. 71 MenuWrapper::MenuAction GetMenuAction() const; 72 73 // Accessors. model()74 ui::MenuModel* model() const { return model_; } 75 76 // Sets the minimum width of the menu. 77 void SetMinimumWidth(int width); 78 79 private: 80 ui::MenuModel* model_; 81 82 // The object that actually implements the menu. 83 std::unique_ptr<MenuWrapper> wrapper_; 84 }; 85 86 } // namespace views 87 88 #endif // CEF_LIBCEF_BROWSER_NATIVE_MENU_2_H_ 89