]> git.saurik.com Git - wxWidgets.git/blame - src/osx/slider_osx.cpp
Try to display '...' text on editor buttons more consistently on wxGTK. This is done...
[wxWidgets.git] / src / osx / slider_osx.cpp
CommitLineData
524c47aa
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/slider_osx.cpp
3// Purpose: wxSlider
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id: slider.cpp 54129 2008-06-11 19:30:52Z SC $
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#if wxUSE_SLIDER
15
16#include "wx/slider.h"
17#include "wx/osx/private.h"
18
19IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
20
21BEGIN_EVENT_TABLE(wxSlider, wxControl)
22END_EVENT_TABLE()
23
24 // The dimensions of the different styles of sliders (from Aqua document)
25#define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
26#define wxSLIDER_DIMENSIONACROSS_ARROW 18
27
28// Distance between slider and text
29#define wxSLIDER_BORDERTEXT 5
30
31// NB: The default orientation for a slider is horizontal; however, if the user specifies
32// some slider styles but doesn't specify the orientation we have to assume he wants a
33// horizontal one. Therefore in this file when testing for the slider's orientation
34// vertical is tested for if this is not set then we use the horizontal one
35// e.g., if (GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }.
36
37wxSlider::wxSlider()
38{
39 m_pageSize = 1;
40 m_lineSize = 1;
41 m_rangeMax = 0;
42 m_rangeMin = 0;
43 m_tickFreq = 0;
44
45 m_macMinimumStatic = NULL;
46 m_macMaximumStatic = NULL;
47 m_macValueStatic = NULL;
48}
49
50bool wxSlider::Create(wxWindow *parent,
51 wxWindowID id,
52 int value, int minValue, int maxValue,
53 const wxPoint& pos,
54 const wxSize& size, long style,
55 const wxValidator& validator,
56 const wxString& name)
57{
58 m_macIsUserPane = false;
59
60 m_macMinimumStatic = NULL;
61 m_macMaximumStatic = NULL;
62 m_macValueStatic = NULL;
63
64 m_lineSize = 1;
65 m_tickFreq = 0;
66
67 m_rangeMax = maxValue;
68 m_rangeMin = minValue;
69
70 m_pageSize = (int)((maxValue - minValue) / 10);
71
72 // our styles are redundant: wxSL_LEFT/RIGHT imply wxSL_VERTICAL and
73 // wxSL_TOP/BOTTOM imply wxSL_HORIZONTAL, but for backwards compatibility
74 // reasons we can't really change it, instead try to infer the orientation
75 // from the flags given to us here
76 switch ( style & (wxSL_LEFT | wxSL_RIGHT | wxSL_TOP | wxSL_BOTTOM) )
77 {
78 case wxSL_LEFT:
79 case wxSL_RIGHT:
80 style |= wxSL_VERTICAL;
81 break;
82
83 case wxSL_TOP:
84 case wxSL_BOTTOM:
85 style |= wxSL_HORIZONTAL;
86 break;
87
88 case 0:
89 default:
90 // no specific direction, do we have at least the orientation?
91 if ( !(style & (wxSL_HORIZONTAL | wxSL_VERTICAL)) )
92 // no: choose default
93 style |= wxSL_BOTTOM | wxSL_HORIZONTAL;
94 break;
95 }
96
97 wxASSERT_MSG( !(style & wxSL_VERTICAL) || !(style & wxSL_HORIZONTAL),
98 wxT("incompatible slider direction and orientation") );
99
100 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
101 return false;
102
103 m_peer = wxWidgetImpl::CreateSlider( this, parent, id, value, minValue, maxValue, pos, size, style, GetExtraStyle() );
104
105 if (style & wxSL_VERTICAL)
106 // Forces SetSize to use the proper width
107 SetSizeHints(10, -1, 10, -1);
108 else
109 // Forces SetSize to use the proper height
110 SetSizeHints(-1, 10, -1, 10);
111
112 // NB: SetSizeHints is overloaded by wxSlider and will substitute 10 with the
113 // proper dimensions, it also means other people cannot bugger the slider with
114 // other values
115
116 if (style & wxSL_LABELS)
117 {
118 m_macMinimumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
119 m_macMaximumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
120 m_macValueStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString );
121 }
122
123 SetRange(minValue, maxValue);
124 SetValue(value);
125
126 MacPostControlCreate(pos, size);
127
128 return true;
129}
130
131wxSlider::~wxSlider()
132{
133 // this is a special case, as we had to add windows as siblings we are
134 // responsible for their disposal, but only if we are not part of a DestroyAllChildren
135 if ( m_parent && !m_parent->IsBeingDeleted() )
136 {
137 delete m_macMinimumStatic;
138 delete m_macMaximumStatic;
139 delete m_macValueStatic;
140 }
141}
142
143int wxSlider::GetValue() const
144{
145 // We may need to invert the value returned by the widget
146 return ValueInvertOrNot( m_peer->GetValue() ) ;
147}
148
149void wxSlider::SetValue(int value)
150{
151 if ( m_macValueStatic )
152 {
153 wxString valuestring;
154 valuestring.Printf( wxT("%d"), value );
155 m_macValueStatic->SetLabel( valuestring );
156 }
157
158 // We only invert for the setting of the actual native widget
159 m_peer->SetValue( ValueInvertOrNot( value ) );
160}
161
162void wxSlider::SetRange(int minValue, int maxValue)
163{
164 wxString value;
165
166 m_rangeMin = minValue;
167 m_rangeMax = maxValue;
168
169 m_peer->SetMinimum( m_rangeMin );
170 m_peer->SetMaximum( m_rangeMax );
171
172 if (m_macMinimumStatic)
173 {
174 value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
175 m_macMinimumStatic->SetLabel( value );
176 }
177
178 if (m_macMaximumStatic)
179 {
180 value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
181 m_macMaximumStatic->SetLabel( value );
182 }
183
184 // If the range is out of bounds, set it to a
185 // value that is within bounds
186 // RN: Testing reveals OSX does its own
187 // bounding, perhaps this isn't needed?
188 int currentValue = GetValue();
189
190 if(currentValue < m_rangeMin)
191 SetValue(m_rangeMin);
192 else if(currentValue > m_rangeMax)
193 SetValue(m_rangeMax);
194}
195
196// For trackbars only
197void wxSlider::SetTickFreq(int n, int WXUNUSED(pos))
198{
199 // TODO
200 m_tickFreq = n;
201}
202
203void wxSlider::SetPageSize(int pageSize)
204{
205 // TODO
206 m_pageSize = pageSize;
207}
208
209int wxSlider::GetPageSize() const
210{
211 return m_pageSize;
212}
213
214void wxSlider::ClearSel()
215{
216 // TODO
217}
218
219void wxSlider::ClearTicks()
220{
221 // TODO
222}
223
224void wxSlider::SetLineSize(int lineSize)
225{
226 m_lineSize = lineSize;
227 // TODO
228}
229
230int wxSlider::GetLineSize() const
231{
232 // TODO
233 return m_lineSize;
234}
235
236int wxSlider::GetSelEnd() const
237{
238 // TODO
239 return 0;
240}
241
242int wxSlider::GetSelStart() const
243{
244 // TODO
245 return 0;
246}
247
248void wxSlider::SetSelection(int WXUNUSED(minPos), int WXUNUSED(maxPos))
249{
250 // TODO
251}
252
253void wxSlider::SetThumbLength(int WXUNUSED(len))
254{
255 // TODO
256}
257
258int wxSlider::GetThumbLength() const
259{
260 // TODO
261 return 0;
262}
263
264void wxSlider::SetTick(int WXUNUSED(tickPos))
265{
266 // TODO
267}
268
269void wxSlider::Command(wxCommandEvent &event)
270{
271 SetValue(event.GetInt());
272 ProcessCommand(event);
273}
274
275void wxSlider::MacHandleControlClick(WXWidget WXUNUSED(control),
276 wxInt16 WXUNUSED(controlpart),
277 bool WXUNUSED(mouseStillDown))
278{
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 int value = ValueInvertOrNot( m_peer->GetValue() );
282
283 SetValue( value );
284
285 wxScrollEvent event( wxEVT_SCROLL_THUMBTRACK, m_windowId );
286 event.SetPosition( value );
287 event.SetEventObject( this );
288 HandleWindowEvent( event );
289
290 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
291 cevent.SetInt( value );
292 cevent.SetEventObject( this );
293 HandleWindowEvent( cevent );
294}
295
296bool wxSlider::HandleClicked( double timestampsec )
297{
298 // Whatever the native value is, we may need to invert it for calling
299 // SetValue and putting the possibly inverted value in the event
300 int value = ValueInvertOrNot( m_peer->GetValue() ) ;
301
302 SetValue( value ) ;
303
304 wxScrollEvent event( wxEVT_SCROLL_THUMBRELEASE, m_windowId );
305 event.SetPosition( value );
306 event.SetEventObject( this );
307 HandleWindowEvent( event );
308
309 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
310 cevent.SetInt( value );
311 cevent.SetEventObject( this );
312
313 HandleWindowEvent( cevent );
314
315 return true;
316}
317
318// This is overloaded in wxSlider so that the proper width/height will always be used
319// for the slider different values would cause redrawing and mouse detection problems
320//
321void wxSlider::DoSetSizeHints( int minW, int minH,
322 int maxW, int maxH,
323 int WXUNUSED(incW), int WXUNUSED(incH) )
324{
325 wxSize size = GetBestSize();
326
327 if (GetWindowStyle() & wxSL_VERTICAL)
328 {
329 SetMinSize( wxSize(size.x,minH) );
330 SetMaxSize( wxSize(size.x,maxH) );
331 }
332 else
333 {
334 SetMinSize( wxSize(minW,size.y) );
335 SetMaxSize( wxSize(maxW,size.y) );
336 }
337}
338
339wxSize wxSlider::DoGetBestSize() const
340{
341 wxSize size;
342 int textwidth, textheight;
343 int mintwidth, mintheight;
344 int maxtwidth, maxtheight;
345
346 textwidth = textheight = 0;
347 mintwidth = mintheight = 0;
348 maxtwidth = maxtheight = 0;
349
350 if (GetWindowStyle() & wxSL_LABELS)
351 {
352 wxString text;
353
354 // Get maximum text label width and height
355 text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
356 GetTextExtent(text, &mintwidth, &mintheight);
357 text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
358 GetTextExtent(text, &maxtwidth, &maxtheight);
359
360 if (maxtheight > mintheight)
361 textheight = maxtheight;
362 else
363 textheight = mintheight;
364
365 if (maxtwidth > mintwidth)
366 textwidth = maxtwidth;
367 else
368 textwidth = mintwidth;
369 }
370
371 if (GetWindowStyle() & wxSL_VERTICAL)
372 {
373 size.y = 150;
374
375 if (GetWindowStyle() & wxSL_AUTOTICKS)
376 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
377 else
378 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
379
380 if (GetWindowStyle() & wxSL_LABELS)
381 size.x += textwidth + wxSLIDER_BORDERTEXT;
382 }
383 else
384 {
385 size.x = 150;
386
387 if (GetWindowStyle() & wxSL_AUTOTICKS)
388 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
389 else
390 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
391
392 if (GetWindowStyle() & wxSL_LABELS)
393 {
394 size.y += textheight + wxSLIDER_BORDERTEXT;
395 size.x += (mintwidth / 2) + (maxtwidth / 2);
396 }
397 }
398
399 return size;
400}
401
402void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags)
403{
404 int yborder = 0;
405 int minValWidth, maxValWidth, textheight;
406 int sliderBreadth;
407 int width = w;
408
409 if (GetWindowStyle() & wxSL_LABELS)
410 {
411 wxString text;
412 int ht, valValWidth;
413
414 // Get maximum text label width and height
415 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin ) );
416 GetTextExtent(text, &minValWidth, &textheight);
417 text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax ) );
418 GetTextExtent(text, &maxValWidth, &ht);
419
420 if (ht > textheight)
421 textheight = ht;
422
423 if (GetWindowStyle() & wxSL_HORIZONTAL)
424 {
425 if ( m_macMinimumStatic )
426 {
427 w -= minValWidth / 2;
428 x += minValWidth / 2;
429 }
430
431 if ( m_macMaximumStatic )
432 w -= maxValWidth / 2;
433 }
434
435 // Labels have this control's parent as their parent
436 // so if this control is not at 0,0 relative to the parent
437 // the labels need to know the position of this control
438 // relative to its parent in order to size properly, so
439 // move the control first so we can use GetPosition()
440 wxControl::DoSetSize( x, y, w, h, sizeFlags );
441
442 if (GetWindowStyle() & wxSL_VERTICAL)
443 // If vertical, use current value
444 text.Printf(wxT("%d"), (int)m_peer->GetValue());
445 else
446 // Use max so that the current value doesn't drift as centering would need to change
447 text.Printf(wxT("%d"), m_rangeMax);
448
449 GetTextExtent(text, &valValWidth, &ht);
450
451 yborder = textheight + wxSLIDER_BORDERTEXT;
452
453 // Get slider breadth
454 if (GetWindowStyle() & wxSL_AUTOTICKS)
455 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
456 else
457 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
458
459 if (GetWindowStyle() & wxSL_VERTICAL)
460 {
461 h = h - yborder;
462
463 if ( m_macMinimumStatic )
464 m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + h - yborder);
465 if ( m_macMaximumStatic )
466 m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0);
467 if ( m_macValueStatic )
468 m_macValueStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + (h / 2) - (ht / 2));
469 }
470 else
471 {
472 if ( m_macMinimumStatic )
473 m_macMinimumStatic->Move(GetPosition().x, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
474 if ( m_macMaximumStatic )
475 m_macMaximumStatic->Move(GetPosition().x + w - maxValWidth, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
476 if ( m_macValueStatic )
477 m_macValueStatic->Move(GetPosition().x + (w / 2) - (valValWidth / 2), GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT);
478 }
479 }
480
481 // yet another hack since this is a composite control
482 // when wxSlider has it's size hardcoded, we're not allowed to
483 // change the size. But when the control has labels, we DO need
484 // to resize the internal Mac control to accommodate the text labels.
485 // We need to trick the wxWidgets resize mechanism so that we can
486 // resize the slider part of the control ONLY.
487
488 // TODO: Can all of this code go in the conditional wxSL_LABELS block?
489
490 int minWidth = m_minWidth;
491
492 if (GetWindowStyle() & wxSL_LABELS)
493 {
494 // make sure we don't allow the entire control to be resized accidently
495 if (width == GetSize().x)
496 m_minWidth = -1;
497 }
498
499 // If the control has labels, we still need to call this again because
500 // the labels alter the control's w and h values.
501 wxControl::DoSetSize( x, y, w, h, sizeFlags );
502
503 m_minWidth = minWidth;
504}
505
506void wxSlider::DoMoveWindow(int x, int y, int width, int height)
507{
508 wxControl::DoMoveWindow( x, y, width, height );
509}
510
511// Common processing to invert slider values based on wxSL_INVERSE
512int wxSlider::ValueInvertOrNot(int value) const
513{
514 int result = 0;
515
516 if (m_windowStyle & wxSL_VERTICAL)
517 {
518 // The reason for the backwards logic is that Mac's vertical sliders are
519 // inverted compared to Windows and GTK, hence we want inversion to be the
520 // default, and if wxSL_INVERSE is set, then we do not invert (use native)
521 if (m_windowStyle & wxSL_INVERSE)
522 result = value;
523 else
524 result = (m_rangeMax + m_rangeMin) - value;
525 }
526 else // normal logic applies to HORIZONTAL sliders
527 {
528 result = wxSliderBase::ValueInvertOrNot(value);
529 }
530
531 return result;
532}
533
534#endif // wxUSE_SLIDER