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