1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "slider.h"
16 #include "wx/slider.h"
17 #include "wx/mac/uma.h"
19 #if !USE_SHARED_LIBRARY
20 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
22 BEGIN_EVENT_TABLE(wxSlider
, wxControl
)
26 // The dimensions of the different styles of sliders (From Aqua document)
27 #define wxSLIDER_DIMENSIONACROSS 15
28 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
29 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
31 // Distance between slider and text
32 #define wxSLIDER_BORDERTEXT 5
34 /* NB! The default orientation for a slider is horizontal however if the user specifies
35 * some slider styles but dosen't specify the orientation we have to assume he wants a
36 * horizontal one. Therefore in this file when testing for the sliders orientation
37 * vertical is tested for if this is not set then we use the horizontal one
38 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
51 extern ControlActionUPP wxMacLiveScrollbarActionUPP
;
53 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
54 int value
, int minValue
, int maxValue
,
56 const wxSize
& size
, long style
,
57 const wxValidator
& validator
,
60 m_macIsUserPane
= FALSE
;
62 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
65 m_macMinimumStatic
= NULL
;
66 m_macMaximumStatic
= NULL
;
67 m_macValueStatic
= NULL
;
73 m_rangeMax
= maxValue
;
74 m_rangeMin
= minValue
;
76 m_pageSize
= (int)((maxValue
-minValue
)/10);
78 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
80 UInt16 tickMarks
= 0 ;
81 if ( style
& wxSL_AUTOTICKS
)
82 tickMarks
= maxValue
- minValue
;
84 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
85 value
, minValue
, maxValue
, kControlSliderPointsDownOrRight
, tickMarks
, true /* liveTracking */ ,
86 wxMacLiveScrollbarActionUPP
, (ControlRef
*) &m_macControl
) ) ;
88 if(style
& wxSL_VERTICAL
) {
89 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
92 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
94 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
95 // proper dimensions, it also means other people cannot bugger the slider with
98 if(style
& wxSL_LABELS
)
100 m_macMinimumStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
101 m_macMaximumStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
102 m_macValueStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
103 SetRange(minValue
, maxValue
);
107 MacPostControlCreate(pos
,size
) ;
112 wxSlider::~wxSlider()
114 delete m_macMinimumStatic
;
115 delete m_macMaximumStatic
;
116 delete m_macValueStatic
;
119 int wxSlider::GetValue() const
121 return GetControl32BitValue( (ControlRef
) m_macControl
) ;
124 void wxSlider::SetValue(int value
)
126 wxString valuestring
;
127 valuestring
.Printf( wxT("%d") , value
) ;
128 if ( m_macValueStatic
)
129 m_macValueStatic
->SetLabel( valuestring
) ;
130 SetControl32BitValue( (ControlRef
) m_macControl
, value
) ;
133 void wxSlider::SetRange(int minValue
, int maxValue
)
137 m_rangeMin
= minValue
;
138 m_rangeMax
= maxValue
;
140 SetControl32BitMinimum( (ControlRef
) m_macControl
, m_rangeMin
);
141 SetControl32BitMaximum( (ControlRef
) m_macControl
, m_rangeMax
);
143 if(m_macMinimumStatic
) {
144 value
.Printf(wxT("%d"), m_rangeMin
);
145 m_macMinimumStatic
->SetLabel(value
);
147 if(m_macMaximumStatic
) {
148 value
.Printf(wxT("%d"), m_rangeMax
);
149 m_macMaximumStatic
->SetLabel(value
);
151 SetValue(m_rangeMin
);
154 // For trackbars only
155 void wxSlider::SetTickFreq(int n
, int pos
)
161 void wxSlider::SetPageSize(int pageSize
)
164 m_pageSize
= pageSize
;
167 int wxSlider::GetPageSize() const
172 void wxSlider::ClearSel()
177 void wxSlider::ClearTicks()
182 void wxSlider::SetLineSize(int lineSize
)
184 m_lineSize
= lineSize
;
188 int wxSlider::GetLineSize() const
194 int wxSlider::GetSelEnd() const
200 int wxSlider::GetSelStart() const
206 void wxSlider::SetSelection(int minPos
, int maxPos
)
211 void wxSlider::SetThumbLength(int len
)
216 int wxSlider::GetThumbLength() const
222 void wxSlider::SetTick(int tickPos
)
227 void wxSlider::Command (wxCommandEvent
& event
)
229 SetValue (event
.GetInt());
230 ProcessCommand (event
);
233 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
235 SInt16 value
= ::GetControl32BitValue( (ControlRef
) m_macControl
) ;
239 wxEventType scrollEvent
= wxEVT_NULL
;
241 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
243 wxScrollEvent
event(scrollEvent
, m_windowId
);
244 event
.SetPosition(value
);
245 event
.SetEventObject( this );
246 GetEventHandler()->ProcessEvent(event
);
248 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
249 cevent
.SetInt( value
);
250 cevent
.SetEventObject( this );
252 GetEventHandler()->ProcessEvent( cevent
);
255 wxInt32
wxSlider::MacControlHit( WXEVENTHANDLERREF handler
, WXEVENTREF mevent
)
257 SInt16 value
= ::GetControl32BitValue( (ControlRef
) m_macControl
) ;
261 wxEventType scrollEvent
= wxEVT_NULL
;
263 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
265 wxScrollEvent
event(scrollEvent
, m_windowId
);
266 event
.SetPosition(value
);
267 event
.SetEventObject( this );
268 GetEventHandler()->ProcessEvent(event
);
270 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
271 cevent
.SetInt( value
);
272 cevent
.SetEventObject( this );
274 GetEventHandler()->ProcessEvent( cevent
);
279 /* This is overloaded in wxSlider so that the proper width/height will always be used
280 * for the slider different values would cause redrawing and mouse detection problems */
281 void wxSlider::SetSizeHints( int minW
, int minH
,
282 int maxW
, int maxH
,
283 int incW
, int incH
)
285 wxSize size
= GetBestSize();
287 if(GetWindowStyle() & wxSL_VERTICAL
) {
288 wxWindow::SetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
291 wxWindow::SetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
295 wxSize
wxSlider::DoGetBestSize() const
298 int textwidth
, textheight
;
300 if(GetWindowStyle() & wxSL_LABELS
)
305 // Get maximum text label width and height
306 text
.Printf(wxT("%d"), m_rangeMin
);
307 GetTextExtent(text
, &textwidth
, &textheight
);
308 text
.Printf(wxT("%d"), m_rangeMax
);
309 GetTextExtent(text
, &wd
, &ht
);
310 if(ht
> textheight
) {
313 if (wd
> textwidth
) {
318 if(GetWindowStyle() & wxSL_VERTICAL
)
320 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
321 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
324 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
326 if(GetWindowStyle() & wxSL_LABELS
) {
327 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
333 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
334 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
337 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
339 if(GetWindowStyle() & wxSL_LABELS
) {
340 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
347 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
349 int xborder
, yborder
;
350 int minValWidth
, maxValWidth
, textwidth
, textheight
;
353 xborder
= yborder
= 0;
355 if (GetWindowStyle() & wxSL_LABELS
)
360 // Get maximum text label width and height
361 text
.Printf(wxT("%d"), m_rangeMin
);
362 GetTextExtent(text
, &minValWidth
, &textheight
);
363 text
.Printf(wxT("%d"), m_rangeMax
);
364 GetTextExtent(text
, &maxValWidth
, &ht
);
365 if(ht
> textheight
) {
368 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
370 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
371 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
373 // Get slider breadth
374 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
375 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
378 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
381 if(GetWindowStyle() & wxSL_VERTICAL
)
384 if ( m_macMinimumStatic
)
385 m_macMinimumStatic
->Move(x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
,
386 y
+ h
- yborder
- textheight
);
387 if ( m_macMaximumStatic
)
388 m_macMaximumStatic
->Move(x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, y
+ 0);
389 if ( m_macValueStatic
)
390 m_macValueStatic
->Move(0, y
+ h
- textheight
);
395 if ( m_macMinimumStatic
)
396 m_macMinimumStatic
->Move(x
+ 0, y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
397 if ( m_macMaximumStatic
)
398 m_macMaximumStatic
->Move(x
+ w
- xborder
- maxValWidth
/ 2,
399 y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
400 if ( m_macValueStatic
)
401 m_macValueStatic
->Move(x
+ w
- textwidth
, y
+ 0);
406 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
409 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
411 wxControl::DoMoveWindow(x
,y
,width
,height
) ;