]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/spinbutt.cpp
e742ea7be40439b682115b43363b23c174a67a64
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"
19 #include "wx/spinbutt.h"
20 #include "wx/spinctrl.h"
24 #pragma message disable nosimpint
26 #include <Xm/ArrowBG.h>
27 #include <Xm/ArrowB.h>
29 #pragma message enable nosimpint
32 #include "wx/motif/private.h"
43 class wxArrowButtonTimer
;
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 static const unsigned int TICK_BEFORE_START
= 10;
51 static const unsigned int TICK_BEFORE_EXPONENTIAL
= 40;
52 static const unsigned int MAX_INCREMENT
= 150;
53 static const unsigned int TICK_INTERVAL
= 113;
55 class wxArrowButtonTimer
: public wxTimer
58 wxArrowButtonTimer( wxArrowButton
* btn
, int sign
)
64 void Reset() { m_ticks
= 0; m_increment
= 1; }
67 unsigned int m_increment
;
69 wxArrowButton
* m_button
;
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 class wxArrowButton
: public wxControl
78 friend class wxArrowButtonTimer
;
80 wxArrowButton( int increment
)
81 : m_increment( increment
),
84 wxArrowButton( wxSpinButton
* parent
, wxWindowID id
, ArrowDirection d
,
85 const wxPoint
& pos
= wxDefaultPosition
,
86 const wxSize
& size
= wxDefaultSize
, int increment
= 1 )
88 m_increment( increment
),
91 Create( parent
, id
, d
, pos
, size
);
97 bool Create( wxSpinButton
* parent
, wxWindowID id
, ArrowDirection d
,
98 const wxPoint
& pos
= wxDefaultPosition
,
99 const wxSize
& size
= wxDefaultSize
);
101 // creates a new timer object, or stops the currently running one
102 wxTimer
* GetFreshTimer();
103 wxSpinButton
* GetSpinButton() { return (wxSpinButton
*)GetParent(); }
104 static void SpinButtonCallback( Widget w
, XtPointer clientData
,
105 XtPointer
WXUNUSED(ptr
) );
106 static void StartTimerCallback( Widget w
, XtPointer clientData
,
107 XtPointer
WXUNUSED(ptr
) );
109 static void StopTimerCallback( Widget w
, XtPointer clientData
,
110 XtPointer
WXUNUSED(ptr
) );
113 wxArrowButtonTimer
* m_timer
;
116 // ----------------------------------------------------------------------------
117 // wxArrowButtonTimer implementation
118 // ----------------------------------------------------------------------------
120 void wxArrowButtonTimer::Notify()
123 if( m_ticks
< TICK_BEFORE_START
) return;
124 // increment every other tick
125 if( m_ticks
<= TICK_BEFORE_EXPONENTIAL
&& m_ticks
& 1 )
127 if( m_ticks
> TICK_BEFORE_EXPONENTIAL
)
128 m_increment
= 2 * m_increment
;
129 if( m_increment
>= MAX_INCREMENT
) m_increment
= MAX_INCREMENT
;
130 m_button
->GetSpinButton()->Increment( m_sign
* m_increment
);
133 // ----------------------------------------------------------------------------
134 // wxArrowButton implementation
135 // ----------------------------------------------------------------------------
137 wxTimer
* wxArrowButton::GetFreshTimer()
145 m_timer
= new wxArrowButtonTimer( this, m_increment
);
150 void wxArrowButton::SpinButtonCallback( Widget w
, XtPointer clientData
,
151 XtPointer
WXUNUSED(ptr
) )
153 if( !wxGetWindowFromTable( w
) )
154 // Widget has been deleted!
157 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
159 btn
->GetSpinButton()->Increment( btn
->m_increment
);
162 void wxArrowButton::StartTimerCallback( Widget w
, XtPointer clientData
,
163 XtPointer
WXUNUSED(ptr
) )
165 if( !wxGetWindowFromTable( w
) )
166 // Widget has been deleted!
169 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
170 btn
->GetFreshTimer()->Start( TICK_INTERVAL
);
173 void wxArrowButton::StopTimerCallback( Widget w
, XtPointer clientData
,
174 XtPointer
WXUNUSED(ptr
) )
176 if( !wxGetWindowFromTable( w
) )
177 // Widget has been deleted!
180 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
185 bool wxArrowButton::Create( wxSpinButton
* parent
,
186 wxWindowID
WXUNUSED(id
),
188 const wxPoint
& pos
, const wxSize
& size
)
190 int arrow_dir
= XmARROW_UP
;
195 arrow_dir
= XmARROW_UP
;
198 arrow_dir
= XmARROW_DOWN
;
201 arrow_dir
= XmARROW_LEFT
;
204 arrow_dir
= XmARROW_RIGHT
;
208 if( parent
) parent
->AddChild( this );
210 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
211 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget( "XmArrowButton",
212 xmArrowButtonWidgetClass
,
214 XmNarrowDirection
, arrow_dir
,
216 XmNshadowThickness
, 0,
219 XtAddCallback( (Widget
) m_mainWidget
,
220 XmNactivateCallback
, (XtCallbackProc
) SpinButtonCallback
,
222 XtAddCallback( (Widget
) m_mainWidget
,
223 XmNarmCallback
, (XtCallbackProc
) StartTimerCallback
,
225 XtAddCallback( (Widget
) m_mainWidget
,
226 XmNactivateCallback
, (XtCallbackProc
) StopTimerCallback
,
229 AttachWidget( parent
, m_mainWidget
, (WXWidget
) NULL
,
230 pos
.x
, pos
.y
, size
.x
, size
.y
);
232 SetForegroundColour( parent
->GetBackgroundColour() );
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
, wxControl
);
242 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxNotifyEvent
);
244 static void CalcSizes( const wxPoint
& pt
, const wxSize
& sz
,
245 wxPoint
& pt1
, wxSize
& sz1
,
246 wxPoint
& pt2
, wxSize
& sz2
,
249 typedef int wxSize::* CDPTR1
;
250 typedef int wxPoint::* CDPTR2
;
255 CDPTR1 szm
= isVertical
? &wxSize::y
: &wxSize::x
;
256 CDPTR2 ptm
= isVertical
? &wxPoint::y
: &wxPoint::x
;
257 int dim
= sz
.*szm
, half
= dim
/2;
260 sz2
.*szm
= dim
- half
;
261 pt2
.*ptm
+= half
+ 1;
264 bool wxSpinButton::Create( wxWindow
*parent
, wxWindowID id
,
265 const wxPoint
& pos
, const wxSize
& size
,
266 long style
, const wxString
& name
)
268 m_windowStyle
= style
;
270 wxSize newSize
= GetBestSize();
271 if( size
.x
!= -1 ) newSize
.x
= size
.x
;
272 if( size
.y
!= -1 ) newSize
.y
= size
.y
;
274 if( !wxControl::Create( parent
, id
, pos
, newSize
, style
) )
281 m_windowId
= ( id
== wxID_ANY
) ? NewControlId() : id
;
283 bool isVert
= IsVertical();
286 CalcSizes( wxPoint(0,0), newSize
, pt1
, sz1
, pt2
, sz2
, isVert
);
287 m_up
= new wxArrowButton( this, -1, isVert
? wxARROW_UP
: wxARROW_RIGHT
,
289 m_down
= new wxArrowButton( this, -1,
290 isVert
? wxARROW_DOWN
: wxARROW_LEFT
,
296 wxSpinButton::~wxSpinButton()
300 void wxSpinButton::DoMoveWindow(int x
, int y
, int width
, int height
)
302 wxControl::DoMoveWindow( x
, y
, width
, height
);
307 CalcSizes( wxPoint(0,0), wxSize(width
,height
), pt1
,
308 sz1
, pt2
, sz2
, IsVertical() );
309 m_up
->SetSize( pt1
.x
, pt1
.y
, sz1
.x
, sz1
.y
);
310 m_down
->SetSize( pt2
.x
, pt2
.y
, sz2
.x
, sz2
.y
);
313 void wxSpinButton::DoSetSize(int x
, int y
, int width
, int height
,
317 #pragma message disable codcauunr
319 if( sizeFlags
& wxSIZE_USE_EXISTING
&& width
== -1 )
321 if( sizeFlags
& wxSIZE_USE_EXISTING
&& height
== -1 )
322 height
= GetSize().y
;
324 #pragma message enable codcauunr
327 wxControl::DoSetSize(x
, y
, width
, height
, 0);
330 void wxSpinButton::Increment( int delta
)
332 if( m_pos
< m_min
) m_pos
= m_min
;
333 if( m_pos
> m_max
) m_pos
= m_max
;
335 int npos
= m_pos
+ delta
;
339 if( GetWindowStyle() & wxSP_WRAP
)
346 if( GetWindowStyle() & wxSP_WRAP
)
351 if( npos
== m_pos
) return;
353 wxSpinEvent
event( delta
> 0 ? wxEVT_SCROLL_LINEUP
: wxEVT_SCROLL_LINEDOWN
,
355 event
.SetPosition( npos
);
356 event
.SetEventObject( this );
358 GetEventHandler()->ProcessEvent( event
);
360 if( event
.IsAllowed() )
363 event
.SetEventType( wxEVT_SCROLL_THUMBTRACK
);
364 event
.SetPosition( m_pos
);
366 GetEventHandler()->ProcessEvent( event
);
370 wxSize
wxSpinButton::DoGetBestSize() const
372 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
376 ////////////////////////////////////////////////////////////////////////////
378 int wxSpinButton::GetValue() const
383 void wxSpinButton::SetValue(int val
)
388 void wxSpinButton::SetRange(int minVal
, int maxVal
)
390 wxSpinButtonBase::SetRange(minVal
, maxVal
);
393 void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize
))
398 void wxSpinButton::ChangeBackgroundColour()
400 wxControl::ChangeBackgroundColour();
403 void wxSpinButton::ChangeForegroundColour()
408 #endif // wxUSE_SPINBTN