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