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