]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/slider.cpp
slowly approaching dcscreen...
[wxWidgets.git] / src / mac / classic / slider.cpp
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/defs.h"
17
18 #if wxUSE_SLIDER
19
20 #include "wx/slider.h"
21 #include "wx/mac/uma.h"
22
23 #if !USE_SHARED_LIBRARY
24 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
25
26 BEGIN_EVENT_TABLE(wxSlider, wxControl)
27 END_EVENT_TABLE()
28 #endif
29
30 // The dimensions of the different styles of sliders (From Aqua document)
31 #define wxSLIDER_DIMENSIONACROSS 15
32 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
33 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
34
35 // Distance between slider and text
36 #define wxSLIDER_BORDERTEXT 5
37
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
42 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
43 */
44
45 // Slider
46 wxSlider::wxSlider()
47 {
48 m_pageSize = 1;
49 m_lineSize = 1;
50 m_rangeMax = 0;
51 m_rangeMin = 0;
52 m_tickFreq = 0;
53 }
54
55 extern ControlActionUPP wxMacLiveScrollbarActionUPP ;
56
57 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
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)
63 {
64 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
65 return false;
66
67 Rect bounds ;
68 Str255 title ;
69 SInt16 procID;
70
71 m_macMinimumStatic = NULL ;
72 m_macMaximumStatic = NULL ;
73 m_macValueStatic = NULL ;
74
75
76 m_lineSize = 1;
77 m_tickFreq = 0;
78
79 m_rangeMax = maxValue;
80 m_rangeMin = minValue;
81
82 m_pageSize = (int)((maxValue-minValue)/10);
83
84 MacPreControlCreate( parent, id, wxEmptyString, pos, size, style,
85 validator, name, &bounds, title );
86
87 procID = kControlSliderProc + kControlSliderLiveFeedback;
88 if(style & wxSL_AUTOTICKS) {
89 procID += kControlSliderHasTickMarks;
90 }
91
92
93 m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, title, false,
94 value, minValue, maxValue, procID, (long) this);
95
96 wxASSERT_MSG( (ControlHandle) m_macControl != NULL , wxT("No valid mac control") ) ;
97
98 ::SetControlAction( (ControlHandle) m_macControl , wxMacLiveScrollbarActionUPP ) ;
99
100 if(style & wxSL_LABELS)
101 {
102 m_macMinimumStatic = new wxStaticText( this, wxID_ANY, wxEmptyString );
103 m_macMaximumStatic = new wxStaticText( this, wxID_ANY, wxEmptyString );
104 m_macValueStatic = new wxStaticText( this, wxID_ANY, wxEmptyString );
105 SetRange(minValue, maxValue);
106 SetValue(value);
107 }
108
109 else {
110 m_macMinimumStatic = NULL ;
111 m_macMaximumStatic = NULL ;
112 m_macValueStatic = NULL ;
113 }
114
115 if(style & wxSL_VERTICAL) {
116 SetSizeHints(10, -1, 10, -1); // Forces SetSize to use the proper width
117 }
118 else {
119 SetSizeHints(-1, 10, -1, 10); // Forces SetSize to use the proper height
120 }
121 // NB! SetSizeHints is overloaded by wxSlider and will substitute 10 with the
122 // proper dimensions, it also means other people cannot bugger the slider with
123 // other values
124
125 MacPostControlCreate() ;
126
127 return true;
128 }
129
130 wxSlider::~wxSlider()
131 {
132 }
133
134 int wxSlider::GetValue() const
135 {
136 return GetControl32BitValue( (ControlHandle) m_macControl) ;
137 }
138
139 void wxSlider::SetValue(int value)
140 {
141 wxString valuestring ;
142 valuestring.Printf( wxT("%d") , value ) ;
143 if ( m_macValueStatic )
144 m_macValueStatic->SetLabel( valuestring ) ;
145 SetControl32BitValue( (ControlHandle) m_macControl , value ) ;
146 }
147
148 void wxSlider::SetRange(int minValue, int maxValue)
149 {
150 wxString value;
151
152 m_rangeMin = minValue;
153 m_rangeMax = maxValue;
154
155 SetControl32BitMinimum( (ControlHandle) m_macControl, m_rangeMin);
156 SetControl32BitMaximum( (ControlHandle) m_macControl, m_rangeMax);
157
158 if(m_macMinimumStatic) {
159 value.Printf(wxT("%d"), m_rangeMin);
160 m_macMinimumStatic->SetLabel(value);
161 }
162 if(m_macMaximumStatic) {
163 value.Printf(wxT("%d"), m_rangeMax);
164 m_macMaximumStatic->SetLabel(value);
165 }
166 SetValue(m_rangeMin);
167 }
168
169 // For trackbars only
170 void wxSlider::SetTickFreq(int n, int pos)
171 {
172 // TODO
173 m_tickFreq = n;
174 }
175
176 void wxSlider::SetPageSize(int pageSize)
177 {
178 // TODO
179 m_pageSize = pageSize;
180 }
181
182 int wxSlider::GetPageSize() const
183 {
184 return m_pageSize;
185 }
186
187 void wxSlider::ClearSel()
188 {
189 // TODO
190 }
191
192 void wxSlider::ClearTicks()
193 {
194 // TODO
195 }
196
197 void wxSlider::SetLineSize(int lineSize)
198 {
199 m_lineSize = lineSize;
200 // TODO
201 }
202
203 int wxSlider::GetLineSize() const
204 {
205 // TODO
206 return 0;
207 }
208
209 int wxSlider::GetSelEnd() const
210 {
211 // TODO
212 return 0;
213 }
214
215 int wxSlider::GetSelStart() const
216 {
217 // TODO
218 return 0;
219 }
220
221 void wxSlider::SetSelection(int minPos, int maxPos)
222 {
223 // TODO
224 }
225
226 void wxSlider::SetThumbLength(int len)
227 {
228 // TODO
229 }
230
231 int wxSlider::GetThumbLength() const
232 {
233 // TODO
234 return 0;
235 }
236
237 void wxSlider::SetTick(int tickPos)
238 {
239 // TODO
240 }
241
242 void wxSlider::Command (wxCommandEvent & event)
243 {
244 SetValue (event.GetInt());
245 ProcessCommand (event);
246 }
247
248 void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown )
249 {
250 SInt16 value = ::GetControl32BitValue( (ControlHandle) m_macControl ) ;
251
252 SetValue( value ) ;
253
254 wxEventType scrollEvent = wxEVT_NULL ;
255
256 if ( mouseStillDown )
257 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
258 else
259 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
260
261 wxScrollEvent event(scrollEvent, m_windowId);
262 event.SetPosition(value);
263 event.SetEventObject( this );
264 GetEventHandler()->ProcessEvent(event);
265
266 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
267 cevent.SetInt( value );
268 cevent.SetEventObject( this );
269
270 GetEventHandler()->ProcessEvent( cevent );
271 }
272
273 /* This is overloaded in wxSlider so that the proper width/height will always be used
274 * for the slider different values would cause redrawing and mouse detection problems */
275 void wxSlider::DoSetSizeHints( int minW, int minH,
276 int maxW , int maxH ,
277 int incW , int incH )
278 {
279 wxSize size = GetBestSize();
280
281 if(GetWindowStyle() & wxSL_VERTICAL) {
282 wxWindow::DoSetSizeHints(size.x, minH, size.x, maxH, incW, incH);
283 }
284 else {
285 wxWindow::DoSetSizeHints(minW, size.y, maxW, size.y, incW, incH);
286 }
287 }
288
289 wxSize wxSlider::DoGetBestSize() const
290 {
291 wxSize size;
292 int textwidth, textheight;
293
294 if(GetWindowStyle() & wxSL_LABELS)
295 {
296 wxString text;
297 int ht, wd;
298
299 // Get maximum text label width and height
300 text.Printf(wxT("%d"), m_rangeMin);
301 GetTextExtent(text, &textwidth, &textheight);
302 text.Printf(wxT("%d"), m_rangeMax);
303 GetTextExtent(text, &wd, &ht);
304 if(ht > textheight) {
305 textheight = ht;
306 }
307 if (wd > textwidth) {
308 textwidth = wd;
309 }
310 }
311
312 if(GetWindowStyle() & wxSL_VERTICAL)
313 {
314 if(GetWindowStyle() & wxSL_AUTOTICKS) {
315 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
316 }
317 else {
318 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
319 }
320 if(GetWindowStyle() & wxSL_LABELS) {
321 size.x += textwidth + wxSLIDER_BORDERTEXT;
322 }
323 size.y = 150;
324 }
325 else
326 {
327 if(GetWindowStyle() & wxSL_AUTOTICKS) {
328 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
329 }
330 else {
331 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
332 }
333 if(GetWindowStyle() & wxSL_LABELS) {
334 size.y += textheight + wxSLIDER_BORDERTEXT;
335 }
336 size.x = 150;
337 }
338 return size;
339 }
340
341 void wxSlider::DoSetSize(int x, int y, int width, int height, int sizeFlags)
342 {
343 wxControl::DoSetSize( x, y , width , height ,sizeFlags ) ;
344 }
345
346 void wxSlider::MacUpdateDimensions()
347 {
348 // actually in the current systems this should never be possible, but later reparenting
349 // may become a reality
350
351 if ( (ControlHandle) m_macControl == NULL )
352 return ;
353
354 if ( GetParent() == NULL )
355 return ;
356
357 WindowRef rootwindow = (WindowRef) MacGetRootWindow() ;
358 if ( rootwindow == NULL )
359 return ;
360
361 int xborder, yborder;
362 int minValWidth, maxValWidth, textwidth, textheight;
363 int sliderBreadth;
364
365 xborder = yborder = 0;
366
367 if (GetWindowStyle() & wxSL_LABELS)
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 m_macMinimumStatic->Move(sliderBreadth + wxSLIDER_BORDERTEXT,
396 m_height - yborder - textheight);
397 m_macMaximumStatic->Move(sliderBreadth + wxSLIDER_BORDERTEXT, 0);
398 m_macValueStatic->Move(0, m_height - textheight);
399 }
400 else
401 {
402 m_macMinimumStatic->Move(0, sliderBreadth + wxSLIDER_BORDERTEXT);
403 m_macMaximumStatic->Move(m_width - xborder - maxValWidth / 2,
404 sliderBreadth + wxSLIDER_BORDERTEXT);
405 m_macValueStatic->Move(m_width - textwidth, 0);
406 }
407 }
408
409 Rect oldBounds ;
410 GetControlBounds( (ControlHandle) m_macControl , &oldBounds ) ;
411
412 int new_x = m_x + MacGetLeftBorderSize() + m_macHorizontalBorder ;
413 int new_y = m_y + MacGetTopBorderSize() + m_macVerticalBorder ;
414 int new_width = m_width - MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder - xborder ;
415 int new_height = m_height - MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder - yborder ;
416
417 GetParent()->MacWindowToRootWindow( & new_x , & new_y ) ;
418 bool doMove = new_x != oldBounds.left || new_y != oldBounds.top ;
419 bool doResize = ( oldBounds.right - oldBounds.left ) != new_width || (oldBounds.bottom - oldBounds.top ) != new_height ;
420 if ( doMove || doResize )
421 {
422 InvalWindowRect( rootwindow, &oldBounds ) ;
423 if ( doMove )
424 {
425 UMAMoveControl( (ControlHandle) m_macControl , new_x , new_y ) ;
426 }
427 if ( doResize )
428 {
429 UMASizeControl( (ControlHandle) m_macControl , new_width , new_height ) ;
430 }
431 }
432 }
433
434 void wxSlider::DoMoveWindow(int x, int y, int width, int height)
435 {
436 wxControl::DoMoveWindow(x,y,width,height) ;
437 }
438
439 #endif // wxUSE_SLIDER