• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 MenuController : NSObject<NSMenuDelegate> {
26  @protected
27   ui::MenuModel* model_;  // weak
28   base::scoped_nsobject<NSMenu> menu_;
29   BOOL useWithPopUpButtonCell_;  // If YES, 0th item is blank
30   BOOL isMenuOpen_;
31 }
32 
33 @property(nonatomic, assign) ui::MenuModel* model;
34 // Note that changing this will have no effect if you use
35 // |-initWithModel:useWithPopUpButtonCell:| or after the first call to |-menu|.
36 @property(nonatomic) BOOL useWithPopUpButtonCell;
37 
38 + (base::string16)elideMenuTitle:(const base::string16&)title
39                          toWidth:(int)width;
40 
41 // NIB-based initializer. This does not create a menu. Clients can set the
42 // properties of the object and the menu will be created upon the first call to
43 // |-menu|. Note that the menu will be immutable after creation.
44 - (id)init;
45 
46 // Builds a NSMenu from the pre-built model (must not be nil). Changes made
47 // to the contents of the model after calling this will not be noticed. If
48 // the menu will be displayed by a NSPopUpButtonCell, it needs to be of a
49 // slightly different form (0th item is empty). Note this attribute of the menu
50 // cannot be changed after it has been created.
51 - (id)initWithModel:(ui::MenuModel*)model
52     useWithPopUpButtonCell:(BOOL)useWithCell;
53 
54 // Programmatically close the constructed menu.
55 - (void)cancel;
56 
57 // Access to the constructed menu if the complex initializer was used. If the
58 // default initializer was used, then this will create the menu on first call.
59 - (NSMenu*)menu;
60 
61 // Whether the menu is currently open.
62 - (BOOL)isMenuOpen;
63 
64 // NSMenuDelegate methods this class implements. Subclasses should call super
65 // if extending the behavior.
66 - (void)menuWillOpen:(NSMenu*)menu;
67 - (void)menuDidClose:(NSMenu*)menu;
68 
69 @end
70 
71 // Exposed only for unit testing, do not call directly.
72 @interface MenuController (PrivateExposedForTesting)
73 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item;
74 @end
75 
76 // Protected methods that subclassers can override.
77 @interface MenuController (Protected)
78 - (void)addItemToMenu:(NSMenu*)menu
79               atIndex:(NSInteger)index
80             fromModel:(ui::MenuModel*)model;
81 - (NSMenu*)menuFromModel:(ui::MenuModel*)model;
82 // Returns the maximum width for the menu item. Returns -1 to indicate
83 // that there's no maximum width.
84 - (int)maxWidthForMenuModel:(ui::MenuModel*)model
85                  modelIndex:(int)modelIndex;
86 @end
87 
88 #endif  // UI_BASE_COCOA_MENU_CONTROLLER_H_
89