Added Cocoa_acceptsFirstMouse.
[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 <AppKit/NSView.h>
29 #import <Foundation/NSNotification.h>
30 #import <Foundation/NSString.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 wxPoserNSView
58 // ============================================================================
59 @interface wxPoserNSView : NSView
60 {
61 }
62
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;
77 @end // wxPoserNSView
78
79 WX_IMPLEMENT_POSER(wxPoserNSView);
80 @implementation wxPoserNSView : NSView
81
82 - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
83 {
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;
89 }
90
91 - (void)drawRect: (NSRect)rect
92 {
93     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
94     if( !win || !win->Cocoa_drawRect(rect) )
95         [super drawRect:rect];
96 }
97
98 - (void)mouseDown:(NSEvent *)theEvent
99 {
100     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
101     if( !win || !win->Cocoa_mouseDown(theEvent) )
102         [super mouseDown:theEvent];
103 }
104
105 - (void)mouseDragged:(NSEvent *)theEvent
106 {
107     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
108     if( !win || !win->Cocoa_mouseDragged(theEvent) )
109         [super mouseDragged:theEvent];
110 }
111
112 - (void)mouseUp:(NSEvent *)theEvent
113 {
114     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
115     if( !win || !win->Cocoa_mouseUp(theEvent) )
116         [super mouseUp:theEvent];
117 }
118
119 - (void)mouseMoved:(NSEvent *)theEvent
120 {
121     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
122     if( !win || !win->Cocoa_mouseMoved(theEvent) )
123         [super mouseMoved:theEvent];
124 }
125
126 - (void)mouseEntered:(NSEvent *)theEvent
127 {
128     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
129     if( !win || !win->Cocoa_mouseEntered(theEvent) )
130         [super mouseEntered:theEvent];
131 }
132
133 - (void)mouseExited:(NSEvent *)theEvent
134 {
135     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
136     if( !win || !win->Cocoa_mouseExited(theEvent) )
137         [super mouseExited:theEvent];
138 }
139
140 - (void)rightMouseDown:(NSEvent *)theEvent
141 {
142     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
143     if( !win || !win->Cocoa_rightMouseDown(theEvent) )
144         [super rightMouseDown:theEvent];
145 }
146
147 - (void)rightMouseDragged:(NSEvent *)theEvent
148 {
149     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
150     if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
151         [super rightMouseDragged:theEvent];
152 }
153
154 - (void)rightMouseUp:(NSEvent *)theEvent
155 {
156     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
157     if( !win || !win->Cocoa_rightMouseUp(theEvent) )
158         [super rightMouseUp:theEvent];
159 }
160
161 - (void)otherMouseDown:(NSEvent *)theEvent
162 {
163     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
164     if( !win || !win->Cocoa_otherMouseDown(theEvent) )
165         [super otherMouseDown:theEvent];
166 }
167
168 - (void)otherMouseDragged:(NSEvent *)theEvent
169 {
170     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
171     if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
172         [super otherMouseDragged:theEvent];
173 }
174
175 - (void)otherMouseUp:(NSEvent *)theEvent
176 {
177     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
178     if( !win || !win->Cocoa_otherMouseUp(theEvent) )
179         [super otherMouseUp:theEvent];
180 }
181
182 - (void)resetCursorRects
183 {
184     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
185     if( !win || !win->Cocoa_resetCursorRects() )
186         [super resetCursorRects];
187 }
188
189 @end // implementation wxPoserNSView
190
191 @interface wxNSViewNotificationObserver : NSObject
192 {
193 }
194
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];
200
201 - (void)notificationFrameChanged: (NSNotification *)notification;
202 @end // interface wxNSViewNotificationObserver
203
204 @implementation wxNSViewNotificationObserver : NSObject
205
206 - (void)notificationFrameChanged: (NSNotification *)notification;
207 {
208     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
209     wxCHECK_RET(win,wxT("notificationFrameChanged received but no wxWindow exists"));
210     win->Cocoa_FrameChanged();
211 }
212
213 @end // implementation wxNSViewNotificationObserver
214