]> git.saurik.com Git - wxWidgets.git/blob - src/motif/spinbutt.cpp
replace apple extended code
[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/defs.h"
20
21 #if wxUSE_SPINBTN
22
23 #include "wx/spinbutt.h"
24 #include "wx/spinctrl.h"
25 #include "wx/timer.h"
26
27 #ifdef __VMS__
28 #pragma message disable nosimpint
29 #endif
30 #include <Xm/ArrowBG.h>
31 #include <Xm/ArrowB.h>
32 #ifdef __VMS__
33 #pragma message enable nosimpint
34 #endif
35
36 #include "wx/motif/private.h"
37
38 // helper class
39 enum ArrowDirection
40 {
41 wxARROW_UP,
42 wxARROW_DOWN,
43 wxARROW_LEFT,
44 wxARROW_RIGHT
45 };
46
47 class wxArrowButtonTimer;
48 class wxArrowButton;
49
50 // ----------------------------------------------------------------------------
51 // wxArrowButtonTimer
52 // ----------------------------------------------------------------------------
53
54 static const unsigned int TICK_BEFORE_START = 10;
55 static const unsigned int TICK_BEFORE_EXPONENTIAL = 40;
56 static const unsigned int MAX_INCREMENT = 150;
57 static const unsigned int TICK_INTERVAL = 113;
58
59 class wxArrowButtonTimer : public wxTimer
60 {
61 public:
62 wxArrowButtonTimer( wxArrowButton* btn, int sign )
63 : m_sign( sign ),
64 m_button( btn )
65 { Reset(); };
66
67 void Notify();
68 void Reset() { m_ticks = 0; m_increment = 1; }
69 private:
70 unsigned int m_ticks;
71 unsigned int m_increment;
72 int m_sign;
73 wxArrowButton* m_button;
74 };
75
76 // ----------------------------------------------------------------------------
77 // wxArrowButton
78 // ----------------------------------------------------------------------------
79
80 class wxArrowButton : public wxControl
81 {
82 friend class wxArrowButtonTimer;
83 public:
84 wxArrowButton( int increment )
85 : m_increment( increment ),
86 m_timer( 0 ) {}
87
88 wxArrowButton( wxSpinButton* parent, wxWindowID id, ArrowDirection d,
89 const wxPoint& pos = wxDefaultPosition,
90 const wxSize& size = wxDefaultSize, int increment = 1 )
91 : wxControl(),
92 m_increment( increment ),
93 m_timer( 0 )
94 {
95 Create( parent, id, d, pos, size );
96 }
97
98 ~wxArrowButton()
99 { delete m_timer; }
100
101 bool Create( wxSpinButton* parent, wxWindowID id, ArrowDirection d,
102 const wxPoint& pos = wxDefaultPosition,
103 const wxSize& size = wxDefaultSize );
104 private:
105 // creates a new timer object, or stops the currently running one
106 wxTimer* GetFreshTimer();
107 wxSpinButton* GetSpinButton() { return (wxSpinButton*)GetParent(); }
108 static void SpinButtonCallback( Widget w, XtPointer clientData,
109 XtPointer WXUNUSED(ptr) );
110 static void StartTimerCallback( Widget w, XtPointer clientData,
111 XtPointer WXUNUSED(ptr) );
112
113 static void StopTimerCallback( Widget w, XtPointer clientData,
114 XtPointer WXUNUSED(ptr) );
115
116 int m_increment;
117 wxArrowButtonTimer* m_timer;
118 };
119
120 // ----------------------------------------------------------------------------
121 // wxArrowButtonTimer implementation
122 // ----------------------------------------------------------------------------
123
124 void wxArrowButtonTimer::Notify()
125 {
126 ++m_ticks;
127 if( m_ticks < TICK_BEFORE_START ) return;
128 // increment every other tick
129 if( m_ticks <= TICK_BEFORE_EXPONENTIAL && m_ticks & 1 )
130 return;
131 if( m_ticks > TICK_BEFORE_EXPONENTIAL )
132 m_increment = 2 * m_increment;
133 if( m_increment >= MAX_INCREMENT ) m_increment = MAX_INCREMENT;
134 m_button->GetSpinButton()->Increment( m_sign * m_increment );
135 }
136
137 // ----------------------------------------------------------------------------
138 // wxArrowButton implementation
139 // ----------------------------------------------------------------------------
140
141 wxTimer* wxArrowButton::GetFreshTimer()
142 {
143 if( m_timer )
144 {
145 m_timer->Stop();
146 m_timer->Reset();
147 }
148 else
149 m_timer = new wxArrowButtonTimer( this, m_increment );
150
151 return m_timer;
152 }
153
154 void wxArrowButton::SpinButtonCallback( Widget w, XtPointer clientData,
155 XtPointer WXUNUSED(ptr) )
156 {
157 if( !wxGetWindowFromTable( w ) )
158 // Widget has been deleted!
159 return;
160
161 wxArrowButton* btn = (wxArrowButton*)clientData;
162
163 btn->GetSpinButton()->Increment( btn->m_increment );
164 }
165
166 void wxArrowButton::StartTimerCallback( Widget w, XtPointer clientData,
167 XtPointer WXUNUSED(ptr) )
168 {
169 if( !wxGetWindowFromTable( w ) )
170 // Widget has been deleted!
171 return;
172
173 wxArrowButton* btn = (wxArrowButton*)clientData;
174 btn->GetFreshTimer()->Start( TICK_INTERVAL );
175 }
176
177 void wxArrowButton::StopTimerCallback( Widget w, XtPointer clientData,
178 XtPointer WXUNUSED(ptr) )
179 {
180 if( !wxGetWindowFromTable( w ) )
181 // Widget has been deleted!
182 return;
183
184 wxArrowButton* btn = (wxArrowButton*)clientData;
185 delete btn->m_timer;
186 btn->m_timer = 0;
187 }
188
189 bool wxArrowButton::Create( wxSpinButton* parent, wxWindowID id,
190 ArrowDirection d,
191 const wxPoint& pos, const wxSize& size )
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 if( parent ) parent->AddChild( this );
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 AttachWidget( parent, m_mainWidget, (WXWidget) NULL,
233 pos.x, pos.y, size.x, size.y );
234
235 SetForegroundColour( parent->GetBackgroundColour() );
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( wxPoint pt, 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,
317 int sizeFlags)
318 {
319 #ifdef __VMS__
320 #pragma message disable codcauunr
321 #endif
322 if( sizeFlags & wxSIZE_USE_EXISTING && width == -1 )
323 width = GetSize().x;
324 if( sizeFlags & wxSIZE_USE_EXISTING && height == -1 )
325 height = GetSize().y;
326 #ifdef __VMS__
327 #pragma message enable codcauunr
328 #endif
329
330 wxControl::DoSetSize(x, y, width, height, 0);
331 }
332
333 void wxSpinButton::Increment( int delta )
334 {
335 if( m_pos < m_min ) m_pos = m_min;
336 if( m_pos > m_max ) m_pos = m_max;
337
338 int npos = m_pos + delta;
339
340 if( npos < m_min )
341 {
342 if( GetWindowStyle() & wxSP_WRAP )
343 npos = m_max;
344 else
345 npos = m_min;
346 }
347 if( npos > m_max )
348 {
349 if( GetWindowStyle() & wxSP_WRAP )
350 npos = m_min;
351 else
352 npos = m_max;
353 }
354 if( npos == m_pos ) return;
355
356 wxSpinEvent event( delta > 0 ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN,
357 m_windowId );
358 event.SetPosition( npos );
359 event.SetEventObject( this );
360
361 GetEventHandler()->ProcessEvent( event );
362
363 if( event.IsAllowed() )
364 {
365 m_pos = npos;
366 event.SetEventType( wxEVT_SCROLL_THUMBTRACK );
367 event.SetPosition( m_pos );
368
369 GetEventHandler()->ProcessEvent( event );
370 }
371 }
372
373 wxSize wxSpinButton::DoGetBestSize() const
374 {
375 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
376 }
377
378 // Attributes
379 ////////////////////////////////////////////////////////////////////////////
380
381 int wxSpinButton::GetValue() const
382 {
383 return m_pos;
384 }
385
386 void wxSpinButton::SetValue(int val)
387 {
388 m_pos = val;
389 }
390
391 void wxSpinButton::SetRange(int minVal, int maxVal)
392 {
393 wxSpinButtonBase::SetRange(minVal, maxVal);
394 }
395
396 void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize))
397 {
398 // TODO
399 }
400
401 void wxSpinButton::ChangeBackgroundColour()
402 {
403 wxControl::ChangeBackgroundColour();
404 }
405
406 void wxSpinButton::ChangeForegroundColour()
407 {
408 // TODO
409 }
410
411 #endif // wxUSE_SPINBTN