]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSpinButton
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "spinbutt.h"
15 #include "wx/spinbutt.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern bool g_blockEventsOnDrag
;
28 static const float sensitivity
= 0.2;
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 static void gtk_spinbutt_callback( GtkWidget
*WXUNUSED(widget
), wxSpinButton
*win
)
36 if (!win
->HasVMT()) return;
37 if (g_blockEventsOnDrag
) return;
39 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
40 if (fabs(diff
) < sensitivity
) return;
41 win
->m_oldPos
= win
->m_adjust
->value
;
43 wxEventType command
= wxEVT_NULL
;
45 float line_step
= win
->m_adjust
->step_increment
;
46 float page_step
= win
->m_adjust
->page_increment
;
48 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
49 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
50 else if (fabs(diff
-page_step
) < sensitivity
) command
= wxEVT_SCROLL_PAGEDOWN
;
51 else if (fabs(diff
+page_step
) < sensitivity
) command
= wxEVT_SCROLL_PAGEUP
;
52 else command
= wxEVT_SCROLL_THUMBTRACK
;
54 int value
= (int)ceil(win
->m_adjust
->value
);
56 wxSpinEvent
event( command
, win
->GetId());
57 event
.SetPosition( value
);
58 event
.SetOrientation( wxVERTICAL
);
59 event
.SetEventObject( win
);
61 win
->GetEventHandler()->ProcessEvent( event
);
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
)
70 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
71 EVT_SIZE(wxSpinButton::OnSize
)
74 wxSpinButton::wxSpinButton()
78 bool wxSpinButton::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
79 long style
, const wxString
& name
)
83 wxSize new_size
= size
;
88 PreCreation( parent
, id
, pos
, new_size
, style
, name
);
90 // SetValidator( validator );
94 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
96 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
98 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
), (m_windowStyle
& wxSP_WRAP
) );
100 gtk_signal_connect( GTK_OBJECT (m_adjust
),
102 (GtkSignalFunc
) gtk_spinbutt_callback
,
105 m_parent
->AddChild( this );
107 (m_parent
->m_insertCallback
)( m_parent
, this );
111 SetBackgroundColour( parent
->GetBackgroundColour() );
118 wxSpinButton::~wxSpinButton()
122 int wxSpinButton::GetMin() const
124 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
126 return (int)ceil(m_adjust
->lower
);
129 int wxSpinButton::GetMax() const
131 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
133 return (int)ceil(m_adjust
->upper
);
136 int wxSpinButton::GetValue() const
138 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
140 return (int)ceil(m_adjust
->value
);
143 void wxSpinButton::SetValue( int value
)
145 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
147 float fpos
= (float)value
;
149 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
151 m_adjust
->value
= fpos
;
153 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
156 void wxSpinButton::SetRange(int minVal
, int maxVal
)
158 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
160 float fmin
= (float)minVal
;
161 float fmax
= (float)maxVal
;
163 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
164 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
169 m_adjust
->lower
= fmin
;
170 m_adjust
->upper
= fmax
;
172 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
175 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
177 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
180 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
183 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
185 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
188 void wxSpinButton::ApplyWidgetStyle()
191 gtk_widget_set_style( m_widget
, m_widgetStyle
);
194 //-----------------------------------------------------------------------------
196 //-----------------------------------------------------------------------------
198 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxScrollEvent
)
200 wxSpinEvent::wxSpinEvent(wxEventType commandType
, int id
):
201 wxScrollEvent(commandType
, id
)