]> git.saurik.com Git - wxWidgets.git/blob - src/motif/spinbutt.cpp
Inital fill in background, removed tabs, -1->wxID_ANY, TRUE->true, FALSE->false
[wxWidgets.git] / src / motif / spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "spinbutt.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
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, wxWindowID id,
186 ArrowDirection d,
187 const wxPoint& pos, const wxSize& size )
188 {
189 int arrow_dir = XmARROW_UP;
190
191 switch( d )
192 {
193 case wxARROW_UP:
194 arrow_dir = XmARROW_UP;
195 break;
196 case wxARROW_DOWN:
197 arrow_dir = XmARROW_DOWN;
198 break;
199 case wxARROW_LEFT:
200 arrow_dir = XmARROW_LEFT;
201 break;
202 case wxARROW_RIGHT:
203 arrow_dir = XmARROW_RIGHT;
204 break;
205 }
206
207 if( parent ) parent->AddChild( this );
208
209 Widget parentWidget = (Widget) parent->GetClientWidget();
210 m_mainWidget = (WXWidget) XtVaCreateManagedWidget( "XmArrowButton",
211 xmArrowButtonWidgetClass,
212 parentWidget,
213 XmNarrowDirection, arrow_dir,
214 XmNborderWidth, 0,
215 XmNshadowThickness, 0,
216 NULL );
217
218 XtAddCallback( (Widget) m_mainWidget,
219 XmNactivateCallback, (XtCallbackProc) SpinButtonCallback,
220 (XtPointer) this );
221 XtAddCallback( (Widget) m_mainWidget,
222 XmNarmCallback, (XtCallbackProc) StartTimerCallback,
223 (XtPointer) this );
224 XtAddCallback( (Widget) m_mainWidget,
225 XmNactivateCallback, (XtCallbackProc) StopTimerCallback,
226 (XtPointer) this );
227
228 AttachWidget( parent, m_mainWidget, (WXWidget) NULL,
229 pos.x, pos.y, size.x, size.y );
230
231 SetForegroundColour( parent->GetBackgroundColour() );
232
233 return TRUE;
234 }
235
236 // ----------------------------------------------------------------------------
237 // wxSpinButton
238 // ----------------------------------------------------------------------------
239
240 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl);
241 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent);
242
243 static void CalcSizes( wxPoint pt, 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 == -1 ) ? 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,
313 int sizeFlags)
314 {
315 #ifdef __VMS__
316 #pragma message disable codcauunr
317 #endif
318 if( sizeFlags & wxSIZE_USE_EXISTING && width == -1 )
319 width = GetSize().x;
320 if( sizeFlags & wxSIZE_USE_EXISTING && height == -1 )
321 height = GetSize().y;
322 #ifdef __VMS__
323 #pragma message enable codcauunr
324 #endif
325
326 wxControl::DoSetSize(x, y, width, height, 0);
327 }
328
329 void wxSpinButton::Increment( int delta )
330 {
331 if( m_pos < m_min ) m_pos = m_min;
332 if( m_pos > m_max ) m_pos = m_max;
333
334 int npos = m_pos + delta;
335
336 if( npos < m_min )
337 {
338 if( GetWindowStyle() & wxSP_WRAP )
339 npos = m_max;
340 else
341 npos = m_min;
342 }
343 if( npos > m_max )
344 {
345 if( GetWindowStyle() & wxSP_WRAP )
346 npos = m_min;
347 else
348 npos = m_max;
349 }
350 if( npos == m_pos ) return;
351
352 wxSpinEvent event( delta > 0 ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN,
353 m_windowId );
354 event.SetPosition( npos );
355 event.SetEventObject( this );
356
357 GetEventHandler()->ProcessEvent( event );
358
359 if( event.IsAllowed() )
360 {
361 m_pos = npos;
362 event.SetEventType( wxEVT_SCROLL_THUMBTRACK );
363 event.SetPosition( m_pos );
364
365 GetEventHandler()->ProcessEvent( event );
366 }
367 }
368
369 wxSize wxSpinButton::DoGetBestSize() const
370 {
371 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
372 }
373
374 // Attributes
375 ////////////////////////////////////////////////////////////////////////////
376
377 int wxSpinButton::GetValue() const
378 {
379 return m_pos;
380 }
381
382 void wxSpinButton::SetValue(int val)
383 {
384 m_pos = val;
385 }
386
387 void wxSpinButton::SetRange(int minVal, int maxVal)
388 {
389 wxSpinButtonBase::SetRange(minVal, maxVal);
390 }
391
392 void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize))
393 {
394 // TODO
395 }
396
397 void wxSpinButton::ChangeBackgroundColour()
398 {
399 wxControl::ChangeBackgroundColour();
400 }
401
402 void wxSpinButton::ChangeForegroundColour()
403 {
404 // TODO
405 }