]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/NSView.mm
added possibility to throw different objects and crash directly from the main frame...
[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 - (void)drawRect: (NSRect)rect
83 {
84 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
85 if( !win || !win->Cocoa_drawRect(rect) )
86 [super drawRect:rect];
87 }
88
89 - (void)mouseDown:(NSEvent *)theEvent
90 {
91 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
92 if( !win || !win->Cocoa_mouseDown(theEvent) )
93 [super mouseDown:theEvent];
94 }
95
96 - (void)mouseDragged:(NSEvent *)theEvent
97 {
98 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
99 if( !win || !win->Cocoa_mouseDragged(theEvent) )
100 [super mouseDragged:theEvent];
101 }
102
103 - (void)mouseUp:(NSEvent *)theEvent
104 {
105 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
106 if( !win || !win->Cocoa_mouseUp(theEvent) )
107 [super mouseUp:theEvent];
108 }
109
110 - (void)mouseMoved:(NSEvent *)theEvent
111 {
112 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
113 if( !win || !win->Cocoa_mouseMoved(theEvent) )
114 [super mouseMoved:theEvent];
115 }
116
117 - (void)mouseEntered:(NSEvent *)theEvent
118 {
119 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
120 if( !win || !win->Cocoa_mouseEntered(theEvent) )
121 [super mouseEntered:theEvent];
122 }
123
124 - (void)mouseExited:(NSEvent *)theEvent
125 {
126 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
127 if( !win || !win->Cocoa_mouseExited(theEvent) )
128 [super mouseExited:theEvent];
129 }
130
131 - (void)rightMouseDown:(NSEvent *)theEvent
132 {
133 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
134 if( !win || !win->Cocoa_rightMouseDown(theEvent) )
135 [super rightMouseDown:theEvent];
136 }
137
138 - (void)rightMouseDragged:(NSEvent *)theEvent
139 {
140 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
141 if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
142 [super rightMouseDragged:theEvent];
143 }
144
145 - (void)rightMouseUp:(NSEvent *)theEvent
146 {
147 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
148 if( !win || !win->Cocoa_rightMouseUp(theEvent) )
149 [super rightMouseUp:theEvent];
150 }
151
152 - (void)otherMouseDown:(NSEvent *)theEvent
153 {
154 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
155 if( !win || !win->Cocoa_otherMouseDown(theEvent) )
156 [super otherMouseDown:theEvent];
157 }
158
159 - (void)otherMouseDragged:(NSEvent *)theEvent
160 {
161 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
162 if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
163 [super otherMouseDragged:theEvent];
164 }
165
166 - (void)otherMouseUp:(NSEvent *)theEvent
167 {
168 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
169 if( !win || !win->Cocoa_otherMouseUp(theEvent) )
170 [super otherMouseUp:theEvent];
171 }
172
173 - (void)resetCursorRects
174 {
175 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
176 if( !win || !win->Cocoa_resetCursorRects() )
177 [super resetCursorRects];
178 }
179
180 @end // implementation wxPoserNSView
181
182 @interface wxNSViewNotificationObserver : NSObject
183 {
184 }
185
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];
191
192 - (void)notificationFrameChanged: (NSNotification *)notification;
193 @end // interface wxNSViewNotificationObserver
194
195 @implementation wxNSViewNotificationObserver : NSObject
196
197 - (void)notificationFrameChanged: (NSNotification *)notification;
198 {
199 wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
200 wxCHECK_RET(win,wxT("notificationFrameChanged received but no wxWindow exists"));
201 win->Cocoa_FrameChanged();
202 }
203
204 @end // implementation wxNSViewNotificationObserver
205