]> git.saurik.com Git - wxWidgets.git/blame - src/motif/spinbutt.cpp
adaptions to m_peer methods
[wxWidgets.git] / src / motif / spinbutt.cpp
CommitLineData
4bb6408c
JS
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
65571936 9// Licence: wxWindows licence
4bb6408c
JS
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
f11bdd03 13 #pragma implementation "spinbutt.h"
4bb6408c
JS
14#endif
15
1248b41f
MB
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
4bb6408c 19#include "wx/spinbutt.h"
6f18aba5 20#include "wx/spinctrl.h"
dba00620 21#include "wx/timer.h"
4bb6408c 22
dba00620
MB
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"
4bb6408c 33
dba00620
MB
34// helper class
35enum ArrowDirection
4bb6408c 36{
dba00620
MB
37 wxARROW_UP,
38 wxARROW_DOWN,
39 wxARROW_LEFT,
40 wxARROW_RIGHT
41};
42
43class wxArrowButtonTimer;
44class wxArrowButton;
45
46// ----------------------------------------------------------------------------
47// wxArrowButtonTimer
48// ----------------------------------------------------------------------------
49
50static const unsigned int TICK_BEFORE_START = 10;
51static const unsigned int TICK_BEFORE_EXPONENTIAL = 40;
52static const unsigned int MAX_INCREMENT = 150;
53static const unsigned int TICK_INTERVAL = 113;
54
55class wxArrowButtonTimer : public wxTimer
56{
57public:
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; }
65private:
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
76class wxArrowButton : public wxControl
77{
78 friend class wxArrowButtonTimer;
79public:
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 );
100private:
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
120void 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
137wxTimer* 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
150void 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
162void 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
173void 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
185bool wxArrowButton::Create( wxSpinButton* parent, wxWindowID id,
186 ArrowDirection d,
187 const wxPoint& pos, const wxSize& size )
188{
02a48e3b 189 int arrow_dir = XmARROW_UP;
dba00620
MB
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 );
4bb6408c 208
dba00620
MB
209 Widget parentWidget = (Widget) parent->GetClientWidget();
210 m_mainWidget = (WXWidget) XtVaCreateManagedWidget( "XmArrowButton",
211 xmArrowButtonWidgetClass,
212 parentWidget,
213 XmNarrowDirection, arrow_dir,
de505a9e 214 XmNborderWidth, 0,
d8d18184 215 XmNshadowThickness, 0,
dba00620
MB
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
79fb2641
MB
231 SetForegroundColour( parent->GetBackgroundColour() );
232
dba00620
MB
233 return TRUE;
234}
235
236// ----------------------------------------------------------------------------
237// wxSpinButton
238// ----------------------------------------------------------------------------
239
240IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl);
241IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent);
242
243static 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
263bool wxSpinButton::Create( wxWindow *parent, wxWindowID id,
264 const wxPoint& pos, const wxSize& size,
265 long style, const wxString& name )
266{
4bb6408c
JS
267 m_windowStyle = style;
268
dba00620
MB
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);
31528cd3 279
dba00620 280 m_windowId = ( id == -1 ) ? NewControlId() : id;
4bb6408c 281
dba00620
MB
282 bool isVert = IsVertical();
283 wxPoint pt1, pt2;
284 wxSize sz1, sz2;
285 CalcSizes( wxPoint(0,0), newSize, pt1, sz1, pt2, sz2, isVert );
038305d0 286 m_up = new wxArrowButton( this, -1, isVert ? wxARROW_UP : wxARROW_RIGHT,
dba00620
MB
287 pt1, sz1, 1 );
288 m_down = new wxArrowButton( this, -1,
038305d0 289 isVert ? wxARROW_DOWN : wxARROW_LEFT,
dba00620
MB
290 pt2, sz2, -1 );
291
292 return TRUE;
4bb6408c
JS
293}
294
295wxSpinButton::~wxSpinButton()
296{
297}
298
dba00620
MB
299void 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
312void wxSpinButton::DoSetSize(int x, int y, int width, int height,
313 int sizeFlags)
314{
b2da87c3
JJ
315#ifdef __VMS__
316#pragma message disable codcauunr
317#endif
dba00620
MB
318 if( sizeFlags & wxSIZE_USE_EXISTING && width == -1 )
319 width = GetSize().x;
320 if( sizeFlags & wxSIZE_USE_EXISTING && height == -1 )
321 height = GetSize().y;
b2da87c3
JJ
322#ifdef __VMS__
323#pragma message enable codcauunr
324#endif
dba00620
MB
325
326 wxControl::DoSetSize(x, y, width, height, 0);
327}
328
329void 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
de505a9e
MB
336 if( npos < m_min )
337 {
038305d0 338 if( GetWindowStyle() & wxSP_WRAP )
de505a9e
MB
339 npos = m_max;
340 else
341 npos = m_min;
342 }
343 if( npos > m_max )
344 {
038305d0 345 if( GetWindowStyle() & wxSP_WRAP )
de505a9e
MB
346 npos = m_min;
347 else
348 npos = m_max;
349 }
dba00620
MB
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
369wxSize wxSpinButton::DoGetBestSize() const
370{
d8d18184 371 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
dba00620
MB
372}
373
4bb6408c
JS
374// Attributes
375////////////////////////////////////////////////////////////////////////////
376
377int wxSpinButton::GetValue() const
378{
dba00620 379 return m_pos;
4bb6408c
JS
380}
381
dba00620 382void wxSpinButton::SetValue(int val)
4bb6408c 383{
dba00620 384 m_pos = val;
4bb6408c
JS
385}
386
387void wxSpinButton::SetRange(int minVal, int maxVal)
388{
31528cd3 389 wxSpinButtonBase::SetRange(minVal, maxVal);
4bb6408c
JS
390}
391
af111fc3 392void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize))
0d57be45
JS
393{
394 // TODO
395}
396
397void wxSpinButton::ChangeBackgroundColour()
398{
dba00620 399 wxControl::ChangeBackgroundColour();
0d57be45
JS
400}
401
402void wxSpinButton::ChangeForegroundColour()
403{
404 // TODO
405}