1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #include "wx/slider.h"
17 #include "wx/mac/uma.h"
19 IMPLEMENT_DYNAMIC_CLASS(wxSlider
, wxControl
)
21 BEGIN_EVENT_TABLE(wxSlider
, wxControl
)
24 // The dimensions of the different styles of sliders (From Aqua document)
25 #define wxSLIDER_DIMENSIONACROSS 15
26 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
27 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
29 // Distance between slider and text
30 #define wxSLIDER_BORDERTEXT 5
32 /* NB! The default orientation for a slider is horizontal however if the user specifies
33 * some slider styles but dosen't specify the orientation we have to assume he wants a
34 * horizontal one. Therefore in this file when testing for the sliders orientation
35 * vertical is tested for if this is not set then we use the horizontal one
36 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
49 extern ControlActionUPP wxMacLiveScrollbarActionUPP
;
51 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
52 int value
, int minValue
, int maxValue
,
54 const wxSize
& size
, long style
,
55 const wxValidator
& validator
,
58 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
65 m_macMinimumStatic
= NULL
;
66 m_macMaximumStatic
= NULL
;
67 m_macValueStatic
= NULL
;
73 m_rangeMax
= maxValue
;
74 m_rangeMin
= minValue
;
76 m_pageSize
= (int)((maxValue
-minValue
)/10);
78 MacPreControlCreate( parent
, id
, wxEmptyString
, pos
, size
, style
,
79 validator
, name
, &bounds
, title
);
81 procID
= kControlSliderProc
+ kControlSliderLiveFeedback
;
82 if(style
& wxSL_AUTOTICKS
) {
83 procID
+= kControlSliderHasTickMarks
;
87 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, title
, false,
88 value
, minValue
, maxValue
, procID
, (long) this);
90 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
92 ::SetControlAction( (ControlHandle
) m_macControl
, wxMacLiveScrollbarActionUPP
) ;
94 if(style
& wxSL_LABELS
)
96 m_macMinimumStatic
= new wxStaticText( this, wxID_ANY
, wxEmptyString
);
97 m_macMaximumStatic
= new wxStaticText( this, wxID_ANY
, wxEmptyString
);
98 m_macValueStatic
= new wxStaticText( this, wxID_ANY
, wxEmptyString
);
99 SetRange(minValue
, maxValue
);
104 m_macMinimumStatic
= NULL
;
105 m_macMaximumStatic
= NULL
;
106 m_macValueStatic
= NULL
;
109 if(style
& wxSL_VERTICAL
) {
110 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
113 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
115 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
116 // proper dimensions, it also means other people cannot bugger the slider with
119 MacPostControlCreate() ;
124 wxSlider::~wxSlider()
128 int wxSlider::GetValue() const
130 return GetControl32BitValue( (ControlHandle
) m_macControl
) ;
133 void wxSlider::SetValue(int value
)
135 wxString valuestring
;
136 valuestring
.Printf( wxT("%d") , value
) ;
137 if ( m_macValueStatic
)
138 m_macValueStatic
->SetLabel( valuestring
) ;
139 SetControl32BitValue( (ControlHandle
) m_macControl
, value
) ;
142 void wxSlider::SetRange(int minValue
, int maxValue
)
146 m_rangeMin
= minValue
;
147 m_rangeMax
= maxValue
;
149 SetControl32BitMinimum( (ControlHandle
) m_macControl
, m_rangeMin
);
150 SetControl32BitMaximum( (ControlHandle
) m_macControl
, m_rangeMax
);
152 if(m_macMinimumStatic
) {
153 value
.Printf(wxT("%d"), m_rangeMin
);
154 m_macMinimumStatic
->SetLabel(value
);
156 if(m_macMaximumStatic
) {
157 value
.Printf(wxT("%d"), m_rangeMax
);
158 m_macMaximumStatic
->SetLabel(value
);
160 SetValue(m_rangeMin
);
163 // For trackbars only
164 void wxSlider::SetTickFreq(int n
, int pos
)
170 void wxSlider::SetPageSize(int pageSize
)
173 m_pageSize
= pageSize
;
176 int wxSlider::GetPageSize() const
181 void wxSlider::ClearSel()
186 void wxSlider::ClearTicks()
191 void wxSlider::SetLineSize(int lineSize
)
193 m_lineSize
= lineSize
;
197 int wxSlider::GetLineSize() const
203 int wxSlider::GetSelEnd() const
209 int wxSlider::GetSelStart() const
215 void wxSlider::SetSelection(int minPos
, int maxPos
)
220 void wxSlider::SetThumbLength(int len
)
225 int wxSlider::GetThumbLength() const
231 void wxSlider::SetTick(int tickPos
)
236 void wxSlider::Command (wxCommandEvent
& event
)
238 SetValue (event
.GetInt());
239 ProcessCommand (event
);
242 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
244 SInt16 value
= ::GetControl32BitValue( (ControlHandle
) m_macControl
) ;
248 wxEventType scrollEvent
= wxEVT_NULL
;
250 if ( mouseStillDown
)
251 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
253 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
255 wxScrollEvent
event(scrollEvent
, m_windowId
);
256 event
.SetPosition(value
);
257 event
.SetEventObject( this );
258 GetEventHandler()->ProcessEvent(event
);
260 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
261 cevent
.SetInt( value
);
262 cevent
.SetEventObject( this );
264 GetEventHandler()->ProcessEvent( cevent
);
267 /* This is overloaded in wxSlider so that the proper width/height will always be used
268 * for the slider different values would cause redrawing and mouse detection problems */
269 void wxSlider::DoSetSizeHints( int minW
, int minH
,
270 int maxW
, int maxH
,
271 int incW
, int incH
)
273 wxSize size
= GetBestSize();
275 if(GetWindowStyle() & wxSL_VERTICAL
) {
276 wxWindow::DoSetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
279 wxWindow::DoSetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
283 wxSize
wxSlider::DoGetBestSize() const
286 int textwidth
, textheight
;
288 if(GetWindowStyle() & wxSL_LABELS
)
293 // Get maximum text label width and height
294 text
.Printf(wxT("%d"), m_rangeMin
);
295 GetTextExtent(text
, &textwidth
, &textheight
);
296 text
.Printf(wxT("%d"), m_rangeMax
);
297 GetTextExtent(text
, &wd
, &ht
);
298 if(ht
> textheight
) {
301 if (wd
> textwidth
) {
306 if(GetWindowStyle() & wxSL_VERTICAL
)
308 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
309 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
312 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
314 if(GetWindowStyle() & wxSL_LABELS
) {
315 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
321 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
322 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
325 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
327 if(GetWindowStyle() & wxSL_LABELS
) {
328 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
335 void wxSlider::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
337 wxControl::DoSetSize( x
, y
, width
, height
,sizeFlags
) ;
340 void wxSlider::MacUpdateDimensions()
342 // actually in the current systems this should never be possible, but later reparenting
343 // may become a reality
345 if ( (ControlHandle
) m_macControl
== NULL
)
348 if ( GetParent() == NULL
)
351 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
352 if ( rootwindow
== NULL
)
355 int xborder
, yborder
;
356 int minValWidth
, maxValWidth
, textwidth
, textheight
;
359 xborder
= yborder
= 0;
361 if (GetWindowStyle() & wxSL_LABELS
)
366 // Get maximum text label width and height
367 text
.Printf(wxT("%d"), m_rangeMin
);
368 GetTextExtent(text
, &minValWidth
, &textheight
);
369 text
.Printf(wxT("%d"), m_rangeMax
);
370 GetTextExtent(text
, &maxValWidth
, &ht
);
371 if(ht
> textheight
) {
374 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
376 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
377 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
379 // Get slider breadth
380 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
381 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
384 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
387 if(GetWindowStyle() & wxSL_VERTICAL
)
389 m_macMinimumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
,
390 m_height
- yborder
- textheight
);
391 m_macMaximumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
, 0);
392 m_macValueStatic
->Move(0, m_height
- textheight
);
396 m_macMinimumStatic
->Move(0, sliderBreadth
+ wxSLIDER_BORDERTEXT
);
397 m_macMaximumStatic
->Move(m_width
- xborder
- maxValWidth
/ 2,
398 sliderBreadth
+ wxSLIDER_BORDERTEXT
);
399 m_macValueStatic
->Move(m_width
- textwidth
, 0);
404 GetControlBounds( (ControlHandle
) m_macControl
, &oldBounds
) ;
406 int new_x
= m_x
+ MacGetLeftBorderSize() + m_macHorizontalBorder
;
407 int new_y
= m_y
+ MacGetTopBorderSize() + m_macVerticalBorder
;
408 int new_width
= m_width
- MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder
- xborder
;
409 int new_height
= m_height
- MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder
- yborder
;
411 GetParent()->MacWindowToRootWindow( & new_x
, & new_y
) ;
412 bool doMove
= new_x
!= oldBounds
.left
|| new_y
!= oldBounds
.top
;
413 bool doResize
= ( oldBounds
.right
- oldBounds
.left
) != new_width
|| (oldBounds
.bottom
- oldBounds
.top
) != new_height
;
414 if ( doMove
|| doResize
)
416 InvalWindowRect( rootwindow
, &oldBounds
) ;
419 UMAMoveControl( (ControlHandle
) m_macControl
, new_x
, new_y
) ;
423 UMASizeControl( (ControlHandle
) m_macControl
, new_width
, new_height
) ;
428 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
430 wxControl::DoMoveWindow(x
,y
,width
,height
) ;
433 #endif // wxUSE_SLIDER