]>
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 void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern bool g_blockEventsOnDrag
;
35 static const float sensitivity
= 0.2;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 static void gtk_spinbutt_callback( GtkWidget
*WXUNUSED(widget
), wxSpinButton
*win
)
43 if (g_isIdle
) wxapp_install_idle_handler();
45 if (!win
->m_hasVMT
) return;
46 if (g_blockEventsOnDrag
) return;
48 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
49 if (fabs(diff
) < sensitivity
) return;
50 win
->m_oldPos
= win
->m_adjust
->value
;
52 wxEventType command
= wxEVT_NULL
;
54 float line_step
= win
->m_adjust
->step_increment
;
55 float page_step
= win
->m_adjust
->page_increment
;
57 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
58 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
59 else if (fabs(diff
-page_step
) < sensitivity
) command
= wxEVT_SCROLL_PAGEDOWN
;
60 else if (fabs(diff
+page_step
) < sensitivity
) command
= wxEVT_SCROLL_PAGEUP
;
61 else command
= wxEVT_SCROLL_THUMBTRACK
;
63 int value
= (int)ceil(win
->m_adjust
->value
);
65 wxSpinEvent
event( command
, win
->GetId());
66 event
.SetPosition( value
);
67 event
.SetOrientation( wxVERTICAL
);
68 event
.SetEventObject( win
);
70 win
->GetEventHandler()->ProcessEvent( event
);
73 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
77 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
)
79 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
80 EVT_SIZE(wxSpinButton::OnSize
)
83 wxSpinButton::wxSpinButton()
87 bool wxSpinButton::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
88 long style
, const wxString
& name
)
92 wxSize new_size
= size
;
97 PreCreation( parent
, id
, pos
, new_size
, style
, name
);
99 // SetValidator( validator );
103 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
105 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
107 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
), (m_windowStyle
& wxSP_WRAP
) );
109 gtk_signal_connect( GTK_OBJECT (m_adjust
),
111 (GtkSignalFunc
) gtk_spinbutt_callback
,
114 m_parent
->DoAddChild( this );
118 SetBackgroundColour( parent
->GetBackgroundColour() );
125 wxSpinButton::~wxSpinButton()
129 int wxSpinButton::GetMin() const
131 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
133 return (int)ceil(m_adjust
->lower
);
136 int wxSpinButton::GetMax() const
138 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
140 return (int)ceil(m_adjust
->upper
);
143 int wxSpinButton::GetValue() const
145 wxCHECK_MSG( (m_widget
!= NULL
), 0, _T("invalid spin button") );
147 return (int)ceil(m_adjust
->value
);
150 void wxSpinButton::SetValue( int value
)
152 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
154 float fpos
= (float)value
;
156 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
158 m_adjust
->value
= fpos
;
160 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
163 void wxSpinButton::SetRange(int minVal
, int maxVal
)
165 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
167 float fmin
= (float)minVal
;
168 float fmax
= (float)maxVal
;
170 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
171 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
176 m_adjust
->lower
= fmin
;
177 m_adjust
->upper
= fmax
;
179 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
182 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
184 wxCHECK_RET( (m_widget
!= NULL
), _T("invalid spin button") );
187 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
190 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
192 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
195 void wxSpinButton::ApplyWidgetStyle()
198 gtk_widget_set_style( m_widget
, m_widgetStyle
);
201 //-----------------------------------------------------------------------------
203 //-----------------------------------------------------------------------------
205 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxScrollEvent
)
207 wxSpinEvent::wxSpinEvent(wxEventType commandType
, int id
):
208 wxScrollEvent(commandType
, id
)