]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinbutt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/spinbutt.cpp
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"
16 #include "wx/spinbutt.h"
23 #include "wx/gtk/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern bool g_blockEventsOnDrag
;
31 static const float sensitivity
= 0.02;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
38 static void gtk_spinbutt_callback( GtkWidget
*WXUNUSED(widget
), wxSpinButton
*win
)
40 if (g_isIdle
) wxapp_install_idle_handler();
42 if (!win
->m_hasVMT
) return;
43 if (g_blockEventsOnDrag
) return;
45 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
46 if (fabs(diff
) < sensitivity
) return;
48 wxEventType command
= wxEVT_NULL
;
50 float line_step
= win
->m_adjust
->step_increment
;
52 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
53 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
54 else command
= wxEVT_SCROLL_THUMBTRACK
;
56 int value
= (int)ceil(win
->m_adjust
->value
);
58 wxSpinEvent
event( command
, win
->GetId());
59 event
.SetPosition( value
);
60 event
.SetEventObject( win
);
62 if ((win
->GetEventHandler()->ProcessEvent( event
)) &&
65 /* program has vetoed */
66 win
->m_adjust
->value
= win
->m_oldPos
;
68 g_signal_handlers_disconnect_by_func (win
->m_adjust
,
69 (gpointer
) gtk_spinbutt_callback
,
72 g_signal_emit_by_name (win
->m_adjust
, "value_changed");
74 g_signal_connect (win
->m_adjust
, "value_changed",
75 G_CALLBACK (gtk_spinbutt_callback
), win
);
79 win
->m_oldPos
= win
->m_adjust
->value
;
81 /* always send a thumbtrack event */
82 if (command
!= wxEVT_SCROLL_THUMBTRACK
)
84 command
= wxEVT_SCROLL_THUMBTRACK
;
85 wxSpinEvent
event2( command
, win
->GetId());
86 event2
.SetPosition( value
);
87 event2
.SetEventObject( win
);
88 win
->GetEventHandler()->ProcessEvent( event2
);
93 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
97 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
)
98 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxNotifyEvent
)
100 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
101 EVT_SIZE(wxSpinButton::OnSize
)
104 bool wxSpinButton::Create(wxWindow
*parent
,
109 const wxString
& name
)
113 wxSize new_size
= size
,
114 sizeBest
= DoGetBestSize();
115 new_size
.x
= sizeBest
.x
; // override width always
116 if (new_size
.y
== -1)
117 new_size
.y
= sizeBest
.y
;
119 if (!PreCreation( parent
, pos
, new_size
) ||
120 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
122 wxFAIL_MSG( wxT("wxXX creation failed") );
128 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
130 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
132 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
133 (int)(m_windowStyle
& wxSP_WRAP
) );
135 g_signal_connect (m_adjust
, "value_changed",
136 G_CALLBACK (gtk_spinbutt_callback
), this);
138 m_parent
->DoAddChild( this );
140 PostCreation(new_size
);
145 int wxSpinButton::GetMin() const
147 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
149 return (int)ceil(m_adjust
->lower
);
152 int wxSpinButton::GetMax() const
154 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
156 return (int)ceil(m_adjust
->upper
);
159 int wxSpinButton::GetValue() const
161 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
163 return (int)ceil(m_adjust
->value
);
166 void wxSpinButton::SetValue( int value
)
168 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
170 float fpos
= (float)value
;
172 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
174 m_adjust
->value
= fpos
;
176 g_signal_emit_by_name (m_adjust
, "value_changed");
179 void wxSpinButton::SetRange(int minVal
, int maxVal
)
181 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
183 float fmin
= (float)minVal
;
184 float fmax
= (float)maxVal
;
186 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
187 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
192 m_adjust
->lower
= fmin
;
193 m_adjust
->upper
= fmax
;
195 g_signal_emit_by_name (m_adjust
, "changed");
197 // these two calls are required due to some bug in GTK
202 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
204 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
206 m_width
= DoGetBestSize().x
;
207 gtk_widget_set_size_request( m_widget
, m_width
, m_height
);
210 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
212 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
215 wxSize
wxSpinButton::DoGetBestSize() const
217 wxSize
best(15, 26); // FIXME
224 wxSpinButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
226 // TODO: overload to accept functions like gtk_spin_button_new?
227 // Until then use a similar type
228 return GetDefaultAttributesFromGTKWidget(gtk_button_new
);