]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinbutt.cpp
899c30c81684000fd9d5bdfce05a82bf71ea1de1
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSpinButton
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "spinbutt.h"
13 #pragma implementation "spinbutbase.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #include "wx/spinbutt.h"
27 #include "wx/gtk/private.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern void wxapp_install_idle_handler();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern bool g_blockEventsOnDrag
;
42 static const float sensitivity
= 0.02;
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 static void gtk_spinbutt_callback( GtkWidget
*WXUNUSED(widget
), wxSpinButton
*win
)
50 if (g_isIdle
) wxapp_install_idle_handler();
52 if (!win
->m_hasVMT
) return;
53 if (g_blockEventsOnDrag
) return;
55 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
56 if (fabs(diff
) < sensitivity
) return;
58 wxEventType command
= wxEVT_NULL
;
60 float line_step
= win
->m_adjust
->step_increment
;
62 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
63 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
64 else command
= wxEVT_SCROLL_THUMBTRACK
;
66 int value
= (int)ceil(win
->m_adjust
->value
);
68 wxSpinEvent
event( command
, win
->GetId());
69 event
.SetPosition( value
);
70 event
.SetEventObject( win
);
72 if ((win
->GetEventHandler()->ProcessEvent( event
)) &&
75 /* program has vetoed */
76 win
->m_adjust
->value
= win
->m_oldPos
;
78 gtk_signal_disconnect_by_func( GTK_OBJECT (win
->m_adjust
),
79 (GtkSignalFunc
) gtk_spinbutt_callback
,
82 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
84 gtk_signal_connect( GTK_OBJECT (win
->m_adjust
),
86 (GtkSignalFunc
) gtk_spinbutt_callback
,
91 win
->m_oldPos
= win
->m_adjust
->value
;
93 /* always send a thumbtrack event */
94 if (command
!= wxEVT_SCROLL_THUMBTRACK
)
96 command
= wxEVT_SCROLL_THUMBTRACK
;
97 wxSpinEvent
event2( command
, win
->GetId());
98 event2
.SetPosition( value
);
99 event2
.SetEventObject( win
);
100 win
->GetEventHandler()->ProcessEvent( event2
);
104 //-----------------------------------------------------------------------------
106 //-----------------------------------------------------------------------------
108 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
)
109 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxNotifyEvent
)
111 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
112 EVT_SIZE(wxSpinButton::OnSize
)
115 bool wxSpinButton::Create(wxWindow
*parent
,
120 const wxString
& name
)
124 wxSize new_size
= size
,
125 sizeBest
= DoGetBestSize();
126 new_size
.x
= sizeBest
.x
; // override width always
127 if (new_size
.y
== -1)
128 new_size
.y
= sizeBest
.y
;
130 if (!PreCreation( parent
, pos
, new_size
) ||
131 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
133 wxFAIL_MSG( wxT("wxXX creation failed") );
139 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
141 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
143 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
144 (int)(m_windowStyle
& wxSP_WRAP
) );
146 gtk_signal_connect( GTK_OBJECT (m_adjust
),
148 (GtkSignalFunc
) gtk_spinbutt_callback
,
151 m_parent
->DoAddChild( this );
155 SetBackgroundColour( parent
->GetBackgroundColour() );
162 int wxSpinButton::GetMin() const
164 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
166 return (int)ceil(m_adjust
->lower
);
169 int wxSpinButton::GetMax() const
171 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
173 return (int)ceil(m_adjust
->upper
);
176 int wxSpinButton::GetValue() const
178 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
180 return (int)ceil(m_adjust
->value
);
183 void wxSpinButton::SetValue( int value
)
185 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
187 float fpos
= (float)value
;
189 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
191 m_adjust
->value
= fpos
;
193 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
196 void wxSpinButton::SetRange(int minVal
, int maxVal
)
198 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
200 float fmin
= (float)minVal
;
201 float fmax
= (float)maxVal
;
203 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
204 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
209 m_adjust
->lower
= fmin
;
210 m_adjust
->upper
= fmax
;
212 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
214 // these two calls are required due to some bug in GTK
219 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
221 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
223 m_width
= DoGetBestSize().x
;
224 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
227 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
229 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
232 void wxSpinButton::ApplyWidgetStyle()
235 gtk_widget_set_style( m_widget
, m_widgetStyle
);
238 wxSize
wxSpinButton::DoGetBestSize() const
240 return wxSize(15, 26);