]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/slider.cpp
Fix centering of top-level children of wxMDIParentFrame on Mac
[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
312ebad4
WS
16#include "wx/defs.h"
17
18#if wxUSE_SLIDER
19
e9576ca5 20#include "wx/slider.h"
519cb848 21#include "wx/mac/uma.h"
e9576ca5 22
2f1ae414 23#if !USE_SHARED_LIBRARY
e9576ca5
SC
24IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
25
26BEGIN_EVENT_TABLE(wxSlider, wxControl)
27END_EVENT_TABLE()
2f1ae414 28#endif
e9576ca5 29
9453cf2b 30 // The dimensions of the different styles of sliders (From Aqua document)
e40298d5
JS
31#define wxSLIDER_DIMENSIONACROSS 15
32#define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
33#define wxSLIDER_DIMENSIONACROSS_ARROW 18
312ebad4 34
e40298d5
JS
35// Distance between slider and text
36#define wxSLIDER_BORDERTEXT 5
312ebad4 37
e40298d5
JS
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
312ebad4 42 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
e40298d5
JS
43 */
44
45 // Slider
46 wxSlider::wxSlider()
e9576ca5 47{
e40298d5
JS
48 m_pageSize = 1;
49 m_lineSize = 1;
50 m_rangeMax = 0;
51 m_rangeMin = 0;
52 m_tickFreq = 0;
e9576ca5
SC
53}
54
519cb848
SC
55extern ControlActionUPP wxMacLiveScrollbarActionUPP ;
56
e9576ca5 57bool wxSlider::Create(wxWindow *parent, wxWindowID id,
e40298d5
JS
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)
e9576ca5 63{
312ebad4
WS
64 m_macIsUserPane = false ;
65
b45ed7a2
VZ
66 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
67 return false;
68
e40298d5
JS
69 m_macMinimumStatic = NULL ;
70 m_macMaximumStatic = NULL ;
71 m_macValueStatic = NULL ;
312ebad4
WS
72
73
e40298d5
JS
74 m_lineSize = 1;
75 m_tickFreq = 0;
312ebad4 76
e40298d5
JS
77 m_rangeMax = maxValue;
78 m_rangeMin = minValue;
312ebad4 79
e40298d5 80 m_pageSize = (int)((maxValue-minValue)/10);
312ebad4
WS
81
82 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
83
344d4802
RN
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 //
4c37f124
SC
88 UInt16 tickMarks = 0 ;
89 if ( style & wxSL_AUTOTICKS )
344d4802 90 tickMarks = (maxValue - minValue) + 1; //+1 for the 0 value
312ebad4 91
344d4802
RN
92 while (tickMarks > 20)
93 tickMarks /= 5; //keep the number of tickmarks from becoming unwieldly
312ebad4 94
21fd5529 95 m_peer = new wxMacControl() ;
312ebad4 96 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds ,
4c37f124 97 value , minValue , maxValue , kControlSliderPointsDownOrRight , tickMarks , true /* liveTracking */ ,
5ca0d812 98 wxMacLiveScrollbarActionUPP , m_peer->GetControlRefAddr() ) );
312ebad4
WS
99
100
e40298d5
JS
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
312ebad4 110
facd6764
SC
111 if(style & wxSL_LABELS)
112 {
312ebad4
WS
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 );
facd6764
SC
116 SetRange(minValue, maxValue);
117 SetValue(value);
118 }
119
120 MacPostControlCreate(pos,size) ;
312ebad4 121
e40298d5 122 return true;
e9576ca5
SC
123}
124
125wxSlider::~wxSlider()
126{
33b35531
SC
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 }
e9576ca5
SC
135}
136
137int wxSlider::GetValue() const
138{
5ca0d812 139 return m_peer->GetValue() ;
e9576ca5
SC
140}
141
142void wxSlider::SetValue(int value)
143{
e40298d5 144 wxString valuestring ;
312ebad4 145 valuestring.Printf( wxT("%d") , value ) ;
e40298d5
JS
146 if ( m_macValueStatic )
147 m_macValueStatic->SetLabel( valuestring ) ;
5ca0d812 148 m_peer->SetValue( value ) ;
e9576ca5
SC
149}
150
151void wxSlider::SetRange(int minValue, int maxValue)
152{
e40298d5 153 wxString value;
312ebad4 154
e40298d5
JS
155 m_rangeMin = minValue;
156 m_rangeMax = maxValue;
312ebad4 157
5ca0d812
SC
158 m_peer->SetMinimum( m_rangeMin);
159 m_peer->SetMaximum( m_rangeMax);
312ebad4 160
e40298d5 161 if(m_macMinimumStatic) {
427ff662 162 value.Printf(wxT("%d"), m_rangeMin);
e40298d5
JS
163 m_macMinimumStatic->SetLabel(value);
164 }
165 if(m_macMaximumStatic) {
427ff662 166 value.Printf(wxT("%d"), m_rangeMax);
e40298d5
JS
167 m_macMaximumStatic->SetLabel(value);
168 }
169 SetValue(m_rangeMin);
e9576ca5
SC
170}
171
172// For trackbars only
173void wxSlider::SetTickFreq(int n, int pos)
174{
175 // TODO
176 m_tickFreq = n;
177}
178
179void wxSlider::SetPageSize(int pageSize)
180{
181 // TODO
182 m_pageSize = pageSize;
183}
184
185int wxSlider::GetPageSize() const
186{
187 return m_pageSize;
188}
189
190void wxSlider::ClearSel()
191{
192 // TODO
193}
194
195void wxSlider::ClearTicks()
196{
197 // TODO
198}
199
200void wxSlider::SetLineSize(int lineSize)
201{
202 m_lineSize = lineSize;
203 // TODO
204}
205
206int wxSlider::GetLineSize() const
207{
208 // TODO
209 return 0;
210}
211
212int wxSlider::GetSelEnd() const
213{
214 // TODO
215 return 0;
216}
217
218int wxSlider::GetSelStart() const
219{
220 // TODO
221 return 0;
222}
223
224void wxSlider::SetSelection(int minPos, int maxPos)
225{
226 // TODO
227}
228
229void wxSlider::SetThumbLength(int len)
230{
231 // TODO
232}
233
234int wxSlider::GetThumbLength() const
235{
236 // TODO
237 return 0;
238}
239
240void wxSlider::SetTick(int tickPos)
241{
242 // TODO
243}
244
245void wxSlider::Command (wxCommandEvent & event)
246{
e40298d5
JS
247 SetValue (event.GetInt());
248 ProcessCommand (event);
e9576ca5
SC
249}
250
312ebad4 251void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown )
519cb848 252{
5ca0d812 253 SInt16 value = m_peer->GetValue() ;
312ebad4
WS
254
255 SetValue( value ) ;
256
cea9c546 257 wxEventType scrollEvent = wxEVT_NULL ;
312ebad4 258
4c37f124 259 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
312ebad4 260
cea9c546 261 wxScrollEvent event(scrollEvent, m_windowId);
e40298d5
JS
262 event.SetPosition(value);
263 event.SetEventObject( this );
264 GetEventHandler()->ProcessEvent(event);
312ebad4 265
e40298d5
JS
266 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
267 cevent.SetInt( value );
268 cevent.SetEventObject( this );
312ebad4 269
e40298d5
JS
270 GetEventHandler()->ProcessEvent( cevent );
271}
272
312ebad4 273wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent )
4c37f124 274{
5ca0d812 275 SInt16 value = m_peer->GetValue() ;
312ebad4
WS
276
277 SetValue( value ) ;
278
4c37f124 279 wxEventType scrollEvent = wxEVT_NULL ;
312ebad4 280
4c37f124 281 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
312ebad4 282
4c37f124
SC
283 wxScrollEvent event(scrollEvent, m_windowId);
284 event.SetPosition(value);
285 event.SetEventObject( this );
286 GetEventHandler()->ProcessEvent(event);
312ebad4 287
4c37f124
SC
288 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
289 cevent.SetInt( value );
290 cevent.SetEventObject( this );
312ebad4 291
4c37f124
SC
292 GetEventHandler()->ProcessEvent( cevent );
293 return noErr ;
294}
295
296
e40298d5
JS
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 */
024f89f9
VS
299void wxSlider::DoSetSizeHints( int minW, int minH,
300 int maxW , int maxH ,
301 int incW , int incH )
e40298d5
JS
302{
303 wxSize size = GetBestSize();
312ebad4 304
e40298d5 305 if(GetWindowStyle() & wxSL_VERTICAL) {
024f89f9 306 wxWindow::DoSetSizeHints(size.x, minH, size.x, maxH, incW, incH);
e40298d5
JS
307 }
308 else {
024f89f9 309 wxWindow::DoSetSizeHints(minW, size.y, maxW, size.y, incW, incH);
e40298d5 310 }
519cb848 311}
9453cf2b 312
e40298d5
JS
313wxSize wxSlider::DoGetBestSize() const
314{
315 wxSize size;
316 int textwidth, textheight;
312ebad4 317
e40298d5
JS
318 if(GetWindowStyle() & wxSL_LABELS)
319 {
320 wxString text;
321 int ht, wd;
312ebad4 322
e40298d5 323 // Get maximum text label width and height
427ff662 324 text.Printf(wxT("%d"), m_rangeMin);
e40298d5 325 GetTextExtent(text, &textwidth, &textheight);
427ff662 326 text.Printf(wxT("%d"), m_rangeMax);
e40298d5
JS
327 GetTextExtent(text, &wd, &ht);
328 if(ht > textheight) {
329 textheight = ht;
330 }
331 if (wd > textwidth) {
332 textwidth = wd;
333 }
334 }
312ebad4 335
e40298d5
JS
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
facd6764 365void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
327788ac 366{
e40298d5
JS
367 int xborder, yborder;
368 int minValWidth, maxValWidth, textwidth, textheight;
369 int sliderBreadth;
312ebad4 370
e40298d5 371 xborder = yborder = 0;
f26ca7f8 372
e40298d5
JS
373 if (GetWindowStyle() & wxSL_LABELS)
374 {
f26ca7f8
KO
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 ) ;
312ebad4 381
e40298d5
JS
382 wxString text;
383 int ht;
312ebad4 384
e40298d5 385 // Get maximum text label width and height
427ff662 386 text.Printf(wxT("%d"), m_rangeMin);
e40298d5 387 GetTextExtent(text, &minValWidth, &textheight);
427ff662 388 text.Printf(wxT("%d"), m_rangeMax);
e40298d5
JS
389 GetTextExtent(text, &maxValWidth, &ht);
390 if(ht > textheight) {
391 textheight = ht;
392 }
393 textwidth = (minValWidth > maxValWidth ? minValWidth : maxValWidth);
312ebad4 394
e40298d5
JS
395 xborder = textwidth + wxSLIDER_BORDERTEXT;
396 yborder = textheight + wxSLIDER_BORDERTEXT;
312ebad4 397
e40298d5
JS
398 // Get slider breadth
399 if(GetWindowStyle() & wxSL_AUTOTICKS) {
400 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
401 }
402 else {
403 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
404 }
312ebad4 405
e40298d5
JS
406 if(GetWindowStyle() & wxSL_VERTICAL)
407 {
f26ca7f8 408 h = h - yborder ;
312ebad4 409
facd6764 410 if ( m_macMinimumStatic )
f26ca7f8 411 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT,
6dfa00e8 412 GetPosition().y + h - yborder);
facd6764 413 if ( m_macMaximumStatic )
f26ca7f8 414 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
facd6764 415 if ( m_macValueStatic )
6dfa00e8 416 m_macValueStatic->Move(GetPosition().x, GetPosition().y + h );
e40298d5
JS
417 }
418 else
419 {
1dc1b891 420 w = w - xborder ;
facd6764 421 if ( m_macMinimumStatic )
f26ca7f8 422 m_macMinimumStatic->Move(GetPosition().x + 0, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
facd6764 423 if ( m_macMaximumStatic )
f26ca7f8
KO
424 m_macMaximumStatic->Move(GetPosition().x + w - (maxValWidth/2),
425 GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
facd6764 426 if ( m_macValueStatic )
f26ca7f8 427 m_macValueStatic->Move(GetPosition().x + w, GetPosition().y + 0);
e40298d5
JS
428 }
429 }
f26ca7f8 430 //If the control has labels, we still need to call this again because
312ebad4 431 //the labels alter the control's w and h values.
facd6764 432 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
f26ca7f8 433
327788ac
SC
434}
435
eb22f2a6
GD
436void wxSlider::DoMoveWindow(int x, int y, int width, int height)
437{
327788ac 438 wxControl::DoMoveWindow(x,y,width,height) ;
eb22f2a6 439}
312ebad4
WS
440
441#endif // wxUSE_SLIDER