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 <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)
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 // ============================================================================
57 // @class wxPoserNSView
58 // ============================================================================
59 @interface wxPoserNSView : NSView
63 - (void)drawRect: (NSRect)rect;
64 - (void)mouseDown:(NSEvent *)theEvent;
65 - (void)mouseDragged:(NSEvent *)theEvent;
66 - (void)mouseUp:(NSEvent *)theEvent;
67 - (void)mouseMoved:(NSEvent *)theEvent;
68 - (void)mouseEntered:(NSEvent *)theEvent;
69 - (void)mouseExited:(NSEvent *)theEvent;
70 - (void)rightMouseDown:(NSEvent *)theEvent;
71 - (void)rightMouseDragged:(NSEvent *)theEvent;
72 - (void)rightMouseUp:(NSEvent *)theEvent;
73 - (void)otherMouseDown:(NSEvent *)theEvent;
74 - (void)otherMouseDragged:(NSEvent *)theEvent;
75 - (void)otherMouseUp:(NSEvent *)theEvent;
76 - (void)resetCursorRects;
79 WX_IMPLEMENT_POSER(wxPoserNSView);
80 @implementation wxPoserNSView : NSView
82 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
84 bool acceptsFirstMouse = false;
85 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
86 if(!win || !win->Cocoa_acceptsFirstMouse(acceptsFirstMouse, theEvent))
87 acceptsFirstMouse = [super acceptsFirstMouse:theEvent];
88 return acceptsFirstMouse;
91 - (void)drawRect: (NSRect)rect
93 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
94 if( !win || !win->Cocoa_drawRect(rect) )
95 [super drawRect:rect];
98 - (void)mouseDown:(NSEvent *)theEvent
100 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
101 if( !win || !win->Cocoa_mouseDown(theEvent) )
102 [super mouseDown:theEvent];
105 - (void)mouseDragged:(NSEvent *)theEvent
107 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
108 if( !win || !win->Cocoa_mouseDragged(theEvent) )
109 [super mouseDragged:theEvent];
112 - (void)mouseUp:(NSEvent *)theEvent
114 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
115 if( !win || !win->Cocoa_mouseUp(theEvent) )
116 [super mouseUp:theEvent];
119 - (void)mouseMoved:(NSEvent *)theEvent
121 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
122 if( !win || !win->Cocoa_mouseMoved(theEvent) )
123 [super mouseMoved:theEvent];
126 - (void)mouseEntered:(NSEvent *)theEvent
128 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
129 if( !win || !win->Cocoa_mouseEntered(theEvent) )
130 [super mouseEntered:theEvent];
133 - (void)mouseExited:(NSEvent *)theEvent
135 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
136 if( !win || !win->Cocoa_mouseExited(theEvent) )
137 [super mouseExited:theEvent];
140 - (void)rightMouseDown:(NSEvent *)theEvent
142 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
143 if( !win || !win->Cocoa_rightMouseDown(theEvent) )
144 [super rightMouseDown:theEvent];
147 - (void)rightMouseDragged:(NSEvent *)theEvent
149 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
150 if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
151 [super rightMouseDragged:theEvent];
154 - (void)rightMouseUp:(NSEvent *)theEvent
156 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
157 if( !win || !win->Cocoa_rightMouseUp(theEvent) )
158 [super rightMouseUp:theEvent];
161 - (void)otherMouseDown:(NSEvent *)theEvent
163 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
164 if( !win || !win->Cocoa_otherMouseDown(theEvent) )
165 [super otherMouseDown:theEvent];
168 - (void)otherMouseDragged:(NSEvent *)theEvent
170 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
171 if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
172 [super otherMouseDragged:theEvent];
175 - (void)otherMouseUp:(NSEvent *)theEvent
177 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
178 if( !win || !win->Cocoa_otherMouseUp(theEvent) )
179 [super otherMouseUp:theEvent];
182 - (void)resetCursorRects
184 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
185 if( !win || !win->Cocoa_resetCursorRects() )
186 [super resetCursorRects];
189 @end // implementation wxPoserNSView
191 @interface wxNSViewNotificationObserver : NSObject
195 // FIXME: Initializing like this is a really bad idea. If for some reason
196 // we ever require posing as an NSObject we won't be able to since an instance
197 // will have already been created here. Of course, catching messages for
198 // NSObject seems like a LOT of overkill, so I doubt we ever will anyway!
199 void *wxCocoaNSView::sm_cocoaObserver = [[wxNSViewNotificationObserver alloc] init];
201 - (void)notificationFrameChanged: (NSNotification *)notification;
202 @end // interface wxNSViewNotificationObserver
204 @implementation wxNSViewNotificationObserver : NSObject
206 - (void)notificationFrameChanged: (NSNotification *)notification;
208 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
209 wxCHECK_RET(win,wxT("notificationFrameChanged received but no wxWindow exists"));
210 win->Cocoa_FrameChanged();
213 @end // implementation wxNSViewNotificationObserver