1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/NSMenu.mm
3 // Purpose: wxCocoaNSMenu implementation
4 // Author: David Elliott
8 // Copyright: (c) 2002 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/cocoa/ObjcRef.h"
19 #include "wx/cocoa/NSMenu.h"
21 #import <Foundation/NSNotification.h>
22 #include "wx/cocoa/objc/NSMenu.h"
24 // ============================================================================
26 // ============================================================================
28 @implementation WXNSMenu : NSMenu
32 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa(self);
34 menu->Cocoa_dealloc();
39 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSMenu,NSMenu)
41 // ============================================================================
42 // @class wxNSMenuNotificationObserver
43 // ============================================================================
44 @interface wxNSMenuNotificationObserver : NSObject
48 - (void)menuDidAddItem: (NSNotification *)notification;
49 - (void)menuDidChangeItem: (NSNotification *)notification;
50 - (void)menuDidRemoveItem: (NSNotification *)notification;
51 - (void)menuDidSendAction: (NSNotification *)notification;
52 - (void)menuWillSendAction: (NSNotification *)notification;
53 @end // interface wxNSMenuNotificationObserver
54 WX_DECLARE_GET_OBJC_CLASS(wxNSMenuNotificationObserver,NSObject)
56 @implementation wxNSMenuNotificationObserver : NSObject
58 - (void)menuDidAddItem: (NSNotification *)notification
60 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
61 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
62 menu->CocoaNotification_menuDidAddItem(notification);
65 - (void)menuDidChangeItem: (NSNotification *)notification
67 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
68 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
69 menu->CocoaNotification_menuDidChangeItem(notification);
72 - (void)menuDidRemoveItem: (NSNotification *)notification
74 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
75 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
76 menu->CocoaNotification_menuDidRemoveItem(notification);
79 - (void)menuDidSendAction: (NSNotification *)notification
81 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
82 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
83 menu->CocoaNotification_menuDidSendAction(notification);
86 - (void)menuWillSendAction: (NSNotification *)notification
88 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
89 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
90 menu->CocoaNotification_menuWillSendAction(notification);
93 @end // implementation wxNSMenuNotificationObserver
94 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSMenuNotificationObserver,NSObject)
96 // ========================================================================
98 // ========================================================================
99 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
101 // New CF-retained observer (this should have been using wxObjcAutoRefFromAlloc to begin with)
102 static wxObjcAutoRefFromAlloc<wxNSMenuNotificationObserver*> s_cocoaNSMenuObserver([[WX_GET_OBJC_CLASS(wxNSMenuNotificationObserver) alloc] init]);
103 // For compatibility with old code
104 struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = s_cocoaNSMenuObserver;
106 void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags)
110 sm_cocoaHash.insert(wxCocoaNSMenuHash::value_type(cocoaNSMenu,this));
111 if(flags&OBSERVE_DidAddItem)
112 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidAddItem:) name:NSMenuDidAddItemNotification object:cocoaNSMenu];
113 if(flags&OBSERVE_DidChangeItem)
114 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidChangeItem:) name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
115 if(flags&OBSERVE_DidRemoveItem)
116 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidRemoveItem:) name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
117 if(flags&OBSERVE_DidSendAction)
118 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidSendAction:) name:NSMenuDidSendActionNotification object:cocoaNSMenu];
119 if(flags&OBSERVE_WillSendAction)
120 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuWillSendAction:) name:NSMenuWillSendActionNotification object:cocoaNSMenu];
124 void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu)
128 sm_cocoaHash.erase(cocoaNSMenu);
129 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidAddItemNotification object:cocoaNSMenu];
130 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
131 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
132 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidSendActionNotification object:cocoaNSMenu];
133 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuWillSendActionNotification object:cocoaNSMenu];
137 #endif // wxUSE_MENUS