]>
Commit | Line | Data |
---|---|---|
8e189077 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: spinbutt.cpp | |
3 | // Purpose: wxSpinButton | |
4 | // Author: Robert | |
5 | // Modified by: | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Robert Roebling | |
6380910c | 8 | // Licence: wxWindows licence |
8e189077 RR |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
14f355c2 | 11 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
510fc784 RR |
12 | #pragma implementation "spinbutt.h" |
13 | #pragma implementation "spinbutbase.h" | |
8e189077 RR |
14 | #endif |
15 | ||
14f355c2 VS |
16 | // For compilers that support precompilation, includes "wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
8e189077 | 19 | #include "wx/spinbutt.h" |
dcf924a3 | 20 | |
2fa7c206 | 21 | #if wxUSE_SPINBTN |
dcf924a3 | 22 | |
8e189077 | 23 | #include "wx/utils.h" |
aec0ed2e | 24 | |
f5368809 | 25 | #include <math.h> |
8e189077 | 26 | |
9e691f46 | 27 | #include "wx/gtk/private.h" |
83624f79 | 28 | |
acfd422a RR |
29 | //----------------------------------------------------------------------------- |
30 | // idle system | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
33 | extern void wxapp_install_idle_handler(); | |
34 | extern bool g_isIdle; | |
35 | ||
8e189077 RR |
36 | //----------------------------------------------------------------------------- |
37 | // data | |
38 | //----------------------------------------------------------------------------- | |
39 | ||
40 | extern bool g_blockEventsOnDrag; | |
41 | ||
738f9e5a | 42 | static const float sensitivity = 0.02; |
6380910c | 43 | |
8e189077 RR |
44 | //----------------------------------------------------------------------------- |
45 | // "value_changed" | |
46 | //----------------------------------------------------------------------------- | |
47 | ||
48 | static void gtk_spinbutt_callback( GtkWidget *WXUNUSED(widget), wxSpinButton *win ) | |
6380910c | 49 | { |
acfd422a RR |
50 | if (g_isIdle) wxapp_install_idle_handler(); |
51 | ||
a2053b27 | 52 | if (!win->m_hasVMT) return; |
83624f79 | 53 | if (g_blockEventsOnDrag) return; |
828f655f | 54 | |
83624f79 | 55 | float diff = win->m_adjust->value - win->m_oldPos; |
6380910c | 56 | if (fabs(diff) < sensitivity) return; |
6380910c | 57 | |
83624f79 | 58 | wxEventType command = wxEVT_NULL; |
6380910c | 59 | |
83624f79 | 60 | float line_step = win->m_adjust->step_increment; |
6380910c | 61 | |
0f42a871 RR |
62 | if (fabs(diff-line_step) < sensitivity) command = wxEVT_SCROLL_LINEUP; |
63 | else if (fabs(diff+line_step) < sensitivity) command = wxEVT_SCROLL_LINEDOWN; | |
83624f79 | 64 | else command = wxEVT_SCROLL_THUMBTRACK; |
8e189077 | 65 | |
66d78146 | 66 | int value = (int)ceil(win->m_adjust->value); |
6380910c | 67 | |
83624f79 RR |
68 | wxSpinEvent event( command, win->GetId()); |
69 | event.SetPosition( value ); | |
83624f79 | 70 | event.SetEventObject( win ); |
0f42a871 RR |
71 | |
72 | if ((win->GetEventHandler()->ProcessEvent( event )) && | |
73 | !event.IsAllowed() ) | |
74 | { | |
75 | /* program has vetoed */ | |
76 | win->m_adjust->value = win->m_oldPos; | |
77 | ||
78 | gtk_signal_disconnect_by_func( GTK_OBJECT (win->m_adjust), | |
79 | (GtkSignalFunc) gtk_spinbutt_callback, | |
80 | (gpointer) win ); | |
81 | ||
82 | gtk_signal_emit_by_name( GTK_OBJECT(win->m_adjust), "value_changed" ); | |
83 | ||
84 | gtk_signal_connect( GTK_OBJECT (win->m_adjust), | |
85 | "value_changed", | |
86 | (GtkSignalFunc) gtk_spinbutt_callback, | |
87 | (gpointer) win ); | |
88 | return; | |
89 | } | |
90 | ||
91 | win->m_oldPos = win->m_adjust->value; | |
e65cc56a RR |
92 | |
93 | /* always send a thumbtrack event */ | |
94 | if (command != wxEVT_SCROLL_THUMBTRACK) | |
95 | { | |
96 | command = wxEVT_SCROLL_THUMBTRACK; | |
97 | wxSpinEvent event2( command, win->GetId()); | |
98 | event2.SetPosition( value ); | |
99 | event2.SetEventObject( win ); | |
100 | win->GetEventHandler()->ProcessEvent( event2 ); | |
101 | } | |
8e189077 RR |
102 | } |
103 | ||
104 | //----------------------------------------------------------------------------- | |
105 | // wxSpinButton | |
106 | //----------------------------------------------------------------------------- | |
107 | ||
108 | IMPLEMENT_DYNAMIC_CLASS(wxSpinButton,wxControl) | |
2fa7c206 | 109 | IMPLEMENT_DYNAMIC_CLASS(wxSpinEvent, wxNotifyEvent) |
8e189077 RR |
110 | |
111 | BEGIN_EVENT_TABLE(wxSpinButton, wxControl) | |
112 | EVT_SIZE(wxSpinButton::OnSize) | |
113 | END_EVENT_TABLE() | |
114 | ||
31528cd3 VZ |
115 | bool wxSpinButton::Create(wxWindow *parent, |
116 | wxWindowID id, | |
117 | const wxPoint& pos, | |
118 | const wxSize& size, | |
119 | long style, | |
120 | const wxString& name) | |
8e189077 | 121 | { |
83624f79 | 122 | m_needParent = TRUE; |
6380910c | 123 | |
9d9b7755 VZ |
124 | wxSize new_size = size, |
125 | sizeBest = DoGetBestSize(); | |
126 | new_size.x = sizeBest.x; // override width always | |
6380910c | 127 | if (new_size.y == -1) |
9d9b7755 | 128 | new_size.y = sizeBest.y; |
6380910c | 129 | |
2259e007 | 130 | if (!PreCreation( parent, pos, new_size ) || |
4dcaf11a RR |
131 | !CreateBase( parent, id, pos, new_size, style, wxDefaultValidator, name )) |
132 | { | |
223d09f6 | 133 | wxFAIL_MSG( wxT("wxXX creation failed") ); |
0f42a871 | 134 | return FALSE; |
4dcaf11a | 135 | } |
8e189077 | 136 | |
83624f79 | 137 | m_oldPos = 0.0; |
8e189077 | 138 | |
83624f79 | 139 | m_adjust = (GtkAdjustment*) gtk_adjustment_new( 0.0, 0.0, 100.0, 1.0, 5.0, 0.0); |
6380910c | 140 | |
83624f79 | 141 | m_widget = gtk_spin_button_new( m_adjust, 0, 0 ); |
6380910c | 142 | |
b02da6b1 VZ |
143 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), |
144 | (int)(m_windowStyle & wxSP_WRAP) ); | |
6380910c VZ |
145 | |
146 | gtk_signal_connect( GTK_OBJECT (m_adjust), | |
83624f79 | 147 | "value_changed", |
6380910c VZ |
148 | (GtkSignalFunc) gtk_spinbutt_callback, |
149 | (gpointer) this ); | |
150 | ||
f03fc89f | 151 | m_parent->DoAddChild( this ); |
6380910c | 152 | |
83624f79 | 153 | PostCreation(); |
e8e24dfa | 154 | InheritAttributes(); |
8e189077 | 155 | |
83624f79 | 156 | Show( TRUE ); |
6380910c | 157 | |
83624f79 | 158 | return TRUE; |
8e189077 RR |
159 | } |
160 | ||
8e189077 RR |
161 | int wxSpinButton::GetMin() const |
162 | { | |
223d09f6 | 163 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); |
6380910c | 164 | |
66d78146 | 165 | return (int)ceil(m_adjust->lower); |
8e189077 RR |
166 | } |
167 | ||
168 | int wxSpinButton::GetMax() const | |
169 | { | |
223d09f6 | 170 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); |
6380910c | 171 | |
66d78146 | 172 | return (int)ceil(m_adjust->upper); |
8e189077 RR |
173 | } |
174 | ||
175 | int wxSpinButton::GetValue() const | |
176 | { | |
223d09f6 | 177 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); |
6380910c | 178 | |
66d78146 | 179 | return (int)ceil(m_adjust->value); |
8e189077 RR |
180 | } |
181 | ||
182 | void wxSpinButton::SetValue( int value ) | |
183 | { | |
223d09f6 | 184 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); |
6380910c | 185 | |
83624f79 RR |
186 | float fpos = (float)value; |
187 | m_oldPos = fpos; | |
6380910c VZ |
188 | if (fabs(fpos-m_adjust->value) < sensitivity) return; |
189 | ||
83624f79 | 190 | m_adjust->value = fpos; |
6380910c | 191 | |
83624f79 | 192 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
8e189077 RR |
193 | } |
194 | ||
195 | void wxSpinButton::SetRange(int minVal, int maxVal) | |
196 | { | |
223d09f6 | 197 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); |
6380910c | 198 | |
83624f79 RR |
199 | float fmin = (float)minVal; |
200 | float fmax = (float)maxVal; | |
6380910c VZ |
201 | |
202 | if ((fabs(fmin-m_adjust->lower) < sensitivity) && | |
203 | (fabs(fmax-m_adjust->upper) < sensitivity)) | |
83624f79 RR |
204 | { |
205 | return; | |
206 | } | |
6380910c | 207 | |
83624f79 RR |
208 | m_adjust->lower = fmin; |
209 | m_adjust->upper = fmax; | |
8e189077 | 210 | |
83624f79 | 211 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); |
65045edd RR |
212 | |
213 | // these two calls are required due to some bug in GTK | |
214 | Refresh(); | |
215 | SetFocus(); | |
8e189077 RR |
216 | } |
217 | ||
218 | void wxSpinButton::OnSize( wxSizeEvent &WXUNUSED(event) ) | |
219 | { | |
223d09f6 | 220 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); |
6380910c | 221 | |
9d9b7755 | 222 | m_width = DoGetBestSize().x; |
83624f79 | 223 | gtk_widget_set_usize( m_widget, m_width, m_height ); |
8e189077 RR |
224 | } |
225 | ||
226 | bool wxSpinButton::IsOwnGtkWindow( GdkWindow *window ) | |
227 | { | |
83624f79 | 228 | return GTK_SPIN_BUTTON(m_widget)->panel == window; |
8e189077 RR |
229 | } |
230 | ||
231 | void wxSpinButton::ApplyWidgetStyle() | |
232 | { | |
83624f79 RR |
233 | SetWidgetStyle(); |
234 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
8e189077 RR |
235 | } |
236 | ||
9d9b7755 VZ |
237 | wxSize wxSpinButton::DoGetBestSize() const |
238 | { | |
239 | return wxSize(15, 26); | |
240 | } | |
241 | ||
31528cd3 | 242 | #endif |