]> git.saurik.com Git - wxWidgets.git/blame - src/motif/spinbutt.cpp
Fix horizontal mouse wheel scrolling in wxGTK.
[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
4bb6408c 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
4bb6408c
JS
9/////////////////////////////////////////////////////////////////////////////
10
1248b41f
MB
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
312ebad4
WS
14#if wxUSE_SPINBTN
15
4bb6408c 16#include "wx/spinbutt.h"
c0badb70
WS
17
18#ifndef WX_PRECOMP
19 #include "wx/timer.h"
20#endif
21
6f18aba5 22#include "wx/spinctrl.h"
4bb6408c 23
dba00620
MB
24#ifdef __VMS__
25#pragma message disable nosimpint
26#endif
27#include <Xm/ArrowBG.h>
28#include <Xm/ArrowB.h>
29#ifdef __VMS__
30#pragma message enable nosimpint
31#endif
32
33#include "wx/motif/private.h"
4bb6408c 34
dba00620
MB
35// helper class
36enum ArrowDirection
4bb6408c 37{
dba00620
MB
38 wxARROW_UP,
39 wxARROW_DOWN,
40 wxARROW_LEFT,
41 wxARROW_RIGHT
42};
43
44class wxArrowButtonTimer;
45class wxArrowButton;
46
47// ----------------------------------------------------------------------------
48// wxArrowButtonTimer
49// ----------------------------------------------------------------------------
50
51static const unsigned int TICK_BEFORE_START = 10;
52static const unsigned int TICK_BEFORE_EXPONENTIAL = 40;
53static const unsigned int MAX_INCREMENT = 150;
54static const unsigned int TICK_INTERVAL = 113;
55
56class wxArrowButtonTimer : public wxTimer
57{
58public:
59 wxArrowButtonTimer( wxArrowButton* btn, int sign )
60 : m_sign( sign ),
61 m_button( btn )
62 { Reset(); };
63
64 void Notify();
65 void Reset() { m_ticks = 0; m_increment = 1; }
66private:
67 unsigned int m_ticks;
68 unsigned int m_increment;
69 int m_sign;
70 wxArrowButton* m_button;
71};
72
73// ----------------------------------------------------------------------------
74// wxArrowButton
75// ----------------------------------------------------------------------------
76
77class wxArrowButton : public wxControl
78{
79 friend class wxArrowButtonTimer;
80public:
312ebad4 81 wxArrowButton( int increment )
dba00620
MB
82 : m_increment( increment ),
83 m_timer( 0 ) {}
84
85 wxArrowButton( wxSpinButton* parent, wxWindowID id, ArrowDirection d,
86 const wxPoint& pos = wxDefaultPosition,
87 const wxSize& size = wxDefaultSize, int increment = 1 )
88 : wxControl(),
89 m_increment( increment ),
90 m_timer( 0 )
91 {
92 Create( parent, id, d, pos, size );
93 }
94
d3c7fc99 95 virtual ~wxArrowButton()
dba00620
MB
96 { delete m_timer; }
97
98 bool Create( wxSpinButton* parent, wxWindowID id, ArrowDirection d,
99 const wxPoint& pos = wxDefaultPosition,
100 const wxSize& size = wxDefaultSize );
101private:
102 // creates a new timer object, or stops the currently running one
103 wxTimer* GetFreshTimer();
104 wxSpinButton* GetSpinButton() { return (wxSpinButton*)GetParent(); }
105 static void SpinButtonCallback( Widget w, XtPointer clientData,
106 XtPointer WXUNUSED(ptr) );
107 static void StartTimerCallback( Widget w, XtPointer clientData,
108 XtPointer WXUNUSED(ptr) );
109
110 static void StopTimerCallback( Widget w, XtPointer clientData,
111 XtPointer WXUNUSED(ptr) );
112
113 int m_increment;
114 wxArrowButtonTimer* m_timer;
115};
116
117// ----------------------------------------------------------------------------
118// wxArrowButtonTimer implementation
119// ----------------------------------------------------------------------------
120
121void wxArrowButtonTimer::Notify()
122{
123 ++m_ticks;
124 if( m_ticks < TICK_BEFORE_START ) return;
125 // increment every other tick
126 if( m_ticks <= TICK_BEFORE_EXPONENTIAL && m_ticks & 1 )
127 return;
128 if( m_ticks > TICK_BEFORE_EXPONENTIAL )
129 m_increment = 2 * m_increment;
130 if( m_increment >= MAX_INCREMENT ) m_increment = MAX_INCREMENT;
131 m_button->GetSpinButton()->Increment( m_sign * m_increment );
132}
133
134// ----------------------------------------------------------------------------
135// wxArrowButton implementation
136// ----------------------------------------------------------------------------
137
138wxTimer* wxArrowButton::GetFreshTimer()
139{
140 if( m_timer )
141 {
142 m_timer->Stop();
143 m_timer->Reset();
144 }
145 else
146 m_timer = new wxArrowButtonTimer( this, m_increment );
147
148 return m_timer;
149}
150
151void wxArrowButton::SpinButtonCallback( Widget w, XtPointer clientData,
152 XtPointer WXUNUSED(ptr) )
153{
154 if( !wxGetWindowFromTable( w ) )
155 // Widget has been deleted!
156 return;
157
158 wxArrowButton* btn = (wxArrowButton*)clientData;
159
160 btn->GetSpinButton()->Increment( btn->m_increment );
161}
162
163void wxArrowButton::StartTimerCallback( Widget w, XtPointer clientData,
164 XtPointer WXUNUSED(ptr) )
165{
166 if( !wxGetWindowFromTable( w ) )
167 // Widget has been deleted!
168 return;
169
170 wxArrowButton* btn = (wxArrowButton*)clientData;
171 btn->GetFreshTimer()->Start( TICK_INTERVAL );
172}
173
174void wxArrowButton::StopTimerCallback( Widget w, XtPointer clientData,
175 XtPointer WXUNUSED(ptr) )
176{
177 if( !wxGetWindowFromTable( w ) )
178 // Widget has been deleted!
179 return;
180
181 wxArrowButton* btn = (wxArrowButton*)clientData;
5276b0a5 182 wxDELETE(btn->m_timer);
dba00620
MB
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{
9a83f860 190 wxCHECK_MSG( parent, false, wxT("must have a valid parent") );
fe03e628 191
02a48e3b 192 int arrow_dir = XmARROW_UP;
dba00620
MB
193
194 switch( d )
195 {
196 case wxARROW_UP:
197 arrow_dir = XmARROW_UP;
198 break;
199 case wxARROW_DOWN:
200 arrow_dir = XmARROW_DOWN;
201 break;
202 case wxARROW_LEFT:
203 arrow_dir = XmARROW_LEFT;
204 break;
205 case wxARROW_RIGHT:
206 arrow_dir = XmARROW_RIGHT;
207 break;
208 }
209
fe03e628 210 parent->AddChild( this );
105fbe1f 211 PreCreation();
4bb6408c 212
dba00620
MB
213 Widget parentWidget = (Widget) parent->GetClientWidget();
214 m_mainWidget = (WXWidget) XtVaCreateManagedWidget( "XmArrowButton",
215 xmArrowButtonWidgetClass,
216 parentWidget,
217 XmNarrowDirection, arrow_dir,
de505a9e 218 XmNborderWidth, 0,
d8d18184 219 XmNshadowThickness, 0,
dba00620
MB
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
105fbe1f 232 PostCreation();
dba00620
MB
233 AttachWidget( parent, m_mainWidget, (WXWidget) NULL,
234 pos.x, pos.y, size.x, size.y );
235
312ebad4 236 return true;
dba00620
MB
237}
238
239// ----------------------------------------------------------------------------
240// wxSpinButton
241// ----------------------------------------------------------------------------
242
fbfb8bcc 243static void CalcSizes( const wxPoint& pt, const wxSize& sz,
dba00620
MB
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 {
312ebad4 275 return false;
dba00620
MB
276 }
277
278 SetName(name);
31528cd3 279
312ebad4 280 m_windowId = ( id == wxID_ANY ) ? 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
312ebad4 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
541c319a 312void wxSpinButton::DoSetSize(int x, int y, int width, int height, int sizeFlags)
dba00620 313{
541c319a 314 if ( (sizeFlags & wxSIZE_ALLOW_MINUS_ONE) && width == -1 )
dba00620 315 width = GetSize().x;
541c319a 316 if ( (sizeFlags & wxSIZE_ALLOW_MINUS_ONE) && height == -1 )
dba00620
MB
317 height = GetSize().y;
318
319 wxControl::DoSetSize(x, y, width, height, 0);
320}
321
322void wxSpinButton::Increment( int delta )
323{
324 if( m_pos < m_min ) m_pos = m_min;
325 if( m_pos > m_max ) m_pos = m_max;
326
327 int npos = m_pos + delta;
328
de505a9e
MB
329 if( npos < m_min )
330 {
038305d0 331 if( GetWindowStyle() & wxSP_WRAP )
de505a9e
MB
332 npos = m_max;
333 else
334 npos = m_min;
335 }
336 if( npos > m_max )
337 {
038305d0 338 if( GetWindowStyle() & wxSP_WRAP )
de505a9e
MB
339 npos = m_min;
340 else
341 npos = m_max;
342 }
dba00620
MB
343 if( npos == m_pos ) return;
344
345 wxSpinEvent event( delta > 0 ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN,
346 m_windowId );
347 event.SetPosition( npos );
348 event.SetEventObject( this );
349
937013e0 350 HandleWindowEvent( event );
dba00620
MB
351
352 if( event.IsAllowed() )
353 {
354 m_pos = npos;
355 event.SetEventType( wxEVT_SCROLL_THUMBTRACK );
356 event.SetPosition( m_pos );
357
937013e0 358 HandleWindowEvent( event );
dba00620
MB
359 }
360}
361
362wxSize wxSpinButton::DoGetBestSize() const
363{
d8d18184 364 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
dba00620
MB
365}
366
4bb6408c
JS
367// Attributes
368////////////////////////////////////////////////////////////////////////////
369
370int wxSpinButton::GetValue() const
371{
dba00620 372 return m_pos;
4bb6408c
JS
373}
374
dba00620 375void wxSpinButton::SetValue(int val)
4bb6408c 376{
dba00620 377 m_pos = val;
4bb6408c
JS
378}
379
380void wxSpinButton::SetRange(int minVal, int maxVal)
381{
31528cd3 382 wxSpinButtonBase::SetRange(minVal, maxVal);
4bb6408c
JS
383}
384
af111fc3 385void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize))
0d57be45
JS
386{
387 // TODO
388}
389
390void wxSpinButton::ChangeBackgroundColour()
391{
dba00620 392 wxControl::ChangeBackgroundColour();
0d57be45
JS
393}
394
395void wxSpinButton::ChangeForegroundColour()
396{
397 // TODO
398}
312ebad4
WS
399
400#endif // wxUSE_SPINBTN