]> git.saurik.com Git - wxWidgets.git/blob - src/osx/slider_osx.cpp
wxRTC: save and load the 'shown' status in case there's a situation where layout...
[wxWidgets.git] / src / osx / slider_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/slider_osx.cpp
3 // Purpose: wxSlider
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_SLIDER
14
15 #include "wx/slider.h"
16 #include "wx/osx/private.h"
17
18 BEGIN_EVENT_TABLE(wxSlider, wxControl)
19 END_EVENT_TABLE()
20
21 // The dimensions of the different styles of sliders (from Aqua document)
22 #if wxOSX_USE_COCOA
23 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 28
24 #define wxSLIDER_DIMENSIONACROSS_ARROW 21
25 #else
26 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
27 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
28 #endif
29
30 // Distance between slider and text
31 #define wxSLIDER_BORDERTEXT 5
32
33 // NB: The default orientation for a slider is horizontal; however, if the user specifies
34 // some slider styles but doesn't specify the orientation we have to assume he wants a
35 // horizontal one. Therefore in this file when testing for the slider's orientation
36 // vertical is tested for if this is not set then we use the horizontal one
37 // e.g., if (GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }.
38
39 wxSlider::wxSlider()
40 {
41 m_pageSize = 1;
42 m_lineSize = 1;
43 m_rangeMax = 0;
44 m_rangeMin = 0;
45 m_tickFreq = 0;
46
47 m_macMinimumStatic = NULL;
48 m_macMaximumStatic = NULL;
49 m_macValueStatic = NULL;
50 }
51
52 bool wxSlider::Create(wxWindow *parent,
53 wxWindowID id,
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)
59 {
60 DontCreatePeer();
61
62 m_macMinimumStatic = NULL;
63 m_macMaximumStatic = NULL;
64 m_macValueStatic = NULL;
65
66 m_lineSize = 1;
67 m_tickFreq = 0;
68
69 m_rangeMax = maxValue;
70 m_rangeMin = minValue;
71
72 m_pageSize = (int)((maxValue - minValue) / 10);
73
74 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
75 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
76 // reasons we can't really change it, instead try to infer the orientation
77 // from the flags given to us here
78 switch ( style & (wxSL_LEFT | wxSL_RIGHT | wxSL_TOP | wxSL_BOTTOM) )
79 {
80 case wxSL_LEFT:
81 case wxSL_RIGHT:
82 style |= wxSL_VERTICAL;
83 break;
84
85 case wxSL_TOP:
86 case wxSL_BOTTOM:
87 style |= wxSL_HORIZONTAL;
88 break;
89
90 case 0:
91 default:
92 // no specific direction, do we have at least the orientation?
93 if ( !(style & (wxSL_HORIZONTAL | wxSL_VERTICAL)) )
94 // no: choose default
95 style |= wxSL_BOTTOM | wxSL_HORIZONTAL;
96 break;
97 }
98
99 wxASSERT_MSG( !(style & wxSL_VERTICAL) || !(style & wxSL_HORIZONTAL),
100 wxT("incompatible slider direction and orientation") );
101
102 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
103 return false;
104
105 SetPeer(wxWidgetImpl::CreateSlider( this, parent, id, value, minValue, maxValue, pos, size, style, GetExtraStyle() ));
106
107 #if 0
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 #endif
119
120 if (style & wxSL_LABELS)
121 {
122 m_macMinimumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
123 m_macMaximumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
124 m_macValueStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
125 }
126
127 SetRange(minValue, maxValue);
128 SetValue(value);
129
130 MacPostControlCreate(pos, size);
131
132 return true;
133 }
134
135 wxSlider::~wxSlider()
136 {
137 // this is a special case, as we had to add windows as siblings we are
138 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
139 if ( m_parent && !m_parent->IsBeingDeleted() )
140 {
141 delete m_macMinimumStatic;
142 delete m_macMaximumStatic;
143 delete m_macValueStatic;
144 }
145 }
146
147 int wxSlider::GetValue() const
148 {
149 // We may need to invert the value returned by the widget
150 return ValueInvertOrNot( GetPeer()->GetValue() ) ;
151 }
152
153 void wxSlider::SetValue(int value)
154 {
155 if ( m_macValueStatic )
156 {
157 wxString valuestring;
158 valuestring.Printf( wxT("%d"), value );
159 m_macValueStatic->SetLabel( valuestring );
160 }
161
162 // We only invert for the setting of the actual native widget
163 GetPeer()->SetValue( ValueInvertOrNot( value ) );
164 }
165
166 void wxSlider::SetRange(int minValue, int maxValue)
167 {
168 // Changing the range preserves the value of the native control but may
169 // change our logical value if we're inverting the native value to get it
170 // as ValueInvertOrNot() depends on the range so preserve it before
171 // changing the range.
172 const int valueOld = GetValue();
173
174 wxString value;
175
176 m_rangeMin = minValue;
177 m_rangeMax = maxValue;
178
179 GetPeer()->SetMinimum( m_rangeMin );
180 GetPeer()->SetMaximum( m_rangeMax );
181
182 if (m_macMinimumStatic)
183 {
184 value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
185 m_macMinimumStatic->SetLabel( value );
186 }
187
188 if (m_macMaximumStatic)
189 {
190 value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
191 m_macMaximumStatic->SetLabel( value );
192 }
193
194 // If the range is out of bounds, set it to a
195 // value that is within bounds
196 // RN: Testing reveals OSX does its own
197 // bounding, perhaps this isn't needed?
198 int currentValue = GetValue();
199
200 if(currentValue < m_rangeMin)
201 SetValue(m_rangeMin);
202 else if(currentValue > m_rangeMax)
203 SetValue(m_rangeMax);
204
205 // Ensure that our value didn't change.
206 SetValue(valueOld);
207 }
208
209 // For trackbars only
210 void wxSlider::DoSetTickFreq(int n)
211 {
212 // TODO
213 m_tickFreq = n;
214 }
215
216 void wxSlider::SetPageSize(int pageSize)
217 {
218 // TODO
219 m_pageSize = pageSize;
220 }
221
222 int wxSlider::GetPageSize() const
223 {
224 return m_pageSize;
225 }
226
227 void wxSlider::ClearSel()
228 {
229 // TODO
230 }
231
232 void wxSlider::ClearTicks()
233 {
234 // TODO
235 }
236
237 void wxSlider::SetLineSize(int lineSize)
238 {
239 m_lineSize = lineSize;
240 // TODO
241 }
242
243 int wxSlider::GetLineSize() const
244 {
245 // TODO
246 return m_lineSize;
247 }
248
249 int wxSlider::GetSelEnd() const
250 {
251 // TODO
252 return 0;
253 }
254
255 int wxSlider::GetSelStart() const
256 {
257 // TODO
258 return 0;
259 }
260
261 void wxSlider::SetSelection(int WXUNUSED(minPos), int WXUNUSED(maxPos))
262 {
263 // TODO
264 }
265
266 void wxSlider::SetThumbLength(int WXUNUSED(len))
267 {
268 // TODO
269 }
270
271 int wxSlider::GetThumbLength() const
272 {
273 // TODO
274 return 0;
275 }
276
277 void wxSlider::SetTick(int WXUNUSED(tickPos))
278 {
279 // TODO
280 }
281
282 void wxSlider::Command(wxCommandEvent &event)
283 {
284 SetValue(event.GetInt());
285 ProcessCommand(event);
286 }
287
288 void wxSlider::TriggerScrollEvent( wxEventType scrollEvent)
289 {
290 // Whatever the native value is, we may need to invert it for calling
291 // SetValue and putting the possibly inverted value in the event
292 int value = ValueInvertOrNot( GetPeer()->GetValue() );
293
294 SetValue( value );
295
296 wxScrollEvent event( scrollEvent, m_windowId );
297 event.SetPosition( value );
298 event.SetEventObject( this );
299 HandleWindowEvent( event );
300
301 wxCommandEvent cevent( wxEVT_SLIDER, m_windowId );
302 cevent.SetInt( value );
303 cevent.SetEventObject( this );
304 HandleWindowEvent( cevent );
305 }
306
307 bool wxSlider::OSXHandleClicked( double WXUNUSED(timestampsec) )
308 {
309 TriggerScrollEvent(wxEVT_SCROLL_THUMBRELEASE);
310
311 return true;
312 }
313
314 // This is overloaded in wxSlider so that the proper width/height will always be used
315 // for the slider different values would cause redrawing and mouse detection problems
316 //
317 void wxSlider::DoSetSizeHints( int minW, int minH,
318 int maxW, int maxH,
319 int WXUNUSED(incW), int WXUNUSED(incH) )
320 {
321 wxSize size = GetBestSize();
322
323 if (GetWindowStyle() & wxSL_VERTICAL)
324 {
325 SetMinSize( wxSize(size.x,minH) );
326 SetMaxSize( wxSize(size.x,maxH) );
327 }
328 else
329 {
330 SetMinSize( wxSize(minW,size.y) );
331 SetMaxSize( wxSize(maxW,size.y) );
332 }
333 }
334
335 wxSize wxSlider::DoGetBestSize() const
336 {
337 wxSize size;
338 int textwidth, textheight;
339 int mintwidth, mintheight;
340 int maxtwidth, maxtheight;
341
342 textwidth = textheight = 0;
343 mintwidth = mintheight = 0;
344 maxtwidth = maxtheight = 0;
345
346 if (GetWindowStyle() & wxSL_LABELS)
347 {
348 wxString text;
349
350 // Get maximum text label width and height
351 text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
352 GetTextExtent(text, &mintwidth, &mintheight);
353 text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
354 GetTextExtent(text, &maxtwidth, &maxtheight);
355
356 if (maxtheight > mintheight)
357 textheight = maxtheight;
358 else
359 textheight = mintheight;
360
361 if (maxtwidth > mintwidth)
362 textwidth = maxtwidth;
363 else
364 textwidth = mintwidth;
365 }
366
367 if (GetWindowStyle() & wxSL_VERTICAL)
368 {
369 size.y = 150;
370
371 if (GetWindowStyle() & wxSL_AUTOTICKS)
372 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
373 else
374 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
375
376 if (GetWindowStyle() & wxSL_LABELS)
377 size.x += textwidth + wxSLIDER_BORDERTEXT;
378 }
379 else
380 {
381 size.x = 150;
382
383 if (GetWindowStyle() & wxSL_AUTOTICKS)
384 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
385 else
386 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
387
388 if (GetWindowStyle() & wxSL_LABELS)
389 {
390 size.y += textheight + wxSLIDER_BORDERTEXT;
391 size.x += (mintwidth / 2) + (maxtwidth / 2);
392 }
393 }
394
395 return size;
396 }
397
398 void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
399 {
400 int yborder = 0;
401 int minValWidth, maxValWidth, textheight;
402 int sliderBreadth;
403 int width = w;
404
405 if (GetWindowStyle() & wxSL_LABELS)
406 {
407 wxString text;
408 int ht, valValWidth;
409
410 // Get maximum text label width and height
411 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
412 GetTextExtent(text, &minValWidth, &textheight);
413 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
414 GetTextExtent(text, &maxValWidth, &ht);
415
416 if (ht > textheight)
417 textheight = ht;
418
419 if (GetWindowStyle() & wxSL_HORIZONTAL)
420 {
421 if ( m_macMinimumStatic )
422 {
423 w -= minValWidth / 2;
424 x += minValWidth / 2;
425 }
426
427 if ( m_macMaximumStatic )
428 w -= maxValWidth / 2;
429 }
430
431 // Labels have this control's parent as their parent
432 // so if this control is not at 0,0 relative to the parent
433 // the labels need to know the position of this control
434 // relative to its parent in order to size properly, so
435 // move the control first so we can use GetPosition()
436 wxControl::DoSetSize( x, y, w, h, sizeFlags );
437
438 if (GetWindowStyle() & wxSL_VERTICAL)
439 // If vertical, use current value
440 text.Printf(wxT("%d"), (int)GetPeer()->GetValue());
441 else
442 // Use max so that the current value doesn't drift as centering would need to change
443 text.Printf(wxT("%d"), m_rangeMax);
444
445 GetTextExtent(text, &valValWidth, &ht);
446
447 yborder = textheight + wxSLIDER_BORDERTEXT;
448
449 // Get slider breadth
450 if (GetWindowStyle() & wxSL_AUTOTICKS)
451 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
452 else
453 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
454
455 if (GetWindowStyle() & wxSL_VERTICAL)
456 {
457 h = h - yborder;
458
459 if ( m_macMinimumStatic )
460 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + h - yborder);
461 if ( m_macMaximumStatic )
462 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
463 if ( m_macValueStatic )
464 m_macValueStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + (h / 2) - (ht / 2));
465 }
466 else
467 {
468 if ( m_macMinimumStatic )
469 m_macMinimumStatic->Move(GetPosition().x, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
470 if ( m_macMaximumStatic )
471 m_macMaximumStatic->Move(GetPosition().x + w - maxValWidth, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
472 if ( m_macValueStatic )
473 m_macValueStatic->Move(GetPosition().x + (w / 2) - (valValWidth / 2), GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
474 }
475 }
476
477 // yet another hack since this is a composite control
478 // when wxSlider has it's size hardcoded, we're not allowed to
479 // change the size. But when the control has labels, we DO need
480
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 accidentally
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
503 void 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
509 int 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