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