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/ObjcPose.h"
26 #include "wx/cocoa/NSView.h"
28 #import <Appkit/NSView.h>
29 #import <Foundation/NSNotification.h>
30 #import <Foundation/NSString.h>
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
35 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSView)
37 void wxCocoaNSView::AssociateNSView(WX_NSView cocoaNSView)
39 sm_cocoaHash.insert(wxCocoaNSViewHash::value_type(cocoaNSView,this));
40 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(notificationFrameChanged:) name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
41 [cocoaNSView setPostsFrameChangedNotifications: YES];
44 void wxCocoaNSView::DisassociateNSView(WX_NSView cocoaNSView)
46 sm_cocoaHash.erase(cocoaNSView);
47 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
50 // ============================================================================
51 // @class wxPoserNSView
52 // ============================================================================
53 @interface wxPoserNSView : NSView
57 - (void)drawRect: (NSRect)rect;
58 - (void)mouseDown:(NSEvent *)theEvent;
59 - (void)mouseDragged:(NSEvent *)theEvent;
60 - (void)mouseUp:(NSEvent *)theEvent;
61 - (void)mouseMoved:(NSEvent *)theEvent;
62 - (void)mouseEntered:(NSEvent *)theEvent;
63 - (void)mouseExited:(NSEvent *)theEvent;
64 - (void)rightMouseDown:(NSEvent *)theEvent;
65 - (void)rightMouseDragged:(NSEvent *)theEvent;
66 - (void)rightMouseUp:(NSEvent *)theEvent;
67 - (void)otherMouseDown:(NSEvent *)theEvent;
68 - (void)otherMouseDragged:(NSEvent *)theEvent;
69 - (void)otherMouseUp:(NSEvent *)theEvent;
72 WX_IMPLEMENT_POSER(wxPoserNSView);
73 @implementation wxPoserNSView : NSView
75 - (void)drawRect: (NSRect)rect
77 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
78 if( !win || !win->Cocoa_drawRect(rect) )
79 [super drawRect:rect];
82 - (void)mouseDown:(NSEvent *)theEvent
84 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
85 if( !win || !win->Cocoa_mouseDown(theEvent) )
86 [super mouseDown:theEvent];
89 - (void)mouseDragged:(NSEvent *)theEvent
91 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
92 if( !win || !win->Cocoa_mouseDragged(theEvent) )
93 [super mouseDragged:theEvent];
96 - (void)mouseUp:(NSEvent *)theEvent
98 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
99 if( !win || !win->Cocoa_mouseUp(theEvent) )
100 [super mouseUp:theEvent];
103 - (void)mouseMoved:(NSEvent *)theEvent
105 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
106 if( !win || !win->Cocoa_mouseMoved(theEvent) )
107 [super mouseMoved:theEvent];
110 - (void)mouseEntered:(NSEvent *)theEvent
112 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
113 if( !win || !win->Cocoa_mouseEntered(theEvent) )
114 [super mouseEntered:theEvent];
117 - (void)mouseExited:(NSEvent *)theEvent
119 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
120 if( !win || !win->Cocoa_mouseExited(theEvent) )
121 [super mouseExited:theEvent];
124 - (void)rightMouseDown:(NSEvent *)theEvent
126 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
127 if( !win || !win->Cocoa_rightMouseDown(theEvent) )
128 [super rightMouseDown:theEvent];
131 - (void)rightMouseDragged:(NSEvent *)theEvent
133 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
134 if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
135 [super rightMouseDragged:theEvent];
138 - (void)rightMouseUp:(NSEvent *)theEvent
140 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
141 if( !win || !win->Cocoa_rightMouseUp(theEvent) )
142 [super rightMouseUp:theEvent];
145 - (void)otherMouseDown:(NSEvent *)theEvent
147 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
148 if( !win || !win->Cocoa_otherMouseDown(theEvent) )
149 [super otherMouseDown:theEvent];
152 - (void)otherMouseDragged:(NSEvent *)theEvent
154 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
155 if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
156 [super otherMouseDragged:theEvent];
159 - (void)otherMouseUp:(NSEvent *)theEvent
161 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
162 if( !win || !win->Cocoa_otherMouseUp(theEvent) )
163 [super otherMouseUp:theEvent];
166 @end // implementation wxPoserNSView
168 @interface wxNSViewNotificationObserver : NSObject
172 // FIXME: Initializing like this is a really bad idea. If for some reason
173 // we ever require posing as an NSObject we won't be able to since an instance
174 // will have already been created here. Of course, catching messages for
175 // NSObject seems like a LOT of overkill, so I doubt we ever will anyway!
176 void *wxCocoaNSView::sm_cocoaObserver = [[wxNSViewNotificationObserver alloc] init];
178 - (void)notificationFrameChanged: (NSNotification *)notification;
179 @end // interface wxNSViewNotificationObserver
181 @implementation wxNSViewNotificationObserver : NSObject
183 - (void)notificationFrameChanged: (NSNotification *)notification;
185 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
186 wxCHECK_RET(win,"notificationFrameChanged received but no wxWindow exists");
187 win->Cocoa_FrameChanged();
190 @end // implementation wxNSViewNotificationObserver