1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/NSView.mm
3 // Purpose: wxCocoaNSView
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
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 <Foundation/NSNotification.h>
29 #import <Foundation/NSString.h>
30 #include "wx/cocoa/objc/NSView.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 @end // implementation WXNSView
171 @interface wxNSViewNotificationObserver : NSObject
175 // FIXME: Initializing like this is a really bad idea. If for some reason
176 // we ever require posing as an NSObject we won't be able to since an instance
177 // will have already been created here. Of course, catching messages for
178 // NSObject seems like a LOT of overkill, so I doubt we ever will anyway!
179 void *wxCocoaNSView::sm_cocoaObserver = [[wxNSViewNotificationObserver alloc] init];
181 - (void)notificationFrameChanged: (NSNotification *)notification;
182 @end // interface wxNSViewNotificationObserver
184 @implementation wxNSViewNotificationObserver : NSObject
186 - (void)notificationFrameChanged: (NSNotification *)notification;
188 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
189 wxCHECK_RET(win,wxT("notificationFrameChanged received but no wxWindow exists"));
190 win->Cocoa_FrameChanged();
193 @end // implementation wxNSViewNotificationObserver