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 m_macIsUserPane
= FALSE
;
62 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 Rect bounds
= wxMacGetBoundsForControl( this , pos
, size
) ;
82 UInt16 tickMarks
= 0 ;
83 if ( style
& wxSL_AUTOTICKS
)
84 tickMarks
= maxValue
- minValue
;
86 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()) , &bounds
,
87 value
, minValue
, maxValue
, kControlSliderPointsDownOrRight
, tickMarks
, true /* liveTracking */ ,
88 wxMacLiveScrollbarActionUPP
, (ControlRef
*) &m_macControl
) ) ;
90 if(style
& wxSL_VERTICAL
) {
91 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
94 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
96 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
97 // proper dimensions, it also means other people cannot bugger the slider with
100 if(style
& wxSL_LABELS
)
102 m_macMinimumStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
103 m_macMaximumStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
104 m_macValueStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
105 SetRange(minValue
, maxValue
);
109 MacPostControlCreate(pos
,size
) ;
114 wxSlider::~wxSlider()
116 delete m_macMinimumStatic
;
117 delete m_macMaximumStatic
;
118 delete m_macValueStatic
;
121 int wxSlider::GetValue() const
123 return GetControl32BitValue( (ControlRef
) m_macControl
) ;
126 void wxSlider::SetValue(int value
)
128 wxString valuestring
;
129 valuestring
.Printf( wxT("%d") , value
) ;
130 if ( m_macValueStatic
)
131 m_macValueStatic
->SetLabel( valuestring
) ;
132 SetControl32BitValue( (ControlRef
) m_macControl
, value
) ;
135 void wxSlider::SetRange(int minValue
, int maxValue
)
139 m_rangeMin
= minValue
;
140 m_rangeMax
= maxValue
;
142 SetControl32BitMinimum( (ControlRef
) m_macControl
, m_rangeMin
);
143 SetControl32BitMaximum( (ControlRef
) m_macControl
, m_rangeMax
);
145 if(m_macMinimumStatic
) {
146 value
.Printf(wxT("%d"), m_rangeMin
);
147 m_macMinimumStatic
->SetLabel(value
);
149 if(m_macMaximumStatic
) {
150 value
.Printf(wxT("%d"), m_rangeMax
);
151 m_macMaximumStatic
->SetLabel(value
);
153 SetValue(m_rangeMin
);
156 // For trackbars only
157 void wxSlider::SetTickFreq(int n
, int pos
)
163 void wxSlider::SetPageSize(int pageSize
)
166 m_pageSize
= pageSize
;
169 int wxSlider::GetPageSize() const
174 void wxSlider::ClearSel()
179 void wxSlider::ClearTicks()
184 void wxSlider::SetLineSize(int lineSize
)
186 m_lineSize
= lineSize
;
190 int wxSlider::GetLineSize() const
196 int wxSlider::GetSelEnd() const
202 int wxSlider::GetSelStart() const
208 void wxSlider::SetSelection(int minPos
, int maxPos
)
213 void wxSlider::SetThumbLength(int len
)
218 int wxSlider::GetThumbLength() const
224 void wxSlider::SetTick(int tickPos
)
229 void wxSlider::Command (wxCommandEvent
& event
)
231 SetValue (event
.GetInt());
232 ProcessCommand (event
);
235 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
237 SInt16 value
= ::GetControl32BitValue( (ControlRef
) m_macControl
) ;
241 wxEventType scrollEvent
= wxEVT_NULL
;
243 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
245 wxScrollEvent
event(scrollEvent
, m_windowId
);
246 event
.SetPosition(value
);
247 event
.SetEventObject( this );
248 GetEventHandler()->ProcessEvent(event
);
250 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
251 cevent
.SetInt( value
);
252 cevent
.SetEventObject( this );
254 GetEventHandler()->ProcessEvent( cevent
);
257 wxInt32
wxSlider::MacControlHit( WXEVENTHANDLERREF handler
, WXEVENTREF mevent
)
259 SInt16 value
= ::GetControl32BitValue( (ControlRef
) m_macControl
) ;
263 wxEventType scrollEvent
= wxEVT_NULL
;
265 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
267 wxScrollEvent
event(scrollEvent
, m_windowId
);
268 event
.SetPosition(value
);
269 event
.SetEventObject( this );
270 GetEventHandler()->ProcessEvent(event
);
272 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
273 cevent
.SetInt( value
);
274 cevent
.SetEventObject( this );
276 GetEventHandler()->ProcessEvent( cevent
);
281 /* This is overloaded in wxSlider so that the proper width/height will always be used
282 * for the slider different values would cause redrawing and mouse detection problems */
283 void wxSlider::SetSizeHints( int minW
, int minH
,
284 int maxW
, int maxH
,
285 int incW
, int incH
)
287 wxSize size
= GetBestSize();
289 if(GetWindowStyle() & wxSL_VERTICAL
) {
290 wxWindow::SetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
293 wxWindow::SetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
297 wxSize
wxSlider::DoGetBestSize() const
300 int textwidth
, textheight
;
302 if(GetWindowStyle() & wxSL_LABELS
)
307 // Get maximum text label width and height
308 text
.Printf(wxT("%d"), m_rangeMin
);
309 GetTextExtent(text
, &textwidth
, &textheight
);
310 text
.Printf(wxT("%d"), m_rangeMax
);
311 GetTextExtent(text
, &wd
, &ht
);
312 if(ht
> textheight
) {
315 if (wd
> textwidth
) {
320 if(GetWindowStyle() & wxSL_VERTICAL
)
322 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
323 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
326 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
328 if(GetWindowStyle() & wxSL_LABELS
) {
329 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
335 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
336 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
339 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
341 if(GetWindowStyle() & wxSL_LABELS
) {
342 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
349 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
351 int xborder
, yborder
;
352 int minValWidth
, maxValWidth
, textwidth
, textheight
;
355 xborder
= yborder
= 0;
357 if (GetWindowStyle() & wxSL_LABELS
)
362 // Get maximum text label width and height
363 text
.Printf(wxT("%d"), m_rangeMin
);
364 GetTextExtent(text
, &minValWidth
, &textheight
);
365 text
.Printf(wxT("%d"), m_rangeMax
);
366 GetTextExtent(text
, &maxValWidth
, &ht
);
367 if(ht
> textheight
) {
370 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
372 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
373 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
375 // Get slider breadth
376 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
377 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
380 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
383 if(GetWindowStyle() & wxSL_VERTICAL
)
386 if ( m_macMinimumStatic
)
387 m_macMinimumStatic
->Move(x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
,
388 y
+ h
- yborder
- textheight
);
389 if ( m_macMaximumStatic
)
390 m_macMaximumStatic
->Move(x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, y
+ 0);
391 if ( m_macValueStatic
)
392 m_macValueStatic
->Move(0, y
+ h
- textheight
);
397 if ( m_macMinimumStatic
)
398 m_macMinimumStatic
->Move(x
+ 0, y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
399 if ( m_macMaximumStatic
)
400 m_macMaximumStatic
->Move(x
+ w
- xborder
- maxValWidth
/ 2,
401 y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
402 if ( m_macValueStatic
)
403 m_macValueStatic
->Move(x
+ w
- textwidth
, y
+ 0);
408 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
411 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
413 wxControl::DoMoveWindow(x
,y
,width
,height
) ;