]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSpinButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "spinbutt.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/spinbutt.h"
24 #include "wx/spinctrl.h"
28 #pragma message disable nosimpint
30 #include <Xm/ArrowBG.h>
31 #include <Xm/ArrowB.h>
33 #pragma message enable nosimpint
36 #include "wx/motif/private.h"
47 class wxArrowButtonTimer
;
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 static const unsigned int TICK_BEFORE_START
= 10;
55 static const unsigned int TICK_BEFORE_EXPONENTIAL
= 40;
56 static const unsigned int MAX_INCREMENT
= 150;
57 static const unsigned int TICK_INTERVAL
= 113;
59 class wxArrowButtonTimer
: public wxTimer
62 wxArrowButtonTimer( wxArrowButton
* btn
, int sign
)
68 void Reset() { m_ticks
= 0; m_increment
= 1; }
71 unsigned int m_increment
;
73 wxArrowButton
* m_button
;
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 class wxArrowButton
: public wxControl
82 friend class wxArrowButtonTimer
;
84 wxArrowButton( int increment
)
85 : m_increment( increment
),
88 wxArrowButton( wxSpinButton
* parent
, wxWindowID id
, ArrowDirection d
,
89 const wxPoint
& pos
= wxDefaultPosition
,
90 const wxSize
& size
= wxDefaultSize
, int increment
= 1 )
92 m_increment( increment
),
95 Create( parent
, id
, d
, pos
, size
);
101 bool Create( wxSpinButton
* parent
, wxWindowID id
, ArrowDirection d
,
102 const wxPoint
& pos
= wxDefaultPosition
,
103 const wxSize
& size
= wxDefaultSize
);
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
) );
113 static void StopTimerCallback( Widget w
, XtPointer clientData
,
114 XtPointer
WXUNUSED(ptr
) );
117 wxArrowButtonTimer
* m_timer
;
120 // ----------------------------------------------------------------------------
121 // wxArrowButtonTimer implementation
122 // ----------------------------------------------------------------------------
124 void wxArrowButtonTimer::Notify()
127 if( m_ticks
< TICK_BEFORE_START
) return;
128 // increment every other tick
129 if( m_ticks
<= TICK_BEFORE_EXPONENTIAL
&& m_ticks
& 1 )
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
);
137 // ----------------------------------------------------------------------------
138 // wxArrowButton implementation
139 // ----------------------------------------------------------------------------
141 wxTimer
* wxArrowButton::GetFreshTimer()
149 m_timer
= new wxArrowButtonTimer( this, m_increment
);
154 void wxArrowButton::SpinButtonCallback( Widget w
, XtPointer clientData
,
155 XtPointer
WXUNUSED(ptr
) )
157 if( !wxGetWindowFromTable( w
) )
158 // Widget has been deleted!
161 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
163 btn
->GetSpinButton()->Increment( btn
->m_increment
);
166 void wxArrowButton::StartTimerCallback( Widget w
, XtPointer clientData
,
167 XtPointer
WXUNUSED(ptr
) )
169 if( !wxGetWindowFromTable( w
) )
170 // Widget has been deleted!
173 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
174 btn
->GetFreshTimer()->Start( TICK_INTERVAL
);
177 void wxArrowButton::StopTimerCallback( Widget w
, XtPointer clientData
,
178 XtPointer
WXUNUSED(ptr
) )
180 if( !wxGetWindowFromTable( w
) )
181 // Widget has been deleted!
184 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
189 bool wxArrowButton::Create( wxSpinButton
* parent
, wxWindowID id
,
191 const wxPoint
& pos
, const wxSize
& size
)
193 int arrow_dir
= XmARROW_UP
;
198 arrow_dir
= XmARROW_UP
;
201 arrow_dir
= XmARROW_DOWN
;
204 arrow_dir
= XmARROW_LEFT
;
207 arrow_dir
= XmARROW_RIGHT
;
211 if( parent
) parent
->AddChild( this );
213 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
214 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget( "XmArrowButton",
215 xmArrowButtonWidgetClass
,
217 XmNarrowDirection
, arrow_dir
,
219 XmNshadowThickness
, 0,
222 XtAddCallback( (Widget
) m_mainWidget
,
223 XmNactivateCallback
, (XtCallbackProc
) SpinButtonCallback
,
225 XtAddCallback( (Widget
) m_mainWidget
,
226 XmNarmCallback
, (XtCallbackProc
) StartTimerCallback
,
228 XtAddCallback( (Widget
) m_mainWidget
,
229 XmNactivateCallback
, (XtCallbackProc
) StopTimerCallback
,
232 AttachWidget( parent
, m_mainWidget
, (WXWidget
) NULL
,
233 pos
.x
, pos
.y
, size
.x
, size
.y
);
235 SetForegroundColour( parent
->GetBackgroundColour() );
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
, wxControl
);
245 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxNotifyEvent
);
247 static void CalcSizes( wxPoint pt
, wxSize sz
,
248 wxPoint
& pt1
, wxSize
& sz1
,
249 wxPoint
& pt2
, wxSize
& sz2
,
252 typedef int wxSize::* CDPTR1
;
253 typedef int wxPoint::* CDPTR2
;
258 CDPTR1 szm
= isVertical
? &wxSize::y
: &wxSize::x
;
259 CDPTR2 ptm
= isVertical
? &wxPoint::y
: &wxPoint::x
;
260 int dim
= sz
.*szm
, half
= dim
/2;
263 sz2
.*szm
= dim
- half
;
264 pt2
.*ptm
+= half
+ 1;
267 bool wxSpinButton::Create( wxWindow
*parent
, wxWindowID id
,
268 const wxPoint
& pos
, const wxSize
& size
,
269 long style
, const wxString
& name
)
271 m_windowStyle
= style
;
273 wxSize newSize
= GetBestSize();
274 if( size
.x
!= -1 ) newSize
.x
= size
.x
;
275 if( size
.y
!= -1 ) newSize
.y
= size
.y
;
277 if( !wxControl::Create( parent
, id
, pos
, newSize
, style
) )
284 m_windowId
= ( id
== wxID_ANY
) ? NewControlId() : id
;
286 bool isVert
= IsVertical();
289 CalcSizes( wxPoint(0,0), newSize
, pt1
, sz1
, pt2
, sz2
, isVert
);
290 m_up
= new wxArrowButton( this, -1, isVert
? wxARROW_UP
: wxARROW_RIGHT
,
292 m_down
= new wxArrowButton( this, -1,
293 isVert
? wxARROW_DOWN
: wxARROW_LEFT
,
299 wxSpinButton::~wxSpinButton()
303 void wxSpinButton::DoMoveWindow(int x
, int y
, int width
, int height
)
305 wxControl::DoMoveWindow( x
, y
, width
, height
);
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
);
316 void wxSpinButton::DoSetSize(int x
, int y
, int width
, int height
,
320 #pragma message disable codcauunr
322 if( sizeFlags
& wxSIZE_USE_EXISTING
&& width
== -1 )
324 if( sizeFlags
& wxSIZE_USE_EXISTING
&& height
== -1 )
325 height
= GetSize().y
;
327 #pragma message enable codcauunr
330 wxControl::DoSetSize(x
, y
, width
, height
, 0);
333 void wxSpinButton::Increment( int delta
)
335 if( m_pos
< m_min
) m_pos
= m_min
;
336 if( m_pos
> m_max
) m_pos
= m_max
;
338 int npos
= m_pos
+ delta
;
342 if( GetWindowStyle() & wxSP_WRAP
)
349 if( GetWindowStyle() & wxSP_WRAP
)
354 if( npos
== m_pos
) return;
356 wxSpinEvent
event( delta
> 0 ? wxEVT_SCROLL_LINEUP
: wxEVT_SCROLL_LINEDOWN
,
358 event
.SetPosition( npos
);
359 event
.SetEventObject( this );
361 GetEventHandler()->ProcessEvent( event
);
363 if( event
.IsAllowed() )
366 event
.SetEventType( wxEVT_SCROLL_THUMBTRACK
);
367 event
.SetPosition( m_pos
);
369 GetEventHandler()->ProcessEvent( event
);
373 wxSize
wxSpinButton::DoGetBestSize() const
375 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
379 ////////////////////////////////////////////////////////////////////////////
381 int wxSpinButton::GetValue() const
386 void wxSpinButton::SetValue(int val
)
391 void wxSpinButton::SetRange(int minVal
, int maxVal
)
393 wxSpinButtonBase::SetRange(minVal
, maxVal
);
396 void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize
))
401 void wxSpinButton::ChangeBackgroundColour()
403 wxControl::ChangeBackgroundColour();
406 void wxSpinButton::ChangeForegroundColour()
411 #endif // wxUSE_SPINBTN