]>
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
)
81 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxScrollEvent
);
83 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
84 EVT_SIZE(wxSpinButton::OnSize
)
87 bool wxSpinButton::Create(wxWindow
*parent
,
96 wxSize new_size
= size
;
101 PreCreation( parent
, id
, pos
, new_size
, style
, name
);
103 // SetValidator( validator );
107 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
109 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
111 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
), (m_windowStyle
& wxSP_WRAP
) );
113 gtk_signal_connect( GTK_OBJECT (m_adjust
),
115 (GtkSignalFunc
) gtk_spinbutt_callback
,
118 m_parent
->DoAddChild( this );
122 SetBackgroundColour( parent
->GetBackgroundColour() );
129 wxSpinButton::~wxSpinButton()
133 int wxSpinButton::GetMin() const
135 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
137 return (int)ceil(m_adjust
->lower
);
140 int wxSpinButton::GetMax() const
142 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
144 return (int)ceil(m_adjust
->upper
);
147 int wxSpinButton::GetValue() const
149 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
151 return (int)ceil(m_adjust
->value
);
154 void wxSpinButton::SetValue( int value
)
156 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
158 float fpos
= (float)value
;
160 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
162 m_adjust
->value
= fpos
;
164 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
167 void wxSpinButton::SetRange(int minVal
, int maxVal
)
169 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
171 float fmin
= (float)minVal
;
172 float fmax
= (float)maxVal
;
174 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
175 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
180 m_adjust
->lower
= fmin
;
181 m_adjust
->upper
= fmax
;
183 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
186 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
188 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
191 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
194 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
196 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
199 void wxSpinButton::ApplyWidgetStyle()
202 gtk_widget_set_style( m_widget
, m_widgetStyle
);