1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/slider.mm
4 // Author: David Elliott
9 // Copyright: (c) 2003 David Elliott
10 // (c) 2007 Software 2000 Ltd.
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/wxprec.h"
18 #include "wx/slider.h"
24 #import <Foundation/NSString.h>
25 #include "wx/cocoa/objc/NSSlider.h"
26 #import <AppKit/NSEvent.h>
27 #import <AppKit/NSWindow.h>
29 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
30 BEGIN_EVENT_TABLE(wxSlider, wxSliderBase)
32 WX_IMPLEMENT_COCOA_OWNER(wxSlider,NSSlider,NSControl,NSView)
35 inline void AdjustDimension(
39 int (wxSize::*GetDimension)() const,
40 void (wxSize::*SetDimension)(int))
42 const int dimension = (size.*GetDimension)();
43 const int minSize = (isTicksStyle) ? 23 : 20;
45 // prevent clipping of overly "thin" sliders
46 if (dimension < minSize)
48 (size.*SetDimension)(minSize);
51 // move the slider control to the middle of the dimension that is not
52 // being used to define its length
53 pos += (dimension - (size.*GetDimension)() + 1) / 2;
56 bool wxSlider::Create(wxWindow *parent, wxWindowID winid,
57 int value, int minValue, int maxValue,
58 const wxPoint& pos, const wxSize& size, long style,
59 const wxValidator& validator, const wxString& name)
61 wxSize adjustedSize(size);
62 wxPoint adjustedPos(pos);
63 const bool isTicksStyle = (style & wxSL_TICKS) != 0;
65 if ((style & wxSL_HORIZONTAL) && (size.GetHeight() != wxDefaultCoord))
67 AdjustDimension(isTicksStyle, adjustedPos.y, adjustedSize, &wxSize::GetHeight, &wxSize::SetHeight);
69 else if ((style & wxSL_VERTICAL) && (size.GetWidth() != wxDefaultCoord))
71 AdjustDimension(isTicksStyle, adjustedPos.x, adjustedSize, &wxSize::GetWidth, &wxSize::SetWidth);
74 if(!CreateControl(parent,winid,adjustedPos,adjustedSize,style,validator,name))
76 SetNSSlider([[WX_GET_OBJC_CLASS(WXNSSlider) alloc] initWithFrame: MakeDefaultNSRect(adjustedSize)]);
77 [m_cocoaNSView release];
80 m_parent->CocoaAddChild(this);
81 SetInitialFrameRect(adjustedPos,adjustedSize);
83 SetRange(minValue, maxValue);
86 // -1 default for wxSL_AUTOTICKS == false
88 // minValue > maxValue not handled, tickMarks set to 0
89 if ( style & wxSL_AUTOTICKS )
90 tickMarks = ((maxValue - minValue >= 0) ? (maxValue - minValue) : 0);
91 // arg2 needed a value, doesnt do anything
92 SetTickFreq(tickMarks,1);
99 DisassociateNSSlider(GetNSSlider());
102 void wxSlider::AssociateNSSlider(WX_NSSlider theSlider)
104 wxCocoaNSSlider::AssociateNSSlider(theSlider);
105 // Set the target/action.. we don't really need to unset these
106 [theSlider setTarget:wxCocoaNSControl::sm_cocoaTarget];
107 [theSlider setAction:@selector(wxNSControlAction:)];
110 void wxSlider::ProcessEventType(wxEventType commandType)
112 wxScrollEvent event(commandType, GetId(), GetValue(), HasFlag(wxSL_VERTICAL)?wxVERTICAL:wxHORIZONTAL);
113 event.SetEventObject(this);
114 HandleWindowEvent(event);
117 static inline wxEventType wxSliderEventTypeForKeyFromEvent(NSEvent *theEvent)
119 NSString *theEventCharacters = [theEvent charactersIgnoringModifiers];
121 if ([theEventCharacters length] == 1)
123 switch ([theEventCharacters characterAtIndex:0])
125 case NSUpArrowFunctionKey:
126 case NSRightArrowFunctionKey: return wxEVT_SCROLL_PAGEDOWN;
127 case NSDownArrowFunctionKey:
128 case NSLeftArrowFunctionKey: return wxEVT_SCROLL_PAGEUP;
129 case NSPageUpFunctionKey: return wxEVT_SCROLL_BOTTOM;
130 case NSPageDownFunctionKey: return wxEVT_SCROLL_TOP;
133 // Overload wxEVT_ANY to mean we can't determine the event type.
137 void wxSlider::CocoaTarget_action()
139 wxEventType sliderEventType;
140 SEL theSelector = wxCocoaNSSlider::GetLastResponderSelector();
142 if( theSelector == @selector(moveUp:)
143 || theSelector == @selector(moveRight:))
144 sliderEventType = wxEVT_SCROLL_PAGEDOWN;
145 else if( theSelector == @selector(moveDown:)
146 || theSelector == @selector(moveLeft:))
147 sliderEventType = wxEVT_SCROLL_PAGEUP;
148 else if( theSelector == @selector(pageUp:))
149 sliderEventType = wxEVT_SCROLL_BOTTOM;
150 else if( theSelector == @selector(pageDown:))
151 sliderEventType = wxEVT_SCROLL_TOP;
152 else if( theSelector == @selector(keyDown:))
153 // This case should ideally never be reached.
154 sliderEventType = wxSliderEventTypeForKeyFromEvent([[GetNSSlider() window] currentEvent]);
156 // Don't generate an event.
158 if(sliderEventType != wxEVT_ANY)
159 ProcessEventType(sliderEventType);
162 void wxSlider::CocoaNotification_startTracking(WX_NSNotification notification)
164 CocoaNotification_continueTracking(notification);
167 void wxSlider::CocoaNotification_continueTracking(WX_NSNotification notification)
169 const double realValue = [GetNSSlider() doubleValue];
171 if (realValue != [GetNSSlider() intValue])
173 SetValue(rint(realValue));
176 ProcessEventType(wxEVT_SCROLL_THUMBTRACK);
179 void wxSlider::CocoaNotification_stopTracking(WX_NSNotification notification)
181 ProcessEventType(wxEVT_SCROLL_THUMBRELEASE);
184 int wxSlider::GetValue() const
186 return [GetNSSlider() intValue];
189 void wxSlider::SetValue(int value)
191 [GetNSSlider() setIntValue:value];
194 void wxSlider::SetRange(int minValue, int maxValue)
196 [GetNSSlider() setMinValue:minValue];
197 [GetNSSlider() setMaxValue:maxValue];
200 int wxSlider::GetMin() const
202 return [GetNSSlider() minValue];
205 int wxSlider::GetMax() const
207 return [GetNSSlider() maxValue];
210 void wxSlider::SetTickFreq(int n, int pos)
212 const int numTicks = (n > 0) ? ((GetMax() - GetMin()) / n) + 1 : 0;
213 [GetNSSlider() setNumberOfTickMarks:numTicks];
216 int wxSlider::GetTickFreq() const
218 const int numTicks = [GetNSSlider() numberOfTickMarks];
219 return ((numTicks != 0) ? (GetMax() - GetMin()) / (numTicks - 1) : 0);
222 void wxSlider::SetTickPos(int pos)
224 NSTickMarkPosition thePos = NSTickMarkBelow;
225 wxSize size = GetSize();
227 if (size.GetWidth() < size.GetHeight()) // NSSlider isVertical method can return -1 if it has not been displayed.
229 thePos = (pos != 1) ? NSTickMarkLeft : NSTickMarkRight;
233 thePos = (pos != 1) ? NSTickMarkBelow : NSTickMarkAbove;
236 [GetNSSlider() setTickMarkPosition:thePos];
239 void wxSlider::SetLineSize(int lineSize)
244 void wxSlider::SetPageSize(int pageSize)
249 int wxSlider::GetLineSize() const
254 int wxSlider::GetPageSize() const
259 int wxSlider::GetThumbLength() const
264 void wxSlider::SetThumbLength(int lenPixels)
269 #endif // wxUSE_SLIDER