1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/NSSlider.mm
3 // Purpose: wxCocoaNSSlider class
4 // Author: Mark Oxenham
5 // Modified by: David Elliott
8 // Copyright: (c) 2007 Software 2000 Ltd. All rights reserved.
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/cocoa/NSSlider.h"
20 #import <Foundation/NSNotification.h>
21 #import <Foundation/NSString.h>
22 #import <AppKit/NSEvent.h>
23 #include "wx/cocoa/objc/NSSlider.h"
25 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSSlider)
27 // ============================================================================
28 // @class wxNSSliderTarget
29 // ============================================================================
30 @interface wxNSSliderTarget : NSObject
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
48 @implementation wxNSSliderTarget : NSObject
50 - (void)wxNSSliderUpArrowKeyDown: (id)sender
52 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
53 wxCHECK_RET(slider,wxT("wxNSSliderUpArrowKeyDown received without associated wx object"));
54 slider->Cocoa_wxNSSliderUpArrowKeyDown();
57 - (void)wxNSSliderDownArrowKeyDown: (id)sender
59 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
60 wxCHECK_RET(slider,wxT("wxNSSliderDownArrowKeyDown received without associated wx object"));
61 slider->Cocoa_wxNSSliderDownArrowKeyDown();
64 - (void)wxNSSliderLeftArrowKeyDown: (id)sender
66 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
67 wxCHECK_RET(slider,wxT("wxNSSliderLeftArrowKeyDown received without associated wx object"));
68 slider->Cocoa_wxNSSliderLeftArrowKeyDown();
71 - (void)wxNSSliderRightArrowKeyDown: (id)sender
73 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
74 wxCHECK_RET(slider,wxT("wxNSSliderRightArrowKeyDown received without associated wx object"));
75 slider->Cocoa_wxNSSliderRightArrowKeyDown();
78 - (void)wxNSSliderPageUpKeyDown: (id)sender
80 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
81 wxCHECK_RET(slider,wxT("wxNSSliderPageUpKeyDown received without associated wx object"));
82 slider->Cocoa_wxNSSliderPageUpKeyDown();
85 - (void)wxNSSliderPageDownKeyDown: (id)sender
87 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
88 wxCHECK_RET(slider,wxT("wxNSSliderPageDownKeyDown received without associated wx object"));
89 slider->Cocoa_wxNSSliderPageDownKeyDown();
92 - (void)wxNSSliderMoveUp: (id)sender
94 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
95 wxCHECK_RET(slider,wxT("wxNSSliderMoveUp received without associated wx object"));
96 slider->Cocoa_wxNSSliderMoveUp();
99 - (void)wxNSSliderMoveDown: (id)sender
101 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
102 wxCHECK_RET(slider,wxT("wxNSSliderMoveDown received without associated wx object"));
103 slider->Cocoa_wxNSSliderMoveDown();
106 - (void)wxNSSliderMoveLeft: (id)sender
108 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
109 wxCHECK_RET(slider,wxT("wxNSSliderMoveLeft received without associated wx object"));
110 slider->Cocoa_wxNSSliderMoveLeft();
113 - (void)wxNSSliderMoveRight: (id)sender
115 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
116 wxCHECK_RET(slider,wxT("wxNSSliderMoveRight received without associated wx object"));
117 slider->Cocoa_wxNSSliderMoveRight();
120 - (void)wxNSSliderPageUp: (id)sender
122 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
123 wxCHECK_RET(slider,wxT("wxNSSliderPageUp received without associated wx object"));
124 slider->Cocoa_wxNSSliderPageUp();
127 - (void)wxNSSliderPageDown: (id)sender
129 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(sender);
130 wxCHECK_RET(slider,wxT("wxNSSliderPageDown received without associated wx object"));
131 slider->Cocoa_wxNSSliderPageDown();
134 @end // implementation wxNSSliderTarget
136 // ============================================================================
138 // ============================================================================
141 @implementation WXNSSlider : NSSlider
143 // Override to ensure that WXNSSlider gets created with a WXNSSliderCell
146 return [WX_GET_OBJC_CLASS(WXNSSliderCell) class];
149 - (void)keyDown:(NSEvent *)theEvent
151 SEL originalAction = [self action];
152 SEL newAction = originalAction;
153 NSString *theEventCharacters = [theEvent charactersIgnoringModifiers];
155 if ([theEventCharacters length] == 1)
157 switch ([theEventCharacters characterAtIndex:0])
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;
169 [self setAction:newAction];
170 [super keyDown:theEvent];
171 [self setAction:originalAction];
174 - (void)moveUp:(id)sender
176 SEL originalAction = [self action];
178 [self setAction:@selector(wxNSSliderMoveUp:)];
179 [super moveUp:sender];
180 [self setAction:originalAction];
183 - (void)moveDown:(id)sender
185 SEL originalAction = [self action];
187 [self setAction:@selector(wxNSSliderMoveDown:)];
188 [super moveDown:sender];
189 [self setAction:originalAction];
192 - (void)moveLeft:(id)sender
194 SEL originalAction = [self action];
196 [self setAction:@selector(wxNSSliderMoveLeft:)];
197 [super moveLeft:sender];
198 [self setAction:originalAction];
201 - (void)moveRight:(id)sender
203 SEL originalAction = [self action];
205 [self setAction:@selector(wxNSSliderMoveRight:)];
206 [super moveRight:sender];
207 [self setAction:originalAction];
210 - (void)pageUp:(id)sender
212 SEL originalAction = [self action];
214 [self setAction:@selector(wxNSSliderPageUp:)];
215 [super pageUp:sender];
216 [self setAction:originalAction];
219 - (void)pageDown:(id)sender
221 SEL originalAction = [self action];
223 [self setAction:@selector(wxNSSliderPageDown:)];
224 [super pageDown:sender];
225 [self setAction:originalAction];
229 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSSlider,NSSlider)
231 // ============================================================================
232 // @class WXNSSliderCell
233 // ============================================================================
235 #define kwxNSSliderStartTracking @"wxNSSliderStartTracking"
236 #define kwxNSSliderContinueTracking @"wxNSSliderContinueTracking"
237 #define kwxNSSliderStopTracking @"wxNSSliderStopTracking"
239 @implementation WXNSSliderCell : NSSliderCell
240 - (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView
242 BOOL result = [super startTrackingAt:startPoint inView:controlView];
244 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(controlView);
246 slider->CocoaNotification_startTracking(NULL);
251 - (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint inView:(NSView *)controlView
253 BOOL result = [super continueTracking:lastPoint at:currentPoint inView:controlView];
255 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(controlView);
257 slider->CocoaNotification_continueTracking(NULL);
262 - (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag
264 [super stopTracking:lastPoint at:stopPoint inView:controlView mouseIsUp:flag];
266 wxCocoaNSSlider *slider = wxCocoaNSSlider::GetFromCocoa(controlView);
268 slider->CocoaNotification_stopTracking(NULL);
271 WX_IMPLEMENT_GET_OBJC_CLASS(WXNSSliderCell,NSSliderCell)
273 // ============================================================================
274 // class wxCocoaNSSlider
275 // ============================================================================
276 const wxObjcAutoRefFromAlloc<struct objc_object*> wxCocoaNSSlider::sm_cocoaTarget = [[wxNSSliderTarget alloc] init];
279 void wxCocoaNSSlider::AssociateNSSlider(WX_NSSlider cocoaNSSlider)
283 sm_cocoaHash.insert(wxCocoaNSSliderHash::value_type(cocoaNSSlider,this));
284 [cocoaNSSlider setTarget:sm_cocoaTarget];
288 void wxCocoaNSSlider::DisassociateNSSlider(WX_NSSlider cocoaNSSlider)
292 sm_cocoaHash.erase(cocoaNSSlider);