]> git.saurik.com Git - wxWidgets.git/blame - src/osx/slider_osx.cpp
Make art provider id of type "char *" instead of "wxChar *".
[wxWidgets.git] / src / osx / slider_osx.cpp
CommitLineData
524c47aa
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/slider_osx.cpp
3// Purpose: wxSlider
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
b5b208a1 7// RCS-ID: $Id$
524c47aa
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#if wxUSE_SLIDER
15
16#include "wx/slider.h"
17#include "wx/osx/private.h"
18
19IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
20
21BEGIN_EVENT_TABLE(wxSlider, wxControl)
22END_EVENT_TABLE()
23
24 // The dimensions of the different styles of sliders (from Aqua document)
e928fe12 25#if wxOSX_USE_COCOA
19c7ac3d 26 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 28
6cd56f6e
SC
27 #define wxSLIDER_DIMENSIONACROSS_ARROW 21
28#else
29 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
30 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
31#endif
524c47aa
SC
32
33// Distance between slider and text
34#define wxSLIDER_BORDERTEXT 5
35
36// NB: The default orientation for a slider is horizontal; however, if the user specifies
37// some slider styles but doesn't specify the orientation we have to assume he wants a
38// horizontal one. Therefore in this file when testing for the slider's orientation
39// vertical is tested for if this is not set then we use the horizontal one
40// e.g., if (GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }.
41
42wxSlider::wxSlider()
43{
44 m_pageSize = 1;
45 m_lineSize = 1;
46 m_rangeMax = 0;
47 m_rangeMin = 0;
48 m_tickFreq = 0;
49
50 m_macMinimumStatic = NULL;
51 m_macMaximumStatic = NULL;
52 m_macValueStatic = NULL;
53}
54
55bool wxSlider::Create(wxWindow *parent,
56 wxWindowID id,
57 int value, int minValue, int maxValue,
58 const wxPoint& pos,
59 const wxSize& size, long style,
60 const wxValidator& validator,
61 const wxString& name)
62{
63 m_macIsUserPane = false;
64
65 m_macMinimumStatic = NULL;
66 m_macMaximumStatic = NULL;
67 m_macValueStatic = NULL;
68
69 m_lineSize = 1;
70 m_tickFreq = 0;
71
72 m_rangeMax = maxValue;
73 m_rangeMin = minValue;
74
75 m_pageSize = (int)((maxValue - minValue) / 10);
76
77 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
78 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
79 // reasons we can't really change it, instead try to infer the orientation
80 // from the flags given to us here
81 switch ( style & (wxSL_LEFT | wxSL_RIGHT | wxSL_TOP | wxSL_BOTTOM) )
82 {
83 case wxSL_LEFT:
84 case wxSL_RIGHT:
85 style |= wxSL_VERTICAL;
86 break;
87
88 case wxSL_TOP:
89 case wxSL_BOTTOM:
90 style |= wxSL_HORIZONTAL;
91 break;
92
93 case 0:
94 default:
95 // no specific direction, do we have at least the orientation?
96 if ( !(style & (wxSL_HORIZONTAL | wxSL_VERTICAL)) )
97 // no: choose default
98 style |= wxSL_BOTTOM | wxSL_HORIZONTAL;
99 break;
100 }
101
102 wxASSERT_MSG( !(style & wxSL_VERTICAL) || !(style & wxSL_HORIZONTAL),
103 wxT("incompatible slider direction and orientation") );
104
105 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
106 return false;
107
108 m_peer = wxWidgetImpl::CreateSlider( this, parent, id, value, minValue, maxValue, pos, size, style, GetExtraStyle() );
109
110 if (style & wxSL_VERTICAL)
111 // Forces SetSize to use the proper width
112 SetSizeHints(10, -1, 10, -1);
113 else
114 // Forces SetSize to use the proper height
115 SetSizeHints(-1, 10, -1, 10);
116
117 // NB: SetSizeHints is overloaded by wxSlider and will substitute 10 with the
118 // proper dimensions, it also means other people cannot bugger the slider with
119 // other values
120
121 if (style & wxSL_LABELS)
122 {
123 m_macMinimumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
124 m_macMaximumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
125 m_macValueStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
126 }
127
128 SetRange(minValue, maxValue);
129 SetValue(value);
130
131 MacPostControlCreate(pos, size);
132
133 return true;
134}
135
136wxSlider::~wxSlider()
137{
138 // this is a special case, as we had to add windows as siblings we are
139 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
140 if ( m_parent && !m_parent->IsBeingDeleted() )
141 {
142 delete m_macMinimumStatic;
143 delete m_macMaximumStatic;
144 delete m_macValueStatic;
145 }
146}
147
148int wxSlider::GetValue() const
149{
150 // We may need to invert the value returned by the widget
151 return ValueInvertOrNot( m_peer->GetValue() ) ;
152}
153
154void wxSlider::SetValue(int value)
155{
156 if ( m_macValueStatic )
157 {
158 wxString valuestring;
159 valuestring.Printf( wxT("%d"), value );
160 m_macValueStatic->SetLabel( valuestring );
161 }
162
163 // We only invert for the setting of the actual native widget
164 m_peer->SetValue( ValueInvertOrNot( value ) );
165}
166
167void wxSlider::SetRange(int minValue, int maxValue)
168{
1c7ce073
VZ
169 // Changing the range preserves the value of the native control but may
170 // change our logical value if we're inverting the native value to get it
171 // as ValueInvertOrNot() depends on the range so preserve it before
172 // changing the range.
173 const int valueOld = GetValue();
174
524c47aa
SC
175 wxString value;
176
177 m_rangeMin = minValue;
178 m_rangeMax = maxValue;
179
180 m_peer->SetMinimum( m_rangeMin );
181 m_peer->SetMaximum( m_rangeMax );
182
183 if (m_macMinimumStatic)
184 {
185 value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
186 m_macMinimumStatic->SetLabel( value );
187 }
188
189 if (m_macMaximumStatic)
190 {
191 value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
192 m_macMaximumStatic->SetLabel( value );
193 }
194
03647350 195 // If the range is out of bounds, set it to a
524c47aa 196 // value that is within bounds
03647350 197 // RN: Testing reveals OSX does its own
524c47aa
SC
198 // bounding, perhaps this isn't needed?
199 int currentValue = GetValue();
200
201 if(currentValue < m_rangeMin)
202 SetValue(m_rangeMin);
203 else if(currentValue > m_rangeMax)
204 SetValue(m_rangeMax);
1c7ce073
VZ
205
206 // Ensure that our value didn't change.
207 SetValue(valueOld);
524c47aa
SC
208}
209
210// For trackbars only
211void wxSlider::SetTickFreq(int n, int WXUNUSED(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 m_lineSize;
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 WXUNUSED(minPos), int WXUNUSED(maxPos))
263{
264 // TODO
265}
266
267void wxSlider::SetThumbLength(int WXUNUSED(len))
268{
269 // TODO
270}
271
272int wxSlider::GetThumbLength() const
273{
274 // TODO
275 return 0;
276}
277
278void wxSlider::SetTick(int WXUNUSED(tickPos))
279{
280 // TODO
281}
282
283void wxSlider::Command(wxCommandEvent &event)
284{
285 SetValue(event.GetInt());
286 ProcessCommand(event);
287}
288
19c7ac3d 289void wxSlider::TriggerScrollEvent( wxEventType scrollEvent)
524c47aa
SC
290{
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
293 int value = ValueInvertOrNot( m_peer->GetValue() );
294
295 SetValue( value );
296
19c7ac3d 297 wxScrollEvent event( scrollEvent, m_windowId );
524c47aa
SC
298 event.SetPosition( value );
299 event.SetEventObject( this );
300 HandleWindowEvent( event );
301
302 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
303 cevent.SetInt( value );
304 cevent.SetEventObject( this );
305 HandleWindowEvent( cevent );
306}
307
0faf03bf 308bool wxSlider::OSXHandleClicked( double WXUNUSED(timestampsec) )
524c47aa 309{
19c7ac3d 310 TriggerScrollEvent(wxEVT_SCROLL_THUMBRELEASE);
03647350 311
524c47aa
SC
312 return true;
313}
314
315// This is overloaded in wxSlider so that the proper width/height will always be used
316// for the slider different values would cause redrawing and mouse detection problems
317//
318void wxSlider::DoSetSizeHints( int minW, int minH,
319 int maxW, int maxH,
320 int WXUNUSED(incW), int WXUNUSED(incH) )
321{
322 wxSize size = GetBestSize();
323
324 if (GetWindowStyle() & wxSL_VERTICAL)
325 {
326 SetMinSize( wxSize(size.x,minH) );
327 SetMaxSize( wxSize(size.x,maxH) );
328 }
329 else
330 {
331 SetMinSize( wxSize(minW,size.y) );
332 SetMaxSize( wxSize(maxW,size.y) );
333 }
334}
335
336wxSize wxSlider::DoGetBestSize() const
337{
338 wxSize size;
339 int textwidth, textheight;
340 int mintwidth, mintheight;
341 int maxtwidth, maxtheight;
342
343 textwidth = textheight = 0;
344 mintwidth = mintheight = 0;
345 maxtwidth = maxtheight = 0;
346
347 if (GetWindowStyle() & wxSL_LABELS)
348 {
349 wxString text;
350
351 // Get maximum text label width and height
352 text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
353 GetTextExtent(text, &mintwidth, &mintheight);
354 text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
355 GetTextExtent(text, &maxtwidth, &maxtheight);
356
357 if (maxtheight > mintheight)
358 textheight = maxtheight;
359 else
360 textheight = mintheight;
361
362 if (maxtwidth > mintwidth)
363 textwidth = maxtwidth;
364 else
365 textwidth = mintwidth;
366 }
367
368 if (GetWindowStyle() & wxSL_VERTICAL)
369 {
370 size.y = 150;
371
372 if (GetWindowStyle() & wxSL_AUTOTICKS)
373 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
374 else
375 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
376
377 if (GetWindowStyle() & wxSL_LABELS)
378 size.x += textwidth + wxSLIDER_BORDERTEXT;
379 }
380 else
381 {
382 size.x = 150;
383
384 if (GetWindowStyle() & wxSL_AUTOTICKS)
385 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
386 else
387 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
388
389 if (GetWindowStyle() & wxSL_LABELS)
390 {
391 size.y += textheight + wxSLIDER_BORDERTEXT;
392 size.x += (mintwidth / 2) + (maxtwidth / 2);
393 }
394 }
395
396 return size;
397}
398
399void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
400{
401 int yborder = 0;
402 int minValWidth, maxValWidth, textheight;
403 int sliderBreadth;
404 int width = w;
405
406 if (GetWindowStyle() & wxSL_LABELS)
407 {
408 wxString text;
409 int ht, valValWidth;
410
411 // Get maximum text label width and height
412 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
413 GetTextExtent(text, &minValWidth, &textheight);
414 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
415 GetTextExtent(text, &maxValWidth, &ht);
416
417 if (ht > textheight)
418 textheight = ht;
419
420 if (GetWindowStyle() & wxSL_HORIZONTAL)
421 {
422 if ( m_macMinimumStatic )
423 {
424 w -= minValWidth / 2;
425 x += minValWidth / 2;
426 }
427
428 if ( m_macMaximumStatic )
429 w -= maxValWidth / 2;
430 }
431
432 // Labels have this control's parent as their parent
433 // so if this control is not at 0,0 relative to the parent
434 // the labels need to know the position of this control
435 // relative to its parent in order to size properly, so
436 // move the control first so we can use GetPosition()
437 wxControl::DoSetSize( x, y, w, h, sizeFlags );
438
439 if (GetWindowStyle() & wxSL_VERTICAL)
440 // If vertical, use current value
441 text.Printf(wxT("%d"), (int)m_peer->GetValue());
442 else
443 // Use max so that the current value doesn't drift as centering would need to change
444 text.Printf(wxT("%d"), m_rangeMax);
445
446 GetTextExtent(text, &valValWidth, &ht);
447
448 yborder = textheight + wxSLIDER_BORDERTEXT;
449
450 // Get slider breadth
451 if (GetWindowStyle() & wxSL_AUTOTICKS)
452 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
453 else
454 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
455
456 if (GetWindowStyle() & wxSL_VERTICAL)
457 {
458 h = h - yborder;
459
460 if ( m_macMinimumStatic )
461 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + h - yborder);
462 if ( m_macMaximumStatic )
463 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
464 if ( m_macValueStatic )
465 m_macValueStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + (h / 2) - (ht / 2));
466 }
467 else
468 {
469 if ( m_macMinimumStatic )
470 m_macMinimumStatic->Move(GetPosition().x, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
471 if ( m_macMaximumStatic )
472 m_macMaximumStatic->Move(GetPosition().x + w - maxValWidth, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
473 if ( m_macValueStatic )
474 m_macValueStatic->Move(GetPosition().x + (w / 2) - (valValWidth / 2), GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
475 }
476 }
477
478 // yet another hack since this is a composite control
479 // when wxSlider has it's size hardcoded, we're not allowed to
480 // change the size. But when the control has labels, we DO need
481 // to resize the internal Mac control to accommodate the text labels.
482 // We need to trick the wxWidgets resize mechanism so that we can
483 // resize the slider part of the control ONLY.
484
485 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
486
487 int minWidth = m_minWidth;
488
489 if (GetWindowStyle() & wxSL_LABELS)
490 {
491 // make sure we don't allow the entire control to be resized accidently
492 if (width == GetSize().x)
493 m_minWidth = -1;
494 }
495
496 // If the control has labels, we still need to call this again because
497 // the labels alter the control's w and h values.
498 wxControl::DoSetSize( x, y, w, h, sizeFlags );
499
500 m_minWidth = minWidth;
501}
502
503void wxSlider::DoMoveWindow(int x, int y, int width, int height)
504{
505 wxControl::DoMoveWindow( x, y, width, height );
506}
507
508// Common processing to invert slider values based on wxSL_INVERSE
509int wxSlider::ValueInvertOrNot(int value) const
510{
511 int result = 0;
512
513 if (m_windowStyle & wxSL_VERTICAL)
514 {
515 // The reason for the backwards logic is that Mac's vertical sliders are
516 // inverted compared to Windows and GTK, hence we want inversion to be the
517 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
518 if (m_windowStyle & wxSL_INVERSE)
519 result = value;
520 else
521 result = (m_rangeMax + m_rangeMin) - value;
522 }
523 else // normal logic applies to HORIZONTAL sliders
524 {
525 result = wxSliderBase::ValueInvertOrNot(value);
526 }
527
528 return result;
529}
530
531#endif // wxUSE_SLIDER