]>
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"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
40 static const float sensitivity
= 0.02;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 static void gtk_spinctrl_callback( GtkWidget
*WXUNUSED(widget
), wxSpinCtrl
*win
)
48 if (g_isIdle
) wxapp_install_idle_handler();
50 if (!win
->m_hasVMT
) return;
51 if (g_blockEventsOnDrag
) return;
53 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
54 if (fabs(diff
) < sensitivity
) return;
55 win
->m_oldPos
= win
->m_adjust
->value
;
57 wxEventType command
= wxEVT_NULL
;
59 float line_step
= win
->m_adjust
->step_increment
;
61 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
62 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
63 else command
= wxEVT_SCROLL_THUMBTRACK
;
65 int value
= (int)ceil(win
->m_adjust
->value
);
67 wxSpinEvent
event( command
, win
->GetId());
68 event
.SetPosition( value
);
69 event
.SetEventObject( win
);
70 win
->GetEventHandler()->ProcessEvent( event
);
72 /* always send a thumbtrack event */
73 if (command
!= wxEVT_SCROLL_THUMBTRACK
)
75 command
= wxEVT_SCROLL_THUMBTRACK
;
76 wxSpinEvent
event2( command
, win
->GetId());
77 event2
.SetPosition( value
);
78 event2
.SetEventObject( win
);
79 win
->GetEventHandler()->ProcessEvent( event2
);
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
87 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl
,wxControl
)
89 BEGIN_EVENT_TABLE(wxSpinCtrl
, wxControl
)
90 EVT_CHAR(wxSpinCtrl::OnChar
)
93 bool wxSpinCtrl::Create(wxWindow
*parent
, wxWindowID id
,
94 const wxString
& value
,
95 const wxPoint
& pos
, const wxSize
& size
,
97 int min
, int max
, int initial
,
101 m_acceptsFocus
= TRUE
;
103 wxSize new_size
= size
;
104 if (new_size
.y
== -1)
107 if (!PreCreation( parent
, pos
, new_size
) ||
108 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
110 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
116 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( initial
, min
, max
, 1.0, 5.0, 0.0);
118 m_widget
= gtk_spin_button_new( m_adjust
, 1, 0 );
120 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
121 (int)(m_windowStyle
& wxSP_WRAP
) );
123 gtk_signal_connect( GTK_OBJECT (m_adjust
),
125 (GtkSignalFunc
) gtk_spinctrl_callback
,
128 m_parent
->DoAddChild( this );
132 SetBackgroundColour( parent
->GetBackgroundColour() );
141 int wxSpinCtrl::GetMin() const
143 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
145 return (int)ceil(m_adjust
->lower
);
148 int wxSpinCtrl::GetMax() const
150 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
152 return (int)ceil(m_adjust
->upper
);
155 int wxSpinCtrl::GetValue() const
157 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
159 return (int)ceil(m_adjust
->value
);
162 void wxSpinCtrl::SetValue( const wxString
& value
)
164 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
167 if ( (wxSscanf(value
, wxT("%d"), &n
) == 1) )
174 // invalid number - set text as is (wxMSW compatible)
175 gtk_entry_set_text( GTK_ENTRY(m_widget
), value
.mbc_str() );
179 void wxSpinCtrl::SetValue( int value
)
181 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
183 float fpos
= (float)value
;
185 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
187 m_adjust
->value
= fpos
;
189 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
192 void wxSpinCtrl::SetRange(int minVal
, int maxVal
)
194 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
196 float fmin
= (float)minVal
;
197 float fmax
= (float)maxVal
;
199 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
200 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
205 m_adjust
->lower
= fmin
;
206 m_adjust
->upper
= fmax
;
208 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
210 // these two calls are required due to some bug in GTK
215 void wxSpinCtrl::OnChar( wxKeyEvent
&event
)
217 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid spin ctrl") );
219 if (event
.KeyCode() == WXK_RETURN
)
221 wxWindow
*top_frame
= m_parent
;
222 while (top_frame
->GetParent() && !(top_frame
->GetParent()->m_isFrame
))
223 top_frame
= top_frame
->GetParent();
224 GtkWindow
*window
= GTK_WINDOW(top_frame
->m_widget
);
226 if (window
->default_widget
)
228 gtk_widget_activate (window
->default_widget
);
236 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow
*window
)
238 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
241 void wxSpinCtrl::ApplyWidgetStyle()
244 gtk_widget_set_style( m_widget
, m_widgetStyle
);