1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "slider.h"
16 #include "wx/wxprec.h"
20 #include "wx/slider.h"
21 #include "wx/mac/uma.h"
23 #if !USE_SHARED_LIBRARY
24 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
26 BEGIN_EVENT_TABLE(wxSlider
, wxControl
)
30 // The dimensions of the different styles of sliders (From Aqua document)
31 #define wxSLIDER_DIMENSIONACROSS 15
32 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
33 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
35 // Distance between slider and text
36 #define wxSLIDER_BORDERTEXT 5
38 /* NB! The default orientation for a slider is horizontal however if the user specifies
39 * some slider styles but dosen't specify the orientation we have to assume he wants a
40 * horizontal one. Therefore in this file when testing for the sliders orientation
41 * vertical is tested for if this is not set then we use the horizontal one
42 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
55 extern ControlActionUPP wxMacLiveScrollbarActionUPP
;
57 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
58 int value
, int minValue
, int maxValue
,
60 const wxSize
& size
, long style
,
61 const wxValidator
& validator
,
64 m_macIsUserPane
= false ;
66 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
69 m_macMinimumStatic
= NULL
;
70 m_macMaximumStatic
= NULL
;
71 m_macValueStatic
= NULL
;
76 m_rangeMax
= maxValue
;
77 m_rangeMin
= minValue
;
79 m_pageSize
= (int)((maxValue
-minValue
)/10);
81 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
84 // NB: (RN) Ticks here are sometimes off in the GUI if there
85 // is not as many ticks as there are values
87 UInt16 tickMarks
= 0 ;
88 if ( style
& wxSL_AUTOTICKS
)
89 tickMarks
= (maxValue
- minValue
) + 1; //+1 for the 0 value
91 while (tickMarks
> 20)
92 tickMarks
/= 5; //keep the number of tickmarks from becoming unwieldly
94 m_peer
= new wxMacControl(this) ;
95 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
96 value
, minValue
, maxValue
, kControlSliderPointsDownOrRight
, tickMarks
, true /* liveTracking */ ,
97 wxMacLiveScrollbarActionUPP
, m_peer
->GetControlRefAddr() ) );
100 if(style
& wxSL_VERTICAL
) {
101 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
104 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
106 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
107 // proper dimensions, it also means other people cannot bugger the slider with
110 if(style
& wxSL_LABELS
)
112 m_macMinimumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
113 m_macMaximumStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
114 m_macValueStatic
= new wxStaticText( parent
, wxID_ANY
, wxEmptyString
);
115 SetRange(minValue
, maxValue
);
119 MacPostControlCreate(pos
,size
) ;
124 wxSlider::~wxSlider()
126 // this is a special case, as we had to add windows as siblings we are
127 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
128 if ( m_parent
&& m_parent
->IsBeingDeleted() == false )
130 delete m_macMinimumStatic
;
131 delete m_macMaximumStatic
;
132 delete m_macValueStatic
;
136 int wxSlider::GetValue() const
138 // We may need to invert the value returned by the widget
139 return ValueInvertOrNot( m_peer
->GetValue() ) ;
142 void wxSlider::SetValue(int value
)
144 wxString valuestring
;
145 valuestring
.Printf( wxT("%d") , value
) ;
146 if ( m_macValueStatic
)
147 m_macValueStatic
->SetLabel( valuestring
) ;
149 // We only invert for the setting of the actual native widget
150 m_peer
->SetValue( ValueInvertOrNot ( value
) ) ;
153 void wxSlider::SetRange(int minValue
, int maxValue
)
157 m_rangeMin
= minValue
;
158 m_rangeMax
= maxValue
;
160 m_peer
->SetMinimum( m_rangeMin
);
161 m_peer
->SetMaximum( m_rangeMax
);
163 if(m_macMinimumStatic
) {
164 value
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
165 m_macMinimumStatic
->SetLabel(value
);
167 if(m_macMaximumStatic
) {
168 value
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
169 m_macMaximumStatic
->SetLabel(value
);
171 SetValue(m_rangeMin
);
174 // For trackbars only
175 void wxSlider::SetTickFreq(int n
, int pos
)
181 void wxSlider::SetPageSize(int pageSize
)
184 m_pageSize
= pageSize
;
187 int wxSlider::GetPageSize() const
192 void wxSlider::ClearSel()
197 void wxSlider::ClearTicks()
202 void wxSlider::SetLineSize(int lineSize
)
204 m_lineSize
= lineSize
;
208 int wxSlider::GetLineSize() const
214 int wxSlider::GetSelEnd() const
220 int wxSlider::GetSelStart() const
226 void wxSlider::SetSelection(int minPos
, int maxPos
)
231 void wxSlider::SetThumbLength(int len
)
236 int wxSlider::GetThumbLength() const
242 void wxSlider::SetTick(int tickPos
)
247 void wxSlider::Command (wxCommandEvent
& event
)
249 SetValue (event
.GetInt());
250 ProcessCommand (event
);
253 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
255 // Whatever the native value is, we may need to invert it for calling
256 // SetValue and putting the possibly inverted value in the event
257 SInt16 value
= ValueInvertOrNot ( m_peer
->GetValue() ) ;
261 wxEventType scrollEvent
= wxEVT_NULL
;
263 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
265 wxScrollEvent
event(scrollEvent
, m_windowId
);
266 event
.SetPosition(value
);
267 event
.SetEventObject( this );
268 GetEventHandler()->ProcessEvent(event
);
270 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
271 cevent
.SetInt( value
);
272 cevent
.SetEventObject( this );
274 GetEventHandler()->ProcessEvent( cevent
);
277 wxInt32
wxSlider::MacControlHit( WXEVENTHANDLERREF handler
, WXEVENTREF mevent
)
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 SInt16 value
= ValueInvertOrNot ( m_peer
->GetValue() ) ;
285 wxEventType scrollEvent
= wxEVT_NULL
;
287 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
289 wxScrollEvent
event(scrollEvent
, m_windowId
);
290 event
.SetPosition(value
);
291 event
.SetEventObject( this );
292 GetEventHandler()->ProcessEvent(event
);
294 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
295 cevent
.SetInt( value
);
296 cevent
.SetEventObject( this );
298 GetEventHandler()->ProcessEvent( cevent
);
302 /* This is overloaded in wxSlider so that the proper width/height will always be used
303 * for the slider different values would cause redrawing and mouse detection problems */
304 void wxSlider::DoSetSizeHints( int minW
, int minH
,
305 int maxW
, int maxH
,
306 int incW
, int incH
)
308 wxSize size
= GetBestSize();
310 if(GetWindowStyle() & wxSL_VERTICAL
) {
311 wxWindow::DoSetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
314 wxWindow::DoSetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
318 wxSize
wxSlider::DoGetBestSize() const
321 int textwidth
, textheight
;
323 if(GetWindowStyle() & wxSL_LABELS
)
328 // Get maximum text label width and height
329 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
330 GetTextExtent(text
, &textwidth
, &textheight
);
331 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
332 GetTextExtent(text
, &wd
, &ht
);
333 if(ht
> textheight
) {
336 if (wd
> textwidth
) {
341 if(GetWindowStyle() & wxSL_VERTICAL
)
343 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
344 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
347 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
349 if(GetWindowStyle() & wxSL_LABELS
) {
350 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
356 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
357 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
360 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
362 if(GetWindowStyle() & wxSL_LABELS
) {
363 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
370 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
372 int xborder
, yborder
;
373 int minValWidth
, maxValWidth
, textwidth
, textheight
;
377 xborder
= yborder
= 0;
379 if (GetWindowStyle() & wxSL_LABELS
)
381 //Labels have this control's parent as their parent
382 //so if this control is not at 0,0 relative to the parent
383 //the labels need to know the position of this control
384 //relative to its parent in order to size properly, so
385 //move the control first so we can use GetPosition()
386 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
391 // Get maximum text label width and height
392 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin
) );
393 GetTextExtent(text
, &minValWidth
, &textheight
);
394 text
.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax
) );
395 GetTextExtent(text
, &maxValWidth
, &ht
);
396 if(ht
> textheight
) {
399 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
401 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
402 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
404 // Get slider breadth
405 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
406 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
409 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
412 if(GetWindowStyle() & wxSL_VERTICAL
)
416 if ( m_macMinimumStatic
)
417 m_macMinimumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
,
418 GetPosition().y
+ h
- yborder
);
419 if ( m_macMaximumStatic
)
420 m_macMaximumStatic
->Move(GetPosition().x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, GetPosition().y
+ 0);
421 if ( m_macValueStatic
)
422 m_macValueStatic
->Move(GetPosition().x
, GetPosition().y
+ h
);
427 if ( m_macMinimumStatic
)
428 m_macMinimumStatic
->Move(GetPosition().x
+ 0, GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
429 if ( m_macMaximumStatic
)
430 m_macMaximumStatic
->Move(GetPosition().x
+ w
- (maxValWidth
/2),
431 GetPosition().y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
432 if ( m_macValueStatic
)
433 m_macValueStatic
->Move(GetPosition().x
+ w
, GetPosition().y
+ 0);
437 // yet another hack since this is a composite control
438 // when wxSlider has it's size hardcoded, we're not allowed to
439 // change the size. But when the control has labels, we DO need
440 // to resize the internal Mac control to accomodate the text labels.
441 // We need to trick the wxWidgets resize mechanism so that we can
442 // resize the slider part of the control ONLY.
444 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
447 minWidth
= m_minWidth
;
449 if (GetWindowStyle() & wxSL_LABELS
)
451 // make sure we don't allow the entire control to be resized accidently
452 if (width
== GetSize().x
)
455 //If the control has labels, we still need to call this again because
456 //the labels alter the control's w and h values.
457 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
459 m_minWidth
= minWidth
;
462 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
464 wxControl::DoMoveWindow(x
,y
,width
,height
) ;
467 // Common processing to invert slider values based on wxSL_INVERSE
468 int wxSlider::ValueInvertOrNot(int value
) const
470 if (m_windowStyle
& wxSL_VERTICAL
)
472 // The reason for the backwards logic is that Mac's vertical sliders are
473 // inverted compared to Windows and GTK, hence we want inversion to be the
474 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
475 if (m_windowStyle
& wxSL_INVERSE
)
478 return (m_rangeMax
+ m_rangeMin
) - value
;
480 else // normal logic applies to HORIZONTAL sliders
482 return wxSliderBase::ValueInvertOrNot(value
);
486 #endif // wxUSE_SLIDER