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