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