]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/slider.cpp
Patch by David Brinegar to fix menubar updating after modal dialog shown
[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
3d1a4878 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "slider.h"
14#endif
15
3d1a4878 16#include "wx/wxprec.h"
312ebad4
WS
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 72
e40298d5
JS
73 m_lineSize = 1;
74 m_tickFreq = 0;
312ebad4 75
e40298d5
JS
76 m_rangeMax = maxValue;
77 m_rangeMin = minValue;
312ebad4 78
e40298d5 79 m_pageSize = (int)((maxValue-minValue)/10);
312ebad4
WS
80
81 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
82
344d4802
RN
83 //
84 // NB: (RN) Ticks here are sometimes off in the GUI if there
85 // is not as many ticks as there are values
86 //
4c37f124
SC
87 UInt16 tickMarks = 0 ;
88 if ( style & wxSL_AUTOTICKS )
344d4802 89 tickMarks = (maxValue - minValue) + 1; //+1 for the 0 value
312ebad4 90
344d4802 91 while (tickMarks > 20)
02812785 92 tickMarks /= 5; //keep the number of tickmarks from becoming unwieldly
312ebad4 93
21fd5529 94 m_peer = new wxMacControl() ;
312ebad4 95 verify_noerr ( CreateSliderControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds ,
02812785 96 value , minValue , maxValue , kControlSliderPointsDownOrRight , tickMarks , true /* liveTracking */ ,
5ca0d812 97 wxMacLiveScrollbarActionUPP , m_peer->GetControlRefAddr() ) );
312ebad4
WS
98
99
e40298d5
JS
100 if(style & wxSL_VERTICAL) {
101 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
102 }
103 else {
104 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
105 }
106 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
107 // proper dimensions, it also means other people cannot bugger the slider with
108 // other values
312ebad4 109
facd6764
SC
110 if(style & wxSL_LABELS)
111 {
312ebad4
WS
112 m_macMinimumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
113 m_macMaximumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
114 m_macValueStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
facd6764
SC
115 SetRange(minValue, maxValue);
116 SetValue(value);
117 }
118
119 MacPostControlCreate(pos,size) ;
312ebad4 120
e40298d5 121 return true;
e9576ca5
SC
122}
123
124wxSlider::~wxSlider()
125{
33b35531
SC
126 // this is a special case, as we had to add windows as siblings we are
127 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
128 if ( m_parent && m_parent->IsBeingDeleted() == false )
129 {
130 delete m_macMinimumStatic ;
131 delete m_macMaximumStatic ;
132 delete m_macValueStatic ;
133 }
e9576ca5
SC
134}
135
136int wxSlider::GetValue() const
137{
02812785
KH
138 // We may need to invert the value returned by the widget
139 return MacInvertOrNot( 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 ) ;
02812785
KH
148
149 // We only invert for the setting of the actual native widget
150 m_peer->SetValue( MacInvertOrNot ( value ) ) ;
e9576ca5
SC
151}
152
153void wxSlider::SetRange(int minValue, int maxValue)
154{
e40298d5 155 wxString value;
312ebad4 156
e40298d5
JS
157 m_rangeMin = minValue;
158 m_rangeMax = maxValue;
312ebad4 159
5ca0d812
SC
160 m_peer->SetMinimum( m_rangeMin);
161 m_peer->SetMaximum( m_rangeMax);
312ebad4 162
e40298d5 163 if(m_macMinimumStatic) {
02812785 164 value.Printf(wxT("%d"), MacInvertOrNot( m_rangeMin ) );
e40298d5
JS
165 m_macMinimumStatic->SetLabel(value);
166 }
167 if(m_macMaximumStatic) {
02812785 168 value.Printf(wxT("%d"), MacInvertOrNot( m_rangeMax ) );
e40298d5
JS
169 m_macMaximumStatic->SetLabel(value);
170 }
171 SetValue(m_rangeMin);
e9576ca5
SC
172}
173
174// For trackbars only
175void wxSlider::SetTickFreq(int n, int pos)
176{
177 // TODO
178 m_tickFreq = n;
179}
180
181void wxSlider::SetPageSize(int pageSize)
182{
183 // TODO
184 m_pageSize = pageSize;
185}
186
187int wxSlider::GetPageSize() const
188{
189 return m_pageSize;
190}
191
192void wxSlider::ClearSel()
193{
194 // TODO
195}
196
197void wxSlider::ClearTicks()
198{
199 // TODO
200}
201
202void wxSlider::SetLineSize(int lineSize)
203{
204 m_lineSize = lineSize;
205 // TODO
206}
207
208int wxSlider::GetLineSize() const
209{
210 // TODO
211 return 0;
212}
213
214int wxSlider::GetSelEnd() const
215{
216 // TODO
217 return 0;
218}
219
220int wxSlider::GetSelStart() const
221{
222 // TODO
223 return 0;
224}
225
226void wxSlider::SetSelection(int minPos, int maxPos)
227{
228 // TODO
229}
230
231void wxSlider::SetThumbLength(int len)
232{
233 // TODO
234}
235
236int wxSlider::GetThumbLength() const
237{
238 // TODO
239 return 0;
240}
241
242void wxSlider::SetTick(int tickPos)
243{
244 // TODO
245}
246
247void wxSlider::Command (wxCommandEvent & event)
248{
e40298d5
JS
249 SetValue (event.GetInt());
250 ProcessCommand (event);
e9576ca5
SC
251}
252
312ebad4 253void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown )
519cb848 254{
02812785
KH
255 // Whatever the native value is, we may need to invert it for calling
256 // SetValue and putting the possibly inverted value in the event
257 SInt16 value = MacInvertOrNot ( m_peer->GetValue() ) ;
312ebad4
WS
258
259 SetValue( value ) ;
260
cea9c546 261 wxEventType scrollEvent = wxEVT_NULL ;
312ebad4 262
4c37f124 263 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
312ebad4 264
cea9c546 265 wxScrollEvent event(scrollEvent, m_windowId);
e40298d5
JS
266 event.SetPosition(value);
267 event.SetEventObject( this );
268 GetEventHandler()->ProcessEvent(event);
312ebad4 269
e40298d5
JS
270 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
271 cevent.SetInt( value );
272 cevent.SetEventObject( this );
312ebad4 273
e40298d5
JS
274 GetEventHandler()->ProcessEvent( cevent );
275}
276
312ebad4 277wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent )
4c37f124 278{
02812785
KH
279 // Whatever the native value is, we may need to invert it for calling
280 // SetValue and putting the possibly inverted value in the event
281 SInt16 value = MacInvertOrNot ( m_peer->GetValue() ) ;
312ebad4
WS
282
283 SetValue( value ) ;
284
4c37f124 285 wxEventType scrollEvent = wxEVT_NULL ;
312ebad4 286
4c37f124 287 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
312ebad4 288
4c37f124
SC
289 wxScrollEvent event(scrollEvent, m_windowId);
290 event.SetPosition(value);
291 event.SetEventObject( this );
292 GetEventHandler()->ProcessEvent(event);
312ebad4 293
4c37f124
SC
294 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
295 cevent.SetInt( value );
296 cevent.SetEventObject( this );
312ebad4 297
4c37f124
SC
298 GetEventHandler()->ProcessEvent( cevent );
299 return noErr ;
300}
301
e40298d5
JS
302/* This is overloaded in wxSlider so that the proper width/height will always be used
303* for the slider different values would cause redrawing and mouse detection problems */
024f89f9
VS
304void wxSlider::DoSetSizeHints( int minW, int minH,
305 int maxW , int maxH ,
306 int incW , int incH )
e40298d5
JS
307{
308 wxSize size = GetBestSize();
312ebad4 309
e40298d5 310 if(GetWindowStyle() & wxSL_VERTICAL) {
024f89f9 311 wxWindow::DoSetSizeHints(size.x, minH, size.x, maxH, incW, incH);
e40298d5
JS
312 }
313 else {
024f89f9 314 wxWindow::DoSetSizeHints(minW, size.y, maxW, size.y, incW, incH);
e40298d5 315 }
519cb848 316}
9453cf2b 317
e40298d5
JS
318wxSize wxSlider::DoGetBestSize() const
319{
320 wxSize size;
321 int textwidth, textheight;
312ebad4 322
e40298d5
JS
323 if(GetWindowStyle() & wxSL_LABELS)
324 {
325 wxString text;
326 int ht, wd;
312ebad4 327
e40298d5 328 // Get maximum text label width and height
02812785 329 text.Printf(wxT("%d"), MacInvertOrNot( m_rangeMin ) );
e40298d5 330 GetTextExtent(text, &textwidth, &textheight);
02812785 331 text.Printf(wxT("%d"), MacInvertOrNot( m_rangeMax ) );
e40298d5
JS
332 GetTextExtent(text, &wd, &ht);
333 if(ht > textheight) {
334 textheight = ht;
335 }
336 if (wd > textwidth) {
337 textwidth = wd;
338 }
339 }
312ebad4 340
e40298d5
JS
341 if(GetWindowStyle() & wxSL_VERTICAL)
342 {
343 if(GetWindowStyle() & wxSL_AUTOTICKS) {
344 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
345 }
346 else {
347 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
348 }
349 if(GetWindowStyle() & wxSL_LABELS) {
350 size.x += textwidth + wxSLIDER_BORDERTEXT;
351 }
352 size.y = 150;
353 }
354 else
355 {
356 if(GetWindowStyle() & wxSL_AUTOTICKS) {
357 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
358 }
359 else {
360 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
361 }
362 if(GetWindowStyle() & wxSL_LABELS) {
363 size.y += textheight + wxSLIDER_BORDERTEXT;
364 }
365 size.x = 150;
366 }
367 return size;
368}
369
facd6764 370void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
327788ac 371{
e40298d5
JS
372 int xborder, yborder;
373 int minValWidth, maxValWidth, textwidth, textheight;
374 int sliderBreadth;
09ff2ee1 375 int width = w;
312ebad4 376
e40298d5 377 xborder = yborder = 0;
f26ca7f8 378
e40298d5
JS
379 if (GetWindowStyle() & wxSL_LABELS)
380 {
f26ca7f8
KO
381 //Labels have this control's parent as their parent
382 //so if this control is not at 0,0 relative to the parent
383 //the labels need to know the position of this control
384 //relative to its parent in order to size properly, so
385 //move the control first so we can use GetPosition()
386 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
312ebad4 387
e40298d5
JS
388 wxString text;
389 int ht;
312ebad4 390
e40298d5 391 // Get maximum text label width and height
02812785 392 text.Printf(wxT("%d"), MacInvertOrNot( m_rangeMin ) );
e40298d5 393 GetTextExtent(text, &minValWidth, &textheight);
02812785 394 text.Printf(wxT("%d"), MacInvertOrNot( m_rangeMax ) );
e40298d5
JS
395 GetTextExtent(text, &maxValWidth, &ht);
396 if(ht > textheight) {
397 textheight = ht;
398 }
399 textwidth = (minValWidth > maxValWidth ? minValWidth : maxValWidth);
312ebad4 400
e40298d5
JS
401 xborder = textwidth + wxSLIDER_BORDERTEXT;
402 yborder = textheight + wxSLIDER_BORDERTEXT;
312ebad4 403
e40298d5
JS
404 // Get slider breadth
405 if(GetWindowStyle() & wxSL_AUTOTICKS) {
406 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
407 }
408 else {
409 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
410 }
312ebad4 411
e40298d5
JS
412 if(GetWindowStyle() & wxSL_VERTICAL)
413 {
f26ca7f8 414 h = h - yborder ;
312ebad4 415
facd6764 416 if ( m_macMinimumStatic )
f26ca7f8 417 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT,
6dfa00e8 418 GetPosition().y + h - yborder);
facd6764 419 if ( m_macMaximumStatic )
f26ca7f8 420 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
facd6764 421 if ( m_macValueStatic )
6dfa00e8 422 m_macValueStatic->Move(GetPosition().x, GetPosition().y + h );
e40298d5
JS
423 }
424 else
425 {
1dc1b891 426 w = w - xborder ;
facd6764 427 if ( m_macMinimumStatic )
f26ca7f8 428 m_macMinimumStatic->Move(GetPosition().x + 0, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
facd6764 429 if ( m_macMaximumStatic )
f26ca7f8
KO
430 m_macMaximumStatic->Move(GetPosition().x + w - (maxValWidth/2),
431 GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
facd6764 432 if ( m_macValueStatic )
f26ca7f8 433 m_macValueStatic->Move(GetPosition().x + w, GetPosition().y + 0);
e40298d5
JS
434 }
435 }
09ff2ee1
KO
436
437
438 // yet another hack since this is a composite control
439 // when wxSlider has it's size hardcoded, we're not allowed to
440 // change the size. But when the control has labels, we DO need
441 // to resize the internal Mac control to accomodate the text labels.
442 // We need to trick the wxWidgets resize mechanism so that we can
443 // resize the slider part of the control ONLY.
444
445 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
446
447 int minWidth;
900a8765 448 minWidth = m_minWidth;
09ff2ee1 449
900a8765 450 if (GetWindowStyle() & wxSL_LABELS)
09ff2ee1 451 {
900a8765
KH
452 // make sure we don't allow the entire control to be resized accidently
453 if (width == GetSize().x)
454 m_minWidth = -1;
455 }
09ff2ee1 456 //If the control has labels, we still need to call this again because
312ebad4 457 //the labels alter the control's w and h values.
facd6764 458 wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ;
09ff2ee1 459
900a8765 460 m_minWidth = minWidth;
327788ac
SC
461}
462
eb22f2a6
GD
463void wxSlider::DoMoveWindow(int x, int y, int width, int height)
464{
327788ac 465 wxControl::DoMoveWindow(x,y,width,height) ;
eb22f2a6 466}
312ebad4 467
02812785
KH
468// Common processing to invert slider values based on wxSL_INVERSE
469int wxSlider::MacInvertOrNot(int value) const
470{
471 if (m_windowStyle & wxSL_VERTICAL)
472 {
473 // The reason for the backwards logic is that Mac's vertical sliders are
474 // inverted compared to Windows and GTK, hence we want inversion to be the
475 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
476 if (m_windowStyle & wxSL_INVERSE)
477 return value;
478 else
479 return (m_rangeMax + m_rangeMin) - value;
480 }
481 else // normal logic applies to HORIZONTAL sliders
482 {
483 if (m_windowStyle & wxSL_INVERSE)
484 return (m_rangeMax + m_rangeMin) - value;
485 else
486 return value;
487 }
488}
489
312ebad4 490#endif // wxUSE_SLIDER