Catch and forward the following mouse related events:
[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:     wxWindows license
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 <Appkit/NSView.h>
28 #import <Foundation/NSNotification.h>
29 #import <Foundation/NSString.h>
30
31 // ----------------------------------------------------------------------------
32 // globals
33 // ----------------------------------------------------------------------------
34 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSView)
35
36 void wxCocoaNSView::AssociateNSView(WX_NSView cocoaNSView)
37 {
38     sm_cocoaHash.insert(wxCocoaNSViewHash::value_type(cocoaNSView,this));
39     [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(notificationFrameChanged:) name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
40     [cocoaNSView setPostsFrameChangedNotifications: YES];
41 }
42
43 void wxCocoaNSView::DisassociateNSView(WX_NSView cocoaNSView)
44 {
45     sm_cocoaHash.erase(cocoaNSView);
46     [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:@"NSViewFrameDidChangeNotification" object:cocoaNSView];
47 }
48
49 // ============================================================================
50 // @class wxPoserNSView
51 // ============================================================================
52 @interface wxPoserNSView : NSView
53 {
54 }
55
56 - (void)drawRect: (NSRect)rect;
57 - (void)mouseDown:(NSEvent *)theEvent;
58 - (void)mouseDragged:(NSEvent *)theEvent;
59 - (void)mouseUp:(NSEvent *)theEvent;
60 - (void)mouseMoved:(NSEvent *)theEvent;
61 - (void)mouseEntered:(NSEvent *)theEvent;
62 - (void)mouseExited:(NSEvent *)theEvent;
63 - (void)rightMouseDown:(NSEvent *)theEvent;
64 - (void)rightMouseDragged:(NSEvent *)theEvent;
65 - (void)rightMouseUp:(NSEvent *)theEvent;
66 - (void)otherMouseDown:(NSEvent *)theEvent;
67 - (void)otherMouseDragged:(NSEvent *)theEvent;
68 - (void)otherMouseUp:(NSEvent *)theEvent;
69 @end // wxPoserNSView
70
71 WX_IMPLEMENT_POSER(wxPoserNSView);
72 @implementation wxPoserNSView : NSView
73
74 - (void)drawRect: (NSRect)rect
75 {
76     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
77     if( !win || !win->Cocoa_drawRect(rect) )
78         [super drawRect:rect];
79 }
80
81 - (void)mouseDown:(NSEvent *)theEvent
82 {
83     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
84     if( !win || !win->Cocoa_mouseDown(theEvent) )
85         [super mouseDown:theEvent];
86 }
87
88 - (void)mouseDragged:(NSEvent *)theEvent
89 {
90     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
91     if( !win || !win->Cocoa_mouseDragged(theEvent) )
92         [super mouseDragged:theEvent];
93 }
94
95 - (void)mouseUp:(NSEvent *)theEvent
96 {
97     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
98     if( !win || !win->Cocoa_mouseUp(theEvent) )
99         [super mouseUp:theEvent];
100 }
101
102 - (void)mouseMoved:(NSEvent *)theEvent
103 {
104     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
105     if( !win || !win->Cocoa_mouseMoved(theEvent) )
106         [super mouseMoved:theEvent];
107 }
108
109 - (void)mouseEntered:(NSEvent *)theEvent
110 {
111     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
112     if( !win || !win->Cocoa_mouseEntered(theEvent) )
113         [super mouseEntered:theEvent];
114 }
115
116 - (void)mouseExited:(NSEvent *)theEvent
117 {
118     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
119     if( !win || !win->Cocoa_mouseExited(theEvent) )
120         [super mouseExited:theEvent];
121 }
122
123 - (void)rightMouseDown:(NSEvent *)theEvent
124 {
125     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
126     if( !win || !win->Cocoa_rightMouseDown(theEvent) )
127         [super rightMouseDown:theEvent];
128 }
129
130 - (void)rightMouseDragged:(NSEvent *)theEvent
131 {
132     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
133     if( !win || !win->Cocoa_rightMouseDragged(theEvent) )
134         [super rightMouseDragged:theEvent];
135 }
136
137 - (void)rightMouseUp:(NSEvent *)theEvent
138 {
139     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
140     if( !win || !win->Cocoa_rightMouseUp(theEvent) )
141         [super rightMouseUp:theEvent];
142 }
143
144 - (void)otherMouseDown:(NSEvent *)theEvent
145 {
146     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
147     if( !win || !win->Cocoa_otherMouseDown(theEvent) )
148         [super otherMouseDown:theEvent];
149 }
150
151 - (void)otherMouseDragged:(NSEvent *)theEvent
152 {
153     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
154     if( !win || !win->Cocoa_otherMouseDragged(theEvent) )
155         [super otherMouseDragged:theEvent];
156 }
157
158 - (void)otherMouseUp:(NSEvent *)theEvent
159 {
160     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa(self);
161     if( !win || !win->Cocoa_otherMouseUp(theEvent) )
162         [super otherMouseUp:theEvent];
163 }
164
165 @end // implementation wxPoserNSView
166
167 @interface wxNSViewNotificationObserver : NSObject
168 {
169 }
170
171 // FIXME: Initializing like this is a really bad idea.  If for some reason
172 // we ever require posing as an NSObject we won't be able to since an instance
173 // will have already been created here.  Of course, catching messages for
174 // NSObject seems like a LOT of overkill, so I doubt we ever will anyway!
175 void *wxCocoaNSView::sm_cocoaObserver = [[wxNSViewNotificationObserver alloc] init];
176
177 - (void)notificationFrameChanged: (NSNotification *)notification;
178 @end // interface wxNSViewNotificationObserver
179
180 @implementation wxNSViewNotificationObserver : NSObject
181
182 - (void)notificationFrameChanged: (NSNotification *)notification;
183 {
184     wxCocoaNSView *win = wxCocoaNSView::GetFromCocoa([notification object]);
185     wxCHECK_RET(win,"notificationFrameChanged received but no wxWindow exists");
186     win->Cocoa_FrameChanged();
187 }
188
189 @end // implementation wxNSViewNotificationObserver
190