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