1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "slider.h"
20 #include "wx/slider.h"
21 #include "wx/mac/uma.h"
23 #if !USE_SHARED_LIBRARY
24 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
26 BEGIN_EVENT_TABLE(wxSlider
, wxControl
)
30 // The dimensions of the different styles of sliders (From Aqua document)
31 #define wxSLIDER_DIMENSIONACROSS 15
32 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
33 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
35 // Distance between slider and text
36 #define wxSLIDER_BORDERTEXT 5
38 /* NB! The default orientation for a slider is horizontal however if the user specifies
39 * some slider styles but dosen't specify the orientation we have to assume he wants a
40 * horizontal one. Therefore in this file when testing for the sliders orientation
41 * vertical is tested for if this is not set then we use the horizontal one
42 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
55 extern ControlActionUPP wxMacLiveScrollbarActionUPP
;
57 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
58 int value
, int minValue
, int maxValue
,
60 const wxSize
& size
, long style
,
61 const wxValidator
& validator
,
64 m_macIsUserPane
= false ;
66 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
69 m_macMinimumStatic
= NULL
;
70 m_macMaximumStatic
= NULL
;
71 m_macValueStatic
= NULL
;
77 m_rangeMax
= maxValue
;
78 m_rangeMin
= minValue
;
80 m_pageSize
= (int)((maxValue
-minValue
)/10);
82 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
85 // NB: (RN) Ticks here are sometimes off in the GUI if there
86 // is not as many ticks as there are values
88 UInt16 tickMarks
= 0 ;
89 if ( style
& wxSL_AUTOTICKS
)
90 tickMarks
= (maxValue
- minValue
) + 1; //+1 for the 0 value
92 while (tickMarks
> 20)
93 tickMarks
/= 5; //keep the number of tickmarks from becoming unwieldly
95 m_peer
= new wxMacControl() ;
96 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
97 value
, minValue
, maxValue
, kControlSliderPointsDownOrRight
, tickMarks
, true /* liveTracking */ ,
98 wxMacLiveScrollbarActionUPP
, m_peer
->GetControlRefAddr() ) );
101 if(style
& wxSL_VERTICAL
) {
102 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
105 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
107 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
108 // proper dimensions, it also means other people cannot bugger the slider with
111 if(style
& wxSL_LABELS
)
113 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
114 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
115 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
116 SetRange(minValue
, maxValue
);
120 MacPostControlCreate(pos
,size
) ;
125 wxSlider::~wxSlider()
127 // this is a special case, as we had to add windows as siblings we are
128 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
129 if ( m_parent
&& m_parent
->IsBeingDeleted() == false )
131 delete m_macMinimumStatic
;
132 delete m_macMaximumStatic
;
133 delete m_macValueStatic
;
137 int wxSlider::GetValue() const
139 return m_peer
->GetValue() ;
142 void wxSlider::SetValue(int value
)
144 wxString valuestring
;
145 valuestring
.Printf( wxT("%d") , value
) ;
146 if ( m_macValueStatic
)
147 m_macValueStatic
->SetLabel( valuestring
) ;
148 m_peer
->SetValue( value
) ;
151 void wxSlider::SetRange(int minValue
, int maxValue
)
155 m_rangeMin
= minValue
;
156 m_rangeMax
= maxValue
;
158 m_peer
->SetMinimum( m_rangeMin
);
159 m_peer
->SetMaximum( m_rangeMax
);
161 if(m_macMinimumStatic
) {
162 value
.Printf(wxT("%d"), m_rangeMin
);
163 m_macMinimumStatic
->SetLabel(value
);
165 if(m_macMaximumStatic
) {
166 value
.Printf(wxT("%d"), m_rangeMax
);
167 m_macMaximumStatic
->SetLabel(value
);
169 SetValue(m_rangeMin
);
172 // For trackbars only
173 void wxSlider::SetTickFreq(int n
, int pos
)
179 void wxSlider::SetPageSize(int pageSize
)
182 m_pageSize
= pageSize
;
185 int wxSlider::GetPageSize() const
190 void wxSlider::ClearSel()
195 void wxSlider::ClearTicks()
200 void wxSlider::SetLineSize(int lineSize
)
202 m_lineSize
= lineSize
;
206 int wxSlider::GetLineSize() const
212 int wxSlider::GetSelEnd() const
218 int wxSlider::GetSelStart() const
224 void wxSlider::SetSelection(int minPos
, int maxPos
)
229 void wxSlider::SetThumbLength(int len
)
234 int wxSlider::GetThumbLength() const
240 void wxSlider::SetTick(int tickPos
)
245 void wxSlider::Command (wxCommandEvent
& event
)
247 SetValue (event
.GetInt());
248 ProcessCommand (event
);
251 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
253 SInt16 value
= m_peer
->GetValue() ;
257 wxEventType scrollEvent
= wxEVT_NULL
;
259 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
261 wxScrollEvent
event(scrollEvent
, m_windowId
);
262 event
.SetPosition(value
);
263 event
.SetEventObject( this );
264 GetEventHandler()->ProcessEvent(event
);
266 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
267 cevent
.SetInt( value
);
268 cevent
.SetEventObject( this );
270 GetEventHandler()->ProcessEvent( cevent
);
273 wxInt32
wxSlider::MacControlHit( WXEVENTHANDLERREF handler
, WXEVENTREF mevent
)
275 SInt16 value
= m_peer
->GetValue() ;
279 wxEventType scrollEvent
= wxEVT_NULL
;
281 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
283 wxScrollEvent
event(scrollEvent
, m_windowId
);
284 event
.SetPosition(value
);
285 event
.SetEventObject( this );
286 GetEventHandler()->ProcessEvent(event
);
288 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
289 cevent
.SetInt( value
);
290 cevent
.SetEventObject( this );
292 GetEventHandler()->ProcessEvent( cevent
);
297 /* This is overloaded in wxSlider so that the proper width/height will always be used
298 * for the slider different values would cause redrawing and mouse detection problems */
299 void wxSlider::DoSetSizeHints( int minW
, int minH
,
300 int maxW
, int maxH
,
301 int incW
, int incH
)
303 wxSize size
= GetBestSize();
305 if(GetWindowStyle() & wxSL_VERTICAL
) {
306 wxWindow::DoSetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
309 wxWindow::DoSetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
313 wxSize
wxSlider::DoGetBestSize() const
316 int textwidth
, textheight
;
318 if(GetWindowStyle() & wxSL_LABELS
)
323 // Get maximum text label width and height
324 text
.Printf(wxT("%d"), m_rangeMin
);
325 GetTextExtent(text
, &textwidth
, &textheight
);
326 text
.Printf(wxT("%d"), m_rangeMax
);
327 GetTextExtent(text
, &wd
, &ht
);
328 if(ht
> textheight
) {
331 if (wd
> textwidth
) {
336 if(GetWindowStyle() & wxSL_VERTICAL
)
338 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
339 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
342 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
344 if(GetWindowStyle() & wxSL_LABELS
) {
345 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
351 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
352 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
355 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
357 if(GetWindowStyle() & wxSL_LABELS
) {
358 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
365 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
367 int xborder
, yborder
;
368 int minValWidth
, maxValWidth
, textwidth
, textheight
;
371 xborder
= yborder
= 0;
373 if (GetWindowStyle() & wxSL_LABELS
)
375 //Labels have this control's parent as their parent
376 //so if this control is not at 0,0 relative to the parent
377 //the labels need to know the position of this control
378 //relative to its parent in order to size properly, so
379 //move the control first so we can use GetPosition()
380 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
385 // Get maximum text label width and height
386 text
.Printf(wxT("%d"), m_rangeMin
);
387 GetTextExtent(text
, &minValWidth
, &textheight
);
388 text
.Printf(wxT("%d"), m_rangeMax
);
389 GetTextExtent(text
, &maxValWidth
, &ht
);
390 if(ht
> textheight
) {
393 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
395 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
396 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
398 // Get slider breadth
399 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
400 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
403 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
406 if(GetWindowStyle() & wxSL_VERTICAL
)
410 if ( m_macMinimumStatic
)
411 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
,
412 GetPosition().y
+ h
- yborder
);
413 if ( m_macMaximumStatic
)
414 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
415 if ( m_macValueStatic
)
416 m_macValueStatic
->Move(GetPosition().x
, GetPosition().y
+ h
);
421 if ( m_macMinimumStatic
)
422 m_macMinimumStatic
->Move(GetPosition().x
+ 0, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
423 if ( m_macMaximumStatic
)
424 m_macMaximumStatic
->Move(GetPosition().x
+ w
- (maxValWidth
/2),
425 GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
426 if ( m_macValueStatic
)
427 m_macValueStatic
->Move(GetPosition().x
+ w
, GetPosition().y
+ 0);
430 //If the control has labels, we still need to call this again because
431 //the labels alter the control's w and h values.
432 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
436 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
438 wxControl::DoMoveWindow(x
,y
,width
,height
) ;
441 #endif // wxUSE_SLIDER