]>
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 | m_peer = new wxMacControl() ; | |
85 | verify_noerr ( CreateSliderControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , | |
86 | value , minValue , maxValue , kControlSliderPointsDownOrRight , tickMarks , true /* liveTracking */ , | |
87 | wxMacLiveScrollbarActionUPP , *m_peer ) ); | |
88 | ||
89 | ||
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 | ||
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) ; | |
110 | ||
111 | return true; | |
112 | } | |
113 | ||
114 | wxSlider::~wxSlider() | |
115 | { | |
116 | delete m_macMinimumStatic ; | |
117 | delete m_macMaximumStatic ; | |
118 | delete m_macValueStatic ; | |
119 | } | |
120 | ||
121 | int wxSlider::GetValue() const | |
122 | { | |
123 | return GetControl32BitValue( *m_peer) ; | |
124 | } | |
125 | ||
126 | void wxSlider::SetValue(int value) | |
127 | { | |
128 | wxString valuestring ; | |
129 | valuestring.Printf( wxT("%d") , value ) ; | |
130 | if ( m_macValueStatic ) | |
131 | m_macValueStatic->SetLabel( valuestring ) ; | |
132 | SetControl32BitValue( *m_peer , value ) ; | |
133 | } | |
134 | ||
135 | void wxSlider::SetRange(int minValue, int maxValue) | |
136 | { | |
137 | wxString value; | |
138 | ||
139 | m_rangeMin = minValue; | |
140 | m_rangeMax = maxValue; | |
141 | ||
142 | SetControl32BitMinimum( *m_peer, m_rangeMin); | |
143 | SetControl32BitMaximum( *m_peer, m_rangeMax); | |
144 | ||
145 | if(m_macMinimumStatic) { | |
146 | value.Printf(wxT("%d"), m_rangeMin); | |
147 | m_macMinimumStatic->SetLabel(value); | |
148 | } | |
149 | if(m_macMaximumStatic) { | |
150 | value.Printf(wxT("%d"), m_rangeMax); | |
151 | m_macMaximumStatic->SetLabel(value); | |
152 | } | |
153 | SetValue(m_rangeMin); | |
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 | { | |
231 | SetValue (event.GetInt()); | |
232 | ProcessCommand (event); | |
233 | } | |
234 | ||
235 | void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown ) | |
236 | { | |
237 | SInt16 value = ::GetControl32BitValue( *m_peer ) ; | |
238 | ||
239 | SetValue( value ) ; | |
240 | ||
241 | wxEventType scrollEvent = wxEVT_NULL ; | |
242 | ||
243 | scrollEvent = wxEVT_SCROLL_THUMBTRACK; | |
244 | ||
245 | wxScrollEvent event(scrollEvent, m_windowId); | |
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 | ||
257 | wxInt32 wxSlider::MacControlHit( WXEVENTHANDLERREF handler , WXEVENTREF mevent ) | |
258 | { | |
259 | SInt16 value = ::GetControl32BitValue( *m_peer ) ; | |
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 | ||
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 | } | |
295 | } | |
296 | ||
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 | |
308 | text.Printf(wxT("%d"), m_rangeMin); | |
309 | GetTextExtent(text, &textwidth, &textheight); | |
310 | text.Printf(wxT("%d"), m_rangeMax); | |
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 | ||
349 | void wxSlider::DoSetSize(int x, int y, int w, int h, int sizeFlags) | |
350 | { | |
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 | |
363 | text.Printf(wxT("%d"), m_rangeMin); | |
364 | GetTextExtent(text, &minValWidth, &textheight); | |
365 | text.Printf(wxT("%d"), m_rangeMax); | |
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 | { | |
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 ; | |
394 | } | |
395 | else | |
396 | { | |
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 ; | |
405 | } | |
406 | } | |
407 | ||
408 | wxControl::DoSetSize( x, y , w , h ,sizeFlags ) ; | |
409 | } | |
410 | ||
411 | void wxSlider::DoMoveWindow(int x, int y, int width, int height) | |
412 | { | |
413 | wxControl::DoMoveWindow(x,y,width,height) ; | |
414 | } |