1 // Copyright 2013 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_BASE_COCOA_MENU_CONTROLLER_H_
6 #define UI_BASE_COCOA_MENU_CONTROLLER_H_
7 
8 #import <Cocoa/Cocoa.h>
9 
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/strings/string16.h"
12 #include "ui/base/ui_base_export.h"
13 
14 namespace ui {
15 class MenuModel;
16 }
17 
18 // A controller for the cross-platform menu model. The menu that's created
19 // has the tag and represented object set for each menu item. The object is a
20 // NSValue holding a pointer to the model for that level of the menu (to
21 // allow for hierarchical menus). The tag is the index into that model for
22 // that particular item. It is important that the model outlives this object
23 // as it only maintains weak references.
24 UI_BASE_EXPORT
25 @interface MenuControllerCocoa
26     : NSObject<NSMenuDelegate, NSUserInterfaceValidations>
27 
28 // Note that changing this will have no effect if you use
29 // |-initWithModel:useWithPopUpButtonCell:| or after the first call to |-menu|.
30 @property(nonatomic, assign) BOOL useWithPopUpButtonCell;
31 
32 // NIB-based initializer. This does not create a menu. Clients can set the
33 // properties of the object and the menu will be created upon the first call to
34 // |-menu|. Note that the menu will be immutable after creation.
35 - (instancetype)init;
36 
37 // Builds a NSMenu from the pre-built model (must not be nil). Changes made
38 // to the contents of the model after calling this will not be noticed. If
39 // the menu will be displayed by a NSPopUpButtonCell, it needs to be of a
40 // slightly different form (0th item is empty). Note this attribute of the menu
41 // cannot be changed after it has been created.
42 - (instancetype)initWithModel:(ui::MenuModel*)model
43        useWithPopUpButtonCell:(BOOL)useWithCell;
44 
45 // Programmatically close the constructed menu.
46 - (void)cancel;
47 
48 - (ui::MenuModel*)model;
49 - (void)setModel:(ui::MenuModel*)model;
50 
51 // Access to the constructed menu if the complex initializer was used. If the
52 // default initializer was used, then this will create the menu on first call.
53 - (NSMenu*)menu;
54 
55 // Whether the menu is currently open.
56 - (BOOL)isMenuOpen;
57 
58 @end
59 
60 #endif  // UI_BASE_COCOA_MENU_CONTROLLER_H_
61