Fix wxMotif compilation with g++ 4.4.
[wxWidgets.git] / src / motif / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/spinbutt.cpp
3 // Purpose: wxSpinButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_SPINBTN
16
17 #include "wx/spinbutt.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/timer.h"
21 #endif
22
23 #include "wx/spinctrl.h"
24
25 #ifdef __VMS__
26 #pragma message disable nosimpint
27 #endif
28 #include <Xm/ArrowBG.h>
29 #include <Xm/ArrowB.h>
30 #ifdef __VMS__
31 #pragma message enable nosimpint
32 #endif
33
34 #include "wx/motif/private.h"
35
36 // helper class
37 enum ArrowDirection
38 {
39 wxARROW_UP,
40 wxARROW_DOWN,
41 wxARROW_LEFT,
42 wxARROW_RIGHT
43 };
44
45 class wxArrowButtonTimer;
46 class wxArrowButton;
47
48 // ----------------------------------------------------------------------------
49 // wxArrowButtonTimer
50 // ----------------------------------------------------------------------------
51
52 static const unsigned int TICK_BEFORE_START = 10;
53 static const unsigned int TICK_BEFORE_EXPONENTIAL = 40;
54 static const unsigned int MAX_INCREMENT = 150;
55 static const unsigned int TICK_INTERVAL = 113;
56
57 class wxArrowButtonTimer : public wxTimer
58 {
59 public:
60 wxArrowButtonTimer( wxArrowButton* btn, int sign )
61 : m_sign( sign ),
62 m_button( btn )
63 { Reset(); };
64
65 void Notify();
66 void Reset() { m_ticks = 0; m_increment = 1; }
67 private:
68 unsigned int m_ticks;
69 unsigned int m_increment;
70 int m_sign;
71 wxArrowButton* m_button;
72 };
73
74 // ----------------------------------------------------------------------------
75 // wxArrowButton
76 // ----------------------------------------------------------------------------
77
78 class wxArrowButton : public wxControl
79 {
80 friend class wxArrowButtonTimer;
81 public:
82 wxArrowButton( int increment )
83 : m_increment( increment ),
84 m_timer( 0 ) {}
85
86 wxArrowButton( wxSpinButton* parent, wxWindowID id, ArrowDirection d,
87 const wxPoint& pos = wxDefaultPosition,
88 const wxSize& size = wxDefaultSize, int increment = 1 )
89 : wxControl(),
90 m_increment( increment ),
91 m_timer( 0 )
92 {
93 Create( parent, id, d, pos, size );
94 }
95
96 virtual ~wxArrowButton()
97 { delete m_timer; }
98
99 bool Create( wxSpinButton* parent, wxWindowID id, ArrowDirection d,
100 const wxPoint& pos = wxDefaultPosition,
101 const wxSize& size = wxDefaultSize );
102 private:
103 // creates a new timer object, or stops the currently running one
104 wxTimer* GetFreshTimer();
105 wxSpinButton* GetSpinButton() { return (wxSpinButton*)GetParent(); }
106 static void SpinButtonCallback( Widget w, XtPointer clientData,
107 XtPointer WXUNUSED(ptr) );
108 static void StartTimerCallback( Widget w, XtPointer clientData,
109 XtPointer WXUNUSED(ptr) );
110
111 static void StopTimerCallback( Widget w, XtPointer clientData,
112 XtPointer WXUNUSED(ptr) );
113
114 int m_increment;
115 wxArrowButtonTimer* m_timer;
116 };
117
118 // ----------------------------------------------------------------------------
119 // wxArrowButtonTimer implementation
120 // ----------------------------------------------------------------------------
121
122 void wxArrowButtonTimer::Notify()
123 {
124 ++m_ticks;
125 if( m_ticks < TICK_BEFORE_START ) return;
126 // increment every other tick
127 if( m_ticks <= TICK_BEFORE_EXPONENTIAL && m_ticks & 1 )
128 return;
129 if( m_ticks > TICK_BEFORE_EXPONENTIAL )
130 m_increment = 2 * m_increment;
131 if( m_increment >= MAX_INCREMENT ) m_increment = MAX_INCREMENT;
132 m_button->GetSpinButton()->Increment( m_sign * m_increment );
133 }
134
135 // ----------------------------------------------------------------------------
136 // wxArrowButton implementation
137 // ----------------------------------------------------------------------------
138
139 wxTimer* wxArrowButton::GetFreshTimer()
140 {
141 if( m_timer )
142 {
143 m_timer->Stop();
144 m_timer->Reset();
145 }
146 else
147 m_timer = new wxArrowButtonTimer( this, m_increment );
148
149 return m_timer;
150 }
151
152 void wxArrowButton::SpinButtonCallback( Widget w, XtPointer clientData,
153 XtPointer WXUNUSED(ptr) )
154 {
155 if( !wxGetWindowFromTable( w ) )
156 // Widget has been deleted!
157 return;
158
159 wxArrowButton* btn = (wxArrowButton*)clientData;
160
161 btn->GetSpinButton()->Increment( btn->m_increment );
162 }
163
164 void wxArrowButton::StartTimerCallback( Widget w, XtPointer clientData,
165 XtPointer WXUNUSED(ptr) )
166 {
167 if( !wxGetWindowFromTable( w ) )
168 // Widget has been deleted!
169 return;
170
171 wxArrowButton* btn = (wxArrowButton*)clientData;
172 btn->GetFreshTimer()->Start( TICK_INTERVAL );
173 }
174
175 void wxArrowButton::StopTimerCallback( Widget w, XtPointer clientData,
176 XtPointer WXUNUSED(ptr) )
177 {
178 if( !wxGetWindowFromTable( w ) )
179 // Widget has been deleted!
180 return;
181
182 wxArrowButton* btn = (wxArrowButton*)clientData;
183 wxDELETE(btn->m_timer);
184 }
185
186 bool wxArrowButton::Create( wxSpinButton* parent,
187 wxWindowID WXUNUSED(id),
188 ArrowDirection d,
189 const wxPoint& pos, const wxSize& size )
190 {
191 wxCHECK_MSG( parent, false, wxT("must have a valid parent") );
192
193 int arrow_dir = XmARROW_UP;
194
195 switch( d )
196 {
197 case wxARROW_UP:
198 arrow_dir = XmARROW_UP;
199 break;
200 case wxARROW_DOWN:
201 arrow_dir = XmARROW_DOWN;
202 break;
203 case wxARROW_LEFT:
204 arrow_dir = XmARROW_LEFT;
205 break;
206 case wxARROW_RIGHT:
207 arrow_dir = XmARROW_RIGHT;
208 break;
209 }
210
211 parent->AddChild( this );
212 PreCreation();
213
214 Widget parentWidget = (Widget) parent->GetClientWidget();
215 m_mainWidget = (WXWidget) XtVaCreateManagedWidget( "XmArrowButton",
216 xmArrowButtonWidgetClass,
217 parentWidget,
218 XmNarrowDirection, arrow_dir,
219 XmNborderWidth, 0,
220 XmNshadowThickness, 0,
221 NULL );
222
223 XtAddCallback( (Widget) m_mainWidget,
224 XmNactivateCallback, (XtCallbackProc) SpinButtonCallback,
225 (XtPointer) this );
226 XtAddCallback( (Widget) m_mainWidget,
227 XmNarmCallback, (XtCallbackProc) StartTimerCallback,
228 (XtPointer) this );
229 XtAddCallback( (Widget) m_mainWidget,
230 XmNactivateCallback, (XtCallbackProc) StopTimerCallback,
231 (XtPointer) this );
232
233 PostCreation();
234 AttachWidget( parent, m_mainWidget, (WXWidget) NULL,
235 pos.x, pos.y, size.x, size.y );
236
237 return true;
238 }
239
240 // ----------------------------------------------------------------------------
241 // wxSpinButton
242 // ----------------------------------------------------------------------------
243
244 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl)
245 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent)
246
247 static void CalcSizes( const wxPoint& pt, const wxSize& sz,
248 wxPoint& pt1, wxSize& sz1,
249 wxPoint& pt2, wxSize& sz2,
250 bool isVertical )
251 {
252 typedef int wxSize::* CDPTR1;
253 typedef int wxPoint::* CDPTR2;
254
255 sz1 = sz2 = sz;
256 pt2 = pt1 = pt;
257
258 CDPTR1 szm = isVertical ? &wxSize::y : &wxSize::x;
259 CDPTR2 ptm = isVertical ? &wxPoint::y : &wxPoint::x;
260 int dim = sz.*szm, half = dim/2;
261
262 sz1.*szm = half;
263 sz2.*szm = dim - half;
264 pt2.*ptm += half + 1;
265 }
266
267 bool wxSpinButton::Create( wxWindow *parent, wxWindowID id,
268 const wxPoint& pos, const wxSize& size,
269 long style, const wxString& name )
270 {
271 m_windowStyle = style;
272
273 wxSize newSize = GetBestSize();
274 if( size.x != -1 ) newSize.x = size.x;
275 if( size.y != -1 ) newSize.y = size.y;
276
277 if( !wxControl::Create( parent, id, pos, newSize, style ) )
278 {
279 return false;
280 }
281
282 SetName(name);
283
284 m_windowId = ( id == wxID_ANY ) ? NewControlId() : id;
285
286 bool isVert = IsVertical();
287 wxPoint pt1, pt2;
288 wxSize sz1, sz2;
289 CalcSizes( wxPoint(0,0), newSize, pt1, sz1, pt2, sz2, isVert );
290 m_up = new wxArrowButton( this, -1, isVert ? wxARROW_UP : wxARROW_RIGHT,
291 pt1, sz1, 1 );
292 m_down = new wxArrowButton( this, -1,
293 isVert ? wxARROW_DOWN : wxARROW_LEFT,
294 pt2, sz2, -1 );
295
296 return true;
297 }
298
299 wxSpinButton::~wxSpinButton()
300 {
301 }
302
303 void wxSpinButton::DoMoveWindow(int x, int y, int width, int height)
304 {
305 wxControl::DoMoveWindow( x, y, width, height );
306
307 wxPoint pt1, pt2;
308 wxSize sz1, sz2;
309
310 CalcSizes( wxPoint(0,0), wxSize(width,height), pt1,
311 sz1, pt2, sz2, IsVertical() );
312 m_up->SetSize( pt1.x, pt1.y, sz1.x, sz1.y );
313 m_down->SetSize( pt2.x, pt2.y, sz2.x, sz2.y );
314 }
315
316 void wxSpinButton::DoSetSize(int x, int y, int width, int height, int sizeFlags)
317 {
318 if ( (sizeFlags & wxSIZE_ALLOW_MINUS_ONE) && width == -1 )
319 width = GetSize().x;
320 if ( (sizeFlags & wxSIZE_ALLOW_MINUS_ONE) && height == -1 )
321 height = GetSize().y;
322
323 wxControl::DoSetSize(x, y, width, height, 0);
324 }
325
326 void wxSpinButton::Increment( int delta )
327 {
328 if( m_pos < m_min ) m_pos = m_min;
329 if( m_pos > m_max ) m_pos = m_max;
330
331 int npos = m_pos + delta;
332
333 if( npos < m_min )
334 {
335 if( GetWindowStyle() & wxSP_WRAP )
336 npos = m_max;
337 else
338 npos = m_min;
339 }
340 if( npos > m_max )
341 {
342 if( GetWindowStyle() & wxSP_WRAP )
343 npos = m_min;
344 else
345 npos = m_max;
346 }
347 if( npos == m_pos ) return;
348
349 wxSpinEvent event( delta > 0 ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN,
350 m_windowId );
351 event.SetPosition( npos );
352 event.SetEventObject( this );
353
354 HandleWindowEvent( event );
355
356 if( event.IsAllowed() )
357 {
358 m_pos = npos;
359 event.SetEventType( wxEVT_SCROLL_THUMBTRACK );
360 event.SetPosition( m_pos );
361
362 HandleWindowEvent( event );
363 }
364 }
365
366 wxSize wxSpinButton::DoGetBestSize() const
367 {
368 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
369 }
370
371 // Attributes
372 ////////////////////////////////////////////////////////////////////////////
373
374 int wxSpinButton::GetValue() const
375 {
376 return m_pos;
377 }
378
379 void wxSpinButton::SetValue(int val)
380 {
381 m_pos = val;
382 }
383
384 void wxSpinButton::SetRange(int minVal, int maxVal)
385 {
386 wxSpinButtonBase::SetRange(minVal, maxVal);
387 }
388
389 void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize))
390 {
391 // TODO
392 }
393
394 void wxSpinButton::ChangeBackgroundColour()
395 {
396 wxControl::ChangeBackgroundColour();
397 }
398
399 void wxSpinButton::ChangeForegroundColour()
400 {
401 // TODO
402 }
403
404 #endif // wxUSE_SPINBTN