]>
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 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "spinbutt.h"
13 #pragma implementation "spinbutbase.h"
16 #include "wx/spinbutt.h"
24 #include "wx/gtk/private.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_spinbutt_callback( GtkWidget
*WXUNUSED(widget
), wxSpinButton
*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;
55 wxEventType command
= wxEVT_NULL
;
57 float line_step
= win
->m_adjust
->step_increment
;
59 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
60 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
61 else command
= wxEVT_SCROLL_THUMBTRACK
;
63 int value
= (int)ceil(win
->m_adjust
->value
);
65 wxSpinEvent
event( command
, win
->GetId());
66 event
.SetPosition( value
);
67 event
.SetEventObject( win
);
69 if ((win
->GetEventHandler()->ProcessEvent( event
)) &&
72 /* program has vetoed */
73 win
->m_adjust
->value
= win
->m_oldPos
;
75 gtk_signal_disconnect_by_func( GTK_OBJECT (win
->m_adjust
),
76 (GtkSignalFunc
) gtk_spinbutt_callback
,
79 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
81 gtk_signal_connect( GTK_OBJECT (win
->m_adjust
),
83 (GtkSignalFunc
) gtk_spinbutt_callback
,
88 win
->m_oldPos
= win
->m_adjust
->value
;
90 /* always send a thumbtrack event */
91 if (command
!= wxEVT_SCROLL_THUMBTRACK
)
93 command
= wxEVT_SCROLL_THUMBTRACK
;
94 wxSpinEvent
event2( command
, win
->GetId());
95 event2
.SetPosition( value
);
96 event2
.SetEventObject( win
);
97 win
->GetEventHandler()->ProcessEvent( event2
);
101 //-----------------------------------------------------------------------------
103 //-----------------------------------------------------------------------------
105 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
)
106 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxNotifyEvent
)
108 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
109 EVT_SIZE(wxSpinButton::OnSize
)
112 bool wxSpinButton::Create(wxWindow
*parent
,
117 const wxString
& name
)
121 wxSize new_size
= size
,
122 sizeBest
= DoGetBestSize();
123 new_size
.x
= sizeBest
.x
; // override width always
124 if (new_size
.y
== -1)
125 new_size
.y
= sizeBest
.y
;
127 if (!PreCreation( parent
, pos
, new_size
) ||
128 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
130 wxFAIL_MSG( wxT("wxXX creation failed") );
136 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
138 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
140 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
141 (int)(m_windowStyle
& wxSP_WRAP
) );
143 gtk_signal_connect( GTK_OBJECT (m_adjust
),
145 (GtkSignalFunc
) gtk_spinbutt_callback
,
148 m_parent
->DoAddChild( this );
152 SetBackgroundColour( parent
->GetBackgroundColour() );
159 int wxSpinButton::GetMin() const
161 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
163 return (int)ceil(m_adjust
->lower
);
166 int wxSpinButton::GetMax() const
168 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
170 return (int)ceil(m_adjust
->upper
);
173 int wxSpinButton::GetValue() const
175 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
177 return (int)ceil(m_adjust
->value
);
180 void wxSpinButton::SetValue( int value
)
182 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
184 float fpos
= (float)value
;
186 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
188 m_adjust
->value
= fpos
;
190 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
193 void wxSpinButton::SetRange(int minVal
, int maxVal
)
195 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
197 float fmin
= (float)minVal
;
198 float fmax
= (float)maxVal
;
200 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
201 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
206 m_adjust
->lower
= fmin
;
207 m_adjust
->upper
= fmax
;
209 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
211 // these two calls are required due to some bug in GTK
216 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
218 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
220 m_width
= DoGetBestSize().x
;
221 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
224 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
226 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
229 void wxSpinButton::ApplyWidgetStyle()
232 gtk_widget_set_style( m_widget
, m_widgetStyle
);
235 wxSize
wxSpinButton::DoGetBestSize() const
237 return wxSize(15, 26);