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