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
) ;
84 UInt16 tickMarks
= 0 ;
85 if ( style
& wxSL_AUTOTICKS
)
86 tickMarks
= (maxValue
- minValue
);
89 tickMarks
= tickMarks
/5; //keep the number of tickmarks from becoming unwieldly
91 m_peer
= new wxMacControl() ;
92 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
93 value
, minValue
, maxValue
, kControlSliderPointsDownOrRight
, tickMarks
, true /* liveTracking */ ,
94 wxMacLiveScrollbarActionUPP
, m_peer
->GetControlRefAddr() ) );
97 if(style
& wxSL_VERTICAL
) {
98 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
101 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
103 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
104 // proper dimensions, it also means other people cannot bugger the slider with
107 if(style
& wxSL_LABELS
)
109 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
110 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
111 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
112 SetRange(minValue
, maxValue
);
116 MacPostControlCreate(pos
,size
) ;
121 wxSlider::~wxSlider()
123 delete m_macMinimumStatic
;
124 delete m_macMaximumStatic
;
125 delete m_macValueStatic
;
128 int wxSlider::GetValue() const
130 return m_peer
->GetValue() ;
133 void wxSlider::SetValue(int value
)
135 wxString valuestring
;
136 valuestring
.Printf( wxT("%d") , value
) ;
137 if ( m_macValueStatic
)
138 m_macValueStatic
->SetLabel( valuestring
) ;
139 m_peer
->SetValue( value
) ;
142 void wxSlider::SetRange(int minValue
, int maxValue
)
146 m_rangeMin
= minValue
;
147 m_rangeMax
= maxValue
;
149 m_peer
->SetMinimum( m_rangeMin
);
150 m_peer
->SetMaximum( m_rangeMax
);
152 if(m_macMinimumStatic
) {
153 value
.Printf(wxT("%d"), m_rangeMin
);
154 m_macMinimumStatic
->SetLabel(value
);
156 if(m_macMaximumStatic
) {
157 value
.Printf(wxT("%d"), m_rangeMax
);
158 m_macMaximumStatic
->SetLabel(value
);
160 SetValue(m_rangeMin
);
163 // For trackbars only
164 void wxSlider::SetTickFreq(int n
, int pos
)
170 void wxSlider::SetPageSize(int pageSize
)
173 m_pageSize
= pageSize
;
176 int wxSlider::GetPageSize() const
181 void wxSlider::ClearSel()
186 void wxSlider::ClearTicks()
191 void wxSlider::SetLineSize(int lineSize
)
193 m_lineSize
= lineSize
;
197 int wxSlider::GetLineSize() const
203 int wxSlider::GetSelEnd() const
209 int wxSlider::GetSelStart() const
215 void wxSlider::SetSelection(int minPos
, int maxPos
)
220 void wxSlider::SetThumbLength(int len
)
225 int wxSlider::GetThumbLength() const
231 void wxSlider::SetTick(int tickPos
)
236 void wxSlider::Command (wxCommandEvent
& event
)
238 SetValue (event
.GetInt());
239 ProcessCommand (event
);
242 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
244 SInt16 value
= m_peer
->GetValue() ;
248 wxEventType scrollEvent
= wxEVT_NULL
;
250 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
252 wxScrollEvent
event(scrollEvent
, m_windowId
);
253 event
.SetPosition(value
);
254 event
.SetEventObject( this );
255 GetEventHandler()->ProcessEvent(event
);
257 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
258 cevent
.SetInt( value
);
259 cevent
.SetEventObject( this );
261 GetEventHandler()->ProcessEvent( cevent
);
264 wxInt32
wxSlider::MacControlHit( WXEVENTHANDLERREF handler
, WXEVENTREF mevent
)
266 SInt16 value
= m_peer
->GetValue() ;
270 wxEventType scrollEvent
= wxEVT_NULL
;
272 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
274 wxScrollEvent
event(scrollEvent
, m_windowId
);
275 event
.SetPosition(value
);
276 event
.SetEventObject( this );
277 GetEventHandler()->ProcessEvent(event
);
279 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
280 cevent
.SetInt( value
);
281 cevent
.SetEventObject( this );
283 GetEventHandler()->ProcessEvent( cevent
);
288 /* This is overloaded in wxSlider so that the proper width/height will always be used
289 * for the slider different values would cause redrawing and mouse detection problems */
290 void wxSlider::SetSizeHints( int minW
, int minH
,
291 int maxW
, int maxH
,
292 int incW
, int incH
)
294 wxSize size
= GetBestSize();
296 if(GetWindowStyle() & wxSL_VERTICAL
) {
297 wxWindow::SetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
300 wxWindow::SetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
304 wxSize
wxSlider::DoGetBestSize() const
307 int textwidth
, textheight
;
309 if(GetWindowStyle() & wxSL_LABELS
)
314 // Get maximum text label width and height
315 text
.Printf(wxT("%d"), m_rangeMin
);
316 GetTextExtent(text
, &textwidth
, &textheight
);
317 text
.Printf(wxT("%d"), m_rangeMax
);
318 GetTextExtent(text
, &wd
, &ht
);
319 if(ht
> textheight
) {
322 if (wd
> textwidth
) {
327 if(GetWindowStyle() & wxSL_VERTICAL
)
329 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
330 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
333 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
335 if(GetWindowStyle() & wxSL_LABELS
) {
336 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
342 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
343 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
346 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
348 if(GetWindowStyle() & wxSL_LABELS
) {
349 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
356 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
358 int xborder
, yborder
;
359 int minValWidth
, maxValWidth
, textwidth
, textheight
;
362 xborder
= yborder
= 0;
364 if (GetWindowStyle() & wxSL_LABELS
)
366 //Labels have this control's parent as their parent
367 //so if this control is not at 0,0 relative to the parent
368 //the labels need to know the position of this control
369 //relative to its parent in order to size properly, so
370 //move the control first so we can use GetPosition()
371 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
376 // Get maximum text label width and height
377 text
.Printf(wxT("%d"), m_rangeMin
);
378 GetTextExtent(text
, &minValWidth
, &textheight
);
379 text
.Printf(wxT("%d"), m_rangeMax
);
380 GetTextExtent(text
, &maxValWidth
, &ht
);
381 if(ht
> textheight
) {
384 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
386 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
387 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
389 // Get slider breadth
390 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
391 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
394 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
397 if(GetWindowStyle() & wxSL_VERTICAL
)
401 if ( m_macMinimumStatic
)
402 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
,
403 GetPosition().y
+ h
- yborder
);
404 if ( m_macMaximumStatic
)
405 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
406 if ( m_macValueStatic
)
407 m_macValueStatic
->Move(GetPosition().x
, GetPosition().y
+ h
);
412 if ( m_macMinimumStatic
)
413 m_macMinimumStatic
->Move(GetPosition().x
+ 0, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
414 if ( m_macMaximumStatic
)
415 m_macMaximumStatic
->Move(GetPosition().x
+ w
- (maxValWidth
/2),
416 GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
417 if ( m_macValueStatic
)
418 m_macValueStatic
->Move(GetPosition().x
+ w
, GetPosition().y
+ 0);
421 //If the control has labels, we still need to call this again because
422 //the labels alter the control's w and h values.
423 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
427 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
429 wxControl::DoMoveWindow(x
,y
,width
,height
) ;
432 #endif // wxUSE_SLIDER