1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "slider.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 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
71 m_macMinimumStatic
= NULL
;
72 m_macMaximumStatic
= NULL
;
73 m_macValueStatic
= NULL
;
79 m_rangeMax
= maxValue
;
80 m_rangeMin
= minValue
;
82 m_pageSize
= (int)((maxValue
-minValue
)/10);
84 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
, style
,
85 validator
, name
, &bounds
, title
);
87 procID
= kControlSliderProc
+ kControlSliderLiveFeedback
;
88 if(style
& wxSL_AUTOTICKS
) {
89 procID
+= kControlSliderHasTickMarks
;
93 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, title
, false,
94 value
, minValue
, maxValue
, procID
, (long) this);
96 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
98 ::SetControlAction( (ControlHandle
) m_macControl
, wxMacLiveScrollbarActionUPP
) ;
100 if(style
& wxSL_LABELS
)
102 m_macMinimumStatic
= new wxStaticText( this, wxID_ANY
, wxEmptyString
);
103 m_macMaximumStatic
= new wxStaticText( this, wxID_ANY
, wxEmptyString
);
104 m_macValueStatic
= new wxStaticText( this, wxID_ANY
, wxEmptyString
);
105 SetRange(minValue
, maxValue
);
110 m_macMinimumStatic
= NULL
;
111 m_macMaximumStatic
= NULL
;
112 m_macValueStatic
= NULL
;
115 if(style
& wxSL_VERTICAL
) {
116 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
119 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
121 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
122 // proper dimensions, it also means other people cannot bugger the slider with
125 MacPostControlCreate() ;
130 wxSlider::~wxSlider()
134 int wxSlider::GetValue() const
136 return GetControl32BitValue( (ControlHandle
) m_macControl
) ;
139 void wxSlider::SetValue(int value
)
141 wxString valuestring
;
142 valuestring
.Printf( wxT("%d") , value
) ;
143 if ( m_macValueStatic
)
144 m_macValueStatic
->SetLabel( valuestring
) ;
145 SetControl32BitValue( (ControlHandle
) m_macControl
, value
) ;
148 void wxSlider::SetRange(int minValue
, int maxValue
)
152 m_rangeMin
= minValue
;
153 m_rangeMax
= maxValue
;
155 SetControl32BitMinimum( (ControlHandle
) m_macControl
, m_rangeMin
);
156 SetControl32BitMaximum( (ControlHandle
) m_macControl
, m_rangeMax
);
158 if(m_macMinimumStatic
) {
159 value
.Printf(wxT("%d"), m_rangeMin
);
160 m_macMinimumStatic
->SetLabel(value
);
162 if(m_macMaximumStatic
) {
163 value
.Printf(wxT("%d"), m_rangeMax
);
164 m_macMaximumStatic
->SetLabel(value
);
166 SetValue(m_rangeMin
);
169 // For trackbars only
170 void wxSlider::SetTickFreq(int n
, int pos
)
176 void wxSlider::SetPageSize(int pageSize
)
179 m_pageSize
= pageSize
;
182 int wxSlider::GetPageSize() const
187 void wxSlider::ClearSel()
192 void wxSlider::ClearTicks()
197 void wxSlider::SetLineSize(int lineSize
)
199 m_lineSize
= lineSize
;
203 int wxSlider::GetLineSize() const
209 int wxSlider::GetSelEnd() const
215 int wxSlider::GetSelStart() const
221 void wxSlider::SetSelection(int minPos
, int maxPos
)
226 void wxSlider::SetThumbLength(int len
)
231 int wxSlider::GetThumbLength() const
237 void wxSlider::SetTick(int tickPos
)
242 void wxSlider::Command (wxCommandEvent
& event
)
244 SetValue (event
.GetInt());
245 ProcessCommand (event
);
248 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
250 SInt16 value
= ::GetControl32BitValue( (ControlHandle
) m_macControl
) ;
254 wxEventType scrollEvent
= wxEVT_NULL
;
256 if ( mouseStillDown
)
257 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
259 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
261 wxScrollEvent
event(scrollEvent
, m_windowId
);
262 event
.SetPosition(value
);
263 event
.SetEventObject( this );
264 GetEventHandler()->ProcessEvent(event
);
266 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
267 cevent
.SetInt( value
);
268 cevent
.SetEventObject( this );
270 GetEventHandler()->ProcessEvent( cevent
);
273 /* This is overloaded in wxSlider so that the proper width/height will always be used
274 * for the slider different values would cause redrawing and mouse detection problems */
275 void wxSlider::DoSetSizeHints( int minW
, int minH
,
276 int maxW
, int maxH
,
277 int incW
, int incH
)
279 wxSize size
= GetBestSize();
281 if(GetWindowStyle() & wxSL_VERTICAL
) {
282 wxWindow::DoSetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
285 wxWindow::DoSetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
289 wxSize
wxSlider::DoGetBestSize() const
292 int textwidth
, textheight
;
294 if(GetWindowStyle() & wxSL_LABELS
)
299 // Get maximum text label width and height
300 text
.Printf(wxT("%d"), m_rangeMin
);
301 GetTextExtent(text
, &textwidth
, &textheight
);
302 text
.Printf(wxT("%d"), m_rangeMax
);
303 GetTextExtent(text
, &wd
, &ht
);
304 if(ht
> textheight
) {
307 if (wd
> textwidth
) {
312 if(GetWindowStyle() & wxSL_VERTICAL
)
314 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
315 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
318 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
320 if(GetWindowStyle() & wxSL_LABELS
) {
321 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
327 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
328 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
331 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
333 if(GetWindowStyle() & wxSL_LABELS
) {
334 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
341 void wxSlider::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
343 wxControl::DoSetSize( x
, y
, width
, height
,sizeFlags
) ;
346 void wxSlider::MacUpdateDimensions()
348 // actually in the current systems this should never be possible, but later reparenting
349 // may become a reality
351 if ( (ControlHandle
) m_macControl
== NULL
)
354 if ( GetParent() == NULL
)
357 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
358 if ( rootwindow
== NULL
)
361 int xborder
, yborder
;
362 int minValWidth
, maxValWidth
, textwidth
, textheight
;
365 xborder
= yborder
= 0;
367 if (GetWindowStyle() & wxSL_LABELS
)
372 // Get maximum text label width and height
373 text
.Printf(wxT("%d"), m_rangeMin
);
374 GetTextExtent(text
, &minValWidth
, &textheight
);
375 text
.Printf(wxT("%d"), m_rangeMax
);
376 GetTextExtent(text
, &maxValWidth
, &ht
);
377 if(ht
> textheight
) {
380 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
382 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
383 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
385 // Get slider breadth
386 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
387 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
390 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
393 if(GetWindowStyle() & wxSL_VERTICAL
)
395 m_macMinimumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
,
396 m_height
- yborder
- textheight
);
397 m_macMaximumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
, 0);
398 m_macValueStatic
->Move(0, m_height
- textheight
);
402 m_macMinimumStatic
->Move(0, sliderBreadth
+ wxSLIDER_BORDERTEXT
);
403 m_macMaximumStatic
->Move(m_width
- xborder
- maxValWidth
/ 2,
404 sliderBreadth
+ wxSLIDER_BORDERTEXT
);
405 m_macValueStatic
->Move(m_width
- textwidth
, 0);
410 GetControlBounds( (ControlHandle
) m_macControl
, &oldBounds
) ;
412 int new_x
= m_x
+ MacGetLeftBorderSize() + m_macHorizontalBorder
;
413 int new_y
= m_y
+ MacGetTopBorderSize() + m_macVerticalBorder
;
414 int new_width
= m_width
- MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder
- xborder
;
415 int new_height
= m_height
- MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder
- yborder
;
417 GetParent()->MacWindowToRootWindow( & new_x
, & new_y
) ;
418 bool doMove
= new_x
!= oldBounds
.left
|| new_y
!= oldBounds
.top
;
419 bool doResize
= ( oldBounds
.right
- oldBounds
.left
) != new_width
|| (oldBounds
.bottom
- oldBounds
.top
) != new_height
;
420 if ( doMove
|| doResize
)
422 InvalWindowRect( rootwindow
, &oldBounds
) ;
425 UMAMoveControl( (ControlHandle
) m_macControl
, new_x
, new_y
) ;
429 UMASizeControl( (ControlHandle
) m_macControl
, new_width
, new_height
) ;
434 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
436 wxControl::DoMoveWindow(x
,y
,width
,height
) ;
439 #endif // wxUSE_SLIDER