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