From: David Elliott Date: Fri, 29 Aug 2003 14:10:23 +0000 (+0000) Subject: Use a delegate instead of manually adding notifications. Not only is this X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/78c67cb6425acb3b93d48a7a14f554bcd32ed72a?ds=inline Use a delegate instead of manually adding notifications. Not only is this less code, but a delegate will be required for future functionality. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@23294 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/cocoa/NSWindow.h b/include/wx/cocoa/NSWindow.h index f67430f868..927b80b70e 100644 --- a/include/wx/cocoa/NSWindow.h +++ b/include/wx/cocoa/NSWindow.h @@ -29,7 +29,7 @@ public: virtual void CocoaNotification_DidBecomeKey(void) { } virtual void CocoaNotification_DidResignKey(void) { } protected: - static void *sm_cocoaObserver; + static struct objc_object *sm_cocoaDelegate; }; #endif // _WX_COCOA_NSWINDOW_H_ diff --git a/src/cocoa/NSWindow.mm b/src/cocoa/NSWindow.mm index 27d506a30b..cd1c735faf 100644 --- a/src/cocoa/NSWindow.mm +++ b/src/cocoa/NSWindow.mm @@ -31,33 +31,33 @@ #import // ============================================================================ -// @class wxNSWindowNotificationObserver +// @class wxNSWindowDelegate // ============================================================================ -@interface wxNSWindowNotificationObserver : NSObject +@interface wxNSWindowDelegate : NSObject { } -- (void)notificationDidBecomeKey: (NSNotification *)notification; -- (void)notificationDidResignKey: (NSNotification *)notification; -@end //interface wxNSWindowNotificationObserver +- (void)windowDidBecomeKey: (NSNotification *)notification; +- (void)windowDidResignKey: (NSNotification *)notification; +@end //interface wxNSWindowDelegate -@implementation wxNSWindowNotificationObserver : NSObject +@implementation wxNSWindowDelegate : NSObject -- (void)notificationDidBecomeKey: (NSNotification *)notification +- (void)windowDidBecomeKey: (NSNotification *)notification { wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]); wxCHECK_RET(win,"notificationDidBecomeKey received but no wxWindow exists"); win->CocoaNotification_DidBecomeKey(); } -- (void)notificationDidResignKey: (NSNotification *)notification +- (void)windowDidResignKey: (NSNotification *)notification { wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]); wxCHECK_RET(win,"notificationDidResignKey received but no wxWindow exists"); win->CocoaNotification_DidResignKey(); } -@end //implementation wxNSWindowNotificationObserver +@end //implementation wxNSWindowDelegate // ============================================================================ // class wxCocoaNSWindow @@ -65,7 +65,7 @@ WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow) -void *wxCocoaNSWindow::sm_cocoaObserver = [[wxNSWindowNotificationObserver alloc] init]; +struct objc_object *wxCocoaNSWindow::sm_cocoaDelegate = [[wxNSWindowDelegate alloc] init]; void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow) { @@ -73,8 +73,7 @@ void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow) { [cocoaNSWindow setReleasedWhenClosed: NO]; sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this)); - [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(notificationDidBecomeKey:) name:@"NSWindowDidBecomeKeyNotification" object:cocoaNSWindow]; - [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(notificationDidResignKey:) name:@"NSWindowDidResignKeyNotification" object:cocoaNSWindow]; + [cocoaNSWindow setDelegate: sm_cocoaDelegate]; } } @@ -82,8 +81,7 @@ void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow) { if(cocoaNSWindow) { - [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:@"NSWindowDidBecomeKeyNotification" object:cocoaNSWindow]; - [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:@"NSWindowDidResignKeyNotification" object:cocoaNSWindow]; + [cocoaNSWindow setDelegate: nil]; sm_cocoaHash.erase(cocoaNSWindow); } }