Don't needlessly indirect through NSNotificationCenter.
[wxWidgets.git] / src / cocoa / NSSlider.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/NSSlider.mm
3 // Purpose:     wxCocoaNSSlider class
4 // Author:      Mark Oxenham
5 // Modified by: David Elliott
6 // Created:     2007/08/10
7 // RCS-ID:      $Id$
8 // Copyright:   (c) 2007 Software 2000 Ltd. All rights reserved.
9 // Licence:     wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #ifndef WX_PRECOMP
15     #include "wx/log.h"
16 #endif // WX_PRECOMP
17
18 #include "wx/cocoa/NSSlider.h"
19
20 #import <Foundation/NSNotification.h>
21 #import <Foundation/NSString.h>
22 #import <AppKit/NSEvent.h>
23 #include "wx/cocoa/objc/NSSlider.h"
24
25 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSSlider)
26
27 // ============================================================================
28 // @class wxNSSliderTarget
29 // ============================================================================
30 @interface wxNSSliderTarget : NSObject
31 {
32 }
33
34 - (void)wxNSSliderUpArrowKeyDown: (id)sender;
35 - (void)wxNSSliderDownArrowKeyDown: (id)sender;
36 - (void)wxNSSliderLeftArrowKeyDown: (id)sender;
37 - (void)wxNSSliderRightArrowKeyDown: (id)sender;
38 - (void)wxNSSliderPageUpKeyDown: (id)sender;
39 - (void)wxNSSliderPageDownKeyDown: (id)sender;
40 - (void)wxNSSliderMoveUp: (id)sender;
41 - (void)wxNSSliderMoveDown: (id)sender;
42 - (void)wxNSSliderMoveLeft: (id)sender;
43 - (void)wxNSSliderMoveRight: (id)sender;
44 - (void)wxNSSliderPageUp: (id)sender;
45 - (void)wxNSSliderPageDown: (id)sender;
46 @end // wxNSSliderTarget
47
48 @implementation wxNSSliderTarget : NSObject
49
50 - (void)wxNSSliderUpArrowKeyDown: (id)sender
51 {
52     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
53     wxCHECK_RET(slider,wxT("wxNSSliderUpArrowKeyDown received without associated wx object"));
54     slider->Cocoa_wxNSSliderUpArrowKeyDown();
55 }
56
57 - (void)wxNSSliderDownArrowKeyDown: (id)sender
58 {
59     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
60     wxCHECK_RET(slider,wxT("wxNSSliderDownArrowKeyDown received without associated wx object"));
61     slider->Cocoa_wxNSSliderDownArrowKeyDown();
62 }
63
64 - (void)wxNSSliderLeftArrowKeyDown: (id)sender
65 {
66     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
67     wxCHECK_RET(slider,wxT("wxNSSliderLeftArrowKeyDown received without associated wx object"));
68     slider->Cocoa_wxNSSliderLeftArrowKeyDown();
69 }
70
71 - (void)wxNSSliderRightArrowKeyDown: (id)sender
72 {
73     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
74     wxCHECK_RET(slider,wxT("wxNSSliderRightArrowKeyDown received without associated wx object"));
75     slider->Cocoa_wxNSSliderRightArrowKeyDown();
76 }
77
78 - (void)wxNSSliderPageUpKeyDown: (id)sender
79 {
80     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
81     wxCHECK_RET(slider,wxT("wxNSSliderPageUpKeyDown received without associated wx object"));
82     slider->Cocoa_wxNSSliderPageUpKeyDown();
83 }
84
85 - (void)wxNSSliderPageDownKeyDown: (id)sender
86 {
87     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
88     wxCHECK_RET(slider,wxT("wxNSSliderPageDownKeyDown received without associated wx object"));
89     slider->Cocoa_wxNSSliderPageDownKeyDown();
90 }
91
92 - (void)wxNSSliderMoveUp: (id)sender
93 {
94     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
95     wxCHECK_RET(slider,wxT("wxNSSliderMoveUp received without associated wx object"));
96     slider->Cocoa_wxNSSliderMoveUp();
97 }
98
99 - (void)wxNSSliderMoveDown: (id)sender
100 {
101     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
102     wxCHECK_RET(slider,wxT("wxNSSliderMoveDown received without associated wx object"));
103     slider->Cocoa_wxNSSliderMoveDown();
104 }
105
106 - (void)wxNSSliderMoveLeft: (id)sender
107 {
108     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
109     wxCHECK_RET(slider,wxT("wxNSSliderMoveLeft received without associated wx object"));
110     slider->Cocoa_wxNSSliderMoveLeft();
111 }
112
113 - (void)wxNSSliderMoveRight: (id)sender
114 {
115     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
116     wxCHECK_RET(slider,wxT("wxNSSliderMoveRight received without associated wx object"));
117     slider->Cocoa_wxNSSliderMoveRight();
118 }
119
120 - (void)wxNSSliderPageUp: (id)sender
121 {
122     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
123     wxCHECK_RET(slider,wxT("wxNSSliderPageUp received without associated wx object"));
124     slider->Cocoa_wxNSSliderPageUp();
125 }
126
127 - (void)wxNSSliderPageDown: (id)sender
128 {
129     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
130     wxCHECK_RET(slider,wxT("wxNSSliderPageDown received without associated wx object"));
131     slider->Cocoa_wxNSSliderPageDown();
132 }
133
134 @end // implementation wxNSSliderTarget
135
136 // ============================================================================
137 // @class WXNSSlider
138 // ============================================================================
139
140
141 @implementation WXNSSlider : NSSlider
142
143 // Override to ensure that WXNSSlider gets created with a WXNSSliderCell
144 + (Class)cellClass
145 {
146     return [WX_GET_OBJC_CLASS(WXNSSliderCell) class];
147 }
148
149 - (void)keyDown:(NSEvent *)theEvent
150 {
151     SEL originalAction = [self action];
152     SEL newAction = originalAction;
153     NSString *theEventCharacters = [theEvent charactersIgnoringModifiers];
154
155     if ([theEventCharacters length] == 1)
156     {
157         switch ([theEventCharacters characterAtIndex:0])
158         {
159             case NSUpArrowFunctionKey:      newAction = @selector(wxNSSliderUpArrowKeyDown:);       break;
160             case NSDownArrowFunctionKey:    newAction = @selector(wxNSSliderDownArrowKeyDown:);     break;
161             case NSLeftArrowFunctionKey:    newAction = @selector(wxNSSliderLeftArrowKeyDown:);     break;
162             case NSRightArrowFunctionKey:   newAction = @selector(wxNSSliderRightArrowKeyDown:);    break;
163             case NSPageUpFunctionKey:       newAction = @selector(wxNSSliderPageUpKeyDown:);        break;
164             case NSPageDownFunctionKey:     newAction = @selector(wxNSSliderPageDownKeyDown:);      break;
165             default:                                                                                break;
166         }
167     }
168
169     [self setAction:newAction];
170     [super keyDown:theEvent];
171     [self setAction:originalAction];
172 }
173
174 - (void)moveUp:(id)sender
175 {
176     SEL originalAction = [self action];
177
178     [self setAction:@selector(wxNSSliderMoveUp:)];
179     [super moveUp:sender];
180     [self setAction:originalAction];
181 }
182
183 - (void)moveDown:(id)sender
184 {
185     SEL originalAction = [self action];
186
187     [self setAction:@selector(wxNSSliderMoveDown:)];
188     [super moveDown:sender];
189     [self setAction:originalAction];
190 }
191
192 - (void)moveLeft:(id)sender
193 {
194     SEL originalAction = [self action];
195
196     [self setAction:@selector(wxNSSliderMoveLeft:)];
197     [super moveLeft:sender];
198     [self setAction:originalAction];
199 }
200
201 - (void)moveRight:(id)sender
202 {
203     SEL originalAction = [self action];
204
205     [self setAction:@selector(wxNSSliderMoveRight:)];
206     [super moveRight:sender];
207     [self setAction:originalAction];
208 }
209
210 - (void)pageUp:(id)sender
211 {
212     SEL originalAction = [self action];
213
214     [self setAction:@selector(wxNSSliderPageUp:)];
215     [super pageUp:sender];
216     [self setAction:originalAction];
217 }
218
219 - (void)pageDown:(id)sender
220 {
221     SEL originalAction = [self action];
222
223     [self setAction:@selector(wxNSSliderPageDown:)];
224     [super pageDown:sender];
225     [self setAction:originalAction];
226 }
227
228 @end
229 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSSlider,NSSlider)
230
231 // ============================================================================
232 // @class WXNSSliderCell
233 // ============================================================================
234
235 #define kwxNSSliderStartTracking    @"wxNSSliderStartTracking"
236 #define kwxNSSliderContinueTracking @"wxNSSliderContinueTracking"
237 #define kwxNSSliderStopTracking     @"wxNSSliderStopTracking"
238
239 @implementation WXNSSliderCell : NSSliderCell
240 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
241 {
242     BOOL result = [super startTrackingAt:startPoint inView:controlView];
243
244     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(controlView);
245     if(slider)
246         slider->CocoaNotification_startTracking(NULL);
247
248     return result;
249 }
250
251 - (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint inView:(NSView *)controlView
252 {
253     BOOL result = [super continueTracking:lastPoint at:currentPoint inView:controlView];
254
255     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(controlView);
256     if(slider)
257         slider->CocoaNotification_continueTracking(NULL);
258
259     return result;
260 }
261
262 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag
263 {
264     [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
265
266     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(controlView);
267     if(slider)
268         slider->CocoaNotification_stopTracking(NULL);
269 }
270 @end
271 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSSliderCell,NSSliderCell)
272
273 // ============================================================================
274 // class wxCocoaNSSlider
275 // ============================================================================
276 const wxObjcAutoRefFromAlloc<struct objc_object*> wxCocoaNSSlider::sm_cocoaTarget = [[wxNSSliderTarget alloc] init];
277
278
279 void wxCocoaNSSlider::AssociateNSSlider(WX_NSSlider cocoaNSSlider)
280 {
281     if(cocoaNSSlider)
282     {
283         sm_cocoaHash.insert(wxCocoaNSSliderHash::value_type(cocoaNSSlider,this));
284         [cocoaNSSlider setTarget:sm_cocoaTarget];
285     }
286 }
287
288 void wxCocoaNSSlider::DisassociateNSSlider(WX_NSSlider cocoaNSSlider)
289 {
290     if(cocoaNSSlider)
291     {
292         sm_cocoaHash.erase(cocoaNSSlider);
293     }
294 }