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
33 * NB! The default orientation for a slider is horizontal however if the user specifies
34 * some slider styles but doesn't specify the orientation we have to assume he wants a
35 * horizontal one. Therefore in this file when testing for the slider's orientation
36 * vertical is tested for if this is not set then we use the horizontal one
37 * e.g., if (GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }.
49 m_macMinimumStatic
= NULL
;
50 m_macMaximumStatic
= NULL
;
51 m_macValueStatic
= NULL
;
54 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
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 _T("incompatible slider direction and orientation") );
103 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
106 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
108 // NB: (RN) Ticks here are sometimes off in the GUI if there
109 // is not as many ticks as there are values
111 UInt16 tickMarks
= 0 ;
112 if ( style
& wxSL_AUTOTICKS
)
113 tickMarks
= (maxValue
- minValue
) + 1; //+1 for the 0 value
115 // keep the number of tickmarks from becoming unwieldly
116 while (tickMarks
> 20)
119 m_peer
= new wxMacControl( this );
120 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
121 value
, minValue
, maxValue
, kControlSliderPointsDownOrRight
, tickMarks
, true /* liveTracking */ ,
122 GetwxMacLiveScrollbarActionProc() , m_peer
->GetControlRefAddr() ) );
124 if (style
& wxSL_VERTICAL
)
125 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
127 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
129 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
130 // proper dimensions, it also means other people cannot bugger the slider with
133 if (style
& wxSL_LABELS
)
135 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
136 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
137 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
140 SetRange(minValue
, maxValue
);
143 MacPostControlCreate(pos
,size
) ;
148 wxSlider::~wxSlider()
150 // this is a special case, as we had to add windows as siblings we are
151 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
152 if ( m_parent
&& !m_parent
->IsBeingDeleted() )
154 delete m_macMinimumStatic
;
155 delete m_macMaximumStatic
;
156 delete m_macValueStatic
;
160 int wxSlider::GetValue() const
162 // We may need to invert the value returned by the widget
163 return ValueInvertOrNot( m_peer
->GetValue() ) ;
166 void wxSlider::SetValue(int value
)
168 if ( m_macValueStatic
)
170 wxString valuestring
;
171 valuestring
.Printf( wxT("%d") , value
);
172 m_macValueStatic
->SetLabel( valuestring
);
175 // We only invert for the setting of the actual native widget
176 m_peer
->SetValue( ValueInvertOrNot ( value
) ) ;
179 void wxSlider::SetRange(int minValue
, int maxValue
)
183 m_rangeMin
= minValue
;
184 m_rangeMax
= maxValue
;
186 m_peer
->SetMinimum( m_rangeMin
);
187 m_peer
->SetMaximum( m_rangeMax
);
189 if (m_macMinimumStatic
)
191 value
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
192 m_macMinimumStatic
->SetLabel(value
);
195 if (m_macMaximumStatic
)
197 value
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
198 m_macMaximumStatic
->SetLabel(value
);
201 SetValue(m_rangeMin
);
204 // For trackbars only
205 void wxSlider::SetTickFreq(int n
, int pos
)
211 void wxSlider::SetPageSize(int pageSize
)
214 m_pageSize
= pageSize
;
217 int wxSlider::GetPageSize() const
222 void wxSlider::ClearSel()
227 void wxSlider::ClearTicks()
232 void wxSlider::SetLineSize(int lineSize
)
234 m_lineSize
= lineSize
;
238 int wxSlider::GetLineSize() const
244 int wxSlider::GetSelEnd() const
250 int wxSlider::GetSelStart() const
256 void wxSlider::SetSelection(int minPos
, int maxPos
)
261 void wxSlider::SetThumbLength(int len
)
266 int wxSlider::GetThumbLength() const
272 void wxSlider::SetTick(int tickPos
)
277 void wxSlider::Command (wxCommandEvent
& event
)
279 SetValue(event
.GetInt());
280 ProcessCommand(event
);
283 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
285 // Whatever the native value is, we may need to invert it for calling
286 // SetValue and putting the possibly inverted value in the event
287 SInt16 value
= ValueInvertOrNot ( m_peer
->GetValue() ) ;
291 wxEventType scrollEvent
= wxEVT_NULL
;
293 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
295 wxScrollEvent
event(scrollEvent
, m_windowId
);
296 event
.SetPosition(value
);
297 event
.SetEventObject( this );
298 GetEventHandler()->ProcessEvent(event
);
300 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
301 cevent
.SetInt( value
);
302 cevent
.SetEventObject( this );
304 GetEventHandler()->ProcessEvent( cevent
);
307 wxInt32
wxSlider::MacControlHit( WXEVENTHANDLERREF handler
, WXEVENTREF mevent
)
309 // Whatever the native value is, we may need to invert it for calling
310 // SetValue and putting the possibly inverted value in the event
311 SInt16 value
= ValueInvertOrNot ( m_peer
->GetValue() ) ;
315 wxEventType scrollEvent
= wxEVT_NULL
;
317 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
319 wxScrollEvent
event(scrollEvent
, m_windowId
);
320 event
.SetPosition(value
);
321 event
.SetEventObject( this );
322 GetEventHandler()->ProcessEvent(event
);
324 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
325 cevent
.SetInt( value
);
326 cevent
.SetEventObject( this );
328 GetEventHandler()->ProcessEvent( cevent
);
333 // This is overloaded in wxSlider so that the proper width/height will always be used
334 // for the slider different values would cause redrawing and mouse detection problems
336 void wxSlider::DoSetSizeHints( int minW
, int minH
,
337 int maxW
, int maxH
,
338 int incW
, int incH
)
340 wxSize size
= GetBestSize();
342 if (GetWindowStyle() & wxSL_VERTICAL
)
343 wxWindow::DoSetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
345 wxWindow::DoSetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
348 wxSize
wxSlider::DoGetBestSize() const
351 int textwidth
, textheight
;
352 int mintwidth
, mintheight
;
353 int maxtwidth
, maxtheight
;
355 textwidth
= textheight
= 0;
356 mintwidth
= mintheight
= 0;
357 maxtwidth
= maxtheight
= 0;
359 if (GetWindowStyle() & wxSL_LABELS
)
363 // Get maximum text label width and height
364 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
365 GetTextExtent(text
, &mintwidth
, &mintheight
);
366 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
367 GetTextExtent(text
, &maxtwidth
, &maxtheight
);
369 if (maxtheight
> mintheight
)
370 textheight
= maxtheight
;
372 textheight
= mintheight
;
374 if (maxtwidth
> mintwidth
)
375 textwidth
= maxtwidth
;
377 textwidth
= mintwidth
;
380 if (GetWindowStyle() & wxSL_VERTICAL
)
382 if (GetWindowStyle() & wxSL_AUTOTICKS
)
383 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
385 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
387 if (GetWindowStyle() & wxSL_LABELS
)
388 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
394 if (GetWindowStyle() & wxSL_AUTOTICKS
)
395 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
397 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
401 if (GetWindowStyle() & wxSL_LABELS
)
403 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
404 size
.x
+= (mintwidth
/ 2) + (maxtwidth
/ 2);
411 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
413 int xborder
, yborder
;
414 int minValWidth
, maxValWidth
, textheight
;
418 xborder
= yborder
= 0;
420 if (GetWindowStyle() & wxSL_LABELS
)
425 // Get maximum text label width and height
426 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
427 GetTextExtent(text
, &minValWidth
, &textheight
);
428 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
429 GetTextExtent(text
, &maxValWidth
, &ht
);
434 if (GetWindowStyle() & wxSL_HORIZONTAL
)
436 if ( m_macMinimumStatic
)
438 w
-= minValWidth
/ 2;
439 x
+= minValWidth
/ 2;
442 if ( m_macMaximumStatic
)
443 w
-= maxValWidth
/ 2;
446 // Labels have this control's parent as their parent
447 // so if this control is not at 0,0 relative to the parent
448 // the labels need to know the position of this control
449 // relative to its parent in order to size properly, so
450 // move the control first so we can use GetPosition()
451 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
453 if (GetWindowStyle() & wxSL_VERTICAL
)
454 // If vertical, use current value
455 text
.Printf(wxT("%d"), (int)m_peer
->GetValue());
457 // Use max so that the current value doesn't drift as centering would need to change
458 text
.Printf(wxT("%d"), m_rangeMax
);
460 GetTextExtent(text
, &valValWidth
, &ht
);
462 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
464 // Get slider breadth
465 if (GetWindowStyle() & wxSL_AUTOTICKS
)
466 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
468 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
470 if (GetWindowStyle() & wxSL_VERTICAL
)
474 if ( m_macMinimumStatic
)
475 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ h
- yborder
);
476 if ( m_macMaximumStatic
)
477 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
478 if ( m_macValueStatic
)
479 m_macValueStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ (h
/ 2) - (ht
/ 2));
483 if ( m_macMinimumStatic
)
484 m_macMinimumStatic
->Move(GetPosition().x
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
485 if ( m_macMaximumStatic
)
486 m_macMaximumStatic
->Move(GetPosition().x
+ w
- maxValWidth
, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
487 if ( m_macValueStatic
)
488 m_macValueStatic
->Move(GetPosition().x
+ (w
/ 2) - (valValWidth
/ 2), GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
492 // yet another hack since this is a composite control
493 // when wxSlider has it's size hardcoded, we're not allowed to
494 // change the size. But when the control has labels, we DO need
495 // to resize the internal Mac control to accommodate the text labels.
496 // We need to trick the wxWidgets resize mechanism so that we can
497 // resize the slider part of the control ONLY.
499 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
501 int minWidth
= m_minWidth
;
503 if (GetWindowStyle() & wxSL_LABELS
)
505 // make sure we don't allow the entire control to be resized accidently
506 if (width
== GetSize().x
)
510 // If the control has labels, we still need to call this again because
511 // the labels alter the control's w and h values.
512 wxControl::DoSetSize( x
, y
, w
, h
, sizeFlags
);
514 m_minWidth
= minWidth
;
517 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
519 wxControl::DoMoveWindow( x
, y
, width
, height
);
522 // Common processing to invert slider values based on wxSL_INVERSE
523 int wxSlider::ValueInvertOrNot(int value
) const
527 if (m_windowStyle
& wxSL_VERTICAL
)
529 // The reason for the backwards logic is that Mac's vertical sliders are
530 // inverted compared to Windows and GTK, hence we want inversion to be the
531 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
532 if (m_windowStyle
& wxSL_INVERSE
)
535 result
= (m_rangeMax
+ m_rangeMin
) - value
;
537 else // normal logic applies to HORIZONTAL sliders
539 result
= wxSliderBase::ValueInvertOrNot(value
);
545 #endif // wxUSE_SLIDER