]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk1/spinbutt.cpp
1901da14bfae646b99610b6e5c793629e0837b2d
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSpinButton
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "spinbutt.h"
15 #include "wx/spinbutt.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
38 static const float sensitivity
= 0.2;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 static void gtk_spinbutt_callback( GtkWidget
*WXUNUSED(widget
), wxSpinButton
*win
)
46 if (g_isIdle
) wxapp_install_idle_handler();
48 if (!win
->m_hasVMT
) return;
49 if (g_blockEventsOnDrag
) return;
51 float diff
= win
->m_adjust
->value
- win
->m_oldPos
;
52 if (fabs(diff
) < sensitivity
) return;
53 win
->m_oldPos
= win
->m_adjust
->value
;
55 wxEventType command
= wxEVT_NULL
;
57 float line_step
= win
->m_adjust
->step_increment
;
59 if (fabs(diff
-line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEDOWN
;
60 else if (fabs(diff
+line_step
) < sensitivity
) command
= wxEVT_SCROLL_LINEUP
;
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
);
68 win
->GetEventHandler()->ProcessEvent( event
);
70 /* always send a thumbtrack event */
71 if (command
!= wxEVT_SCROLL_THUMBTRACK
)
73 command
= wxEVT_SCROLL_THUMBTRACK
;
74 wxSpinEvent
event2( command
, win
->GetId());
75 event2
.SetPosition( value
);
76 event2
.SetEventObject( win
);
77 win
->GetEventHandler()->ProcessEvent( event2
);
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
85 IMPLEMENT_DYNAMIC_CLASS(wxSpinButton
,wxControl
)
86 IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent
, wxScrollEvent
);
88 BEGIN_EVENT_TABLE(wxSpinButton
, wxControl
)
89 EVT_SIZE(wxSpinButton::OnSize
)
92 bool wxSpinButton::Create(wxWindow
*parent
,
101 wxSize new_size
= size
;
103 if (new_size
.y
== -1)
106 if (!PreCreation( parent
, pos
, new_size
) ||
107 !CreateBase( parent
, id
, pos
, new_size
, style
, wxDefaultValidator
, name
))
109 wxFAIL_MSG( wxT("wxXX creation failed") );
115 m_adjust
= (GtkAdjustment
*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0);
117 m_widget
= gtk_spin_button_new( m_adjust
, 0, 0 );
119 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget
), (m_windowStyle
& wxSP_WRAP
) );
121 gtk_signal_connect( GTK_OBJECT (m_adjust
),
123 (GtkSignalFunc
) gtk_spinbutt_callback
,
126 m_parent
->DoAddChild( this );
130 SetBackgroundColour( parent
->GetBackgroundColour() );
137 wxSpinButton::~wxSpinButton()
141 int wxSpinButton::GetMin() const
143 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
145 return (int)ceil(m_adjust
->lower
);
148 int wxSpinButton::GetMax() const
150 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
152 return (int)ceil(m_adjust
->upper
);
155 int wxSpinButton::GetValue() const
157 wxCHECK_MSG( (m_widget
!= NULL
), 0, wxT("invalid spin button") );
159 return (int)ceil(m_adjust
->value
);
162 void wxSpinButton::SetValue( int value
)
164 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
166 float fpos
= (float)value
;
168 if (fabs(fpos
-m_adjust
->value
) < sensitivity
) return;
170 m_adjust
->value
= fpos
;
172 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "value_changed" );
175 void wxSpinButton::SetRange(int minVal
, int maxVal
)
177 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
179 float fmin
= (float)minVal
;
180 float fmax
= (float)maxVal
;
182 if ((fabs(fmin
-m_adjust
->lower
) < sensitivity
) &&
183 (fabs(fmax
-m_adjust
->upper
) < sensitivity
))
188 m_adjust
->lower
= fmin
;
189 m_adjust
->upper
= fmax
;
191 gtk_signal_emit_by_name( GTK_OBJECT(m_adjust
), "changed" );
193 // these two calls are required due to some bug in GTK
198 void wxSpinButton::OnSize( wxSizeEvent
&WXUNUSED(event
) )
200 wxCHECK_RET( (m_widget
!= NULL
), wxT("invalid spin button") );
203 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
206 bool wxSpinButton::IsOwnGtkWindow( GdkWindow
*window
)
208 return GTK_SPIN_BUTTON(m_widget
)->panel
== window
;
211 void wxSpinButton::ApplyWidgetStyle()
214 gtk_widget_set_style( m_widget
, m_widgetStyle
);