]> git.saurik.com Git - wxWidgets.git/blame - src/mac/slider.cpp
Added wxUSE_TOOLBAR, wxUSE_STATUSBAR tests
[wxWidgets.git] / src / mac / slider.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: slider.cpp
3// Purpose: wxSlider
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
e40298d5 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "slider.h"
14#endif
15
16#include "wx/slider.h"
519cb848 17#include "wx/mac/uma.h"
e9576ca5 18
2f1ae414 19#if !USE_SHARED_LIBRARY
e9576ca5
SC
20IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
21
22BEGIN_EVENT_TABLE(wxSlider, wxControl)
23END_EVENT_TABLE()
2f1ae414 24#endif
e9576ca5 25
9453cf2b 26 // The dimensions of the different styles of sliders (From Aqua document)
e40298d5
JS
27#define wxSLIDER_DIMENSIONACROSS 15
28#define wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS 24
29#define wxSLIDER_DIMENSIONACROSS_ARROW 18
9453cf2b 30
e40298d5
JS
31// Distance between slider and text
32#define wxSLIDER_BORDERTEXT 5
9453cf2b 33
e40298d5
JS
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()
e9576ca5 43{
e40298d5
JS
44 m_pageSize = 1;
45 m_lineSize = 1;
46 m_rangeMax = 0;
47 m_rangeMin = 0;
48 m_tickFreq = 0;
e9576ca5
SC
49}
50
519cb848
SC
51extern ControlActionUPP wxMacLiveScrollbarActionUPP ;
52
e9576ca5 53bool wxSlider::Create(wxWindow *parent, wxWindowID id,
e40298d5
JS
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)
e9576ca5 59{
b45ed7a2
VZ
60 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
61 return false;
62
e40298d5
JS
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
427ff662 80 MacPreControlCreate( parent, id, wxEmptyString, pos, size, style,
e40298d5
JS
81 validator, name, &bounds, title );
82
83 procID = kControlSliderProc + kControlSliderLiveFeedback;
84 if(style & wxSL_AUTOTICKS) {
85 procID += kControlSliderHasTickMarks;
03e11df5 86 }
e40298d5
JS
87
88
89 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, title, false,
90 value, minValue, maxValue, procID, (long) this);
91
427ff662 92 wxASSERT_MSG( (ControlHandle) m_macControl != NULL , wxT("No valid mac control") ) ;
e40298d5
JS
93
94 ::SetControlAction( (ControlHandle) m_macControl , wxMacLiveScrollbarActionUPP ) ;
95
96 if(style & wxSL_LABELS)
97 {
427ff662
SC
98 m_macMinimumStatic = new wxStaticText( this, -1, wxEmptyString );
99 m_macMaximumStatic = new wxStaticText( this, -1, wxEmptyString );
100 m_macValueStatic = new wxStaticText( this, -1, wxEmptyString );
e40298d5
JS
101 SetRange(minValue, maxValue);
102 SetValue(value);
03e11df5 103 }
e40298d5
JS
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;
e9576ca5
SC
124}
125
126wxSlider::~wxSlider()
127{
128}
129
130int wxSlider::GetValue() const
131{
e40298d5 132 return GetControl32BitValue( (ControlHandle) m_macControl) ;
e9576ca5
SC
133}
134
135void wxSlider::SetValue(int value)
136{
e40298d5 137 wxString valuestring ;
427ff662 138 valuestring.Printf( wxT("%d") , value ) ;
e40298d5
JS
139 if ( m_macValueStatic )
140 m_macValueStatic->SetLabel( valuestring ) ;
141 SetControl32BitValue( (ControlHandle) m_macControl , value ) ;
e9576ca5
SC
142}
143
144void wxSlider::SetRange(int minValue, int maxValue)
145{
e40298d5
JS
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) {
427ff662 155 value.Printf(wxT("%d"), m_rangeMin);
e40298d5
JS
156 m_macMinimumStatic->SetLabel(value);
157 }
158 if(m_macMaximumStatic) {
427ff662 159 value.Printf(wxT("%d"), m_rangeMax);
e40298d5
JS
160 m_macMaximumStatic->SetLabel(value);
161 }
162 SetValue(m_rangeMin);
e9576ca5
SC
163}
164
165// For trackbars only
166void wxSlider::SetTickFreq(int n, int pos)
167{
168 // TODO
169 m_tickFreq = n;
170}
171
172void wxSlider::SetPageSize(int pageSize)
173{
174 // TODO
175 m_pageSize = pageSize;
176}
177
178int wxSlider::GetPageSize() const
179{
180 return m_pageSize;
181}
182
183void wxSlider::ClearSel()
184{
185 // TODO
186}
187
188void wxSlider::ClearTicks()
189{
190 // TODO
191}
192
193void wxSlider::SetLineSize(int lineSize)
194{
195 m_lineSize = lineSize;
196 // TODO
197}
198
199int wxSlider::GetLineSize() const
200{
201 // TODO
202 return 0;
203}
204
205int wxSlider::GetSelEnd() const
206{
207 // TODO
208 return 0;
209}
210
211int wxSlider::GetSelStart() const
212{
213 // TODO
214 return 0;
215}
216
217void wxSlider::SetSelection(int minPos, int maxPos)
218{
219 // TODO
220}
221
222void wxSlider::SetThumbLength(int len)
223{
224 // TODO
225}
226
227int wxSlider::GetThumbLength() const
228{
229 // TODO
230 return 0;
231}
232
233void wxSlider::SetTick(int tickPos)
234{
235 // TODO
236}
237
238void wxSlider::Command (wxCommandEvent & event)
239{
e40298d5
JS
240 SetValue (event.GetInt());
241 ProcessCommand (event);
e9576ca5
SC
242}
243
cea9c546 244void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown )
519cb848 245{
e40298d5
JS
246 SInt16 value = ::GetControl32BitValue( (ControlHandle) m_macControl ) ;
247
248 SetValue( value ) ;
249
cea9c546
SC
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);
e40298d5
JS
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 */
271void 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 }
519cb848 283}
9453cf2b 284
e40298d5
JS
285wxSize 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
427ff662 296 text.Printf(wxT("%d"), m_rangeMin);
e40298d5 297 GetTextExtent(text, &textwidth, &textheight);
427ff662 298 text.Printf(wxT("%d"), m_rangeMax);
e40298d5
JS
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
337void wxSlider::DoSetSize(int x, int y, int width, int height, int sizeFlags)
338{
327788ac 339 wxControl::DoSetSize( x, y , width , height ,sizeFlags ) ;
e40298d5
JS
340}
341
342void wxSlider::MacUpdateDimensions()
327788ac 343{
e40298d5
JS
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
327788ac
SC
353 WindowRef rootwindow = (WindowRef) MacGetRootWindow() ;
354 if ( rootwindow == NULL )
e40298d5
JS
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
427ff662 369 text.Printf(wxT("%d"), m_rangeMin);
e40298d5 370 GetTextExtent(text, &minValWidth, &textheight);
427ff662 371 text.Printf(wxT("%d"), m_rangeMax);
e40298d5
JS
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
327788ac
SC
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 ;
e40298d5
JS
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 }
327788ac
SC
428}
429
eb22f2a6
GD
430void wxSlider::DoMoveWindow(int x, int y, int width, int height)
431{
327788ac 432 wxControl::DoMoveWindow(x,y,width,height) ;
eb22f2a6 433}