Use SetNSSlider instead of SetNSView so that the WXNSSlider (Objective-C) gets associ...
[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 #include "wx/cocoa/objc/NSSlider.h"
25
26 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
27     BEGIN_EVENT_TABLE(wxSlider, wxSliderBase)
28 END_EVENT_TABLE()
29 WX_IMPLEMENT_COCOA_OWNER(wxSlider,NSSlider,NSControl,NSView)
30
31
32 inline void AdjustDimension(
33                 bool            isTicksStyle,
34                 int             &pos,
35                 wxSize          &size,
36                 int             (wxSize::*GetDimension)() const,
37                 void            (wxSize::*SetDimension)(int))
38 {
39     const int dimension = (size.*GetDimension)();
40     const int minSize = (isTicksStyle) ? 23 : 20;
41
42     if (dimension < minSize)
43     {
44         (size.*SetDimension)(minSize);
45     }
46
47     pos += (dimension - (size.*GetDimension)() + 1) / 2;
48 }
49
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)
54 {
55     wxSize adjustedSize(size);
56     wxPoint adjustedPos(pos);
57     const bool isTicksStyle = (style & wxSL_TICKS) != 0;
58
59     if ((style & wxSL_HORIZONTAL) && (size.GetHeight() != wxDefaultCoord))
60     {
61         AdjustDimension(isTicksStyle, adjustedPos.y, adjustedSize, &wxSize::GetHeight, &wxSize::SetHeight);
62     }
63     else if ((style & wxSL_VERTICAL) && (size.GetWidth() != wxDefaultCoord))
64     {
65         AdjustDimension(isTicksStyle, adjustedPos.x, adjustedSize, &wxSize::GetWidth, &wxSize::SetWidth);
66     }
67     
68     if(!CreateControl(parent,winid,pos,size,style,validator,name))
69         return false;
70     SetNSSlider([[WX_GET_OBJC_CLASS(WXNSSlider) alloc] initWithFrame: MakeDefaultNSRect(size)]);
71     [m_cocoaNSView release];
72     
73     if(m_parent)
74         m_parent->CocoaAddChild(this);
75     SetInitialFrameRect(pos,size);
76     
77     SetRange(minValue, maxValue);
78     SetValue(value);
79
80     return true;
81 }
82
83 wxSlider::~wxSlider()
84 {
85     DisassociateNSSlider(GetNSSlider());
86 }
87
88 void wxSlider::ProcessEventType(wxEventType commandType)
89 {
90     wxScrollEvent event(commandType, GetId(), GetValue(), HasFlag(wxSL_VERTICAL)?wxVERTICAL:wxHORIZONTAL);
91     event.SetEventObject(this);
92     GetEventHandler()->ProcessEvent(event);
93 }
94
95 void wxSlider::CocoaNotification_startTracking(WX_NSNotification notification)
96 {
97     CocoaNotification_continueTracking(notification);
98 }
99
100 void wxSlider::CocoaNotification_continueTracking(WX_NSNotification notification)
101 {
102     const double realValue = [GetNSSlider() doubleValue];
103
104     if (realValue != [GetNSSlider() intValue])
105     {
106         SetValue(rint(realValue));
107     }
108
109     ProcessEventType(wxEVT_SCROLL_THUMBTRACK);
110 }
111
112 void wxSlider::CocoaNotification_stopTracking(WX_NSNotification notification)
113 {
114     ProcessEventType(wxEVT_SCROLL_THUMBRELEASE);
115 }
116
117 int wxSlider::GetValue() const
118 {
119     return [GetNSSlider() intValue];
120 }
121
122 void wxSlider::SetValue(int value)
123 {
124     [GetNSSlider() setIntValue:value];
125 }
126
127 void wxSlider::SetRange(int minValue, int maxValue)
128 {
129     [GetNSSlider() setMinValue:minValue];
130     [GetNSSlider() setMaxValue:maxValue];
131 }
132
133 int wxSlider::GetMin() const
134 {
135     return [GetNSSlider() minValue];
136 }
137
138 int wxSlider::GetMax() const
139 {
140     return [GetNSSlider() maxValue];
141 }
142
143 void wxSlider::SetTickFreq(int n, int pos)
144 {
145     const int numTicks = (n > 0) ? ((GetMax() - GetMin()) / n) + 1 : 0;
146     [GetNSSlider() setNumberOfTickMarks:numTicks];
147 }
148
149 int wxSlider::GetTickFreq() const
150 {
151     const int numTicks = [GetNSSlider() numberOfTickMarks];
152     return ((numTicks != 0) ? (GetMax() - GetMin()) / (numTicks - 1) : 0);
153 }
154
155 void wxSlider::SetTickPos(int pos)
156 {
157     NSTickMarkPosition thePos = NSTickMarkBelow;
158     wxSize size = GetSize();
159
160     if (size.GetWidth() < size.GetHeight()) // NSSlider isVertical method can return -1 if it has not been displayed.
161     {
162         thePos = (pos != 1) ? NSTickMarkLeft : NSTickMarkRight;
163     }
164     else
165     {
166         thePos = (pos != 1) ? NSTickMarkBelow : NSTickMarkAbove;
167     }
168
169     [GetNSSlider() setTickMarkPosition:thePos];
170 }
171
172 void wxSlider::SetLineSize(int lineSize)
173 {
174     // to do
175 }
176
177 void wxSlider::SetPageSize(int pageSize)
178 {
179     // to do
180 }
181
182 int wxSlider::GetLineSize() const
183 {
184     return 1;
185 }
186
187 int wxSlider::GetPageSize() const
188 {
189     return 1;
190 }
191
192 int wxSlider::GetThumbLength() const
193 {
194     return 1;
195 }
196
197 void wxSlider::SetThumbLength(int lenPixels)
198 {
199     // to do
200 }
201
202 #endif // wxUSE_SLIDER