1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "slider.h"
16 #include "wx/wxprec.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 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
56 int value
, int minValue
, int maxValue
,
58 const wxSize
& size
, long style
,
59 const wxValidator
& validator
,
62 m_macIsUserPane
= false ;
64 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
67 m_macMinimumStatic
= NULL
;
68 m_macMaximumStatic
= NULL
;
69 m_macValueStatic
= NULL
;
74 m_rangeMax
= maxValue
;
75 m_rangeMin
= minValue
;
77 m_pageSize
= (int)((maxValue
-minValue
)/10);
79 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
82 // NB: (RN) Ticks here are sometimes off in the GUI if there
83 // is not as many ticks as there are values
85 UInt16 tickMarks
= 0 ;
86 if ( style
& wxSL_AUTOTICKS
)
87 tickMarks
= (maxValue
- minValue
) + 1; //+1 for the 0 value
89 while (tickMarks
> 20)
90 tickMarks
/= 5; //keep the number of tickmarks from becoming unwieldly
92 m_peer
= new wxMacControl(this) ;
93 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
94 value
, minValue
, maxValue
, kControlSliderPointsDownOrRight
, tickMarks
, true /* liveTracking */ ,
95 GetwxMacLiveScrollbarActionProc() , m_peer
->GetControlRefAddr() ) );
98 if(style
& wxSL_VERTICAL
) {
99 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
102 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
104 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
105 // proper dimensions, it also means other people cannot bugger the slider with
108 if(style
& wxSL_LABELS
)
110 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
111 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
112 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
113 SetRange(minValue
, maxValue
);
117 MacPostControlCreate(pos
,size
) ;
122 wxSlider::~wxSlider()
124 // this is a special case, as we had to add windows as siblings we are
125 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
126 if ( m_parent
&& m_parent
->IsBeingDeleted() == false )
128 delete m_macMinimumStatic
;
129 delete m_macMaximumStatic
;
130 delete m_macValueStatic
;
134 int wxSlider::GetValue() const
136 // We may need to invert the value returned by the widget
137 return ValueInvertOrNot( m_peer
->GetValue() ) ;
140 void wxSlider::SetValue(int value
)
142 wxString valuestring
;
143 valuestring
.Printf( wxT("%d") , value
) ;
144 if ( m_macValueStatic
)
145 m_macValueStatic
->SetLabel( valuestring
) ;
147 // We only invert for the setting of the actual native widget
148 m_peer
->SetValue( ValueInvertOrNot ( 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"), ValueInvertOrNot( m_rangeMin
) );
163 m_macMinimumStatic
->SetLabel(value
);
165 if(m_macMaximumStatic
) {
166 value
.Printf(wxT("%d"), ValueInvertOrNot( 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 // Whatever the native value is, we may need to invert it for calling
254 // SetValue and putting the possibly inverted value in the event
255 SInt16 value
= ValueInvertOrNot ( m_peer
->GetValue() ) ;
259 wxEventType scrollEvent
= wxEVT_NULL
;
261 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
263 wxScrollEvent
event(scrollEvent
, m_windowId
);
264 event
.SetPosition(value
);
265 event
.SetEventObject( this );
266 GetEventHandler()->ProcessEvent(event
);
268 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
269 cevent
.SetInt( value
);
270 cevent
.SetEventObject( this );
272 GetEventHandler()->ProcessEvent( cevent
);
275 wxInt32
wxSlider::MacControlHit( WXEVENTHANDLERREF handler
, WXEVENTREF mevent
)
277 // Whatever the native value is, we may need to invert it for calling
278 // SetValue and putting the possibly inverted value in the event
279 SInt16 value
= ValueInvertOrNot ( m_peer
->GetValue() ) ;
283 wxEventType scrollEvent
= wxEVT_NULL
;
285 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
287 wxScrollEvent
event(scrollEvent
, m_windowId
);
288 event
.SetPosition(value
);
289 event
.SetEventObject( this );
290 GetEventHandler()->ProcessEvent(event
);
292 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
293 cevent
.SetInt( value
);
294 cevent
.SetEventObject( this );
296 GetEventHandler()->ProcessEvent( cevent
);
300 /* This is overloaded in wxSlider so that the proper width/height will always be used
301 * for the slider different values would cause redrawing and mouse detection problems */
302 void wxSlider::DoSetSizeHints( int minW
, int minH
,
303 int maxW
, int maxH
,
304 int incW
, int incH
)
306 wxSize size
= GetBestSize();
308 if(GetWindowStyle() & wxSL_VERTICAL
) {
309 wxWindow::DoSetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
312 wxWindow::DoSetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
316 wxSize
wxSlider::DoGetBestSize() const
319 int textwidth
, textheight
;
321 if(GetWindowStyle() & wxSL_LABELS
)
326 // Get maximum text label width and height
327 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
328 GetTextExtent(text
, &textwidth
, &textheight
);
329 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
330 GetTextExtent(text
, &wd
, &ht
);
331 if(ht
> textheight
) {
334 if (wd
> textwidth
) {
339 if(GetWindowStyle() & wxSL_VERTICAL
)
341 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
342 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
345 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
347 if(GetWindowStyle() & wxSL_LABELS
) {
348 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
354 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
355 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
358 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
360 if(GetWindowStyle() & wxSL_LABELS
) {
361 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
368 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
370 int xborder
, yborder
;
371 int minValWidth
, maxValWidth
, textwidth
, textheight
;
375 xborder
= yborder
= 0;
377 if (GetWindowStyle() & wxSL_LABELS
)
379 //Labels have this control's parent as their parent
380 //so if this control is not at 0,0 relative to the parent
381 //the labels need to know the position of this control
382 //relative to its parent in order to size properly, so
383 //move the control first so we can use GetPosition()
384 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
389 // Get maximum text label width and height
390 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
391 GetTextExtent(text
, &minValWidth
, &textheight
);
392 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
393 GetTextExtent(text
, &maxValWidth
, &ht
);
394 if(ht
> textheight
) {
397 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
399 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
400 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
402 // Get slider breadth
403 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
404 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
407 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
410 if(GetWindowStyle() & wxSL_VERTICAL
)
414 if ( m_macMinimumStatic
)
415 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
,
416 GetPosition().y
+ h
- yborder
);
417 if ( m_macMaximumStatic
)
418 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
419 if ( m_macValueStatic
)
420 m_macValueStatic
->Move(GetPosition().x
, GetPosition().y
+ h
);
425 if ( m_macMinimumStatic
)
426 m_macMinimumStatic
->Move(GetPosition().x
+ 0, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
427 if ( m_macMaximumStatic
)
428 m_macMaximumStatic
->Move(GetPosition().x
+ w
- (maxValWidth
/2),
429 GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
430 if ( m_macValueStatic
)
431 m_macValueStatic
->Move(GetPosition().x
+ w
, GetPosition().y
+ 0);
435 // yet another hack since this is a composite control
436 // when wxSlider has it's size hardcoded, we're not allowed to
437 // change the size. But when the control has labels, we DO need
438 // to resize the internal Mac control to accomodate the text labels.
439 // We need to trick the wxWidgets resize mechanism so that we can
440 // resize the slider part of the control ONLY.
442 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
445 minWidth
= m_minWidth
;
447 if (GetWindowStyle() & wxSL_LABELS
)
449 // make sure we don't allow the entire control to be resized accidently
450 if (width
== GetSize().x
)
453 //If the control has labels, we still need to call this again because
454 //the labels alter the control's w and h values.
455 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
457 m_minWidth
= minWidth
;
460 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
462 wxControl::DoMoveWindow(x
,y
,width
,height
) ;
465 // Common processing to invert slider values based on wxSL_INVERSE
466 int wxSlider::ValueInvertOrNot(int value
) const
468 if (m_windowStyle
& wxSL_VERTICAL
)
470 // The reason for the backwards logic is that Mac's vertical sliders are
471 // inverted compared to Windows and GTK, hence we want inversion to be the
472 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
473 if (m_windowStyle
& wxSL_INVERSE
)
476 return (m_rangeMax
+ m_rangeMin
) - value
;
478 else // normal logic applies to HORIZONTAL sliders
480 return wxSliderBase::ValueInvertOrNot(value
);
484 #endif // wxUSE_SLIDER