]>
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 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "spinbutt.h"
15 #include "wx/spinbutt.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;
54 win
->m_oldPos
= win
->m_adjust
->value
;
56 wxEventType command
= wxEVT_NULL
;
58 float line_step
= win
->m_adjust
->step_increment
;
60 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
61 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
62 else command
= wxEVT_SCROLL_THUMBTRACK
;
64 int value
= (int)ceil(win
->m_adjust
->value
);
66 wxSpinEvent
event( command
, win
->GetId());
67 event
.SetPosition( value
);
68 event
.SetEventObject( win
);
69 win
->GetEventHandler()->ProcessEvent( event
);
71 /* always send a thumbtrack event */
72 if (command
!= wxEVT_SCROLL_THUMBTRACK
)
74 command
= wxEVT_SCROLL_THUMBTRACK
;
75 wxSpinEvent
event2( command
, win
->GetId());
76 event2
.SetPosition( value
);
77 event2
.SetEventObject( win
);
78 win
->GetEventHandler()->ProcessEvent( event2
);
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
)
87 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxScrollEvent
);
89 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
90 EVT_SIZE(wxSpinButton::OnSize
)
93 bool wxSpinButton::Create(wxWindow
*parent
,
102 wxSize new_size
= size
,
103 sizeBest
= DoGetBestSize();
104 new_size
.x
= sizeBest
.x
; // override width always
105 if (new_size
.y
== -1)
106 new_size
.y
= sizeBest
.y
;
108 if (!PreCreation( parent
, pos
, new_size
) ||
109 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
111 wxFAIL_MSG( wxT("wxXX creation failed") );
117 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
119 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
121 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
),
122 (int)(m_windowStyle
& wxSP_WRAP
) );
124 gtk_signal_connect( GTK_OBJECT (m_adjust
),
126 (GtkSignalFunc
) gtk_spinbutt_callback
,
129 m_parent
->DoAddChild( this );
133 SetBackgroundColour( parent
->GetBackgroundColour() );
140 int wxSpinButton::GetMin() const
142 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
144 return (int)ceil(m_adjust
->lower
);
147 int wxSpinButton::GetMax() const
149 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
151 return (int)ceil(m_adjust
->upper
);
154 int wxSpinButton::GetValue() const
156 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
158 return (int)ceil(m_adjust
->value
);
161 void wxSpinButton::SetValue( int value
)
163 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
165 float fpos
= (float)value
;
167 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
169 m_adjust
->value
= fpos
;
171 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
174 void wxSpinButton::SetRange(int minVal
, int maxVal
)
176 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
178 float fmin
= (float)minVal
;
179 float fmax
= (float)maxVal
;
181 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
182 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
187 m_adjust
->lower
= fmin
;
188 m_adjust
->upper
= fmax
;
190 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
192 // these two calls are required due to some bug in GTK
197 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
199 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
201 m_width
= DoGetBestSize().x
;
202 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
205 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
207 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
210 void wxSpinButton::ApplyWidgetStyle()
213 gtk_widget_set_style( m_widget
, m_widgetStyle
);
216 wxSize
wxSpinButton::DoGetBestSize() const
218 return wxSize(15, 26);