]> git.saurik.com Git - wxWidgets.git/blob - src/osx/slider_osx.cpp
Give some OSX specific methods OSX prefix, fixes hidden virtual method warning elsewhere
[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 // RCS-ID: $Id: slider.cpp 54129 2008-06-11 19:30:52Z 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
19 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
20
21 BEGIN_EVENT_TABLE(wxSlider, wxControl)
22 END_EVENT_TABLE()
23
24 // The dimensions of the different styles of sliders (from Aqua document)
25 #ifdef wxOSX_USE_COCOA
26 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 28
27 #define wxSLIDER_DIMENSIONACROSS_ARROW 21
28 #else
29 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
30 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
31 #endif
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
42 wxSlider::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
55 bool 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
136 wxSlider::~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
148 int wxSlider::GetValue() const
149 {
150 // We may need to invert the value returned by the widget
151 return ValueInvertOrNot( m_peer->GetValue() ) ;
152 }
153
154 void 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
167 void wxSlider::SetRange(int minValue, int maxValue)
168 {
169 wxString value;
170
171 m_rangeMin = minValue;
172 m_rangeMax = maxValue;
173
174 m_peer->SetMinimum( m_rangeMin );
175 m_peer->SetMaximum( m_rangeMax );
176
177 if (m_macMinimumStatic)
178 {
179 value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
180 m_macMinimumStatic->SetLabel( value );
181 }
182
183 if (m_macMaximumStatic)
184 {
185 value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
186 m_macMaximumStatic->SetLabel( value );
187 }
188
189 // If the range is out of bounds, set it to a
190 // value that is within bounds
191 // RN: Testing reveals OSX does its own
192 // bounding, perhaps this isn't needed?
193 int currentValue = GetValue();
194
195 if(currentValue < m_rangeMin)
196 SetValue(m_rangeMin);
197 else if(currentValue > m_rangeMax)
198 SetValue(m_rangeMax);
199 }
200
201 // For trackbars only
202 void wxSlider::SetTickFreq(int n, int WXUNUSED(pos))
203 {
204 // TODO
205 m_tickFreq = n;
206 }
207
208 void wxSlider::SetPageSize(int pageSize)
209 {
210 // TODO
211 m_pageSize = pageSize;
212 }
213
214 int wxSlider::GetPageSize() const
215 {
216 return m_pageSize;
217 }
218
219 void wxSlider::ClearSel()
220 {
221 // TODO
222 }
223
224 void wxSlider::ClearTicks()
225 {
226 // TODO
227 }
228
229 void wxSlider::SetLineSize(int lineSize)
230 {
231 m_lineSize = lineSize;
232 // TODO
233 }
234
235 int wxSlider::GetLineSize() const
236 {
237 // TODO
238 return m_lineSize;
239 }
240
241 int wxSlider::GetSelEnd() const
242 {
243 // TODO
244 return 0;
245 }
246
247 int wxSlider::GetSelStart() const
248 {
249 // TODO
250 return 0;
251 }
252
253 void wxSlider::SetSelection(int WXUNUSED(minPos), int WXUNUSED(maxPos))
254 {
255 // TODO
256 }
257
258 void wxSlider::SetThumbLength(int WXUNUSED(len))
259 {
260 // TODO
261 }
262
263 int wxSlider::GetThumbLength() const
264 {
265 // TODO
266 return 0;
267 }
268
269 void wxSlider::SetTick(int WXUNUSED(tickPos))
270 {
271 // TODO
272 }
273
274 void wxSlider::Command(wxCommandEvent &event)
275 {
276 SetValue(event.GetInt());
277 ProcessCommand(event);
278 }
279
280 void wxSlider::TriggerScrollEvent( wxEventType scrollEvent)
281 {
282 // Whatever the native value is, we may need to invert it for calling
283 // SetValue and putting the possibly inverted value in the event
284 int value = ValueInvertOrNot( m_peer->GetValue() );
285
286 SetValue( value );
287
288 wxScrollEvent event( scrollEvent, m_windowId );
289 event.SetPosition( value );
290 event.SetEventObject( this );
291 HandleWindowEvent( event );
292
293 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
294 cevent.SetInt( value );
295 cevent.SetEventObject( this );
296 HandleWindowEvent( cevent );
297 }
298
299 bool wxSlider::OSXHandleClicked( double timestampsec )
300 {
301 TriggerScrollEvent(wxEVT_SCROLL_THUMBRELEASE);
302
303 return true;
304 }
305
306 // This is overloaded in wxSlider so that the proper width/height will always be used
307 // for the slider different values would cause redrawing and mouse detection problems
308 //
309 void wxSlider::DoSetSizeHints( int minW, int minH,
310 int maxW, int maxH,
311 int WXUNUSED(incW), int WXUNUSED(incH) )
312 {
313 wxSize size = GetBestSize();
314
315 if (GetWindowStyle() & wxSL_VERTICAL)
316 {
317 SetMinSize( wxSize(size.x,minH) );
318 SetMaxSize( wxSize(size.x,maxH) );
319 }
320 else
321 {
322 SetMinSize( wxSize(minW,size.y) );
323 SetMaxSize( wxSize(maxW,size.y) );
324 }
325 }
326
327 wxSize wxSlider::DoGetBestSize() const
328 {
329 wxSize size;
330 int textwidth, textheight;
331 int mintwidth, mintheight;
332 int maxtwidth, maxtheight;
333
334 textwidth = textheight = 0;
335 mintwidth = mintheight = 0;
336 maxtwidth = maxtheight = 0;
337
338 if (GetWindowStyle() & wxSL_LABELS)
339 {
340 wxString text;
341
342 // Get maximum text label width and height
343 text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
344 GetTextExtent(text, &mintwidth, &mintheight);
345 text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
346 GetTextExtent(text, &maxtwidth, &maxtheight);
347
348 if (maxtheight > mintheight)
349 textheight = maxtheight;
350 else
351 textheight = mintheight;
352
353 if (maxtwidth > mintwidth)
354 textwidth = maxtwidth;
355 else
356 textwidth = mintwidth;
357 }
358
359 if (GetWindowStyle() & wxSL_VERTICAL)
360 {
361 size.y = 150;
362
363 if (GetWindowStyle() & wxSL_AUTOTICKS)
364 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
365 else
366 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
367
368 if (GetWindowStyle() & wxSL_LABELS)
369 size.x += textwidth + wxSLIDER_BORDERTEXT;
370 }
371 else
372 {
373 size.x = 150;
374
375 if (GetWindowStyle() & wxSL_AUTOTICKS)
376 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
377 else
378 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
379
380 if (GetWindowStyle() & wxSL_LABELS)
381 {
382 size.y += textheight + wxSLIDER_BORDERTEXT;
383 size.x += (mintwidth / 2) + (maxtwidth / 2);
384 }
385 }
386
387 return size;
388 }
389
390 void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
391 {
392 int yborder = 0;
393 int minValWidth, maxValWidth, textheight;
394 int sliderBreadth;
395 int width = w;
396
397 if (GetWindowStyle() & wxSL_LABELS)
398 {
399 wxString text;
400 int ht, valValWidth;
401
402 // Get maximum text label width and height
403 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
404 GetTextExtent(text, &minValWidth, &textheight);
405 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
406 GetTextExtent(text, &maxValWidth, &ht);
407
408 if (ht > textheight)
409 textheight = ht;
410
411 if (GetWindowStyle() & wxSL_HORIZONTAL)
412 {
413 if ( m_macMinimumStatic )
414 {
415 w -= minValWidth / 2;
416 x += minValWidth / 2;
417 }
418
419 if ( m_macMaximumStatic )
420 w -= maxValWidth / 2;
421 }
422
423 // Labels have this control's parent as their parent
424 // so if this control is not at 0,0 relative to the parent
425 // the labels need to know the position of this control
426 // relative to its parent in order to size properly, so
427 // move the control first so we can use GetPosition()
428 wxControl::DoSetSize( x, y, w, h, sizeFlags );
429
430 if (GetWindowStyle() & wxSL_VERTICAL)
431 // If vertical, use current value
432 text.Printf(wxT("%d"), (int)m_peer->GetValue());
433 else
434 // Use max so that the current value doesn't drift as centering would need to change
435 text.Printf(wxT("%d"), m_rangeMax);
436
437 GetTextExtent(text, &valValWidth, &ht);
438
439 yborder = textheight + wxSLIDER_BORDERTEXT;
440
441 // Get slider breadth
442 if (GetWindowStyle() & wxSL_AUTOTICKS)
443 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
444 else
445 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
446
447 if (GetWindowStyle() & wxSL_VERTICAL)
448 {
449 h = h - yborder;
450
451 if ( m_macMinimumStatic )
452 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + h - yborder);
453 if ( m_macMaximumStatic )
454 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
455 if ( m_macValueStatic )
456 m_macValueStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + (h / 2) - (ht / 2));
457 }
458 else
459 {
460 if ( m_macMinimumStatic )
461 m_macMinimumStatic->Move(GetPosition().x, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
462 if ( m_macMaximumStatic )
463 m_macMaximumStatic->Move(GetPosition().x + w - maxValWidth, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
464 if ( m_macValueStatic )
465 m_macValueStatic->Move(GetPosition().x + (w / 2) - (valValWidth / 2), GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
466 }
467 }
468
469 // yet another hack since this is a composite control
470 // when wxSlider has it's size hardcoded, we're not allowed to
471 // change the size. But when the control has labels, we DO need
472 // to resize the internal Mac control to accommodate the text labels.
473 // We need to trick the wxWidgets resize mechanism so that we can
474 // resize the slider part of the control ONLY.
475
476 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
477
478 int minWidth = m_minWidth;
479
480 if (GetWindowStyle() & wxSL_LABELS)
481 {
482 // make sure we don't allow the entire control to be resized accidently
483 if (width == GetSize().x)
484 m_minWidth = -1;
485 }
486
487 // If the control has labels, we still need to call this again because
488 // the labels alter the control's w and h values.
489 wxControl::DoSetSize( x, y, w, h, sizeFlags );
490
491 m_minWidth = minWidth;
492 }
493
494 void wxSlider::DoMoveWindow(int x, int y, int width, int height)
495 {
496 wxControl::DoMoveWindow( x, y, width, height );
497 }
498
499 // Common processing to invert slider values based on wxSL_INVERSE
500 int wxSlider::ValueInvertOrNot(int value) const
501 {
502 int result = 0;
503
504 if (m_windowStyle & wxSL_VERTICAL)
505 {
506 // The reason for the backwards logic is that Mac's vertical sliders are
507 // inverted compared to Windows and GTK, hence we want inversion to be the
508 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
509 if (m_windowStyle & wxSL_INVERSE)
510 result = value;
511 else
512 result = (m_rangeMax + m_rangeMin) - value;
513 }
514 else // normal logic applies to HORIZONTAL sliders
515 {
516 result = wxSliderBase::ValueInvertOrNot(value);
517 }
518
519 return result;
520 }
521
522 #endif // wxUSE_SLIDER