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