]> git.saurik.com Git - wxWidgets.git/blame - src/motif/spinbutt.cpp
added support for OSF1 (Tru64)
[wxWidgets.git] / src / motif / spinbutt.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
355b4d3d 2// Name: src/motif/spinbutt.cpp
4bb6408c
JS
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
1248b41f
MB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
312ebad4
WS
15#include "wx/defs.h"
16
17#if wxUSE_SPINBTN
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:
312ebad4 80 wxArrowButton( int increment )
dba00620
MB
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
355b4d3d
WS
185bool wxArrowButton::Create( wxSpinButton* parent,
186 wxWindowID WXUNUSED(id),
dba00620
MB
187 ArrowDirection d,
188 const wxPoint& pos, const wxSize& size )
189{
02a48e3b 190 int arrow_dir = XmARROW_UP;
dba00620
MB
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 );
4bb6408c 209
dba00620
MB
210 Widget parentWidget = (Widget) parent->GetClientWidget();
211 m_mainWidget = (WXWidget) XtVaCreateManagedWidget( "XmArrowButton",
212 xmArrowButtonWidgetClass,
213 parentWidget,
214 XmNarrowDirection, arrow_dir,
de505a9e 215 XmNborderWidth, 0,
d8d18184 216 XmNshadowThickness, 0,
dba00620
MB
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
79fb2641
MB
232 SetForegroundColour( parent->GetBackgroundColour() );
233
312ebad4 234 return true;
dba00620
MB
235}
236
237// ----------------------------------------------------------------------------
238// wxSpinButton
239// ----------------------------------------------------------------------------
240
241IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl);
242IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent);
243
fbfb8bcc 244static void CalcSizes( const wxPoint& pt, const wxSize& sz,
dba00620
MB
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
264bool wxSpinButton::Create( wxWindow *parent, wxWindowID id,
265 const wxPoint& pos, const wxSize& size,
266 long style, const wxString& name )
267{
4bb6408c
JS
268 m_windowStyle = style;
269
dba00620
MB
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 {
312ebad4 276 return false;
dba00620
MB
277 }
278
279 SetName(name);
31528cd3 280
312ebad4 281 m_windowId = ( id == wxID_ANY ) ? NewControlId() : id;
4bb6408c 282
dba00620
MB
283 bool isVert = IsVertical();
284 wxPoint pt1, pt2;
285 wxSize sz1, sz2;
286 CalcSizes( wxPoint(0,0), newSize, pt1, sz1, pt2, sz2, isVert );
038305d0 287 m_up = new wxArrowButton( this, -1, isVert ? wxARROW_UP : wxARROW_RIGHT,
dba00620
MB
288 pt1, sz1, 1 );
289 m_down = new wxArrowButton( this, -1,
038305d0 290 isVert ? wxARROW_DOWN : wxARROW_LEFT,
dba00620
MB
291 pt2, sz2, -1 );
292
312ebad4 293 return true;
4bb6408c
JS
294}
295
296wxSpinButton::~wxSpinButton()
297{
298}
299
dba00620
MB
300void 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
313void wxSpinButton::DoSetSize(int x, int y, int width, int height,
314 int sizeFlags)
315{
b2da87c3
JJ
316#ifdef __VMS__
317#pragma message disable codcauunr
318#endif
dba00620
MB
319 if( sizeFlags & wxSIZE_USE_EXISTING && width == -1 )
320 width = GetSize().x;
321 if( sizeFlags & wxSIZE_USE_EXISTING && height == -1 )
322 height = GetSize().y;
b2da87c3
JJ
323#ifdef __VMS__
324#pragma message enable codcauunr
325#endif
dba00620
MB
326
327 wxControl::DoSetSize(x, y, width, height, 0);
328}
329
330void wxSpinButton::Increment( int delta )
331{
332 if( m_pos < m_min ) m_pos = m_min;
333 if( m_pos > m_max ) m_pos = m_max;
334
335 int npos = m_pos + delta;
336
de505a9e
MB
337 if( npos < m_min )
338 {
038305d0 339 if( GetWindowStyle() & wxSP_WRAP )
de505a9e
MB
340 npos = m_max;
341 else
342 npos = m_min;
343 }
344 if( npos > m_max )
345 {
038305d0 346 if( GetWindowStyle() & wxSP_WRAP )
de505a9e
MB
347 npos = m_min;
348 else
349 npos = m_max;
350 }
dba00620
MB
351 if( npos == m_pos ) return;
352
353 wxSpinEvent event( delta > 0 ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN,
354 m_windowId );
355 event.SetPosition( npos );
356 event.SetEventObject( this );
357
358 GetEventHandler()->ProcessEvent( event );
359
360 if( event.IsAllowed() )
361 {
362 m_pos = npos;
363 event.SetEventType( wxEVT_SCROLL_THUMBTRACK );
364 event.SetPosition( m_pos );
365
366 GetEventHandler()->ProcessEvent( event );
367 }
368}
369
370wxSize wxSpinButton::DoGetBestSize() const
371{
d8d18184 372 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
dba00620
MB
373}
374
4bb6408c
JS
375// Attributes
376////////////////////////////////////////////////////////////////////////////
377
378int wxSpinButton::GetValue() const
379{
dba00620 380 return m_pos;
4bb6408c
JS
381}
382
dba00620 383void wxSpinButton::SetValue(int val)
4bb6408c 384{
dba00620 385 m_pos = val;
4bb6408c
JS
386}
387
388void wxSpinButton::SetRange(int minVal, int maxVal)
389{
31528cd3 390 wxSpinButtonBase::SetRange(minVal, maxVal);
4bb6408c
JS
391}
392
af111fc3 393void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize))
0d57be45
JS
394{
395 // TODO
396}
397
398void wxSpinButton::ChangeBackgroundColour()
399{
dba00620 400 wxControl::ChangeBackgroundColour();
0d57be45
JS
401}
402
403void wxSpinButton::ChangeForegroundColour()
404{
405 // TODO
406}
312ebad4
WS
407
408#endif // wxUSE_SPINBTN