1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/slider.h"
17 #include "wx/mac/uma.h"
19 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
21 BEGIN_EVENT_TABLE(wxSlider
, wxControl
)
24 // The dimensions of the different styles of sliders (from Aqua document)
25 #define wxSLIDER_DIMENSIONACROSS 15
26 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
27 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
29 // Distance between slider and text
30 #define wxSLIDER_BORDERTEXT 5
33 * NB! The default orientation for a slider is horizontal however if the user specifies
34 * some slider styles but doesn't specify the orientation we have to assume he wants a
35 * horizontal one. Therefore in this file when testing for the slider's orientation
36 * vertical is tested for if this is not set then we use the horizontal one
37 * e.g., if (GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }.
49 m_macMinimumStatic
= NULL
;
50 m_macMaximumStatic
= NULL
;
51 m_macValueStatic
= NULL
;
54 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
55 int value
, int minValue
, int maxValue
,
57 const wxSize
& size
, long style
,
58 const wxValidator
& validator
,
61 m_macIsUserPane
= false ;
63 m_macMinimumStatic
= NULL
;
64 m_macMaximumStatic
= NULL
;
65 m_macValueStatic
= NULL
;
70 m_rangeMax
= maxValue
;
71 m_rangeMin
= minValue
;
73 m_pageSize
= (int)((maxValue
-minValue
)/10);
75 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
76 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
77 // reasons we can't really change it, instead try to infer the orientation
78 // from the flags given to us here
79 switch ( style
& (wxSL_LEFT
| wxSL_RIGHT
| wxSL_TOP
| wxSL_BOTTOM
) )
83 style
|= wxSL_VERTICAL
;
88 style
|= wxSL_HORIZONTAL
;
93 // no specific direction, do we have at least the orientation?
94 if ( !(style
& (wxSL_HORIZONTAL
| wxSL_VERTICAL
)) )
96 style
|= wxSL_BOTTOM
| wxSL_HORIZONTAL
;
100 wxASSERT_MSG( !(style
& wxSL_VERTICAL
) || !(style
& wxSL_HORIZONTAL
),
101 _T("incompatible slider direction and orientation") );
103 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
106 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
108 // NB: (RN) Ticks here are sometimes off in the GUI if there
109 // is not as many ticks as there are values
112 if ( style
& wxSL_AUTOTICKS
)
113 tickMarks
= (maxValue
- minValue
) + 1; //+1 for the 0 value
115 // keep the number of tickmarks from becoming unwieldly, therefore below it is ok to cast
117 while (tickMarks
> 20)
120 m_peer
= new wxMacControl( this );
121 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
122 value
, minValue
, maxValue
, kControlSliderPointsDownOrRight
, (UInt16
) tickMarks
, true /* liveTracking */ ,
123 GetwxMacLiveScrollbarActionProc() , m_peer
->GetControlRefAddr() ) );
125 if (style
& wxSL_VERTICAL
)
126 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
128 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
130 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
131 // proper dimensions, it also means other people cannot bugger the slider with
134 if (style
& wxSL_LABELS
)
136 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
137 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
138 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
141 SetRange(minValue
, maxValue
);
144 MacPostControlCreate(pos
,size
) ;
149 wxSlider::~wxSlider()
151 // this is a special case, as we had to add windows as siblings we are
152 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
153 if ( m_parent
&& !m_parent
->IsBeingDeleted() )
155 delete m_macMinimumStatic
;
156 delete m_macMaximumStatic
;
157 delete m_macValueStatic
;
161 int wxSlider::GetValue() const
163 // We may need to invert the value returned by the widget
164 return ValueInvertOrNot( m_peer
->GetValue() ) ;
167 void wxSlider::SetValue(int value
)
169 if ( m_macValueStatic
)
171 wxString valuestring
;
172 valuestring
.Printf( wxT("%d") , value
);
173 m_macValueStatic
->SetLabel( valuestring
);
176 // We only invert for the setting of the actual native widget
177 m_peer
->SetValue( ValueInvertOrNot ( value
) ) ;
180 void wxSlider::SetRange(int minValue
, int maxValue
)
184 m_rangeMin
= minValue
;
185 m_rangeMax
= maxValue
;
187 m_peer
->SetMinimum( m_rangeMin
);
188 m_peer
->SetMaximum( m_rangeMax
);
190 if (m_macMinimumStatic
)
192 value
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
193 m_macMinimumStatic
->SetLabel(value
);
196 if (m_macMaximumStatic
)
198 value
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
199 m_macMaximumStatic
->SetLabel(value
);
202 SetValue(m_rangeMin
);
205 // For trackbars only
206 void wxSlider::SetTickFreq(int n
, int pos
)
212 void wxSlider::SetPageSize(int pageSize
)
215 m_pageSize
= pageSize
;
218 int wxSlider::GetPageSize() const
223 void wxSlider::ClearSel()
228 void wxSlider::ClearTicks()
233 void wxSlider::SetLineSize(int lineSize
)
235 m_lineSize
= lineSize
;
239 int wxSlider::GetLineSize() const
245 int wxSlider::GetSelEnd() const
251 int wxSlider::GetSelStart() const
257 void wxSlider::SetSelection(int minPos
, int maxPos
)
262 void wxSlider::SetThumbLength(int len
)
267 int wxSlider::GetThumbLength() const
273 void wxSlider::SetTick(int tickPos
)
278 void wxSlider::Command (wxCommandEvent
& event
)
280 SetValue(event
.GetInt());
281 ProcessCommand(event
);
284 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
286 // Whatever the native value is, we may need to invert it for calling
287 // SetValue and putting the possibly inverted value in the event
288 int value
= ValueInvertOrNot ( m_peer
->GetValue() ) ;
292 wxEventType scrollEvent
= wxEVT_NULL
;
294 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
296 wxScrollEvent
event(scrollEvent
, m_windowId
);
297 event
.SetPosition(value
);
298 event
.SetEventObject( this );
299 GetEventHandler()->ProcessEvent(event
);
301 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
302 cevent
.SetInt( value
);
303 cevent
.SetEventObject( this );
305 GetEventHandler()->ProcessEvent( cevent
);
308 wxInt32
wxSlider::MacControlHit( WXEVENTHANDLERREF handler
, WXEVENTREF mevent
)
310 // Whatever the native value is, we may need to invert it for calling
311 // SetValue and putting the possibly inverted value in the event
312 int value
= ValueInvertOrNot ( m_peer
->GetValue() ) ;
316 wxEventType scrollEvent
= wxEVT_NULL
;
318 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
320 wxScrollEvent
event(scrollEvent
, m_windowId
);
321 event
.SetPosition(value
);
322 event
.SetEventObject( this );
323 GetEventHandler()->ProcessEvent(event
);
325 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
326 cevent
.SetInt( value
);
327 cevent
.SetEventObject( this );
329 GetEventHandler()->ProcessEvent( cevent
);
334 // This is overloaded in wxSlider so that the proper width/height will always be used
335 // for the slider different values would cause redrawing and mouse detection problems
337 void wxSlider::DoSetSizeHints( int minW
, int minH
,
338 int maxW
, int maxH
,
339 int incW
, int incH
)
341 wxSize size
= GetBestSize();
343 if (GetWindowStyle() & wxSL_VERTICAL
)
344 wxWindow::DoSetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
346 wxWindow::DoSetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
349 wxSize
wxSlider::DoGetBestSize() const
352 int textwidth
, textheight
;
353 int mintwidth
, mintheight
;
354 int maxtwidth
, maxtheight
;
356 textwidth
= textheight
= 0;
357 mintwidth
= mintheight
= 0;
358 maxtwidth
= maxtheight
= 0;
360 if (GetWindowStyle() & wxSL_LABELS
)
364 // Get maximum text label width and height
365 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
366 GetTextExtent(text
, &mintwidth
, &mintheight
);
367 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
368 GetTextExtent(text
, &maxtwidth
, &maxtheight
);
370 if (maxtheight
> mintheight
)
371 textheight
= maxtheight
;
373 textheight
= mintheight
;
375 if (maxtwidth
> mintwidth
)
376 textwidth
= maxtwidth
;
378 textwidth
= mintwidth
;
381 if (GetWindowStyle() & wxSL_VERTICAL
)
383 if (GetWindowStyle() & wxSL_AUTOTICKS
)
384 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
386 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
388 if (GetWindowStyle() & wxSL_LABELS
)
389 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
395 if (GetWindowStyle() & wxSL_AUTOTICKS
)
396 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
398 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
402 if (GetWindowStyle() & wxSL_LABELS
)
404 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
405 size
.x
+= (mintwidth
/ 2) + (maxtwidth
/ 2);
412 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
414 int xborder
, yborder
;
415 int minValWidth
, maxValWidth
, textheight
;
419 xborder
= yborder
= 0;
421 if (GetWindowStyle() & wxSL_LABELS
)
426 // Get maximum text label width and height
427 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
428 GetTextExtent(text
, &minValWidth
, &textheight
);
429 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
430 GetTextExtent(text
, &maxValWidth
, &ht
);
435 if (GetWindowStyle() & wxSL_HORIZONTAL
)
437 if ( m_macMinimumStatic
)
439 w
-= minValWidth
/ 2;
440 x
+= minValWidth
/ 2;
443 if ( m_macMaximumStatic
)
444 w
-= maxValWidth
/ 2;
447 // Labels have this control's parent as their parent
448 // so if this control is not at 0,0 relative to the parent
449 // the labels need to know the position of this control
450 // relative to its parent in order to size properly, so
451 // move the control first so we can use GetPosition()
452 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
454 if (GetWindowStyle() & wxSL_VERTICAL
)
455 // If vertical, use current value
456 text
.Printf(wxT("%d"), (int)m_peer
->GetValue());
458 // Use max so that the current value doesn't drift as centering would need to change
459 text
.Printf(wxT("%d"), m_rangeMax
);
461 GetTextExtent(text
, &valValWidth
, &ht
);
463 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
465 // Get slider breadth
466 if (GetWindowStyle() & wxSL_AUTOTICKS
)
467 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
469 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
471 if (GetWindowStyle() & wxSL_VERTICAL
)
475 if ( m_macMinimumStatic
)
476 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ h
- yborder
);
477 if ( m_macMaximumStatic
)
478 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
479 if ( m_macValueStatic
)
480 m_macValueStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ (h
/ 2) - (ht
/ 2));
484 if ( m_macMinimumStatic
)
485 m_macMinimumStatic
->Move(GetPosition().x
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
486 if ( m_macMaximumStatic
)
487 m_macMaximumStatic
->Move(GetPosition().x
+ w
- maxValWidth
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
488 if ( m_macValueStatic
)
489 m_macValueStatic
->Move(GetPosition().x
+ (w
/ 2) - (valValWidth
/ 2), GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
493 // yet another hack since this is a composite control
494 // when wxSlider has it's size hardcoded, we're not allowed to
495 // change the size. But when the control has labels, we DO need
496 // to resize the internal Mac control to accommodate the text labels.
497 // We need to trick the wxWidgets resize mechanism so that we can
498 // resize the slider part of the control ONLY.
500 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
502 int minWidth
= m_minWidth
;
504 if (GetWindowStyle() & wxSL_LABELS
)
506 // make sure we don't allow the entire control to be resized accidently
507 if (width
== GetSize().x
)
511 // If the control has labels, we still need to call this again because
512 // the labels alter the control's w and h values.
513 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
515 m_minWidth
= minWidth
;
518 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
520 wxControl::DoMoveWindow( x
, y
, width
, height
);
523 // Common processing to invert slider values based on wxSL_INVERSE
524 int wxSlider::ValueInvertOrNot(int value
) const
528 if (m_windowStyle
& wxSL_VERTICAL
)
530 // The reason for the backwards logic is that Mac's vertical sliders are
531 // inverted compared to Windows and GTK, hence we want inversion to be the
532 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
533 if (m_windowStyle
& wxSL_INVERSE
)
536 result
= (m_rangeMax
+ m_rangeMin
) - value
;
538 else // normal logic applies to HORIZONTAL sliders
540 result
= wxSliderBase::ValueInvertOrNot(value
);
546 #endif // wxUSE_SLIDER