1 /////////////////////////////////////////////////////////////////////////////
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/mac/uma.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 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
);
105 // NB: (RN) Ticks here are sometimes off in the GUI if there
106 // are not as many tick marks as there are values
109 if ( style
& wxSL_AUTOTICKS
)
110 tickMarks
= (maxValue
- minValue
) + 1; // +1 for the 0 value
112 // keep the number of tickmarks from becoming unwieldly, therefore below it is ok to cast
114 while (tickMarks
> 20)
117 m_peer
= new wxMacControl( this );
118 OSStatus err
= CreateSliderControl(
119 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
,
120 value
, minValue
, maxValue
,
121 kControlSliderPointsDownOrRight
,
122 (UInt16
) tickMarks
, true /* liveTracking */,
123 GetwxMacLiveScrollbarActionProc(),
124 m_peer
->GetControlRefAddr() );
127 if (style
& wxSL_VERTICAL
)
128 // Forces SetSize to use the proper width
129 SetSizeHints(10, -1, 10, -1);
131 // Forces SetSize to use the proper height
132 SetSizeHints(-1, 10, -1, 10);
134 // NB: SetSizeHints is overloaded by wxSlider and will substitute 10 with the
135 // proper dimensions, it also means other people cannot bugger the slider with
138 if (style
& wxSL_LABELS
)
140 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
141 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
142 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
145 SetRange(minValue
, maxValue
);
148 MacPostControlCreate(pos
, size
);
153 wxSlider::~wxSlider()
155 // this is a special case, as we had to add windows as siblings we are
156 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
157 if ( m_parent
&& !m_parent
->IsBeingDeleted() )
159 delete m_macMinimumStatic
;
160 delete m_macMaximumStatic
;
161 delete m_macValueStatic
;
165 int wxSlider::GetValue() const
167 // We may need to invert the value returned by the widget
168 return ValueInvertOrNot( m_peer
->GetValue() ) ;
171 void wxSlider::SetValue(int value
)
173 if ( m_macValueStatic
)
175 wxString valuestring
;
176 valuestring
.Printf( wxT("%d"), value
);
177 m_macValueStatic
->SetLabel( valuestring
);
180 // We only invert for the setting of the actual native widget
181 m_peer
->SetValue( ValueInvertOrNot( value
) );
184 void wxSlider::SetRange(int minValue
, int maxValue
)
188 m_rangeMin
= minValue
;
189 m_rangeMax
= maxValue
;
191 m_peer
->SetMinimum( m_rangeMin
);
192 m_peer
->SetMaximum( m_rangeMax
);
194 if (m_macMinimumStatic
)
196 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
197 m_macMinimumStatic
->SetLabel( value
);
200 if (m_macMaximumStatic
)
202 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
203 m_macMaximumStatic
->SetLabel( value
);
206 // If the range is out of bounds, set it to a
207 // value that is within bounds
208 // RN: Testing reveals OSX does its own
209 // bounding, perhaps this isn't needed?
210 int currentValue
= GetValue();
212 if(currentValue
< m_rangeMin
)
213 SetValue(m_rangeMin
);
214 else if(currentValue
> m_rangeMax
)
215 SetValue(m_rangeMax
);
218 // For trackbars only
219 void wxSlider::SetTickFreq(int n
, int WXUNUSED(pos
))
225 void wxSlider::SetPageSize(int pageSize
)
228 m_pageSize
= pageSize
;
231 int wxSlider::GetPageSize() const
236 void wxSlider::ClearSel()
241 void wxSlider::ClearTicks()
246 void wxSlider::SetLineSize(int lineSize
)
248 m_lineSize
= lineSize
;
252 int wxSlider::GetLineSize() const
258 int wxSlider::GetSelEnd() const
264 int wxSlider::GetSelStart() const
270 void wxSlider::SetSelection(int WXUNUSED(minPos
), int WXUNUSED(maxPos
))
275 void wxSlider::SetThumbLength(int WXUNUSED(len
))
280 int wxSlider::GetThumbLength() const
286 void wxSlider::SetTick(int WXUNUSED(tickPos
))
291 void wxSlider::Command(wxCommandEvent
&event
)
293 SetValue(event
.GetInt());
294 ProcessCommand(event
);
297 void wxSlider::MacHandleControlClick(WXWidget
WXUNUSED(control
),
298 wxInt16
WXUNUSED(controlpart
),
299 bool WXUNUSED(mouseStillDown
))
301 // Whatever the native value is, we may need to invert it for calling
302 // SetValue and putting the possibly inverted value in the event
303 int value
= ValueInvertOrNot( m_peer
->GetValue() );
307 wxScrollEvent
event( wxEVT_SCROLL_THUMBTRACK
, m_windowId
);
308 event
.SetPosition( value
);
309 event
.SetEventObject( this );
310 HandleWindowEvent( event
);
312 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
313 cevent
.SetInt( value
);
314 cevent
.SetEventObject( this );
315 HandleWindowEvent( cevent
);
318 wxInt32
wxSlider::MacControlHit(WXEVENTHANDLERREF
WXUNUSED(handler
),
319 WXEVENTREF
WXUNUSED(mevent
))
321 // Whatever the native value is, we may need to invert it for calling
322 // SetValue and putting the possibly inverted value in the event
323 int value
= ValueInvertOrNot( m_peer
->GetValue() ) ;
327 wxScrollEvent
event( wxEVT_SCROLL_THUMBRELEASE
, m_windowId
);
328 event
.SetPosition( value
);
329 event
.SetEventObject( this );
330 HandleWindowEvent( event
);
332 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
333 cevent
.SetInt( value
);
334 cevent
.SetEventObject( this );
336 HandleWindowEvent( cevent
);
341 // This is overloaded in wxSlider so that the proper width/height will always be used
342 // for the slider different values would cause redrawing and mouse detection problems
344 void wxSlider::DoSetSizeHints( int minW
, int minH
,
346 int WXUNUSED(incW
), int WXUNUSED(incH
) )
348 wxSize size
= GetBestSize();
350 if (GetWindowStyle() & wxSL_VERTICAL
)
352 SetMinSize( wxSize(size
.x
,minH
) );
353 SetMaxSize( wxSize(size
.x
,maxH
) );
357 SetMinSize( wxSize(minW
,size
.y
) );
358 SetMaxSize( wxSize(maxW
,size
.y
) );
362 wxSize
wxSlider::DoGetBestSize() const
365 int textwidth
, textheight
;
366 int mintwidth
, mintheight
;
367 int maxtwidth
, maxtheight
;
369 textwidth
= textheight
= 0;
370 mintwidth
= mintheight
= 0;
371 maxtwidth
= maxtheight
= 0;
373 if (GetWindowStyle() & wxSL_LABELS
)
377 // Get maximum text label width and height
378 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
379 GetTextExtent(text
, &mintwidth
, &mintheight
);
380 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
381 GetTextExtent(text
, &maxtwidth
, &maxtheight
);
383 if (maxtheight
> mintheight
)
384 textheight
= maxtheight
;
386 textheight
= mintheight
;
388 if (maxtwidth
> mintwidth
)
389 textwidth
= maxtwidth
;
391 textwidth
= mintwidth
;
394 if (GetWindowStyle() & wxSL_VERTICAL
)
398 if (GetWindowStyle() & wxSL_AUTOTICKS
)
399 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
401 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
403 if (GetWindowStyle() & wxSL_LABELS
)
404 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
410 if (GetWindowStyle() & wxSL_AUTOTICKS
)
411 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
413 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
415 if (GetWindowStyle() & wxSL_LABELS
)
417 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
418 size
.x
+= (mintwidth
/ 2) + (maxtwidth
/ 2);
425 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
428 int minValWidth
, maxValWidth
, textheight
;
432 if (GetWindowStyle() & wxSL_LABELS
)
437 // Get maximum text label width and height
438 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
439 GetTextExtent(text
, &minValWidth
, &textheight
);
440 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
441 GetTextExtent(text
, &maxValWidth
, &ht
);
446 if (GetWindowStyle() & wxSL_HORIZONTAL
)
448 if ( m_macMinimumStatic
)
450 w
-= minValWidth
/ 2;
451 x
+= minValWidth
/ 2;
454 if ( m_macMaximumStatic
)
455 w
-= maxValWidth
/ 2;
458 // Labels have this control's parent as their parent
459 // so if this control is not at 0,0 relative to the parent
460 // the labels need to know the position of this control
461 // relative to its parent in order to size properly, so
462 // move the control first so we can use GetPosition()
463 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
465 if (GetWindowStyle() & wxSL_VERTICAL
)
466 // If vertical, use current value
467 text
.Printf(wxT("%d"), (int)m_peer
->GetValue());
469 // Use max so that the current value doesn't drift as centering would need to change
470 text
.Printf(wxT("%d"), m_rangeMax
);
472 GetTextExtent(text
, &valValWidth
, &ht
);
474 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
476 // Get slider breadth
477 if (GetWindowStyle() & wxSL_AUTOTICKS
)
478 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
480 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
482 if (GetWindowStyle() & wxSL_VERTICAL
)
486 if ( m_macMinimumStatic
)
487 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ h
- yborder
);
488 if ( m_macMaximumStatic
)
489 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
490 if ( m_macValueStatic
)
491 m_macValueStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ (h
/ 2) - (ht
/ 2));
495 if ( m_macMinimumStatic
)
496 m_macMinimumStatic
->Move(GetPosition().x
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
497 if ( m_macMaximumStatic
)
498 m_macMaximumStatic
->Move(GetPosition().x
+ w
- maxValWidth
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
499 if ( m_macValueStatic
)
500 m_macValueStatic
->Move(GetPosition().x
+ (w
/ 2) - (valValWidth
/ 2), GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
504 // yet another hack since this is a composite control
505 // when wxSlider has it's size hardcoded, we're not allowed to
506 // change the size. But when the control has labels, we DO need
507 // to resize the internal Mac control to accommodate the text labels.
508 // We need to trick the wxWidgets resize mechanism so that we can
509 // resize the slider part of the control ONLY.
511 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
513 int minWidth
= m_minWidth
;
515 if (GetWindowStyle() & wxSL_LABELS
)
517 // make sure we don't allow the entire control to be resized accidently
518 if (width
== GetSize().x
)
522 // If the control has labels, we still need to call this again because
523 // the labels alter the control's w and h values.
524 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
526 m_minWidth
= minWidth
;
529 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
531 wxControl::DoMoveWindow( x
, y
, width
, height
);
534 // Common processing to invert slider values based on wxSL_INVERSE
535 int wxSlider::ValueInvertOrNot(int value
) const
539 if (m_windowStyle
& wxSL_VERTICAL
)
541 // The reason for the backwards logic is that Mac's vertical sliders are
542 // inverted compared to Windows and GTK, hence we want inversion to be the
543 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
544 if (m_windowStyle
& wxSL_INVERSE
)
547 result
= (m_rangeMax
+ m_rangeMin
) - value
;
549 else // normal logic applies to HORIZONTAL sliders
551 result
= wxSliderBase::ValueInvertOrNot(value
);
557 #endif // wxUSE_SLIDER