]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/slider.cpp
Make wxPASSWORD and wxPROCESS_ENTER really deprecated.
[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
93a2619d 207 SetValue( m_rangeMin );
e9576ca5
SC
208}
209
210// For trackbars only
211void wxSlider::SetTickFreq(int n, int pos)
212{
213 // TODO
214 m_tickFreq = n;
215}
216
217void wxSlider::SetPageSize(int pageSize)
218{
219 // TODO
220 m_pageSize = pageSize;
221}
222
223int wxSlider::GetPageSize() const
224{
225 return m_pageSize;
226}
227
228void wxSlider::ClearSel()
229{
230 // TODO
231}
232
233void wxSlider::ClearTicks()
234{
235 // TODO
236}
237
238void wxSlider::SetLineSize(int lineSize)
239{
240 m_lineSize = lineSize;
241 // TODO
242}
243
244int wxSlider::GetLineSize() const
245{
246 // TODO
247 return 0;
248}
249
250int wxSlider::GetSelEnd() const
251{
252 // TODO
253 return 0;
254}
255
256int wxSlider::GetSelStart() const
257{
258 // TODO
259 return 0;
260}
261
262void wxSlider::SetSelection(int minPos, int maxPos)
263{
264 // TODO
265}
266
267void wxSlider::SetThumbLength(int len)
268{
269 // TODO
270}
271
272int wxSlider::GetThumbLength() const
273{
274 // TODO
275 return 0;
276}
277
278void wxSlider::SetTick(int tickPos)
279{
280 // TODO
281}
282
93a2619d 283void wxSlider::Command(wxCommandEvent &event)
e9576ca5 284{
904ffc03
DS
285 SetValue(event.GetInt());
286 ProcessCommand(event);
e9576ca5
SC
287}
288
93a2619d 289void wxSlider::MacHandleControlClick( WXWidget control, wxInt16 controlpart, bool mouseStillDown )
519cb848 290{
02812785
KH
291 // Whatever the native value is, we may need to invert it for calling
292 // SetValue and putting the possibly inverted value in the event
93a2619d 293 int value = ValueInvertOrNot( m_peer->GetValue() );
312ebad4 294
93a2619d 295 SetValue( value );
312ebad4 296
93a2619d 297 wxEventType scrollEvent = wxEVT_NULL;
312ebad4 298
4c37f124 299 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
312ebad4 300
cea9c546 301 wxScrollEvent event(scrollEvent, m_windowId);
e40298d5
JS
302 event.SetPosition(value);
303 event.SetEventObject( this );
304 GetEventHandler()->ProcessEvent(event);
312ebad4 305
e40298d5
JS
306 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
307 cevent.SetInt( value );
308 cevent.SetEventObject( this );
312ebad4 309
e40298d5
JS
310 GetEventHandler()->ProcessEvent( cevent );
311}
312
312ebad4 313wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent )
4c37f124 314{
02812785
KH
315 // Whatever the native value is, we may need to invert it for calling
316 // SetValue and putting the possibly inverted value in the event
93a2619d 317 int value = ValueInvertOrNot( m_peer->GetValue() ) ;
312ebad4
WS
318
319 SetValue( value ) ;
320
4c37f124 321 wxEventType scrollEvent = wxEVT_NULL ;
312ebad4 322
4c37f124 323 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
312ebad4 324
4c37f124
SC
325 wxScrollEvent event(scrollEvent, m_windowId);
326 event.SetPosition(value);
327 event.SetEventObject( this );
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