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 procID
= kControlSliderProc
+ kControlSliderLiveFeedback
;
83 if(style
& wxSL_AUTOTICKS
) {
84 procID
+= kControlSliderHasTickMarks
;
88 m_macControl
= (WXWidget
) ::NewControl( MAC_WXHWND(parent
->MacGetTopLevelWindowRef()), &bounds
, "\p", true,
89 value
, minValue
, maxValue
, procID
, (long) this);
91 wxASSERT_MSG( (ControlRef
) m_macControl
!= NULL
, wxT("No valid mac control") ) ;
93 ::SetControlAction( (ControlRef
) m_macControl
, wxMacLiveScrollbarActionUPP
) ;
95 if(style
& wxSL_VERTICAL
) {
96 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
99 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
101 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
102 // proper dimensions, it also means other people cannot bugger the slider with
105 if(style
& wxSL_LABELS
)
107 m_macMinimumStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
108 m_macMaximumStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
109 m_macValueStatic
= new wxStaticText( parent
, -1, wxEmptyString
);
110 SetRange(minValue
, maxValue
);
114 MacPostControlCreate(pos
,size
) ;
119 wxSlider::~wxSlider()
121 delete m_macMinimumStatic
;
122 delete m_macMaximumStatic
;
123 delete m_macValueStatic
;
126 int wxSlider::GetValue() const
128 return GetControl32BitValue( (ControlRef
) m_macControl
) ;
131 void wxSlider::SetValue(int value
)
133 wxString valuestring
;
134 valuestring
.Printf( wxT("%d") , value
) ;
135 if ( m_macValueStatic
)
136 m_macValueStatic
->SetLabel( valuestring
) ;
137 SetControl32BitValue( (ControlRef
) m_macControl
, value
) ;
140 void wxSlider::SetRange(int minValue
, int maxValue
)
144 m_rangeMin
= minValue
;
145 m_rangeMax
= maxValue
;
147 SetControl32BitMinimum( (ControlRef
) m_macControl
, m_rangeMin
);
148 SetControl32BitMaximum( (ControlRef
) m_macControl
, m_rangeMax
);
150 if(m_macMinimumStatic
) {
151 value
.Printf(wxT("%d"), m_rangeMin
);
152 m_macMinimumStatic
->SetLabel(value
);
154 if(m_macMaximumStatic
) {
155 value
.Printf(wxT("%d"), m_rangeMax
);
156 m_macMaximumStatic
->SetLabel(value
);
158 SetValue(m_rangeMin
);
161 // For trackbars only
162 void wxSlider::SetTickFreq(int n
, int pos
)
168 void wxSlider::SetPageSize(int pageSize
)
171 m_pageSize
= pageSize
;
174 int wxSlider::GetPageSize() const
179 void wxSlider::ClearSel()
184 void wxSlider::ClearTicks()
189 void wxSlider::SetLineSize(int lineSize
)
191 m_lineSize
= lineSize
;
195 int wxSlider::GetLineSize() const
201 int wxSlider::GetSelEnd() const
207 int wxSlider::GetSelStart() const
213 void wxSlider::SetSelection(int minPos
, int maxPos
)
218 void wxSlider::SetThumbLength(int len
)
223 int wxSlider::GetThumbLength() const
229 void wxSlider::SetTick(int tickPos
)
234 void wxSlider::Command (wxCommandEvent
& event
)
236 SetValue (event
.GetInt());
237 ProcessCommand (event
);
240 void wxSlider::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
, bool mouseStillDown
)
242 SInt16 value
= ::GetControl32BitValue( (ControlRef
) m_macControl
) ;
246 wxEventType scrollEvent
= wxEVT_NULL
;
248 if ( mouseStillDown
)
249 scrollEvent
= wxEVT_SCROLL_THUMBTRACK
;
251 scrollEvent
= wxEVT_SCROLL_THUMBRELEASE
;
253 wxScrollEvent
event(scrollEvent
, m_windowId
);
254 event
.SetPosition(value
);
255 event
.SetEventObject( this );
256 GetEventHandler()->ProcessEvent(event
);
258 wxCommandEvent
cevent( wxEVT_COMMAND_SLIDER_UPDATED
, m_windowId
);
259 cevent
.SetInt( value
);
260 cevent
.SetEventObject( this );
262 GetEventHandler()->ProcessEvent( cevent
);
265 /* This is overloaded in wxSlider so that the proper width/height will always be used
266 * for the slider different values would cause redrawing and mouse detection problems */
267 void wxSlider::SetSizeHints( int minW
, int minH
,
268 int maxW
, int maxH
,
269 int incW
, int incH
)
271 wxSize size
= GetBestSize();
273 if(GetWindowStyle() & wxSL_VERTICAL
) {
274 wxWindow::SetSizeHints(size
.x
, minH
, size
.x
, maxH
, incW
, incH
);
277 wxWindow::SetSizeHints(minW
, size
.y
, maxW
, size
.y
, incW
, incH
);
281 wxSize
wxSlider::DoGetBestSize() const
284 int textwidth
, textheight
;
286 if(GetWindowStyle() & wxSL_LABELS
)
291 // Get maximum text label width and height
292 text
.Printf(wxT("%d"), m_rangeMin
);
293 GetTextExtent(text
, &textwidth
, &textheight
);
294 text
.Printf(wxT("%d"), m_rangeMax
);
295 GetTextExtent(text
, &wd
, &ht
);
296 if(ht
> textheight
) {
299 if (wd
> textwidth
) {
304 if(GetWindowStyle() & wxSL_VERTICAL
)
306 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
307 size
.x
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
310 size
.x
= wxSLIDER_DIMENSIONACROSS_ARROW
;
312 if(GetWindowStyle() & wxSL_LABELS
) {
313 size
.x
+= textwidth
+ wxSLIDER_BORDERTEXT
;
319 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
320 size
.y
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
323 size
.y
= wxSLIDER_DIMENSIONACROSS_ARROW
;
325 if(GetWindowStyle() & wxSL_LABELS
) {
326 size
.y
+= textheight
+ wxSLIDER_BORDERTEXT
;
333 void wxSlider::DoSetSize(int x
, int y
, int w
, int h
, int sizeFlags
)
335 int xborder
, yborder
;
336 int minValWidth
, maxValWidth
, textwidth
, textheight
;
339 xborder
= yborder
= 0;
341 if (GetWindowStyle() & wxSL_LABELS
)
346 // Get maximum text label width and height
347 text
.Printf(wxT("%d"), m_rangeMin
);
348 GetTextExtent(text
, &minValWidth
, &textheight
);
349 text
.Printf(wxT("%d"), m_rangeMax
);
350 GetTextExtent(text
, &maxValWidth
, &ht
);
351 if(ht
> textheight
) {
354 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
356 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
357 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
359 // Get slider breadth
360 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
361 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
364 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
367 if(GetWindowStyle() & wxSL_VERTICAL
)
370 if ( m_macMinimumStatic
)
371 m_macMinimumStatic
->Move(x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
,
372 y
+ h
- yborder
- textheight
);
373 if ( m_macMaximumStatic
)
374 m_macMaximumStatic
->Move(x
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
, y
+ 0);
375 if ( m_macValueStatic
)
376 m_macValueStatic
->Move(0, y
+ h
- textheight
);
381 if ( m_macMinimumStatic
)
382 m_macMinimumStatic
->Move(x
+ 0, y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
383 if ( m_macMaximumStatic
)
384 m_macMaximumStatic
->Move(x
+ w
- xborder
- maxValWidth
/ 2,
385 y
+ sliderBreadth
+ wxSLIDER_BORDERTEXT
);
386 if ( m_macValueStatic
)
387 m_macValueStatic
->Move(x
+ w
- textwidth
, y
+ 0);
392 wxControl::DoSetSize( x
, y
, w
, h
,sizeFlags
) ;
395 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
397 wxControl::DoMoveWindow(x
,y
,width
,height
) ;