1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/classic/slider.cpp
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
20 #include "wx/slider.h"
21 #include "wx/mac/uma.h"
23 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
25 BEGIN_EVENT_TABLE(wxSlider
, wxControl
)
28 // The dimensions of the different styles of sliders (From Aqua document)
29 #define wxSLIDER_DIMENSIONACROSS 15
30 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
31 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
33 // Distance between slider and text
34 #define wxSLIDER_BORDERTEXT 5
36 /* NB! The default orientation for a slider is horizontal however if the user specifies
37 * some slider styles but dosen't specify the orientation we have to assume he wants a
38 * horizontal one. Therefore in this file when testing for the sliders orientation
39 * vertical is tested for if this is not set then we use the horizontal one
40 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
53 extern ControlActionUPP wxMacLiveScrollbarActionUPP
;
55 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
56 int value
, int minValue
, int maxValue
,
58 const wxSize
& size
, long style
,
59 const wxValidator
& validator
,
62 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
69 m_macMinimumStatic
= NULL
;
70 m_macMaximumStatic
= NULL
;
71 m_macValueStatic
= NULL
;
77 m_rangeMax
= maxValue
;
78 m_rangeMin
= minValue
;
80 m_pageSize
= (int)((maxValue
-minValue
)/10);
82 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
, style
,
83 validator
, name
, &bounds
, title
);
85 procID
= kControlSliderProc
+ kControlSliderLiveFeedback
;
86 if(style
& wxSL_AUTOTICKS
) {
87 procID
+= kControlSliderHasTickMarks
;
91 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, title
, false,
92 value
, minValue
, maxValue
, procID
, (long) this);
94 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
96 ::SetControlAction( (ControlHandle
) m_macControl
, wxMacLiveScrollbarActionUPP
) ;
98 if(style
& wxSL_LABELS
)
100 m_macMinimumStatic
= new wxStaticText( this, wxID_ANY
, wxEmptyString
);
101 m_macMaximumStatic
= new wxStaticText( this, wxID_ANY
, wxEmptyString
);
102 m_macValueStatic
= new wxStaticText( this, wxID_ANY
, wxEmptyString
);
103 SetRange(minValue
, maxValue
);
108 m_macMinimumStatic
= NULL
;
109 m_macMaximumStatic
= NULL
;
110 m_macValueStatic
= NULL
;
113 if(style
& wxSL_VERTICAL
) {
114 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
117 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
119 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
120 // proper dimensions, it also means other people cannot bugger the slider with
123 MacPostControlCreate() ;
128 wxSlider::~wxSlider()
132 int wxSlider::GetValue() const
134 return GetControl32BitValue( (ControlHandle
) m_macControl
) ;
137 void wxSlider::SetValue(int value
)
139 wxString valuestring
;
140 valuestring
.Printf( wxT("%d") , value
) ;
141 if ( m_macValueStatic
)
142 m_macValueStatic
->SetLabel( valuestring
) ;
143 SetControl32BitValue( (ControlHandle
) m_macControl
, value
) ;
146 void wxSlider::SetRange(int minValue
, int maxValue
)
150 m_rangeMin
= minValue
;
151 m_rangeMax
= maxValue
;
153 SetControl32BitMinimum( (ControlHandle
) m_macControl
, m_rangeMin
);
154 SetControl32BitMaximum( (ControlHandle
) m_macControl
, m_rangeMax
);
156 if(m_macMinimumStatic
) {
157 value
.Printf(wxT("%d"), m_rangeMin
);
158 m_macMinimumStatic
->SetLabel(value
);
160 if(m_macMaximumStatic
) {
161 value
.Printf(wxT("%d"), m_rangeMax
);
162 m_macMaximumStatic
->SetLabel(value
);
164 SetValue(m_rangeMin
);
167 // For trackbars only
168 void wxSlider::SetTickFreq(int n
, int pos
)
174 void wxSlider::SetPageSize(int pageSize
)
177 m_pageSize
= pageSize
;
180 int wxSlider::GetPageSize() const
185 void wxSlider::ClearSel()
190 void wxSlider::ClearTicks()
195 void wxSlider::SetLineSize(int lineSize
)
197 m_lineSize
= lineSize
;
201 int wxSlider::GetLineSize() const
207 int wxSlider::GetSelEnd() const
213 int wxSlider::GetSelStart() const
219 void wxSlider::SetSelection(int minPos
, int maxPos
)
224 void wxSlider::SetThumbLength(int len
)
229 int wxSlider::GetThumbLength() const
235 void wxSlider::SetTick(int tickPos
)
240 void wxSlider::Command (wxCommandEvent
& event
)
242 SetValue (event
.GetInt());
243 ProcessCommand (event
);
246 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
248 SInt16 value
= ::GetControl32BitValue( (ControlHandle
) m_macControl
) ;
252 wxEventType scrollEvent
= wxEVT_NULL
;
254 if ( mouseStillDown
)
255 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
257 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
259 wxScrollEvent
event(scrollEvent
, m_windowId
);
260 event
.SetPosition(value
);
261 event
.SetEventObject( this );
262 GetEventHandler()->ProcessEvent(event
);
264 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
265 cevent
.SetInt( value
);
266 cevent
.SetEventObject( this );
268 GetEventHandler()->ProcessEvent( cevent
);
271 /* This is overloaded in wxSlider so that the proper width/height will always be used
272 * for the slider different values would cause redrawing and mouse detection problems */
273 void wxSlider::DoSetSizeHints( int minW
, int minH
,
274 int maxW
, int maxH
,
275 int incW
, int incH
)
277 wxSize size
= GetBestSize();
279 if(GetWindowStyle() & wxSL_VERTICAL
) {
280 wxWindow::DoSetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
283 wxWindow::DoSetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
287 wxSize
wxSlider::DoGetBestSize() const
290 int textwidth
, textheight
;
292 if(GetWindowStyle() & wxSL_LABELS
)
297 // Get maximum text label width and height
298 text
.Printf(wxT("%d"), m_rangeMin
);
299 GetTextExtent(text
, &textwidth
, &textheight
);
300 text
.Printf(wxT("%d"), m_rangeMax
);
301 GetTextExtent(text
, &wd
, &ht
);
302 if(ht
> textheight
) {
305 if (wd
> textwidth
) {
310 if(GetWindowStyle() & wxSL_VERTICAL
)
312 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
313 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
316 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
318 if(GetWindowStyle() & wxSL_LABELS
) {
319 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
325 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
326 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
329 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
331 if(GetWindowStyle() & wxSL_LABELS
) {
332 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
339 void wxSlider::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
341 wxControl::DoSetSize( x
, y
, width
, height
,sizeFlags
) ;
344 void wxSlider::MacUpdateDimensions()
346 // actually in the current systems this should never be possible, but later reparenting
347 // may become a reality
349 if ( (ControlHandle
) m_macControl
== NULL
)
352 if ( GetParent() == NULL
)
355 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
356 if ( rootwindow
== NULL
)
359 int xborder
, yborder
;
360 int minValWidth
, maxValWidth
, textwidth
, textheight
;
363 xborder
= yborder
= 0;
365 if (GetWindowStyle() & wxSL_LABELS
)
370 // Get maximum text label width and height
371 text
.Printf(wxT("%d"), m_rangeMin
);
372 GetTextExtent(text
, &minValWidth
, &textheight
);
373 text
.Printf(wxT("%d"), m_rangeMax
);
374 GetTextExtent(text
, &maxValWidth
, &ht
);
375 if(ht
> textheight
) {
378 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
380 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
381 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
383 // Get slider breadth
384 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
385 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
388 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
391 if(GetWindowStyle() & wxSL_VERTICAL
)
393 m_macMinimumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
,
394 m_height
- yborder
- textheight
);
395 m_macMaximumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
, 0);
396 m_macValueStatic
->Move(0, m_height
- textheight
);
400 m_macMinimumStatic
->Move(0, sliderBreadth
+ wxSLIDER_BORDERTEXT
);
401 m_macMaximumStatic
->Move(m_width
- xborder
- maxValWidth
/ 2,
402 sliderBreadth
+ wxSLIDER_BORDERTEXT
);
403 m_macValueStatic
->Move(m_width
- textwidth
, 0);
408 GetControlBounds( (ControlHandle
) m_macControl
, &oldBounds
) ;
410 int new_x
= m_x
+ MacGetLeftBorderSize() + m_macHorizontalBorder
;
411 int new_y
= m_y
+ MacGetTopBorderSize() + m_macVerticalBorder
;
412 int new_width
= m_width
- MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder
- xborder
;
413 int new_height
= m_height
- MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder
- yborder
;
415 GetParent()->MacWindowToRootWindow( & new_x
, & new_y
) ;
416 bool doMove
= new_x
!= oldBounds
.left
|| new_y
!= oldBounds
.top
;
417 bool doResize
= ( oldBounds
.right
- oldBounds
.left
) != new_width
|| (oldBounds
.bottom
- oldBounds
.top
) != new_height
;
418 if ( doMove
|| doResize
)
420 InvalWindowRect( rootwindow
, &oldBounds
) ;
423 UMAMoveControl( (ControlHandle
) m_macControl
, new_x
, new_y
) ;
427 UMASizeControl( (ControlHandle
) m_macControl
, new_width
, new_height
) ;
432 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
434 wxControl::DoMoveWindow(x
,y
,width
,height
) ;
437 #endif // wxUSE_SLIDER