1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/NSView.mm
3 // Purpose: wxCocoaNSView
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
21 #include "wx/window.h"
24 #include "wx/cocoa/objc/objc_uniquifying.h"
25 #include "wx/cocoa/NSView.h"
27 #import <Foundation/NSNotification.h>
28 #import <Foundation/NSString.h>
29 #include "wx/cocoa/objc/NSView.h"
30 #include "wx/cocoa/ObjcRef.h"
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
35 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSView)
37 void wxCocoaNSView::AssociateNSView(WX_NSView cocoaNSView)
41 sm_cocoaHash.insert(wxCocoaNSViewHash::value_type(cocoaNSView,this));
42 [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(notificationFrameChanged:) name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
43 [cocoaNSView setPostsFrameChangedNotifications: YES];
47 void wxCocoaNSView::DisassociateNSView(WX_NSView cocoaNSView)
51 sm_cocoaHash.erase(cocoaNSView);
52 [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
56 // ============================================================================
58 // ============================================================================
60 @implementation WXNSView : NSView
62 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
64 bool acceptsFirstMouse = false;
65 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
66 if(!win || !win->Cocoa_acceptsFirstMouse(acceptsFirstMouse, theEvent))
67 acceptsFirstMouse = [super acceptsFirstMouse:theEvent];
68 return acceptsFirstMouse;
71 - (void)drawRect: (NSRect)rect
73 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
74 if( !win || !win->Cocoa_drawRect(rect) )
75 [super drawRect:rect];
78 - (void)mouseDown:(NSEvent *)theEvent
80 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
81 if( !win || !win->Cocoa_mouseDown(theEvent) )
82 [super mouseDown:theEvent];
85 - (void)mouseDragged:(NSEvent *)theEvent
87 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
88 if( !win || !win->Cocoa_mouseDragged(theEvent) )
89 [super mouseDragged:theEvent];
92 - (void)mouseUp:(NSEvent *)theEvent
94 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
95 if( !win || !win->Cocoa_mouseUp(theEvent) )
96 [super mouseUp:theEvent];
99 - (void)mouseMoved:(NSEvent *)theEvent
101 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
102 if( !win || !win->Cocoa_mouseMoved(theEvent) )
103 [super mouseMoved:theEvent];
106 - (void)mouseEntered:(NSEvent *)theEvent
108 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
109 if( !win || !win->Cocoa_mouseEntered(theEvent) )
110 [super mouseEntered:theEvent];
113 - (void)mouseExited:(NSEvent *)theEvent
115 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
116 if( !win || !win->Cocoa_mouseExited(theEvent) )
117 [super mouseExited:theEvent];
120 - (void)rightMouseDown:(NSEvent *)theEvent
122 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
123 if( !win || !win->Cocoa_rightMouseDown(theEvent) )
124 [super rightMouseDown:theEvent];
127 - (void)rightMouseDragged:(NSEvent *)theEvent
129 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
130 if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
131 [super rightMouseDragged:theEvent];
134 - (void)rightMouseUp:(NSEvent *)theEvent
136 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
137 if( !win || !win->Cocoa_rightMouseUp(theEvent) )
138 [super rightMouseUp:theEvent];
141 - (void)otherMouseDown:(NSEvent *)theEvent
143 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
144 if( !win || !win->Cocoa_otherMouseDown(theEvent) )
145 [super otherMouseDown:theEvent];
148 - (void)otherMouseDragged:(NSEvent *)theEvent
150 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
151 if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
152 [super otherMouseDragged:theEvent];
155 - (void)otherMouseUp:(NSEvent *)theEvent
157 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
158 if( !win || !win->Cocoa_otherMouseUp(theEvent) )
159 [super otherMouseUp:theEvent];
162 - (void)resetCursorRects
164 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
165 if( !win || !win->Cocoa_resetCursorRects() )
166 [super resetCursorRects];
169 - (void)viewDidMoveToWindow
171 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
172 if( !win || !win->Cocoa_viewDidMoveToWindow() )
173 [super viewDidMoveToWindow];
176 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
178 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
179 if( !win || !win->Cocoa_viewWillMoveToWindow(newWindow) )
180 [super viewWillMoveToWindow:newWindow];
183 @end // implementation WXNSView
184 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSView,NSView)
186 // ============================================================================
187 // @class wxNSViewNotificationObserver
188 // ============================================================================
190 @interface wxNSViewNotificationObserver : NSObject
194 - (void)notificationFrameChanged: (NSNotification *)notification;
195 - (void)synthesizeMouseMovedForView: (NSView *)theView;
196 @end // interface wxNSViewNotificationObserver
197 WX_DECLARE_GET_OBJC_CLASS(wxNSViewNotificationObserver,NSObject)
199 @implementation wxNSViewNotificationObserver : NSObject
201 - (void)notificationFrameChanged: (NSNotification *)notification;
203 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
204 wxCHECK_RET(win,wxT("notificationFrameChanged received but no wxWindow exists"));
205 win->Cocoa_FrameChanged();
208 - (void)synthesizeMouseMovedForView: (NSView *)theView
210 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(theView);
211 wxCHECK_RET(win,wxT("synthesizeMouseMovedForView received but no wxWindow exists"));
212 win->Cocoa_synthesizeMouseMoved();
215 @end // implementation wxNSViewNotificationObserver
216 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSViewNotificationObserver,NSObject)
218 // New CF-retained observer (this should have been using wxObjcAutoRefFromAlloc to begin with)
219 wxObjcAutoRefFromAlloc<wxNSViewNotificationObserver*> s_cocoaNSViewObserver([[WX_GET_OBJC_CLASS(wxNSViewNotificationObserver) alloc] init]);
220 // For compatibility with old code
221 id wxCocoaNSView::sm_cocoaObserver = s_cocoaNSViewObserver;