]> git.saurik.com Git - wxWidgets.git/blame - src/motif/spinbutt.cpp
preselect the text in the dialog to allow replacing it easier (just as in the text...
[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
31528cd3 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
16#include "wx/spinbutt.h"
6f18aba5 17#include "wx/spinctrl.h"
dba00620 18#include "wx/timer.h"
4bb6408c 19
dba00620
MB
20#ifdef __VMS__
21#pragma message disable nosimpint
22#endif
23#include <Xm/ArrowBG.h>
24#include <Xm/ArrowB.h>
25#ifdef __VMS__
26#pragma message enable nosimpint
27#endif
28
29#include "wx/motif/private.h"
4bb6408c 30
dba00620
MB
31// helper class
32enum ArrowDirection
4bb6408c 33{
dba00620
MB
34 wxARROW_UP,
35 wxARROW_DOWN,
36 wxARROW_LEFT,
37 wxARROW_RIGHT
38};
39
40class wxArrowButtonTimer;
41class wxArrowButton;
42
43// ----------------------------------------------------------------------------
44// wxArrowButtonTimer
45// ----------------------------------------------------------------------------
46
47static const unsigned int TICK_BEFORE_START = 10;
48static const unsigned int TICK_BEFORE_EXPONENTIAL = 40;
49static const unsigned int MAX_INCREMENT = 150;
50static const unsigned int TICK_INTERVAL = 113;
51
52class wxArrowButtonTimer : public wxTimer
53{
54public:
55 wxArrowButtonTimer( wxArrowButton* btn, int sign )
56 : m_sign( sign ),
57 m_button( btn )
58 { Reset(); };
59
60 void Notify();
61 void Reset() { m_ticks = 0; m_increment = 1; }
62private:
63 unsigned int m_ticks;
64 unsigned int m_increment;
65 int m_sign;
66 wxArrowButton* m_button;
67};
68
69// ----------------------------------------------------------------------------
70// wxArrowButton
71// ----------------------------------------------------------------------------
72
73class wxArrowButton : public wxControl
74{
75 friend class wxArrowButtonTimer;
76public:
77 wxArrowButton( int increment )
78 : m_increment( increment ),
79 m_timer( 0 ) {}
80
81 wxArrowButton( wxSpinButton* parent, wxWindowID id, ArrowDirection d,
82 const wxPoint& pos = wxDefaultPosition,
83 const wxSize& size = wxDefaultSize, int increment = 1 )
84 : wxControl(),
85 m_increment( increment ),
86 m_timer( 0 )
87 {
88 Create( parent, id, d, pos, size );
89 }
90
91 ~wxArrowButton()
92 { delete m_timer; }
93
94 bool Create( wxSpinButton* parent, wxWindowID id, ArrowDirection d,
95 const wxPoint& pos = wxDefaultPosition,
96 const wxSize& size = wxDefaultSize );
97private:
98 // creates a new timer object, or stops the currently running one
99 wxTimer* GetFreshTimer();
100 wxSpinButton* GetSpinButton() { return (wxSpinButton*)GetParent(); }
101 static void SpinButtonCallback( Widget w, XtPointer clientData,
102 XtPointer WXUNUSED(ptr) );
103 static void StartTimerCallback( Widget w, XtPointer clientData,
104 XtPointer WXUNUSED(ptr) );
105
106 static void StopTimerCallback( Widget w, XtPointer clientData,
107 XtPointer WXUNUSED(ptr) );
108
109 int m_increment;
110 wxArrowButtonTimer* m_timer;
111};
112
113// ----------------------------------------------------------------------------
114// wxArrowButtonTimer implementation
115// ----------------------------------------------------------------------------
116
117void wxArrowButtonTimer::Notify()
118{
119 ++m_ticks;
120 if( m_ticks < TICK_BEFORE_START ) return;
121 // increment every other tick
122 if( m_ticks <= TICK_BEFORE_EXPONENTIAL && m_ticks & 1 )
123 return;
124 if( m_ticks > TICK_BEFORE_EXPONENTIAL )
125 m_increment = 2 * m_increment;
126 if( m_increment >= MAX_INCREMENT ) m_increment = MAX_INCREMENT;
127 m_button->GetSpinButton()->Increment( m_sign * m_increment );
128}
129
130// ----------------------------------------------------------------------------
131// wxArrowButton implementation
132// ----------------------------------------------------------------------------
133
134wxTimer* wxArrowButton::GetFreshTimer()
135{
136 if( m_timer )
137 {
138 m_timer->Stop();
139 m_timer->Reset();
140 }
141 else
142 m_timer = new wxArrowButtonTimer( this, m_increment );
143
144 return m_timer;
145}
146
147void wxArrowButton::SpinButtonCallback( Widget w, XtPointer clientData,
148 XtPointer WXUNUSED(ptr) )
149{
150 if( !wxGetWindowFromTable( w ) )
151 // Widget has been deleted!
152 return;
153
154 wxArrowButton* btn = (wxArrowButton*)clientData;
155
156 btn->GetSpinButton()->Increment( btn->m_increment );
157}
158
159void wxArrowButton::StartTimerCallback( Widget w, XtPointer clientData,
160 XtPointer WXUNUSED(ptr) )
161{
162 if( !wxGetWindowFromTable( w ) )
163 // Widget has been deleted!
164 return;
165
166 wxArrowButton* btn = (wxArrowButton*)clientData;
167 btn->GetFreshTimer()->Start( TICK_INTERVAL );
168}
169
170void wxArrowButton::StopTimerCallback( Widget w, XtPointer clientData,
171 XtPointer WXUNUSED(ptr) )
172{
173 if( !wxGetWindowFromTable( w ) )
174 // Widget has been deleted!
175 return;
176
177 wxArrowButton* btn = (wxArrowButton*)clientData;
178 delete btn->m_timer;
179 btn->m_timer = 0;
180}
181
182bool wxArrowButton::Create( wxSpinButton* parent, wxWindowID id,
183 ArrowDirection d,
184 const wxPoint& pos, const wxSize& size )
185{
02a48e3b 186 int arrow_dir = XmARROW_UP;
dba00620
MB
187
188 switch( d )
189 {
190 case wxARROW_UP:
191 arrow_dir = XmARROW_UP;
192 break;
193 case wxARROW_DOWN:
194 arrow_dir = XmARROW_DOWN;
195 break;
196 case wxARROW_LEFT:
197 arrow_dir = XmARROW_LEFT;
198 break;
199 case wxARROW_RIGHT:
200 arrow_dir = XmARROW_RIGHT;
201 break;
202 }
203
204 if( parent ) parent->AddChild( this );
4bb6408c 205
dba00620
MB
206 Widget parentWidget = (Widget) parent->GetClientWidget();
207 m_mainWidget = (WXWidget) XtVaCreateManagedWidget( "XmArrowButton",
208 xmArrowButtonWidgetClass,
209 parentWidget,
210 XmNarrowDirection, arrow_dir,
de505a9e 211 XmNborderWidth, 0,
d8d18184 212 XmNshadowThickness, 0,
dba00620
MB
213 NULL );
214
215 XtAddCallback( (Widget) m_mainWidget,
216 XmNactivateCallback, (XtCallbackProc) SpinButtonCallback,
217 (XtPointer) this );
218 XtAddCallback( (Widget) m_mainWidget,
219 XmNarmCallback, (XtCallbackProc) StartTimerCallback,
220 (XtPointer) this );
221 XtAddCallback( (Widget) m_mainWidget,
222 XmNactivateCallback, (XtCallbackProc) StopTimerCallback,
223 (XtPointer) this );
224
225 AttachWidget( parent, m_mainWidget, (WXWidget) NULL,
226 pos.x, pos.y, size.x, size.y );
227
79fb2641
MB
228 SetForegroundColour( parent->GetBackgroundColour() );
229
dba00620
MB
230 return TRUE;
231}
232
233// ----------------------------------------------------------------------------
234// wxSpinButton
235// ----------------------------------------------------------------------------
236
237IMPLEMENT_DYNAMIC_CLASS(wxSpinButton, wxControl);
238IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent);
239
240static void CalcSizes( wxPoint pt, wxSize sz,
241 wxPoint& pt1, wxSize& sz1,
242 wxPoint& pt2, wxSize& sz2,
243 bool isVertical )
244{
245 typedef int wxSize::* CDPTR1;
246 typedef int wxPoint::* CDPTR2;
247
248 sz1 = sz2 = sz;
249 pt2 = pt1 = pt;
250
251 CDPTR1 szm = isVertical ? &wxSize::y : &wxSize::x;
252 CDPTR2 ptm = isVertical ? &wxPoint::y : &wxPoint::x;
253 int dim = sz.*szm, half = dim/2;
254
255 sz1.*szm = half;
256 sz2.*szm = dim - half;
257 pt2.*ptm += half + 1;
258}
259
260bool wxSpinButton::Create( wxWindow *parent, wxWindowID id,
261 const wxPoint& pos, const wxSize& size,
262 long style, const wxString& name )
263{
4bb6408c
JS
264 m_windowStyle = style;
265
dba00620
MB
266 wxSize newSize = GetBestSize();
267 if( size.x != -1 ) newSize.x = size.x;
268 if( size.y != -1 ) newSize.y = size.y;
269
270 if( !wxControl::Create( parent, id, pos, newSize, style ) )
271 {
272 return FALSE;
273 }
274
275 SetName(name);
31528cd3 276
dba00620 277 m_windowId = ( id == -1 ) ? NewControlId() : id;
4bb6408c 278
dba00620
MB
279 bool isVert = IsVertical();
280 wxPoint pt1, pt2;
281 wxSize sz1, sz2;
282 CalcSizes( wxPoint(0,0), newSize, pt1, sz1, pt2, sz2, isVert );
038305d0 283 m_up = new wxArrowButton( this, -1, isVert ? wxARROW_UP : wxARROW_RIGHT,
dba00620
MB
284 pt1, sz1, 1 );
285 m_down = new wxArrowButton( this, -1,
038305d0 286 isVert ? wxARROW_DOWN : wxARROW_LEFT,
dba00620
MB
287 pt2, sz2, -1 );
288
289 return TRUE;
4bb6408c
JS
290}
291
292wxSpinButton::~wxSpinButton()
293{
294}
295
dba00620
MB
296void wxSpinButton::DoMoveWindow(int x, int y, int width, int height)
297{
298 wxControl::DoMoveWindow( x, y, width, height );
299
300 wxPoint pt1, pt2;
301 wxSize sz1, sz2;
302
303 CalcSizes( wxPoint(0,0), wxSize(width,height), pt1,
304 sz1, pt2, sz2, IsVertical() );
305 m_up->SetSize( pt1.x, pt1.y, sz1.x, sz1.y );
306 m_down->SetSize( pt2.x, pt2.y, sz2.x, sz2.y );
307}
308
309void wxSpinButton::DoSetSize(int x, int y, int width, int height,
310 int sizeFlags)
311{
b2da87c3
JJ
312#ifdef __VMS__
313#pragma message disable codcauunr
314#endif
dba00620
MB
315 if( sizeFlags & wxSIZE_USE_EXISTING && width == -1 )
316 width = GetSize().x;
317 if( sizeFlags & wxSIZE_USE_EXISTING && height == -1 )
318 height = GetSize().y;
b2da87c3
JJ
319#ifdef __VMS__
320#pragma message enable codcauunr
321#endif
dba00620
MB
322
323 wxControl::DoSetSize(x, y, width, height, 0);
324}
325
326void wxSpinButton::Increment( int delta )
327{
328 if( m_pos < m_min ) m_pos = m_min;
329 if( m_pos > m_max ) m_pos = m_max;
330
331 int npos = m_pos + delta;
332
de505a9e
MB
333 if( npos < m_min )
334 {
038305d0 335 if( GetWindowStyle() & wxSP_WRAP )
de505a9e
MB
336 npos = m_max;
337 else
338 npos = m_min;
339 }
340 if( npos > m_max )
341 {
038305d0 342 if( GetWindowStyle() & wxSP_WRAP )
de505a9e
MB
343 npos = m_min;
344 else
345 npos = m_max;
346 }
dba00620
MB
347 if( npos == m_pos ) return;
348
349 wxSpinEvent event( delta > 0 ? wxEVT_SCROLL_LINEUP : wxEVT_SCROLL_LINEDOWN,
350 m_windowId );
351 event.SetPosition( npos );
352 event.SetEventObject( this );
353
354 GetEventHandler()->ProcessEvent( event );
355
356 if( event.IsAllowed() )
357 {
358 m_pos = npos;
359 event.SetEventType( wxEVT_SCROLL_THUMBTRACK );
360 event.SetPosition( m_pos );
361
362 GetEventHandler()->ProcessEvent( event );
363 }
364}
365
366wxSize wxSpinButton::DoGetBestSize() const
367{
d8d18184 368 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
dba00620
MB
369}
370
4bb6408c
JS
371// Attributes
372////////////////////////////////////////////////////////////////////////////
373
374int wxSpinButton::GetValue() const
375{
dba00620 376 return m_pos;
4bb6408c
JS
377}
378
dba00620 379void wxSpinButton::SetValue(int val)
4bb6408c 380{
dba00620 381 m_pos = val;
4bb6408c
JS
382}
383
384void wxSpinButton::SetRange(int minVal, int maxVal)
385{
31528cd3 386 wxSpinButtonBase::SetRange(minVal, maxVal);
4bb6408c
JS
387}
388
af111fc3 389void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize))
0d57be45
JS
390{
391 // TODO
392}
393
394void wxSpinButton::ChangeBackgroundColour()
395{
dba00620 396 wxControl::ChangeBackgroundColour();
0d57be45
JS
397}
398
399void wxSpinButton::ChangeForegroundColour()
400{
401 // TODO
402}