OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef VIEWS_CONTROLS_MENU_MENU_RUNNER_H_ | 5 #ifndef VIEWS_CONTROLS_MENU_MENU_RUNNER_H_ |
6 #define VIEWS_CONTROLS_MENU_MENU_RUNNER_H_ | 6 #define VIEWS_CONTROLS_MENU_MENU_RUNNER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" |
10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
11 #include "views/controls/menu/menu_item_view.h" | 12 #include "views/controls/menu/menu_item_view.h" |
12 | 13 |
13 namespace views { | 14 namespace views { |
14 | 15 |
15 class MenuButton; | 16 class MenuButton; |
16 class Widget; | 17 class Widget; |
17 | 18 |
18 // MenuRunner handles the lifetime of the root MenuItemView. MenuItemView runs a | 19 namespace internal { |
19 // nested message loop, which means care must be taken when the MenuItemView | 20 class MenuRunnerImpl; |
20 // needs to be deleted. MenuRunner makes sure the menu is deleted after the | 21 } |
21 // nested message loop completes. | 22 |
| 23 // MenuRunner is responsible for showing (running) the menu and additionally |
| 24 // owning the MenuItemView. RunMenuAt() runs a nested message loop. It is safe |
| 25 // to delete MenuRunner at any point, but MenuRunner internally only deletes the |
| 26 // MenuItemView *after* the nested message loop completes. If MenuRunner is |
| 27 // deleted while the menu is showing the delegate of the menu is reset. This is |
| 28 // done to ensure delegates aren't notified after they may have been deleted. |
22 // | 29 // |
23 // MenuRunner can be deleted at any time and will correctly handle deleting the | 30 // NOTE: while you can delete a MenuRunner at any point, the nested message loop |
24 // underlying menu. | 31 // won't return immediately. This means if you delete the object that owns |
25 // | 32 // the MenuRunner while the menu is running, your object is effectively still |
26 // TODO: this is a work around for 57890. If we fix it this class shouldn't be | 33 // on the stack. A return value of MENU_DELETED indicated this. In most cases |
27 // needed. | 34 // if RunMenuAt() returns MENU_DELETED, you should return immediately. |
28 class VIEWS_EXPORT MenuRunner { | 35 class VIEWS_EXPORT MenuRunner { |
29 public: | 36 public: |
| 37 enum RunTypes { |
| 38 // The menu has mnemonics. |
| 39 HAS_MNEMONICS = 1 << 0, |
| 40 |
| 41 // The menu is a nested context menu. For example, click a folder on the |
| 42 // bookmark bar, then right click an entry to get its context menu. |
| 43 IS_NESTED = 1 << 1, |
| 44 |
| 45 // Used for showing a menu during a drop operation. This does NOT block the |
| 46 // caller, instead the delegate is notified when the menu closes via the |
| 47 // DropMenuClosed method. |
| 48 FOR_DROP = 1 << 2, |
| 49 }; |
| 50 |
| 51 enum RunResult { |
| 52 // Indicates RunMenuAt is returning because the MenuRunner was deleted. |
| 53 MENU_DELETED, |
| 54 |
| 55 // Indicates RunMenuAt returned and MenuRunner was not deleted. |
| 56 NORMAL_EXIT |
| 57 }; |
| 58 |
| 59 // Creates a new MenuRunner. MenuRunner owns the supplied menu. |
30 explicit MenuRunner(MenuItemView* menu); | 60 explicit MenuRunner(MenuItemView* menu); |
31 ~MenuRunner(); | 61 ~MenuRunner(); |
32 | 62 |
33 // Runs the menu. | 63 // Returns the menu. |
34 void RunMenuAt(Widget* parent, | 64 MenuItemView* GetMenu(); |
35 MenuButton* button, | 65 |
36 const gfx::Rect& bounds, | 66 // Takes ownership of |menu|, deleting it when MenuRunner is deleted. You |
37 MenuItemView::AnchorPosition anchor, | 67 // only need call this if you create additional menus from |
38 bool has_mnemonics); | 68 // MenuDelegate::GetSiblingMenu. |
| 69 void OwnMenu(MenuItemView* menu); |
| 70 |
| 71 // Runs the menu. |types| is a bitmask of RunTypes. If this returns |
| 72 // MENU_DELETED the method is returning because the MenuRunner was deleted. |
| 73 // Typically callers should NOT do any processing if this returns |
| 74 // MENU_DELETED. |
| 75 RunResult RunMenuAt(Widget* parent, |
| 76 MenuButton* button, |
| 77 const gfx::Rect& bounds, |
| 78 MenuItemView::AnchorPosition anchor, |
| 79 int32 types) WARN_UNUSED_RESULT; |
| 80 |
| 81 // Hides and cancels the menu. This does nothing if the menu is not open. |
| 82 void Cancel(); |
39 | 83 |
40 private: | 84 private: |
41 class Holder; | 85 internal::MenuRunnerImpl* holder_; |
42 | |
43 Holder* holder_; | |
44 | 86 |
45 DISALLOW_COPY_AND_ASSIGN(MenuRunner); | 87 DISALLOW_COPY_AND_ASSIGN(MenuRunner); |
46 }; | 88 }; |
47 | 89 |
48 } // namespace views | 90 } // namespace views |
49 | 91 |
50 #endif // VIEWS_CONTROLS_MENU_MENU_RUNNER_H_ | 92 #endif // VIEWS_CONTROLS_MENU_MENU_RUNNER_H_ |
OLD | NEW |