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