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
);
85 tickMarks
= tickMarks
/5; //keep the number of tickmarks from becoming unwieldly
87 m_peer
= new wxMacControl() ;
88 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
89 value
, minValue
, maxValue
, kControlSliderPointsDownOrRight
, tickMarks
, true /* liveTracking */ ,
90 wxMacLiveScrollbarActionUPP
, m_peer
->GetControlRefAddr() ) );
93 if(style
& wxSL_VERTICAL
) {
94 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
97 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
99 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
100 // proper dimensions, it also means other people cannot bugger the slider with
103 if(style
& wxSL_LABELS
)
105 m_macMinimumStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
106 m_macMaximumStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
107 m_macValueStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
108 SetRange(minValue
, maxValue
);
112 MacPostControlCreate(pos
,size
) ;
117 wxSlider::~wxSlider()
119 delete m_macMinimumStatic
;
120 delete m_macMaximumStatic
;
121 delete m_macValueStatic
;
124 int wxSlider::GetValue() const
126 return m_peer
->GetValue() ;
129 void wxSlider::SetValue(int value
)
131 wxString valuestring
;
132 valuestring
.Printf( wxT("%d") , value
) ;
133 if ( m_macValueStatic
)
134 m_macValueStatic
->SetLabel( valuestring
) ;
135 m_peer
->SetValue( value
) ;
138 void wxSlider::SetRange(int minValue
, int maxValue
)
142 m_rangeMin
= minValue
;
143 m_rangeMax
= maxValue
;
145 m_peer
->SetMinimum( m_rangeMin
);
146 m_peer
->SetMaximum( m_rangeMax
);
148 if(m_macMinimumStatic
) {
149 value
.Printf(wxT("%d"), m_rangeMin
);
150 m_macMinimumStatic
->SetLabel(value
);
152 if(m_macMaximumStatic
) {
153 value
.Printf(wxT("%d"), m_rangeMax
);
154 m_macMaximumStatic
->SetLabel(value
);
156 SetValue(m_rangeMin
);
159 // For trackbars only
160 void wxSlider::SetTickFreq(int n
, int pos
)
166 void wxSlider::SetPageSize(int pageSize
)
169 m_pageSize
= pageSize
;
172 int wxSlider::GetPageSize() const
177 void wxSlider::ClearSel()
182 void wxSlider::ClearTicks()
187 void wxSlider::SetLineSize(int lineSize
)
189 m_lineSize
= lineSize
;
193 int wxSlider::GetLineSize() const
199 int wxSlider::GetSelEnd() const
205 int wxSlider::GetSelStart() const
211 void wxSlider::SetSelection(int minPos
, int maxPos
)
216 void wxSlider::SetThumbLength(int len
)
221 int wxSlider::GetThumbLength() const
227 void wxSlider::SetTick(int tickPos
)
232 void wxSlider::Command (wxCommandEvent
& event
)
234 SetValue (event
.GetInt());
235 ProcessCommand (event
);
238 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
240 SInt16 value
= m_peer
->GetValue() ;
244 wxEventType scrollEvent
= wxEVT_NULL
;
246 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
248 wxScrollEvent
event(scrollEvent
, m_windowId
);
249 event
.SetPosition(value
);
250 event
.SetEventObject( this );
251 GetEventHandler()->ProcessEvent(event
);
253 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
254 cevent
.SetInt( value
);
255 cevent
.SetEventObject( this );
257 GetEventHandler()->ProcessEvent( cevent
);
260 wxInt32
wxSlider::MacControlHit( WXEVENTHANDLERREF handler
, WXEVENTREF mevent
)
262 SInt16 value
= m_peer
->GetValue() ;
266 wxEventType scrollEvent
= wxEVT_NULL
;
268 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
270 wxScrollEvent
event(scrollEvent
, m_windowId
);
271 event
.SetPosition(value
);
272 event
.SetEventObject( this );
273 GetEventHandler()->ProcessEvent(event
);
275 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
276 cevent
.SetInt( value
);
277 cevent
.SetEventObject( this );
279 GetEventHandler()->ProcessEvent( cevent
);
284 /* This is overloaded in wxSlider so that the proper width/height will always be used
285 * for the slider different values would cause redrawing and mouse detection problems */
286 void wxSlider::SetSizeHints( int minW
, int minH
,
287 int maxW
, int maxH
,
288 int incW
, int incH
)
290 wxSize size
= GetBestSize();
292 if(GetWindowStyle() & wxSL_VERTICAL
) {
293 wxWindow::SetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
296 wxWindow::SetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
300 wxSize
wxSlider::DoGetBestSize() const
303 int textwidth
, textheight
;
305 if(GetWindowStyle() & wxSL_LABELS
)
310 // Get maximum text label width and height
311 text
.Printf(wxT("%d"), m_rangeMin
);
312 GetTextExtent(text
, &textwidth
, &textheight
);
313 text
.Printf(wxT("%d"), m_rangeMax
);
314 GetTextExtent(text
, &wd
, &ht
);
315 if(ht
> textheight
) {
318 if (wd
> textwidth
) {
323 if(GetWindowStyle() & wxSL_VERTICAL
)
325 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
326 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
329 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
331 if(GetWindowStyle() & wxSL_LABELS
) {
332 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
338 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
339 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
342 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
344 if(GetWindowStyle() & wxSL_LABELS
) {
345 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
352 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
354 int xborder
, yborder
;
355 int minValWidth
, maxValWidth
, textwidth
, textheight
;
358 xborder
= yborder
= 0;
360 if (GetWindowStyle() & wxSL_LABELS
)
362 //Labels have this control's parent as their parent
363 //so if this control is not at 0,0 relative to the parent
364 //the labels need to know the position of this control
365 //relative to its parent in order to size properly, so
366 //move the control first so we can use GetPosition()
367 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
372 // Get maximum text label width and height
373 text
.Printf(wxT("%d"), m_rangeMin
);
374 GetTextExtent(text
, &minValWidth
, &textheight
);
375 text
.Printf(wxT("%d"), m_rangeMax
);
376 GetTextExtent(text
, &maxValWidth
, &ht
);
377 if(ht
> textheight
) {
380 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
382 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
383 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
385 // Get slider breadth
386 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
387 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
390 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
393 if(GetWindowStyle() & wxSL_VERTICAL
)
397 if ( m_macMinimumStatic
)
398 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
,
399 GetPosition().y
+ h
- yborder
);
400 if ( m_macMaximumStatic
)
401 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
402 if ( m_macValueStatic
)
403 m_macValueStatic
->Move(GetPosition().x
, GetPosition().y
+ h
);
408 if ( m_macMinimumStatic
)
409 m_macMinimumStatic
->Move(GetPosition().x
+ 0, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
410 if ( m_macMaximumStatic
)
411 m_macMaximumStatic
->Move(GetPosition().x
+ w
- (maxValWidth
/2),
412 GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
413 if ( m_macValueStatic
)
414 m_macValueStatic
->Move(GetPosition().x
+ w
, GetPosition().y
+ 0);
417 //If the control has labels, we still need to call this again because
418 //the labels alter the control's w and h values.
419 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
423 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
425 wxControl::DoMoveWindow(x
,y
,width
,height
) ;