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