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