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 CHROME_BROWSER_UI_COCOA_MENU_CONTROLLER_H_ 6 #define CHROME_BROWSER_UI_COCOA_MENU_CONTROLLER_H_ 7 #pragma once 8 9 #import <Cocoa/Cocoa.h> 10 11 #import "base/mac/cocoa_protocols.h" 12 #include "base/memory/scoped_nsobject.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 @interface MenuController : NSObject<NSMenuDelegate> { 25 @protected 26 ui::MenuModel* model_; // weak 27 scoped_nsobject<NSMenu> menu_; 28 BOOL useWithPopUpButtonCell_; // If YES, 0th item is blank 29 } 30 31 @property(nonatomic, assign) ui::MenuModel* model; 32 // Note that changing this will have no effect if you use 33 // |-initWithModel:useWithPopUpButtonCell:| or after the first call to |-menu|. 34 @property(nonatomic) BOOL useWithPopUpButtonCell; 35 36 // NIB-based initializer. This does not create a menu. Clients can set the 37 // properties of the object and the menu will be created upon the first call to 38 // |-menu|. Note that the menu will be immutable after creation. 39 - (id)init; 40 41 // Builds a NSMenu from the pre-built model (must not be nil). Changes made 42 // to the contents of the model after calling this will not be noticed. If 43 // the menu will be displayed by a NSPopUpButtonCell, it needs to be of a 44 // slightly different form (0th item is empty). Note this attribute of the menu 45 // cannot be changed after it has been created. 46 - (id)initWithModel:(ui::MenuModel*)model 47 useWithPopUpButtonCell:(BOOL)useWithCell; 48 49 // Access to the constructed menu if the complex initializer was used. If the 50 // default initializer was used, then this will create the menu on first call. 51 - (NSMenu*)menu; 52 53 @end 54 55 // Exposed only for unit testing, do not call directly. 56 @interface MenuController (PrivateExposedForTesting) 57 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item; 58 @end 59 60 // Protected methods that subclassers can override. 61 @interface MenuController (Protected) 62 - (void)addItemToMenu:(NSMenu*)menu 63 atIndex:(NSInteger)index 64 fromModel:(ui::MenuModel*)model 65 modelIndex:(int)modelIndex; 66 @end 67 68 #endif // CHROME_BROWSER_UI_COCOA_MENU_CONTROLLER_H_ 69