]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/slider.cpp
fixed memory leak and optimized GetSubBitmap (Kudos to Eric Lavigne)
[wxWidgets.git] / src / mac / carbon / slider.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: slider.cpp
3 // Purpose: wxSlider
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "slider.h"
14 #endif
15
16 #include "wx/slider.h"
17 #include "wx/mac/uma.h"
18
19 #if !USE_SHARED_LIBRARY
20 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
21
22 BEGIN_EVENT_TABLE(wxSlider, wxControl)
23 END_EVENT_TABLE()
24 #endif
25
26 // The dimensions of the different styles of sliders (From Aqua document)
27 #define wxSLIDER_DIMENSIONACROSS 15
28 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
29 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
30
31 // Distance between slider and text
32 #define wxSLIDER_BORDERTEXT 5
33
34 /* NB! The default orientation for a slider is horizontal however if the user specifies
35 * some slider styles but dosen't specify the orientation we have to assume he wants a
36 * horizontal one. Therefore in this file when testing for the sliders orientation
37 * vertical is tested for if this is not set then we use the horizontal one
38 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
39 */
40
41
42 // Slider
43 wxSlider::wxSlider()
44 {
45 m_pageSize = 1;
46 m_lineSize = 1;
47 m_rangeMax = 0;
48 m_rangeMin = 0;
49 m_tickFreq = 0;
50 }
51
52 extern ControlActionUPP wxMacLiveScrollbarActionUPP ;
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 Rect bounds ;
62 Str255 title ;
63 SInt16 procID;
64
65 m_macMinimumStatic = NULL ;
66 m_macMaximumStatic = NULL ;
67 m_macValueStatic = NULL ;
68
69
70 m_lineSize = 1;
71 m_tickFreq = 0;
72
73 m_rangeMax = maxValue;
74 m_rangeMin = minValue;
75
76 m_pageSize = (int)((maxValue-minValue)/10);
77
78 MacPreControlCreate( parent, id, "", pos, size, style,
79 validator, name, &bounds, title );
80
81 procID = kControlSliderProc + kControlSliderLiveFeedback;
82 if(style & wxSL_AUTOTICKS) {
83 procID += kControlSliderHasTickMarks;
84 }
85
86
87 m_macControl = UMANewControl( parent->GetMacRootWindow(), &bounds, title, false,
88 value, minValue, maxValue, procID, (long) this);
89
90 wxASSERT_MSG( m_macControl != NULL , "No valid mac control" ) ;
91
92 ::SetControlAction( m_macControl , wxMacLiveScrollbarActionUPP ) ;
93
94 if(style & wxSL_LABELS)
95 {
96 m_macMinimumStatic = new wxStaticText( this, -1, "" );
97 m_macMaximumStatic = new wxStaticText( this, -1, "" );
98 m_macValueStatic = new wxStaticText( this, -1, "" );
99 SetRange(minValue, maxValue);
100 SetValue(value);
101 }
102
103 else {
104 m_macMinimumStatic = NULL ;
105 m_macMaximumStatic = NULL ;
106 m_macValueStatic = NULL ;
107 }
108
109 if(style & wxSL_VERTICAL) {
110 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
111 }
112 else {
113 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
114 }
115 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
116 // proper dimensions, it also means other people cannot bugger the slider with
117 // other values
118
119 MacPostControlCreate() ;
120
121 return true;
122 }
123
124 wxSlider::~wxSlider()
125 {
126 }
127
128 int wxSlider::GetValue() const
129 {
130 return GetControlValue( m_macControl) ;
131 }
132
133 void wxSlider::SetValue(int value)
134 {
135 wxString valuestring ;
136 valuestring.Printf( "%d" , value ) ;
137 if ( m_macValueStatic )
138 m_macValueStatic->SetLabel( valuestring ) ;
139 SetControlValue( m_macControl , value ) ;
140 }
141
142 void wxSlider::SetRange(int minValue, int maxValue)
143 {
144 wxString value;
145
146 m_rangeMin = minValue;
147 m_rangeMax = maxValue;
148
149 SetControlMinimum(m_macControl, m_rangeMin);
150 SetControlMaximum(m_macControl, m_rangeMax);
151
152 if(m_macMinimumStatic) {
153 value.Printf("%d", m_rangeMin);
154 m_macMinimumStatic->SetLabel(value);
155 }
156 if(m_macMaximumStatic) {
157 value.Printf("%d", m_rangeMax);
158 m_macMaximumStatic->SetLabel(value);
159 }
160 SetValue(m_rangeMin);
161 }
162
163 // For trackbars only
164 void wxSlider::SetTickFreq(int n, int pos)
165 {
166 // TODO
167 m_tickFreq = n;
168 }
169
170 void wxSlider::SetPageSize(int pageSize)
171 {
172 // TODO
173 m_pageSize = pageSize;
174 }
175
176 int wxSlider::GetPageSize() const
177 {
178 return m_pageSize;
179 }
180
181 void wxSlider::ClearSel()
182 {
183 // TODO
184 }
185
186 void wxSlider::ClearTicks()
187 {
188 // TODO
189 }
190
191 void wxSlider::SetLineSize(int lineSize)
192 {
193 m_lineSize = lineSize;
194 // TODO
195 }
196
197 int wxSlider::GetLineSize() const
198 {
199 // TODO
200 return 0;
201 }
202
203 int wxSlider::GetSelEnd() const
204 {
205 // TODO
206 return 0;
207 }
208
209 int wxSlider::GetSelStart() const
210 {
211 // TODO
212 return 0;
213 }
214
215 void wxSlider::SetSelection(int minPos, int maxPos)
216 {
217 // TODO
218 }
219
220 void wxSlider::SetThumbLength(int len)
221 {
222 // TODO
223 }
224
225 int wxSlider::GetThumbLength() const
226 {
227 // TODO
228 return 0;
229 }
230
231 void wxSlider::SetTick(int tickPos)
232 {
233 // TODO
234 }
235
236 void wxSlider::Command (wxCommandEvent & event)
237 {
238 SetValue (event.GetInt());
239 ProcessCommand (event);
240 }
241
242 void wxSlider::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
243 {
244 SInt16 value = ::GetControlValue( m_macControl ) ;
245
246 SetValue( value ) ;
247
248 wxScrollEvent event(wxEVT_SCROLL_THUMBTRACK, m_windowId);
249 event.SetPosition(value);
250 event.SetEventObject( this );
251 GetEventHandler()->ProcessEvent(event);
252
253 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
254 cevent.SetInt( value );
255 cevent.SetEventObject( this );
256
257 GetEventHandler()->ProcessEvent( cevent );
258 }
259
260 /* This is overloaded in wxSlider so that the proper width/height will always be used
261 * for the slider different values would cause redrawing and mouse detection problems */
262 void wxSlider::SetSizeHints( int minW, int minH,
263 int maxW , int maxH ,
264 int incW , int incH )
265 {
266 wxSize size = GetBestSize();
267
268 if(GetWindowStyle() & wxSL_VERTICAL) {
269 wxWindow::SetSizeHints(size.x, minH, size.x, maxH, incW, incH);
270 }
271 else {
272 wxWindow::SetSizeHints(minW, size.y, maxW, size.y, incW, incH);
273 }
274 }
275
276 wxSize wxSlider::DoGetBestSize() const
277 {
278 wxSize size;
279 int textwidth, textheight;
280
281 if(GetWindowStyle() & wxSL_LABELS)
282 {
283 wxString text;
284 int ht, wd;
285
286 // Get maximum text label width and height
287 text.Printf("%d", m_rangeMin);
288 GetTextExtent(text, &textwidth, &textheight);
289 text.Printf("%d", m_rangeMax);
290 GetTextExtent(text, &wd, &ht);
291 if(ht > textheight) {
292 textheight = ht;
293 }
294 if (wd > textwidth) {
295 textwidth = wd;
296 }
297 }
298
299 if(GetWindowStyle() & wxSL_VERTICAL)
300 {
301 if(GetWindowStyle() & wxSL_AUTOTICKS) {
302 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
303 }
304 else {
305 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
306 }
307 if(GetWindowStyle() & wxSL_LABELS) {
308 size.x += textwidth + wxSLIDER_BORDERTEXT;
309 }
310 size.y = 150;
311 }
312 else
313 {
314 if(GetWindowStyle() & wxSL_AUTOTICKS) {
315 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
316 }
317 else {
318 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
319 }
320 if(GetWindowStyle() & wxSL_LABELS) {
321 size.y += textheight + wxSLIDER_BORDERTEXT;
322 }
323 size.x = 150;
324 }
325 return size;
326 }
327
328 void wxSlider::DoSetSize(int x, int y, int width, int height, int sizeFlags)
329 {
330 Rect oldbounds, newbounds;
331 int new_x, new_y, new_width, new_height;
332 int mac_x, mac_y;
333
334 new_x = m_x;
335 new_y = m_y;
336 new_width = m_width;
337 new_height = m_height;
338
339 if (sizeFlags & wxSIZE_ALLOW_MINUS_ONE)
340 {
341 new_x = x;
342 new_y = y;
343 new_width = width;
344 new_height = height;
345 }
346 else
347 {
348 if (x != -1) new_x = x;
349 if (y != -1) new_y = y;
350 if (width != -1) new_width = width;
351 if (height != -1) new_height = height;
352 }
353
354 if(sizeFlags & wxSIZE_AUTO)
355 {
356 wxSize size = GetBestSize();
357 if (sizeFlags & wxSIZE_AUTO_WIDTH)
358 {
359 if (width == -1) new_width = size.x;
360 }
361 if (sizeFlags & wxSIZE_AUTO_HEIGHT)
362 {
363 if (height == -1) new_height = size.y;
364 }
365 }
366
367 AdjustForParentClientOrigin(new_x, new_y, sizeFlags);
368
369 mac_x = new_x;
370 mac_y = new_y;
371 if(GetParent()) {
372 GetParent()->MacClientToRootWindow(&mac_x, &mac_y);
373 }
374
375 GetControlBounds(m_macControl, &oldbounds);
376 oldbounds.right = oldbounds.left + m_width;
377 oldbounds.bottom = oldbounds.top + m_height;
378
379 bool doMove = false;
380 bool doResize = false;
381
382 if ( mac_x != oldbounds.left || mac_y != oldbounds.top )
383 {
384 doMove = true ;
385 }
386 if ( new_width != m_width || new_height != m_height )
387 {
388 doResize = true ;
389 }
390
391 if ( doMove || doResize )
392 {
393 // Ensure resize is within constraints
394 if ((m_minWidth != -1) && (new_width < m_minWidth)) {
395 new_width = m_minWidth;
396 }
397 if ((m_minHeight != -1) && (new_height < m_minHeight)) {
398 new_height = m_minHeight;
399 }
400 if ((m_maxWidth != -1) && (new_width > m_maxWidth)) {
401 new_width = m_maxWidth;
402 }
403 if ((m_maxHeight != -1) && (new_height > m_maxHeight)) {
404 new_height = m_maxHeight;
405 }
406
407 DoMoveWindow(new_x, new_y, new_width, new_height);
408
409 // Update window at old and new positions
410 SetRect(&newbounds, m_x, m_y, m_x + m_width, m_y + m_height);
411 WindowRef rootwindow = GetMacRootWindow();
412 InvalWindowRect( rootwindow , &oldbounds );
413 InvalWindowRect( rootwindow , &newbounds );
414
415 if ( doMove )
416 {
417 wxMoveEvent event(wxPoint(m_x, m_y), m_windowId);
418 event.SetEventObject(this);
419 GetEventHandler()->ProcessEvent(event) ;
420 }
421 if ( doResize )
422 {
423 wxSizeEvent event(wxSize(m_width, m_height), m_windowId);
424 event.SetEventObject(this);
425 GetEventHandler()->ProcessEvent(event);
426 }
427 }
428 }
429
430 void wxSlider::DoMoveWindow(int x, int y, int width, int height)
431 {
432 m_x = x;
433 m_y = y;
434 m_width = width;
435 m_height = height;
436
437 int xborder, yborder;
438 int minValWidth, maxValWidth, textwidth, textheight;
439 int sliderBreadth;
440
441 xborder = yborder = 0;
442
443 if (GetWindowStyle() & wxSL_LABELS)
444 {
445 wxString text;
446 int ht;
447
448 // Get maximum text label width and height
449 text.Printf("%d", m_rangeMin);
450 GetTextExtent(text, &minValWidth, &textheight);
451 text.Printf("%d", m_rangeMax);
452 GetTextExtent(text, &maxValWidth, &ht);
453 if(ht > textheight) {
454 textheight = ht;
455 }
456 textwidth = (minValWidth > maxValWidth ? minValWidth : maxValWidth);
457
458 xborder = textwidth + wxSLIDER_BORDERTEXT;
459 yborder = textheight + wxSLIDER_BORDERTEXT;
460
461 // Get slider breadth
462 if(GetWindowStyle() & wxSL_AUTOTICKS) {
463 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
464 }
465 else {
466 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
467 }
468
469 if(GetWindowStyle() & wxSL_VERTICAL)
470 {
471 m_macMinimumStatic->Move(sliderBreadth + wxSLIDER_BORDERTEXT,
472 height - yborder - textheight);
473 m_macMaximumStatic->Move(sliderBreadth + wxSLIDER_BORDERTEXT, 0);
474 m_macValueStatic->Move(0, height - textheight);
475 }
476 else
477 {
478 m_macMinimumStatic->Move(0, sliderBreadth + wxSLIDER_BORDERTEXT);
479 m_macMaximumStatic->Move(width - xborder - maxValWidth / 2,
480 sliderBreadth + wxSLIDER_BORDERTEXT);
481 m_macValueStatic->Move(width - textwidth, 0);
482 }
483 }
484
485 if(GetParent()) {
486 GetParent()->MacClientToRootWindow(&x, &y);
487 }
488 UMAMoveControl(m_macControl, x, y);
489 UMASizeControl(m_macControl, width - xborder, height - yborder);
490 }