]> git.saurik.com Git - wxWidgets.git/blob - src/mac/classic/slider.cpp
Now works when derived from wxTextCtrlBase, using wxScrollHelper.
[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 #include "wx/defs.h"
13
14 #if wxUSE_SLIDER
15
16 #include "wx/slider.h"
17 #include "wx/mac/uma.h"
18
19 IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
20
21 BEGIN_EVENT_TABLE(wxSlider, wxControl)
22 END_EVENT_TABLE()
23
24 // The dimensions of the different styles of sliders (From Aqua document)
25 #define wxSLIDER_DIMENSIONACROSS 15
26 #define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
27 #define wxSLIDER_DIMENSIONACROSS_ARROW 18
28
29 // Distance between slider and text
30 #define wxSLIDER_BORDERTEXT 5
31
32 /* NB! The default orientation for a slider is horizontal however if the user specifies
33 * some slider styles but dosen't specify the orientation we have to assume he wants a
34 * horizontal one. Therefore in this file when testing for the sliders orientation
35 * vertical is tested for if this is not set then we use the horizontal one
36 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
37 */
38
39 // Slider
40 wxSlider::wxSlider()
41 {
42 m_pageSize = 1;
43 m_lineSize = 1;
44 m_rangeMax = 0;
45 m_rangeMin = 0;
46 m_tickFreq = 0;
47 }
48
49 extern ControlActionUPP wxMacLiveScrollbarActionUPP ;
50
51 bool wxSlider::Create(wxWindow *parent, wxWindowID id,
52 int value, int minValue, int maxValue,
53 const wxPoint& pos,
54 const wxSize& size, long style,
55 const wxValidator& validator,
56 const wxString& name)
57 {
58 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
59 return false;
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, wxEmptyString, 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 = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, title, false,
88 value, minValue, maxValue, procID, (long) this);
89
90 wxASSERT_MSG( (ControlHandle) m_macControl != NULL , wxT("No valid mac control") ) ;
91
92 ::SetControlAction( (ControlHandle) m_macControl , wxMacLiveScrollbarActionUPP ) ;
93
94 if(style & wxSL_LABELS)
95 {
96 m_macMinimumStatic = new wxStaticText( this, wxID_ANY, wxEmptyString );
97 m_macMaximumStatic = new wxStaticText( this, wxID_ANY, wxEmptyString );
98 m_macValueStatic = new wxStaticText( this, wxID_ANY, wxEmptyString );
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( wxT("%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(wxT("%d"), m_rangeMin);
154 m_macMinimumStatic->SetLabel(value);
155 }
156 if(m_macMaximumStatic) {
157 value.Printf(wxT("%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, bool mouseStillDown )
243 {
244 SInt16 value = ::GetControl32BitValue( (ControlHandle) m_macControl ) ;
245
246 SetValue( value ) ;
247
248 wxEventType scrollEvent = wxEVT_NULL ;
249
250 if ( mouseStillDown )
251 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
252 else
253 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
254
255 wxScrollEvent event(scrollEvent, m_windowId);
256 event.SetPosition(value);
257 event.SetEventObject( this );
258 GetEventHandler()->ProcessEvent(event);
259
260 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
261 cevent.SetInt( value );
262 cevent.SetEventObject( this );
263
264 GetEventHandler()->ProcessEvent( cevent );
265 }
266
267 /* This is overloaded in wxSlider so that the proper width/height will always be used
268 * for the slider different values would cause redrawing and mouse detection problems */
269 void wxSlider::DoSetSizeHints( int minW, int minH,
270 int maxW , int maxH ,
271 int incW , int incH )
272 {
273 wxSize size = GetBestSize();
274
275 if(GetWindowStyle() & wxSL_VERTICAL) {
276 wxWindow::DoSetSizeHints(size.x, minH, size.x, maxH, incW, incH);
277 }
278 else {
279 wxWindow::DoSetSizeHints(minW, size.y, maxW, size.y, incW, incH);
280 }
281 }
282
283 wxSize wxSlider::DoGetBestSize() const
284 {
285 wxSize size;
286 int textwidth, textheight;
287
288 if(GetWindowStyle() & wxSL_LABELS)
289 {
290 wxString text;
291 int ht, wd;
292
293 // Get maximum text label width and height
294 text.Printf(wxT("%d"), m_rangeMin);
295 GetTextExtent(text, &textwidth, &textheight);
296 text.Printf(wxT("%d"), m_rangeMax);
297 GetTextExtent(text, &wd, &ht);
298 if(ht > textheight) {
299 textheight = ht;
300 }
301 if (wd > textwidth) {
302 textwidth = wd;
303 }
304 }
305
306 if(GetWindowStyle() & wxSL_VERTICAL)
307 {
308 if(GetWindowStyle() & wxSL_AUTOTICKS) {
309 size.x = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
310 }
311 else {
312 size.x = wxSLIDER_DIMENSIONACROSS_ARROW;
313 }
314 if(GetWindowStyle() & wxSL_LABELS) {
315 size.x += textwidth + wxSLIDER_BORDERTEXT;
316 }
317 size.y = 150;
318 }
319 else
320 {
321 if(GetWindowStyle() & wxSL_AUTOTICKS) {
322 size.y = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
323 }
324 else {
325 size.y = wxSLIDER_DIMENSIONACROSS_ARROW;
326 }
327 if(GetWindowStyle() & wxSL_LABELS) {
328 size.y += textheight + wxSLIDER_BORDERTEXT;
329 }
330 size.x = 150;
331 }
332 return size;
333 }
334
335 void wxSlider::DoSetSize(int x, int y, int width, int height, int sizeFlags)
336 {
337 wxControl::DoSetSize( x, y , width , height ,sizeFlags ) ;
338 }
339
340 void wxSlider::MacUpdateDimensions()
341 {
342 // actually in the current systems this should never be possible, but later reparenting
343 // may become a reality
344
345 if ( (ControlHandle) m_macControl == NULL )
346 return ;
347
348 if ( GetParent() == NULL )
349 return ;
350
351 WindowRef rootwindow = (WindowRef) MacGetRootWindow() ;
352 if ( rootwindow == NULL )
353 return ;
354
355 int xborder, yborder;
356 int minValWidth, maxValWidth, textwidth, textheight;
357 int sliderBreadth;
358
359 xborder = yborder = 0;
360
361 if (GetWindowStyle() & wxSL_LABELS)
362 {
363 wxString text;
364 int ht;
365
366 // Get maximum text label width and height
367 text.Printf(wxT("%d"), m_rangeMin);
368 GetTextExtent(text, &minValWidth, &textheight);
369 text.Printf(wxT("%d"), m_rangeMax);
370 GetTextExtent(text, &maxValWidth, &ht);
371 if(ht > textheight) {
372 textheight = ht;
373 }
374 textwidth = (minValWidth > maxValWidth ? minValWidth : maxValWidth);
375
376 xborder = textwidth + wxSLIDER_BORDERTEXT;
377 yborder = textheight + wxSLIDER_BORDERTEXT;
378
379 // Get slider breadth
380 if(GetWindowStyle() & wxSL_AUTOTICKS) {
381 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
382 }
383 else {
384 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
385 }
386
387 if(GetWindowStyle() & wxSL_VERTICAL)
388 {
389 m_macMinimumStatic->Move(sliderBreadth + wxSLIDER_BORDERTEXT,
390 m_height - yborder - textheight);
391 m_macMaximumStatic->Move(sliderBreadth + wxSLIDER_BORDERTEXT, 0);
392 m_macValueStatic->Move(0, m_height - textheight);
393 }
394 else
395 {
396 m_macMinimumStatic->Move(0, sliderBreadth + wxSLIDER_BORDERTEXT);
397 m_macMaximumStatic->Move(m_width - xborder - maxValWidth / 2,
398 sliderBreadth + wxSLIDER_BORDERTEXT);
399 m_macValueStatic->Move(m_width - textwidth, 0);
400 }
401 }
402
403 Rect oldBounds ;
404 GetControlBounds( (ControlHandle) m_macControl , &oldBounds ) ;
405
406 int new_x = m_x + MacGetLeftBorderSize() + m_macHorizontalBorder ;
407 int new_y = m_y + MacGetTopBorderSize() + m_macVerticalBorder ;
408 int new_width = m_width - MacGetLeftBorderSize() - MacGetRightBorderSize() - 2 * m_macHorizontalBorder - xborder ;
409 int new_height = m_height - MacGetTopBorderSize() - MacGetBottomBorderSize() - 2 * m_macVerticalBorder - yborder ;
410
411 GetParent()->MacWindowToRootWindow( & new_x , & new_y ) ;
412 bool doMove = new_x != oldBounds.left || new_y != oldBounds.top ;
413 bool doResize = ( oldBounds.right - oldBounds.left ) != new_width || (oldBounds.bottom - oldBounds.top ) != new_height ;
414 if ( doMove || doResize )
415 {
416 InvalWindowRect( rootwindow, &oldBounds ) ;
417 if ( doMove )
418 {
419 UMAMoveControl( (ControlHandle) m_macControl , new_x , new_y ) ;
420 }
421 if ( doResize )
422 {
423 UMASizeControl( (ControlHandle) m_macControl , new_width , new_height ) ;
424 }
425 }
426 }
427
428 void wxSlider::DoMoveWindow(int x, int y, int width, int height)
429 {
430 wxControl::DoMoveWindow(x,y,width,height) ;
431 }
432
433 #endif // wxUSE_SLIDER