]>
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 #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"
25 #include "wx/gtk/private.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
40 static const float sensitivity
= 0.02;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
47 static void gtk_spinbutt_callback( GtkWidget
*WXUNUSED(widget
), wxSpinButton
*win
)
49 if (g_isIdle
) wxapp_install_idle_handler();
51 if (!win
->m_hasVMT
) return;
52 if (g_blockEventsOnDrag
) return;
54 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
55 if (fabs(diff
) < sensitivity
) return;
57 wxEventType command
= wxEVT_NULL
;
59 float line_step
= win
->m_adjust
->step_increment
;
61 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
62 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
63 else command
= wxEVT_SCROLL_THUMBTRACK
;
65 int value
= (int)ceil(win
->m_adjust
->value
);
67 wxSpinEvent
event( command
, win
->GetId());
68 event
.SetPosition( value
);
69 event
.SetEventObject( win
);
71 if ((win
->GetEventHandler()->ProcessEvent( event
)) &&
74 /* program has vetoed */
75 win
->m_adjust
->value
= win
->m_oldPos
;
77 gtk_signal_disconnect_by_func( GTK_OBJECT (win
->m_adjust
),
78 (GtkSignalFunc
) gtk_spinbutt_callback
,
81 gtk_signal_emit_by_name( GTK_OBJECT(win
->m_adjust
), "value_changed" );
83 gtk_signal_connect( GTK_OBJECT (win
->m_adjust
),
85 (GtkSignalFunc
) gtk_spinbutt_callback
,
90 win
->m_oldPos
= win
->m_adjust
->value
;
92 /* always send a thumbtrack event */
93 if (command
!= wxEVT_SCROLL_THUMBTRACK
)
95 command
= wxEVT_SCROLL_THUMBTRACK
;
96 wxSpinEvent
event2( command
, win
->GetId());
97 event2
.SetPosition( value
);
98 event2
.SetEventObject( win
);
99 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 );
153 PostCreation(new_size
);
158 int wxSpinButton::GetMin() const
160 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
162 return (int)ceil(m_adjust
->lower
);
165 int wxSpinButton::GetMax() const
167 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
169 return (int)ceil(m_adjust
->upper
);
172 int wxSpinButton::GetValue() const
174 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
176 return (int)ceil(m_adjust
->value
);
179 void wxSpinButton::SetValue( int value
)
181 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
183 float fpos
= (float)value
;
185 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
187 m_adjust
->value
= fpos
;
189 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
192 void wxSpinButton::SetRange(int minVal
, int maxVal
)
194 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
196 float fmin
= (float)minVal
;
197 float fmax
= (float)maxVal
;
199 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
200 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
205 m_adjust
->lower
= fmin
;
206 m_adjust
->upper
= fmax
;
208 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
210 // these two calls are required due to some bug in GTK
215 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
217 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
219 m_width
= DoGetBestSize().x
;
220 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
223 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
225 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
228 wxSize
wxSpinButton::DoGetBestSize() const
230 wxSize
best(15, 26); // FIXME
237 wxSpinButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
239 // TODO: overload to accept functions like gtk_spin_button_new?
240 // Until then use a similar type
241 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);