]>
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
.SetEventObject( win
);
72 win
->GetEventHandler()->ProcessEvent( event
);
75 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
79 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
)
80 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxScrollEvent
);
82 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
83 EVT_SIZE(wxSpinButton::OnSize
)
86 bool wxSpinButton::Create(wxWindow
*parent
,
95 wxSize new_size
= size
;
100 if (!PreCreation( parent
, pos
, new_size
) ||
101 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
103 wxFAIL_MSG( _T("wxXX creation failed") );
109 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
111 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
113 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
), (m_windowStyle
& wxSP_WRAP
) );
115 gtk_signal_connect( GTK_OBJECT (m_adjust
),
117 (GtkSignalFunc
) gtk_spinbutt_callback
,
120 m_parent
->DoAddChild( this );
124 SetBackgroundColour( parent
->GetBackgroundColour() );
131 wxSpinButton::~wxSpinButton()
135 int wxSpinButton::GetMin() const
137 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
139 return (int)ceil(m_adjust
->lower
);
142 int wxSpinButton::GetMax() const
144 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
146 return (int)ceil(m_adjust
->upper
);
149 int wxSpinButton::GetValue() const
151 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
153 return (int)ceil(m_adjust
->value
);
156 void wxSpinButton::SetValue( int value
)
158 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
160 float fpos
= (float)value
;
162 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
164 m_adjust
->value
= fpos
;
166 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
169 void wxSpinButton::SetRange(int minVal
, int maxVal
)
171 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
173 float fmin
= (float)minVal
;
174 float fmax
= (float)maxVal
;
176 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
177 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
182 m_adjust
->lower
= fmin
;
183 m_adjust
->upper
= fmax
;
185 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
188 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
190 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
193 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
196 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
198 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
201 void wxSpinButton::ApplyWidgetStyle()
204 gtk_widget_set_style( m_widget
, m_widgetStyle
);