]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/spinbutt.cpp
3 // Purpose: wxSpinButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/spinbutt.h"
18 #include "wx/spinctrl.h"
22 #pragma message disable nosimpint
24 #include <Xm/ArrowBG.h>
25 #include <Xm/ArrowB.h>
27 #pragma message enable nosimpint
30 #include "wx/motif/private.h"
41 class wxArrowButtonTimer
;
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 static const unsigned int TICK_BEFORE_START
= 10;
49 static const unsigned int TICK_BEFORE_EXPONENTIAL
= 40;
50 static const unsigned int MAX_INCREMENT
= 150;
51 static const unsigned int TICK_INTERVAL
= 113;
53 class wxArrowButtonTimer
: public wxTimer
56 wxArrowButtonTimer( wxArrowButton
* btn
, int sign
)
62 void Reset() { m_ticks
= 0; m_increment
= 1; }
65 unsigned int m_increment
;
67 wxArrowButton
* m_button
;
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 class wxArrowButton
: public wxControl
76 friend class wxArrowButtonTimer
;
78 wxArrowButton( int increment
)
79 : m_increment( increment
),
82 wxArrowButton( wxSpinButton
* parent
, wxWindowID id
, ArrowDirection d
,
83 const wxPoint
& pos
= wxDefaultPosition
,
84 const wxSize
& size
= wxDefaultSize
, int increment
= 1 )
86 m_increment( increment
),
89 Create( parent
, id
, d
, pos
, size
);
95 bool Create( wxSpinButton
* parent
, wxWindowID id
, ArrowDirection d
,
96 const wxPoint
& pos
= wxDefaultPosition
,
97 const wxSize
& size
= wxDefaultSize
);
99 // creates a new timer object, or stops the currently running one
100 wxTimer
* GetFreshTimer();
101 wxSpinButton
* GetSpinButton() { return (wxSpinButton
*)GetParent(); }
102 static void SpinButtonCallback( Widget w
, XtPointer clientData
,
103 XtPointer
WXUNUSED(ptr
) );
104 static void StartTimerCallback( Widget w
, XtPointer clientData
,
105 XtPointer
WXUNUSED(ptr
) );
107 static void StopTimerCallback( Widget w
, XtPointer clientData
,
108 XtPointer
WXUNUSED(ptr
) );
111 wxArrowButtonTimer
* m_timer
;
114 // ----------------------------------------------------------------------------
115 // wxArrowButtonTimer implementation
116 // ----------------------------------------------------------------------------
118 void wxArrowButtonTimer::Notify()
121 if( m_ticks
< TICK_BEFORE_START
) return;
122 // increment every other tick
123 if( m_ticks
<= TICK_BEFORE_EXPONENTIAL
&& m_ticks
& 1 )
125 if( m_ticks
> TICK_BEFORE_EXPONENTIAL
)
126 m_increment
= 2 * m_increment
;
127 if( m_increment
>= MAX_INCREMENT
) m_increment
= MAX_INCREMENT
;
128 m_button
->GetSpinButton()->Increment( m_sign
* m_increment
);
131 // ----------------------------------------------------------------------------
132 // wxArrowButton implementation
133 // ----------------------------------------------------------------------------
135 wxTimer
* wxArrowButton::GetFreshTimer()
143 m_timer
= new wxArrowButtonTimer( this, m_increment
);
148 void wxArrowButton::SpinButtonCallback( Widget w
, XtPointer clientData
,
149 XtPointer
WXUNUSED(ptr
) )
151 if( !wxGetWindowFromTable( w
) )
152 // Widget has been deleted!
155 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
157 btn
->GetSpinButton()->Increment( btn
->m_increment
);
160 void wxArrowButton::StartTimerCallback( Widget w
, XtPointer clientData
,
161 XtPointer
WXUNUSED(ptr
) )
163 if( !wxGetWindowFromTable( w
) )
164 // Widget has been deleted!
167 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
168 btn
->GetFreshTimer()->Start( TICK_INTERVAL
);
171 void wxArrowButton::StopTimerCallback( Widget w
, XtPointer clientData
,
172 XtPointer
WXUNUSED(ptr
) )
174 if( !wxGetWindowFromTable( w
) )
175 // Widget has been deleted!
178 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
183 bool wxArrowButton::Create( wxSpinButton
* parent
,
184 wxWindowID
WXUNUSED(id
),
186 const wxPoint
& pos
, const wxSize
& size
)
188 int arrow_dir
= XmARROW_UP
;
193 arrow_dir
= XmARROW_UP
;
196 arrow_dir
= XmARROW_DOWN
;
199 arrow_dir
= XmARROW_LEFT
;
202 arrow_dir
= XmARROW_RIGHT
;
206 if( parent
) parent
->AddChild( this );
208 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
209 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget( "XmArrowButton",
210 xmArrowButtonWidgetClass
,
212 XmNarrowDirection
, arrow_dir
,
214 XmNshadowThickness
, 0,
217 XtAddCallback( (Widget
) m_mainWidget
,
218 XmNactivateCallback
, (XtCallbackProc
) SpinButtonCallback
,
220 XtAddCallback( (Widget
) m_mainWidget
,
221 XmNarmCallback
, (XtCallbackProc
) StartTimerCallback
,
223 XtAddCallback( (Widget
) m_mainWidget
,
224 XmNactivateCallback
, (XtCallbackProc
) StopTimerCallback
,
227 AttachWidget( parent
, m_mainWidget
, (WXWidget
) NULL
,
228 pos
.x
, pos
.y
, size
.x
, size
.y
);
230 SetForegroundColour( parent
->GetBackgroundColour() );
235 // ----------------------------------------------------------------------------
237 // ----------------------------------------------------------------------------
239 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
, wxControl
)
240 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxNotifyEvent
)
242 static void CalcSizes( const wxPoint
& pt
, const wxSize
& sz
,
243 wxPoint
& pt1
, wxSize
& sz1
,
244 wxPoint
& pt2
, wxSize
& sz2
,
247 typedef int wxSize::* CDPTR1
;
248 typedef int wxPoint::* CDPTR2
;
253 CDPTR1 szm
= isVertical
? &wxSize::y
: &wxSize::x
;
254 CDPTR2 ptm
= isVertical
? &wxPoint::y
: &wxPoint::x
;
255 int dim
= sz
.*szm
, half
= dim
/2;
258 sz2
.*szm
= dim
- half
;
259 pt2
.*ptm
+= half
+ 1;
262 bool wxSpinButton::Create( wxWindow
*parent
, wxWindowID id
,
263 const wxPoint
& pos
, const wxSize
& size
,
264 long style
, const wxString
& name
)
266 m_windowStyle
= style
;
268 wxSize newSize
= GetBestSize();
269 if( size
.x
!= -1 ) newSize
.x
= size
.x
;
270 if( size
.y
!= -1 ) newSize
.y
= size
.y
;
272 if( !wxControl::Create( parent
, id
, pos
, newSize
, style
) )
279 m_windowId
= ( id
== wxID_ANY
) ? NewControlId() : id
;
281 bool isVert
= IsVertical();
284 CalcSizes( wxPoint(0,0), newSize
, pt1
, sz1
, pt2
, sz2
, isVert
);
285 m_up
= new wxArrowButton( this, -1, isVert
? wxARROW_UP
: wxARROW_RIGHT
,
287 m_down
= new wxArrowButton( this, -1,
288 isVert
? wxARROW_DOWN
: wxARROW_LEFT
,
294 wxSpinButton::~wxSpinButton()
298 void wxSpinButton::DoMoveWindow(int x
, int y
, int width
, int height
)
300 wxControl::DoMoveWindow( x
, y
, width
, height
);
305 CalcSizes( wxPoint(0,0), wxSize(width
,height
), pt1
,
306 sz1
, pt2
, sz2
, IsVertical() );
307 m_up
->SetSize( pt1
.x
, pt1
.y
, sz1
.x
, sz1
.y
);
308 m_down
->SetSize( pt2
.x
, pt2
.y
, sz2
.x
, sz2
.y
);
311 void wxSpinButton::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
313 if ( (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) && width
== -1 )
315 if ( (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
) && height
== -1 )
316 height
= GetSize().y
;
318 wxControl::DoSetSize(x
, y
, width
, height
, 0);
321 void wxSpinButton::Increment( int delta
)
323 if( m_pos
< m_min
) m_pos
= m_min
;
324 if( m_pos
> m_max
) m_pos
= m_max
;
326 int npos
= m_pos
+ delta
;
330 if( GetWindowStyle() & wxSP_WRAP
)
337 if( GetWindowStyle() & wxSP_WRAP
)
342 if( npos
== m_pos
) return;
344 wxSpinEvent
event( delta
> 0 ? wxEVT_SCROLL_LINEUP
: wxEVT_SCROLL_LINEDOWN
,
346 event
.SetPosition( npos
);
347 event
.SetEventObject( this );
349 GetEventHandler()->ProcessEvent( event
);
351 if( event
.IsAllowed() )
354 event
.SetEventType( wxEVT_SCROLL_THUMBTRACK
);
355 event
.SetPosition( m_pos
);
357 GetEventHandler()->ProcessEvent( event
);
361 wxSize
wxSpinButton::DoGetBestSize() const
363 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
367 ////////////////////////////////////////////////////////////////////////////
369 int wxSpinButton::GetValue() const
374 void wxSpinButton::SetValue(int val
)
379 void wxSpinButton::SetRange(int minVal
, int maxVal
)
381 wxSpinButtonBase::SetRange(minVal
, maxVal
);
384 void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize
))
389 void wxSpinButton::ChangeBackgroundColour()
391 wxControl::ChangeBackgroundColour();
394 void wxSpinButton::ChangeForegroundColour()
399 #endif // wxUSE_SPINBTN