1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/NSMenu.mm
3 // Purpose: wxCocoaNSMenu implementation
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/cocoa/NSMenu.h"
19 #include "wx/cocoa/ObjcPose.h"
21 #import <Foundation/NSNotification.h>
22 #import <AppKit/NSMenu.h>
24 // ============================================================================
25 // @class wxPoserNSMenu
26 // ============================================================================
27 @interface wxPoserNSMenu : NSMenu
35 WX_IMPLEMENT_POSER(wxPoserNSMenu);
36 @implementation wxPoserNSMenu : NSMenu
40 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa(self);
42 menu->Cocoa_dealloc();
48 // ============================================================================
49 // @class wxNSMenuNotificationObserver
50 // ============================================================================
51 @interface wxNSMenuNotificationObserver : NSObject
55 struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = [[wxNSMenuNotificationObserver alloc] init];
57 - (void)menuDidAddItem: (NSNotification *)notification;
58 - (void)menuDidChangeItem: (NSNotification *)notification;
59 - (void)menuDidRemoveItem: (NSNotification *)notification;
60 - (void)menuDidSendAction: (NSNotification *)notification;
61 - (void)menuWillSendAction: (NSNotification *)notification;
62 @end // interface wxNSMenuNotificationObserver
64 @implementation wxNSMenuNotificationObserver : NSObject
66 - (void)menuDidAddItem: (NSNotification *)notification
68 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
69 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
70 menu->CocoaNotification_menuDidAddItem(notification);
73 - (void)menuDidChangeItem: (NSNotification *)notification
75 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
76 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
77 menu->CocoaNotification_menuDidChangeItem(notification);
80 - (void)menuDidRemoveItem: (NSNotification *)notification
82 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
83 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
84 menu->CocoaNotification_menuDidRemoveItem(notification);
87 - (void)menuDidSendAction: (NSNotification *)notification
89 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
90 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
91 menu->CocoaNotification_menuDidSendAction(notification);
94 - (void)menuWillSendAction: (NSNotification *)notification
96 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
97 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
98 menu->CocoaNotification_menuWillSendAction(notification);
101 @end // implementation wxNSMenuNotificationObserver
103 // ========================================================================
105 // ========================================================================
106 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
108 void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags)
112 sm_cocoaHash.insert(wxCocoaNSMenuHash::value_type(cocoaNSMenu,this));
113 if(flags&OBSERVE_DidAddItem)
114 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidAddItem:) name:NSMenuDidAddItemNotification object:cocoaNSMenu];
115 if(flags&OBSERVE_DidChangeItem)
116 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidChangeItem:) name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
117 if(flags&OBSERVE_DidRemoveItem)
118 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidRemoveItem:) name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
119 if(flags&OBSERVE_DidSendAction)
120 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidSendAction:) name:NSMenuDidSendActionNotification object:cocoaNSMenu];
121 if(flags&OBSERVE_WillSendAction)
122 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuWillSendAction:) name:NSMenuWillSendActionNotification object:cocoaNSMenu];
126 void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu)
130 sm_cocoaHash.erase(cocoaNSMenu);
131 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidAddItemNotification object:cocoaNSMenu];
132 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
133 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
134 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidSendActionNotification object:cocoaNSMenu];
135 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuWillSendActionNotification object:cocoaNSMenu];
139 #endif // wxUSE_MENUS