]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "spinctrl.h"
15 #include "wx/spinctrl.h"
20 #include "wx/spinbutt.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
39 static const float sensitivity
= 0.02;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static void gtk_spinctrl_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
47 if (g_isIdle
) wxapp_install_idle_handler();
49 if (!win
->m_hasVMT
) return;
50 if (g_blockEventsOnDrag
) return;
52 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
53 if (fabs(diff
) < sensitivity
) return;
54 win
->m_oldPos
= win
->m_adjust
->value
;
56 wxEventType command
= wxEVT_NULL
;
58 float line_step
= win
->m_adjust
->step_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 command
= wxEVT_SCROLL_THUMBTRACK
;
64 int value
= (int)ceil(win
->m_adjust
->value
);
66 wxSpinEvent
event( command
, win
->GetId());
67 event
.SetPosition( value
);
68 event
.SetEventObject( win
);
69 win
->GetEventHandler()->ProcessEvent( event
);
71 /* always send a thumbtrack event */
72 if (command
!= wxEVT_SCROLL_THUMBTRACK
)
74 command
= wxEVT_SCROLL_THUMBTRACK
;
75 wxSpinEvent
event2( command
, win
->GetId());
76 event2
.SetPosition( value
);
77 event2
.SetEventObject( win
);
78 win
->GetEventHandler()->ProcessEvent( event2
);
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
88 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
89 EVT_CHAR(wxSpinCtrl::OnChar
)
92 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
93 const wxString
& value
,
94 const wxPoint
& pos
, const wxSize
& size
,
96 int min
, int max
, int initial
,
101 wxSize new_size
= size
;
102 if (new_size
.y
== -1)
105 if (!PreCreation( parent
, pos
, new_size
) ||
106 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
108 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
114 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
116 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
118 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
), (m_windowStyle
& wxSP_WRAP
) );
120 gtk_signal_connect( GTK_OBJECT (m_adjust
),
122 (GtkSignalFunc
) gtk_spinctrl_callback
,
125 m_parent
->DoAddChild( this );
129 SetBackgroundColour( parent
->GetBackgroundColour() );
138 int wxSpinCtrl::GetMin() const
140 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
142 return (int)ceil(m_adjust
->lower
);
145 int wxSpinCtrl::GetMax() const
147 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
149 return (int)ceil(m_adjust
->upper
);
152 int wxSpinCtrl::GetValue() const
154 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
156 return (int)ceil(m_adjust
->value
);
159 void wxSpinCtrl::SetValue( const wxString
& value
)
161 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
164 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
171 // invalid number - set text as is (wxMSW compatible)
172 gtk_entry_set_text( GTK_ENTRY(m_widget
), value
.mbc_str() );
176 void wxSpinCtrl::SetValue( int value
)
178 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
180 float fpos
= (float)value
;
182 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
184 m_adjust
->value
= fpos
;
186 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
189 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
191 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
193 float fmin
= (float)minVal
;
194 float fmax
= (float)maxVal
;
196 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
197 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
202 m_adjust
->lower
= fmin
;
203 m_adjust
->upper
= fmax
;
205 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
207 // these two calls are required due to some bug in GTK
212 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
214 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
216 if (event
.KeyCode() == WXK_RETURN
)
218 wxWindow
*top_frame
= m_parent
;
219 while (top_frame
->GetParent() && !(top_frame
->GetParent()->m_isFrame
))
220 top_frame
= top_frame
->GetParent();
221 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
223 if (window
->default_widget
)
225 gtk_widget_activate (window
->default_widget
);
233 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
235 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
238 void wxSpinCtrl::ApplyWidgetStyle()
241 gtk_widget_set_style( m_widget
, m_widgetStyle
);