1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/NSMenu.mm
3 // Purpose: wxCocoaNSMenu implementation
4 // Author: David Elliott
7 // Copyright: (c) 2002 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
17 #include "wx/cocoa/ObjcRef.h"
18 #include "wx/cocoa/NSMenu.h"
20 #import <Foundation/NSNotification.h>
21 #include "wx/cocoa/objc/NSMenu.h"
23 // ============================================================================
25 // ============================================================================
27 @implementation WXNSMenu : NSMenu
31 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa(self);
33 menu->Cocoa_dealloc();
38 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSMenu,NSMenu)
40 // ============================================================================
41 // @class wxNSMenuNotificationObserver
42 // ============================================================================
43 @interface wxNSMenuNotificationObserver : NSObject
47 - (void)menuDidAddItem: (NSNotification *)notification;
48 - (void)menuDidChangeItem: (NSNotification *)notification;
49 - (void)menuDidRemoveItem: (NSNotification *)notification;
50 - (void)menuDidSendAction: (NSNotification *)notification;
51 - (void)menuWillSendAction: (NSNotification *)notification;
52 @end // interface wxNSMenuNotificationObserver
53 WX_DECLARE_GET_OBJC_CLASS(wxNSMenuNotificationObserver,NSObject)
55 @implementation wxNSMenuNotificationObserver : NSObject
57 - (void)menuDidAddItem: (NSNotification *)notification
59 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
60 wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists"));
61 menu->CocoaNotification_menuDidAddItem(notification);
64 - (void)menuDidChangeItem: (NSNotification *)notification
66 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
67 wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists"));
68 menu->CocoaNotification_menuDidChangeItem(notification);
71 - (void)menuDidRemoveItem: (NSNotification *)notification
73 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
74 wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists"));
75 menu->CocoaNotification_menuDidRemoveItem(notification);
78 - (void)menuDidSendAction: (NSNotification *)notification
80 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
81 wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists"));
82 menu->CocoaNotification_menuDidSendAction(notification);
85 - (void)menuWillSendAction: (NSNotification *)notification
87 wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]);
88 wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists"));
89 menu->CocoaNotification_menuWillSendAction(notification);
92 @end // implementation wxNSMenuNotificationObserver
93 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSMenuNotificationObserver,NSObject)
95 // ========================================================================
97 // ========================================================================
98 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu)
100 // New CF-retained observer (this should have been using wxObjcAutoRefFromAlloc to begin with)
101 static wxObjcAutoRefFromAlloc<wxNSMenuNotificationObserver*> s_cocoaNSMenuObserver([[WX_GET_OBJC_CLASS(wxNSMenuNotificationObserver) alloc] init]);
102 // For compatibility with old code
103 struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = s_cocoaNSMenuObserver;
105 void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags)
109 sm_cocoaHash.insert(wxCocoaNSMenuHash::value_type(cocoaNSMenu,this));
110 if(flags&OBSERVE_DidAddItem)
111 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidAddItem:) name:NSMenuDidAddItemNotification object:cocoaNSMenu];
112 if(flags&OBSERVE_DidChangeItem)
113 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidChangeItem:) name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
114 if(flags&OBSERVE_DidRemoveItem)
115 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidRemoveItem:) name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
116 if(flags&OBSERVE_DidSendAction)
117 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidSendAction:) name:NSMenuDidSendActionNotification object:cocoaNSMenu];
118 if(flags&OBSERVE_WillSendAction)
119 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuWillSendAction:) name:NSMenuWillSendActionNotification object:cocoaNSMenu];
123 void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu)
127 sm_cocoaHash.erase(cocoaNSMenu);
128 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidAddItemNotification object:cocoaNSMenu];
129 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidChangeItemNotification object:cocoaNSMenu];
130 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidRemoveItemNotification object:cocoaNSMenu];
131 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidSendActionNotification object:cocoaNSMenu];
132 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuWillSendActionNotification object:cocoaNSMenu];
136 #endif // wxUSE_MENUS