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