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