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 15
26 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
27 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
29 // Distance between slider and text
30 #define wxSLIDER_BORDERTEXT 5
32 // NB: The default orientation for a slider is horizontal; however, if the user specifies
33 // some slider styles but doesn't specify the orientation we have to assume he wants a
34 // horizontal one. Therefore in this file when testing for the slider's orientation
35 // vertical is tested for if this is not set then we use the horizontal one
36 // e.g., if (GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }.
46 m_macMinimumStatic
= NULL
;
47 m_macMaximumStatic
= NULL
;
48 m_macValueStatic
= NULL
;
51 bool wxSlider::Create(wxWindow
*parent
,
53 int value
, int minValue
, int maxValue
,
55 const wxSize
& size
, long style
,
56 const wxValidator
& validator
,
59 m_macIsUserPane
= false;
61 m_macMinimumStatic
= NULL
;
62 m_macMaximumStatic
= NULL
;
63 m_macValueStatic
= NULL
;
68 m_rangeMax
= maxValue
;
69 m_rangeMin
= minValue
;
71 m_pageSize
= (int)((maxValue
- minValue
) / 10);
73 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
74 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
75 // reasons we can't really change it, instead try to infer the orientation
76 // from the flags given to us here
77 switch ( style
& (wxSL_LEFT
| wxSL_RIGHT
| wxSL_TOP
| wxSL_BOTTOM
) )
81 style
|= wxSL_VERTICAL
;
86 style
|= wxSL_HORIZONTAL
;
91 // no specific direction, do we have at least the orientation?
92 if ( !(style
& (wxSL_HORIZONTAL
| wxSL_VERTICAL
)) )
94 style
|= wxSL_BOTTOM
| wxSL_HORIZONTAL
;
98 wxASSERT_MSG( !(style
& wxSL_VERTICAL
) || !(style
& wxSL_HORIZONTAL
),
99 wxT("incompatible slider direction and orientation") );
101 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
104 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
);
106 // NB: (RN) Ticks here are sometimes off in the GUI if there
107 // are not as many tick marks as there are values
110 if ( style
& wxSL_AUTOTICKS
)
111 tickMarks
= (maxValue
- minValue
) + 1; // +1 for the 0 value
113 // keep the number of tickmarks from becoming unwieldly, therefore below it is ok to cast
115 while (tickMarks
> 20)
118 m_peer
= new wxMacControl( this );
119 OSStatus err
= CreateSliderControl(
120 MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
,
121 value
, minValue
, maxValue
,
122 kControlSliderPointsDownOrRight
,
123 (UInt16
) tickMarks
, true /* liveTracking */,
124 GetwxMacLiveScrollbarActionProc(),
125 m_peer
->GetControlRefAddr() );
128 if (style
& wxSL_VERTICAL
)
129 // Forces SetSize to use the proper width
130 SetSizeHints(10, -1, 10, -1);
132 // Forces SetSize to use the proper height
133 SetSizeHints(-1, 10, -1, 10);
135 // NB: SetSizeHints is overloaded by wxSlider and will substitute 10 with the
136 // proper dimensions, it also means other people cannot bugger the slider with
139 if (style
& wxSL_LABELS
)
141 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
142 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
143 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
146 SetRange(minValue
, maxValue
);
149 MacPostControlCreate(pos
, size
);
154 wxSlider::~wxSlider()
156 // this is a special case, as we had to add windows as siblings we are
157 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
158 if ( m_parent
&& !m_parent
->IsBeingDeleted() )
160 delete m_macMinimumStatic
;
161 delete m_macMaximumStatic
;
162 delete m_macValueStatic
;
166 int wxSlider::GetValue() const
168 // We may need to invert the value returned by the widget
169 return ValueInvertOrNot( m_peer
->GetValue() ) ;
172 void wxSlider::SetValue(int value
)
174 if ( m_macValueStatic
)
176 wxString valuestring
;
177 valuestring
.Printf( wxT("%d"), value
);
178 m_macValueStatic
->SetLabel( valuestring
);
181 // We only invert for the setting of the actual native widget
182 m_peer
->SetValue( ValueInvertOrNot( value
) );
185 void wxSlider::SetRange(int minValue
, int maxValue
)
189 m_rangeMin
= minValue
;
190 m_rangeMax
= maxValue
;
192 m_peer
->SetMinimum( m_rangeMin
);
193 m_peer
->SetMaximum( m_rangeMax
);
195 if (m_macMinimumStatic
)
197 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
198 m_macMinimumStatic
->SetLabel( value
);
201 if (m_macMaximumStatic
)
203 value
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
204 m_macMaximumStatic
->SetLabel( value
);
207 // If the range is out of bounds, set it to a
208 // value that is within bounds
209 // RN: Testing reveals OSX does its own
210 // bounding, perhaps this isn't needed?
211 int currentValue
= GetValue();
213 if(currentValue
< m_rangeMin
)
214 SetValue(m_rangeMin
);
215 else if(currentValue
> m_rangeMax
)
216 SetValue(m_rangeMax
);
219 // For trackbars only
220 void wxSlider::SetTickFreq(int n
, int pos
)
226 void wxSlider::SetPageSize(int pageSize
)
229 m_pageSize
= pageSize
;
232 int wxSlider::GetPageSize() const
237 void wxSlider::ClearSel()
242 void wxSlider::ClearTicks()
247 void wxSlider::SetLineSize(int lineSize
)
249 m_lineSize
= lineSize
;
253 int wxSlider::GetLineSize() const
259 int wxSlider::GetSelEnd() const
265 int wxSlider::GetSelStart() const
271 void wxSlider::SetSelection(int minPos
, int maxPos
)
276 void wxSlider::SetThumbLength(int len
)
281 int wxSlider::GetThumbLength() const
287 void wxSlider::SetTick(int tickPos
)
292 void wxSlider::Command(wxCommandEvent
&event
)
294 SetValue(event
.GetInt());
295 ProcessCommand(event
);
298 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
300 // Whatever the native value is, we may need to invert it for calling
301 // SetValue and putting the possibly inverted value in the event
302 int value
= ValueInvertOrNot( m_peer
->GetValue() );
306 wxScrollEvent
event( wxEVT_SCROLL_THUMBTRACK
, m_windowId
);
307 event
.SetPosition( value
);
308 event
.SetEventObject( this );
309 GetEventHandler()->ProcessEvent( event
);
311 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
312 cevent
.SetInt( value
);
313 cevent
.SetEventObject( this );
314 GetEventHandler()->ProcessEvent( cevent
);
317 wxInt32
wxSlider::MacControlHit( WXEVENTHANDLERREF handler
, WXEVENTREF mevent
)
319 // Whatever the native value is, we may need to invert it for calling
320 // SetValue and putting the possibly inverted value in the event
321 int value
= ValueInvertOrNot( m_peer
->GetValue() ) ;
325 wxScrollEvent
event( wxEVT_SCROLL_THUMBRELEASE
, m_windowId
);
326 event
.SetPosition( value
);
327 event
.SetEventObject( this );
328 GetEventHandler()->ProcessEvent( event
);
330 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
331 cevent
.SetInt( value
);
332 cevent
.SetEventObject( this );
334 GetEventHandler()->ProcessEvent( cevent
);
339 // This is overloaded in wxSlider so that the proper width/height will always be used
340 // for the slider different values would cause redrawing and mouse detection problems
342 void wxSlider::DoSetSizeHints( int minW
, int minH
,
346 wxSize size
= GetBestSize();
348 if (GetWindowStyle() & wxSL_VERTICAL
)
349 wxWindow::DoSetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
351 wxWindow::DoSetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
354 wxSize
wxSlider::DoGetBestSize() const
357 int textwidth
, textheight
;
358 int mintwidth
, mintheight
;
359 int maxtwidth
, maxtheight
;
361 textwidth
= textheight
= 0;
362 mintwidth
= mintheight
= 0;
363 maxtwidth
= maxtheight
= 0;
365 if (GetWindowStyle() & wxSL_LABELS
)
369 // Get maximum text label width and height
370 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
371 GetTextExtent(text
, &mintwidth
, &mintheight
);
372 text
.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
373 GetTextExtent(text
, &maxtwidth
, &maxtheight
);
375 if (maxtheight
> mintheight
)
376 textheight
= maxtheight
;
378 textheight
= mintheight
;
380 if (maxtwidth
> mintwidth
)
381 textwidth
= maxtwidth
;
383 textwidth
= mintwidth
;
386 if (GetWindowStyle() & wxSL_VERTICAL
)
390 if (GetWindowStyle() & wxSL_AUTOTICKS
)
391 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
393 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
395 if (GetWindowStyle() & wxSL_LABELS
)
396 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
402 if (GetWindowStyle() & wxSL_AUTOTICKS
)
403 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
405 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
407 if (GetWindowStyle() & wxSL_LABELS
)
409 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
410 size
.x
+= (mintwidth
/ 2) + (maxtwidth
/ 2);
417 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
419 int xborder
, yborder
;
420 int minValWidth
, maxValWidth
, textheight
;
424 xborder
= yborder
= 0;
426 if (GetWindowStyle() & wxSL_LABELS
)
431 // Get maximum text label width and height
432 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
433 GetTextExtent(text
, &minValWidth
, &textheight
);
434 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
435 GetTextExtent(text
, &maxValWidth
, &ht
);
440 if (GetWindowStyle() & wxSL_HORIZONTAL
)
442 if ( m_macMinimumStatic
)
444 w
-= minValWidth
/ 2;
445 x
+= minValWidth
/ 2;
448 if ( m_macMaximumStatic
)
449 w
-= maxValWidth
/ 2;
452 // Labels have this control's parent as their parent
453 // so if this control is not at 0,0 relative to the parent
454 // the labels need to know the position of this control
455 // relative to its parent in order to size properly, so
456 // move the control first so we can use GetPosition()
457 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
459 if (GetWindowStyle() & wxSL_VERTICAL
)
460 // If vertical, use current value
461 text
.Printf(wxT("%d"), (int)m_peer
->GetValue());
463 // Use max so that the current value doesn't drift as centering would need to change
464 text
.Printf(wxT("%d"), m_rangeMax
);
466 GetTextExtent(text
, &valValWidth
, &ht
);
468 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
470 // Get slider breadth
471 if (GetWindowStyle() & wxSL_AUTOTICKS
)
472 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
474 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
476 if (GetWindowStyle() & wxSL_VERTICAL
)
480 if ( m_macMinimumStatic
)
481 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ h
- yborder
);
482 if ( m_macMaximumStatic
)
483 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
484 if ( m_macValueStatic
)
485 m_macValueStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ (h
/ 2) - (ht
/ 2));
489 if ( m_macMinimumStatic
)
490 m_macMinimumStatic
->Move(GetPosition().x
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
491 if ( m_macMaximumStatic
)
492 m_macMaximumStatic
->Move(GetPosition().x
+ w
- maxValWidth
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
493 if ( m_macValueStatic
)
494 m_macValueStatic
->Move(GetPosition().x
+ (w
/ 2) - (valValWidth
/ 2), GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
498 // yet another hack since this is a composite control
499 // when wxSlider has it's size hardcoded, we're not allowed to
500 // change the size. But when the control has labels, we DO need
501 // to resize the internal Mac control to accommodate the text labels.
502 // We need to trick the wxWidgets resize mechanism so that we can
503 // resize the slider part of the control ONLY.
505 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
507 int minWidth
= m_minWidth
;
509 if (GetWindowStyle() & wxSL_LABELS
)
511 // make sure we don't allow the entire control to be resized accidently
512 if (width
== GetSize().x
)
516 // If the control has labels, we still need to call this again because
517 // the labels alter the control's w and h values.
518 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
520 m_minWidth
= minWidth
;
523 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
525 wxControl::DoMoveWindow( x
, y
, width
, height
);
528 // Common processing to invert slider values based on wxSL_INVERSE
529 int wxSlider::ValueInvertOrNot(int value
) const
533 if (m_windowStyle
& wxSL_VERTICAL
)
535 // The reason for the backwards logic is that Mac's vertical sliders are
536 // inverted compared to Windows and GTK, hence we want inversion to be the
537 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
538 if (m_windowStyle
& wxSL_INVERSE
)
541 result
= (m_rangeMax
+ m_rangeMin
) - value
;
543 else // normal logic applies to HORIZONTAL sliders
545 result
= wxSliderBase::ValueInvertOrNot(value
);
551 #endif // wxUSE_SLIDER