Remove Objective-C class posing for everything except for NSApplication.
[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/ObjcPose.h"
26 #include "wx/cocoa/NSView.h"
27
28 #import <Foundation/NSNotification.h>
29 #import <Foundation/NSString.h>
30 #include "wx/cocoa/objc/NSView.h"
31
32 // ----------------------------------------------------------------------------
33 // globals
34 // ----------------------------------------------------------------------------
35 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSView)
36
37 void wxCocoaNSView::AssociateNSView(WX_NSView cocoaNSView)
38 {
39     if(cocoaNSView)
40     {
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];
44     }
45 }
46
47 void wxCocoaNSView::DisassociateNSView(WX_NSView cocoaNSView)
48 {
49     if(cocoaNSView)
50     {
51         sm_cocoaHash.erase(cocoaNSView);
52         [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
53     }
54 }
55
56 // ============================================================================
57 // @class WXNSView
58 // ============================================================================
59
60 @implementation WXNSView : NSView
61
62 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
63 {
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;
69 }
70
71 - (void)drawRect: (NSRect)rect
72 {
73     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
74     if( !win || !win->Cocoa_drawRect(rect) )
75         [super drawRect:rect];
76 }
77
78 - (void)mouseDown:(NSEvent *)theEvent
79 {
80     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
81     if( !win || !win->Cocoa_mouseDown(theEvent) )
82         [super mouseDown:theEvent];
83 }
84
85 - (void)mouseDragged:(NSEvent *)theEvent
86 {
87     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
88     if( !win || !win->Cocoa_mouseDragged(theEvent) )
89         [super mouseDragged:theEvent];
90 }
91
92 - (void)mouseUp:(NSEvent *)theEvent
93 {
94     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
95     if( !win || !win->Cocoa_mouseUp(theEvent) )
96         [super mouseUp:theEvent];
97 }
98
99 - (void)mouseMoved:(NSEvent *)theEvent
100 {
101     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
102     if( !win || !win->Cocoa_mouseMoved(theEvent) )
103         [super mouseMoved:theEvent];
104 }
105
106 - (void)mouseEntered:(NSEvent *)theEvent
107 {
108     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
109     if( !win || !win->Cocoa_mouseEntered(theEvent) )
110         [super mouseEntered:theEvent];
111 }
112
113 - (void)mouseExited:(NSEvent *)theEvent
114 {
115     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
116     if( !win || !win->Cocoa_mouseExited(theEvent) )
117         [super mouseExited:theEvent];
118 }
119
120 - (void)rightMouseDown:(NSEvent *)theEvent
121 {
122     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
123     if( !win || !win->Cocoa_rightMouseDown(theEvent) )
124         [super rightMouseDown:theEvent];
125 }
126
127 - (void)rightMouseDragged:(NSEvent *)theEvent
128 {
129     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
130     if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
131         [super rightMouseDragged:theEvent];
132 }
133
134 - (void)rightMouseUp:(NSEvent *)theEvent
135 {
136     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
137     if( !win || !win->Cocoa_rightMouseUp(theEvent) )
138         [super rightMouseUp:theEvent];
139 }
140
141 - (void)otherMouseDown:(NSEvent *)theEvent
142 {
143     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
144     if( !win || !win->Cocoa_otherMouseDown(theEvent) )
145         [super otherMouseDown:theEvent];
146 }
147
148 - (void)otherMouseDragged:(NSEvent *)theEvent
149 {
150     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
151     if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
152         [super otherMouseDragged:theEvent];
153 }
154
155 - (void)otherMouseUp:(NSEvent *)theEvent
156 {
157     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
158     if( !win || !win->Cocoa_otherMouseUp(theEvent) )
159         [super otherMouseUp:theEvent];
160 }
161
162 - (void)resetCursorRects
163 {
164     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
165     if( !win || !win->Cocoa_resetCursorRects() )
166         [super resetCursorRects];
167 }
168
169 @end // implementation WXNSView
170
171 @interface wxNSViewNotificationObserver : NSObject
172 {
173 }
174
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];
180
181 - (void)notificationFrameChanged: (NSNotification *)notification;
182 @end // interface wxNSViewNotificationObserver
183
184 @implementation wxNSViewNotificationObserver : NSObject
185
186 - (void)notificationFrameChanged: (NSNotification *)notification;
187 {
188     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
189     wxCHECK_RET(win,wxT("notificationFrameChanged received but no wxWindow exists"));
190     win->Cocoa_FrameChanged();
191 }
192
193 @end // implementation wxNSViewNotificationObserver
194