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 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
26 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
28 // Distance between slider and text
29 #define wxSLIDER_BORDERTEXT 5
31 // NB: The default orientation for a slider is horizontal; however, if the user specifies
32 // some slider styles but doesn't specify the orientation we have to assume he wants a
33 // horizontal one. Therefore in this file when testing for the slider's orientation
34 // vertical is tested for if this is not set then we use the horizontal one
35 // e.g., if (GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }.
45 m_macMinimumStatic
= NULL
;
46 m_macMaximumStatic
= NULL
;
47 m_macValueStatic
= NULL
;
50 bool wxSlider::Create(wxWindow
*parent
,
52 int value
, int minValue
, int maxValue
,
54 const wxSize
& size
, long style
,
55 const wxValidator
& validator
,
58 m_macIsUserPane
= false;
60 m_macMinimumStatic
= NULL
;
61 m_macMaximumStatic
= NULL
;
62 m_macValueStatic
= NULL
;
67 m_rangeMax
= maxValue
;
68 m_rangeMin
= minValue
;
70 m_pageSize
= (int)((maxValue
- minValue
) / 10);
72 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
73 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
74 // reasons we can't really change it, instead try to infer the orientation
75 // from the flags given to us here
76 switch ( style
& (wxSL_LEFT
| wxSL_RIGHT
| wxSL_TOP
| wxSL_BOTTOM
) )
80 style
|= wxSL_VERTICAL
;
85 style
|= wxSL_HORIZONTAL
;
90 // no specific direction, do we have at least the orientation?
91 if ( !(style
& (wxSL_HORIZONTAL
| wxSL_VERTICAL
)) )
93 style
|= wxSL_BOTTOM
| wxSL_HORIZONTAL
;
97 wxASSERT_MSG( !(style
& wxSL_VERTICAL
) || !(style
& wxSL_HORIZONTAL
),
98 wxT("incompatible slider direction and orientation") );
100 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
103 m_peer
= wxWidgetImpl::CreateSlider( this, parent
, id
, value
, minValue
, maxValue
, pos
, size
, style
, GetExtraStyle() );
105 if (style
& wxSL_VERTICAL
)
106 // Forces SetSize to use the proper width
107 SetSizeHints(10, -1, 10, -1);
109 // Forces SetSize to use the proper height
110 SetSizeHints(-1, 10, -1, 10);
112 // NB: SetSizeHints is overloaded by wxSlider and will substitute 10 with the
113 // proper dimensions, it also means other people cannot bugger the slider with
116 if (style
& wxSL_LABELS
)
118 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
119 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
120 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
123 SetRange(minValue
, maxValue
);
126 MacPostControlCreate(pos
, size
);
131 wxSlider::~wxSlider()
133 // this is a special case, as we had to add windows as siblings we are
134 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
135 if ( m_parent
&& !m_parent
->IsBeingDeleted() )
137 delete m_macMinimumStatic
;
138 delete m_macMaximumStatic
;
139 delete m_macValueStatic
;
143 int wxSlider::GetValue() const
145 // We may need to invert the value returned by the widget
146 return ValueInvertOrNot( m_peer
->GetValue() ) ;
149 void wxSlider::SetValue(int value
)
151 if ( m_macValueStatic
)
153 wxString valuestring
;
154 valuestring
.Printf( wxT("%d"), value
);
155 m_macValueStatic
->SetLabel( valuestring
);
158 // We only invert for the setting of the actual native widget
159 m_peer
->SetValue( ValueInvertOrNot( value
) );
162 void wxSlider::SetRange(int minValue
, int maxValue
)
166 m_rangeMin
= minValue
;
167 m_rangeMax
= maxValue
;
169 m_peer
->SetMinimum( m_rangeMin
);
170 m_peer
->SetMaximum( m_rangeMax
);
172 if (m_macMinimumStatic
)
174 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
175 m_macMinimumStatic
->SetLabel( value
);
178 if (m_macMaximumStatic
)
180 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
181 m_macMaximumStatic
->SetLabel( value
);
184 // If the range is out of bounds, set it to a
185 // value that is within bounds
186 // RN: Testing reveals OSX does its own
187 // bounding, perhaps this isn't needed?
188 int currentValue
= GetValue();
190 if(currentValue
< m_rangeMin
)
191 SetValue(m_rangeMin
);
192 else if(currentValue
> m_rangeMax
)
193 SetValue(m_rangeMax
);
196 // For trackbars only
197 void wxSlider::SetTickFreq(int n
, int WXUNUSED(pos
))
203 void wxSlider::SetPageSize(int pageSize
)
206 m_pageSize
= pageSize
;
209 int wxSlider::GetPageSize() const
214 void wxSlider::ClearSel()
219 void wxSlider::ClearTicks()
224 void wxSlider::SetLineSize(int lineSize
)
226 m_lineSize
= lineSize
;
230 int wxSlider::GetLineSize() const
236 int wxSlider::GetSelEnd() const
242 int wxSlider::GetSelStart() const
248 void wxSlider::SetSelection(int WXUNUSED(minPos
), int WXUNUSED(maxPos
))
253 void wxSlider::SetThumbLength(int WXUNUSED(len
))
258 int wxSlider::GetThumbLength() const
264 void wxSlider::SetTick(int WXUNUSED(tickPos
))
269 void wxSlider::Command(wxCommandEvent
&event
)
271 SetValue(event
.GetInt());
272 ProcessCommand(event
);
275 void wxSlider::MacHandleControlClick(WXWidget
WXUNUSED(control
),
276 wxInt16
WXUNUSED(controlpart
),
277 bool WXUNUSED(mouseStillDown
))
279 // Whatever the native value is, we may need to invert it for calling
280 // SetValue and putting the possibly inverted value in the event
281 int value
= ValueInvertOrNot( m_peer
->GetValue() );
285 wxScrollEvent
event( wxEVT_SCROLL_THUMBTRACK
, m_windowId
);
286 event
.SetPosition( value
);
287 event
.SetEventObject( this );
288 HandleWindowEvent( event
);
290 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
291 cevent
.SetInt( value
);
292 cevent
.SetEventObject( this );
293 HandleWindowEvent( cevent
);
296 bool wxSlider::HandleClicked( double timestampsec
)
298 // Whatever the native value is, we may need to invert it for calling
299 // SetValue and putting the possibly inverted value in the event
300 int value
= ValueInvertOrNot( m_peer
->GetValue() ) ;
304 wxScrollEvent
event( wxEVT_SCROLL_THUMBRELEASE
, m_windowId
);
305 event
.SetPosition( value
);
306 event
.SetEventObject( this );
307 HandleWindowEvent( event
);
309 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
310 cevent
.SetInt( value
);
311 cevent
.SetEventObject( this );
313 HandleWindowEvent( cevent
);
318 // This is overloaded in wxSlider so that the proper width/height will always be used
319 // for the slider different values would cause redrawing and mouse detection problems
321 void wxSlider::DoSetSizeHints( int minW
, int minH
,
323 int WXUNUSED(incW
), int WXUNUSED(incH
) )
325 wxSize size
= GetBestSize();
327 if (GetWindowStyle() & wxSL_VERTICAL
)
329 SetMinSize( wxSize(size
.x
,minH
) );
330 SetMaxSize( wxSize(size
.x
,maxH
) );
334 SetMinSize( wxSize(minW
,size
.y
) );
335 SetMaxSize( wxSize(maxW
,size
.y
) );
339 wxSize
wxSlider::DoGetBestSize() const
342 int textwidth
, textheight
;
343 int mintwidth
, mintheight
;
344 int maxtwidth
, maxtheight
;
346 textwidth
= textheight
= 0;
347 mintwidth
= mintheight
= 0;
348 maxtwidth
= maxtheight
= 0;
350 if (GetWindowStyle() & wxSL_LABELS
)
354 // Get maximum text label width and height
355 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
356 GetTextExtent(text
, &mintwidth
, &mintheight
);
357 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
358 GetTextExtent(text
, &maxtwidth
, &maxtheight
);
360 if (maxtheight
> mintheight
)
361 textheight
= maxtheight
;
363 textheight
= mintheight
;
365 if (maxtwidth
> mintwidth
)
366 textwidth
= maxtwidth
;
368 textwidth
= mintwidth
;
371 if (GetWindowStyle() & wxSL_VERTICAL
)
375 if (GetWindowStyle() & wxSL_AUTOTICKS
)
376 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
378 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
380 if (GetWindowStyle() & wxSL_LABELS
)
381 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
387 if (GetWindowStyle() & wxSL_AUTOTICKS
)
388 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
390 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
392 if (GetWindowStyle() & wxSL_LABELS
)
394 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
395 size
.x
+= (mintwidth
/ 2) + (maxtwidth
/ 2);
402 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
405 int minValWidth
, maxValWidth
, textheight
;
409 if (GetWindowStyle() & wxSL_LABELS
)
414 // Get maximum text label width and height
415 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
416 GetTextExtent(text
, &minValWidth
, &textheight
);
417 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
418 GetTextExtent(text
, &maxValWidth
, &ht
);
423 if (GetWindowStyle() & wxSL_HORIZONTAL
)
425 if ( m_macMinimumStatic
)
427 w
-= minValWidth
/ 2;
428 x
+= minValWidth
/ 2;
431 if ( m_macMaximumStatic
)
432 w
-= maxValWidth
/ 2;
435 // Labels have this control's parent as their parent
436 // so if this control is not at 0,0 relative to the parent
437 // the labels need to know the position of this control
438 // relative to its parent in order to size properly, so
439 // move the control first so we can use GetPosition()
440 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
442 if (GetWindowStyle() & wxSL_VERTICAL
)
443 // If vertical, use current value
444 text
.Printf(wxT("%d"), (int)m_peer
->GetValue());
446 // Use max so that the current value doesn't drift as centering would need to change
447 text
.Printf(wxT("%d"), m_rangeMax
);
449 GetTextExtent(text
, &valValWidth
, &ht
);
451 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
453 // Get slider breadth
454 if (GetWindowStyle() & wxSL_AUTOTICKS
)
455 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
457 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
459 if (GetWindowStyle() & wxSL_VERTICAL
)
463 if ( m_macMinimumStatic
)
464 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ h
- yborder
);
465 if ( m_macMaximumStatic
)
466 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
467 if ( m_macValueStatic
)
468 m_macValueStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ (h
/ 2) - (ht
/ 2));
472 if ( m_macMinimumStatic
)
473 m_macMinimumStatic
->Move(GetPosition().x
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
474 if ( m_macMaximumStatic
)
475 m_macMaximumStatic
->Move(GetPosition().x
+ w
- maxValWidth
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
476 if ( m_macValueStatic
)
477 m_macValueStatic
->Move(GetPosition().x
+ (w
/ 2) - (valValWidth
/ 2), GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
481 // yet another hack since this is a composite control
482 // when wxSlider has it's size hardcoded, we're not allowed to
483 // change the size. But when the control has labels, we DO need
484 // to resize the internal Mac control to accommodate the text labels.
485 // We need to trick the wxWidgets resize mechanism so that we can
486 // resize the slider part of the control ONLY.
488 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
490 int minWidth
= m_minWidth
;
492 if (GetWindowStyle() & wxSL_LABELS
)
494 // make sure we don't allow the entire control to be resized accidently
495 if (width
== GetSize().x
)
499 // If the control has labels, we still need to call this again because
500 // the labels alter the control's w and h values.
501 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
503 m_minWidth
= minWidth
;
506 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
508 wxControl::DoMoveWindow( x
, y
, width
, height
);
511 // Common processing to invert slider values based on wxSL_INVERSE
512 int wxSlider::ValueInvertOrNot(int value
) const
516 if (m_windowStyle
& wxSL_VERTICAL
)
518 // The reason for the backwards logic is that Mac's vertical sliders are
519 // inverted compared to Windows and GTK, hence we want inversion to be the
520 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
521 if (m_windowStyle
& wxSL_INVERSE
)
524 result
= (m_rangeMax
+ m_rangeMin
) - value
;
526 else // normal logic applies to HORIZONTAL sliders
528 result
= wxSliderBase::ValueInvertOrNot(value
);
534 #endif // wxUSE_SLIDER