]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/slider.cpp
optimizing gauge cpu usage
[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 #ifdef __GNUG__
13 #pragma implementation "slider.h"
14 #endif
15
16 #include "wx/defs.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
74 m_lineSize = 1;
75 m_tickFreq = 0;
76
77 m_rangeMax = maxValue;
78 m_rangeMin = minValue;
79
80 m_pageSize = (int)((maxValue-minValue)/10);
81
82 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
83
84 //
85 // NB: (RN) Ticks here are sometimes off in the GUI if there
86 // is not as many ticks as there are values
87 //
88 UInt16 tickMarks = 0 ;
89 if ( style & wxSL_AUTOTICKS )
90 tickMarks = (maxValue - minValue) + 1; //+1 for the 0 value
91
92 while (tickMarks > 20)
93 tickMarks /= 5; //keep the number of tickmarks from becoming unwieldly
94
95 m_peer = new wxMacControl() ;
96 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds ,
97 value , minValue , maxValue , kControlSliderPointsDownOrRight , tickMarks , true /* liveTracking */ ,
98 wxMacLiveScrollbarActionUPP , m_peer->GetControlRefAddr() ) );
99
100
101 if(style & wxSL_VERTICAL) {
102 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
103 }
104 else {
105 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
106 }
107 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
108 // proper dimensions, it also means other people cannot bugger the slider with
109 // other values
110
111 if(style & wxSL_LABELS)
112 {
113 m_macMinimumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
114 m_macMaximumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
115 m_macValueStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
116 SetRange(minValue, maxValue);
117 SetValue(value);
118 }
119
120 MacPostControlCreate(pos,size) ;
121
122 return true;
123 }
124
125 wxSlider::~wxSlider()
126 {
127 // this is a special case, as we had to add windows as siblings we are
128 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
129 if ( m_parent && m_parent->IsBeingDeleted() == false )
130 {
131 delete m_macMinimumStatic ;
132 delete m_macMaximumStatic ;
133 delete m_macValueStatic ;
134 }
135 }
136
137 int wxSlider::GetValue() const
138 {
139 return 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 m_peer->SetValue( value ) ;
149 }
150
151 void wxSlider::SetRange(int minValue, int maxValue)
152 {
153 wxString value;
154
155 m_rangeMin = minValue;
156 m_rangeMax = maxValue;
157
158 m_peer->SetMinimum( m_rangeMin);
159 m_peer->SetMaximum( m_rangeMax);
160
161 if(m_macMinimumStatic) {
162 value.Printf(wxT("%d"), m_rangeMin);
163 m_macMinimumStatic->SetLabel(value);
164 }
165 if(m_macMaximumStatic) {
166 value.Printf(wxT("%d"), m_rangeMax);
167 m_macMaximumStatic->SetLabel(value);
168 }
169 SetValue(m_rangeMin);
170 }
171
172 // For trackbars only
173 void wxSlider::SetTickFreq(int n, int pos)
174 {
175 // TODO
176 m_tickFreq = n;
177 }
178
179 void wxSlider::SetPageSize(int pageSize)
180 {
181 // TODO
182 m_pageSize = pageSize;
183 }
184
185 int wxSlider::GetPageSize() const
186 {
187 return m_pageSize;
188 }
189
190 void wxSlider::ClearSel()
191 {
192 // TODO
193 }
194
195 void wxSlider::ClearTicks()
196 {
197 // TODO
198 }
199
200 void wxSlider::SetLineSize(int lineSize)
201 {
202 m_lineSize = lineSize;
203 // TODO
204 }
205
206 int wxSlider::GetLineSize() const
207 {
208 // TODO
209 return 0;
210 }
211
212 int wxSlider::GetSelEnd() const
213 {
214 // TODO
215 return 0;
216 }
217
218 int wxSlider::GetSelStart() const
219 {
220 // TODO
221 return 0;
222 }
223
224 void wxSlider::SetSelection(int minPos, int maxPos)
225 {
226 // TODO
227 }
228
229 void wxSlider::SetThumbLength(int len)
230 {
231 // TODO
232 }
233
234 int wxSlider::GetThumbLength() const
235 {
236 // TODO
237 return 0;
238 }
239
240 void wxSlider::SetTick(int tickPos)
241 {
242 // TODO
243 }
244
245 void wxSlider::Command (wxCommandEvent & event)
246 {
247 SetValue (event.GetInt());
248 ProcessCommand (event);
249 }
250
251 void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown )
252 {
253 SInt16 value = m_peer->GetValue() ;
254
255 SetValue( value ) ;
256
257 wxEventType scrollEvent = wxEVT_NULL ;
258
259 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
260
261 wxScrollEvent event(scrollEvent, m_windowId);
262 event.SetPosition(value);
263 event.SetEventObject( this );
264 GetEventHandler()->ProcessEvent(event);
265
266 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
267 cevent.SetInt( value );
268 cevent.SetEventObject( this );
269
270 GetEventHandler()->ProcessEvent( cevent );
271 }
272
273 wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent )
274 {
275 SInt16 value = m_peer->GetValue() ;
276
277 SetValue( value ) ;
278
279 wxEventType scrollEvent = wxEVT_NULL ;
280
281 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
282
283 wxScrollEvent event(scrollEvent, m_windowId);
284 event.SetPosition(value);
285 event.SetEventObject( this );
286 GetEventHandler()->ProcessEvent(event);
287
288 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
289 cevent.SetInt( value );
290 cevent.SetEventObject( this );
291
292 GetEventHandler()->ProcessEvent( cevent );
293 return noErr ;
294 }
295
296
297 /* This is overloaded in wxSlider so that the proper width/height will always be used
298 * for the slider different values would cause redrawing and mouse detection problems */
299 void wxSlider::DoSetSizeHints( int minW, int minH,
300 int maxW , int maxH ,
301 int incW , int incH )
302 {
303 wxSize size = GetBestSize();
304
305 if(GetWindowStyle() & wxSL_VERTICAL) {
306 wxWindow::DoSetSizeHints(size.x, minH, size.x, maxH, incW, incH);
307 }
308 else {
309 wxWindow::DoSetSizeHints(minW, size.y, maxW, size.y, incW, incH);
310 }
311 }
312
313 wxSize wxSlider::DoGetBestSize() const
314 {
315 wxSize size;
316 int textwidth, textheight;
317
318 if(GetWindowStyle() & wxSL_LABELS)
319 {
320 wxString text;
321 int ht, wd;
322
323 // Get maximum text label width and height
324 text.Printf(wxT("%d"), m_rangeMin);
325 GetTextExtent(text, &textwidth, &textheight);
326 text.Printf(wxT("%d"), m_rangeMax);
327 GetTextExtent(text, &wd, &ht);
328 if(ht > textheight) {
329 textheight = ht;
330 }
331 if (wd > textwidth) {
332 textwidth = wd;
333 }
334 }
335
336 if(GetWindowStyle() & wxSL_VERTICAL)
337 {
338 if(GetWindowStyle() & wxSL_AUTOTICKS) {
339 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
340 }
341 else {
342 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
343 }
344 if(GetWindowStyle() & wxSL_LABELS) {
345 size.x += textwidth + wxSLIDER_BORDERTEXT;
346 }
347 size.y = 150;
348 }
349 else
350 {
351 if(GetWindowStyle() & wxSL_AUTOTICKS) {
352 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
353 }
354 else {
355 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
356 }
357 if(GetWindowStyle() & wxSL_LABELS) {
358 size.y += textheight + wxSLIDER_BORDERTEXT;
359 }
360 size.x = 150;
361 }
362 return size;
363 }
364
365 void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
366 {
367 int xborder, yborder;
368 int minValWidth, maxValWidth, textwidth, textheight;
369 int sliderBreadth;
370
371 xborder = yborder = 0;
372
373 if (GetWindowStyle() & wxSL_LABELS)
374 {
375 //Labels have this control's parent as their parent
376 //so if this control is not at 0,0 relative to the parent
377 //the labels need to know the position of this control
378 //relative to its parent in order to size properly, so
379 //move the control first so we can use GetPosition()
380 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
381
382 wxString text;
383 int ht;
384
385 // Get maximum text label width and height
386 text.Printf(wxT("%d"), m_rangeMin);
387 GetTextExtent(text, &minValWidth, &textheight);
388 text.Printf(wxT("%d"), m_rangeMax);
389 GetTextExtent(text, &maxValWidth, &ht);
390 if(ht > textheight) {
391 textheight = ht;
392 }
393 textwidth = (minValWidth > maxValWidth ? minValWidth : maxValWidth);
394
395 xborder = textwidth + wxSLIDER_BORDERTEXT;
396 yborder = textheight + wxSLIDER_BORDERTEXT;
397
398 // Get slider breadth
399 if(GetWindowStyle() & wxSL_AUTOTICKS) {
400 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
401 }
402 else {
403 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
404 }
405
406 if(GetWindowStyle() & wxSL_VERTICAL)
407 {
408 h = h - yborder ;
409
410 if ( m_macMinimumStatic )
411 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT,
412 GetPosition().y + h - yborder);
413 if ( m_macMaximumStatic )
414 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
415 if ( m_macValueStatic )
416 m_macValueStatic->Move(GetPosition().x, GetPosition().y + h );
417 }
418 else
419 {
420 w = w - xborder ;
421 if ( m_macMinimumStatic )
422 m_macMinimumStatic->Move(GetPosition().x + 0, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
423 if ( m_macMaximumStatic )
424 m_macMaximumStatic->Move(GetPosition().x + w - (maxValWidth/2),
425 GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
426 if ( m_macValueStatic )
427 m_macValueStatic->Move(GetPosition().x + w, GetPosition().y + 0);
428 }
429 }
430 //If the control has labels, we still need to call this again because
431 //the labels alter the control's w and h values.
432 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
433
434 }
435
436 void wxSlider::DoMoveWindow(int x, int y, int width, int height)
437 {
438 wxControl::DoMoveWindow(x,y,width,height) ;
439 }
440
441 #endif // wxUSE_SLIDER