]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/slider.cpp
resource fork not used anymore
[wxWidgets.git] / src / mac / carbon / slider.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: slider.cpp
3// Purpose: wxSlider
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "slider.h"
14#endif
15
16#include "wx/slider.h"
519cb848 17#include "wx/mac/uma.h"
e9576ca5 18
2f1ae414 19#if !USE_SHARED_LIBRARY
e9576ca5
SC
20IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
21
22BEGIN_EVENT_TABLE(wxSlider, wxControl)
23END_EVENT_TABLE()
2f1ae414 24#endif
e9576ca5 25
9453cf2b 26 // The dimensions of the different styles of sliders (From Aqua document)
e40298d5
JS
27#define wxSLIDER_DIMENSIONACROSS 15
28#define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
29#define wxSLIDER_DIMENSIONACROSS_ARROW 18
9453cf2b 30
e40298d5
JS
31// Distance between slider and text
32#define wxSLIDER_BORDERTEXT 5
9453cf2b 33
e40298d5
JS
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 // Slider
42 wxSlider::wxSlider()
e9576ca5 43{
e40298d5
JS
44 m_pageSize = 1;
45 m_lineSize = 1;
46 m_rangeMax = 0;
47 m_rangeMin = 0;
48 m_tickFreq = 0;
e9576ca5
SC
49}
50
519cb848
SC
51extern ControlActionUPP wxMacLiveScrollbarActionUPP ;
52
e9576ca5 53bool wxSlider::Create(wxWindow *parent, wxWindowID id,
e40298d5
JS
54 int value, int minValue, int maxValue,
55 const wxPoint& pos,
56 const wxSize& size, long style,
57 const wxValidator& validator,
58 const wxString& name)
e9576ca5 59{
facd6764
SC
60 m_macIsUserPane = FALSE ;
61
b45ed7a2
VZ
62 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
63 return false;
64
e40298d5
JS
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
facd6764 78 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
e40298d5 79
4c37f124
SC
80 UInt16 tickMarks = 0 ;
81 if ( style & wxSL_AUTOTICKS )
f26ca7f8
KO
82 tickMarks = (maxValue - minValue);
83
84 if (tickMarks > 20)
85 tickMarks = tickMarks/5; //keep the number of tickmarks from becoming unwieldly
4c37f124 86
21fd5529 87 m_peer = new wxMacControl() ;
4c37f124
SC
88 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds ,
89 value , minValue , maxValue , kControlSliderPointsDownOrRight , tickMarks , true /* liveTracking */ ,
5ca0d812 90 wxMacLiveScrollbarActionUPP , m_peer->GetControlRefAddr() ) );
21fd5529 91
facd6764 92
e40298d5
JS
93 if(style & wxSL_VERTICAL) {
94 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
95 }
96 else {
97 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
98 }
99 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
100 // proper dimensions, it also means other people cannot bugger the slider with
101 // other values
102
facd6764
SC
103 if(style & wxSL_LABELS)
104 {
105 m_macMinimumStatic = new wxStaticText( parent, -1, wxEmptyString );
106 m_macMaximumStatic = new wxStaticText( parent, -1, wxEmptyString );
107 m_macValueStatic = new wxStaticText( parent, -1, wxEmptyString );
108 SetRange(minValue, maxValue);
109 SetValue(value);
110 }
111
112 MacPostControlCreate(pos,size) ;
e40298d5
JS
113
114 return true;
e9576ca5
SC
115}
116
117wxSlider::~wxSlider()
118{
facd6764
SC
119 delete m_macMinimumStatic ;
120 delete m_macMaximumStatic ;
121 delete m_macValueStatic ;
e9576ca5
SC
122}
123
124int wxSlider::GetValue() const
125{
5ca0d812 126 return m_peer->GetValue() ;
e9576ca5
SC
127}
128
129void wxSlider::SetValue(int value)
130{
e40298d5 131 wxString valuestring ;
427ff662 132 valuestring.Printf( wxT("%d") , value ) ;
e40298d5
JS
133 if ( m_macValueStatic )
134 m_macValueStatic->SetLabel( valuestring ) ;
5ca0d812 135 m_peer->SetValue( value ) ;
e9576ca5
SC
136}
137
138void wxSlider::SetRange(int minValue, int maxValue)
139{
e40298d5
JS
140 wxString value;
141
142 m_rangeMin = minValue;
143 m_rangeMax = maxValue;
144
5ca0d812
SC
145 m_peer->SetMinimum( m_rangeMin);
146 m_peer->SetMaximum( m_rangeMax);
e40298d5
JS
147
148 if(m_macMinimumStatic) {
427ff662 149 value.Printf(wxT("%d"), m_rangeMin);
e40298d5
JS
150 m_macMinimumStatic->SetLabel(value);
151 }
152 if(m_macMaximumStatic) {
427ff662 153 value.Printf(wxT("%d"), m_rangeMax);
e40298d5
JS
154 m_macMaximumStatic->SetLabel(value);
155 }
156 SetValue(m_rangeMin);
e9576ca5
SC
157}
158
159// For trackbars only
160void wxSlider::SetTickFreq(int n, int pos)
161{
162 // TODO
163 m_tickFreq = n;
164}
165
166void wxSlider::SetPageSize(int pageSize)
167{
168 // TODO
169 m_pageSize = pageSize;
170}
171
172int wxSlider::GetPageSize() const
173{
174 return m_pageSize;
175}
176
177void wxSlider::ClearSel()
178{
179 // TODO
180}
181
182void wxSlider::ClearTicks()
183{
184 // TODO
185}
186
187void wxSlider::SetLineSize(int lineSize)
188{
189 m_lineSize = lineSize;
190 // TODO
191}
192
193int wxSlider::GetLineSize() const
194{
195 // TODO
196 return 0;
197}
198
199int wxSlider::GetSelEnd() const
200{
201 // TODO
202 return 0;
203}
204
205int wxSlider::GetSelStart() const
206{
207 // TODO
208 return 0;
209}
210
211void wxSlider::SetSelection(int minPos, int maxPos)
212{
213 // TODO
214}
215
216void wxSlider::SetThumbLength(int len)
217{
218 // TODO
219}
220
221int wxSlider::GetThumbLength() const
222{
223 // TODO
224 return 0;
225}
226
227void wxSlider::SetTick(int tickPos)
228{
229 // TODO
230}
231
232void wxSlider::Command (wxCommandEvent & event)
233{
e40298d5
JS
234 SetValue (event.GetInt());
235 ProcessCommand (event);
e9576ca5
SC
236}
237
cea9c546 238void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown )
519cb848 239{
5ca0d812 240 SInt16 value = m_peer->GetValue() ;
e40298d5
JS
241
242 SetValue( value ) ;
243
cea9c546
SC
244 wxEventType scrollEvent = wxEVT_NULL ;
245
4c37f124 246 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
cea9c546
SC
247
248 wxScrollEvent event(scrollEvent, m_windowId);
e40298d5
JS
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
4c37f124
SC
260wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent )
261{
5ca0d812 262 SInt16 value = m_peer->GetValue() ;
4c37f124
SC
263
264 SetValue( value ) ;
265
266 wxEventType scrollEvent = wxEVT_NULL ;
267
268 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
269
270 wxScrollEvent event(scrollEvent, m_windowId);
271 event.SetPosition(value);
272 event.SetEventObject( this );
273 GetEventHandler()->ProcessEvent(event);
274
275 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
276 cevent.SetInt( value );
277 cevent.SetEventObject( this );
278
279 GetEventHandler()->ProcessEvent( cevent );
280 return noErr ;
281}
282
283
e40298d5
JS
284/* This is overloaded in wxSlider so that the proper width/height will always be used
285* for the slider different values would cause redrawing and mouse detection problems */
286void wxSlider::SetSizeHints( int minW, int minH,
287 int maxW , int maxH ,
288 int incW , int incH )
289{
290 wxSize size = GetBestSize();
291
292 if(GetWindowStyle() & wxSL_VERTICAL) {
293 wxWindow::SetSizeHints(size.x, minH, size.x, maxH, incW, incH);
294 }
295 else {
296 wxWindow::SetSizeHints(minW, size.y, maxW, size.y, incW, incH);
297 }
519cb848 298}
9453cf2b 299
e40298d5
JS
300wxSize wxSlider::DoGetBestSize() const
301{
302 wxSize size;
303 int textwidth, textheight;
304
305 if(GetWindowStyle() & wxSL_LABELS)
306 {
307 wxString text;
308 int ht, wd;
309
310 // Get maximum text label width and height
427ff662 311 text.Printf(wxT("%d"), m_rangeMin);
e40298d5 312 GetTextExtent(text, &textwidth, &textheight);
427ff662 313 text.Printf(wxT("%d"), m_rangeMax);
e40298d5
JS
314 GetTextExtent(text, &wd, &ht);
315 if(ht > textheight) {
316 textheight = ht;
317 }
318 if (wd > textwidth) {
319 textwidth = wd;
320 }
321 }
322
323 if(GetWindowStyle() & wxSL_VERTICAL)
324 {
325 if(GetWindowStyle() & wxSL_AUTOTICKS) {
326 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
327 }
328 else {
329 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
330 }
331 if(GetWindowStyle() & wxSL_LABELS) {
332 size.x += textwidth + wxSLIDER_BORDERTEXT;
333 }
334 size.y = 150;
335 }
336 else
337 {
338 if(GetWindowStyle() & wxSL_AUTOTICKS) {
339 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
340 }
341 else {
342 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
343 }
344 if(GetWindowStyle() & wxSL_LABELS) {
345 size.y += textheight + wxSLIDER_BORDERTEXT;
346 }
347 size.x = 150;
348 }
349 return size;
350}
351
facd6764 352void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
327788ac 353{
e40298d5
JS
354 int xborder, yborder;
355 int minValWidth, maxValWidth, textwidth, textheight;
356 int sliderBreadth;
357
358 xborder = yborder = 0;
f26ca7f8 359
e40298d5
JS
360 if (GetWindowStyle() & wxSL_LABELS)
361 {
f26ca7f8
KO
362 //Labels have this control's parent as their parent
363 //so if this control is not at 0,0 relative to the parent
364 //the labels need to know the position of this control
365 //relative to its parent in order to size properly, so
366 //move the control first so we can use GetPosition()
367 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
368
e40298d5
JS
369 wxString text;
370 int ht;
371
372 // Get maximum text label width and height
427ff662 373 text.Printf(wxT("%d"), m_rangeMin);
e40298d5 374 GetTextExtent(text, &minValWidth, &textheight);
427ff662 375 text.Printf(wxT("%d"), m_rangeMax);
e40298d5
JS
376 GetTextExtent(text, &maxValWidth, &ht);
377 if(ht > textheight) {
378 textheight = ht;
379 }
380 textwidth = (minValWidth > maxValWidth ? minValWidth : maxValWidth);
381
382 xborder = textwidth + wxSLIDER_BORDERTEXT;
383 yborder = textheight + wxSLIDER_BORDERTEXT;
384
385 // Get slider breadth
386 if(GetWindowStyle() & wxSL_AUTOTICKS) {
387 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
388 }
389 else {
390 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
391 }
392
393 if(GetWindowStyle() & wxSL_VERTICAL)
394 {
f26ca7f8
KO
395 h = h - yborder ;
396
facd6764 397 if ( m_macMinimumStatic )
f26ca7f8 398 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT,
6dfa00e8 399 GetPosition().y + h - yborder);
facd6764 400 if ( m_macMaximumStatic )
f26ca7f8 401 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
facd6764 402 if ( m_macValueStatic )
6dfa00e8 403 m_macValueStatic->Move(GetPosition().x, GetPosition().y + h );
e40298d5
JS
404 }
405 else
406 {
1dc1b891 407 w = w - xborder ;
facd6764 408 if ( m_macMinimumStatic )
f26ca7f8 409 m_macMinimumStatic->Move(GetPosition().x + 0, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
facd6764 410 if ( m_macMaximumStatic )
f26ca7f8
KO
411 m_macMaximumStatic->Move(GetPosition().x + w - (maxValWidth/2),
412 GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
facd6764 413 if ( m_macValueStatic )
f26ca7f8 414 m_macValueStatic->Move(GetPosition().x + w, GetPosition().y + 0);
e40298d5
JS
415 }
416 }
f26ca7f8
KO
417 //If the control has labels, we still need to call this again because
418 //the labels alter the control's w and h values.
facd6764 419 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
f26ca7f8 420
327788ac
SC
421}
422
eb22f2a6
GD
423void wxSlider::DoMoveWindow(int x, int y, int width, int height)
424{
327788ac 425 wxControl::DoMoveWindow(x,y,width,height) ;
eb22f2a6 426}