X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/605c7e7ed81c740e80de66c751f50fc54bd77e4d..339e9747d26ed4cef83de1e50b5313c2aed8d443:/src/cocoa/NSMenu.mm diff --git a/src/cocoa/NSMenu.mm b/src/cocoa/NSMenu.mm index bbf68e96d5..c5a55f9c3a 100644 --- a/src/cocoa/NSMenu.mm +++ b/src/cocoa/NSMenu.mm @@ -15,23 +15,123 @@ #include "wx/log.h" #endif // WX_PRECOMP +#include "wx/cocoa/ObjcRef.h" #include "wx/cocoa/NSMenu.h" -//#include "wx/cocoa/ObjcPose.h" -#if 0 // There is no reason to pose for NSMenu at this time. +#import +#include "wx/cocoa/objc/NSMenu.h" + +// ============================================================================ +// @class WXNSMenu +// ============================================================================ + +@implementation WXNSMenu : NSMenu + +- (void)dealloc +{ + wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa(self); + if(menu) + menu->Cocoa_dealloc(); + [super dealloc]; +} + +@end // WXNSMenu +WX_IMPLEMENT_GET_OBJC_CLASS(WXNSMenu,NSMenu) + // ============================================================================ -// @class wxPoserNSMenu +// @class wxNSMenuNotificationObserver // ============================================================================ -@interface wxPoserNSMenu : NSMenu +@interface wxNSMenuNotificationObserver : NSObject +{ +} + +- (void)menuDidAddItem: (NSNotification *)notification; +- (void)menuDidChangeItem: (NSNotification *)notification; +- (void)menuDidRemoveItem: (NSNotification *)notification; +- (void)menuDidSendAction: (NSNotification *)notification; +- (void)menuWillSendAction: (NSNotification *)notification; +@end // interface wxNSMenuNotificationObserver +WX_DECLARE_GET_OBJC_CLASS(wxNSMenuNotificationObserver,NSObject) + +@implementation wxNSMenuNotificationObserver : NSObject + +- (void)menuDidAddItem: (NSNotification *)notification +{ + wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]); + wxCHECK_RET(menu,wxT("menuDidAddItem received but no wxMenu exists")); + menu->CocoaNotification_menuDidAddItem(notification); +} + +- (void)menuDidChangeItem: (NSNotification *)notification +{ + wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]); + wxCHECK_RET(menu,wxT("menuDidChangeItem received but no wxMenu exists")); + menu->CocoaNotification_menuDidChangeItem(notification); +} + +- (void)menuDidRemoveItem: (NSNotification *)notification +{ + wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]); + wxCHECK_RET(menu,wxT("menuDidRemoveItem received but no wxMenu exists")); + menu->CocoaNotification_menuDidRemoveItem(notification); +} + +- (void)menuDidSendAction: (NSNotification *)notification +{ + wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]); + wxCHECK_RET(menu,wxT("menuDidSendAction received but no wxMenu exists")); + menu->CocoaNotification_menuDidSendAction(notification); +} + +- (void)menuWillSendAction: (NSNotification *)notification { + wxCocoaNSMenu *menu = wxCocoaNSMenu::GetFromCocoa([notification object]); + wxCHECK_RET(menu,wxT("menuWillSendAction received but no wxMenu exists")); + menu->CocoaNotification_menuWillSendAction(notification); } -@end // wxPoserNSMenu +@end // implementation wxNSMenuNotificationObserver +WX_IMPLEMENT_GET_OBJC_CLASS(wxNSMenuNotificationObserver,NSObject) + +// ======================================================================== +// wxCocoaNSMenu +// ======================================================================== +WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSMenu) + +// New CF-retained observer (this should have been using wxObjcAutoRefFromAlloc to begin with) +static wxObjcAutoRefFromAlloc s_cocoaNSMenuObserver([[WX_GET_OBJC_CLASS(wxNSMenuNotificationObserver) alloc] init]); +// For compatibility with old code +struct objc_object *wxCocoaNSMenu::sm_cocoaObserver = s_cocoaNSMenuObserver; -WX_IMPLEMENT_POSER(wxPoserNSMenu); -@implementation wxPoserNSMenu : NSMenu +void wxCocoaNSMenu::AssociateNSMenu(WX_NSMenu cocoaNSMenu, unsigned int flags) +{ + if(cocoaNSMenu) + { + sm_cocoaHash.insert(wxCocoaNSMenuHash::value_type(cocoaNSMenu,this)); + if(flags&OBSERVE_DidAddItem) + [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidAddItem:) name:NSMenuDidAddItemNotification object:cocoaNSMenu]; + if(flags&OBSERVE_DidChangeItem) + [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidChangeItem:) name:NSMenuDidChangeItemNotification object:cocoaNSMenu]; + if(flags&OBSERVE_DidRemoveItem) + [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidRemoveItem:) name:NSMenuDidRemoveItemNotification object:cocoaNSMenu]; + if(flags&OBSERVE_DidSendAction) + [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuDidSendAction:) name:NSMenuDidSendActionNotification object:cocoaNSMenu]; + if(flags&OBSERVE_WillSendAction) + [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(menuWillSendAction:) name:NSMenuWillSendActionNotification object:cocoaNSMenu]; + } +} -@end // wxPoserNSMenu -#endif // 0 +void wxCocoaNSMenu::DisassociateNSMenu(WX_NSMenu cocoaNSMenu) +{ + if(cocoaNSMenu) + { + sm_cocoaHash.erase(cocoaNSMenu); + [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidAddItemNotification object:cocoaNSMenu]; + [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidChangeItemNotification object:cocoaNSMenu]; + [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidRemoveItemNotification object:cocoaNSMenu]; + [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuDidSendActionNotification object:cocoaNSMenu]; + [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:NSMenuWillSendActionNotification object:cocoaNSMenu]; + } +} #endif // wxUSE_MENUS