]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/spinbutt.cpp
3 // Purpose: wxSpinButton
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/spinbutt.h"
22 #include "wx/gtk1/private.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
37 static const float sensitivity
= 0.02;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
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;
54 wxEventType command
= wxEVT_NULL
;
56 float line_step
= win
->m_adjust
->step_increment
;
58 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
59 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
60 else command
= wxEVT_SCROLL_THUMBTRACK
;
62 int value
= (int)ceil(win
->m_adjust
->value
);
64 wxSpinEvent
event( command
, win
->GetId());
65 event
.SetPosition( value
);
66 event
.SetEventObject( win
);
68 if ((win
->HandleWindowEvent( event
)) &&
71 /* program has vetoed */
72 win
->m_adjust
->value
= win
->m_oldPos
;
74 gtk_signal_disconnect_by_func( GTK_OBJECT (win
->m_adjust
),
75 (GtkSignalFunc
) gtk_spinbutt_callback
,
78 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
80 gtk_signal_connect( GTK_OBJECT (win
->m_adjust
),
82 (GtkSignalFunc
) gtk_spinbutt_callback
,
87 win
->m_oldPos
= win
->m_adjust
->value
;
89 /* always send a thumbtrack event */
90 if (command
!= wxEVT_SCROLL_THUMBTRACK
)
92 command
= wxEVT_SCROLL_THUMBTRACK
;
93 wxSpinEvent
event2( command
, win
->GetId());
94 event2
.SetPosition( value
);
95 event2
.SetEventObject( win
);
96 win
->HandleWindowEvent( event2
);
101 //-----------------------------------------------------------------------------
103 //-----------------------------------------------------------------------------
105 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
106 EVT_SIZE(wxSpinButton::OnSize
)
109 bool wxSpinButton::Create(wxWindow
*parent
,
114 const wxString
& name
)
118 wxSize new_size
= size
,
119 sizeBest
= DoGetBestSize();
120 new_size
.x
= sizeBest
.x
; // override width always
121 if (new_size
.y
== -1)
122 new_size
.y
= sizeBest
.y
;
124 if (!PreCreation( parent
, pos
, new_size
) ||
125 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
127 wxFAIL_MSG( wxT("wxXX creation failed") );
133 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
135 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
137 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
138 (int)(m_windowStyle
& wxSP_WRAP
) );
140 gtk_signal_connect( GTK_OBJECT (m_adjust
),
142 (GtkSignalFunc
) gtk_spinbutt_callback
,
145 m_parent
->DoAddChild( this );
147 PostCreation(new_size
);
152 int wxSpinButton::GetMin() const
154 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
156 return (int)ceil(m_adjust
->lower
);
159 int wxSpinButton::GetMax() const
161 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
163 return (int)ceil(m_adjust
->upper
);
166 int wxSpinButton::GetValue() const
168 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
170 return (int)ceil(m_adjust
->value
);
173 void wxSpinButton::SetValue( int value
)
175 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
177 float fpos
= (float)value
;
179 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
181 m_adjust
->value
= fpos
;
183 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
186 void wxSpinButton::SetRange(int minVal
, int maxVal
)
188 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
190 float fmin
= (float)minVal
;
191 float fmax
= (float)maxVal
;
193 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
194 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
199 m_adjust
->lower
= fmin
;
200 m_adjust
->upper
= fmax
;
202 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
204 // these two calls are required due to some bug in GTK
209 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
211 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
213 m_width
= DoGetBestSize().x
;
214 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
217 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
219 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
222 wxSize
wxSpinButton::DoGetBestSize() const
224 wxSize
best(15, 26); // FIXME
231 wxSpinButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
233 // TODO: overload to accept functions like gtk_spin_button_new?
234 // Until then use a similar type
235 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);
238 #endif // wxUSE_SPINBTN