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