]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/slider.cpp
wxSYS_COLOUR_WINDOW is better for window backgrounds on PPC/Smartphone
[wxWidgets.git] / src / mac / carbon / slider.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: slider.cpp
3// Purpose: wxSlider
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "slider.h"
14#endif
15
3d1a4878 16#include "wx/wxprec.h"
312ebad4
WS
17
18#if wxUSE_SLIDER
19
e9576ca5 20#include "wx/slider.h"
519cb848 21#include "wx/mac/uma.h"
e9576ca5 22
2f1ae414 23#if !USE_SHARED_LIBRARY
e9576ca5
SC
24IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
25
26BEGIN_EVENT_TABLE(wxSlider, wxControl)
27END_EVENT_TABLE()
2f1ae414 28#endif
e9576ca5 29
9453cf2b 30 // The dimensions of the different styles of sliders (From Aqua document)
e40298d5
JS
31#define wxSLIDER_DIMENSIONACROSS 15
32#define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
33#define wxSLIDER_DIMENSIONACROSS_ARROW 18
312ebad4 34
e40298d5
JS
35// Distance between slider and text
36#define wxSLIDER_BORDERTEXT 5
312ebad4 37
e40298d5
JS
38/* NB! The default orientation for a slider is horizontal however if the user specifies
39 * some slider styles but dosen't specify the orientation we have to assume he wants a
40 * horizontal one. Therefore in this file when testing for the sliders orientation
41 * vertical is tested for if this is not set then we use the horizontal one
312ebad4 42 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
e40298d5
JS
43 */
44
45 // Slider
46 wxSlider::wxSlider()
e9576ca5 47{
e40298d5
JS
48 m_pageSize = 1;
49 m_lineSize = 1;
50 m_rangeMax = 0;
51 m_rangeMin = 0;
52 m_tickFreq = 0;
e9576ca5
SC
53}
54
55bool wxSlider::Create(wxWindow *parent, wxWindowID id,
e40298d5
JS
56 int value, int minValue, int maxValue,
57 const wxPoint& pos,
58 const wxSize& size, long style,
59 const wxValidator& validator,
60 const wxString& name)
e9576ca5 61{
312ebad4
WS
62 m_macIsUserPane = false ;
63
b45ed7a2
VZ
64 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
65 return false;
66
e40298d5
JS
67 m_macMinimumStatic = NULL ;
68 m_macMaximumStatic = NULL ;
69 m_macValueStatic = NULL ;
312ebad4 70
e40298d5
JS
71 m_lineSize = 1;
72 m_tickFreq = 0;
312ebad4 73
e40298d5
JS
74 m_rangeMax = maxValue;
75 m_rangeMin = minValue;
312ebad4 76
e40298d5 77 m_pageSize = (int)((maxValue-minValue)/10);
312ebad4
WS
78
79 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
80
344d4802
RN
81 //
82 // NB: (RN) Ticks here are sometimes off in the GUI if there
83 // is not as many ticks as there are values
84 //
4c37f124
SC
85 UInt16 tickMarks = 0 ;
86 if ( style & wxSL_AUTOTICKS )
344d4802 87 tickMarks = (maxValue - minValue) + 1; //+1 for the 0 value
312ebad4 88
344d4802 89 while (tickMarks > 20)
02812785 90 tickMarks /= 5; //keep the number of tickmarks from becoming unwieldly
312ebad4 91
b905d6cc 92 m_peer = new wxMacControl(this) ;
312ebad4 93 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds ,
02812785 94 value , minValue , maxValue , kControlSliderPointsDownOrRight , tickMarks , true /* liveTracking */ ,
cd780027 95 GetwxMacLiveScrollbarActionProc() , m_peer->GetControlRefAddr() ) );
312ebad4
WS
96
97
e40298d5
JS
98 if(style & wxSL_VERTICAL) {
99 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
100 }
101 else {
102 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
103 }
104 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
105 // proper dimensions, it also means other people cannot bugger the slider with
106 // other values
312ebad4 107
facd6764
SC
108 if(style & wxSL_LABELS)
109 {
312ebad4
WS
110 m_macMinimumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
111 m_macMaximumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
112 m_macValueStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
facd6764
SC
113 SetRange(minValue, maxValue);
114 SetValue(value);
115 }
116
117 MacPostControlCreate(pos,size) ;
312ebad4 118
e40298d5 119 return true;
e9576ca5
SC
120}
121
122wxSlider::~wxSlider()
123{
01526d4f 124 // this is a special case, as we had to add windows as siblings we are
33b35531
SC
125 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
126 if ( m_parent && m_parent->IsBeingDeleted() == false )
127 {
128 delete m_macMinimumStatic ;
129 delete m_macMaximumStatic ;
130 delete m_macValueStatic ;
131 }
e9576ca5
SC
132}
133
134int wxSlider::GetValue() const
135{
02812785 136 // We may need to invert the value returned by the widget
01526d4f 137 return ValueInvertOrNot( m_peer->GetValue() ) ;
e9576ca5
SC
138}
139
140void wxSlider::SetValue(int value)
141{
e40298d5 142 wxString valuestring ;
312ebad4 143 valuestring.Printf( wxT("%d") , value ) ;
e40298d5
JS
144 if ( m_macValueStatic )
145 m_macValueStatic->SetLabel( valuestring ) ;
02812785
KH
146
147 // We only invert for the setting of the actual native widget
01526d4f 148 m_peer->SetValue( ValueInvertOrNot ( value ) ) ;
e9576ca5
SC
149}
150
151void wxSlider::SetRange(int minValue, int maxValue)
152{
e40298d5 153 wxString value;
312ebad4 154
e40298d5
JS
155 m_rangeMin = minValue;
156 m_rangeMax = maxValue;
312ebad4 157
5ca0d812
SC
158 m_peer->SetMinimum( m_rangeMin);
159 m_peer->SetMaximum( m_rangeMax);
312ebad4 160
e40298d5 161 if(m_macMinimumStatic) {
01526d4f 162 value.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
e40298d5
JS
163 m_macMinimumStatic->SetLabel(value);
164 }
165 if(m_macMaximumStatic) {
01526d4f 166 value.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
e40298d5
JS
167 m_macMaximumStatic->SetLabel(value);
168 }
169 SetValue(m_rangeMin);
e9576ca5
SC
170}
171
172// For trackbars only
173void wxSlider::SetTickFreq(int n, int pos)
174{
175 // TODO
176 m_tickFreq = n;
177}
178
179void wxSlider::SetPageSize(int pageSize)
180{
181 // TODO
182 m_pageSize = pageSize;
183}
184
185int wxSlider::GetPageSize() const
186{
187 return m_pageSize;
188}
189
190void wxSlider::ClearSel()
191{
192 // TODO
193}
194
195void wxSlider::ClearTicks()
196{
197 // TODO
198}
199
200void wxSlider::SetLineSize(int lineSize)
201{
202 m_lineSize = lineSize;
203 // TODO
204}
205
206int wxSlider::GetLineSize() const
207{
208 // TODO
209 return 0;
210}
211
212int wxSlider::GetSelEnd() const
213{
214 // TODO
215 return 0;
216}
217
218int wxSlider::GetSelStart() const
219{
220 // TODO
221 return 0;
222}
223
224void wxSlider::SetSelection(int minPos, int maxPos)
225{
226 // TODO
227}
228
229void wxSlider::SetThumbLength(int len)
230{
231 // TODO
232}
233
234int wxSlider::GetThumbLength() const
235{
236 // TODO
237 return 0;
238}
239
240void wxSlider::SetTick(int tickPos)
241{
242 // TODO
243}
244
245void wxSlider::Command (wxCommandEvent & event)
246{
e40298d5
JS
247 SetValue (event.GetInt());
248 ProcessCommand (event);
e9576ca5
SC
249}
250
312ebad4 251void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown )
519cb848 252{
02812785
KH
253 // Whatever the native value is, we may need to invert it for calling
254 // SetValue and putting the possibly inverted value in the event
01526d4f 255 SInt16 value = ValueInvertOrNot ( m_peer->GetValue() ) ;
312ebad4
WS
256
257 SetValue( value ) ;
258
cea9c546 259 wxEventType scrollEvent = wxEVT_NULL ;
312ebad4 260
4c37f124 261 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
312ebad4 262
cea9c546 263 wxScrollEvent event(scrollEvent, m_windowId);
e40298d5
JS
264 event.SetPosition(value);
265 event.SetEventObject( this );
266 GetEventHandler()->ProcessEvent(event);
312ebad4 267
e40298d5
JS
268 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
269 cevent.SetInt( value );
270 cevent.SetEventObject( this );
312ebad4 271
e40298d5
JS
272 GetEventHandler()->ProcessEvent( cevent );
273}
274
312ebad4 275wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent )
4c37f124 276{
02812785
KH
277 // Whatever the native value is, we may need to invert it for calling
278 // SetValue and putting the possibly inverted value in the event
01526d4f 279 SInt16 value = ValueInvertOrNot ( m_peer->GetValue() ) ;
312ebad4
WS
280
281 SetValue( value ) ;
282
4c37f124 283 wxEventType scrollEvent = wxEVT_NULL ;
312ebad4 284
4c37f124 285 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
312ebad4 286
4c37f124
SC
287 wxScrollEvent event(scrollEvent, m_windowId);
288 event.SetPosition(value);
289 event.SetEventObject( this );
290 GetEventHandler()->ProcessEvent(event);
312ebad4 291
4c37f124
SC
292 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
293 cevent.SetInt( value );
294 cevent.SetEventObject( this );
312ebad4 295
4c37f124
SC
296 GetEventHandler()->ProcessEvent( cevent );
297 return noErr ;
298}
299
e40298d5
JS
300/* This is overloaded in wxSlider so that the proper width/height will always be used
301* for the slider different values would cause redrawing and mouse detection problems */
024f89f9
VS
302void wxSlider::DoSetSizeHints( int minW, int minH,
303 int maxW , int maxH ,
304 int incW , int incH )
e40298d5
JS
305{
306 wxSize size = GetBestSize();
312ebad4 307
e40298d5 308 if(GetWindowStyle() & wxSL_VERTICAL) {
024f89f9 309 wxWindow::DoSetSizeHints(size.x, minH, size.x, maxH, incW, incH);
e40298d5
JS
310 }
311 else {
024f89f9 312 wxWindow::DoSetSizeHints(minW, size.y, maxW, size.y, incW, incH);
e40298d5 313 }
519cb848 314}
9453cf2b 315
e40298d5
JS
316wxSize wxSlider::DoGetBestSize() const
317{
318 wxSize size;
319 int textwidth, textheight;
312ebad4 320
e40298d5
JS
321 if(GetWindowStyle() & wxSL_LABELS)
322 {
323 wxString text;
324 int ht, wd;
312ebad4 325
e40298d5 326 // Get maximum text label width and height
01526d4f 327 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
e40298d5 328 GetTextExtent(text, &textwidth, &textheight);
01526d4f 329 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
e40298d5
JS
330 GetTextExtent(text, &wd, &ht);
331 if(ht > textheight) {
332 textheight = ht;
333 }
334 if (wd > textwidth) {
335 textwidth = wd;
336 }
337 }
312ebad4 338
e40298d5
JS
339 if(GetWindowStyle() & wxSL_VERTICAL)
340 {
341 if(GetWindowStyle() & wxSL_AUTOTICKS) {
342 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
343 }
344 else {
345 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
346 }
347 if(GetWindowStyle() & wxSL_LABELS) {
348 size.x += textwidth + wxSLIDER_BORDERTEXT;
349 }
350 size.y = 150;
351 }
352 else
353 {
354 if(GetWindowStyle() & wxSL_AUTOTICKS) {
355 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
356 }
357 else {
358 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
359 }
360 if(GetWindowStyle() & wxSL_LABELS) {
361 size.y += textheight + wxSLIDER_BORDERTEXT;
362 }
363 size.x = 150;
364 }
365 return size;
366}
367
facd6764 368void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
327788ac 369{
e40298d5
JS
370 int xborder, yborder;
371 int minValWidth, maxValWidth, textwidth, textheight;
372 int sliderBreadth;
09ff2ee1 373 int width = w;
312ebad4 374
e40298d5 375 xborder = yborder = 0;
f26ca7f8 376
e40298d5
JS
377 if (GetWindowStyle() & wxSL_LABELS)
378 {
f26ca7f8
KO
379 //Labels have this control's parent as their parent
380 //so if this control is not at 0,0 relative to the parent
381 //the labels need to know the position of this control
382 //relative to its parent in order to size properly, so
383 //move the control first so we can use GetPosition()
384 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
312ebad4 385
e40298d5
JS
386 wxString text;
387 int ht;
312ebad4 388
e40298d5 389 // Get maximum text label width and height
01526d4f 390 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
e40298d5 391 GetTextExtent(text, &minValWidth, &textheight);
01526d4f 392 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
e40298d5
JS
393 GetTextExtent(text, &maxValWidth, &ht);
394 if(ht > textheight) {
395 textheight = ht;
396 }
397 textwidth = (minValWidth > maxValWidth ? minValWidth : maxValWidth);
312ebad4 398
e40298d5
JS
399 xborder = textwidth + wxSLIDER_BORDERTEXT;
400 yborder = textheight + wxSLIDER_BORDERTEXT;
312ebad4 401
e40298d5
JS
402 // Get slider breadth
403 if(GetWindowStyle() & wxSL_AUTOTICKS) {
404 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
405 }
406 else {
407 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
408 }
312ebad4 409
e40298d5
JS
410 if(GetWindowStyle() & wxSL_VERTICAL)
411 {
f26ca7f8 412 h = h - yborder ;
312ebad4 413
facd6764 414 if ( m_macMinimumStatic )
f26ca7f8 415 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT,
6dfa00e8 416 GetPosition().y + h - yborder);
facd6764 417 if ( m_macMaximumStatic )
f26ca7f8 418 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
facd6764 419 if ( m_macValueStatic )
6dfa00e8 420 m_macValueStatic->Move(GetPosition().x, GetPosition().y + h );
e40298d5
JS
421 }
422 else
423 {
1dc1b891 424 w = w - xborder ;
facd6764 425 if ( m_macMinimumStatic )
f26ca7f8 426 m_macMinimumStatic->Move(GetPosition().x + 0, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
facd6764 427 if ( m_macMaximumStatic )
f26ca7f8
KO
428 m_macMaximumStatic->Move(GetPosition().x + w - (maxValWidth/2),
429 GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
facd6764 430 if ( m_macValueStatic )
f26ca7f8 431 m_macValueStatic->Move(GetPosition().x + w, GetPosition().y + 0);
e40298d5
JS
432 }
433 }
01526d4f 434
09ff2ee1 435 // yet another hack since this is a composite control
01526d4f
WS
436 // when wxSlider has it's size hardcoded, we're not allowed to
437 // change the size. But when the control has labels, we DO need
09ff2ee1
KO
438 // to resize the internal Mac control to accomodate the text labels.
439 // We need to trick the wxWidgets resize mechanism so that we can
01526d4f
WS
440 // resize the slider part of the control ONLY.
441
09ff2ee1 442 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
01526d4f 443
09ff2ee1 444 int minWidth;
900a8765 445 minWidth = m_minWidth;
01526d4f 446
900a8765 447 if (GetWindowStyle() & wxSL_LABELS)
09ff2ee1 448 {
900a8765
KH
449 // make sure we don't allow the entire control to be resized accidently
450 if (width == GetSize().x)
451 m_minWidth = -1;
452 }
01526d4f 453 //If the control has labels, we still need to call this again because
312ebad4 454 //the labels alter the control's w and h values.
facd6764 455 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
09ff2ee1 456
900a8765 457 m_minWidth = minWidth;
327788ac
SC
458}
459
eb22f2a6
GD
460void wxSlider::DoMoveWindow(int x, int y, int width, int height)
461{
327788ac 462 wxControl::DoMoveWindow(x,y,width,height) ;
eb22f2a6 463}
312ebad4 464
02812785 465// Common processing to invert slider values based on wxSL_INVERSE
01526d4f 466int wxSlider::ValueInvertOrNot(int value) const
02812785
KH
467{
468 if (m_windowStyle & wxSL_VERTICAL)
469 {
470 // The reason for the backwards logic is that Mac's vertical sliders are
471 // inverted compared to Windows and GTK, hence we want inversion to be the
472 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
473 if (m_windowStyle & wxSL_INVERSE)
474 return value;
475 else
476 return (m_rangeMax + m_rangeMin) - value;
477 }
478 else // normal logic applies to HORIZONTAL sliders
479 {
01526d4f 480 return wxSliderBase::ValueInvertOrNot(value);
02812785
KH
481 }
482}
483
312ebad4 484#endif // wxUSE_SLIDER