Attempt to fix RCS-ID tags so svn will fill them in.
[wxWidgets.git] / src / cocoa / NSSlider.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/NSSlider.mm
3 // Purpose:     wxCocoaNSSlider class
4 // Author:      Mark Oxenham
5 // Modified by:
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/ObjcPose.h"
19 #include "wx/cocoa/NSSlider.h"
20
21 #import <Foundation/NSNotification.h>
22 #import <Foundation/NSString.h>
23 #import <AppKit/NSEvent.h>
24 #import <AppKit/NSSlider.h>
25
26 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSSlider)
27
28 // ============================================================================
29 // @class wxNSSliderTarget
30 // ============================================================================
31 @interface wxNSSliderTarget : NSObject
32 {
33 }
34
35 - (void)wxNSSliderUpArrowKeyDown: (id)sender;
36 - (void)wxNSSliderDownArrowKeyDown: (id)sender;
37 - (void)wxNSSliderLeftArrowKeyDown: (id)sender;
38 - (void)wxNSSliderRightArrowKeyDown: (id)sender;
39 - (void)wxNSSliderPageUpKeyDown: (id)sender;
40 - (void)wxNSSliderPageDownKeyDown: (id)sender;
41 - (void)wxNSSliderMoveUp: (id)sender;
42 - (void)wxNSSliderMoveDown: (id)sender;
43 - (void)wxNSSliderMoveLeft: (id)sender;
44 - (void)wxNSSliderMoveRight: (id)sender;
45 - (void)wxNSSliderPageUp: (id)sender;
46 - (void)wxNSSliderPageDown: (id)sender;
47 @end // wxNSSliderTarget
48
49 @implementation wxNSSliderTarget : NSObject
50
51 - (void)wxNSSliderUpArrowKeyDown: (id)sender
52 {
53     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
54     wxCHECK_RET(slider,wxT("wxNSSliderUpArrowKeyDown received without associated wx object"));
55     slider->Cocoa_wxNSSliderUpArrowKeyDown();
56 }
57
58 - (void)wxNSSliderDownArrowKeyDown: (id)sender
59 {
60     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
61     wxCHECK_RET(slider,wxT("wxNSSliderDownArrowKeyDown received without associated wx object"));
62     slider->Cocoa_wxNSSliderDownArrowKeyDown();
63 }
64
65 - (void)wxNSSliderLeftArrowKeyDown: (id)sender
66 {
67     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
68     wxCHECK_RET(slider,wxT("wxNSSliderLeftArrowKeyDown received without associated wx object"));
69     slider->Cocoa_wxNSSliderLeftArrowKeyDown();
70 }
71
72 - (void)wxNSSliderRightArrowKeyDown: (id)sender
73 {
74     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
75     wxCHECK_RET(slider,wxT("wxNSSliderRightArrowKeyDown received without associated wx object"));
76     slider->Cocoa_wxNSSliderRightArrowKeyDown();
77 }
78
79 - (void)wxNSSliderPageUpKeyDown: (id)sender
80 {
81     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
82     wxCHECK_RET(slider,wxT("wxNSSliderPageUpKeyDown received without associated wx object"));
83     slider->Cocoa_wxNSSliderPageUpKeyDown();
84 }
85
86 - (void)wxNSSliderPageDownKeyDown: (id)sender
87 {
88     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
89     wxCHECK_RET(slider,wxT("wxNSSliderPageDownKeyDown received without associated wx object"));
90     slider->Cocoa_wxNSSliderPageDownKeyDown();
91 }
92
93 - (void)wxNSSliderMoveUp: (id)sender
94 {
95     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
96     wxCHECK_RET(slider,wxT("wxNSSliderMoveUp received without associated wx object"));
97     slider->Cocoa_wxNSSliderMoveUp();
98 }
99
100 - (void)wxNSSliderMoveDown: (id)sender
101 {
102     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
103     wxCHECK_RET(slider,wxT("wxNSSliderMoveDown received without associated wx object"));
104     slider->Cocoa_wxNSSliderMoveDown();
105 }
106
107 - (void)wxNSSliderMoveLeft: (id)sender
108 {
109     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
110     wxCHECK_RET(slider,wxT("wxNSSliderMoveLeft received without associated wx object"));
111     slider->Cocoa_wxNSSliderMoveLeft();
112 }
113
114 - (void)wxNSSliderMoveRight: (id)sender
115 {
116     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
117     wxCHECK_RET(slider,wxT("wxNSSliderMoveRight received without associated wx object"));
118     slider->Cocoa_wxNSSliderMoveRight();
119 }
120
121 - (void)wxNSSliderPageUp: (id)sender
122 {
123     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
124     wxCHECK_RET(slider,wxT("wxNSSliderPageUp received without associated wx object"));
125     slider->Cocoa_wxNSSliderPageUp();
126 }
127
128 - (void)wxNSSliderPageDown: (id)sender
129 {
130     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
131     wxCHECK_RET(slider,wxT("wxNSSliderPageDown received without associated wx object"));
132     slider->Cocoa_wxNSSliderPageDown();
133 }
134
135 @end // implementation wxNSSliderTarget
136
137 // ============================================================================
138 // @class wxPoserNSSlider
139 // ============================================================================
140
141 @interface wxPoserNSSlider : NSSlider
142 @end
143
144 WX_IMPLEMENT_POSER(wxPoserNSSlider);
145 @implementation wxPoserNSSlider : NSSlider
146
147 - (void)keyDown:(NSEvent *)theEvent
148 {
149     SEL originalAction = [self action];
150     SEL newAction = originalAction;
151     NSString *theEventCharacters = [theEvent charactersIgnoringModifiers];
152
153     if ([theEventCharacters length] == 1)
154     {
155         switch ([theEventCharacters characterAtIndex:0])
156         {
157             case NSUpArrowFunctionKey:      newAction = @selector(wxNSSliderUpArrowKeyDown:);       break;
158             case NSDownArrowFunctionKey:    newAction = @selector(wxNSSliderDownArrowKeyDown:);     break;
159             case NSLeftArrowFunctionKey:    newAction = @selector(wxNSSliderLeftArrowKeyDown:);     break;
160             case NSRightArrowFunctionKey:   newAction = @selector(wxNSSliderRightArrowKeyDown:);    break;
161             case NSPageUpFunctionKey:       newAction = @selector(wxNSSliderPageUpKeyDown:);        break;
162             case NSPageDownFunctionKey:     newAction = @selector(wxNSSliderPageDownKeyDown:);      break;
163             default:                                                                                break;
164         }
165     }
166
167     [self setAction:newAction];
168     [super keyDown:theEvent];
169     [self setAction:originalAction];
170 }
171
172 - (void)moveUp:(id)sender
173 {
174     SEL originalAction = [self action];
175
176     [self setAction:@selector(wxNSSliderMoveUp:)];
177     [super moveUp:sender];
178     [self setAction:originalAction];
179 }
180
181 - (void)moveDown:(id)sender
182 {
183     SEL originalAction = [self action];
184
185     [self setAction:@selector(wxNSSliderMoveDown:)];
186     [super moveDown:sender];
187     [self setAction:originalAction];
188 }
189
190 - (void)moveLeft:(id)sender
191 {
192     SEL originalAction = [self action];
193
194     [self setAction:@selector(wxNSSliderMoveLeft:)];
195     [super moveLeft:sender];
196     [self setAction:originalAction];
197 }
198
199 - (void)moveRight:(id)sender
200 {
201     SEL originalAction = [self action];
202
203     [self setAction:@selector(wxNSSliderMoveRight:)];
204     [super moveRight:sender];
205     [self setAction:originalAction];
206 }
207
208 - (void)pageUp:(id)sender
209 {
210     SEL originalAction = [self action];
211
212     [self setAction:@selector(wxNSSliderPageUp:)];
213     [super pageUp:sender];
214     [self setAction:originalAction];
215 }
216
217 - (void)pageDown:(id)sender
218 {
219     SEL originalAction = [self action];
220
221     [self setAction:@selector(wxNSSliderPageDown:)];
222     [super pageDown:sender];
223     [self setAction:originalAction];
224 }
225
226 @end
227
228 // ============================================================================
229 // @class wxPoserNSSliderCell
230 // ============================================================================
231
232 #define kwxNSSliderStartTracking    @"wxNSSliderStartTracking"
233 #define kwxNSSliderContinueTracking @"wxNSSliderContinueTracking"
234 #define kwxNSSliderStopTracking     @"wxNSSliderStopTracking"
235
236 @interface wxPoserNSSliderCell : NSSliderCell
237 @end
238
239 WX_IMPLEMENT_POSER(wxPoserNSSliderCell);
240 @implementation wxPoserNSSliderCell : NSSliderCell
241 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
242 {
243     BOOL result = [super startTrackingAt:startPoint inView:controlView];
244     [[NSNotificationCenter defaultCenter] postNotificationName:kwxNSSliderStartTracking object:controlView];
245     return result;
246 }
247
248 - (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint inView:(NSView *)controlView
249 {
250     BOOL result = [super continueTracking:lastPoint at:currentPoint inView:controlView];
251     [[NSNotificationCenter defaultCenter] postNotificationName:kwxNSSliderContinueTracking object:controlView];
252     return result;
253 }
254
255 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag
256 {
257     [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
258     [[NSNotificationCenter defaultCenter] postNotificationName:kwxNSSliderStopTracking object:controlView];
259 }
260 @end
261
262 // ============================================================================
263 // @class wxNSSliderNotificationObserver
264 // ============================================================================
265 @interface wxNSSliderNotificationObserver : NSObject
266 {
267 }
268
269 struct objc_object *wxCocoaNSSlider::sm_cocoaObserver = [[wxNSSliderNotificationObserver alloc] init];
270
271 - (void)startTracking: (NSNotification *)notification;
272 - (void)continueTracking: (NSNotification *)notification;
273 - (void)stopTracking: (NSNotification *)notification;
274 @end // interface wxNSSliderNotificationObserver
275
276 @implementation wxNSSliderNotificationObserver : NSObject
277
278 - (void)startTracking: (NSNotification *)notification;
279 {
280     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa([notification object]);
281     wxCHECK_RET(slider,wxT("startTracking received but no wxSlider exists"));
282     slider->CocoaNotification_startTracking(notification);
283 }
284
285 - (void)continueTracking: (NSNotification *)notification;
286 {
287     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa([notification object]);
288     wxCHECK_RET(slider,wxT("continueTracking received but no wxSlider exists"));
289     slider->CocoaNotification_continueTracking(notification);
290 }
291
292 - (void)stopTracking: (NSNotification *)notification;
293 {
294     wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa([notification object]);
295     wxCHECK_RET(slider,wxT("stopTracking received but no wxSlider exists"));
296     slider->CocoaNotification_stopTracking(notification);
297 }
298
299 @end // implementation wxNSSliderNotificationObserver
300
301 // ============================================================================
302 // class wxCocoaNSSlider
303 // ============================================================================
304 const wxObjcAutoRefFromAlloc<struct objc_object*> wxCocoaNSSlider::sm_cocoaTarget = [[wxNSSliderTarget alloc] init];
305
306
307 void wxCocoaNSSlider::AssociateNSSlider(WX_NSSlider cocoaNSSlider)
308 {
309     if(cocoaNSSlider)
310     {
311         sm_cocoaHash.insert(wxCocoaNSSliderHash::value_type(cocoaNSSlider,this));
312         [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(startTracking:) name:kwxNSSliderStartTracking object:cocoaNSSlider];
313         [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(continueTracking:) name:kwxNSSliderContinueTracking object:cocoaNSSlider];
314         [[NSNotificationCenter defaultCenter] addObserver:(id)sm_cocoaObserver selector:@selector(stopTracking:) name:kwxNSSliderStopTracking object:cocoaNSSlider];
315         [cocoaNSSlider setTarget:sm_cocoaTarget];
316     }
317 }
318
319 void wxCocoaNSSlider::DisassociateNSSlider(WX_NSSlider cocoaNSSlider)
320 {
321     if(cocoaNSSlider)
322     {
323         sm_cocoaHash.erase(cocoaNSSlider);
324         [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:kwxNSSliderStartTracking object:cocoaNSSlider];
325         [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:kwxNSSliderContinueTracking object:cocoaNSSlider];
326         [[NSNotificationCenter defaultCenter] removeObserver:(id)sm_cocoaObserver name:kwxNSSliderStopTracking object:cocoaNSSlider];
327     }
328 }