The rounded corners look really dumb at this size.
[wxWidgets.git] / src / cocoa / NSView.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/NSView.mm
3 // Purpose:     wxCocoaNSView
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/02/15
7 // Copyright:   (c) 2003 David Elliott
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/wxprec.h"
20 #ifndef WX_PRECOMP
21     #include "wx/window.h"
22 #endif // WX_PRECOMP
23
24 #include "wx/cocoa/objc/objc_uniquifying.h"
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 #include "wx/cocoa/ObjcRef.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 - (void)viewDidMoveToWindow
170 {
171     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
172     if( !win || !win->Cocoa_viewDidMoveToWindow() )
173         [super viewDidMoveToWindow];
174 }
175
176 - (void)viewWillMoveToWindow:(NSWindow *)newWindow
177 {
178     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
179     if( !win || !win->Cocoa_viewWillMoveToWindow(newWindow) )
180         [super viewWillMoveToWindow:newWindow];
181 }
182
183 @end // implementation WXNSView
184 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSView,NSView)
185
186 // ============================================================================
187 // @class wxNSViewNotificationObserver
188 // ============================================================================
189
190 @interface wxNSViewNotificationObserver : NSObject
191 {
192 }
193
194 - (void)notificationFrameChanged: (NSNotification *)notification;
195 - (void)synthesizeMouseMovedForView: (NSView *)theView;
196 @end // interface wxNSViewNotificationObserver
197 WX_DECLARE_GET_OBJC_CLASS(wxNSViewNotificationObserver,NSObject)
198
199 @implementation wxNSViewNotificationObserver : NSObject
200
201 - (void)notificationFrameChanged: (NSNotification *)notification;
202 {
203     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
204     wxCHECK_RET(win,wxT("notificationFrameChanged received but no wxWindow exists"));
205     win->Cocoa_FrameChanged();
206 }
207
208 - (void)synthesizeMouseMovedForView: (NSView *)theView
209 {
210     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(theView);
211     wxCHECK_RET(win,wxT("synthesizeMouseMovedForView received but no wxWindow exists"));
212     win->Cocoa_synthesizeMouseMoved();
213 }
214
215 @end // implementation wxNSViewNotificationObserver
216 WX_IMPLEMENT_GET_OBJC_CLASS(wxNSViewNotificationObserver,NSObject)
217
218 // New CF-retained observer (this should have been using wxObjcAutoRefFromAlloc to begin with)
219 wxObjcAutoRefFromAlloc<wxNSViewNotificationObserver*> s_cocoaNSViewObserver([[WX_GET_OBJC_CLASS(wxNSViewNotificationObserver) alloc] init]);
220 // For compatibility with old code
221 id wxCocoaNSView::sm_cocoaObserver = s_cocoaNSViewObserver;