]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/slider.cpp
filedata implementation streamlined
[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 SetValue( m_rangeMin );
208 }
209
210 // For trackbars only
211 void wxSlider::SetTickFreq(int n, int pos)
212 {
213 // TODO
214 m_tickFreq = n;
215 }
216
217 void wxSlider::SetPageSize(int pageSize)
218 {
219 // TODO
220 m_pageSize = pageSize;
221 }
222
223 int wxSlider::GetPageSize() const
224 {
225 return m_pageSize;
226 }
227
228 void wxSlider::ClearSel()
229 {
230 // TODO
231 }
232
233 void wxSlider::ClearTicks()
234 {
235 // TODO
236 }
237
238 void wxSlider::SetLineSize(int lineSize)
239 {
240 m_lineSize = lineSize;
241 // TODO
242 }
243
244 int wxSlider::GetLineSize() const
245 {
246 // TODO
247 return 0;
248 }
249
250 int wxSlider::GetSelEnd() const
251 {
252 // TODO
253 return 0;
254 }
255
256 int wxSlider::GetSelStart() const
257 {
258 // TODO
259 return 0;
260 }
261
262 void wxSlider::SetSelection(int minPos, int maxPos)
263 {
264 // TODO
265 }
266
267 void wxSlider::SetThumbLength(int len)
268 {
269 // TODO
270 }
271
272 int wxSlider::GetThumbLength() const
273 {
274 // TODO
275 return 0;
276 }
277
278 void wxSlider::SetTick(int tickPos)
279 {
280 // TODO
281 }
282
283 void wxSlider::Command(wxCommandEvent &event)
284 {
285 SetValue(event.GetInt());
286 ProcessCommand(event);
287 }
288
289 void wxSlider::MacHandleControlClick( WXWidget control, wxInt16 controlpart, bool mouseStillDown )
290 {
291 // Whatever the native value is, we may need to invert it for calling
292 // SetValue and putting the possibly inverted value in the event
293 int value = ValueInvertOrNot( m_peer->GetValue() );
294
295 SetValue( value );
296
297 wxEventType scrollEvent = wxEVT_NULL;
298
299 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
300
301 wxScrollEvent event(scrollEvent, m_windowId);
302 event.SetPosition(value);
303 event.SetEventObject( this );
304 GetEventHandler()->ProcessEvent(event);
305
306 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
307 cevent.SetInt( value );
308 cevent.SetEventObject( this );
309
310 GetEventHandler()->ProcessEvent( cevent );
311 }
312
313 wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent )
314 {
315 // Whatever the native value is, we may need to invert it for calling
316 // SetValue and putting the possibly inverted value in the event
317 int value = ValueInvertOrNot( m_peer->GetValue() ) ;
318
319 SetValue( value ) ;
320
321 wxEventType scrollEvent = wxEVT_NULL ;
322
323 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
324
325 wxScrollEvent event(scrollEvent, 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