]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/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"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
38 static const float sensitivity
= 0.2;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 static void gtk_spinbutt_callback( GtkWidget
*WXUNUSED(widget
), wxSpinButton
*win
)
46 if (g_isIdle
) wxapp_install_idle_handler();
48 if (!win
->m_hasVMT
) return;
49 if (g_blockEventsOnDrag
) return;
51 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
52 if (fabs(diff
) < sensitivity
) return;
53 win
->m_oldPos
= win
->m_adjust
->value
;
55 wxEventType command
= wxEVT_NULL
;
57 float line_step
= win
->m_adjust
->step_increment
;
58 float page_step
= win
->m_adjust
->page_increment
;
60 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
61 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
62 else if (fabs(diff
-page_step
) < sensitivity
) command
= wxEVT_SCROLL_PAGEDOWN
;
63 else if (fabs(diff
+page_step
) < sensitivity
) command
= wxEVT_SCROLL_PAGEUP
;
64 else command
= wxEVT_SCROLL_THUMBTRACK
;
66 int value
= (int)ceil(win
->m_adjust
->value
);
68 wxSpinEvent
event( command
, win
->GetId());
69 event
.SetPosition( value
);
70 event
.SetOrientation( wxVERTICAL
);
71 event
.SetEventObject( win
);
73 win
->GetEventHandler()->ProcessEvent( event
);
76 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
80 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
)
82 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
83 EVT_SIZE(wxSpinButton::OnSize
)
86 wxSpinButton::wxSpinButton()
90 bool wxSpinButton::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
91 long style
, const wxString
& name
)
95 wxSize new_size
= size
;
100 PreCreation( parent
, id
, pos
, new_size
, style
, name
);
102 // SetValidator( validator );
106 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
108 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
110 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
), (m_windowStyle
& wxSP_WRAP
) );
112 gtk_signal_connect( GTK_OBJECT (m_adjust
),
114 (GtkSignalFunc
) gtk_spinbutt_callback
,
117 m_parent
->DoAddChild( this );
121 SetBackgroundColour( parent
->GetBackgroundColour() );
128 wxSpinButton::~wxSpinButton()
132 int wxSpinButton::GetMin() const
134 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
136 return (int)ceil(m_adjust
->lower
);
139 int wxSpinButton::GetMax() const
141 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
143 return (int)ceil(m_adjust
->upper
);
146 int wxSpinButton::GetValue() const
148 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
150 return (int)ceil(m_adjust
->value
);
153 void wxSpinButton::SetValue( int value
)
155 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
157 float fpos
= (float)value
;
159 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
161 m_adjust
->value
= fpos
;
163 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
166 void wxSpinButton::SetRange(int minVal
, int maxVal
)
168 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
170 float fmin
= (float)minVal
;
171 float fmax
= (float)maxVal
;
173 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
174 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
179 m_adjust
->lower
= fmin
;
180 m_adjust
->upper
= fmax
;
182 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
185 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
187 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
190 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
193 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
195 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
198 void wxSpinButton::ApplyWidgetStyle()
201 gtk_widget_set_style( m_widget
, m_widgetStyle
);
204 //-----------------------------------------------------------------------------
206 //-----------------------------------------------------------------------------
208 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxScrollEvent
)
210 wxSpinEvent::wxSpinEvent(wxEventType commandType
, int id
):
211 wxScrollEvent(commandType
, id
)