]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/slider.cpp
pass by reference, not value
[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 UInt16 tickMarks = 0 ;
85 if ( style & wxSL_AUTOTICKS )
86 tickMarks = (maxValue - minValue);
87
88 if (tickMarks > 20)
89 tickMarks = tickMarks/5; //keep the number of tickmarks from becoming unwieldly
90
91 m_peer = new wxMacControl() ;
92 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds ,
93 value , minValue , maxValue , kControlSliderPointsDownOrRight , tickMarks , true /* liveTracking */ ,
94 wxMacLiveScrollbarActionUPP , m_peer->GetControlRefAddr() ) );
95
96
97 if(style & wxSL_VERTICAL) {
98 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
99 }
100 else {
101 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
102 }
103 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
104 // proper dimensions, it also means other people cannot bugger the slider with
105 // other values
106
107 if(style & wxSL_LABELS)
108 {
109 m_macMinimumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
110 m_macMaximumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
111 m_macValueStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
112 SetRange(minValue, maxValue);
113 SetValue(value);
114 }
115
116 MacPostControlCreate(pos,size) ;
117
118 return true;
119 }
120
121 wxSlider::~wxSlider()
122 {
123 delete m_macMinimumStatic ;
124 delete m_macMaximumStatic ;
125 delete m_macValueStatic ;
126 }
127
128 int wxSlider::GetValue() const
129 {
130 return m_peer->GetValue() ;
131 }
132
133 void wxSlider::SetValue(int value)
134 {
135 wxString valuestring ;
136 valuestring.Printf( wxT("%d") , value ) ;
137 if ( m_macValueStatic )
138 m_macValueStatic->SetLabel( valuestring ) ;
139 m_peer->SetValue( 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 m_peer->SetMinimum( m_rangeMin);
150 m_peer->SetMaximum( m_rangeMax);
151
152 if(m_macMinimumStatic) {
153 value.Printf(wxT("%d"), m_rangeMin);
154 m_macMinimumStatic->SetLabel(value);
155 }
156 if(m_macMaximumStatic) {
157 value.Printf(wxT("%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( WXWidget control , wxInt16 controlpart, bool mouseStillDown )
243 {
244 SInt16 value = m_peer->GetValue() ;
245
246 SetValue( value ) ;
247
248 wxEventType scrollEvent = wxEVT_NULL ;
249
250 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
251
252 wxScrollEvent event(scrollEvent, m_windowId);
253 event.SetPosition(value);
254 event.SetEventObject( this );
255 GetEventHandler()->ProcessEvent(event);
256
257 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
258 cevent.SetInt( value );
259 cevent.SetEventObject( this );
260
261 GetEventHandler()->ProcessEvent( cevent );
262 }
263
264 wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent )
265 {
266 SInt16 value = m_peer->GetValue() ;
267
268 SetValue( value ) ;
269
270 wxEventType scrollEvent = wxEVT_NULL ;
271
272 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
273
274 wxScrollEvent event(scrollEvent, m_windowId);
275 event.SetPosition(value);
276 event.SetEventObject( this );
277 GetEventHandler()->ProcessEvent(event);
278
279 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
280 cevent.SetInt( value );
281 cevent.SetEventObject( this );
282
283 GetEventHandler()->ProcessEvent( cevent );
284 return noErr ;
285 }
286
287
288 /* This is overloaded in wxSlider so that the proper width/height will always be used
289 * for the slider different values would cause redrawing and mouse detection problems */
290 void wxSlider::SetSizeHints( int minW, int minH,
291 int maxW , int maxH ,
292 int incW , int incH )
293 {
294 wxSize size = GetBestSize();
295
296 if(GetWindowStyle() & wxSL_VERTICAL) {
297 wxWindow::SetSizeHints(size.x, minH, size.x, maxH, incW, incH);
298 }
299 else {
300 wxWindow::SetSizeHints(minW, size.y, maxW, size.y, incW, incH);
301 }
302 }
303
304 wxSize wxSlider::DoGetBestSize() const
305 {
306 wxSize size;
307 int textwidth, textheight;
308
309 if(GetWindowStyle() & wxSL_LABELS)
310 {
311 wxString text;
312 int ht, wd;
313
314 // Get maximum text label width and height
315 text.Printf(wxT("%d"), m_rangeMin);
316 GetTextExtent(text, &textwidth, &textheight);
317 text.Printf(wxT("%d"), m_rangeMax);
318 GetTextExtent(text, &wd, &ht);
319 if(ht > textheight) {
320 textheight = ht;
321 }
322 if (wd > textwidth) {
323 textwidth = wd;
324 }
325 }
326
327 if(GetWindowStyle() & wxSL_VERTICAL)
328 {
329 if(GetWindowStyle() & wxSL_AUTOTICKS) {
330 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
331 }
332 else {
333 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
334 }
335 if(GetWindowStyle() & wxSL_LABELS) {
336 size.x += textwidth + wxSLIDER_BORDERTEXT;
337 }
338 size.y = 150;
339 }
340 else
341 {
342 if(GetWindowStyle() & wxSL_AUTOTICKS) {
343 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
344 }
345 else {
346 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
347 }
348 if(GetWindowStyle() & wxSL_LABELS) {
349 size.y += textheight + wxSLIDER_BORDERTEXT;
350 }
351 size.x = 150;
352 }
353 return size;
354 }
355
356 void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
357 {
358 int xborder, yborder;
359 int minValWidth, maxValWidth, textwidth, textheight;
360 int sliderBreadth;
361
362 xborder = yborder = 0;
363
364 if (GetWindowStyle() & wxSL_LABELS)
365 {
366 //Labels have this control's parent as their parent
367 //so if this control is not at 0,0 relative to the parent
368 //the labels need to know the position of this control
369 //relative to its parent in order to size properly, so
370 //move the control first so we can use GetPosition()
371 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
372
373 wxString text;
374 int ht;
375
376 // Get maximum text label width and height
377 text.Printf(wxT("%d"), m_rangeMin);
378 GetTextExtent(text, &minValWidth, &textheight);
379 text.Printf(wxT("%d"), m_rangeMax);
380 GetTextExtent(text, &maxValWidth, &ht);
381 if(ht > textheight) {
382 textheight = ht;
383 }
384 textwidth = (minValWidth > maxValWidth ? minValWidth : maxValWidth);
385
386 xborder = textwidth + wxSLIDER_BORDERTEXT;
387 yborder = textheight + wxSLIDER_BORDERTEXT;
388
389 // Get slider breadth
390 if(GetWindowStyle() & wxSL_AUTOTICKS) {
391 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
392 }
393 else {
394 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
395 }
396
397 if(GetWindowStyle() & wxSL_VERTICAL)
398 {
399 h = h - yborder ;
400
401 if ( m_macMinimumStatic )
402 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT,
403 GetPosition().y + h - yborder);
404 if ( m_macMaximumStatic )
405 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
406 if ( m_macValueStatic )
407 m_macValueStatic->Move(GetPosition().x, GetPosition().y + h );
408 }
409 else
410 {
411 w = w - xborder ;
412 if ( m_macMinimumStatic )
413 m_macMinimumStatic->Move(GetPosition().x + 0, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
414 if ( m_macMaximumStatic )
415 m_macMaximumStatic->Move(GetPosition().x + w - (maxValWidth/2),
416 GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
417 if ( m_macValueStatic )
418 m_macValueStatic->Move(GetPosition().x + w, GetPosition().y + 0);
419 }
420 }
421 //If the control has labels, we still need to call this again because
422 //the labels alter the control's w and h values.
423 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
424
425 }
426
427 void wxSlider::DoMoveWindow(int x, int y, int width, int height)
428 {
429 wxControl::DoMoveWindow(x,y,width,height) ;
430 }
431
432 #endif // wxUSE_SLIDER