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 28
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::TriggerScrollEvent( wxEventType scrollEvent
)
282 // Whatever the native value is, we may need to invert it for calling
283 // SetValue and putting the possibly inverted value in the event
284 int value
= ValueInvertOrNot( m_peer
->GetValue() );
288 wxScrollEvent
event( scrollEvent
, m_windowId
);
289 event
.SetPosition( value
);
290 event
.SetEventObject( this );
291 HandleWindowEvent( event
);
293 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
294 cevent
.SetInt( value
);
295 cevent
.SetEventObject( this );
296 HandleWindowEvent( cevent
);
299 bool wxSlider::OSXHandleClicked( double timestampsec
)
301 TriggerScrollEvent(wxEVT_SCROLL_THUMBRELEASE
);
306 // This is overloaded in wxSlider so that the proper width/height will always be used
307 // for the slider different values would cause redrawing and mouse detection problems
309 void wxSlider::DoSetSizeHints( int minW
, int minH
,
311 int WXUNUSED(incW
), int WXUNUSED(incH
) )
313 wxSize size
= GetBestSize();
315 if (GetWindowStyle() & wxSL_VERTICAL
)
317 SetMinSize( wxSize(size
.x
,minH
) );
318 SetMaxSize( wxSize(size
.x
,maxH
) );
322 SetMinSize( wxSize(minW
,size
.y
) );
323 SetMaxSize( wxSize(maxW
,size
.y
) );
327 wxSize
wxSlider::DoGetBestSize() const
330 int textwidth
, textheight
;
331 int mintwidth
, mintheight
;
332 int maxtwidth
, maxtheight
;
334 textwidth
= textheight
= 0;
335 mintwidth
= mintheight
= 0;
336 maxtwidth
= maxtheight
= 0;
338 if (GetWindowStyle() & wxSL_LABELS
)
342 // Get maximum text label width and height
343 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
344 GetTextExtent(text
, &mintwidth
, &mintheight
);
345 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
346 GetTextExtent(text
, &maxtwidth
, &maxtheight
);
348 if (maxtheight
> mintheight
)
349 textheight
= maxtheight
;
351 textheight
= mintheight
;
353 if (maxtwidth
> mintwidth
)
354 textwidth
= maxtwidth
;
356 textwidth
= mintwidth
;
359 if (GetWindowStyle() & wxSL_VERTICAL
)
363 if (GetWindowStyle() & wxSL_AUTOTICKS
)
364 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
366 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
368 if (GetWindowStyle() & wxSL_LABELS
)
369 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
375 if (GetWindowStyle() & wxSL_AUTOTICKS
)
376 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
378 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
380 if (GetWindowStyle() & wxSL_LABELS
)
382 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
383 size
.x
+= (mintwidth
/ 2) + (maxtwidth
/ 2);
390 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
393 int minValWidth
, maxValWidth
, textheight
;
397 if (GetWindowStyle() & wxSL_LABELS
)
402 // Get maximum text label width and height
403 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
404 GetTextExtent(text
, &minValWidth
, &textheight
);
405 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
406 GetTextExtent(text
, &maxValWidth
, &ht
);
411 if (GetWindowStyle() & wxSL_HORIZONTAL
)
413 if ( m_macMinimumStatic
)
415 w
-= minValWidth
/ 2;
416 x
+= minValWidth
/ 2;
419 if ( m_macMaximumStatic
)
420 w
-= maxValWidth
/ 2;
423 // Labels have this control's parent as their parent
424 // so if this control is not at 0,0 relative to the parent
425 // the labels need to know the position of this control
426 // relative to its parent in order to size properly, so
427 // move the control first so we can use GetPosition()
428 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
430 if (GetWindowStyle() & wxSL_VERTICAL
)
431 // If vertical, use current value
432 text
.Printf(wxT("%d"), (int)m_peer
->GetValue());
434 // Use max so that the current value doesn't drift as centering would need to change
435 text
.Printf(wxT("%d"), m_rangeMax
);
437 GetTextExtent(text
, &valValWidth
, &ht
);
439 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
441 // Get slider breadth
442 if (GetWindowStyle() & wxSL_AUTOTICKS
)
443 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
445 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
447 if (GetWindowStyle() & wxSL_VERTICAL
)
451 if ( m_macMinimumStatic
)
452 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ h
- yborder
);
453 if ( m_macMaximumStatic
)
454 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
455 if ( m_macValueStatic
)
456 m_macValueStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ (h
/ 2) - (ht
/ 2));
460 if ( m_macMinimumStatic
)
461 m_macMinimumStatic
->Move(GetPosition().x
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
462 if ( m_macMaximumStatic
)
463 m_macMaximumStatic
->Move(GetPosition().x
+ w
- maxValWidth
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
464 if ( m_macValueStatic
)
465 m_macValueStatic
->Move(GetPosition().x
+ (w
/ 2) - (valValWidth
/ 2), GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
469 // yet another hack since this is a composite control
470 // when wxSlider has it's size hardcoded, we're not allowed to
471 // change the size. But when the control has labels, we DO need
472 // to resize the internal Mac control to accommodate the text labels.
473 // We need to trick the wxWidgets resize mechanism so that we can
474 // resize the slider part of the control ONLY.
476 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
478 int minWidth
= m_minWidth
;
480 if (GetWindowStyle() & wxSL_LABELS
)
482 // make sure we don't allow the entire control to be resized accidently
483 if (width
== GetSize().x
)
487 // If the control has labels, we still need to call this again because
488 // the labels alter the control's w and h values.
489 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
491 m_minWidth
= minWidth
;
494 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
496 wxControl::DoMoveWindow( x
, y
, width
, height
);
499 // Common processing to invert slider values based on wxSL_INVERSE
500 int wxSlider::ValueInvertOrNot(int value
) const
504 if (m_windowStyle
& wxSL_VERTICAL
)
506 // The reason for the backwards logic is that Mac's vertical sliders are
507 // inverted compared to Windows and GTK, hence we want inversion to be the
508 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
509 if (m_windowStyle
& wxSL_INVERSE
)
512 result
= (m_rangeMax
+ m_rangeMin
) - value
;
514 else // normal logic applies to HORIZONTAL sliders
516 result
= wxSliderBase::ValueInvertOrNot(value
);
522 #endif // wxUSE_SLIDER