1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
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 }>
52 extern ControlActionUPP wxMacLiveScrollbarActionUPP
;
54 bool wxSlider::Create(wxWindow
*parent
, wxWindowID id
,
55 int value
, int minValue
, int maxValue
,
57 const wxSize
& size
, long style
,
58 const wxValidator
& validator
,
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
, "", pos
, size
, style
,
79 validator
, name
, &bounds
, title
);
81 procID
= kControlSliderProc
+ kControlSliderLiveFeedback
;
82 if(style
& wxSL_AUTOTICKS
) {
83 procID
+= kControlSliderHasTickMarks
;
87 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, title
, false,
88 value
, minValue
, maxValue
, procID
, (long) this);
90 wxASSERT_MSG( (ControlHandle
) m_macControl
!= NULL
, "No valid mac control" ) ;
92 ::SetControlAction( (ControlHandle
) m_macControl
, wxMacLiveScrollbarActionUPP
) ;
94 if(style
& wxSL_LABELS
)
96 m_macMinimumStatic
= new wxStaticText( this, -1, "" );
97 m_macMaximumStatic
= new wxStaticText( this, -1, "" );
98 m_macValueStatic
= new wxStaticText( this, -1, "" );
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( "%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("%d", m_rangeMin
);
154 m_macMinimumStatic
->SetLabel(value
);
156 if(m_macMaximumStatic
) {
157 value
.Printf("%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
)
244 SInt16 value
= ::GetControl32BitValue( (ControlHandle
) m_macControl
) ;
248 wxScrollEvent
event(wxEVT_SCROLL_THUMBTRACK
, m_windowId
);
249 event
.SetPosition(value
);
250 event
.SetEventObject( this );
251 GetEventHandler()->ProcessEvent(event
);
253 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
254 cevent
.SetInt( value
);
255 cevent
.SetEventObject( this );
257 GetEventHandler()->ProcessEvent( cevent
);
260 /* This is overloaded in wxSlider so that the proper width/height will always be used
261 * for the slider different values would cause redrawing and mouse detection problems */
262 void wxSlider::SetSizeHints( int minW
, int minH
,
263 int maxW
, int maxH
,
264 int incW
, int incH
)
266 wxSize size
= GetBestSize();
268 if(GetWindowStyle() & wxSL_VERTICAL
) {
269 wxWindow::SetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
272 wxWindow::SetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
276 wxSize
wxSlider::DoGetBestSize() const
279 int textwidth
, textheight
;
281 if(GetWindowStyle() & wxSL_LABELS
)
286 // Get maximum text label width and height
287 text
.Printf("%d", m_rangeMin
);
288 GetTextExtent(text
, &textwidth
, &textheight
);
289 text
.Printf("%d", m_rangeMax
);
290 GetTextExtent(text
, &wd
, &ht
);
291 if(ht
> textheight
) {
294 if (wd
> textwidth
) {
299 if(GetWindowStyle() & wxSL_VERTICAL
)
301 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
302 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
305 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
307 if(GetWindowStyle() & wxSL_LABELS
) {
308 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
314 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
315 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
318 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
320 if(GetWindowStyle() & wxSL_LABELS
) {
321 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
328 void wxSlider::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
330 wxControl::DoSetSize( x
, y
, width
, height
,sizeFlags
) ;
333 void wxSlider::MacUpdateDimensions()
335 // actually in the current systems this should never be possible, but later reparenting
336 // may become a reality
338 if ( (ControlHandle
) m_macControl
== NULL
)
341 if ( GetParent() == NULL
)
344 WindowRef rootwindow
= (WindowRef
) MacGetRootWindow() ;
345 if ( rootwindow
== NULL
)
348 int xborder
, yborder
;
349 int minValWidth
, maxValWidth
, textwidth
, textheight
;
352 xborder
= yborder
= 0;
354 if (GetWindowStyle() & wxSL_LABELS
)
359 // Get maximum text label width and height
360 text
.Printf("%d", m_rangeMin
);
361 GetTextExtent(text
, &minValWidth
, &textheight
);
362 text
.Printf("%d", m_rangeMax
);
363 GetTextExtent(text
, &maxValWidth
, &ht
);
364 if(ht
> textheight
) {
367 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
369 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
370 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
372 // Get slider breadth
373 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
374 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
377 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
380 if(GetWindowStyle() & wxSL_VERTICAL
)
382 m_macMinimumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
,
383 m_height
- yborder
- textheight
);
384 m_macMaximumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
, 0);
385 m_macValueStatic
->Move(0, m_height
- textheight
);
389 m_macMinimumStatic
->Move(0, sliderBreadth
+ wxSLIDER_BORDERTEXT
);
390 m_macMaximumStatic
->Move(m_width
- xborder
- maxValWidth
/ 2,
391 sliderBreadth
+ wxSLIDER_BORDERTEXT
);
392 m_macValueStatic
->Move(m_width
- textwidth
, 0);
397 GetControlBounds( (ControlHandle
) m_macControl
, &oldBounds
) ;
399 int new_x
= m_x
+ MacGetLeftBorderSize() + m_macHorizontalBorder
;
400 int new_y
= m_y
+ MacGetTopBorderSize() + m_macVerticalBorder
;
401 int new_width
= m_width
- MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder
- xborder
;
402 int new_height
= m_height
- MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder
- yborder
;
404 GetParent()->MacWindowToRootWindow( & new_x
, & new_y
) ;
405 bool doMove
= new_x
!= oldBounds
.left
|| new_y
!= oldBounds
.top
;
406 bool doResize
= ( oldBounds
.right
- oldBounds
.left
) != new_width
|| (oldBounds
.bottom
- oldBounds
.top
) != new_height
;
407 if ( doMove
|| doResize
)
409 InvalWindowRect( rootwindow
, &oldBounds
) ;
412 UMAMoveControl( (ControlHandle
) m_macControl
, new_x
, new_y
) ;
416 UMASizeControl( (ControlHandle
) m_macControl
, new_width
, new_height
) ;
421 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
423 wxControl::DoMoveWindow(x
,y
,width
,height
) ;