1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "slider.h"
16 #include "wx/slider.h"
17 #include "wx/mac/uma.h"
19 #if !USE_SHARED_LIBRARY
20 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
22 BEGIN_EVENT_TABLE(wxSlider
, wxControl
)
26 // The dimensions of the different styles of sliders (From Aqua document)
27 #define wxSLIDER_DIMENSIONACROSS 15
28 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
29 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
31 // Distance between slider and text
32 #define wxSLIDER_BORDERTEXT 5
34 /* NB! The default orientation for a slider is horizontal however if the user specifies
35 * some slider styles but dosen't specify the orientation we have to assume he wants a
36 * horizontal one. Therefore in this file when testing for the sliders orientation
37 * vertical is tested for if this is not set then we use the horizontal one
38 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
51 extern ControlActionUPP wxMacLiveScrollbarActionUPP
;
53 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
54 int value
, int minValue
, int maxValue
,
56 const wxSize
& size
, long style
,
57 const wxValidator
& validator
,
60 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
67 m_macMinimumStatic
= NULL
;
68 m_macMaximumStatic
= NULL
;
69 m_macValueStatic
= NULL
;
75 m_rangeMax
= maxValue
;
76 m_rangeMin
= minValue
;
78 m_pageSize
= (int)((maxValue
-minValue
)/10);
80 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
, style
,
81 validator
, name
, &bounds
, title
);
83 procID
= kControlSliderProc
+ kControlSliderLiveFeedback
;
84 if(style
& wxSL_AUTOTICKS
) {
85 procID
+= kControlSliderHasTickMarks
;
89 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, title
, false,
90 value
, minValue
, maxValue
, procID
, (long) this);
92 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
94 ::SetControlAction( (ControlHandle
) m_macControl
, wxMacLiveScrollbarActionUPP
) ;
96 if(style
& wxSL_LABELS
)
98 m_macMinimumStatic
= new wxStaticText( this, -1, wxEmptyString
);
99 m_macMaximumStatic
= new wxStaticText( this, -1, wxEmptyString
);
100 m_macValueStatic
= new wxStaticText( this, -1, wxEmptyString
);
101 SetRange(minValue
, maxValue
);
106 m_macMinimumStatic
= NULL
;
107 m_macMaximumStatic
= NULL
;
108 m_macValueStatic
= NULL
;
111 if(style
& wxSL_VERTICAL
) {
112 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
115 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
117 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
118 // proper dimensions, it also means other people cannot bugger the slider with
121 MacPostControlCreate() ;
126 wxSlider::~wxSlider()
130 int wxSlider::GetValue() const
132 return GetControl32BitValue( (ControlHandle
) m_macControl
) ;
135 void wxSlider::SetValue(int value
)
137 wxString valuestring
;
138 valuestring
.Printf( wxT("%d") , value
) ;
139 if ( m_macValueStatic
)
140 m_macValueStatic
->SetLabel( valuestring
) ;
141 SetControl32BitValue( (ControlHandle
) m_macControl
, value
) ;
144 void wxSlider::SetRange(int minValue
, int maxValue
)
148 m_rangeMin
= minValue
;
149 m_rangeMax
= maxValue
;
151 SetControl32BitMinimum( (ControlHandle
) m_macControl
, m_rangeMin
);
152 SetControl32BitMaximum( (ControlHandle
) m_macControl
, m_rangeMax
);
154 if(m_macMinimumStatic
) {
155 value
.Printf(wxT("%d"), m_rangeMin
);
156 m_macMinimumStatic
->SetLabel(value
);
158 if(m_macMaximumStatic
) {
159 value
.Printf(wxT("%d"), m_rangeMax
);
160 m_macMaximumStatic
->SetLabel(value
);
162 SetValue(m_rangeMin
);
165 // For trackbars only
166 void wxSlider::SetTickFreq(int n
, int pos
)
172 void wxSlider::SetPageSize(int pageSize
)
175 m_pageSize
= pageSize
;
178 int wxSlider::GetPageSize() const
183 void wxSlider::ClearSel()
188 void wxSlider::ClearTicks()
193 void wxSlider::SetLineSize(int lineSize
)
195 m_lineSize
= lineSize
;
199 int wxSlider::GetLineSize() const
205 int wxSlider::GetSelEnd() const
211 int wxSlider::GetSelStart() const
217 void wxSlider::SetSelection(int minPos
, int maxPos
)
222 void wxSlider::SetThumbLength(int len
)
227 int wxSlider::GetThumbLength() const
233 void wxSlider::SetTick(int tickPos
)
238 void wxSlider::Command (wxCommandEvent
& event
)
240 SetValue (event
.GetInt());
241 ProcessCommand (event
);
244 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
246 SInt16 value
= ::GetControl32BitValue( (ControlHandle
) m_macControl
) ;
250 wxEventType scrollEvent
= wxEVT_NULL
;
252 if ( mouseStillDown
)
253 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
255 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
257 wxScrollEvent
event(scrollEvent
, m_windowId
);
258 event
.SetPosition(value
);
259 event
.SetEventObject( this );
260 GetEventHandler()->ProcessEvent(event
);
262 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
263 cevent
.SetInt( value
);
264 cevent
.SetEventObject( this );
266 GetEventHandler()->ProcessEvent( cevent
);
269 /* This is overloaded in wxSlider so that the proper width/height will always be used
270 * for the slider different values would cause redrawing and mouse detection problems */
271 void wxSlider::SetSizeHints( int minW
, int minH
,
272 int maxW
, int maxH
,
273 int incW
, int incH
)
275 wxSize size
= GetBestSize();
277 if(GetWindowStyle() & wxSL_VERTICAL
) {
278 wxWindow::SetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
281 wxWindow::SetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
285 wxSize
wxSlider::DoGetBestSize() const
288 int textwidth
, textheight
;
290 if(GetWindowStyle() & wxSL_LABELS
)
295 // Get maximum text label width and height
296 text
.Printf(wxT("%d"), m_rangeMin
);
297 GetTextExtent(text
, &textwidth
, &textheight
);
298 text
.Printf(wxT("%d"), m_rangeMax
);
299 GetTextExtent(text
, &wd
, &ht
);
300 if(ht
> textheight
) {
303 if (wd
> textwidth
) {
308 if(GetWindowStyle() & wxSL_VERTICAL
)
310 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
311 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
314 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
316 if(GetWindowStyle() & wxSL_LABELS
) {
317 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
323 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
324 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
327 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
329 if(GetWindowStyle() & wxSL_LABELS
) {
330 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
337 void wxSlider::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
339 wxControl::DoSetSize( x
, y
, width
, height
,sizeFlags
) ;
342 void wxSlider::MacUpdateDimensions()
344 // actually in the current systems this should never be possible, but later reparenting
345 // may become a reality
347 if ( (ControlHandle
) m_macControl
== NULL
)
350 if ( GetParent() == NULL
)
353 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
354 if ( rootwindow
== NULL
)
357 int xborder
, yborder
;
358 int minValWidth
, maxValWidth
, textwidth
, textheight
;
361 xborder
= yborder
= 0;
363 if (GetWindowStyle() & wxSL_LABELS
)
368 // Get maximum text label width and height
369 text
.Printf(wxT("%d"), m_rangeMin
);
370 GetTextExtent(text
, &minValWidth
, &textheight
);
371 text
.Printf(wxT("%d"), m_rangeMax
);
372 GetTextExtent(text
, &maxValWidth
, &ht
);
373 if(ht
> textheight
) {
376 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
378 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
379 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
381 // Get slider breadth
382 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
383 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
386 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
389 if(GetWindowStyle() & wxSL_VERTICAL
)
391 m_macMinimumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
,
392 m_height
- yborder
- textheight
);
393 m_macMaximumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
, 0);
394 m_macValueStatic
->Move(0, m_height
- textheight
);
398 m_macMinimumStatic
->Move(0, sliderBreadth
+ wxSLIDER_BORDERTEXT
);
399 m_macMaximumStatic
->Move(m_width
- xborder
- maxValWidth
/ 2,
400 sliderBreadth
+ wxSLIDER_BORDERTEXT
);
401 m_macValueStatic
->Move(m_width
- textwidth
, 0);
406 GetControlBounds( (ControlHandle
) m_macControl
, &oldBounds
) ;
408 int new_x
= m_x
+ MacGetLeftBorderSize() + m_macHorizontalBorder
;
409 int new_y
= m_y
+ MacGetTopBorderSize() + m_macVerticalBorder
;
410 int new_width
= m_width
- MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder
- xborder
;
411 int new_height
= m_height
- MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder
- yborder
;
413 GetParent()->MacWindowToRootWindow( & new_x
, & new_y
) ;
414 bool doMove
= new_x
!= oldBounds
.left
|| new_y
!= oldBounds
.top
;
415 bool doResize
= ( oldBounds
.right
- oldBounds
.left
) != new_width
|| (oldBounds
.bottom
- oldBounds
.top
) != new_height
;
416 if ( doMove
|| doResize
)
418 InvalWindowRect( rootwindow
, &oldBounds
) ;
421 UMAMoveControl( (ControlHandle
) m_macControl
, new_x
, new_y
) ;
425 UMASizeControl( (ControlHandle
) m_macControl
, new_width
, new_height
) ;
430 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
432 wxControl::DoMoveWindow(x
,y
,width
,height
) ;