1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/NSView.mm
3 // Purpose: wxCocoaNSView
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
22 #include "wx/window.h"
25 #include "wx/cocoa/NSView.h"
27 #import <Appkit/NSView.h>
28 #import <Foundation/NSNotification.h>
29 #import <Foundation/NSString.h>
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
34 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSView)
36 void wxCocoaNSView::AssociateNSView(WX_NSView cocoaNSView)
38 sm_cocoaHash.insert(wxCocoaNSViewHash::value_type(cocoaNSView,this));
39 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(notificationFrameChanged:) name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
40 [cocoaNSView setPostsFrameChangedNotifications: YES];
43 void wxCocoaNSView::DisassociateNSView(WX_NSView cocoaNSView)
45 sm_cocoaHash.erase(cocoaNSView);
46 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
49 // ============================================================================
50 // @class wxPoserNSView
51 // ============================================================================
52 @interface wxPoserNSView : NSView
56 - (void)drawRect: (NSRect)rect;
59 WX_IMPLEMENT_POSER(wxPoserNSView);
60 @implementation wxPoserNSView : NSView
62 - (void)drawRect: (NSRect)rect
64 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
65 if( !win || !win->Cocoa_drawRect(rect) )
66 [super drawRect:rect];
69 @end // implementation wxPoserNSView
71 @interface wxNSViewNotificationObserver : NSObject
75 // FIXME: Initializing like this is a really bad idea. If for some reason
76 // we ever require posing as an NSObject we won't be able to since an instance
77 // will have already been created here. Of course, catching messages for
78 // NSObject seems like a LOT of overkill, so I doubt we ever will anyway!
79 void *wxCocoaNSView::sm_cocoaObserver = [[wxNSViewNotificationObserver alloc] init];
81 - (void)notificationFrameChanged: (NSNotification *)notification;
82 @end // interface wxNSViewNotificationObserver
84 @implementation wxNSViewNotificationObserver : NSObject
86 - (void)notificationFrameChanged: (NSNotification *)notification;
88 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
89 wxCHECK_RET(win,"notificationFrameChanged received but no wxWindow exists");
90 win->Cocoa_FrameChanged();
93 @end // implementation wxNSViewNotificationObserver