1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/slider_osx.cpp
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: slider.cpp 54129 2008-06-11 19:30:52Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/slider.h"
17 #include "wx/osx/private.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 #ifdef wxOSX_USE_COCOA
26 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 25
27 #define wxSLIDER_DIMENSIONACROSS_ARROW 21
29 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
30 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
33 // Distance between slider and text
34 #define wxSLIDER_BORDERTEXT 5
36 // NB: The default orientation for a slider is horizontal; however, if the user specifies
37 // some slider styles but doesn't specify the orientation we have to assume he wants a
38 // horizontal one. Therefore in this file when testing for the slider's orientation
39 // vertical is tested for if this is not set then we use the horizontal one
40 // e.g., if (GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }.
50 m_macMinimumStatic
= NULL
;
51 m_macMaximumStatic
= NULL
;
52 m_macValueStatic
= NULL
;
55 bool wxSlider::Create(wxWindow
*parent
,
57 int value
, int minValue
, int maxValue
,
59 const wxSize
& size
, long style
,
60 const wxValidator
& validator
,
63 m_macIsUserPane
= false;
65 m_macMinimumStatic
= NULL
;
66 m_macMaximumStatic
= NULL
;
67 m_macValueStatic
= NULL
;
72 m_rangeMax
= maxValue
;
73 m_rangeMin
= minValue
;
75 m_pageSize
= (int)((maxValue
- minValue
) / 10);
77 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
78 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
79 // reasons we can't really change it, instead try to infer the orientation
80 // from the flags given to us here
81 switch ( style
& (wxSL_LEFT
| wxSL_RIGHT
| wxSL_TOP
| wxSL_BOTTOM
) )
85 style
|= wxSL_VERTICAL
;
90 style
|= wxSL_HORIZONTAL
;
95 // no specific direction, do we have at least the orientation?
96 if ( !(style
& (wxSL_HORIZONTAL
| wxSL_VERTICAL
)) )
98 style
|= wxSL_BOTTOM
| wxSL_HORIZONTAL
;
102 wxASSERT_MSG( !(style
& wxSL_VERTICAL
) || !(style
& wxSL_HORIZONTAL
),
103 wxT("incompatible slider direction and orientation") );
105 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
108 m_peer
= wxWidgetImpl::CreateSlider( this, parent
, id
, value
, minValue
, maxValue
, pos
, size
, style
, GetExtraStyle() );
110 if (style
& wxSL_VERTICAL
)
111 // Forces SetSize to use the proper width
112 SetSizeHints(10, -1, 10, -1);
114 // Forces SetSize to use the proper height
115 SetSizeHints(-1, 10, -1, 10);
117 // NB: SetSizeHints is overloaded by wxSlider and will substitute 10 with the
118 // proper dimensions, it also means other people cannot bugger the slider with
121 if (style
& wxSL_LABELS
)
123 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
124 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
125 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
128 SetRange(minValue
, maxValue
);
131 MacPostControlCreate(pos
, size
);
136 wxSlider::~wxSlider()
138 // this is a special case, as we had to add windows as siblings we are
139 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
140 if ( m_parent
&& !m_parent
->IsBeingDeleted() )
142 delete m_macMinimumStatic
;
143 delete m_macMaximumStatic
;
144 delete m_macValueStatic
;
148 int wxSlider::GetValue() const
150 // We may need to invert the value returned by the widget
151 return ValueInvertOrNot( m_peer
->GetValue() ) ;
154 void wxSlider::SetValue(int value
)
156 if ( m_macValueStatic
)
158 wxString valuestring
;
159 valuestring
.Printf( wxT("%d"), value
);
160 m_macValueStatic
->SetLabel( valuestring
);
163 // We only invert for the setting of the actual native widget
164 m_peer
->SetValue( ValueInvertOrNot( value
) );
167 void wxSlider::SetRange(int minValue
, int maxValue
)
171 m_rangeMin
= minValue
;
172 m_rangeMax
= maxValue
;
174 m_peer
->SetMinimum( m_rangeMin
);
175 m_peer
->SetMaximum( m_rangeMax
);
177 if (m_macMinimumStatic
)
179 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
180 m_macMinimumStatic
->SetLabel( value
);
183 if (m_macMaximumStatic
)
185 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
186 m_macMaximumStatic
->SetLabel( value
);
189 // If the range is out of bounds, set it to a
190 // value that is within bounds
191 // RN: Testing reveals OSX does its own
192 // bounding, perhaps this isn't needed?
193 int currentValue
= GetValue();
195 if(currentValue
< m_rangeMin
)
196 SetValue(m_rangeMin
);
197 else if(currentValue
> m_rangeMax
)
198 SetValue(m_rangeMax
);
201 // For trackbars only
202 void wxSlider::SetTickFreq(int n
, int WXUNUSED(pos
))
208 void wxSlider::SetPageSize(int pageSize
)
211 m_pageSize
= pageSize
;
214 int wxSlider::GetPageSize() const
219 void wxSlider::ClearSel()
224 void wxSlider::ClearTicks()
229 void wxSlider::SetLineSize(int lineSize
)
231 m_lineSize
= lineSize
;
235 int wxSlider::GetLineSize() const
241 int wxSlider::GetSelEnd() const
247 int wxSlider::GetSelStart() const
253 void wxSlider::SetSelection(int WXUNUSED(minPos
), int WXUNUSED(maxPos
))
258 void wxSlider::SetThumbLength(int WXUNUSED(len
))
263 int wxSlider::GetThumbLength() const
269 void wxSlider::SetTick(int WXUNUSED(tickPos
))
274 void wxSlider::Command(wxCommandEvent
&event
)
276 SetValue(event
.GetInt());
277 ProcessCommand(event
);
280 void wxSlider::MacHandleControlClick(WXWidget
WXUNUSED(control
),
281 wxInt16
WXUNUSED(controlpart
),
282 bool WXUNUSED(mouseStillDown
))
284 // Whatever the native value is, we may need to invert it for calling
285 // SetValue and putting the possibly inverted value in the event
286 int value
= ValueInvertOrNot( m_peer
->GetValue() );
290 wxScrollEvent
event( wxEVT_SCROLL_THUMBTRACK
, m_windowId
);
291 event
.SetPosition( value
);
292 event
.SetEventObject( this );
293 HandleWindowEvent( event
);
295 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
296 cevent
.SetInt( value
);
297 cevent
.SetEventObject( this );
298 HandleWindowEvent( cevent
);
301 bool wxSlider::HandleClicked( double timestampsec
)
303 // Whatever the native value is, we may need to invert it for calling
304 // SetValue and putting the possibly inverted value in the event
305 int value
= ValueInvertOrNot( m_peer
->GetValue() ) ;
309 wxScrollEvent
event( wxEVT_SCROLL_THUMBRELEASE
, m_windowId
);
310 event
.SetPosition( value
);
311 event
.SetEventObject( this );
312 HandleWindowEvent( event
);
314 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
315 cevent
.SetInt( value
);
316 cevent
.SetEventObject( this );
318 HandleWindowEvent( cevent
);
323 // This is overloaded in wxSlider so that the proper width/height will always be used
324 // for the slider different values would cause redrawing and mouse detection problems
326 void wxSlider::DoSetSizeHints( int minW
, int minH
,
328 int WXUNUSED(incW
), int WXUNUSED(incH
) )
330 wxSize size
= GetBestSize();
332 if (GetWindowStyle() & wxSL_VERTICAL
)
334 SetMinSize( wxSize(size
.x
,minH
) );
335 SetMaxSize( wxSize(size
.x
,maxH
) );
339 SetMinSize( wxSize(minW
,size
.y
) );
340 SetMaxSize( wxSize(maxW
,size
.y
) );
344 wxSize
wxSlider::DoGetBestSize() const
347 int textwidth
, textheight
;
348 int mintwidth
, mintheight
;
349 int maxtwidth
, maxtheight
;
351 textwidth
= textheight
= 0;
352 mintwidth
= mintheight
= 0;
353 maxtwidth
= maxtheight
= 0;
355 if (GetWindowStyle() & wxSL_LABELS
)
359 // Get maximum text label width and height
360 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
361 GetTextExtent(text
, &mintwidth
, &mintheight
);
362 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
363 GetTextExtent(text
, &maxtwidth
, &maxtheight
);
365 if (maxtheight
> mintheight
)
366 textheight
= maxtheight
;
368 textheight
= mintheight
;
370 if (maxtwidth
> mintwidth
)
371 textwidth
= maxtwidth
;
373 textwidth
= mintwidth
;
376 if (GetWindowStyle() & wxSL_VERTICAL
)
380 if (GetWindowStyle() & wxSL_AUTOTICKS
)
381 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
383 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
385 if (GetWindowStyle() & wxSL_LABELS
)
386 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
392 if (GetWindowStyle() & wxSL_AUTOTICKS
)
393 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
395 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
397 if (GetWindowStyle() & wxSL_LABELS
)
399 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
400 size
.x
+= (mintwidth
/ 2) + (maxtwidth
/ 2);
407 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
410 int minValWidth
, maxValWidth
, textheight
;
414 if (GetWindowStyle() & wxSL_LABELS
)
419 // Get maximum text label width and height
420 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
421 GetTextExtent(text
, &minValWidth
, &textheight
);
422 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
423 GetTextExtent(text
, &maxValWidth
, &ht
);
428 if (GetWindowStyle() & wxSL_HORIZONTAL
)
430 if ( m_macMinimumStatic
)
432 w
-= minValWidth
/ 2;
433 x
+= minValWidth
/ 2;
436 if ( m_macMaximumStatic
)
437 w
-= maxValWidth
/ 2;
440 // Labels have this control's parent as their parent
441 // so if this control is not at 0,0 relative to the parent
442 // the labels need to know the position of this control
443 // relative to its parent in order to size properly, so
444 // move the control first so we can use GetPosition()
445 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
447 if (GetWindowStyle() & wxSL_VERTICAL
)
448 // If vertical, use current value
449 text
.Printf(wxT("%d"), (int)m_peer
->GetValue());
451 // Use max so that the current value doesn't drift as centering would need to change
452 text
.Printf(wxT("%d"), m_rangeMax
);
454 GetTextExtent(text
, &valValWidth
, &ht
);
456 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
458 // Get slider breadth
459 if (GetWindowStyle() & wxSL_AUTOTICKS
)
460 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
462 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
464 if (GetWindowStyle() & wxSL_VERTICAL
)
468 if ( m_macMinimumStatic
)
469 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ h
- yborder
);
470 if ( m_macMaximumStatic
)
471 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
472 if ( m_macValueStatic
)
473 m_macValueStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ (h
/ 2) - (ht
/ 2));
477 if ( m_macMinimumStatic
)
478 m_macMinimumStatic
->Move(GetPosition().x
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
479 if ( m_macMaximumStatic
)
480 m_macMaximumStatic
->Move(GetPosition().x
+ w
- maxValWidth
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
481 if ( m_macValueStatic
)
482 m_macValueStatic
->Move(GetPosition().x
+ (w
/ 2) - (valValWidth
/ 2), GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
486 // yet another hack since this is a composite control
487 // when wxSlider has it's size hardcoded, we're not allowed to
488 // change the size. But when the control has labels, we DO need
489 // to resize the internal Mac control to accommodate the text labels.
490 // We need to trick the wxWidgets resize mechanism so that we can
491 // resize the slider part of the control ONLY.
493 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
495 int minWidth
= m_minWidth
;
497 if (GetWindowStyle() & wxSL_LABELS
)
499 // make sure we don't allow the entire control to be resized accidently
500 if (width
== GetSize().x
)
504 // If the control has labels, we still need to call this again because
505 // the labels alter the control's w and h values.
506 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
508 m_minWidth
= minWidth
;
511 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
513 wxControl::DoMoveWindow( x
, y
, width
, height
);
516 // Common processing to invert slider values based on wxSL_INVERSE
517 int wxSlider::ValueInvertOrNot(int value
) const
521 if (m_windowStyle
& wxSL_VERTICAL
)
523 // The reason for the backwards logic is that Mac's vertical sliders are
524 // inverted compared to Windows and GTK, hence we want inversion to be the
525 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
526 if (m_windowStyle
& wxSL_INVERSE
)
529 result
= (m_rangeMax
+ m_rangeMin
) - value
;
531 else // normal logic applies to HORIZONTAL sliders
533 result
= wxSliderBase::ValueInvertOrNot(value
);
539 #endif // wxUSE_SLIDER