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