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