]> git.saurik.com Git - wxWidgets.git/blame - src/motif/spinbutt.cpp
Patch [ 1100272 ] wxFlexGridSizer works wrong when some rows/cols are hidden
[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
312ebad4
WS
19#include "wx/defs.h"
20
21#if wxUSE_SPINBTN
22
4bb6408c 23#include "wx/spinbutt.h"
6f18aba5 24#include "wx/spinctrl.h"
dba00620 25#include "wx/timer.h"
4bb6408c 26
dba00620
MB
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"
4bb6408c 37
dba00620
MB
38// helper class
39enum ArrowDirection
4bb6408c 40{
dba00620
MB
41 wxARROW_UP,
42 wxARROW_DOWN,
43 wxARROW_LEFT,
44 wxARROW_RIGHT
45};
46
47class wxArrowButtonTimer;
48class wxArrowButton;
49
50// ----------------------------------------------------------------------------
51// wxArrowButtonTimer
52// ----------------------------------------------------------------------------
53
54static const unsigned int TICK_BEFORE_START = 10;
55static const unsigned int TICK_BEFORE_EXPONENTIAL = 40;
56static const unsigned int MAX_INCREMENT = 150;
57static const unsigned int TICK_INTERVAL = 113;
58
59class wxArrowButtonTimer : public wxTimer
60{
61public:
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; }
69private:
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
80class wxArrowButton : public wxControl
81{
82 friend class wxArrowButtonTimer;
83public:
312ebad4 84 wxArrowButton( int increment )
dba00620
MB
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 );
104private:
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
124void 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
141wxTimer* 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
154void 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
166void 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
177void 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
189bool wxArrowButton::Create( wxSpinButton* parent, wxWindowID id,
190 ArrowDirection d,
191 const wxPoint& pos, const wxSize& size )
192{
02a48e3b 193 int arrow_dir = XmARROW_UP;
dba00620
MB
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 );
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
232 AttachWidget( parent, m_mainWidget, (WXWidget) NULL,
233 pos.x, pos.y, size.x, size.y );
234
79fb2641
MB
235 SetForegroundColour( parent->GetBackgroundColour() );
236
312ebad4 237 return true;
dba00620
MB
238}
239
240// ----------------------------------------------------------------------------
241// wxSpinButton
242// ----------------------------------------------------------------------------
243
244IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl);
245IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent);
246
247static 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
267bool wxSpinButton::Create( wxWindow *parent, wxWindowID id,
268 const wxPoint& pos, const wxSize& size,
269 long style, const wxString& name )
270{
4bb6408c
JS
271 m_windowStyle = style;
272
dba00620
MB
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 {
312ebad4 279 return false;
dba00620
MB
280 }
281
282 SetName(name);
31528cd3 283
312ebad4 284 m_windowId = ( id == wxID_ANY ) ? NewControlId() : id;
4bb6408c 285
dba00620
MB
286 bool isVert = IsVertical();
287 wxPoint pt1, pt2;
288 wxSize sz1, sz2;
289 CalcSizes( wxPoint(0,0), newSize, pt1, sz1, pt2, sz2, isVert );
038305d0 290 m_up = new wxArrowButton( this, -1, isVert ? wxARROW_UP : wxARROW_RIGHT,
dba00620
MB
291 pt1, sz1, 1 );
292 m_down = new wxArrowButton( this, -1,
038305d0 293 isVert ? wxARROW_DOWN : wxARROW_LEFT,
dba00620
MB
294 pt2, sz2, -1 );
295
312ebad4 296 return true;
4bb6408c
JS
297}
298
299wxSpinButton::~wxSpinButton()
300{
301}
302
dba00620
MB
303void 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
316void wxSpinButton::DoSetSize(int x, int y, int width, int height,
317 int sizeFlags)
318{
b2da87c3
JJ
319#ifdef __VMS__
320#pragma message disable codcauunr
321#endif
dba00620
MB
322 if( sizeFlags & wxSIZE_USE_EXISTING && width == -1 )
323 width = GetSize().x;
324 if( sizeFlags & wxSIZE_USE_EXISTING && height == -1 )
325 height = GetSize().y;
b2da87c3
JJ
326#ifdef __VMS__
327#pragma message enable codcauunr
328#endif
dba00620
MB
329
330 wxControl::DoSetSize(x, y, width, height, 0);
331}
332
333void 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
de505a9e
MB
340 if( npos < m_min )
341 {
038305d0 342 if( GetWindowStyle() & wxSP_WRAP )
de505a9e
MB
343 npos = m_max;
344 else
345 npos = m_min;
346 }
347 if( npos > m_max )
348 {
038305d0 349 if( GetWindowStyle() & wxSP_WRAP )
de505a9e
MB
350 npos = m_min;
351 else
352 npos = m_max;
353 }
dba00620
MB
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
373wxSize wxSpinButton::DoGetBestSize() const
374{
d8d18184 375 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
dba00620
MB
376}
377
4bb6408c
JS
378// Attributes
379////////////////////////////////////////////////////////////////////////////
380
381int wxSpinButton::GetValue() const
382{
dba00620 383 return m_pos;
4bb6408c
JS
384}
385
dba00620 386void wxSpinButton::SetValue(int val)
4bb6408c 387{
dba00620 388 m_pos = val;
4bb6408c
JS
389}
390
391void wxSpinButton::SetRange(int minVal, int maxVal)
392{
31528cd3 393 wxSpinButtonBase::SetRange(minVal, maxVal);
4bb6408c
JS
394}
395
af111fc3 396void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize))
0d57be45
JS
397{
398 // TODO
399}
400
401void wxSpinButton::ChangeBackgroundColour()
402{
dba00620 403 wxControl::ChangeBackgroundColour();
0d57be45
JS
404}
405
406void wxSpinButton::ChangeForegroundColour()
407{
408 // TODO
409}
312ebad4
WS
410
411#endif // wxUSE_SPINBTN