]>
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 | ||
16 | #include "wx/slider.h" | |
519cb848 | 17 | #include "wx/mac/uma.h" |
e9576ca5 | 18 | |
2f1ae414 | 19 | #if !USE_SHARED_LIBRARY |
e9576ca5 SC |
20 | IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl) |
21 | ||
22 | BEGIN_EVENT_TABLE(wxSlider, wxControl) | |
23 | END_EVENT_TABLE() | |
2f1ae414 | 24 | #endif |
e9576ca5 | 25 | |
9453cf2b | 26 | // The dimensions of the different styles of sliders (From Aqua document) |
e40298d5 JS |
27 | #define wxSLIDER_DIMENSIONACROSS 15 |
28 | #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24 | |
29 | #define wxSLIDER_DIMENSIONACROSS_ARROW 18 | |
9453cf2b | 30 | |
e40298d5 JS |
31 | // Distance between slider and text |
32 | #define wxSLIDER_BORDERTEXT 5 | |
9453cf2b | 33 | |
e40298d5 JS |
34 | /* NB! The default orientation for a slider is horizontal however if the user specifies |
35 | * some slider styles but dosen't specify the orientation we have to assume he wants a | |
36 | * horizontal one. Therefore in this file when testing for the sliders orientation | |
37 | * vertical is tested for if this is not set then we use the horizontal one | |
38 | * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }> | |
39 | */ | |
40 | ||
41 | // Slider | |
42 | wxSlider::wxSlider() | |
e9576ca5 | 43 | { |
e40298d5 JS |
44 | m_pageSize = 1; |
45 | m_lineSize = 1; | |
46 | m_rangeMax = 0; | |
47 | m_rangeMin = 0; | |
48 | m_tickFreq = 0; | |
e9576ca5 SC |
49 | } |
50 | ||
519cb848 SC |
51 | extern ControlActionUPP wxMacLiveScrollbarActionUPP ; |
52 | ||
e9576ca5 | 53 | bool wxSlider::Create(wxWindow *parent, wxWindowID id, |
e40298d5 JS |
54 | int value, int minValue, int maxValue, |
55 | const wxPoint& pos, | |
56 | const wxSize& size, long style, | |
57 | const wxValidator& validator, | |
58 | const wxString& name) | |
e9576ca5 | 59 | { |
facd6764 SC |
60 | m_macIsUserPane = FALSE ; |
61 | ||
b45ed7a2 VZ |
62 | if ( !wxControl::Create(parent, id, pos, size, style, validator, name) ) |
63 | return false; | |
64 | ||
e40298d5 JS |
65 | m_macMinimumStatic = NULL ; |
66 | m_macMaximumStatic = NULL ; | |
67 | m_macValueStatic = NULL ; | |
68 | ||
69 | ||
70 | m_lineSize = 1; | |
71 | m_tickFreq = 0; | |
72 | ||
73 | m_rangeMax = maxValue; | |
74 | m_rangeMin = minValue; | |
75 | ||
76 | m_pageSize = (int)((maxValue-minValue)/10); | |
77 | ||
facd6764 | 78 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; |
e40298d5 | 79 | |
4c37f124 SC |
80 | UInt16 tickMarks = 0 ; |
81 | if ( style & wxSL_AUTOTICKS ) | |
82 | tickMarks = maxValue - minValue ; | |
83 | ||
84 | verify_noerr ( CreateSliderControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , | |
85 | value , minValue , maxValue , kControlSliderPointsDownOrRight , tickMarks , true /* liveTracking */ , | |
86 | wxMacLiveScrollbarActionUPP , (ControlRef*) &m_macControl ) ) ; | |
facd6764 | 87 | |
e40298d5 JS |
88 | if(style & wxSL_VERTICAL) { |
89 | SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width | |
90 | } | |
91 | else { | |
92 | SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height | |
93 | } | |
94 | // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the | |
95 | // proper dimensions, it also means other people cannot bugger the slider with | |
96 | // other values | |
97 | ||
facd6764 SC |
98 | if(style & wxSL_LABELS) |
99 | { | |
100 | m_macMinimumStatic = new wxStaticText( parent, -1, wxEmptyString ); | |
101 | m_macMaximumStatic = new wxStaticText( parent, -1, wxEmptyString ); | |
102 | m_macValueStatic = new wxStaticText( parent, -1, wxEmptyString ); | |
103 | SetRange(minValue, maxValue); | |
104 | SetValue(value); | |
105 | } | |
106 | ||
107 | MacPostControlCreate(pos,size) ; | |
e40298d5 JS |
108 | |
109 | return true; | |
e9576ca5 SC |
110 | } |
111 | ||
112 | wxSlider::~wxSlider() | |
113 | { | |
facd6764 SC |
114 | delete m_macMinimumStatic ; |
115 | delete m_macMaximumStatic ; | |
116 | delete m_macValueStatic ; | |
e9576ca5 SC |
117 | } |
118 | ||
119 | int wxSlider::GetValue() const | |
120 | { | |
facd6764 | 121 | return GetControl32BitValue( (ControlRef) m_macControl) ; |
e9576ca5 SC |
122 | } |
123 | ||
124 | void wxSlider::SetValue(int value) | |
125 | { | |
e40298d5 | 126 | wxString valuestring ; |
427ff662 | 127 | valuestring.Printf( wxT("%d") , value ) ; |
e40298d5 JS |
128 | if ( m_macValueStatic ) |
129 | m_macValueStatic->SetLabel( valuestring ) ; | |
facd6764 | 130 | SetControl32BitValue( (ControlRef) m_macControl , value ) ; |
e9576ca5 SC |
131 | } |
132 | ||
133 | void wxSlider::SetRange(int minValue, int maxValue) | |
134 | { | |
e40298d5 JS |
135 | wxString value; |
136 | ||
137 | m_rangeMin = minValue; | |
138 | m_rangeMax = maxValue; | |
139 | ||
facd6764 SC |
140 | SetControl32BitMinimum( (ControlRef) m_macControl, m_rangeMin); |
141 | SetControl32BitMaximum( (ControlRef) m_macControl, m_rangeMax); | |
e40298d5 JS |
142 | |
143 | if(m_macMinimumStatic) { | |
427ff662 | 144 | value.Printf(wxT("%d"), m_rangeMin); |
e40298d5 JS |
145 | m_macMinimumStatic->SetLabel(value); |
146 | } | |
147 | if(m_macMaximumStatic) { | |
427ff662 | 148 | value.Printf(wxT("%d"), m_rangeMax); |
e40298d5 JS |
149 | m_macMaximumStatic->SetLabel(value); |
150 | } | |
151 | SetValue(m_rangeMin); | |
e9576ca5 SC |
152 | } |
153 | ||
154 | // For trackbars only | |
155 | void wxSlider::SetTickFreq(int n, int pos) | |
156 | { | |
157 | // TODO | |
158 | m_tickFreq = n; | |
159 | } | |
160 | ||
161 | void wxSlider::SetPageSize(int pageSize) | |
162 | { | |
163 | // TODO | |
164 | m_pageSize = pageSize; | |
165 | } | |
166 | ||
167 | int wxSlider::GetPageSize() const | |
168 | { | |
169 | return m_pageSize; | |
170 | } | |
171 | ||
172 | void wxSlider::ClearSel() | |
173 | { | |
174 | // TODO | |
175 | } | |
176 | ||
177 | void wxSlider::ClearTicks() | |
178 | { | |
179 | // TODO | |
180 | } | |
181 | ||
182 | void wxSlider::SetLineSize(int lineSize) | |
183 | { | |
184 | m_lineSize = lineSize; | |
185 | // TODO | |
186 | } | |
187 | ||
188 | int wxSlider::GetLineSize() const | |
189 | { | |
190 | // TODO | |
191 | return 0; | |
192 | } | |
193 | ||
194 | int wxSlider::GetSelEnd() const | |
195 | { | |
196 | // TODO | |
197 | return 0; | |
198 | } | |
199 | ||
200 | int wxSlider::GetSelStart() const | |
201 | { | |
202 | // TODO | |
203 | return 0; | |
204 | } | |
205 | ||
206 | void wxSlider::SetSelection(int minPos, int maxPos) | |
207 | { | |
208 | // TODO | |
209 | } | |
210 | ||
211 | void wxSlider::SetThumbLength(int len) | |
212 | { | |
213 | // TODO | |
214 | } | |
215 | ||
216 | int wxSlider::GetThumbLength() const | |
217 | { | |
218 | // TODO | |
219 | return 0; | |
220 | } | |
221 | ||
222 | void wxSlider::SetTick(int tickPos) | |
223 | { | |
224 | // TODO | |
225 | } | |
226 | ||
227 | void wxSlider::Command (wxCommandEvent & event) | |
228 | { | |
e40298d5 JS |
229 | SetValue (event.GetInt()); |
230 | ProcessCommand (event); | |
e9576ca5 SC |
231 | } |
232 | ||
cea9c546 | 233 | void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown ) |
519cb848 | 234 | { |
facd6764 | 235 | SInt16 value = ::GetControl32BitValue( (ControlRef) m_macControl ) ; |
e40298d5 JS |
236 | |
237 | SetValue( value ) ; | |
238 | ||
cea9c546 SC |
239 | wxEventType scrollEvent = wxEVT_NULL ; |
240 | ||
4c37f124 | 241 | scrollEvent = wxEVT_SCROLL_THUMBTRACK; |
cea9c546 SC |
242 | |
243 | wxScrollEvent event(scrollEvent, m_windowId); | |
e40298d5 JS |
244 | event.SetPosition(value); |
245 | event.SetEventObject( this ); | |
246 | GetEventHandler()->ProcessEvent(event); | |
247 | ||
248 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId ); | |
249 | cevent.SetInt( value ); | |
250 | cevent.SetEventObject( this ); | |
251 | ||
252 | GetEventHandler()->ProcessEvent( cevent ); | |
253 | } | |
254 | ||
4c37f124 SC |
255 | wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent ) |
256 | { | |
257 | SInt16 value = ::GetControl32BitValue( (ControlRef) m_macControl ) ; | |
258 | ||
259 | SetValue( value ) ; | |
260 | ||
261 | wxEventType scrollEvent = wxEVT_NULL ; | |
262 | ||
263 | scrollEvent = wxEVT_SCROLL_THUMBRELEASE; | |
264 | ||
265 | wxScrollEvent event(scrollEvent, m_windowId); | |
266 | event.SetPosition(value); | |
267 | event.SetEventObject( this ); | |
268 | GetEventHandler()->ProcessEvent(event); | |
269 | ||
270 | wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId ); | |
271 | cevent.SetInt( value ); | |
272 | cevent.SetEventObject( this ); | |
273 | ||
274 | GetEventHandler()->ProcessEvent( cevent ); | |
275 | return noErr ; | |
276 | } | |
277 | ||
278 | ||
e40298d5 JS |
279 | /* This is overloaded in wxSlider so that the proper width/height will always be used |
280 | * for the slider different values would cause redrawing and mouse detection problems */ | |
281 | void wxSlider::SetSizeHints( int minW, int minH, | |
282 | int maxW , int maxH , | |
283 | int incW , int incH ) | |
284 | { | |
285 | wxSize size = GetBestSize(); | |
286 | ||
287 | if(GetWindowStyle() & wxSL_VERTICAL) { | |
288 | wxWindow::SetSizeHints(size.x, minH, size.x, maxH, incW, incH); | |
289 | } | |
290 | else { | |
291 | wxWindow::SetSizeHints(minW, size.y, maxW, size.y, incW, incH); | |
292 | } | |
519cb848 | 293 | } |
9453cf2b | 294 | |
e40298d5 JS |
295 | wxSize wxSlider::DoGetBestSize() const |
296 | { | |
297 | wxSize size; | |
298 | int textwidth, textheight; | |
299 | ||
300 | if(GetWindowStyle() & wxSL_LABELS) | |
301 | { | |
302 | wxString text; | |
303 | int ht, wd; | |
304 | ||
305 | // Get maximum text label width and height | |
427ff662 | 306 | text.Printf(wxT("%d"), m_rangeMin); |
e40298d5 | 307 | GetTextExtent(text, &textwidth, &textheight); |
427ff662 | 308 | text.Printf(wxT("%d"), m_rangeMax); |
e40298d5 JS |
309 | GetTextExtent(text, &wd, &ht); |
310 | if(ht > textheight) { | |
311 | textheight = ht; | |
312 | } | |
313 | if (wd > textwidth) { | |
314 | textwidth = wd; | |
315 | } | |
316 | } | |
317 | ||
318 | if(GetWindowStyle() & wxSL_VERTICAL) | |
319 | { | |
320 | if(GetWindowStyle() & wxSL_AUTOTICKS) { | |
321 | size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS; | |
322 | } | |
323 | else { | |
324 | size.x = wxSLIDER_DIMENSIONACROSS_ARROW; | |
325 | } | |
326 | if(GetWindowStyle() & wxSL_LABELS) { | |
327 | size.x += textwidth + wxSLIDER_BORDERTEXT; | |
328 | } | |
329 | size.y = 150; | |
330 | } | |
331 | else | |
332 | { | |
333 | if(GetWindowStyle() & wxSL_AUTOTICKS) { | |
334 | size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS; | |
335 | } | |
336 | else { | |
337 | size.y = wxSLIDER_DIMENSIONACROSS_ARROW; | |
338 | } | |
339 | if(GetWindowStyle() & wxSL_LABELS) { | |
340 | size.y += textheight + wxSLIDER_BORDERTEXT; | |
341 | } | |
342 | size.x = 150; | |
343 | } | |
344 | return size; | |
345 | } | |
346 | ||
facd6764 | 347 | void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags) |
327788ac | 348 | { |
e40298d5 JS |
349 | int xborder, yborder; |
350 | int minValWidth, maxValWidth, textwidth, textheight; | |
351 | int sliderBreadth; | |
352 | ||
353 | xborder = yborder = 0; | |
354 | ||
355 | if (GetWindowStyle() & wxSL_LABELS) | |
356 | { | |
357 | wxString text; | |
358 | int ht; | |
359 | ||
360 | // Get maximum text label width and height | |
427ff662 | 361 | text.Printf(wxT("%d"), m_rangeMin); |
e40298d5 | 362 | GetTextExtent(text, &minValWidth, &textheight); |
427ff662 | 363 | text.Printf(wxT("%d"), m_rangeMax); |
e40298d5 JS |
364 | GetTextExtent(text, &maxValWidth, &ht); |
365 | if(ht > textheight) { | |
366 | textheight = ht; | |
367 | } | |
368 | textwidth = (minValWidth > maxValWidth ? minValWidth : maxValWidth); | |
369 | ||
370 | xborder = textwidth + wxSLIDER_BORDERTEXT; | |
371 | yborder = textheight + wxSLIDER_BORDERTEXT; | |
372 | ||
373 | // Get slider breadth | |
374 | if(GetWindowStyle() & wxSL_AUTOTICKS) { | |
375 | sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS; | |
376 | } | |
377 | else { | |
378 | sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW; | |
379 | } | |
380 | ||
381 | if(GetWindowStyle() & wxSL_VERTICAL) | |
382 | { | |
facd6764 SC |
383 | |
384 | if ( m_macMinimumStatic ) | |
385 | m_macMinimumStatic->Move(x + sliderBreadth + wxSLIDER_BORDERTEXT, | |
386 | y + h - yborder - textheight); | |
387 | if ( m_macMaximumStatic ) | |
388 | m_macMaximumStatic->Move(x + sliderBreadth + wxSLIDER_BORDERTEXT, y + 0); | |
389 | if ( m_macValueStatic ) | |
390 | m_macValueStatic->Move(0, y + h - textheight); | |
391 | h = h - yborder ; | |
e40298d5 JS |
392 | } |
393 | else | |
394 | { | |
facd6764 SC |
395 | if ( m_macMinimumStatic ) |
396 | m_macMinimumStatic->Move(x + 0, y + sliderBreadth + wxSLIDER_BORDERTEXT); | |
397 | if ( m_macMaximumStatic ) | |
398 | m_macMaximumStatic->Move(x + w - xborder - maxValWidth / 2, | |
399 | y + sliderBreadth + wxSLIDER_BORDERTEXT); | |
400 | if ( m_macValueStatic ) | |
401 | m_macValueStatic->Move(x + w - textwidth, y + 0); | |
402 | w = w - xborder ; | |
e40298d5 JS |
403 | } |
404 | } | |
405 | ||
facd6764 | 406 | wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ; |
327788ac SC |
407 | } |
408 | ||
eb22f2a6 GD |
409 | void wxSlider::DoMoveWindow(int x, int y, int width, int height) |
410 | { | |
327788ac | 411 | wxControl::DoMoveWindow(x,y,width,height) ; |
eb22f2a6 | 412 | } |