1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/slider.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // (c) 2007 Software 2000 Ltd.
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/wxprec.h"
17 #include "wx/slider.h"
23 #import <Foundation/NSString.h>
24 #include "wx/cocoa/objc/NSSlider.h"
25 #import <AppKit/NSEvent.h>
26 #import <AppKit/NSWindow.h>
28 BEGIN_EVENT_TABLE(wxSlider, wxSliderBase)
30 WX_IMPLEMENT_COCOA_OWNER(wxSlider,NSSlider,NSControl,NSView)
33 inline void AdjustDimension(
37 int (wxSize::*GetDimension)() const,
38 void (wxSize::*SetDimension)(int))
40 const int dimension = (size.*GetDimension)();
41 const int minSize = (isTicksStyle) ? 23 : 20;
43 // prevent clipping of overly "thin" sliders
44 if (dimension < minSize)
46 (size.*SetDimension)(minSize);
49 // move the slider control to the middle of the dimension that is not
50 // being used to define its length
51 pos += (dimension - (size.*GetDimension)() + 1) / 2;
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)
59 wxSize adjustedSize(size);
60 wxPoint adjustedPos(pos);
61 const bool isTicksStyle = (style & wxSL_TICKS) != 0;
63 if ((style & wxSL_HORIZONTAL) && (size.GetHeight() != wxDefaultCoord))
65 AdjustDimension(isTicksStyle, adjustedPos.y, adjustedSize, &wxSize::GetHeight, &wxSize::SetHeight);
67 else if ((style & wxSL_VERTICAL) && (size.GetWidth() != wxDefaultCoord))
69 AdjustDimension(isTicksStyle, adjustedPos.x, adjustedSize, &wxSize::GetWidth, &wxSize::SetWidth);
72 if(!CreateControl(parent,winid,adjustedPos,adjustedSize,style,validator,name))
74 SetNSSlider([[WX_GET_OBJC_CLASS(WXNSSlider) alloc] initWithFrame: MakeDefaultNSRect(adjustedSize)]);
75 [m_cocoaNSView release];
78 m_parent->CocoaAddChild(this);
79 SetInitialFrameRect(adjustedPos,adjustedSize);
81 SetRange(minValue, maxValue);
84 // -1 default for wxSL_AUTOTICKS == false
86 // minValue > maxValue not handled, tickMarks set to 0
87 if ( style & wxSL_AUTOTICKS )
88 tickMarks = ((maxValue - minValue >= 0) ? (maxValue - minValue) : 0);
89 SetTickFreq(tickMarks);
96 DisassociateNSSlider(GetNSSlider());
99 void wxSlider::AssociateNSSlider(WX_NSSlider theSlider)
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:)];
107 void wxSlider::ProcessEventType(wxEventType commandType)
109 wxScrollEvent event(commandType, GetId(), GetValue(), HasFlag(wxSL_VERTICAL)?wxVERTICAL:wxHORIZONTAL);
110 event.SetEventObject(this);
111 HandleWindowEvent(event);
114 static inline wxEventType wxSliderEventTypeForKeyFromEvent(NSEvent *theEvent)
116 NSString *theEventCharacters = [theEvent charactersIgnoringModifiers];
118 if ([theEventCharacters length] == 1)
120 switch ([theEventCharacters characterAtIndex:0])
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;
130 // Overload wxEVT_ANY to mean we can't determine the event type.
134 void wxSlider::CocoaTarget_action()
136 wxEventType sliderEventType;
137 SEL theSelector = wxCocoaNSSlider::GetLastResponderSelector();
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]);
153 // Don't generate an event.
155 if(sliderEventType != wxEVT_ANY)
156 ProcessEventType(sliderEventType);
159 void wxSlider::CocoaNotification_startTracking(WX_NSNotification notification)
161 CocoaNotification_continueTracking(notification);
164 void wxSlider::CocoaNotification_continueTracking(WX_NSNotification notification)
166 const double realValue = [GetNSSlider() doubleValue];
168 if (realValue != [GetNSSlider() intValue])
170 SetValue(rint(realValue));
173 ProcessEventType(wxEVT_SCROLL_THUMBTRACK);
176 void wxSlider::CocoaNotification_stopTracking(WX_NSNotification notification)
178 ProcessEventType(wxEVT_SCROLL_THUMBRELEASE);
181 int wxSlider::GetValue() const
183 return [GetNSSlider() intValue];
186 void wxSlider::SetValue(int value)
188 [GetNSSlider() setIntValue:value];
191 void wxSlider::SetRange(int minValue, int maxValue)
193 [GetNSSlider() setMinValue:minValue];
194 [GetNSSlider() setMaxValue:maxValue];
197 int wxSlider::GetMin() const
199 return [GetNSSlider() minValue];
202 int wxSlider::GetMax() const
204 return [GetNSSlider() maxValue];
207 void wxSlider::DoSetTickFreq(int n)
209 const int numTicks = (n > 0) ? ((GetMax() - GetMin()) / n) + 1 : 0;
210 [GetNSSlider() setNumberOfTickMarks:numTicks];
213 int wxSlider::GetTickFreq() const
215 const int numTicks = [GetNSSlider() numberOfTickMarks];
216 return ((numTicks != 0) ? (GetMax() - GetMin()) / (numTicks - 1) : 0);
219 void wxSlider::SetTickPos(int pos)
221 NSTickMarkPosition thePos = NSTickMarkBelow;
222 wxSize size = GetSize();
224 if (size.GetWidth() < size.GetHeight()) // NSSlider isVertical method can return -1 if it has not been displayed.
226 thePos = (pos != 1) ? NSTickMarkLeft : NSTickMarkRight;
230 thePos = (pos != 1) ? NSTickMarkBelow : NSTickMarkAbove;
233 [GetNSSlider() setTickMarkPosition:thePos];
236 void wxSlider::SetLineSize(int lineSize)
241 void wxSlider::SetPageSize(int pageSize)
246 int wxSlider::GetLineSize() const
251 int wxSlider::GetPageSize() const
256 int wxSlider::GetThumbLength() const
261 void wxSlider::SetThumbLength(int lenPixels)
266 #endif // wxUSE_SLIDER