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 #include "wx/cocoa/objc/NSSlider.h"
26 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
27 BEGIN_EVENT_TABLE(wxSlider, wxSliderBase)
29 WX_IMPLEMENT_COCOA_OWNER(wxSlider,NSSlider,NSControl,NSView)
32 inline void AdjustDimension(
36 int (wxSize::*GetDimension)() const,
37 void (wxSize::*SetDimension)(int))
39 const int dimension = (size.*GetDimension)();
40 const int minSize = (isTicksStyle) ? 23 : 20;
42 if (dimension < minSize)
44 (size.*SetDimension)(minSize);
47 pos += (dimension - (size.*GetDimension)() + 1) / 2;
50 bool wxSlider::Create(wxWindow *parent, wxWindowID winid,
51 int value, int minValue, int maxValue,
52 const wxPoint& pos, const wxSize& size, long style,
53 const wxValidator& validator, const wxString& name)
55 wxSize adjustedSize(size);
56 wxPoint adjustedPos(pos);
57 const bool isTicksStyle = (style & wxSL_TICKS) != 0;
59 if ((style & wxSL_HORIZONTAL) && (size.GetHeight() != wxDefaultCoord))
61 AdjustDimension(isTicksStyle, adjustedPos.y, adjustedSize, &wxSize::GetHeight, &wxSize::SetHeight);
63 else if ((style & wxSL_VERTICAL) && (size.GetWidth() != wxDefaultCoord))
65 AdjustDimension(isTicksStyle, adjustedPos.x, adjustedSize, &wxSize::GetWidth, &wxSize::SetWidth);
68 if(!CreateControl(parent,winid,pos,size,style,validator,name))
70 SetNSSlider([[WX_GET_OBJC_CLASS(WXNSSlider) alloc] initWithFrame: MakeDefaultNSRect(size)]);
71 [m_cocoaNSView release];
74 m_parent->CocoaAddChild(this);
75 SetInitialFrameRect(pos,size);
77 SetRange(minValue, maxValue);
85 DisassociateNSSlider(GetNSSlider());
88 void wxSlider::ProcessEventType(wxEventType commandType)
90 wxScrollEvent event(commandType, GetId(), GetValue(), HasFlag(wxSL_VERTICAL)?wxVERTICAL:wxHORIZONTAL);
91 event.SetEventObject(this);
92 GetEventHandler()->ProcessEvent(event);
95 void wxSlider::CocoaNotification_startTracking(WX_NSNotification notification)
97 CocoaNotification_continueTracking(notification);
100 void wxSlider::CocoaNotification_continueTracking(WX_NSNotification notification)
102 const double realValue = [GetNSSlider() doubleValue];
104 if (realValue != [GetNSSlider() intValue])
106 SetValue(rint(realValue));
109 ProcessEventType(wxEVT_SCROLL_THUMBTRACK);
112 void wxSlider::CocoaNotification_stopTracking(WX_NSNotification notification)
114 ProcessEventType(wxEVT_SCROLL_THUMBRELEASE);
117 int wxSlider::GetValue() const
119 return [GetNSSlider() intValue];
122 void wxSlider::SetValue(int value)
124 [GetNSSlider() setIntValue:value];
127 void wxSlider::SetRange(int minValue, int maxValue)
129 [GetNSSlider() setMinValue:minValue];
130 [GetNSSlider() setMaxValue:maxValue];
133 int wxSlider::GetMin() const
135 return [GetNSSlider() minValue];
138 int wxSlider::GetMax() const
140 return [GetNSSlider() maxValue];
143 void wxSlider::SetTickFreq(int n, int pos)
145 const int numTicks = (n > 0) ? ((GetMax() - GetMin()) / n) + 1 : 0;
146 [GetNSSlider() setNumberOfTickMarks:numTicks];
149 int wxSlider::GetTickFreq() const
151 const int numTicks = [GetNSSlider() numberOfTickMarks];
152 return ((numTicks != 0) ? (GetMax() - GetMin()) / (numTicks - 1) : 0);
155 void wxSlider::SetTickPos(int pos)
157 NSTickMarkPosition thePos = NSTickMarkBelow;
158 wxSize size = GetSize();
160 if (size.GetWidth() < size.GetHeight()) // NSSlider isVertical method can return -1 if it has not been displayed.
162 thePos = (pos != 1) ? NSTickMarkLeft : NSTickMarkRight;
166 thePos = (pos != 1) ? NSTickMarkBelow : NSTickMarkAbove;
169 [GetNSSlider() setTickMarkPosition:thePos];
172 void wxSlider::SetLineSize(int lineSize)
177 void wxSlider::SetPageSize(int pageSize)
182 int wxSlider::GetLineSize() const
187 int wxSlider::GetPageSize() const
192 int wxSlider::GetThumbLength() const
197 void wxSlider::SetThumbLength(int lenPixels)
202 #endif // wxUSE_SLIDER