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( parent
->MacGetRootWindow(), &bounds
, title
, false,
88 value
, minValue
, maxValue
, procID
, (long) this);
90 wxASSERT_MSG( m_macControl
!= NULL
, "No valid mac control" ) ;
92 ::SetControlAction( 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 GetControlValue( 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 SetControlValue( m_macControl
, value
) ;
142 void wxSlider::SetRange(int minValue
, int maxValue
)
146 m_rangeMin
= minValue
;
147 m_rangeMax
= maxValue
;
149 SetControlMinimum(m_macControl
, m_rangeMin
);
150 SetControlMaximum(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( ControlHandle control
, SInt16 controlpart
)
244 SInt16 value
= ::GetControlValue( 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 Rect oldbounds
, newbounds
;
331 int new_x
, new_y
, new_width
, new_height
;
337 new_height
= m_height
;
339 if (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
)
348 if (x
!= -1) new_x
= x
;
349 if (y
!= -1) new_y
= y
;
350 if (width
!= -1) new_width
= width
;
351 if (height
!= -1) new_height
= height
;
354 if(sizeFlags
& wxSIZE_AUTO
)
356 wxSize size
= GetBestSize();
357 if (sizeFlags
& wxSIZE_AUTO_WIDTH
)
359 if (width
== -1) new_width
= size
.x
;
361 if (sizeFlags
& wxSIZE_AUTO_HEIGHT
)
363 if (height
== -1) new_height
= size
.y
;
367 AdjustForParentClientOrigin(new_x
, new_y
, sizeFlags
);
372 GetParent()->MacClientToRootWindow(&mac_x
, &mac_y
);
375 GetControlBounds(m_macControl
, &oldbounds
);
376 oldbounds
.right
= oldbounds
.left
+ m_width
;
377 oldbounds
.bottom
= oldbounds
.top
+ m_height
;
380 bool doResize
= false;
382 if ( mac_x
!= oldbounds
.left
|| mac_y
!= oldbounds
.top
)
386 if ( new_width
!= m_width
|| new_height
!= m_height
)
391 if ( doMove
|| doResize
)
393 // Ensure resize is within constraints
394 if ((m_minWidth
!= -1) && (new_width
< m_minWidth
)) {
395 new_width
= m_minWidth
;
397 if ((m_minHeight
!= -1) && (new_height
< m_minHeight
)) {
398 new_height
= m_minHeight
;
400 if ((m_maxWidth
!= -1) && (new_width
> m_maxWidth
)) {
401 new_width
= m_maxWidth
;
403 if ((m_maxHeight
!= -1) && (new_height
> m_maxHeight
)) {
404 new_height
= m_maxHeight
;
407 DoMoveWindow(new_x
, new_y
, new_width
, new_height
);
409 // Update window at old and new positions
410 SetRect(&newbounds
, m_x
, m_y
, m_x
+ m_width
, m_y
+ m_height
);
411 WindowRef rootwindow
= MacGetRootWindow();
412 InvalWindowRect( rootwindow
, &oldbounds
);
413 InvalWindowRect( rootwindow
, &newbounds
);
417 wxMoveEvent
event(wxPoint(m_x
, m_y
), m_windowId
);
418 event
.SetEventObject(this);
419 GetEventHandler()->ProcessEvent(event
) ;
423 wxSizeEvent
event(wxSize(m_width
, m_height
), m_windowId
);
424 event
.SetEventObject(this);
425 GetEventHandler()->ProcessEvent(event
);
430 void wxSlider::DoMoveWindow(int x
, int y
, int width
, int height
)
437 int xborder
, yborder
;
438 int minValWidth
, maxValWidth
, textwidth
, textheight
;
441 xborder
= yborder
= 0;
443 if (GetWindowStyle() & wxSL_LABELS
)
448 // Get maximum text label width and height
449 text
.Printf("%d", m_rangeMin
);
450 GetTextExtent(text
, &minValWidth
, &textheight
);
451 text
.Printf("%d", m_rangeMax
);
452 GetTextExtent(text
, &maxValWidth
, &ht
);
453 if(ht
> textheight
) {
456 textwidth
= (minValWidth
> maxValWidth
? minValWidth
: maxValWidth
);
458 xborder
= textwidth
+ wxSLIDER_BORDERTEXT
;
459 yborder
= textheight
+ wxSLIDER_BORDERTEXT
;
461 // Get slider breadth
462 if(GetWindowStyle() & wxSL_AUTOTICKS
) {
463 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS
;
466 sliderBreadth
= wxSLIDER_DIMENSIONACROSS_ARROW
;
469 if(GetWindowStyle() & wxSL_VERTICAL
)
471 m_macMinimumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
,
472 height
- yborder
- textheight
);
473 m_macMaximumStatic
->Move(sliderBreadth
+ wxSLIDER_BORDERTEXT
, 0);
474 m_macValueStatic
->Move(0, height
- textheight
);
478 m_macMinimumStatic
->Move(0, sliderBreadth
+ wxSLIDER_BORDERTEXT
);
479 m_macMaximumStatic
->Move(width
- xborder
- maxValWidth
/ 2,
480 sliderBreadth
+ wxSLIDER_BORDERTEXT
);
481 m_macValueStatic
->Move(width
- textwidth
, 0);
486 GetParent()->MacClientToRootWindow(&x
, &y
);
488 UMAMoveControl(m_macControl
, x
, y
);
489 UMASizeControl(m_macControl
, width
- xborder
, height
- yborder
);