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