1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/slider_osx.cpp
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/osx/private.h"
19 BEGIN_EVENT_TABLE(wxSlider
, wxControl
)
22 // The dimensions of the different styles of sliders (from Aqua document)
24 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 28
25 #define wxSLIDER_DIMENSIONACROSS_ARROW 21
27 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
28 #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 doesn't specify the orientation we have to assume he wants a
36 // horizontal one. Therefore in this file when testing for the slider's orientation
37 // vertical is tested for if this is not set then we use the horizontal one
38 // e.g., if (GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }.
48 m_macMinimumStatic
= NULL
;
49 m_macMaximumStatic
= NULL
;
50 m_macValueStatic
= NULL
;
53 bool wxSlider::Create(wxWindow
*parent
,
55 int value
, int minValue
, int maxValue
,
57 const wxSize
& size
, long style
,
58 const wxValidator
& validator
,
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 wxT("incompatible slider direction and orientation") );
103 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
106 SetPeer(wxWidgetImpl::CreateSlider( this, parent
, id
, value
, minValue
, maxValue
, pos
, size
, style
, GetExtraStyle() ));
109 if (style
& wxSL_VERTICAL
)
110 // Forces SetSize to use the proper width
111 SetSizeHints(10, -1, 10, -1);
113 // Forces SetSize to use the proper height
114 SetSizeHints(-1, 10, -1, 10);
116 // NB: SetSizeHints is overloaded by wxSlider and will substitute 10 with the
117 // 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( GetPeer()->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 GetPeer()->SetValue( ValueInvertOrNot( value
) );
167 void wxSlider::SetRange(int minValue
, int maxValue
)
169 // Changing the range preserves the value of the native control but may
170 // change our logical value if we're inverting the native value to get it
171 // as ValueInvertOrNot() depends on the range so preserve it before
172 // changing the range.
173 const int valueOld
= GetValue();
177 m_rangeMin
= minValue
;
178 m_rangeMax
= maxValue
;
180 GetPeer()->SetMinimum( m_rangeMin
);
181 GetPeer()->SetMaximum( m_rangeMax
);
183 if (m_macMinimumStatic
)
185 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
186 m_macMinimumStatic
->SetLabel( value
);
189 if (m_macMaximumStatic
)
191 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
192 m_macMaximumStatic
->SetLabel( value
);
195 // If the range is out of bounds, set it to a
196 // value that is within bounds
197 // RN: Testing reveals OSX does its own
198 // bounding, perhaps this isn't needed?
199 int currentValue
= GetValue();
201 if(currentValue
< m_rangeMin
)
202 SetValue(m_rangeMin
);
203 else if(currentValue
> m_rangeMax
)
204 SetValue(m_rangeMax
);
206 // Ensure that our value didn't change.
210 // For trackbars only
211 void wxSlider::DoSetTickFreq(int n
)
217 void wxSlider::SetPageSize(int pageSize
)
220 m_pageSize
= pageSize
;
223 int wxSlider::GetPageSize() const
228 void wxSlider::ClearSel()
233 void wxSlider::ClearTicks()
238 void wxSlider::SetLineSize(int lineSize
)
240 m_lineSize
= lineSize
;
244 int wxSlider::GetLineSize() const
250 int wxSlider::GetSelEnd() const
256 int wxSlider::GetSelStart() const
262 void wxSlider::SetSelection(int WXUNUSED(minPos
), int WXUNUSED(maxPos
))
267 void wxSlider::SetThumbLength(int WXUNUSED(len
))
272 int wxSlider::GetThumbLength() const
278 void wxSlider::SetTick(int WXUNUSED(tickPos
))
283 void wxSlider::Command(wxCommandEvent
&event
)
285 SetValue(event
.GetInt());
286 ProcessCommand(event
);
289 void wxSlider::TriggerScrollEvent( wxEventType scrollEvent
)
291 // Whatever the native value is, we may need to invert it for calling
292 // SetValue and putting the possibly inverted value in the event
293 int value
= ValueInvertOrNot( GetPeer()->GetValue() );
297 wxScrollEvent
event( scrollEvent
, m_windowId
);
298 event
.SetPosition( value
);
299 event
.SetEventObject( this );
300 HandleWindowEvent( event
);
302 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
303 cevent
.SetInt( value
);
304 cevent
.SetEventObject( this );
305 HandleWindowEvent( cevent
);
308 bool wxSlider::OSXHandleClicked( double WXUNUSED(timestampsec
) )
310 TriggerScrollEvent(wxEVT_SCROLL_THUMBRELEASE
);
315 // This is overloaded in wxSlider so that the proper width/height will always be used
316 // for the slider different values would cause redrawing and mouse detection problems
318 void wxSlider::DoSetSizeHints( int minW
, int minH
,
320 int WXUNUSED(incW
), int WXUNUSED(incH
) )
322 wxSize size
= GetBestSize();
324 if (GetWindowStyle() & wxSL_VERTICAL
)
326 SetMinSize( wxSize(size
.x
,minH
) );
327 SetMaxSize( wxSize(size
.x
,maxH
) );
331 SetMinSize( wxSize(minW
,size
.y
) );
332 SetMaxSize( wxSize(maxW
,size
.y
) );
336 wxSize
wxSlider::DoGetBestSize() const
339 int textwidth
, textheight
;
340 int mintwidth
, mintheight
;
341 int maxtwidth
, maxtheight
;
343 textwidth
= textheight
= 0;
344 mintwidth
= mintheight
= 0;
345 maxtwidth
= maxtheight
= 0;
347 if (GetWindowStyle() & wxSL_LABELS
)
351 // Get maximum text label width and height
352 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
353 GetTextExtent(text
, &mintwidth
, &mintheight
);
354 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
355 GetTextExtent(text
, &maxtwidth
, &maxtheight
);
357 if (maxtheight
> mintheight
)
358 textheight
= maxtheight
;
360 textheight
= mintheight
;
362 if (maxtwidth
> mintwidth
)
363 textwidth
= maxtwidth
;
365 textwidth
= mintwidth
;
368 if (GetWindowStyle() & wxSL_VERTICAL
)
372 if (GetWindowStyle() & wxSL_AUTOTICKS
)
373 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
375 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
377 if (GetWindowStyle() & wxSL_LABELS
)
378 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
384 if (GetWindowStyle() & wxSL_AUTOTICKS
)
385 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
387 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
389 if (GetWindowStyle() & wxSL_LABELS
)
391 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
392 size
.x
+= (mintwidth
/ 2) + (maxtwidth
/ 2);
399 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
402 int minValWidth
, maxValWidth
, textheight
;
406 if (GetWindowStyle() & wxSL_LABELS
)
411 // Get maximum text label width and height
412 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
413 GetTextExtent(text
, &minValWidth
, &textheight
);
414 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
415 GetTextExtent(text
, &maxValWidth
, &ht
);
420 if (GetWindowStyle() & wxSL_HORIZONTAL
)
422 if ( m_macMinimumStatic
)
424 w
-= minValWidth
/ 2;
425 x
+= minValWidth
/ 2;
428 if ( m_macMaximumStatic
)
429 w
-= maxValWidth
/ 2;
432 // Labels have this control's parent as their parent
433 // so if this control is not at 0,0 relative to the parent
434 // the labels need to know the position of this control
435 // relative to its parent in order to size properly, so
436 // move the control first so we can use GetPosition()
437 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
439 if (GetWindowStyle() & wxSL_VERTICAL
)
440 // If vertical, use current value
441 text
.Printf(wxT("%d"), (int)GetPeer()->GetValue());
443 // Use max so that the current value doesn't drift as centering would need to change
444 text
.Printf(wxT("%d"), m_rangeMax
);
446 GetTextExtent(text
, &valValWidth
, &ht
);
448 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
450 // Get slider breadth
451 if (GetWindowStyle() & wxSL_AUTOTICKS
)
452 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
454 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
456 if (GetWindowStyle() & wxSL_VERTICAL
)
460 if ( m_macMinimumStatic
)
461 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ h
- yborder
);
462 if ( m_macMaximumStatic
)
463 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
464 if ( m_macValueStatic
)
465 m_macValueStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ (h
/ 2) - (ht
/ 2));
469 if ( m_macMinimumStatic
)
470 m_macMinimumStatic
->Move(GetPosition().x
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
471 if ( m_macMaximumStatic
)
472 m_macMaximumStatic
->Move(GetPosition().x
+ w
- maxValWidth
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
473 if ( m_macValueStatic
)
474 m_macValueStatic
->Move(GetPosition().x
+ (w
/ 2) - (valValWidth
/ 2), GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
478 // yet another hack since this is a composite control
479 // when wxSlider has it's size hardcoded, we're not allowed to
480 // change the size. But when the control has labels, we DO need
482 // to resize the internal Mac control to accommodate the text labels.
483 // We need to trick the wxWidgets resize mechanism so that we can
484 // resize the slider part of the control ONLY.
486 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
488 int minWidth
= m_minWidth
;
490 if (GetWindowStyle() & wxSL_LABELS
)
492 // make sure we don't allow the entire control to be resized accidently
493 if (width
== GetSize().x
)
497 // If the control has labels, we still need to call this again because
498 // the labels alter the control's w and h values.
499 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
501 m_minWidth
= minWidth
;
504 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
506 wxControl::DoMoveWindow( x
, y
, width
, height
);
509 // Common processing to invert slider values based on wxSL_INVERSE
510 int wxSlider::ValueInvertOrNot(int value
) const
514 if (m_windowStyle
& wxSL_VERTICAL
)
516 // The reason for the backwards logic is that Mac's vertical sliders are
517 // inverted compared to Windows and GTK, hence we want inversion to be the
518 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
519 if (m_windowStyle
& wxSL_INVERSE
)
522 result
= (m_rangeMax
+ m_rangeMin
) - value
;
524 else // normal logic applies to HORIZONTAL sliders
526 result
= wxSliderBase::ValueInvertOrNot(value
);
532 #endif // wxUSE_SLIDER