]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/slider.cpp
Replaced SOCKLEN_T by WX_SOCKLEN_T to resolve conflict with AIX system headers.
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "slider.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #if wxUSE_SLIDER
19
20 #include "wx/slider.h"
21 #include "wx/mac/uma.h"
22
23 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
24
25 BEGIN_EVENT_TABLE(wxSlider, wxControl)
26 END_EVENT_TABLE()
27
28 // The dimensions of the different styles of sliders (From Aqua document)
29 #define wxSLIDER_DIMENSIONACROSS 15
30 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
31 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
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 dosen't specify the orientation we have to assume he wants a
38 * horizontal one. Therefore in this file when testing for the sliders orientation
39 * vertical is tested for if this is not set then we use the horizontal one
40 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
41 */
42
43 // Slider
44 wxSlider::wxSlider()
45 {
46 m_pageSize = 1;
47 m_lineSize = 1;
48 m_rangeMax = 0;
49 m_rangeMin = 0;
50 m_tickFreq = 0;
51 }
52
53 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
54 int value, int minValue, int maxValue,
55 const wxPoint& pos,
56 const wxSize& size, long style,
57 const wxValidator& validator,
58 const wxString& name)
59 {
60 m_macIsUserPane = false ;
61
62 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
63 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
64 // reasons we can't really change it, instead try to infer the orientation
65 // from the flags given to us here
66 switch ( style & (wxSL_LEFT | wxSL_RIGHT | wxSL_TOP | wxSL_BOTTOM) )
67 {
68 case wxSL_LEFT:
69 case wxSL_RIGHT:
70 style |= wxSL_VERTICAL;
71 break;
72
73 case wxSL_TOP:
74 case wxSL_BOTTOM:
75 style |= wxSL_HORIZONTAL;
76 break;
77
78 case 0:
79 // no specific direction, do we have at least the orientation?
80 if ( !(style & (wxSL_HORIZONTAL | wxSL_VERTICAL)) )
81 {
82 // no, choose default
83 style |= wxSL_BOTTOM | wxSL_HORIZONTAL;
84 }
85 };
86
87 wxASSERT_MSG( !(style & wxSL_VERTICAL) || !(style & wxSL_HORIZONTAL),
88 _T("incompatible slider direction and orientation") );
89
90 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
91 return false;
92
93 m_macMinimumStatic = NULL ;
94 m_macMaximumStatic = NULL ;
95 m_macValueStatic = NULL ;
96
97 m_lineSize = 1;
98 m_tickFreq = 0;
99
100 m_rangeMax = maxValue;
101 m_rangeMin = minValue;
102
103 m_pageSize = (int)((maxValue-minValue)/10);
104
105 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
106
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 while (tickMarks > 20)
116 tickMarks /= 5; //keep the number of tickmarks from becoming unwieldly
117
118 m_peer = new wxMacControl(this) ;
119 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds ,
120 value , minValue , maxValue , kControlSliderPointsDownOrRight , tickMarks , true /* liveTracking */ ,
121 GetwxMacLiveScrollbarActionProc() , m_peer->GetControlRefAddr() ) );
122
123 if(style & wxSL_VERTICAL) {
124 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
125 }
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 SetRange(minValue, maxValue);
139 SetValue(value);
140 }
141
142 MacPostControlCreate(pos,size) ;
143
144 return true;
145 }
146
147 wxSlider::~wxSlider()
148 {
149 // this is a special case, as we had to add windows as siblings we are
150 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
151 if ( m_parent && m_parent->IsBeingDeleted() == false )
152 {
153 delete m_macMinimumStatic ;
154 delete m_macMaximumStatic ;
155 delete m_macValueStatic ;
156 }
157 }
158
159 int wxSlider::GetValue() const
160 {
161 // We may need to invert the value returned by the widget
162 return ValueInvertOrNot( m_peer->GetValue() ) ;
163 }
164
165 void wxSlider::SetValue(int value)
166 {
167 wxString valuestring ;
168 valuestring.Printf( wxT("%d") , value ) ;
169 if ( m_macValueStatic )
170 m_macValueStatic->SetLabel( valuestring ) ;
171
172 // We only invert for the setting of the actual native widget
173 m_peer->SetValue( ValueInvertOrNot ( value ) ) ;
174 }
175
176 void wxSlider::SetRange(int minValue, int maxValue)
177 {
178 wxString value;
179
180 m_rangeMin = minValue;
181 m_rangeMax = maxValue;
182
183 m_peer->SetMinimum( m_rangeMin);
184 m_peer->SetMaximum( m_rangeMax);
185
186 if(m_macMinimumStatic) {
187 value.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
188 m_macMinimumStatic->SetLabel(value);
189 }
190 if(m_macMaximumStatic) {
191 value.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
192 m_macMaximumStatic->SetLabel(value);
193 }
194 SetValue(m_rangeMin);
195 }
196
197 // For trackbars only
198 void wxSlider::SetTickFreq(int n, int pos)
199 {
200 // TODO
201 m_tickFreq = n;
202 }
203
204 void wxSlider::SetPageSize(int pageSize)
205 {
206 // TODO
207 m_pageSize = pageSize;
208 }
209
210 int wxSlider::GetPageSize() const
211 {
212 return m_pageSize;
213 }
214
215 void wxSlider::ClearSel()
216 {
217 // TODO
218 }
219
220 void wxSlider::ClearTicks()
221 {
222 // TODO
223 }
224
225 void wxSlider::SetLineSize(int lineSize)
226 {
227 m_lineSize = lineSize;
228 // TODO
229 }
230
231 int wxSlider::GetLineSize() const
232 {
233 // TODO
234 return 0;
235 }
236
237 int wxSlider::GetSelEnd() const
238 {
239 // TODO
240 return 0;
241 }
242
243 int wxSlider::GetSelStart() const
244 {
245 // TODO
246 return 0;
247 }
248
249 void wxSlider::SetSelection(int minPos, int maxPos)
250 {
251 // TODO
252 }
253
254 void wxSlider::SetThumbLength(int len)
255 {
256 // TODO
257 }
258
259 int wxSlider::GetThumbLength() const
260 {
261 // TODO
262 return 0;
263 }
264
265 void wxSlider::SetTick(int tickPos)
266 {
267 // TODO
268 }
269
270 void wxSlider::Command (wxCommandEvent & event)
271 {
272 SetValue (event.GetInt());
273 ProcessCommand (event);
274 }
275
276 void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown )
277 {
278 // Whatever the native value is, we may need to invert it for calling
279 // SetValue and putting the possibly inverted value in the event
280 SInt16 value = ValueInvertOrNot ( m_peer->GetValue() ) ;
281
282 SetValue( value ) ;
283
284 wxEventType scrollEvent = wxEVT_NULL ;
285
286 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
287
288 wxScrollEvent event(scrollEvent, m_windowId);
289 event.SetPosition(value);
290 event.SetEventObject( this );
291 GetEventHandler()->ProcessEvent(event);
292
293 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
294 cevent.SetInt( value );
295 cevent.SetEventObject( this );
296
297 GetEventHandler()->ProcessEvent( cevent );
298 }
299
300 wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent )
301 {
302 // Whatever the native value is, we may need to invert it for calling
303 // SetValue and putting the possibly inverted value in the event
304 SInt16 value = ValueInvertOrNot ( m_peer->GetValue() ) ;
305
306 SetValue( value ) ;
307
308 wxEventType scrollEvent = wxEVT_NULL ;
309
310 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
311
312 wxScrollEvent event(scrollEvent, m_windowId);
313 event.SetPosition(value);
314 event.SetEventObject( this );
315 GetEventHandler()->ProcessEvent(event);
316
317 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
318 cevent.SetInt( value );
319 cevent.SetEventObject( this );
320
321 GetEventHandler()->ProcessEvent( cevent );
322 return noErr ;
323 }
324
325 /* This is overloaded in wxSlider so that the proper width/height will always be used
326 * for the slider different values would cause redrawing and mouse detection problems */
327 void wxSlider::DoSetSizeHints( int minW, int minH,
328 int maxW , int maxH ,
329 int incW , int incH )
330 {
331 wxSize size = GetBestSize();
332
333 if(GetWindowStyle() & wxSL_VERTICAL) {
334 wxWindow::DoSetSizeHints(size.x, minH, size.x, maxH, incW, incH);
335 }
336 else {
337 wxWindow::DoSetSizeHints(minW, size.y, maxW, size.y, incW, incH);
338 }
339 }
340
341 wxSize wxSlider::DoGetBestSize() const
342 {
343 wxSize size;
344 int textwidth = 0;
345 int textheight = 0;
346 int mintwidth, mintheight;
347 int maxtwidth, maxtheight;
348
349 if(GetWindowStyle() & wxSL_LABELS)
350 {
351 wxString text;
352
353 // Get maximum text label width and height
354 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
355 GetTextExtent(text, &mintwidth, &mintheight);
356 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
357 GetTextExtent(text, &maxtwidth, &maxtheight);
358 if(maxtheight > mintheight) {
359 textheight = maxtheight;
360 }
361 else {
362 textheight = mintheight;
363 }
364 if (maxtwidth > mintwidth) {
365 textwidth = maxtwidth;
366 }
367 else {
368 textwidth = mintwidth;
369 }
370 }
371
372 if(GetWindowStyle() & wxSL_VERTICAL)
373 {
374 if(GetWindowStyle() & wxSL_AUTOTICKS) {
375 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
376 }
377 else {
378 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
379 }
380 if(GetWindowStyle() & wxSL_LABELS) {
381 size.x += textwidth + wxSLIDER_BORDERTEXT;
382 }
383 size.y = 150;
384 }
385 else
386 {
387 if(GetWindowStyle() & wxSL_AUTOTICKS) {
388 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
389 }
390 else {
391 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
392 }
393
394 size.x = 150;
395
396 if(GetWindowStyle() & wxSL_LABELS) {
397 size.y += textheight + wxSLIDER_BORDERTEXT;
398 size.x += (mintwidth/2) + (maxtwidth/2);
399 }
400 }
401 return size;
402 }
403
404 void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
405 {
406 int xborder, yborder;
407 int minValWidth, maxValWidth, textheight;
408 int sliderBreadth;
409 int width = w;
410
411 xborder = yborder = 0;
412
413 if (GetWindowStyle() & wxSL_LABELS)
414 {
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
429 if(GetWindowStyle() & wxSL_HORIZONTAL)
430 {
431 if ( m_macMinimumStatic ) {
432 w-=minValWidth/2;
433 x+=minValWidth/2;
434 }
435 if ( m_macMaximumStatic ) {
436 w-=maxValWidth/2;
437 }
438 }
439
440
441 //Labels have this control's parent as their parent
442 //so if this control is not at 0,0 relative to the parent
443 //the labels need to know the position of this control
444 //relative to its parent in order to size properly, so
445 //move the control first so we can use GetPosition()
446 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
447
448 // If vertical, use current value
449 if(GetWindowStyle() & wxSL_VERTICAL)
450 {
451 text.Printf(wxT("%d"), (int)m_peer->GetValue());
452 }
453 // Use max so that the current value doesn't drift as centering would need to change
454 else
455 {
456 text.Printf(wxT("%d"), m_rangeMax);
457 }
458
459 GetTextExtent(text, &valValWidth, &ht);
460
461 yborder = textheight + wxSLIDER_BORDERTEXT;
462
463 // Get slider breadth
464 if(GetWindowStyle() & wxSL_AUTOTICKS) {
465 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
466 }
467 else {
468 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
469 }
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;
503 minWidth = m_minWidth;
504
505 if (GetWindowStyle() & wxSL_LABELS)
506 {
507 // make sure we don't allow the entire control to be resized accidently
508 if (width == GetSize().x)
509 m_minWidth = -1;
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 if (m_windowStyle & wxSL_VERTICAL)
527 {
528 // The reason for the backwards logic is that Mac's vertical sliders are
529 // inverted compared to Windows and GTK, hence we want inversion to be the
530 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
531 if (m_windowStyle & wxSL_INVERSE)
532 return value;
533 else
534 return (m_rangeMax + m_rangeMin) - value;
535 }
536 else // normal logic applies to HORIZONTAL sliders
537 {
538 return wxSliderBase::ValueInvertOrNot(value);
539 }
540 }
541
542 #endif // wxUSE_SLIDER