]>
Commit | Line | Data |
---|---|---|
738f9e5a RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: spinbutt.cpp | |
3 | // Purpose: wxSpinCtrl | |
4 | // Author: Robert | |
5 | // Modified by: | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Robert Roebling | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
14f355c2 | 11 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
738f9e5a RR |
12 | #pragma implementation "spinctrl.h" |
13 | #endif | |
14 | ||
14f355c2 VS |
15 | // For compilers that support precompilation, includes "wx.h". |
16 | #include "wx/wxprec.h" | |
17 | ||
738f9e5a RR |
18 | #include "wx/spinctrl.h" |
19 | ||
0e0d8857 | 20 | #if wxUSE_SPINCTRL |
738f9e5a RR |
21 | |
22 | #include "wx/utils.h" | |
aec0ed2e | 23 | |
78bcfcfc VZ |
24 | #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED |
25 | ||
738f9e5a RR |
26 | #include <math.h> |
27 | ||
e1910715 | 28 | #include "wx/gtk/private.h" |
738f9e5a RR |
29 | |
30 | //----------------------------------------------------------------------------- | |
31 | // idle system | |
32 | //----------------------------------------------------------------------------- | |
33 | ||
34 | extern void wxapp_install_idle_handler(); | |
35 | extern bool g_isIdle; | |
36 | ||
0c623889 RR |
37 | static const float sensitivity = 0.02; |
38 | ||
738f9e5a RR |
39 | //----------------------------------------------------------------------------- |
40 | // data | |
41 | //----------------------------------------------------------------------------- | |
42 | ||
43 | extern bool g_blockEventsOnDrag; | |
44 | ||
738f9e5a RR |
45 | //----------------------------------------------------------------------------- |
46 | // "value_changed" | |
47 | //----------------------------------------------------------------------------- | |
48 | ||
49 | static void gtk_spinctrl_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win ) | |
50 | { | |
51 | if (g_isIdle) wxapp_install_idle_handler(); | |
52 | ||
53 | if (!win->m_hasVMT) return; | |
54 | if (g_blockEventsOnDrag) return; | |
55 | ||
58c837a4 | 56 | wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId()); |
738f9e5a | 57 | event.SetEventObject( win ); |
4477936a VZ |
58 | |
59 | // note that we don't use wxSpinCtrl::GetValue() here because it would | |
60 | // adjust the value to fit into the control range and this means that we | |
61 | // would never be able to enter an "invalid" value in the control, even | |
62 | // temporarily - and trying to enter 10 into the control which accepts the | |
63 | // values in range 5..50 is then, ummm, quite challenging (hint: you can't | |
64 | // enter 1!) (VZ) | |
65 | event.SetInt( (int)ceil(win->m_adjust->value) ); | |
738f9e5a | 66 | win->GetEventHandler()->ProcessEvent( event ); |
738f9e5a RR |
67 | } |
68 | ||
0a07a7d8 RR |
69 | //----------------------------------------------------------------------------- |
70 | // "changed" | |
71 | //----------------------------------------------------------------------------- | |
72 | ||
73 | static void | |
74 | gtk_spinctrl_text_changed_callback( GtkWidget *WXUNUSED(widget), wxSpinCtrl *win ) | |
75 | { | |
76 | if (!win->m_hasVMT) return; | |
77 | ||
78 | if (g_isIdle) | |
79 | wxapp_install_idle_handler(); | |
80 | ||
81 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() ); | |
82 | event.SetEventObject( win ); | |
ea46eba0 RR |
83 | |
84 | // see above | |
85 | event.SetInt( (int)ceil(win->m_adjust->value) ); | |
0a07a7d8 RR |
86 | win->GetEventHandler()->ProcessEvent( event ); |
87 | } | |
88 | ||
738f9e5a RR |
89 | //----------------------------------------------------------------------------- |
90 | // wxSpinCtrl | |
91 | //----------------------------------------------------------------------------- | |
92 | ||
93 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl) | |
94 | ||
da048e3d RR |
95 | BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl) |
96 | EVT_CHAR(wxSpinCtrl::OnChar) | |
97 | END_EVENT_TABLE() | |
98 | ||
738f9e5a | 99 | bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id, |
ce89fdd2 VZ |
100 | const wxString& value, |
101 | const wxPoint& pos, const wxSize& size, | |
102 | long style, | |
103 | int min, int max, int initial, | |
104 | const wxString& name) | |
738f9e5a RR |
105 | { |
106 | m_needParent = TRUE; | |
354aa1e3 | 107 | m_acceptsFocus = TRUE; |
738f9e5a | 108 | |
0279e844 RR |
109 | if (!PreCreation( parent, pos, size ) || |
110 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
738f9e5a RR |
111 | { |
112 | wxFAIL_MSG( wxT("wxSpinCtrl creation failed") ); | |
9750fc42 | 113 | return FALSE; |
738f9e5a RR |
114 | } |
115 | ||
116 | m_oldPos = initial; | |
117 | ||
118 | m_adjust = (GtkAdjustment*) gtk_adjustment_new( initial, min, max, 1.0, 5.0, 0.0); | |
119 | ||
120 | m_widget = gtk_spin_button_new( m_adjust, 1, 0 ); | |
0279e844 | 121 | |
b02da6b1 VZ |
122 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), |
123 | (int)(m_windowStyle & wxSP_WRAP) ); | |
738f9e5a | 124 | |
07f5b19a | 125 | GtkEnableEvents(); |
db434467 | 126 | |
738f9e5a RR |
127 | m_parent->DoAddChild( this ); |
128 | ||
129 | PostCreation(); | |
e8e24dfa | 130 | InheritAttributes(); |
738f9e5a | 131 | |
db434467 RR |
132 | wxSize size_best( DoGetBestSize() ); |
133 | wxSize new_size( size ); | |
134 | if (new_size.x == -1) | |
135 | new_size.x = size_best.x; | |
136 | if (new_size.y == -1) | |
137 | new_size.y = size_best.y; | |
b0e282b3 RR |
138 | if (new_size.y > size_best.y) |
139 | new_size.y = size_best.y; | |
db434467 RR |
140 | if ((new_size.x != size.x) || (new_size.y != size.y)) |
141 | SetSize( new_size.x, new_size.y ); | |
142 | ||
ce89fdd2 VZ |
143 | SetValue( value ); |
144 | ||
738f9e5a RR |
145 | Show( TRUE ); |
146 | ||
147 | return TRUE; | |
148 | } | |
149 | ||
07f5b19a RR |
150 | void wxSpinCtrl::GtkDisableEvents() |
151 | { | |
152 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_adjust), | |
153 | GTK_SIGNAL_FUNC(gtk_spinctrl_callback), | |
154 | (gpointer) this ); | |
155 | ||
0a07a7d8 RR |
156 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), |
157 | GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback), | |
158 | (gpointer) this ); | |
07f5b19a RR |
159 | } |
160 | ||
161 | void wxSpinCtrl::GtkEnableEvents() | |
162 | { | |
163 | gtk_signal_connect( GTK_OBJECT (m_adjust), | |
164 | "value_changed", | |
165 | GTK_SIGNAL_FUNC(gtk_spinctrl_callback), | |
166 | (gpointer) this ); | |
0a07a7d8 RR |
167 | |
168 | gtk_signal_connect( GTK_OBJECT(m_widget), | |
169 | "changed", | |
170 | GTK_SIGNAL_FUNC(gtk_spinctrl_text_changed_callback), | |
171 | (gpointer)this); | |
07f5b19a RR |
172 | } |
173 | ||
738f9e5a RR |
174 | int wxSpinCtrl::GetMin() const |
175 | { | |
176 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
177 | ||
178 | return (int)ceil(m_adjust->lower); | |
179 | } | |
180 | ||
181 | int wxSpinCtrl::GetMax() const | |
182 | { | |
183 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
184 | ||
185 | return (int)ceil(m_adjust->upper); | |
186 | } | |
187 | ||
188 | int wxSpinCtrl::GetValue() const | |
189 | { | |
190 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
191 | ||
33720b2d RR |
192 | gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) ); |
193 | ||
738f9e5a RR |
194 | return (int)ceil(m_adjust->value); |
195 | } | |
196 | ||
ce89fdd2 VZ |
197 | void wxSpinCtrl::SetValue( const wxString& value ) |
198 | { | |
199 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
200 | ||
201 | int n; | |
202 | if ( (wxSscanf(value, wxT("%d"), &n) == 1) ) | |
203 | { | |
204 | // a number - set it | |
205 | SetValue(n); | |
206 | } | |
207 | else | |
208 | { | |
209 | // invalid number - set text as is (wxMSW compatible) | |
07f5b19a | 210 | GtkDisableEvents(); |
fab591c5 | 211 | gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) ); |
07f5b19a | 212 | GtkEnableEvents(); |
ce89fdd2 VZ |
213 | } |
214 | } | |
215 | ||
738f9e5a RR |
216 | void wxSpinCtrl::SetValue( int value ) |
217 | { | |
218 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
219 | ||
220 | float fpos = (float)value; | |
221 | m_oldPos = fpos; | |
222 | if (fabs(fpos-m_adjust->value) < sensitivity) return; | |
223 | ||
224 | m_adjust->value = fpos; | |
225 | ||
07f5b19a | 226 | GtkDisableEvents(); |
738f9e5a | 227 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "value_changed" ); |
07f5b19a | 228 | GtkEnableEvents(); |
738f9e5a RR |
229 | } |
230 | ||
f8f9ec55 VZ |
231 | void wxSpinCtrl::SetSelection(long from, long to) |
232 | { | |
233 | // translate from wxWindows conventions to GTK+ ones: (-1, -1) means the | |
234 | // entire range | |
235 | if ( from == -1 && to == -1 ) | |
236 | { | |
237 | from = 0; | |
238 | to = INT_MAX; | |
239 | } | |
240 | ||
241 | gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to ); | |
242 | } | |
243 | ||
738f9e5a RR |
244 | void wxSpinCtrl::SetRange(int minVal, int maxVal) |
245 | { | |
246 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
247 | ||
248 | float fmin = (float)minVal; | |
249 | float fmax = (float)maxVal; | |
250 | ||
251 | if ((fabs(fmin-m_adjust->lower) < sensitivity) && | |
252 | (fabs(fmax-m_adjust->upper) < sensitivity)) | |
253 | { | |
254 | return; | |
255 | } | |
256 | ||
257 | m_adjust->lower = fmin; | |
258 | m_adjust->upper = fmax; | |
259 | ||
260 | gtk_signal_emit_by_name( GTK_OBJECT(m_adjust), "changed" ); | |
0e0d8857 | 261 | |
738f9e5a RR |
262 | // these two calls are required due to some bug in GTK |
263 | Refresh(); | |
264 | SetFocus(); | |
265 | } | |
266 | ||
da048e3d RR |
267 | void wxSpinCtrl::OnChar( wxKeyEvent &event ) |
268 | { | |
269 | wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") ); | |
270 | ||
12a3f227 | 271 | if (event.GetKeyCode() == WXK_RETURN) |
da048e3d RR |
272 | { |
273 | wxWindow *top_frame = m_parent; | |
055e633d | 274 | while (top_frame->GetParent() && !(top_frame->IsTopLevel())) |
da048e3d | 275 | top_frame = top_frame->GetParent(); |
0e0d8857 | 276 | |
fa8a793a | 277 | if ( GTK_IS_WINDOW(top_frame->m_widget) ) |
da048e3d | 278 | { |
fa8a793a VZ |
279 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
280 | if ( window ) | |
281 | { | |
282 | GtkWidget *widgetDef = window->default_widget; | |
283 | ||
055e633d | 284 | if ( widgetDef ) |
fa8a793a VZ |
285 | { |
286 | gtk_widget_activate(widgetDef); | |
287 | return; | |
288 | } | |
289 | } | |
9750fc42 | 290 | } |
da048e3d RR |
291 | } |
292 | ||
75a22e81 | 293 | if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxPROCESS_ENTER)) |
4a11cca2 RR |
294 | { |
295 | wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId ); | |
296 | evt.SetEventObject(this); | |
297 | GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget); | |
298 | wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) ); | |
299 | evt.SetString( val ); | |
300 | if (GetEventHandler()->ProcessEvent(evt)) return; | |
301 | } | |
302 | ||
da048e3d RR |
303 | event.Skip(); |
304 | } | |
305 | ||
738f9e5a RR |
306 | bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window ) |
307 | { | |
2b904684 RR |
308 | if (GTK_SPIN_BUTTON(m_widget)->entry.text_area == window) return TRUE; |
309 | ||
310 | if (GTK_SPIN_BUTTON(m_widget)->panel == window) return TRUE; | |
311 | ||
312 | return FALSE; | |
738f9e5a RR |
313 | } |
314 | ||
315 | void wxSpinCtrl::ApplyWidgetStyle() | |
316 | { | |
317 | SetWidgetStyle(); | |
318 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
319 | } | |
320 | ||
9d9b7755 VZ |
321 | wxSize wxSpinCtrl::DoGetBestSize() const |
322 | { | |
0279e844 RR |
323 | wxSize ret( wxControl::DoGetBestSize() ); |
324 | return wxSize(95, ret.y); | |
9d9b7755 VZ |
325 | } |
326 | ||
738f9e5a | 327 | #endif |
aec0ed2e | 328 | // wxUSE_SPINCTRL |