]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/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/gtk/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 g_signal_handlers_disconnect_by_func (win
->m_adjust
,
73 (gpointer
) gtk_spinbutt_callback
,
76 g_signal_emit_by_name (win
->m_adjust
, "value_changed");
78 g_signal_connect (win
->m_adjust
, "value_changed",
79 G_CALLBACK (gtk_spinbutt_callback
), win
);
83 win
->m_oldPos
= win
->m_adjust
->value
;
85 /* always send a thumbtrack event */
86 if (command
!= wxEVT_SCROLL_THUMBTRACK
)
88 command
= wxEVT_SCROLL_THUMBTRACK
;
89 wxSpinEvent
event2( command
, win
->GetId());
90 event2
.SetPosition( value
);
91 event2
.SetEventObject( win
);
92 win
->GetEventHandler()->ProcessEvent( event2
);
97 //-----------------------------------------------------------------------------
99 //-----------------------------------------------------------------------------
101 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
)
102 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxNotifyEvent
)
104 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
105 EVT_SIZE(wxSpinButton::OnSize
)
108 bool wxSpinButton::Create(wxWindow
*parent
,
113 const wxString
& name
)
117 wxSize new_size
= size
,
118 sizeBest
= DoGetBestSize();
119 new_size
.x
= sizeBest
.x
; // override width always
120 if (new_size
.y
== -1)
121 new_size
.y
= sizeBest
.y
;
123 if (!PreCreation( parent
, pos
, new_size
) ||
124 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
126 wxFAIL_MSG( wxT("wxXX creation failed") );
132 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
134 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
136 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
137 (int)(m_windowStyle
& wxSP_WRAP
) );
139 g_signal_connect (m_adjust
, "value_changed",
140 G_CALLBACK (gtk_spinbutt_callback
), this);
142 m_parent
->DoAddChild( this );
144 PostCreation(new_size
);
149 int wxSpinButton::GetMin() const
151 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
153 return (int)ceil(m_adjust
->lower
);
156 int wxSpinButton::GetMax() const
158 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
160 return (int)ceil(m_adjust
->upper
);
163 int wxSpinButton::GetValue() const
165 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
167 return (int)ceil(m_adjust
->value
);
170 void wxSpinButton::SetValue( int value
)
172 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
174 float fpos
= (float)value
;
176 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
178 m_adjust
->value
= fpos
;
180 g_signal_emit_by_name (m_adjust
, "value_changed");
183 void wxSpinButton::SetRange(int minVal
, int maxVal
)
185 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
187 float fmin
= (float)minVal
;
188 float fmax
= (float)maxVal
;
190 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
191 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
196 m_adjust
->lower
= fmin
;
197 m_adjust
->upper
= fmax
;
199 g_signal_emit_by_name (m_adjust
, "changed");
201 // these two calls are required due to some bug in GTK
206 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
208 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
210 m_width
= DoGetBestSize().x
;
211 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
214 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
216 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
219 wxSize
wxSpinButton::DoGetBestSize() const
221 wxSize
best(15, 26); // FIXME
228 wxSpinButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
230 // TODO: overload to accept functions like gtk_spin_button_new?
231 // Until then use a similar type
232 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);