]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/slider.cpp
[#1472076] Fixes after stock gdi changes within core library.
[wxWidgets.git] / src / mac / classic / slider.cpp
CommitLineData
2646f485
SC
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
65571936 9// Licence: wxWindows licence
2646f485
SC
10/////////////////////////////////////////////////////////////////////////////
11
312ebad4
WS
12#include "wx/defs.h"
13
14#if wxUSE_SLIDER
15
2646f485
SC
16#include "wx/slider.h"
17#include "wx/mac/uma.h"
18
2646f485
SC
19IMPLEMENT_DYNAMIC_CLASS(wxSlider, wxControl)
20
21BEGIN_EVENT_TABLE(wxSlider, wxControl)
22END_EVENT_TABLE()
2646f485
SC
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
312ebad4 28
2646f485
SC
29// Distance between slider and text
30#define wxSLIDER_BORDERTEXT 5
312ebad4 31
2646f485
SC
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
312ebad4 36 * eg. if(GetWindowStyle() & wxSL_VERTICAL) {} else { horizontal case }>
2646f485
SC
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
49extern ControlActionUPP wxMacLiveScrollbarActionUPP ;
50
51bool 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;
312ebad4 64
2646f485
SC
65 m_macMinimumStatic = NULL ;
66 m_macMaximumStatic = NULL ;
67 m_macValueStatic = NULL ;
312ebad4
WS
68
69
2646f485
SC
70 m_lineSize = 1;
71 m_tickFreq = 0;
312ebad4 72
2646f485
SC
73 m_rangeMax = maxValue;
74 m_rangeMin = minValue;
312ebad4 75
2646f485 76 m_pageSize = (int)((maxValue-minValue)/10);
312ebad4 77
2646f485
SC
78 MacPreControlCreate( parent, id, wxEmptyString, pos, size, style,
79 validator, name, &bounds, title );
312ebad4 80
2646f485
SC
81 procID = kControlSliderProc + kControlSliderLiveFeedback;
82 if(style & wxSL_AUTOTICKS) {
83 procID += kControlSliderHasTickMarks;
84 }
312ebad4
WS
85
86
6bc3b8e9 87 m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, title, false,
2646f485 88 value, minValue, maxValue, procID, (long) this);
312ebad4 89
2646f485 90 wxASSERT_MSG( (ControlHandle) m_macControl != NULL , wxT("No valid mac control") ) ;
312ebad4 91
2646f485 92 ::SetControlAction( (ControlHandle) m_macControl , wxMacLiveScrollbarActionUPP ) ;
312ebad4 93
2646f485
SC
94 if(style & wxSL_LABELS)
95 {
312ebad4
WS
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 );
2646f485
SC
99 SetRange(minValue, maxValue);
100 SetValue(value);
101 }
312ebad4 102
2646f485
SC
103 else {
104 m_macMinimumStatic = NULL ;
105 m_macMaximumStatic = NULL ;
106 m_macValueStatic = NULL ;
107 }
312ebad4 108
2646f485
SC
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
312ebad4 118
2646f485 119 MacPostControlCreate() ;
312ebad4 120
2646f485
SC
121 return true;
122}
123
124wxSlider::~wxSlider()
125{
126}
127
128int wxSlider::GetValue() const
129{
130 return GetControl32BitValue( (ControlHandle) m_macControl) ;
131}
132
133void wxSlider::SetValue(int value)
134{
135 wxString valuestring ;
312ebad4 136 valuestring.Printf( wxT("%d") , value ) ;
2646f485
SC
137 if ( m_macValueStatic )
138 m_macValueStatic->SetLabel( valuestring ) ;
139 SetControl32BitValue( (ControlHandle) m_macControl , value ) ;
140}
141
142void wxSlider::SetRange(int minValue, int maxValue)
143{
144 wxString value;
312ebad4 145
2646f485
SC
146 m_rangeMin = minValue;
147 m_rangeMax = maxValue;
312ebad4 148
2646f485
SC
149 SetControl32BitMinimum( (ControlHandle) m_macControl, m_rangeMin);
150 SetControl32BitMaximum( (ControlHandle) m_macControl, m_rangeMax);
312ebad4 151
2646f485
SC
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
164void wxSlider::SetTickFreq(int n, int pos)
165{
166 // TODO
167 m_tickFreq = n;
168}
169
170void wxSlider::SetPageSize(int pageSize)
171{
172 // TODO
173 m_pageSize = pageSize;
174}
175
176int wxSlider::GetPageSize() const
177{
178 return m_pageSize;
179}
180
181void wxSlider::ClearSel()
182{
183 // TODO
184}
185
186void wxSlider::ClearTicks()
187{
188 // TODO
189}
190
191void wxSlider::SetLineSize(int lineSize)
192{
193 m_lineSize = lineSize;
194 // TODO
195}
196
197int wxSlider::GetLineSize() const
198{
199 // TODO
200 return 0;
201}
202
203int wxSlider::GetSelEnd() const
204{
205 // TODO
206 return 0;
207}
208
209int wxSlider::GetSelStart() const
210{
211 // TODO
212 return 0;
213}
214
215void wxSlider::SetSelection(int minPos, int maxPos)
216{
217 // TODO
218}
219
220void wxSlider::SetThumbLength(int len)
221{
222 // TODO
223}
224
225int wxSlider::GetThumbLength() const
226{
227 // TODO
228 return 0;
229}
230
231void wxSlider::SetTick(int tickPos)
232{
233 // TODO
234}
235
236void wxSlider::Command (wxCommandEvent & event)
237{
238 SetValue (event.GetInt());
239 ProcessCommand (event);
240}
241
312ebad4 242void wxSlider::MacHandleControlClick( WXWidget control , wxInt16 controlpart, bool mouseStillDown )
2646f485
SC
243{
244 SInt16 value = ::GetControl32BitValue( (ControlHandle) m_macControl ) ;
312ebad4
WS
245
246 SetValue( value ) ;
247
2646f485 248 wxEventType scrollEvent = wxEVT_NULL ;
312ebad4 249
2646f485
SC
250 if ( mouseStillDown )
251 scrollEvent = wxEVT_SCROLL_THUMBTRACK;
252 else
253 scrollEvent = wxEVT_SCROLL_THUMBRELEASE;
312ebad4 254
2646f485
SC
255 wxScrollEvent event(scrollEvent, m_windowId);
256 event.SetPosition(value);
257 event.SetEventObject( this );
258 GetEventHandler()->ProcessEvent(event);
312ebad4 259
2646f485
SC
260 wxCommandEvent cevent( wxEVT_COMMAND_SLIDER_UPDATED, m_windowId );
261 cevent.SetInt( value );
262 cevent.SetEventObject( this );
312ebad4 263
2646f485
SC
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 */
024f89f9
VS
269void wxSlider::DoSetSizeHints( int minW, int minH,
270 int maxW , int maxH ,
271 int incW , int incH )
2646f485
SC
272{
273 wxSize size = GetBestSize();
312ebad4 274
2646f485 275 if(GetWindowStyle() & wxSL_VERTICAL) {
024f89f9 276 wxWindow::DoSetSizeHints(size.x, minH, size.x, maxH, incW, incH);
2646f485
SC
277 }
278 else {
024f89f9 279 wxWindow::DoSetSizeHints(minW, size.y, maxW, size.y, incW, incH);
2646f485
SC
280 }
281}
282
283wxSize wxSlider::DoGetBestSize() const
284{
285 wxSize size;
286 int textwidth, textheight;
312ebad4 287
2646f485
SC
288 if(GetWindowStyle() & wxSL_LABELS)
289 {
290 wxString text;
291 int ht, wd;
312ebad4 292
2646f485
SC
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 }
312ebad4 305
2646f485
SC
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
335void wxSlider::DoSetSize(int x, int y, int width, int height, int sizeFlags)
336{
337 wxControl::DoSetSize( x, y , width , height ,sizeFlags ) ;
338}
339
312ebad4 340void wxSlider::MacUpdateDimensions()
2646f485
SC
341{
342 // actually in the current systems this should never be possible, but later reparenting
343 // may become a reality
312ebad4 344
2646f485
SC
345 if ( (ControlHandle) m_macControl == NULL )
346 return ;
312ebad4 347
2646f485
SC
348 if ( GetParent() == NULL )
349 return ;
312ebad4 350
2646f485
SC
351 WindowRef rootwindow = (WindowRef) MacGetRootWindow() ;
352 if ( rootwindow == NULL )
353 return ;
312ebad4 354
2646f485
SC
355 int xborder, yborder;
356 int minValWidth, maxValWidth, textwidth, textheight;
357 int sliderBreadth;
312ebad4 358
2646f485 359 xborder = yborder = 0;
312ebad4 360
2646f485
SC
361 if (GetWindowStyle() & wxSL_LABELS)
362 {
363 wxString text;
364 int ht;
312ebad4 365
2646f485
SC
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);
312ebad4 375
2646f485
SC
376 xborder = textwidth + wxSLIDER_BORDERTEXT;
377 yborder = textheight + wxSLIDER_BORDERTEXT;
312ebad4 378
2646f485
SC
379 // Get slider breadth
380 if(GetWindowStyle() & wxSL_AUTOTICKS) {
381 sliderBreadth = wxSLIDER_DIMENSIONACROSS_WITHTICKMARKS;
382 }
383 else {
384 sliderBreadth = wxSLIDER_DIMENSIONACROSS_ARROW;
385 }
312ebad4 386
2646f485
SC
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 }
312ebad4
WS
402
403 Rect oldBounds ;
404 GetControlBounds( (ControlHandle) m_macControl , &oldBounds ) ;
405
2646f485
SC
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 ;
312ebad4 410
2646f485
SC
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
428void wxSlider::DoMoveWindow(int x, int y, int width, int height)
429{
430 wxControl::DoMoveWindow(x,y,width,height) ;
431}
312ebad4
WS
432
433#endif // wxUSE_SLIDER