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 - (void)drawRect: (NSRect)rect
84 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
85 if( !win || !win->Cocoa_drawRect(rect) )
86 [super drawRect:rect];
89 - (void)mouseDown:(NSEvent *)theEvent
91 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
92 if( !win || !win->Cocoa_mouseDown(theEvent) )
93 [super mouseDown:theEvent];
96 - (void)mouseDragged:(NSEvent *)theEvent
98 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
99 if( !win || !win->Cocoa_mouseDragged(theEvent) )
100 [super mouseDragged:theEvent];
103 - (void)mouseUp:(NSEvent *)theEvent
105 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
106 if( !win || !win->Cocoa_mouseUp(theEvent) )
107 [super mouseUp:theEvent];
110 - (void)mouseMoved:(NSEvent *)theEvent
112 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
113 if( !win || !win->Cocoa_mouseMoved(theEvent) )
114 [super mouseMoved:theEvent];
117 - (void)mouseEntered:(NSEvent *)theEvent
119 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
120 if( !win || !win->Cocoa_mouseEntered(theEvent) )
121 [super mouseEntered:theEvent];
124 - (void)mouseExited:(NSEvent *)theEvent
126 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
127 if( !win || !win->Cocoa_mouseExited(theEvent) )
128 [super mouseExited:theEvent];
131 - (void)rightMouseDown:(NSEvent *)theEvent
133 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
134 if( !win || !win->Cocoa_rightMouseDown(theEvent) )
135 [super rightMouseDown:theEvent];
138 - (void)rightMouseDragged:(NSEvent *)theEvent
140 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
141 if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
142 [super rightMouseDragged:theEvent];
145 - (void)rightMouseUp:(NSEvent *)theEvent
147 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
148 if( !win || !win->Cocoa_rightMouseUp(theEvent) )
149 [super rightMouseUp:theEvent];
152 - (void)otherMouseDown:(NSEvent *)theEvent
154 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
155 if( !win || !win->Cocoa_otherMouseDown(theEvent) )
156 [super otherMouseDown:theEvent];
159 - (void)otherMouseDragged:(NSEvent *)theEvent
161 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
162 if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
163 [super otherMouseDragged:theEvent];
166 - (void)otherMouseUp:(NSEvent *)theEvent
168 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
169 if( !win || !win->Cocoa_otherMouseUp(theEvent) )
170 [super otherMouseUp:theEvent];
173 - (void)resetCursorRects
175 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
176 if( !win || !win->Cocoa_resetCursorRects() )
177 [super resetCursorRects];
180 @end // implementation wxPoserNSView
182 @interface wxNSViewNotificationObserver : NSObject
186 // FIXME: Initializing like this is a really bad idea. If for some reason
187 // we ever require posing as an NSObject we won't be able to since an instance
188 // will have already been created here. Of course, catching messages for
189 // NSObject seems like a LOT of overkill, so I doubt we ever will anyway!
190 void *wxCocoaNSView::sm_cocoaObserver = [[wxNSViewNotificationObserver alloc] init];
192 - (void)notificationFrameChanged: (NSNotification *)notification;
193 @end // interface wxNSViewNotificationObserver
195 @implementation wxNSViewNotificationObserver : NSObject
197 - (void)notificationFrameChanged: (NSNotification *)notification;
199 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
200 wxCHECK_RET(win,wxT("notificationFrameChanged received but no wxWindow exists"));
201 win->Cocoa_FrameChanged();
204 @end // implementation wxNSViewNotificationObserver