]>
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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "spinbutt.h"
16 #include "wx/spinbutt.h"
17 #include "wx/spinctrl.h"
21 #pragma message disable nosimpint
23 #include <Xm/ArrowBG.h>
24 #include <Xm/ArrowB.h>
26 #pragma message enable nosimpint
29 #include "wx/motif/private.h"
40 class wxArrowButtonTimer
;
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
47 static const unsigned int TICK_BEFORE_START
= 10;
48 static const unsigned int TICK_BEFORE_EXPONENTIAL
= 40;
49 static const unsigned int MAX_INCREMENT
= 150;
50 static const unsigned int TICK_INTERVAL
= 113;
52 class wxArrowButtonTimer
: public wxTimer
55 wxArrowButtonTimer( wxArrowButton
* btn
, int sign
)
61 void Reset() { m_ticks
= 0; m_increment
= 1; }
64 unsigned int m_increment
;
66 wxArrowButton
* m_button
;
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 class wxArrowButton
: public wxControl
75 friend class wxArrowButtonTimer
;
77 wxArrowButton( int increment
)
78 : m_increment( increment
),
81 wxArrowButton( wxSpinButton
* parent
, wxWindowID id
, ArrowDirection d
,
82 const wxPoint
& pos
= wxDefaultPosition
,
83 const wxSize
& size
= wxDefaultSize
, int increment
= 1 )
85 m_increment( increment
),
88 Create( parent
, id
, d
, pos
, size
);
94 bool Create( wxSpinButton
* parent
, wxWindowID id
, ArrowDirection d
,
95 const wxPoint
& pos
= wxDefaultPosition
,
96 const wxSize
& size
= wxDefaultSize
);
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
) );
106 static void StopTimerCallback( Widget w
, XtPointer clientData
,
107 XtPointer
WXUNUSED(ptr
) );
110 wxArrowButtonTimer
* m_timer
;
113 // ----------------------------------------------------------------------------
114 // wxArrowButtonTimer implementation
115 // ----------------------------------------------------------------------------
117 void wxArrowButtonTimer::Notify()
120 if( m_ticks
< TICK_BEFORE_START
) return;
121 // increment every other tick
122 if( m_ticks
<= TICK_BEFORE_EXPONENTIAL
&& m_ticks
& 1 )
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
);
130 // ----------------------------------------------------------------------------
131 // wxArrowButton implementation
132 // ----------------------------------------------------------------------------
134 wxTimer
* wxArrowButton::GetFreshTimer()
142 m_timer
= new wxArrowButtonTimer( this, m_increment
);
147 void wxArrowButton::SpinButtonCallback( Widget w
, XtPointer clientData
,
148 XtPointer
WXUNUSED(ptr
) )
150 if( !wxGetWindowFromTable( w
) )
151 // Widget has been deleted!
154 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
156 btn
->GetSpinButton()->Increment( btn
->m_increment
);
159 void wxArrowButton::StartTimerCallback( Widget w
, XtPointer clientData
,
160 XtPointer
WXUNUSED(ptr
) )
162 if( !wxGetWindowFromTable( w
) )
163 // Widget has been deleted!
166 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
167 btn
->GetFreshTimer()->Start( TICK_INTERVAL
);
170 void wxArrowButton::StopTimerCallback( Widget w
, XtPointer clientData
,
171 XtPointer
WXUNUSED(ptr
) )
173 if( !wxGetWindowFromTable( w
) )
174 // Widget has been deleted!
177 wxArrowButton
* btn
= (wxArrowButton
*)clientData
;
182 bool wxArrowButton::Create( wxSpinButton
* parent
, wxWindowID id
,
184 const wxPoint
& pos
, const wxSize
& size
)
191 arrow_dir
= XmARROW_UP
;
194 arrow_dir
= XmARROW_DOWN
;
197 arrow_dir
= XmARROW_LEFT
;
200 arrow_dir
= XmARROW_RIGHT
;
204 if( parent
) parent
->AddChild( this );
206 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
207 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget( "XmArrowButton",
208 xmArrowButtonWidgetClass
,
210 XmNarrowDirection
, arrow_dir
,
212 XmNshadowThickness
, 0,
215 XtAddCallback( (Widget
) m_mainWidget
,
216 XmNactivateCallback
, (XtCallbackProc
) SpinButtonCallback
,
218 XtAddCallback( (Widget
) m_mainWidget
,
219 XmNarmCallback
, (XtCallbackProc
) StartTimerCallback
,
221 XtAddCallback( (Widget
) m_mainWidget
,
222 XmNactivateCallback
, (XtCallbackProc
) StopTimerCallback
,
225 AttachWidget( parent
, m_mainWidget
, (WXWidget
) NULL
,
226 pos
.x
, pos
.y
, size
.x
, size
.y
);
228 SetForegroundColour( parent
->GetBackgroundColour() );
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
, wxControl
);
238 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxNotifyEvent
);
240 static void CalcSizes( wxPoint pt
, wxSize sz
,
241 wxPoint
& pt1
, wxSize
& sz1
,
242 wxPoint
& pt2
, wxSize
& sz2
,
245 typedef int wxSize::* CDPTR1
;
246 typedef int wxPoint::* CDPTR2
;
251 CDPTR1 szm
= isVertical
? &wxSize::y
: &wxSize::x
;
252 CDPTR2 ptm
= isVertical
? &wxPoint::y
: &wxPoint::x
;
253 int dim
= sz
.*szm
, half
= dim
/2;
256 sz2
.*szm
= dim
- half
;
257 pt2
.*ptm
+= half
+ 1;
260 bool wxSpinButton::Create( wxWindow
*parent
, wxWindowID id
,
261 const wxPoint
& pos
, const wxSize
& size
,
262 long style
, const wxString
& name
)
264 m_windowStyle
= style
;
266 wxSize newSize
= GetBestSize();
267 if( size
.x
!= -1 ) newSize
.x
= size
.x
;
268 if( size
.y
!= -1 ) newSize
.y
= size
.y
;
270 if( !wxControl::Create( parent
, id
, pos
, newSize
, style
) )
279 m_windowId
= ( id
== -1 ) ? 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
,
315 #pragma message disable codcauunr
317 if( sizeFlags
& wxSIZE_USE_EXISTING
&& width
== -1 )
319 if( sizeFlags
& wxSIZE_USE_EXISTING
&& height
== -1 )
320 height
= GetSize().y
;
322 #pragma message enable codcauunr
325 wxControl::DoSetSize(x
, y
, width
, height
, 0);
328 void wxSpinButton::Increment( int delta
)
330 if( m_pos
< m_min
) m_pos
= m_min
;
331 if( m_pos
> m_max
) m_pos
= m_max
;
333 int npos
= m_pos
+ delta
;
337 if( GetWindowStyle() & wxSP_WRAP
)
344 if( GetWindowStyle() & wxSP_WRAP
)
349 if( npos
== m_pos
) return;
351 wxSpinEvent
event( delta
> 0 ? wxEVT_SCROLL_LINEUP
: wxEVT_SCROLL_LINEDOWN
,
353 event
.SetPosition( npos
);
354 event
.SetEventObject( this );
356 GetEventHandler()->ProcessEvent( event
);
358 if( event
.IsAllowed() )
361 event
.SetEventType( wxEVT_SCROLL_THUMBTRACK
);
362 event
.SetPosition( m_pos
);
364 GetEventHandler()->ProcessEvent( event
);
368 wxSize
wxSpinButton::DoGetBestSize() const
370 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
374 ////////////////////////////////////////////////////////////////////////////
376 int wxSpinButton::GetValue() const
381 void wxSpinButton::SetValue(int val
)
386 void wxSpinButton::SetRange(int minVal
, int maxVal
)
388 wxSpinButtonBase::SetRange(minVal
, maxVal
);
391 void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize
))
396 void wxSpinButton::ChangeBackgroundColour()
398 wxControl::ChangeBackgroundColour();
401 void wxSpinButton::ChangeForegroundColour()