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