]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "slider.h" | |
14 | #endif | |
15 | ||
312ebad4 WS |
16 | #include "wx/defs.h" |
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 |
24 | IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl) |
25 | ||
26 | BEGIN_EVENT_TABLE(wxSlider, wxControl) | |
27 | END_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 |
55 | extern ControlActionUPP wxMacLiveScrollbarActionUPP ; |
56 | ||
e9576ca5 | 57 | bool 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 WS |
72 | |
73 | ||
e40298d5 JS |
74 | m_lineSize = 1; |
75 | m_tickFreq = 0; | |
312ebad4 | 76 | |
e40298d5 JS |
77 | m_rangeMax = maxValue; |
78 | m_rangeMin = minValue; | |
312ebad4 | 79 | |
e40298d5 | 80 | m_pageSize = (int)((maxValue-minValue)/10); |
312ebad4 WS |
81 | |
82 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; | |
83 | ||
344d4802 RN |
84 | // |
85 | // NB: (RN) Ticks here are sometimes off in the GUI if there | |
86 | // is not as many ticks as there are values | |
87 | // | |
4c37f124 SC |
88 | UInt16 tickMarks = 0 ; |
89 | if ( style & wxSL_AUTOTICKS ) | |
344d4802 | 90 | tickMarks = (maxValue - minValue) + 1; //+1 for the 0 value |
312ebad4 | 91 | |
344d4802 RN |
92 | while (tickMarks > 20) |
93 | tickMarks /= 5; //keep the number of tickmarks from becoming unwieldly | |
312ebad4 | 94 | |
21fd5529 | 95 | m_peer = new wxMacControl() ; |
312ebad4 | 96 | verify_noerr ( CreateSliderControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , |
4c37f124 | 97 | value , minValue , maxValue , kControlSliderPointsDownOrRight , tickMarks , true /* liveTracking */ , |
5ca0d812 | 98 | wxMacLiveScrollbarActionUPP , m_peer->GetControlRefAddr() ) ); |
312ebad4 WS |
99 | |
100 | ||
e40298d5 JS |
101 | if(style & wxSL_VERTICAL) { |
102 | SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width | |
103 | } | |
104 | else { | |
105 | SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height | |
106 | } | |
107 | // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the | |
108 | // proper dimensions, it also means other people cannot bugger the slider with | |
109 | // other values | |
312ebad4 | 110 | |
facd6764 SC |
111 | if(style & wxSL_LABELS) |
112 | { | |
312ebad4 WS |
113 | m_macMinimumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString ); |
114 | m_macMaximumStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString ); | |
115 | m_macValueStatic = new wxStaticText( parent, wxID_ANY, wxEmptyString ); | |
facd6764 SC |
116 | SetRange(minValue, maxValue); |
117 | SetValue(value); | |
118 | } | |
119 | ||
120 | MacPostControlCreate(pos,size) ; | |
312ebad4 | 121 | |
e40298d5 | 122 | return true; |
e9576ca5 SC |
123 | } |
124 | ||
125 | wxSlider::~wxSlider() | |
126 | { | |
facd6764 SC |
127 | delete m_macMinimumStatic ; |
128 | delete m_macMaximumStatic ; | |
129 | delete m_macValueStatic ; | |
e9576ca5 SC |
130 | } |
131 | ||
132 | int wxSlider::GetValue() const | |
133 | { | |
5ca0d812 | 134 | return m_peer->GetValue() ; |
e9576ca5 SC |
135 | } |
136 | ||
137 | void wxSlider::SetValue(int value) | |
138 | { | |
e40298d5 | 139 | wxString valuestring ; |
312ebad4 | 140 | valuestring.Printf( wxT("%d") , value ) ; |
e40298d5 JS |
141 | if ( m_macValueStatic ) |
142 | m_macValueStatic->SetLabel( valuestring ) ; | |
5ca0d812 | 143 | m_peer->SetValue( value ) ; |
e9576ca5 SC |
144 | } |
145 | ||
146 | void wxSlider::SetRange(int minValue, int maxValue) | |
147 | { | |
e40298d5 | 148 | wxString value; |
312ebad4 | 149 | |
e40298d5 JS |
150 | m_rangeMin = minValue; |
151 | m_rangeMax = maxValue; | |
312ebad4 | 152 | |
5ca0d812 SC |
153 | m_peer->SetMinimum( m_rangeMin); |
154 | m_peer->SetMaximum( m_rangeMax); | |
312ebad4 | 155 | |
e40298d5 | 156 | if(m_macMinimumStatic) { |
427ff662 | 157 | value.Printf(wxT("%d"), m_rangeMin); |
e40298d5 JS |
158 | m_macMinimumStatic->SetLabel(value); |
159 | } | |
160 | if(m_macMaximumStatic) { | |
427ff662 | 161 | value.Printf(wxT("%d"), m_rangeMax); |
e40298d5 JS |
162 | m_macMaximumStatic->SetLabel(value); |
163 | } | |
164 | SetValue(m_rangeMin); | |
e9576ca5 SC |
165 | } |
166 | ||
167 | // For trackbars only | |
168 | void wxSlider::SetTickFreq(int n, int pos) | |
169 | { | |
170 | // TODO | |
171 | m_tickFreq = n; | |
172 | } | |
173 | ||
174 | void wxSlider::SetPageSize(int pageSize) | |
175 | { | |
176 | // TODO | |
177 | m_pageSize = pageSize; | |
178 | } | |
179 | ||
180 | int wxSlider::GetPageSize() const | |
181 | { | |
182 | return m_pageSize; | |
183 | } | |
184 | ||
185 | void wxSlider::ClearSel() | |
186 | { | |
187 | // TODO | |
188 | } | |
189 | ||
190 | void wxSlider::ClearTicks() | |
191 | { | |
192 | // TODO | |
193 | } | |
194 | ||
195 | void wxSlider::SetLineSize(int lineSize) | |
196 | { | |
197 | m_lineSize = lineSize; | |
198 | // TODO | |
199 | } | |
200 | ||
201 | int wxSlider::GetLineSize() const | |
202 | { | |
203 | // TODO | |
204 | return 0; | |
205 | } | |
206 | ||
207 | int wxSlider::GetSelEnd() const | |
208 | { | |
209 | // TODO | |
210 | return 0; | |
211 | } | |
212 | ||
213 | int wxSlider::GetSelStart() const | |
214 | { | |
215 | // TODO | |
216 | return 0; | |
217 | } | |
218 | ||
219 | void wxSlider::SetSelection(int minPos, int maxPos) | |
220 | { | |
221 | // TODO | |
222 | } | |
223 | ||
224 | void wxSlider::SetThumbLength(int len) | |
225 | { | |
226 | // TODO | |
227 | } | |
228 | ||
229 | int wxSlider::GetThumbLength() const | |
230 | { | |
231 | // TODO | |
232 | return 0; | |
233 | } | |
234 | ||
235 | void wxSlider::SetTick(int tickPos) | |
236 | { | |
237 | // TODO | |
238 | } | |
239 | ||
240 | void wxSlider::Command (wxCommandEvent & event) | |
241 | { | |
e40298d5 JS |
242 | SetValue (event.GetInt()); |
243 | ProcessCommand (event); | |
e9576ca5 SC |
244 | } |
245 | ||
312ebad4 | 246 | void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown ) |
519cb848 | 247 | { |
5ca0d812 | 248 | SInt16 value = m_peer->GetValue() ; |
312ebad4 WS |
249 | |
250 | SetValue( value ) ; | |
251 | ||
cea9c546 | 252 | wxEventType scrollEvent = wxEVT_NULL ; |
312ebad4 | 253 | |
4c37f124 | 254 | scrollEvent = wxEVT_SCROLL_THUMBTRACK; |
312ebad4 | 255 | |
cea9c546 | 256 | wxScrollEvent event(scrollEvent, m_windowId); |
e40298d5 JS |
257 | event.SetPosition(value); |
258 | event.SetEventObject( this ); | |
259 | GetEventHandler()->ProcessEvent(event); | |
312ebad4 | 260 | |
e40298d5 JS |
261 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId ); |
262 | cevent.SetInt( value ); | |
263 | cevent.SetEventObject( this ); | |
312ebad4 | 264 | |
e40298d5 JS |
265 | GetEventHandler()->ProcessEvent( cevent ); |
266 | } | |
267 | ||
312ebad4 | 268 | wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent ) |
4c37f124 | 269 | { |
5ca0d812 | 270 | SInt16 value = m_peer->GetValue() ; |
312ebad4 WS |
271 | |
272 | SetValue( value ) ; | |
273 | ||
4c37f124 | 274 | wxEventType scrollEvent = wxEVT_NULL ; |
312ebad4 | 275 | |
4c37f124 | 276 | scrollEvent = wxEVT_SCROLL_THUMBRELEASE; |
312ebad4 | 277 | |
4c37f124 SC |
278 | wxScrollEvent event(scrollEvent, m_windowId); |
279 | event.SetPosition(value); | |
280 | event.SetEventObject( this ); | |
281 | GetEventHandler()->ProcessEvent(event); | |
312ebad4 | 282 | |
4c37f124 SC |
283 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId ); |
284 | cevent.SetInt( value ); | |
285 | cevent.SetEventObject( this ); | |
312ebad4 | 286 | |
4c37f124 SC |
287 | GetEventHandler()->ProcessEvent( cevent ); |
288 | return noErr ; | |
289 | } | |
290 | ||
291 | ||
e40298d5 JS |
292 | /* This is overloaded in wxSlider so that the proper width/height will always be used |
293 | * for the slider different values would cause redrawing and mouse detection problems */ | |
294 | void wxSlider::SetSizeHints( int minW, int minH, | |
295 | int maxW , int maxH , | |
296 | int incW , int incH ) | |
297 | { | |
298 | wxSize size = GetBestSize(); | |
312ebad4 | 299 | |
e40298d5 JS |
300 | if(GetWindowStyle() & wxSL_VERTICAL) { |
301 | wxWindow::SetSizeHints(size.x, minH, size.x, maxH, incW, incH); | |
302 | } | |
303 | else { | |
304 | wxWindow::SetSizeHints(minW, size.y, maxW, size.y, incW, incH); | |
305 | } | |
519cb848 | 306 | } |
9453cf2b | 307 | |
e40298d5 JS |
308 | wxSize wxSlider::DoGetBestSize() const |
309 | { | |
310 | wxSize size; | |
311 | int textwidth, textheight; | |
312ebad4 | 312 | |
e40298d5 JS |
313 | if(GetWindowStyle() & wxSL_LABELS) |
314 | { | |
315 | wxString text; | |
316 | int ht, wd; | |
312ebad4 | 317 | |
e40298d5 | 318 | // Get maximum text label width and height |
427ff662 | 319 | text.Printf(wxT("%d"), m_rangeMin); |
e40298d5 | 320 | GetTextExtent(text, &textwidth, &textheight); |
427ff662 | 321 | text.Printf(wxT("%d"), m_rangeMax); |
e40298d5 JS |
322 | GetTextExtent(text, &wd, &ht); |
323 | if(ht > textheight) { | |
324 | textheight = ht; | |
325 | } | |
326 | if (wd > textwidth) { | |
327 | textwidth = wd; | |
328 | } | |
329 | } | |
312ebad4 | 330 | |
e40298d5 JS |
331 | if(GetWindowStyle() & wxSL_VERTICAL) |
332 | { | |
333 | if(GetWindowStyle() & wxSL_AUTOTICKS) { | |
334 | size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS; | |
335 | } | |
336 | else { | |
337 | size.x = wxSLIDER_DIMENSIONACROSS_ARROW; | |
338 | } | |
339 | if(GetWindowStyle() & wxSL_LABELS) { | |
340 | size.x += textwidth + wxSLIDER_BORDERTEXT; | |
341 | } | |
342 | size.y = 150; | |
343 | } | |
344 | else | |
345 | { | |
346 | if(GetWindowStyle() & wxSL_AUTOTICKS) { | |
347 | size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS; | |
348 | } | |
349 | else { | |
350 | size.y = wxSLIDER_DIMENSIONACROSS_ARROW; | |
351 | } | |
352 | if(GetWindowStyle() & wxSL_LABELS) { | |
353 | size.y += textheight + wxSLIDER_BORDERTEXT; | |
354 | } | |
355 | size.x = 150; | |
356 | } | |
357 | return size; | |
358 | } | |
359 | ||
facd6764 | 360 | void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags) |
327788ac | 361 | { |
e40298d5 JS |
362 | int xborder, yborder; |
363 | int minValWidth, maxValWidth, textwidth, textheight; | |
364 | int sliderBreadth; | |
312ebad4 | 365 | |
e40298d5 | 366 | xborder = yborder = 0; |
f26ca7f8 | 367 | |
e40298d5 JS |
368 | if (GetWindowStyle() & wxSL_LABELS) |
369 | { | |
f26ca7f8 KO |
370 | //Labels have this control's parent as their parent |
371 | //so if this control is not at 0,0 relative to the parent | |
372 | //the labels need to know the position of this control | |
373 | //relative to its parent in order to size properly, so | |
374 | //move the control first so we can use GetPosition() | |
375 | wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ; | |
312ebad4 | 376 | |
e40298d5 JS |
377 | wxString text; |
378 | int ht; | |
312ebad4 | 379 | |
e40298d5 | 380 | // Get maximum text label width and height |
427ff662 | 381 | text.Printf(wxT("%d"), m_rangeMin); |
e40298d5 | 382 | GetTextExtent(text, &minValWidth, &textheight); |
427ff662 | 383 | text.Printf(wxT("%d"), m_rangeMax); |
e40298d5 JS |
384 | GetTextExtent(text, &maxValWidth, &ht); |
385 | if(ht > textheight) { | |
386 | textheight = ht; | |
387 | } | |
388 | textwidth = (minValWidth > maxValWidth ? minValWidth : maxValWidth); | |
312ebad4 | 389 | |
e40298d5 JS |
390 | xborder = textwidth + wxSLIDER_BORDERTEXT; |
391 | yborder = textheight + wxSLIDER_BORDERTEXT; | |
312ebad4 | 392 | |
e40298d5 JS |
393 | // Get slider breadth |
394 | if(GetWindowStyle() & wxSL_AUTOTICKS) { | |
395 | sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS; | |
396 | } | |
397 | else { | |
398 | sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW; | |
399 | } | |
312ebad4 | 400 | |
e40298d5 JS |
401 | if(GetWindowStyle() & wxSL_VERTICAL) |
402 | { | |
f26ca7f8 | 403 | h = h - yborder ; |
312ebad4 | 404 | |
facd6764 | 405 | if ( m_macMinimumStatic ) |
f26ca7f8 | 406 | m_macMinimumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, |
6dfa00e8 | 407 | GetPosition().y + h - yborder); |
facd6764 | 408 | if ( m_macMaximumStatic ) |
f26ca7f8 | 409 | m_macMaximumStatic->Move(GetPosition().x + sliderBreadth + wxSLIDER_BORDERTEXT, GetPosition().y + 0); |
facd6764 | 410 | if ( m_macValueStatic ) |
6dfa00e8 | 411 | m_macValueStatic->Move(GetPosition().x, GetPosition().y + h ); |
e40298d5 JS |
412 | } |
413 | else | |
414 | { | |
1dc1b891 | 415 | w = w - xborder ; |
facd6764 | 416 | if ( m_macMinimumStatic ) |
f26ca7f8 | 417 | m_macMinimumStatic->Move(GetPosition().x + 0, GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT); |
facd6764 | 418 | if ( m_macMaximumStatic ) |
f26ca7f8 KO |
419 | m_macMaximumStatic->Move(GetPosition().x + w - (maxValWidth/2), |
420 | GetPosition().y + sliderBreadth + wxSLIDER_BORDERTEXT); | |
facd6764 | 421 | if ( m_macValueStatic ) |
f26ca7f8 | 422 | m_macValueStatic->Move(GetPosition().x + w, GetPosition().y + 0); |
e40298d5 JS |
423 | } |
424 | } | |
f26ca7f8 | 425 | //If the control has labels, we still need to call this again because |
312ebad4 | 426 | //the labels alter the control's w and h values. |
facd6764 | 427 | wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ; |
f26ca7f8 | 428 | |
327788ac SC |
429 | } |
430 | ||
eb22f2a6 GD |
431 | void wxSlider::DoMoveWindow(int x, int y, int width, int height) |
432 | { | |
327788ac | 433 | wxControl::DoMoveWindow(x,y,width,height) ; |
eb22f2a6 | 434 | } |
312ebad4 WS |
435 | |
436 | #endif // wxUSE_SLIDER |