1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/slider_osx.cpp
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/slider.h"
16 #include "wx/osx/private.h"
18 BEGIN_EVENT_TABLE(wxSlider
, wxControl
)
21 // The dimensions of the different styles of sliders (from Aqua document)
23 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 28
24 #define wxSLIDER_DIMENSIONACROSS_ARROW 21
26 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
27 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
30 // Distance between slider and text
31 #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 }.
47 m_macMinimumStatic
= NULL
;
48 m_macMaximumStatic
= NULL
;
49 m_macValueStatic
= NULL
;
52 bool wxSlider::Create(wxWindow
*parent
,
54 int value
, int minValue
, int maxValue
,
56 const wxSize
& size
, long style
,
57 const wxValidator
& validator
,
62 m_macMinimumStatic
= NULL
;
63 m_macMaximumStatic
= NULL
;
64 m_macValueStatic
= NULL
;
69 m_rangeMax
= maxValue
;
70 m_rangeMin
= minValue
;
72 m_pageSize
= (int)((maxValue
- minValue
) / 10);
74 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
75 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
76 // reasons we can't really change it, instead try to infer the orientation
77 // from the flags given to us here
78 switch ( style
& (wxSL_LEFT
| wxSL_RIGHT
| wxSL_TOP
| wxSL_BOTTOM
) )
82 style
|= wxSL_VERTICAL
;
87 style
|= wxSL_HORIZONTAL
;
92 // no specific direction, do we have at least the orientation?
93 if ( !(style
& (wxSL_HORIZONTAL
| wxSL_VERTICAL
)) )
95 style
|= wxSL_BOTTOM
| wxSL_HORIZONTAL
;
99 wxASSERT_MSG( !(style
& wxSL_VERTICAL
) || !(style
& wxSL_HORIZONTAL
),
100 wxT("incompatible slider direction and orientation") );
102 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
105 SetPeer(wxWidgetImpl::CreateSlider( this, parent
, id
, value
, minValue
, maxValue
, pos
, size
, style
, GetExtraStyle() ));
108 if (style
& wxSL_VERTICAL
)
109 // Forces SetSize to use the proper width
110 SetSizeHints(10, -1, 10, -1);
112 // Forces SetSize to use the proper height
113 SetSizeHints(-1, 10, -1, 10);
115 // NB: SetSizeHints is overloaded by wxSlider and will substitute 10 with the
116 // proper dimensions, it also means other people cannot bugger the slider with
120 if (style
& wxSL_LABELS
)
122 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
123 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
124 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
127 SetRange(minValue
, maxValue
);
130 MacPostControlCreate(pos
, size
);
135 wxSlider::~wxSlider()
137 // this is a special case, as we had to add windows as siblings we are
138 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
139 if ( m_parent
&& !m_parent
->IsBeingDeleted() )
141 delete m_macMinimumStatic
;
142 delete m_macMaximumStatic
;
143 delete m_macValueStatic
;
147 int wxSlider::GetValue() const
149 // We may need to invert the value returned by the widget
150 return ValueInvertOrNot( GetPeer()->GetValue() ) ;
153 void wxSlider::SetValue(int value
)
155 if ( m_macValueStatic
)
157 wxString valuestring
;
158 valuestring
.Printf( wxT("%d"), value
);
159 m_macValueStatic
->SetLabel( valuestring
);
162 // We only invert for the setting of the actual native widget
163 GetPeer()->SetValue( ValueInvertOrNot( value
) );
166 void wxSlider::SetRange(int minValue
, int maxValue
)
168 // Changing the range preserves the value of the native control but may
169 // change our logical value if we're inverting the native value to get it
170 // as ValueInvertOrNot() depends on the range so preserve it before
171 // changing the range.
172 const int valueOld
= GetValue();
176 m_rangeMin
= minValue
;
177 m_rangeMax
= maxValue
;
179 GetPeer()->SetMinimum( m_rangeMin
);
180 GetPeer()->SetMaximum( m_rangeMax
);
182 if (m_macMinimumStatic
)
184 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
185 m_macMinimumStatic
->SetLabel( value
);
188 if (m_macMaximumStatic
)
190 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
191 m_macMaximumStatic
->SetLabel( value
);
194 // If the range is out of bounds, set it to a
195 // value that is within bounds
196 // RN: Testing reveals OSX does its own
197 // bounding, perhaps this isn't needed?
198 int currentValue
= GetValue();
200 if(currentValue
< m_rangeMin
)
201 SetValue(m_rangeMin
);
202 else if(currentValue
> m_rangeMax
)
203 SetValue(m_rangeMax
);
205 // Ensure that our value didn't change.
209 // For trackbars only
210 void wxSlider::DoSetTickFreq(int n
)
216 void wxSlider::SetPageSize(int pageSize
)
219 m_pageSize
= pageSize
;
222 int wxSlider::GetPageSize() const
227 void wxSlider::ClearSel()
232 void wxSlider::ClearTicks()
237 void wxSlider::SetLineSize(int lineSize
)
239 m_lineSize
= lineSize
;
243 int wxSlider::GetLineSize() const
249 int wxSlider::GetSelEnd() const
255 int wxSlider::GetSelStart() const
261 void wxSlider::SetSelection(int WXUNUSED(minPos
), int WXUNUSED(maxPos
))
266 void wxSlider::SetThumbLength(int WXUNUSED(len
))
271 int wxSlider::GetThumbLength() const
277 void wxSlider::SetTick(int WXUNUSED(tickPos
))
282 void wxSlider::Command(wxCommandEvent
&event
)
284 SetValue(event
.GetInt());
285 ProcessCommand(event
);
288 void wxSlider::TriggerScrollEvent( wxEventType scrollEvent
)
290 // Whatever the native value is, we may need to invert it for calling
291 // SetValue and putting the possibly inverted value in the event
292 int value
= ValueInvertOrNot( GetPeer()->GetValue() );
296 wxScrollEvent
event( scrollEvent
, m_windowId
);
297 event
.SetPosition( value
);
298 event
.SetEventObject( this );
299 HandleWindowEvent( event
);
301 wxCommandEvent
cevent( wxEVT_SLIDER
, m_windowId
);
302 cevent
.SetInt( value
);
303 cevent
.SetEventObject( this );
304 HandleWindowEvent( cevent
);
307 bool wxSlider::OSXHandleClicked( double WXUNUSED(timestampsec
) )
309 TriggerScrollEvent(wxEVT_SCROLL_THUMBRELEASE
);
314 // This is overloaded in wxSlider so that the proper width/height will always be used
315 // for the slider different values would cause redrawing and mouse detection problems
317 void wxSlider::DoSetSizeHints( int minW
, int minH
,
319 int WXUNUSED(incW
), int WXUNUSED(incH
) )
321 wxSize size
= GetBestSize();
323 if (GetWindowStyle() & wxSL_VERTICAL
)
325 SetMinSize( wxSize(size
.x
,minH
) );
326 SetMaxSize( wxSize(size
.x
,maxH
) );
330 SetMinSize( wxSize(minW
,size
.y
) );
331 SetMaxSize( wxSize(maxW
,size
.y
) );
335 wxSize
wxSlider::DoGetBestSize() const
338 int textwidth
, textheight
;
339 int mintwidth
, mintheight
;
340 int maxtwidth
, maxtheight
;
342 textwidth
= textheight
= 0;
343 mintwidth
= mintheight
= 0;
344 maxtwidth
= maxtheight
= 0;
346 if (GetWindowStyle() & wxSL_LABELS
)
350 // Get maximum text label width and height
351 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
352 GetTextExtent(text
, &mintwidth
, &mintheight
);
353 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
354 GetTextExtent(text
, &maxtwidth
, &maxtheight
);
356 if (maxtheight
> mintheight
)
357 textheight
= maxtheight
;
359 textheight
= mintheight
;
361 if (maxtwidth
> mintwidth
)
362 textwidth
= maxtwidth
;
364 textwidth
= mintwidth
;
367 if (GetWindowStyle() & wxSL_VERTICAL
)
371 if (GetWindowStyle() & wxSL_AUTOTICKS
)
372 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
374 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
376 if (GetWindowStyle() & wxSL_LABELS
)
377 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
383 if (GetWindowStyle() & wxSL_AUTOTICKS
)
384 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
386 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
388 if (GetWindowStyle() & wxSL_LABELS
)
390 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
391 size
.x
+= (mintwidth
/ 2) + (maxtwidth
/ 2);
398 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
401 int minValWidth
, maxValWidth
, textheight
;
405 if (GetWindowStyle() & wxSL_LABELS
)
410 // Get maximum text label width and height
411 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
412 GetTextExtent(text
, &minValWidth
, &textheight
);
413 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
414 GetTextExtent(text
, &maxValWidth
, &ht
);
419 if (GetWindowStyle() & wxSL_HORIZONTAL
)
421 if ( m_macMinimumStatic
)
423 w
-= minValWidth
/ 2;
424 x
+= minValWidth
/ 2;
427 if ( m_macMaximumStatic
)
428 w
-= maxValWidth
/ 2;
431 // Labels have this control's parent as their parent
432 // so if this control is not at 0,0 relative to the parent
433 // the labels need to know the position of this control
434 // relative to its parent in order to size properly, so
435 // move the control first so we can use GetPosition()
436 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
438 if (GetWindowStyle() & wxSL_VERTICAL
)
439 // If vertical, use current value
440 text
.Printf(wxT("%d"), (int)GetPeer()->GetValue());
442 // Use max so that the current value doesn't drift as centering would need to change
443 text
.Printf(wxT("%d"), m_rangeMax
);
445 GetTextExtent(text
, &valValWidth
, &ht
);
447 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
449 // Get slider breadth
450 if (GetWindowStyle() & wxSL_AUTOTICKS
)
451 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
453 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
455 if (GetWindowStyle() & wxSL_VERTICAL
)
459 if ( m_macMinimumStatic
)
460 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ h
- yborder
);
461 if ( m_macMaximumStatic
)
462 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
463 if ( m_macValueStatic
)
464 m_macValueStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ (h
/ 2) - (ht
/ 2));
468 if ( m_macMinimumStatic
)
469 m_macMinimumStatic
->Move(GetPosition().x
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
470 if ( m_macMaximumStatic
)
471 m_macMaximumStatic
->Move(GetPosition().x
+ w
- maxValWidth
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
472 if ( m_macValueStatic
)
473 m_macValueStatic
->Move(GetPosition().x
+ (w
/ 2) - (valValWidth
/ 2), GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
477 // yet another hack since this is a composite control
478 // when wxSlider has it's size hardcoded, we're not allowed to
479 // change the size. But when the control has labels, we DO need
481 // to resize the internal Mac control to accommodate the text labels.
482 // We need to trick the wxWidgets resize mechanism so that we can
483 // resize the slider part of the control ONLY.
485 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
487 int minWidth
= m_minWidth
;
489 if (GetWindowStyle() & wxSL_LABELS
)
491 // make sure we don't allow the entire control to be resized accidentally
492 if (width
== GetSize().x
)
496 // If the control has labels, we still need to call this again because
497 // the labels alter the control's w and h values.
498 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
500 m_minWidth
= minWidth
;
503 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
505 wxControl::DoMoveWindow( x
, y
, width
, height
);
508 // Common processing to invert slider values based on wxSL_INVERSE
509 int wxSlider::ValueInvertOrNot(int value
) const
513 if (m_windowStyle
& wxSL_VERTICAL
)
515 // The reason for the backwards logic is that Mac's vertical sliders are
516 // inverted compared to Windows and GTK, hence we want inversion to be the
517 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
518 if (m_windowStyle
& wxSL_INVERSE
)
521 result
= (m_rangeMax
+ m_rangeMin
) - value
;
523 else // normal logic applies to HORIZONTAL sliders
525 result
= wxSliderBase::ValueInvertOrNot(value
);
531 #endif // wxUSE_SLIDER