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
,
61 m_macIsUserPane
= false;
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 m_peer
= 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
119 if (style
& wxSL_LABELS
)
121 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
122 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
123 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
126 SetRange(minValue
, maxValue
);
129 MacPostControlCreate(pos
, size
);
134 wxSlider::~wxSlider()
136 // this is a special case, as we had to add windows as siblings we are
137 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
138 if ( m_parent
&& !m_parent
->IsBeingDeleted() )
140 delete m_macMinimumStatic
;
141 delete m_macMaximumStatic
;
142 delete m_macValueStatic
;
146 int wxSlider::GetValue() const
148 // We may need to invert the value returned by the widget
149 return ValueInvertOrNot( m_peer
->GetValue() ) ;
152 void wxSlider::SetValue(int value
)
154 if ( m_macValueStatic
)
156 wxString valuestring
;
157 valuestring
.Printf( wxT("%d"), value
);
158 m_macValueStatic
->SetLabel( valuestring
);
161 // We only invert for the setting of the actual native widget
162 m_peer
->SetValue( ValueInvertOrNot( value
) );
165 void wxSlider::SetRange(int minValue
, int maxValue
)
167 // Changing the range preserves the value of the native control but may
168 // change our logical value if we're inverting the native value to get it
169 // as ValueInvertOrNot() depends on the range so preserve it before
170 // changing the range.
171 const int valueOld
= GetValue();
175 m_rangeMin
= minValue
;
176 m_rangeMax
= maxValue
;
178 m_peer
->SetMinimum( m_rangeMin
);
179 m_peer
->SetMaximum( m_rangeMax
);
181 if (m_macMinimumStatic
)
183 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
184 m_macMinimumStatic
->SetLabel( value
);
187 if (m_macMaximumStatic
)
189 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
190 m_macMaximumStatic
->SetLabel( value
);
193 // If the range is out of bounds, set it to a
194 // value that is within bounds
195 // RN: Testing reveals OSX does its own
196 // bounding, perhaps this isn't needed?
197 int currentValue
= GetValue();
199 if(currentValue
< m_rangeMin
)
200 SetValue(m_rangeMin
);
201 else if(currentValue
> m_rangeMax
)
202 SetValue(m_rangeMax
);
204 // Ensure that our value didn't change.
208 // For trackbars only
209 void wxSlider::DoSetTickFreq(int n
)
215 void wxSlider::SetPageSize(int pageSize
)
218 m_pageSize
= pageSize
;
221 int wxSlider::GetPageSize() const
226 void wxSlider::ClearSel()
231 void wxSlider::ClearTicks()
236 void wxSlider::SetLineSize(int lineSize
)
238 m_lineSize
= lineSize
;
242 int wxSlider::GetLineSize() const
248 int wxSlider::GetSelEnd() const
254 int wxSlider::GetSelStart() const
260 void wxSlider::SetSelection(int WXUNUSED(minPos
), int WXUNUSED(maxPos
))
265 void wxSlider::SetThumbLength(int WXUNUSED(len
))
270 int wxSlider::GetThumbLength() const
276 void wxSlider::SetTick(int WXUNUSED(tickPos
))
281 void wxSlider::Command(wxCommandEvent
&event
)
283 SetValue(event
.GetInt());
284 ProcessCommand(event
);
287 void wxSlider::TriggerScrollEvent( wxEventType scrollEvent
)
289 // Whatever the native value is, we may need to invert it for calling
290 // SetValue and putting the possibly inverted value in the event
291 int value
= ValueInvertOrNot( m_peer
->GetValue() );
295 wxScrollEvent
event( scrollEvent
, m_windowId
);
296 event
.SetPosition( value
);
297 event
.SetEventObject( this );
298 HandleWindowEvent( event
);
300 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
301 cevent
.SetInt( value
);
302 cevent
.SetEventObject( this );
303 HandleWindowEvent( cevent
);
306 bool wxSlider::OSXHandleClicked( double WXUNUSED(timestampsec
) )
308 TriggerScrollEvent(wxEVT_SCROLL_THUMBRELEASE
);
313 // This is overloaded in wxSlider so that the proper width/height will always be used
314 // for the slider different values would cause redrawing and mouse detection problems
316 void wxSlider::DoSetSizeHints( int minW
, int minH
,
318 int WXUNUSED(incW
), int WXUNUSED(incH
) )
320 wxSize size
= GetBestSize();
322 if (GetWindowStyle() & wxSL_VERTICAL
)
324 SetMinSize( wxSize(size
.x
,minH
) );
325 SetMaxSize( wxSize(size
.x
,maxH
) );
329 SetMinSize( wxSize(minW
,size
.y
) );
330 SetMaxSize( wxSize(maxW
,size
.y
) );
334 wxSize
wxSlider::DoGetBestSize() const
337 int textwidth
, textheight
;
338 int mintwidth
, mintheight
;
339 int maxtwidth
, maxtheight
;
341 textwidth
= textheight
= 0;
342 mintwidth
= mintheight
= 0;
343 maxtwidth
= maxtheight
= 0;
345 if (GetWindowStyle() & wxSL_LABELS
)
349 // Get maximum text label width and height
350 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
351 GetTextExtent(text
, &mintwidth
, &mintheight
);
352 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
353 GetTextExtent(text
, &maxtwidth
, &maxtheight
);
355 if (maxtheight
> mintheight
)
356 textheight
= maxtheight
;
358 textheight
= mintheight
;
360 if (maxtwidth
> mintwidth
)
361 textwidth
= maxtwidth
;
363 textwidth
= mintwidth
;
366 if (GetWindowStyle() & wxSL_VERTICAL
)
370 if (GetWindowStyle() & wxSL_AUTOTICKS
)
371 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
373 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
375 if (GetWindowStyle() & wxSL_LABELS
)
376 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
382 if (GetWindowStyle() & wxSL_AUTOTICKS
)
383 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
385 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
387 if (GetWindowStyle() & wxSL_LABELS
)
389 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
390 size
.x
+= (mintwidth
/ 2) + (maxtwidth
/ 2);
397 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
400 int minValWidth
, maxValWidth
, textheight
;
404 if (GetWindowStyle() & wxSL_LABELS
)
409 // Get maximum text label width and height
410 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
411 GetTextExtent(text
, &minValWidth
, &textheight
);
412 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
413 GetTextExtent(text
, &maxValWidth
, &ht
);
418 if (GetWindowStyle() & wxSL_HORIZONTAL
)
420 if ( m_macMinimumStatic
)
422 w
-= minValWidth
/ 2;
423 x
+= minValWidth
/ 2;
426 if ( m_macMaximumStatic
)
427 w
-= maxValWidth
/ 2;
430 // Labels have this control's parent as their parent
431 // so if this control is not at 0,0 relative to the parent
432 // the labels need to know the position of this control
433 // relative to its parent in order to size properly, so
434 // move the control first so we can use GetPosition()
435 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
437 if (GetWindowStyle() & wxSL_VERTICAL
)
438 // If vertical, use current value
439 text
.Printf(wxT("%d"), (int)m_peer
->GetValue());
441 // Use max so that the current value doesn't drift as centering would need to change
442 text
.Printf(wxT("%d"), m_rangeMax
);
444 GetTextExtent(text
, &valValWidth
, &ht
);
446 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
448 // Get slider breadth
449 if (GetWindowStyle() & wxSL_AUTOTICKS
)
450 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
452 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
454 if (GetWindowStyle() & wxSL_VERTICAL
)
458 if ( m_macMinimumStatic
)
459 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ h
- yborder
);
460 if ( m_macMaximumStatic
)
461 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
462 if ( m_macValueStatic
)
463 m_macValueStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ (h
/ 2) - (ht
/ 2));
467 if ( m_macMinimumStatic
)
468 m_macMinimumStatic
->Move(GetPosition().x
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
469 if ( m_macMaximumStatic
)
470 m_macMaximumStatic
->Move(GetPosition().x
+ w
- maxValWidth
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
471 if ( m_macValueStatic
)
472 m_macValueStatic
->Move(GetPosition().x
+ (w
/ 2) - (valValWidth
/ 2), GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
476 // yet another hack since this is a composite control
477 // when wxSlider has it's size hardcoded, we're not allowed to
478 // change the size. But when the control has labels, we DO need
479 // to resize the internal Mac control to accommodate the text labels.
480 // We need to trick the wxWidgets resize mechanism so that we can
481 // resize the slider part of the control ONLY.
483 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
485 int minWidth
= m_minWidth
;
487 if (GetWindowStyle() & wxSL_LABELS
)
489 // make sure we don't allow the entire control to be resized accidently
490 if (width
== GetSize().x
)
494 // If the control has labels, we still need to call this again because
495 // the labels alter the control's w and h values.
496 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
498 m_minWidth
= minWidth
;
501 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
503 wxControl::DoMoveWindow( x
, y
, width
, height
);
506 // Common processing to invert slider values based on wxSL_INVERSE
507 int wxSlider::ValueInvertOrNot(int value
) const
511 if (m_windowStyle
& wxSL_VERTICAL
)
513 // The reason for the backwards logic is that Mac's vertical sliders are
514 // inverted compared to Windows and GTK, hence we want inversion to be the
515 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
516 if (m_windowStyle
& wxSL_INVERSE
)
519 result
= (m_rangeMax
+ m_rangeMin
) - value
;
521 else // normal logic applies to HORIZONTAL sliders
523 result
= wxSliderBase::ValueInvertOrNot(value
);
529 #endif // wxUSE_SLIDER