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