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