]> git.saurik.com Git - wxWidgets.git/blob - src/osx/slider_osx.cpp
use text cursor only when control is enabled, #10347
[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 25
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::MacHandleControlClick(WXWidget WXUNUSED(control),
281 wxInt16 WXUNUSED(controlpart),
282 bool WXUNUSED(mouseStillDown))
283 {
284 // Whatever the native value is, we may need to invert it for calling
285 // SetValue and putting the possibly inverted value in the event
286 int value = ValueInvertOrNot( m_peer->GetValue() );
287
288 SetValue( value );
289
290 wxScrollEvent event( wxEVT_SCROLL_THUMBTRACK, m_windowId );
291 event.SetPosition( value );
292 event.SetEventObject( this );
293 HandleWindowEvent( event );
294
295 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
296 cevent.SetInt( value );
297 cevent.SetEventObject( this );
298 HandleWindowEvent( cevent );
299 }
300
301 bool wxSlider::HandleClicked( double timestampsec )
302 {
303 // Whatever the native value is, we may need to invert it for calling
304 // SetValue and putting the possibly inverted value in the event
305 int value = ValueInvertOrNot( m_peer->GetValue() ) ;
306
307 SetValue( value ) ;
308
309 wxScrollEvent event( wxEVT_SCROLL_THUMBRELEASE, m_windowId );
310 event.SetPosition( value );
311 event.SetEventObject( this );
312 HandleWindowEvent( event );
313
314 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
315 cevent.SetInt( value );
316 cevent.SetEventObject( this );
317
318 HandleWindowEvent( cevent );
319
320 return true;
321 }
322
323 // This is overloaded in wxSlider so that the proper width/height will always be used
324 // for the slider different values would cause redrawing and mouse detection problems
325 //
326 void wxSlider::DoSetSizeHints( int minW, int minH,
327 int maxW, int maxH,
328 int WXUNUSED(incW), int WXUNUSED(incH) )
329 {
330 wxSize size = GetBestSize();
331
332 if (GetWindowStyle() & wxSL_VERTICAL)
333 {
334 SetMinSize( wxSize(size.x,minH) );
335 SetMaxSize( wxSize(size.x,maxH) );
336 }
337 else
338 {
339 SetMinSize( wxSize(minW,size.y) );
340 SetMaxSize( wxSize(maxW,size.y) );
341 }
342 }
343
344 wxSize wxSlider::DoGetBestSize() const
345 {
346 wxSize size;
347 int textwidth, textheight;
348 int mintwidth, mintheight;
349 int maxtwidth, maxtheight;
350
351 textwidth = textheight = 0;
352 mintwidth = mintheight = 0;
353 maxtwidth = maxtheight = 0;
354
355 if (GetWindowStyle() & wxSL_LABELS)
356 {
357 wxString text;
358
359 // Get maximum text label width and height
360 text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
361 GetTextExtent(text, &mintwidth, &mintheight);
362 text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
363 GetTextExtent(text, &maxtwidth, &maxtheight);
364
365 if (maxtheight > mintheight)
366 textheight = maxtheight;
367 else
368 textheight = mintheight;
369
370 if (maxtwidth > mintwidth)
371 textwidth = maxtwidth;
372 else
373 textwidth = mintwidth;
374 }
375
376 if (GetWindowStyle() & wxSL_VERTICAL)
377 {
378 size.y = 150;
379
380 if (GetWindowStyle() & wxSL_AUTOTICKS)
381 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
382 else
383 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
384
385 if (GetWindowStyle() & wxSL_LABELS)
386 size.x += textwidth + wxSLIDER_BORDERTEXT;
387 }
388 else
389 {
390 size.x = 150;
391
392 if (GetWindowStyle() & wxSL_AUTOTICKS)
393 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
394 else
395 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
396
397 if (GetWindowStyle() & wxSL_LABELS)
398 {
399 size.y += textheight + wxSLIDER_BORDERTEXT;
400 size.x += (mintwidth / 2) + (maxtwidth / 2);
401 }
402 }
403
404 return size;
405 }
406
407 void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
408 {
409 int yborder = 0;
410 int minValWidth, maxValWidth, textheight;
411 int sliderBreadth;
412 int width = w;
413
414 if (GetWindowStyle() & wxSL_LABELS)
415 {
416 wxString text;
417 int ht, valValWidth;
418
419 // Get maximum text label width and height
420 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
421 GetTextExtent(text, &minValWidth, &textheight);
422 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
423 GetTextExtent(text, &maxValWidth, &ht);
424
425 if (ht > textheight)
426 textheight = ht;
427
428 if (GetWindowStyle() & wxSL_HORIZONTAL)
429 {
430 if ( m_macMinimumStatic )
431 {
432 w -= minValWidth / 2;
433 x += minValWidth / 2;
434 }
435
436 if ( m_macMaximumStatic )
437 w -= maxValWidth / 2;
438 }
439
440 // Labels have this control's parent as their parent
441 // so if this control is not at 0,0 relative to the parent
442 // the labels need to know the position of this control
443 // relative to its parent in order to size properly, so
444 // move the control first so we can use GetPosition()
445 wxControl::DoSetSize( x, y, w, h, sizeFlags );
446
447 if (GetWindowStyle() & wxSL_VERTICAL)
448 // If vertical, use current value
449 text.Printf(wxT("%d"), (int)m_peer->GetValue());
450 else
451 // Use max so that the current value doesn't drift as centering would need to change
452 text.Printf(wxT("%d"), m_rangeMax);
453
454 GetTextExtent(text, &valValWidth, &ht);
455
456 yborder = textheight + wxSLIDER_BORDERTEXT;
457
458 // Get slider breadth
459 if (GetWindowStyle() & wxSL_AUTOTICKS)
460 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
461 else
462 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
463
464 if (GetWindowStyle() & wxSL_VERTICAL)
465 {
466 h = h - yborder;
467
468 if ( m_macMinimumStatic )
469 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + h - yborder);
470 if ( m_macMaximumStatic )
471 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
472 if ( m_macValueStatic )
473 m_macValueStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + (h / 2) - (ht / 2));
474 }
475 else
476 {
477 if ( m_macMinimumStatic )
478 m_macMinimumStatic->Move(GetPosition().x, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
479 if ( m_macMaximumStatic )
480 m_macMaximumStatic->Move(GetPosition().x + w - maxValWidth, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
481 if ( m_macValueStatic )
482 m_macValueStatic->Move(GetPosition().x + (w / 2) - (valValWidth / 2), GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
483 }
484 }
485
486 // yet another hack since this is a composite control
487 // when wxSlider has it's size hardcoded, we're not allowed to
488 // change the size. But when the control has labels, we DO need
489 // to resize the internal Mac control to accommodate the text labels.
490 // We need to trick the wxWidgets resize mechanism so that we can
491 // resize the slider part of the control ONLY.
492
493 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
494
495 int minWidth = m_minWidth;
496
497 if (GetWindowStyle() & wxSL_LABELS)
498 {
499 // make sure we don't allow the entire control to be resized accidently
500 if (width == GetSize().x)
501 m_minWidth = -1;
502 }
503
504 // If the control has labels, we still need to call this again because
505 // the labels alter the control's w and h values.
506 wxControl::DoSetSize( x, y, w, h, sizeFlags );
507
508 m_minWidth = minWidth;
509 }
510
511 void wxSlider::DoMoveWindow(int x, int y, int width, int height)
512 {
513 wxControl::DoMoveWindow( x, y, width, height );
514 }
515
516 // Common processing to invert slider values based on wxSL_INVERSE
517 int wxSlider::ValueInvertOrNot(int value) const
518 {
519 int result = 0;
520
521 if (m_windowStyle & wxSL_VERTICAL)
522 {
523 // The reason for the backwards logic is that Mac's vertical sliders are
524 // inverted compared to Windows and GTK, hence we want inversion to be the
525 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
526 if (m_windowStyle & wxSL_INVERSE)
527 result = value;
528 else
529 result = (m_rangeMax + m_rangeMin) - value;
530 }
531 else // normal logic applies to HORIZONTAL sliders
532 {
533 result = wxSliderBase::ValueInvertOrNot(value);
534 }
535
536 return result;
537 }
538
539 #endif // wxUSE_SLIDER