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: wxWidgets 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 if (dimension < minSize)
47 (size.*SetDimension)(minSize);
50 pos += (dimension - (size.*GetDimension)() + 1) / 2;
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)
58 wxSize adjustedSize(size);
59 wxPoint adjustedPos(pos);
60 const bool isTicksStyle = (style & wxSL_TICKS) != 0;
62 if ((style & wxSL_HORIZONTAL) && (size.GetHeight() != wxDefaultCoord))
64 AdjustDimension(isTicksStyle, adjustedPos.y, adjustedSize, &wxSize::GetHeight, &wxSize::SetHeight);
66 else if ((style & wxSL_VERTICAL) && (size.GetWidth() != wxDefaultCoord))
68 AdjustDimension(isTicksStyle, adjustedPos.x, adjustedSize, &wxSize::GetWidth, &wxSize::SetWidth);
71 if(!CreateControl(parent,winid,pos,size,style,validator,name))
73 SetNSSlider([[WX_GET_OBJC_CLASS(WXNSSlider) alloc] initWithFrame: MakeDefaultNSRect(size)]);
74 [m_cocoaNSView release];
77 m_parent->CocoaAddChild(this);
78 SetInitialFrameRect(pos,size);
80 SetRange(minValue, maxValue);
88 DisassociateNSSlider(GetNSSlider());
91 void wxSlider::AssociateNSSlider(WX_NSSlider theSlider)
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:)];
99 void wxSlider::ProcessEventType(wxEventType commandType)
101 wxScrollEvent event(commandType, GetId(), GetValue(), HasFlag(wxSL_VERTICAL)?wxVERTICAL:wxHORIZONTAL);
102 event.SetEventObject(this);
103 GetEventHandler()->ProcessEvent(event);
106 static inline wxEventType wxSliderEventTypeForKeyFromEvent(NSEvent *theEvent)
108 NSString *theEventCharacters = [theEvent charactersIgnoringModifiers];
110 if ([theEventCharacters length] == 1)
112 switch ([theEventCharacters characterAtIndex:0])
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;
122 // Overload wxEVT_ANY to mean we can't determine the event type.
126 void wxSlider::CocoaTarget_action()
128 wxEventType sliderEventType;
129 SEL theSelector = wxCocoaNSSlider::GetLastResponderSelector();
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]);
145 // Don't generate an event.
147 if(sliderEventType != wxEVT_ANY)
148 ProcessEventType(sliderEventType);
151 void wxSlider::CocoaNotification_startTracking(WX_NSNotification notification)
153 CocoaNotification_continueTracking(notification);
156 void wxSlider::CocoaNotification_continueTracking(WX_NSNotification notification)
158 const double realValue = [GetNSSlider() doubleValue];
160 if (realValue != [GetNSSlider() intValue])
162 SetValue(rint(realValue));
165 ProcessEventType(wxEVT_SCROLL_THUMBTRACK);
168 void wxSlider::CocoaNotification_stopTracking(WX_NSNotification notification)
170 ProcessEventType(wxEVT_SCROLL_THUMBRELEASE);
173 int wxSlider::GetValue() const
175 return [GetNSSlider() intValue];
178 void wxSlider::SetValue(int value)
180 [GetNSSlider() setIntValue:value];
183 void wxSlider::SetRange(int minValue, int maxValue)
185 [GetNSSlider() setMinValue:minValue];
186 [GetNSSlider() setMaxValue:maxValue];
189 int wxSlider::GetMin() const
191 return [GetNSSlider() minValue];
194 int wxSlider::GetMax() const
196 return [GetNSSlider() maxValue];
199 void wxSlider::SetTickFreq(int n, int pos)
201 const int numTicks = (n > 0) ? ((GetMax() - GetMin()) / n) + 1 : 0;
202 [GetNSSlider() setNumberOfTickMarks:numTicks];
205 int wxSlider::GetTickFreq() const
207 const int numTicks = [GetNSSlider() numberOfTickMarks];
208 return ((numTicks != 0) ? (GetMax() - GetMin()) / (numTicks - 1) : 0);
211 void wxSlider::SetTickPos(int pos)
213 NSTickMarkPosition thePos = NSTickMarkBelow;
214 wxSize size = GetSize();
216 if (size.GetWidth() < size.GetHeight()) // NSSlider isVertical method can return -1 if it has not been displayed.
218 thePos = (pos != 1) ? NSTickMarkLeft : NSTickMarkRight;
222 thePos = (pos != 1) ? NSTickMarkBelow : NSTickMarkAbove;
225 [GetNSSlider() setTickMarkPosition:thePos];
228 void wxSlider::SetLineSize(int lineSize)
233 void wxSlider::SetPageSize(int pageSize)
238 int wxSlider::GetLineSize() const
243 int wxSlider::GetPageSize() const
248 int wxSlider::GetThumbLength() const
253 void wxSlider::SetThumbLength(int lenPixels)
258 #endif // wxUSE_SLIDER