]>
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"
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
, wxWindowID id
,
187 const wxPoint
& pos
, const wxSize
& size
)
189 int arrow_dir
= XmARROW_UP
;
194 arrow_dir
= XmARROW_UP
;
197 arrow_dir
= XmARROW_DOWN
;
200 arrow_dir
= XmARROW_LEFT
;
203 arrow_dir
= XmARROW_RIGHT
;
207 if( parent
) parent
->AddChild( this );
209 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
210 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget( "XmArrowButton",
211 xmArrowButtonWidgetClass
,
213 XmNarrowDirection
, arrow_dir
,
215 XmNshadowThickness
, 0,
218 XtAddCallback( (Widget
) m_mainWidget
,
219 XmNactivateCallback
, (XtCallbackProc
) SpinButtonCallback
,
221 XtAddCallback( (Widget
) m_mainWidget
,
222 XmNarmCallback
, (XtCallbackProc
) StartTimerCallback
,
224 XtAddCallback( (Widget
) m_mainWidget
,
225 XmNactivateCallback
, (XtCallbackProc
) StopTimerCallback
,
228 AttachWidget( parent
, m_mainWidget
, (WXWidget
) NULL
,
229 pos
.x
, pos
.y
, size
.x
, size
.y
);
231 SetForegroundColour( parent
->GetBackgroundColour() );
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
, wxControl
);
241 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxNotifyEvent
);
243 static void CalcSizes( wxPoint pt
, wxSize sz
,
244 wxPoint
& pt1
, wxSize
& sz1
,
245 wxPoint
& pt2
, wxSize
& sz2
,
248 typedef int wxSize::* CDPTR1
;
249 typedef int wxPoint::* CDPTR2
;
254 CDPTR1 szm
= isVertical
? &wxSize::y
: &wxSize::x
;
255 CDPTR2 ptm
= isVertical
? &wxPoint::y
: &wxPoint::x
;
256 int dim
= sz
.*szm
, half
= dim
/2;
259 sz2
.*szm
= dim
- half
;
260 pt2
.*ptm
+= half
+ 1;
263 bool wxSpinButton::Create( wxWindow
*parent
, wxWindowID id
,
264 const wxPoint
& pos
, const wxSize
& size
,
265 long style
, const wxString
& name
)
267 m_windowStyle
= style
;
269 wxSize newSize
= GetBestSize();
270 if( size
.x
!= -1 ) newSize
.x
= size
.x
;
271 if( size
.y
!= -1 ) newSize
.y
= size
.y
;
273 if( !wxControl::Create( parent
, id
, pos
, newSize
, style
) )
280 m_windowId
= ( id
== -1 ) ? NewControlId() : id
;
282 bool isVert
= IsVertical();
285 CalcSizes( wxPoint(0,0), newSize
, pt1
, sz1
, pt2
, sz2
, isVert
);
286 m_up
= new wxArrowButton( this, -1, isVert
? wxARROW_UP
: wxARROW_RIGHT
,
288 m_down
= new wxArrowButton( this, -1,
289 isVert
? wxARROW_DOWN
: wxARROW_LEFT
,
295 wxSpinButton::~wxSpinButton()
299 void wxSpinButton::DoMoveWindow(int x
, int y
, int width
, int height
)
301 wxControl::DoMoveWindow( x
, y
, width
, height
);
306 CalcSizes( wxPoint(0,0), wxSize(width
,height
), pt1
,
307 sz1
, pt2
, sz2
, IsVertical() );
308 m_up
->SetSize( pt1
.x
, pt1
.y
, sz1
.x
, sz1
.y
);
309 m_down
->SetSize( pt2
.x
, pt2
.y
, sz2
.x
, sz2
.y
);
312 void wxSpinButton::DoSetSize(int x
, int y
, int width
, int height
,
316 #pragma message disable codcauunr
318 if( sizeFlags
& wxSIZE_USE_EXISTING
&& width
== -1 )
320 if( sizeFlags
& wxSIZE_USE_EXISTING
&& height
== -1 )
321 height
= GetSize().y
;
323 #pragma message enable codcauunr
326 wxControl::DoSetSize(x
, y
, width
, height
, 0);
329 void wxSpinButton::Increment( int delta
)
331 if( m_pos
< m_min
) m_pos
= m_min
;
332 if( m_pos
> m_max
) m_pos
= m_max
;
334 int npos
= m_pos
+ delta
;
338 if( GetWindowStyle() & wxSP_WRAP
)
345 if( GetWindowStyle() & wxSP_WRAP
)
350 if( npos
== m_pos
) return;
352 wxSpinEvent
event( delta
> 0 ? wxEVT_SCROLL_LINEUP
: wxEVT_SCROLL_LINEDOWN
,
354 event
.SetPosition( npos
);
355 event
.SetEventObject( this );
357 GetEventHandler()->ProcessEvent( event
);
359 if( event
.IsAllowed() )
362 event
.SetEventType( wxEVT_SCROLL_THUMBTRACK
);
363 event
.SetPosition( m_pos
);
365 GetEventHandler()->ProcessEvent( event
);
369 wxSize
wxSpinButton::DoGetBestSize() const
371 return IsVertical() ? wxSize( 20, 30 ) : wxSize( 30, 20 );
375 ////////////////////////////////////////////////////////////////////////////
377 int wxSpinButton::GetValue() const
382 void wxSpinButton::SetValue(int val
)
387 void wxSpinButton::SetRange(int minVal
, int maxVal
)
389 wxSpinButtonBase::SetRange(minVal
, maxVal
);
392 void wxSpinButton::ChangeFont(bool WXUNUSED(keepOriginalSize
))
397 void wxSpinButton::ChangeBackgroundColour()
399 wxControl::ChangeBackgroundColour();
402 void wxSpinButton::ChangeForegroundColour()