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