]>
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 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #include "wx/spinbutt.h"
20 #include "wx/gtk1/private.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern bool g_blockEventsOnDrag
;
35 static const float sensitivity
= 0.02;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
42 static void gtk_spinbutt_callback( GtkWidget
*WXUNUSED(widget
), wxSpinButton
*win
)
44 if (g_isIdle
) wxapp_install_idle_handler();
46 if (!win
->m_hasVMT
) return;
47 if (g_blockEventsOnDrag
) return;
49 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
50 if (fabs(diff
) < sensitivity
) return;
52 wxEventType command
= wxEVT_NULL
;
54 float line_step
= win
->m_adjust
->step_increment
;
56 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
57 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
58 else command
= wxEVT_SCROLL_THUMBTRACK
;
60 int value
= (int)ceil(win
->m_adjust
->value
);
62 wxSpinEvent
event( command
, win
->GetId());
63 event
.SetPosition( value
);
64 event
.SetEventObject( win
);
66 if ((win
->GetEventHandler()->ProcessEvent( event
)) &&
69 /* program has vetoed */
70 win
->m_adjust
->value
= win
->m_oldPos
;
72 gtk_signal_disconnect_by_func( GTK_OBJECT (win
->m_adjust
),
73 (GtkSignalFunc
) gtk_spinbutt_callback
,
76 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
78 gtk_signal_connect( GTK_OBJECT (win
->m_adjust
),
80 (GtkSignalFunc
) gtk_spinbutt_callback
,
85 win
->m_oldPos
= win
->m_adjust
->value
;
87 /* always send a thumbtrack event */
88 if (command
!= wxEVT_SCROLL_THUMBTRACK
)
90 command
= wxEVT_SCROLL_THUMBTRACK
;
91 wxSpinEvent
event2( command
, win
->GetId());
92 event2
.SetPosition( value
);
93 event2
.SetEventObject( win
);
94 win
->GetEventHandler()->ProcessEvent( event2
);
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
103 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
)
104 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxNotifyEvent
)
106 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
107 EVT_SIZE(wxSpinButton::OnSize
)
110 bool wxSpinButton::Create(wxWindow
*parent
,
115 const wxString
& name
)
119 wxSize new_size
= size
,
120 sizeBest
= DoGetBestSize();
121 new_size
.x
= sizeBest
.x
; // override width always
122 if (new_size
.y
== -1)
123 new_size
.y
= sizeBest
.y
;
125 if (!PreCreation( parent
, pos
, new_size
) ||
126 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
128 wxFAIL_MSG( wxT("wxXX creation failed") );
134 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
136 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
138 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
139 (int)(m_windowStyle
& wxSP_WRAP
) );
141 gtk_signal_connect( GTK_OBJECT (m_adjust
),
143 (GtkSignalFunc
) gtk_spinbutt_callback
,
146 m_parent
->DoAddChild( this );
148 PostCreation(new_size
);
153 int wxSpinButton::GetMin() const
155 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
157 return (int)ceil(m_adjust
->lower
);
160 int wxSpinButton::GetMax() const
162 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
164 return (int)ceil(m_adjust
->upper
);
167 int wxSpinButton::GetValue() const
169 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
171 return (int)ceil(m_adjust
->value
);
174 void wxSpinButton::SetValue( int value
)
176 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
178 float fpos
= (float)value
;
180 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
182 m_adjust
->value
= fpos
;
184 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
187 void wxSpinButton::SetRange(int minVal
, int maxVal
)
189 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
191 float fmin
= (float)minVal
;
192 float fmax
= (float)maxVal
;
194 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
195 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
200 m_adjust
->lower
= fmin
;
201 m_adjust
->upper
= fmax
;
203 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
205 // these two calls are required due to some bug in GTK
210 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
212 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
214 m_width
= DoGetBestSize().x
;
215 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
218 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
220 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
223 wxSize
wxSpinButton::DoGetBestSize() const
225 wxSize
best(15, 26); // FIXME
232 wxSpinButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
234 // TODO: overload to accept functions like gtk_spin_button_new?
235 // Until then use a similar type
236 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);