Remove ObjcPose.h since nothing is using it anymore.
[wxWidgets.git] / src / cocoa / NSView.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        cocoa/NSView.mm
3 // Purpose:     wxCocoaNSView
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/02/15
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21 #ifndef WX_PRECOMP
22     #include "wx/window.h"
23 #endif // WX_PRECOMP
24
25 #include "wx/cocoa/NSView.h"
26
27 #import <Foundation/NSNotification.h>
28 #import <Foundation/NSString.h>
29 #include "wx/cocoa/objc/NSView.h"
30
31 // ----------------------------------------------------------------------------
32 // globals
33 // ----------------------------------------------------------------------------
34 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSView)
35
36 void wxCocoaNSView::AssociateNSView(WX_NSView cocoaNSView)
37 {
38     if(cocoaNSView)
39     {
40         sm_cocoaHash.insert(wxCocoaNSViewHash::value_type(cocoaNSView,this));
41         [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(notificationFrameChanged:) name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
42         [cocoaNSView setPostsFrameChangedNotifications: YES];
43     }
44 }
45
46 void wxCocoaNSView::DisassociateNSView(WX_NSView cocoaNSView)
47 {
48     if(cocoaNSView)
49     {
50         sm_cocoaHash.erase(cocoaNSView);
51         [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
52     }
53 }
54
55 // ============================================================================
56 // @class WXNSView
57 // ============================================================================
58
59 @implementation WXNSView : NSView
60
61 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
62 {
63     bool acceptsFirstMouse = false;
64     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
65     if(!win || !win->Cocoa_acceptsFirstMouse(acceptsFirstMouse, theEvent))
66         acceptsFirstMouse = [super acceptsFirstMouse:theEvent];
67     return acceptsFirstMouse;
68 }
69
70 - (void)drawRect: (NSRect)rect
71 {
72     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
73     if( !win || !win->Cocoa_drawRect(rect) )
74         [super drawRect:rect];
75 }
76
77 - (void)mouseDown:(NSEvent *)theEvent
78 {
79     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
80     if( !win || !win->Cocoa_mouseDown(theEvent) )
81         [super mouseDown:theEvent];
82 }
83
84 - (void)mouseDragged:(NSEvent *)theEvent
85 {
86     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
87     if( !win || !win->Cocoa_mouseDragged(theEvent) )
88         [super mouseDragged:theEvent];
89 }
90
91 - (void)mouseUp:(NSEvent *)theEvent
92 {
93     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
94     if( !win || !win->Cocoa_mouseUp(theEvent) )
95         [super mouseUp:theEvent];
96 }
97
98 - (void)mouseMoved:(NSEvent *)theEvent
99 {
100     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
101     if( !win || !win->Cocoa_mouseMoved(theEvent) )
102         [super mouseMoved:theEvent];
103 }
104
105 - (void)mouseEntered:(NSEvent *)theEvent
106 {
107     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
108     if( !win || !win->Cocoa_mouseEntered(theEvent) )
109         [super mouseEntered:theEvent];
110 }
111
112 - (void)mouseExited:(NSEvent *)theEvent
113 {
114     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
115     if( !win || !win->Cocoa_mouseExited(theEvent) )
116         [super mouseExited:theEvent];
117 }
118
119 - (void)rightMouseDown:(NSEvent *)theEvent
120 {
121     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
122     if( !win || !win->Cocoa_rightMouseDown(theEvent) )
123         [super rightMouseDown:theEvent];
124 }
125
126 - (void)rightMouseDragged:(NSEvent *)theEvent
127 {
128     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
129     if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
130         [super rightMouseDragged:theEvent];
131 }
132
133 - (void)rightMouseUp:(NSEvent *)theEvent
134 {
135     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
136     if( !win || !win->Cocoa_rightMouseUp(theEvent) )
137         [super rightMouseUp:theEvent];
138 }
139
140 - (void)otherMouseDown:(NSEvent *)theEvent
141 {
142     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
143     if( !win || !win->Cocoa_otherMouseDown(theEvent) )
144         [super otherMouseDown:theEvent];
145 }
146
147 - (void)otherMouseDragged:(NSEvent *)theEvent
148 {
149     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
150     if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
151         [super otherMouseDragged:theEvent];
152 }
153
154 - (void)otherMouseUp:(NSEvent *)theEvent
155 {
156     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
157     if( !win || !win->Cocoa_otherMouseUp(theEvent) )
158         [super otherMouseUp:theEvent];
159 }
160
161 - (void)resetCursorRects
162 {
163     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
164     if( !win || !win->Cocoa_resetCursorRects() )
165         [super resetCursorRects];
166 }
167
168 @end // implementation WXNSView
169
170 @interface wxNSViewNotificationObserver : NSObject
171 {
172 }
173
174 // FIXME: Initializing like this is a really bad idea.  If for some reason
175 // we ever require posing as an NSObject we won't be able to since an instance
176 // will have already been created here.  Of course, catching messages for
177 // NSObject seems like a LOT of overkill, so I doubt we ever will anyway!
178 void *wxCocoaNSView::sm_cocoaObserver = [[wxNSViewNotificationObserver alloc] init];
179
180 - (void)notificationFrameChanged: (NSNotification *)notification;
181 @end // interface wxNSViewNotificationObserver
182
183 @implementation wxNSViewNotificationObserver : NSObject
184
185 - (void)notificationFrameChanged: (NSNotification *)notification;
186 {
187     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
188     wxCHECK_RET(win,wxT("notificationFrameChanged received but no wxWindow exists"));
189     win->Cocoa_FrameChanged();
190 }
191
192 @end // implementation wxNSViewNotificationObserver
193