]>
Commit | Line | Data |
---|---|---|
2ec55dc0 | 1 | ///////////////////////////////////////////////////////////////////////////// |
f4da9a94 | 2 | // Name: src/cocoa/slider.mm |
2ec55dc0 DE |
3 | // Purpose: wxSlider |
4 | // Author: David Elliott | |
ddac39da | 5 | // Mark Oxenham |
2ec55dc0 DE |
6 | // Modified by: |
7 | // Created: 2003/06/19 | |
2ec55dc0 | 8 | // Copyright: (c) 2003 David Elliott |
ddac39da | 9 | // (c) 2007 Software 2000 Ltd. |
526954c5 | 10 | // Licence: wxWindows licence |
2ec55dc0 DE |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
449c5673 | 13 | #include "wx/wxprec.h" |
f4da9a94 | 14 | |
ef3e4a2c DE |
15 | #if wxUSE_SLIDER |
16 | ||
f4da9a94 WS |
17 | #include "wx/slider.h" |
18 | ||
449c5673 DE |
19 | #ifndef WX_PRECOMP |
20 | #include "wx/app.h" | |
449c5673 | 21 | #endif //WX_PRECOMP |
2ec55dc0 | 22 | |
4f46c20b | 23 | #import <Foundation/NSString.h> |
90f6792f | 24 | #include "wx/cocoa/objc/NSSlider.h" |
4f46c20b DE |
25 | #import <AppKit/NSEvent.h> |
26 | #import <AppKit/NSWindow.h> | |
2ec55dc0 | 27 | |
28953245 | 28 | BEGIN_EVENT_TABLE(wxSlider, wxSliderBase) |
2ec55dc0 | 29 | END_EVENT_TABLE() |
ddac39da DE |
30 | WX_IMPLEMENT_COCOA_OWNER(wxSlider,NSSlider,NSControl,NSView) |
31 | ||
32 | ||
33 | inline void AdjustDimension( | |
34 | bool isTicksStyle, | |
35 | int &pos, | |
36 | wxSize &size, | |
37 | int (wxSize::*GetDimension)() const, | |
38 | void (wxSize::*SetDimension)(int)) | |
39 | { | |
40 | const int dimension = (size.*GetDimension)(); | |
41 | const int minSize = (isTicksStyle) ? 23 : 20; | |
42 | ||
780220b0 | 43 | // prevent clipping of overly "thin" sliders |
ddac39da DE |
44 | if (dimension < minSize) |
45 | { | |
46 | (size.*SetDimension)(minSize); | |
47 | } | |
48 | ||
780220b0 DE |
49 | // move the slider control to the middle of the dimension that is not |
50 | // being used to define its length | |
ddac39da DE |
51 | pos += (dimension - (size.*GetDimension)() + 1) / 2; |
52 | } | |
2ec55dc0 DE |
53 | |
54 | bool wxSlider::Create(wxWindow *parent, wxWindowID winid, | |
55 | int value, int minValue, int maxValue, | |
56 | const wxPoint& pos, const wxSize& size, long style, | |
57 | const wxValidator& validator, const wxString& name) | |
58 | { | |
ddac39da DE |
59 | wxSize adjustedSize(size); |
60 | wxPoint adjustedPos(pos); | |
61 | const bool isTicksStyle = (style & wxSL_TICKS) != 0; | |
62 | ||
63 | if ((style & wxSL_HORIZONTAL) && (size.GetHeight() != wxDefaultCoord)) | |
64 | { | |
65 | AdjustDimension(isTicksStyle, adjustedPos.y, adjustedSize, &wxSize::GetHeight, &wxSize::SetHeight); | |
66 | } | |
67 | else if ((style & wxSL_VERTICAL) && (size.GetWidth() != wxDefaultCoord)) | |
68 | { | |
69 | AdjustDimension(isTicksStyle, adjustedPos.x, adjustedSize, &wxSize::GetWidth, &wxSize::SetWidth); | |
70 | } | |
71 | ||
780220b0 | 72 | if(!CreateControl(parent,winid,adjustedPos,adjustedSize,style,validator,name)) |
2ec55dc0 | 73 | return false; |
780220b0 | 74 | SetNSSlider([[WX_GET_OBJC_CLASS(WXNSSlider) alloc] initWithFrame: MakeDefaultNSRect(adjustedSize)]); |
2ec55dc0 | 75 | [m_cocoaNSView release]; |
ddac39da | 76 | |
2ec55dc0 DE |
77 | if(m_parent) |
78 | m_parent->CocoaAddChild(this); | |
780220b0 | 79 | SetInitialFrameRect(adjustedPos,adjustedSize); |
ddac39da DE |
80 | |
81 | SetRange(minValue, maxValue); | |
82 | SetValue(value); | |
780220b0 DE |
83 | |
84 | // -1 default for wxSL_AUTOTICKS == false | |
85 | int tickMarks = -1; | |
86 | // minValue > maxValue not handled, tickMarks set to 0 | |
87 | if ( style & wxSL_AUTOTICKS ) | |
88 | tickMarks = ((maxValue - minValue >= 0) ? (maxValue - minValue) : 0); | |
0a12e013 | 89 | SetTickFreq(tickMarks); |
8d656ea9 | 90 | |
2ec55dc0 DE |
91 | return true; |
92 | } | |
93 | ||
94 | wxSlider::~wxSlider() | |
95 | { | |
ddac39da DE |
96 | DisassociateNSSlider(GetNSSlider()); |
97 | } | |
98 | ||
4f46c20b DE |
99 | void wxSlider::AssociateNSSlider(WX_NSSlider theSlider) |
100 | { | |
101 | wxCocoaNSSlider::AssociateNSSlider(theSlider); | |
102 | // Set the target/action.. we don't really need to unset these | |
103 | [theSlider setTarget:wxCocoaNSControl::sm_cocoaTarget]; | |
104 | [theSlider setAction:@selector(wxNSControlAction:)]; | |
105 | } | |
106 | ||
ddac39da DE |
107 | void wxSlider::ProcessEventType(wxEventType commandType) |
108 | { | |
109 | wxScrollEvent event(commandType, GetId(), GetValue(), HasFlag(wxSL_VERTICAL)?wxVERTICAL:wxHORIZONTAL); | |
110 | event.SetEventObject(this); | |
937013e0 | 111 | HandleWindowEvent(event); |
ddac39da DE |
112 | } |
113 | ||
4f46c20b DE |
114 | static inline wxEventType wxSliderEventTypeForKeyFromEvent(NSEvent *theEvent) |
115 | { | |
116 | NSString *theEventCharacters = [theEvent charactersIgnoringModifiers]; | |
117 | ||
118 | if ([theEventCharacters length] == 1) | |
119 | { | |
120 | switch ([theEventCharacters characterAtIndex:0]) | |
121 | { | |
122 | case NSUpArrowFunctionKey: | |
123 | case NSRightArrowFunctionKey: return wxEVT_SCROLL_PAGEDOWN; | |
124 | case NSDownArrowFunctionKey: | |
125 | case NSLeftArrowFunctionKey: return wxEVT_SCROLL_PAGEUP; | |
126 | case NSPageUpFunctionKey: return wxEVT_SCROLL_BOTTOM; | |
127 | case NSPageDownFunctionKey: return wxEVT_SCROLL_TOP; | |
128 | } | |
129 | } | |
130 | // Overload wxEVT_ANY to mean we can't determine the event type. | |
131 | return wxEVT_ANY; | |
132 | } | |
133 | ||
134 | void wxSlider::CocoaTarget_action() | |
135 | { | |
136 | wxEventType sliderEventType; | |
137 | SEL theSelector = wxCocoaNSSlider::GetLastResponderSelector(); | |
138 | ||
139 | if( theSelector == @selector(moveUp:) | |
140 | || theSelector == @selector(moveRight:)) | |
141 | sliderEventType = wxEVT_SCROLL_PAGEDOWN; | |
142 | else if( theSelector == @selector(moveDown:) | |
143 | || theSelector == @selector(moveLeft:)) | |
144 | sliderEventType = wxEVT_SCROLL_PAGEUP; | |
145 | else if( theSelector == @selector(pageUp:)) | |
146 | sliderEventType = wxEVT_SCROLL_BOTTOM; | |
147 | else if( theSelector == @selector(pageDown:)) | |
148 | sliderEventType = wxEVT_SCROLL_TOP; | |
149 | else if( theSelector == @selector(keyDown:)) | |
150 | // This case should ideally never be reached. | |
151 | sliderEventType = wxSliderEventTypeForKeyFromEvent([[GetNSSlider() window] currentEvent]); | |
152 | else | |
153 | // Don't generate an event. | |
154 | return; | |
155 | if(sliderEventType != wxEVT_ANY) | |
156 | ProcessEventType(sliderEventType); | |
157 | } | |
158 | ||
ddac39da DE |
159 | void wxSlider::CocoaNotification_startTracking(WX_NSNotification notification) |
160 | { | |
161 | CocoaNotification_continueTracking(notification); | |
162 | } | |
163 | ||
164 | void wxSlider::CocoaNotification_continueTracking(WX_NSNotification notification) | |
165 | { | |
166 | const double realValue = [GetNSSlider() doubleValue]; | |
167 | ||
168 | if (realValue != [GetNSSlider() intValue]) | |
169 | { | |
170 | SetValue(rint(realValue)); | |
171 | } | |
172 | ||
173 | ProcessEventType(wxEVT_SCROLL_THUMBTRACK); | |
174 | } | |
175 | ||
176 | void wxSlider::CocoaNotification_stopTracking(WX_NSNotification notification) | |
177 | { | |
178 | ProcessEventType(wxEVT_SCROLL_THUMBRELEASE); | |
179 | } | |
180 | ||
181 | int wxSlider::GetValue() const | |
182 | { | |
183 | return [GetNSSlider() intValue]; | |
184 | } | |
185 | ||
186 | void wxSlider::SetValue(int value) | |
187 | { | |
188 | [GetNSSlider() setIntValue:value]; | |
189 | } | |
190 | ||
191 | void wxSlider::SetRange(int minValue, int maxValue) | |
192 | { | |
193 | [GetNSSlider() setMinValue:minValue]; | |
194 | [GetNSSlider() setMaxValue:maxValue]; | |
195 | } | |
196 | ||
197 | int wxSlider::GetMin() const | |
198 | { | |
199 | return [GetNSSlider() minValue]; | |
200 | } | |
201 | ||
202 | int wxSlider::GetMax() const | |
203 | { | |
204 | return [GetNSSlider() maxValue]; | |
205 | } | |
206 | ||
0a12e013 | 207 | void wxSlider::DoSetTickFreq(int n) |
ddac39da DE |
208 | { |
209 | const int numTicks = (n > 0) ? ((GetMax() - GetMin()) / n) + 1 : 0; | |
210 | [GetNSSlider() setNumberOfTickMarks:numTicks]; | |
211 | } | |
212 | ||
213 | int wxSlider::GetTickFreq() const | |
214 | { | |
215 | const int numTicks = [GetNSSlider() numberOfTickMarks]; | |
216 | return ((numTicks != 0) ? (GetMax() - GetMin()) / (numTicks - 1) : 0); | |
217 | } | |
218 | ||
219 | void wxSlider::SetTickPos(int pos) | |
220 | { | |
221 | NSTickMarkPosition thePos = NSTickMarkBelow; | |
222 | wxSize size = GetSize(); | |
223 | ||
224 | if (size.GetWidth() < size.GetHeight()) // NSSlider isVertical method can return -1 if it has not been displayed. | |
225 | { | |
226 | thePos = (pos != 1) ? NSTickMarkLeft : NSTickMarkRight; | |
227 | } | |
228 | else | |
229 | { | |
230 | thePos = (pos != 1) ? NSTickMarkBelow : NSTickMarkAbove; | |
231 | } | |
232 | ||
233 | [GetNSSlider() setTickMarkPosition:thePos]; | |
234 | } | |
235 | ||
236 | void wxSlider::SetLineSize(int lineSize) | |
237 | { | |
238 | // to do | |
239 | } | |
240 | ||
241 | void wxSlider::SetPageSize(int pageSize) | |
242 | { | |
243 | // to do | |
244 | } | |
245 | ||
246 | int wxSlider::GetLineSize() const | |
247 | { | |
248 | return 1; | |
249 | } | |
250 | ||
251 | int wxSlider::GetPageSize() const | |
252 | { | |
253 | return 1; | |
254 | } | |
255 | ||
256 | int wxSlider::GetThumbLength() const | |
257 | { | |
258 | return 1; | |
259 | } | |
260 | ||
261 | void wxSlider::SetThumbLength(int lenPixels) | |
262 | { | |
263 | // to do | |
2ec55dc0 DE |
264 | } |
265 | ||
ef3e4a2c | 266 | #endif // wxUSE_SLIDER |