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