]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: slider.cpp | |
3 | // Purpose: wxSlider | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
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/mac/uma.h" | |
18 | ||
19 | IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl) | |
20 | ||
21 | BEGIN_EVENT_TABLE(wxSlider, wxControl) | |
22 | END_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 | ||
37 | wxSlider::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 | ||
50 | bool 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 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ); | |
104 | ||
105 | // NB: (RN) Ticks here are sometimes off in the GUI if there | |
106 | // are not as many tick marks as there are values | |
107 | // | |
108 | int tickMarks = 0; | |
109 | if ( style & wxSL_AUTOTICKS ) | |
110 | tickMarks = (maxValue - minValue) + 1; // +1 for the 0 value | |
111 | ||
112 | // keep the number of tickmarks from becoming unwieldly, therefore below it is ok to cast | |
113 | // it to a UInt16 | |
114 | while (tickMarks > 20) | |
115 | tickMarks /= 5; | |
116 | ||
117 | m_peer = new wxMacControl( this ); | |
118 | OSStatus err = CreateSliderControl( | |
119 | MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, | |
120 | value, minValue, maxValue, | |
121 | kControlSliderPointsDownOrRight, | |
122 | (UInt16) tickMarks, true /* liveTracking */, | |
123 | GetwxMacLiveScrollbarActionProc(), | |
124 | m_peer->GetControlRefAddr() ); | |
125 | verify_noerr( err ); | |
126 | ||
127 | if (style & wxSL_VERTICAL) | |
128 | // Forces SetSize to use the proper width | |
129 | SetSizeHints(10, -1, 10, -1); | |
130 | else | |
131 | // Forces SetSize to use the proper height | |
132 | SetSizeHints(-1, 10, -1, 10); | |
133 | ||
134 | // NB: SetSizeHints is overloaded by wxSlider and will substitute 10 with the | |
135 | // proper dimensions, it also means other people cannot bugger the slider with | |
136 | // other values | |
137 | ||
138 | if (style & wxSL_LABELS) | |
139 | { | |
140 | m_macMinimumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString ); | |
141 | m_macMaximumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString ); | |
142 | m_macValueStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString ); | |
143 | } | |
144 | ||
145 | SetRange(minValue, maxValue); | |
146 | SetValue(value); | |
147 | ||
148 | MacPostControlCreate(pos, size); | |
149 | ||
150 | return true; | |
151 | } | |
152 | ||
153 | wxSlider::~wxSlider() | |
154 | { | |
155 | // this is a special case, as we had to add windows as siblings we are | |
156 | // responsible for their disposal, but only if we are not part of a DestroyAllChildren | |
157 | if ( m_parent && !m_parent->IsBeingDeleted() ) | |
158 | { | |
159 | delete m_macMinimumStatic; | |
160 | delete m_macMaximumStatic; | |
161 | delete m_macValueStatic; | |
162 | } | |
163 | } | |
164 | ||
165 | int wxSlider::GetValue() const | |
166 | { | |
167 | // We may need to invert the value returned by the widget | |
168 | return ValueInvertOrNot( m_peer->GetValue() ) ; | |
169 | } | |
170 | ||
171 | void wxSlider::SetValue(int value) | |
172 | { | |
173 | if ( m_macValueStatic ) | |
174 | { | |
175 | wxString valuestring; | |
176 | valuestring.Printf( wxT("%d"), value ); | |
177 | m_macValueStatic->SetLabel( valuestring ); | |
178 | } | |
179 | ||
180 | // We only invert for the setting of the actual native widget | |
181 | m_peer->SetValue( ValueInvertOrNot( value ) ); | |
182 | } | |
183 | ||
184 | void wxSlider::SetRange(int minValue, int maxValue) | |
185 | { | |
186 | wxString value; | |
187 | ||
188 | m_rangeMin = minValue; | |
189 | m_rangeMax = maxValue; | |
190 | ||
191 | m_peer->SetMinimum( m_rangeMin ); | |
192 | m_peer->SetMaximum( m_rangeMax ); | |
193 | ||
194 | if (m_macMinimumStatic) | |
195 | { | |
196 | value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) ); | |
197 | m_macMinimumStatic->SetLabel( value ); | |
198 | } | |
199 | ||
200 | if (m_macMaximumStatic) | |
201 | { | |
202 | value.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) ); | |
203 | m_macMaximumStatic->SetLabel( value ); | |
204 | } | |
205 | ||
206 | // If the range is out of bounds, set it to a | |
207 | // value that is within bounds | |
208 | // RN: Testing reveals OSX does its own | |
209 | // bounding, perhaps this isn't needed? | |
210 | int currentValue = GetValue(); | |
211 | ||
212 | if(currentValue < m_rangeMin) | |
213 | SetValue(m_rangeMin); | |
214 | else if(currentValue > m_rangeMax) | |
215 | SetValue(m_rangeMax); | |
216 | } | |
217 | ||
218 | // For trackbars only | |
219 | void wxSlider::SetTickFreq(int n, int pos) | |
220 | { | |
221 | // TODO | |
222 | m_tickFreq = n; | |
223 | } | |
224 | ||
225 | void wxSlider::SetPageSize(int pageSize) | |
226 | { | |
227 | // TODO | |
228 | m_pageSize = pageSize; | |
229 | } | |
230 | ||
231 | int wxSlider::GetPageSize() const | |
232 | { | |
233 | return m_pageSize; | |
234 | } | |
235 | ||
236 | void wxSlider::ClearSel() | |
237 | { | |
238 | // TODO | |
239 | } | |
240 | ||
241 | void wxSlider::ClearTicks() | |
242 | { | |
243 | // TODO | |
244 | } | |
245 | ||
246 | void wxSlider::SetLineSize(int lineSize) | |
247 | { | |
248 | m_lineSize = lineSize; | |
249 | // TODO | |
250 | } | |
251 | ||
252 | int wxSlider::GetLineSize() const | |
253 | { | |
254 | // TODO | |
255 | return 0; | |
256 | } | |
257 | ||
258 | int wxSlider::GetSelEnd() const | |
259 | { | |
260 | // TODO | |
261 | return 0; | |
262 | } | |
263 | ||
264 | int wxSlider::GetSelStart() const | |
265 | { | |
266 | // TODO | |
267 | return 0; | |
268 | } | |
269 | ||
270 | void wxSlider::SetSelection(int minPos, int maxPos) | |
271 | { | |
272 | // TODO | |
273 | } | |
274 | ||
275 | void wxSlider::SetThumbLength(int len) | |
276 | { | |
277 | // TODO | |
278 | } | |
279 | ||
280 | int wxSlider::GetThumbLength() const | |
281 | { | |
282 | // TODO | |
283 | return 0; | |
284 | } | |
285 | ||
286 | void wxSlider::SetTick(int tickPos) | |
287 | { | |
288 | // TODO | |
289 | } | |
290 | ||
291 | void wxSlider::Command(wxCommandEvent &event) | |
292 | { | |
293 | SetValue(event.GetInt()); | |
294 | ProcessCommand(event); | |
295 | } | |
296 | ||
297 | void wxSlider::MacHandleControlClick( WXWidget control, wxInt16 controlpart, bool mouseStillDown ) | |
298 | { | |
299 | // Whatever the native value is, we may need to invert it for calling | |
300 | // SetValue and putting the possibly inverted value in the event | |
301 | int value = ValueInvertOrNot( m_peer->GetValue() ); | |
302 | ||
303 | SetValue( value ); | |
304 | ||
305 | wxScrollEvent event( wxEVT_SCROLL_THUMBTRACK, m_windowId ); | |
306 | event.SetPosition( value ); | |
307 | event.SetEventObject( this ); | |
308 | GetEventHandler()->ProcessEvent( event ); | |
309 | ||
310 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId ); | |
311 | cevent.SetInt( value ); | |
312 | cevent.SetEventObject( this ); | |
313 | GetEventHandler()->ProcessEvent( cevent ); | |
314 | } | |
315 | ||
316 | wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent ) | |
317 | { | |
318 | // Whatever the native value is, we may need to invert it for calling | |
319 | // SetValue and putting the possibly inverted value in the event | |
320 | int value = ValueInvertOrNot( m_peer->GetValue() ) ; | |
321 | ||
322 | SetValue( value ) ; | |
323 | ||
324 | wxScrollEvent event( wxEVT_SCROLL_THUMBRELEASE, m_windowId ); | |
325 | event.SetPosition( value ); | |
326 | event.SetEventObject( this ); | |
327 | GetEventHandler()->ProcessEvent( event ); | |
328 | ||
329 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId ); | |
330 | cevent.SetInt( value ); | |
331 | cevent.SetEventObject( this ); | |
332 | ||
333 | GetEventHandler()->ProcessEvent( cevent ); | |
334 | ||
335 | return noErr; | |
336 | } | |
337 | ||
338 | // This is overloaded in wxSlider so that the proper width/height will always be used | |
339 | // for the slider different values would cause redrawing and mouse detection problems | |
340 | // | |
341 | void wxSlider::DoSetSizeHints( int minW, int minH, | |
342 | int maxW, int maxH, | |
343 | int incW, int incH ) | |
344 | { | |
345 | wxSize size = GetBestSize(); | |
346 | ||
347 | if (GetWindowStyle() & wxSL_VERTICAL) | |
348 | wxWindow::DoSetSizeHints(size.x, minH, size.x, maxH, incW, incH); | |
349 | else | |
350 | wxWindow::DoSetSizeHints(minW, size.y, maxW, size.y, incW, incH); | |
351 | } | |
352 | ||
353 | wxSize wxSlider::DoGetBestSize() const | |
354 | { | |
355 | wxSize size; | |
356 | int textwidth, textheight; | |
357 | int mintwidth, mintheight; | |
358 | int maxtwidth, maxtheight; | |
359 | ||
360 | textwidth = textheight = 0; | |
361 | mintwidth = mintheight = 0; | |
362 | maxtwidth = maxtheight = 0; | |
363 | ||
364 | if (GetWindowStyle() & wxSL_LABELS) | |
365 | { | |
366 | wxString text; | |
367 | ||
368 | // Get maximum text label width and height | |
369 | text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMin ) ); | |
370 | GetTextExtent(text, &mintwidth, &mintheight); | |
371 | text.Printf( wxT("%d"), ValueInvertOrNot( m_rangeMax ) ); | |
372 | GetTextExtent(text, &maxtwidth, &maxtheight); | |
373 | ||
374 | if (maxtheight > mintheight) | |
375 | textheight = maxtheight; | |
376 | else | |
377 | textheight = mintheight; | |
378 | ||
379 | if (maxtwidth > mintwidth) | |
380 | textwidth = maxtwidth; | |
381 | else | |
382 | textwidth = mintwidth; | |
383 | } | |
384 | ||
385 | if (GetWindowStyle() & wxSL_VERTICAL) | |
386 | { | |
387 | size.y = 150; | |
388 | ||
389 | if (GetWindowStyle() & wxSL_AUTOTICKS) | |
390 | size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS; | |
391 | else | |
392 | size.x = wxSLIDER_DIMENSIONACROSS_ARROW; | |
393 | ||
394 | if (GetWindowStyle() & wxSL_LABELS) | |
395 | size.x += textwidth + wxSLIDER_BORDERTEXT; | |
396 | } | |
397 | else | |
398 | { | |
399 | size.x = 150; | |
400 | ||
401 | if (GetWindowStyle() & wxSL_AUTOTICKS) | |
402 | size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS; | |
403 | else | |
404 | size.y = wxSLIDER_DIMENSIONACROSS_ARROW; | |
405 | ||
406 | if (GetWindowStyle() & wxSL_LABELS) | |
407 | { | |
408 | size.y += textheight + wxSLIDER_BORDERTEXT; | |
409 | size.x += (mintwidth / 2) + (maxtwidth / 2); | |
410 | } | |
411 | } | |
412 | ||
413 | return size; | |
414 | } | |
415 | ||
416 | void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags) | |
417 | { | |
418 | int yborder = 0; | |
419 | int minValWidth, maxValWidth, textheight; | |
420 | int sliderBreadth; | |
421 | int width = w; | |
422 | ||
423 | if (GetWindowStyle() & wxSL_LABELS) | |
424 | { | |
425 | wxString text; | |
426 | int ht, valValWidth; | |
427 | ||
428 | // Get maximum text label width and height | |
429 | text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMin ) ); | |
430 | GetTextExtent(text, &minValWidth, &textheight); | |
431 | text.Printf(wxT("%d"), ValueInvertOrNot( m_rangeMax ) ); | |
432 | GetTextExtent(text, &maxValWidth, &ht); | |
433 | ||
434 | if (ht > textheight) | |
435 | textheight = ht; | |
436 | ||
437 | if (GetWindowStyle() & wxSL_HORIZONTAL) | |
438 | { | |
439 | if ( m_macMinimumStatic ) | |
440 | { | |
441 | w -= minValWidth / 2; | |
442 | x += minValWidth / 2; | |
443 | } | |
444 | ||
445 | if ( m_macMaximumStatic ) | |
446 | w -= maxValWidth / 2; | |
447 | } | |
448 | ||
449 | // Labels have this control's parent as their parent | |
450 | // so if this control is not at 0,0 relative to the parent | |
451 | // the labels need to know the position of this control | |
452 | // relative to its parent in order to size properly, so | |
453 | // move the control first so we can use GetPosition() | |
454 | wxControl::DoSetSize( x, y, w, h, sizeFlags ); | |
455 | ||
456 | if (GetWindowStyle() & wxSL_VERTICAL) | |
457 | // If vertical, use current value | |
458 | text.Printf(wxT("%d"), (int)m_peer->GetValue()); | |
459 | else | |
460 | // Use max so that the current value doesn't drift as centering would need to change | |
461 | text.Printf(wxT("%d"), m_rangeMax); | |
462 | ||
463 | GetTextExtent(text, &valValWidth, &ht); | |
464 | ||
465 | yborder = textheight + wxSLIDER_BORDERTEXT; | |
466 | ||
467 | // Get slider breadth | |
468 | if (GetWindowStyle() & wxSL_AUTOTICKS) | |
469 | sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS; | |
470 | else | |
471 | sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW; | |
472 | ||
473 | if (GetWindowStyle() & wxSL_VERTICAL) | |
474 | { | |
475 | h = h - yborder; | |
476 | ||
477 | if ( m_macMinimumStatic ) | |
478 | m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + h - yborder); | |
479 | if ( m_macMaximumStatic ) | |
480 | m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0); | |
481 | if ( m_macValueStatic ) | |
482 | m_macValueStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + (h / 2) - (ht / 2)); | |
483 | } | |
484 | else | |
485 | { | |
486 | if ( m_macMinimumStatic ) | |
487 | m_macMinimumStatic->Move(GetPosition().x, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT); | |
488 | if ( m_macMaximumStatic ) | |
489 | m_macMaximumStatic->Move(GetPosition().x + w - maxValWidth, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT); | |
490 | if ( m_macValueStatic ) | |
491 | m_macValueStatic->Move(GetPosition().x + (w / 2) - (valValWidth / 2), GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT); | |
492 | } | |
493 | } | |
494 | ||
495 | // yet another hack since this is a composite control | |
496 | // when wxSlider has it's size hardcoded, we're not allowed to | |
497 | // change the size. But when the control has labels, we DO need | |
498 | // to resize the internal Mac control to accommodate the text labels. | |
499 | // We need to trick the wxWidgets resize mechanism so that we can | |
500 | // resize the slider part of the control ONLY. | |
501 | ||
502 | // TODO: Can all of this code go in the conditional wxSL_LABELS block? | |
503 | ||
504 | int minWidth = m_minWidth; | |
505 | ||
506 | if (GetWindowStyle() & wxSL_LABELS) | |
507 | { | |
508 | // make sure we don't allow the entire control to be resized accidently | |
509 | if (width == GetSize().x) | |
510 | m_minWidth = -1; | |
511 | } | |
512 | ||
513 | // If the control has labels, we still need to call this again because | |
514 | // the labels alter the control's w and h values. | |
515 | wxControl::DoSetSize( x, y, w, h, sizeFlags ); | |
516 | ||
517 | m_minWidth = minWidth; | |
518 | } | |
519 | ||
520 | void wxSlider::DoMoveWindow(int x, int y, int width, int height) | |
521 | { | |
522 | wxControl::DoMoveWindow( x, y, width, height ); | |
523 | } | |
524 | ||
525 | // Common processing to invert slider values based on wxSL_INVERSE | |
526 | int wxSlider::ValueInvertOrNot(int value) const | |
527 | { | |
528 | int result = 0; | |
529 | ||
530 | if (m_windowStyle & wxSL_VERTICAL) | |
531 | { | |
532 | // The reason for the backwards logic is that Mac's vertical sliders are | |
533 | // inverted compared to Windows and GTK, hence we want inversion to be the | |
534 | // default, and if wxSL_INVERSE is set, then we do not invert (use native) | |
535 | if (m_windowStyle & wxSL_INVERSE) | |
536 | result = value; | |
537 | else | |
538 | result = (m_rangeMax + m_rangeMin) - value; | |
539 | } | |
540 | else // normal logic applies to HORIZONTAL sliders | |
541 | { | |
542 | result = wxSliderBase::ValueInvertOrNot(value); | |
543 | } | |
544 | ||
545 | return result; | |
546 | } | |
547 | ||
548 | #endif // wxUSE_SLIDER |