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
,
64 m_macMinimumStatic
= NULL
;
65 m_macMaximumStatic
= NULL
;
66 m_macValueStatic
= NULL
;
72 m_rangeMax
= maxValue
;
73 m_rangeMin
= minValue
;
75 m_pageSize
= (int)((maxValue
-minValue
)/10);
77 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
, style
,
78 validator
, name
, &bounds
, title
);
80 procID
= kControlSliderProc
+ kControlSliderLiveFeedback
;
81 if(style
& wxSL_AUTOTICKS
) {
82 procID
+= kControlSliderHasTickMarks
;
86 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, title
, false,
87 value
, minValue
, maxValue
, procID
, (long) this);
89 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
91 ::SetControlAction( (ControlHandle
) m_macControl
, wxMacLiveScrollbarActionUPP
) ;
93 if(style
& wxSL_LABELS
)
95 m_macMinimumStatic
= new wxStaticText( this, -1, wxEmptyString
);
96 m_macMaximumStatic
= new wxStaticText( this, -1, wxEmptyString
);
97 m_macValueStatic
= new wxStaticText( this, -1, wxEmptyString
);
98 SetRange(minValue
, maxValue
);
103 m_macMinimumStatic
= NULL
;
104 m_macMaximumStatic
= NULL
;
105 m_macValueStatic
= NULL
;
108 if(style
& wxSL_VERTICAL
) {
109 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
112 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
114 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
115 // proper dimensions, it also means other people cannot bugger the slider with
118 MacPostControlCreate() ;
123 wxSlider::~wxSlider()
127 int wxSlider::GetValue() const
129 return GetControl32BitValue( (ControlHandle
) m_macControl
) ;
132 void wxSlider::SetValue(int value
)
134 wxString valuestring
;
135 valuestring
.Printf( wxT("%d") , value
) ;
136 if ( m_macValueStatic
)
137 m_macValueStatic
->SetLabel( valuestring
) ;
138 SetControl32BitValue( (ControlHandle
) m_macControl
, value
) ;
141 void wxSlider::SetRange(int minValue
, int maxValue
)
145 m_rangeMin
= minValue
;
146 m_rangeMax
= maxValue
;
148 SetControl32BitMinimum( (ControlHandle
) m_macControl
, m_rangeMin
);
149 SetControl32BitMaximum( (ControlHandle
) m_macControl
, m_rangeMax
);
151 if(m_macMinimumStatic
) {
152 value
.Printf(wxT("%d"), m_rangeMin
);
153 m_macMinimumStatic
->SetLabel(value
);
155 if(m_macMaximumStatic
) {
156 value
.Printf(wxT("%d"), m_rangeMax
);
157 m_macMaximumStatic
->SetLabel(value
);
159 SetValue(m_rangeMin
);
162 // For trackbars only
163 void wxSlider::SetTickFreq(int n
, int pos
)
169 void wxSlider::SetPageSize(int pageSize
)
172 m_pageSize
= pageSize
;
175 int wxSlider::GetPageSize() const
180 void wxSlider::ClearSel()
185 void wxSlider::ClearTicks()
190 void wxSlider::SetLineSize(int lineSize
)
192 m_lineSize
= lineSize
;
196 int wxSlider::GetLineSize() const
202 int wxSlider::GetSelEnd() const
208 int wxSlider::GetSelStart() const
214 void wxSlider::SetSelection(int minPos
, int maxPos
)
219 void wxSlider::SetThumbLength(int len
)
224 int wxSlider::GetThumbLength() const
230 void wxSlider::SetTick(int tickPos
)
235 void wxSlider::Command (wxCommandEvent
& event
)
237 SetValue (event
.GetInt());
238 ProcessCommand (event
);
241 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
243 SInt16 value
= ::GetControl32BitValue( (ControlHandle
) m_macControl
) ;
247 wxEventType scrollEvent
= wxEVT_NULL
;
249 if ( mouseStillDown
)
250 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
252 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
254 wxScrollEvent
event(scrollEvent
, m_windowId
);
255 event
.SetPosition(value
);
256 event
.SetEventObject( this );
257 GetEventHandler()->ProcessEvent(event
);
259 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
260 cevent
.SetInt( value
);
261 cevent
.SetEventObject( this );
263 GetEventHandler()->ProcessEvent( cevent
);
266 /* This is overloaded in wxSlider so that the proper width/height will always be used
267 * for the slider different values would cause redrawing and mouse detection problems */
268 void wxSlider::SetSizeHints( int minW
, int minH
,
269 int maxW
, int maxH
,
270 int incW
, int incH
)
272 wxSize size
= GetBestSize();
274 if(GetWindowStyle() & wxSL_VERTICAL
) {
275 wxWindow::SetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
278 wxWindow::SetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
282 wxSize
wxSlider::DoGetBestSize() const
285 int textwidth
, textheight
;
287 if(GetWindowStyle() & wxSL_LABELS
)
292 // Get maximum text label width and height
293 text
.Printf(wxT("%d"), m_rangeMin
);
294 GetTextExtent(text
, &textwidth
, &textheight
);
295 text
.Printf(wxT("%d"), m_rangeMax
);
296 GetTextExtent(text
, &wd
, &ht
);
297 if(ht
> textheight
) {
300 if (wd
> textwidth
) {
305 if(GetWindowStyle() & wxSL_VERTICAL
)
307 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
308 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
311 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
313 if(GetWindowStyle() & wxSL_LABELS
) {
314 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
320 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
321 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
324 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
326 if(GetWindowStyle() & wxSL_LABELS
) {
327 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
334 void wxSlider::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
336 wxControl::DoSetSize( x
, y
, width
, height
,sizeFlags
) ;
339 void wxSlider::MacUpdateDimensions()
341 // actually in the current systems this should never be possible, but later reparenting
342 // may become a reality
344 if ( (ControlHandle
) m_macControl
== NULL
)
347 if ( GetParent() == NULL
)
350 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
351 if ( rootwindow
== NULL
)
354 int xborder
, yborder
;
355 int minValWidth
, maxValWidth
, textwidth
, textheight
;
358 xborder
= yborder
= 0;
360 if (GetWindowStyle() & wxSL_LABELS
)
365 // Get maximum text label width and height
366 text
.Printf(wxT("%d"), m_rangeMin
);
367 GetTextExtent(text
, &minValWidth
, &textheight
);
368 text
.Printf(wxT("%d"), m_rangeMax
);
369 GetTextExtent(text
, &maxValWidth
, &ht
);
370 if(ht
> textheight
) {
373 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
375 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
376 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
378 // Get slider breadth
379 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
380 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
383 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
386 if(GetWindowStyle() & wxSL_VERTICAL
)
388 m_macMinimumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
,
389 m_height
- yborder
- textheight
);
390 m_macMaximumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
, 0);
391 m_macValueStatic
->Move(0, m_height
- textheight
);
395 m_macMinimumStatic
->Move(0, sliderBreadth
+ wxSLIDER_BORDERTEXT
);
396 m_macMaximumStatic
->Move(m_width
- xborder
- maxValWidth
/ 2,
397 sliderBreadth
+ wxSLIDER_BORDERTEXT
);
398 m_macValueStatic
->Move(m_width
- textwidth
, 0);
403 GetControlBounds( (ControlHandle
) m_macControl
, &oldBounds
) ;
405 int new_x
= m_x
+ MacGetLeftBorderSize() + m_macHorizontalBorder
;
406 int new_y
= m_y
+ MacGetTopBorderSize() + m_macVerticalBorder
;
407 int new_width
= m_width
- MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder
- xborder
;
408 int new_height
= m_height
- MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder
- yborder
;
410 GetParent()->MacWindowToRootWindow( & new_x
, & new_y
) ;
411 bool doMove
= new_x
!= oldBounds
.left
|| new_y
!= oldBounds
.top
;
412 bool doResize
= ( oldBounds
.right
- oldBounds
.left
) != new_width
|| (oldBounds
.bottom
- oldBounds
.top
) != new_height
;
413 if ( doMove
|| doResize
)
415 InvalWindowRect( rootwindow
, &oldBounds
) ;
418 UMAMoveControl( (ControlHandle
) m_macControl
, new_x
, new_y
) ;
422 UMASizeControl( (ControlHandle
) m_macControl
, new_width
, new_height
) ;
427 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
429 wxControl::DoMoveWindow(x
,y
,width
,height
) ;