]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/slider.mm
no real changes, just some cleanup
[wxWidgets.git] / src / cocoa / slider.mm
CommitLineData
2ec55dc0 1/////////////////////////////////////////////////////////////////////////////
f4da9a94 2// Name: src/cocoa/slider.mm
2ec55dc0
DE
3// Purpose: wxSlider
4// Author: David Elliott
ddac39da 5// Mark Oxenham
2ec55dc0
DE
6// Modified by:
7// Created: 2003/06/19
8// RCS-ID: $Id$
9// Copyright: (c) 2003 David Elliott
ddac39da 10// (c) 2007 Software 2000 Ltd.
f4da9a94 11// Licence: wxWidgets licence
2ec55dc0
DE
12/////////////////////////////////////////////////////////////////////////////
13
449c5673 14#include "wx/wxprec.h"
f4da9a94 15
ef3e4a2c
DE
16#if wxUSE_SLIDER
17
f4da9a94
WS
18#include "wx/slider.h"
19
449c5673
DE
20#ifndef WX_PRECOMP
21 #include "wx/app.h"
449c5673 22#endif //WX_PRECOMP
2ec55dc0 23
90f6792f 24#include "wx/cocoa/objc/NSSlider.h"
2ec55dc0
DE
25
26IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
f4da9a94 27 BEGIN_EVENT_TABLE(wxSlider, wxSliderBase)
2ec55dc0 28END_EVENT_TABLE()
ddac39da
DE
29WX_IMPLEMENT_COCOA_OWNER(wxSlider,NSSlider,NSControl,NSView)
30
31
32inline 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}
2ec55dc0
DE
49
50bool 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{
ddac39da
DE
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
2ec55dc0
DE
68 if(!CreateControl(parent,winid,pos,size,style,validator,name))
69 return false;
605497ce 70 SetNSSlider([[WX_GET_OBJC_CLASS(WXNSSlider) alloc] initWithFrame: MakeDefaultNSRect(size)]);
2ec55dc0 71 [m_cocoaNSView release];
ddac39da 72
2ec55dc0
DE
73 if(m_parent)
74 m_parent->CocoaAddChild(this);
8d656ea9 75 SetInitialFrameRect(pos,size);
ddac39da
DE
76
77 SetRange(minValue, maxValue);
78 SetValue(value);
8d656ea9 79
2ec55dc0
DE
80 return true;
81}
82
83wxSlider::~wxSlider()
84{
ddac39da
DE
85 DisassociateNSSlider(GetNSSlider());
86}
87
88void 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
95void wxSlider::CocoaNotification_startTracking(WX_NSNotification notification)
96{
97 CocoaNotification_continueTracking(notification);
98}
99
100void 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
112void wxSlider::CocoaNotification_stopTracking(WX_NSNotification notification)
113{
114 ProcessEventType(wxEVT_SCROLL_THUMBRELEASE);
115}
116
117int wxSlider::GetValue() const
118{
119 return [GetNSSlider() intValue];
120}
121
122void wxSlider::SetValue(int value)
123{
124 [GetNSSlider() setIntValue:value];
125}
126
127void wxSlider::SetRange(int minValue, int maxValue)
128{
129 [GetNSSlider() setMinValue:minValue];
130 [GetNSSlider() setMaxValue:maxValue];
131}
132
133int wxSlider::GetMin() const
134{
135 return [GetNSSlider() minValue];
136}
137
138int wxSlider::GetMax() const
139{
140 return [GetNSSlider() maxValue];
141}
142
143void wxSlider::SetTickFreq(int n, int pos)
144{
145 const int numTicks = (n > 0) ? ((GetMax() - GetMin()) / n) + 1 : 0;
146 [GetNSSlider() setNumberOfTickMarks:numTicks];
147}
148
149int wxSlider::GetTickFreq() const
150{
151 const int numTicks = [GetNSSlider() numberOfTickMarks];
152 return ((numTicks != 0) ? (GetMax() - GetMin()) / (numTicks - 1) : 0);
153}
154
155void 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
172void wxSlider::SetLineSize(int lineSize)
173{
174 // to do
175}
176
177void wxSlider::SetPageSize(int pageSize)
178{
179 // to do
180}
181
182int wxSlider::GetLineSize() const
183{
184 return 1;
185}
186
187int wxSlider::GetPageSize() const
188{
189 return 1;
190}
191
192int wxSlider::GetThumbLength() const
193{
194 return 1;
195}
196
197void wxSlider::SetThumbLength(int lenPixels)
198{
199 // to do
2ec55dc0
DE
200}
201
ef3e4a2c 202#endif // wxUSE_SLIDER