]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/slider.cpp
moved wxWindow::GetBestSize implementation into DoGetBestSize to make it easier to...
[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 delete m_macMinimumStatic ;
128 delete m_macMaximumStatic ;
129 delete m_macValueStatic ;
130 }
131
132 int wxSlider::GetValue() const
133 {
134 return m_peer->GetValue() ;
135 }
136
137 void wxSlider::SetValue(int value)
138 {
139 wxString valuestring ;
140 valuestring.Printf( wxT("%d") , value ) ;
141 if ( m_macValueStatic )
142 m_macValueStatic->SetLabel( valuestring ) ;
143 m_peer->SetValue( value ) ;
144 }
145
146 void wxSlider::SetRange(int minValue, int maxValue)
147 {
148 wxString value;
149
150 m_rangeMin = minValue;
151 m_rangeMax = maxValue;
152
153 m_peer->SetMinimum( m_rangeMin);
154 m_peer->SetMaximum( m_rangeMax);
155
156 if(m_macMinimumStatic) {
157 value.Printf(wxT("%d"), m_rangeMin);
158 m_macMinimumStatic->SetLabel(value);
159 }
160 if(m_macMaximumStatic) {
161 value.Printf(wxT("%d"), m_rangeMax);
162 m_macMaximumStatic->SetLabel(value);
163 }
164 SetValue(m_rangeMin);
165 }
166
167 // For trackbars only
168 void wxSlider::SetTickFreq(int n, int pos)
169 {
170 // TODO
171 m_tickFreq = n;
172 }
173
174 void wxSlider::SetPageSize(int pageSize)
175 {
176 // TODO
177 m_pageSize = pageSize;
178 }
179
180 int wxSlider::GetPageSize() const
181 {
182 return m_pageSize;
183 }
184
185 void wxSlider::ClearSel()
186 {
187 // TODO
188 }
189
190 void wxSlider::ClearTicks()
191 {
192 // TODO
193 }
194
195 void wxSlider::SetLineSize(int lineSize)
196 {
197 m_lineSize = lineSize;
198 // TODO
199 }
200
201 int wxSlider::GetLineSize() const
202 {
203 // TODO
204 return 0;
205 }
206
207 int wxSlider::GetSelEnd() const
208 {
209 // TODO
210 return 0;
211 }
212
213 int wxSlider::GetSelStart() const
214 {
215 // TODO
216 return 0;
217 }
218
219 void wxSlider::SetSelection(int minPos, int maxPos)
220 {
221 // TODO
222 }
223
224 void wxSlider::SetThumbLength(int len)
225 {
226 // TODO
227 }
228
229 int wxSlider::GetThumbLength() const
230 {
231 // TODO
232 return 0;
233 }
234
235 void wxSlider::SetTick(int tickPos)
236 {
237 // TODO
238 }
239
240 void wxSlider::Command (wxCommandEvent & event)
241 {
242 SetValue (event.GetInt());
243 ProcessCommand (event);
244 }
245
246 void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown )
247 {
248 SInt16 value = m_peer->GetValue() ;
249
250 SetValue( value ) ;
251
252 wxEventType scrollEvent = wxEVT_NULL ;
253
254 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
255
256 wxScrollEvent event(scrollEvent, m_windowId);
257 event.SetPosition(value);
258 event.SetEventObject( this );
259 GetEventHandler()->ProcessEvent(event);
260
261 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
262 cevent.SetInt( value );
263 cevent.SetEventObject( this );
264
265 GetEventHandler()->ProcessEvent( cevent );
266 }
267
268 wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent )
269 {
270 SInt16 value = m_peer->GetValue() ;
271
272 SetValue( value ) ;
273
274 wxEventType scrollEvent = wxEVT_NULL ;
275
276 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
277
278 wxScrollEvent event(scrollEvent, m_windowId);
279 event.SetPosition(value);
280 event.SetEventObject( this );
281 GetEventHandler()->ProcessEvent(event);
282
283 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
284 cevent.SetInt( value );
285 cevent.SetEventObject( this );
286
287 GetEventHandler()->ProcessEvent( cevent );
288 return noErr ;
289 }
290
291
292 /* This is overloaded in wxSlider so that the proper width/height will always be used
293 * for the slider different values would cause redrawing and mouse detection problems */
294 void wxSlider::DoSetSizeHints( int minW, int minH,
295 int maxW , int maxH ,
296 int incW , int incH )
297 {
298 wxSize size = GetBestSize();
299
300 if(GetWindowStyle() & wxSL_VERTICAL) {
301 wxWindow::DoSetSizeHints(size.x, minH, size.x, maxH, incW, incH);
302 }
303 else {
304 wxWindow::DoSetSizeHints(minW, size.y, maxW, size.y, incW, incH);
305 }
306 }
307
308 wxSize wxSlider::DoGetBestSize() const
309 {
310 wxSize size;
311 int textwidth, textheight;
312
313 if(GetWindowStyle() & wxSL_LABELS)
314 {
315 wxString text;
316 int ht, wd;
317
318 // Get maximum text label width and height
319 text.Printf(wxT("%d"), m_rangeMin);
320 GetTextExtent(text, &textwidth, &textheight);
321 text.Printf(wxT("%d"), m_rangeMax);
322 GetTextExtent(text, &wd, &ht);
323 if(ht > textheight) {
324 textheight = ht;
325 }
326 if (wd > textwidth) {
327 textwidth = wd;
328 }
329 }
330
331 if(GetWindowStyle() & wxSL_VERTICAL)
332 {
333 if(GetWindowStyle() & wxSL_AUTOTICKS) {
334 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
335 }
336 else {
337 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
338 }
339 if(GetWindowStyle() & wxSL_LABELS) {
340 size.x += textwidth + wxSLIDER_BORDERTEXT;
341 }
342 size.y = 150;
343 }
344 else
345 {
346 if(GetWindowStyle() & wxSL_AUTOTICKS) {
347 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
348 }
349 else {
350 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
351 }
352 if(GetWindowStyle() & wxSL_LABELS) {
353 size.y += textheight + wxSLIDER_BORDERTEXT;
354 }
355 size.x = 150;
356 }
357 return size;
358 }
359
360 void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
361 {
362 int xborder, yborder;
363 int minValWidth, maxValWidth, textwidth, textheight;
364 int sliderBreadth;
365
366 xborder = yborder = 0;
367
368 if (GetWindowStyle() & wxSL_LABELS)
369 {
370 //Labels have this control's parent as their parent
371 //so if this control is not at 0,0 relative to the parent
372 //the labels need to know the position of this control
373 //relative to its parent in order to size properly, so
374 //move the control first so we can use GetPosition()
375 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
376
377 wxString text;
378 int ht;
379
380 // Get maximum text label width and height
381 text.Printf(wxT("%d"), m_rangeMin);
382 GetTextExtent(text, &minValWidth, &textheight);
383 text.Printf(wxT("%d"), m_rangeMax);
384 GetTextExtent(text, &maxValWidth, &ht);
385 if(ht > textheight) {
386 textheight = ht;
387 }
388 textwidth = (minValWidth > maxValWidth ? minValWidth : maxValWidth);
389
390 xborder = textwidth + wxSLIDER_BORDERTEXT;
391 yborder = textheight + wxSLIDER_BORDERTEXT;
392
393 // Get slider breadth
394 if(GetWindowStyle() & wxSL_AUTOTICKS) {
395 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
396 }
397 else {
398 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
399 }
400
401 if(GetWindowStyle() & wxSL_VERTICAL)
402 {
403 h = h - yborder ;
404
405 if ( m_macMinimumStatic )
406 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT,
407 GetPosition().y + h - yborder);
408 if ( m_macMaximumStatic )
409 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
410 if ( m_macValueStatic )
411 m_macValueStatic->Move(GetPosition().x, GetPosition().y + h );
412 }
413 else
414 {
415 w = w - xborder ;
416 if ( m_macMinimumStatic )
417 m_macMinimumStatic->Move(GetPosition().x + 0, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
418 if ( m_macMaximumStatic )
419 m_macMaximumStatic->Move(GetPosition().x + w - (maxValWidth/2),
420 GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
421 if ( m_macValueStatic )
422 m_macValueStatic->Move(GetPosition().x + w, GetPosition().y + 0);
423 }
424 }
425 //If the control has labels, we still need to call this again because
426 //the labels alter the control's w and h values.
427 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
428
429 }
430
431 void wxSlider::DoMoveWindow(int x, int y, int width, int height)
432 {
433 wxControl::DoMoveWindow(x,y,width,height) ;
434 }
435
436 #endif // wxUSE_SLIDER