]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/slider.mm
make wxChoice and wxListBox inherit from wxControlWithItems and not wxControl for...
[wxWidgets.git] / src / cocoa / slider.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/slider.mm
3 // Purpose: wxSlider
4 // Author: David Elliott
5 // Mark Oxenham
6 // Modified by:
7 // Created: 2003/06/19
8 // RCS-ID: $Id$
9 // Copyright: (c) 2003 David Elliott
10 // (c) 2007 Software 2000 Ltd.
11 // Licence: wxWidgets licence
12 /////////////////////////////////////////////////////////////////////////////
13
14 #include "wx/wxprec.h"
15
16 #if wxUSE_SLIDER
17
18 #include "wx/slider.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/app.h"
22 #endif //WX_PRECOMP
23
24 #import <Foundation/NSString.h>
25 #include "wx/cocoa/objc/NSSlider.h"
26 #import <AppKit/NSEvent.h>
27 #import <AppKit/NSWindow.h>
28
29 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
30 BEGIN_EVENT_TABLE(wxSlider, wxSliderBase)
31 END_EVENT_TABLE()
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 }
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 {
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
71 if(!CreateControl(parent,winid,pos,size,style,validator,name))
72 return false;
73 SetNSSlider([[WX_GET_OBJC_CLASS(WXNSSlider) alloc] initWithFrame: MakeDefaultNSRect(size)]);
74 [m_cocoaNSView release];
75
76 if(m_parent)
77 m_parent->CocoaAddChild(this);
78 SetInitialFrameRect(pos,size);
79
80 SetRange(minValue, maxValue);
81 SetValue(value);
82
83 return true;
84 }
85
86 wxSlider::~wxSlider()
87 {
88 DisassociateNSSlider(GetNSSlider());
89 }
90
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
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
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
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
256 }
257
258 #endif // wxUSE_SLIDER