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