]>
Commit | Line | Data |
---|---|---|
738f9e5a | 1 | ///////////////////////////////////////////////////////////////////////////// |
8e13c1ec | 2 | // Name: src/gtk/spinbutt.cpp |
738f9e5a RR |
3 | // Purpose: wxSpinCtrl |
4 | // Author: Robert | |
5 | // Modified by: | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Robert Roebling | |
65571936 | 8 | // Licence: wxWindows licence |
738f9e5a RR |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
14f355c2 VS |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
0e0d8857 | 14 | #if wxUSE_SPINCTRL |
738f9e5a | 15 | |
de6185e2 WS |
16 | #include "wx/spinctrl.h" |
17 | ||
18 | #ifndef WX_PRECOMP | |
7f81dfa1 | 19 | #include "wx/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED |
de6185e2 | 20 | #include "wx/utils.h" |
0cb7e05c | 21 | #include "wx/wxcrtvararg.h" |
de6185e2 | 22 | #endif |
aec0ed2e | 23 | |
e1910715 | 24 | #include "wx/gtk/private.h" |
738f9e5a | 25 | |
738f9e5a RR |
26 | //----------------------------------------------------------------------------- |
27 | // data | |
28 | //----------------------------------------------------------------------------- | |
29 | ||
30 | extern bool g_blockEventsOnDrag; | |
31 | ||
738f9e5a RR |
32 | //----------------------------------------------------------------------------- |
33 | // "value_changed" | |
34 | //----------------------------------------------------------------------------- | |
35 | ||
865bb325 | 36 | extern "C" { |
7f81dfa1 | 37 | static void |
8cd6a9ad | 38 | gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrlGTKBase* win) |
738f9e5a | 39 | { |
8cd6a9ad | 40 | win->m_value = gtk_spin_button_get_value(spinbutton); |
00dc9772 | 41 | if (!win->m_hasVMT || g_blockEventsOnDrag) |
7f81dfa1 | 42 | return; |
738f9e5a | 43 | |
4477936a VZ |
44 | // note that we don't use wxSpinCtrl::GetValue() here because it would |
45 | // adjust the value to fit into the control range and this means that we | |
46 | // would never be able to enter an "invalid" value in the control, even | |
47 | // temporarily - and trying to enter 10 into the control which accepts the | |
48 | // values in range 5..50 is then, ummm, quite challenging (hint: you can't | |
49 | // enter 1!) (VZ) | |
8cd6a9ad VZ |
50 | |
51 | if (wxIsKindOf(win, wxSpinCtrl)) | |
52 | { | |
53 | wxSpinEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId()); | |
54 | event.SetEventObject( win ); | |
55 | event.SetPosition((int)(win->m_value + 0.5)); // FIXME should be SetValue | |
56 | event.SetString(GTK_ENTRY(spinbutton)->text); | |
57 | win->HandleWindowEvent( event ); | |
58 | } | |
59 | else // wxIsKindOf(win, wxSpinCtrlDouble) | |
60 | { | |
61 | wxSpinDoubleEvent event( wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED, win->GetId()); | |
62 | event.SetEventObject( win ); | |
63 | event.SetValue(win->m_value); | |
64 | event.SetString(GTK_ENTRY(spinbutton)->text); | |
65 | win->HandleWindowEvent( event ); | |
66 | } | |
738f9e5a | 67 | } |
865bb325 | 68 | } |
738f9e5a | 69 | |
0a07a7d8 RR |
70 | //----------------------------------------------------------------------------- |
71 | // "changed" | |
72 | //----------------------------------------------------------------------------- | |
73 | ||
865bb325 | 74 | extern "C" { |
0a07a7d8 | 75 | static void |
7f81dfa1 | 76 | gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win) |
0a07a7d8 | 77 | { |
00dc9772 | 78 | if (!win->m_hasVMT) |
7f81dfa1 PC |
79 | return; |
80 | ||
0a07a7d8 RR |
81 | wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() ); |
82 | event.SetEventObject( win ); | |
cf5d5057 | 83 | event.SetString( GTK_ENTRY(spinbutton)->text ); |
3d257b8d | 84 | |
ea46eba0 | 85 | // see above |
8cd6a9ad | 86 | event.SetInt((int)win->m_value); |
937013e0 | 87 | win->HandleWindowEvent( event ); |
0a07a7d8 | 88 | } |
865bb325 | 89 | } |
0a07a7d8 | 90 | |
738f9e5a | 91 | //----------------------------------------------------------------------------- |
8cd6a9ad | 92 | // wxSpinCtrlGTKBase |
738f9e5a RR |
93 | //----------------------------------------------------------------------------- |
94 | ||
8cd6a9ad | 95 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlGTKBase, wxSpinCtrlBase) |
738f9e5a | 96 | |
8cd6a9ad VZ |
97 | BEGIN_EVENT_TABLE(wxSpinCtrlGTKBase, wxSpinCtrlBase) |
98 | EVT_CHAR(wxSpinCtrlGTKBase::OnChar) | |
da048e3d RR |
99 | END_EVENT_TABLE() |
100 | ||
8cd6a9ad | 101 | bool wxSpinCtrlGTKBase::Create(wxWindow *parent, wxWindowID id, |
ce89fdd2 VZ |
102 | const wxString& value, |
103 | const wxPoint& pos, const wxSize& size, | |
104 | long style, | |
8cd6a9ad | 105 | double min, double max, double initial, double inc, |
ce89fdd2 | 106 | const wxString& name) |
738f9e5a | 107 | { |
0279e844 RR |
108 | if (!PreCreation( parent, pos, size ) || |
109 | !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name )) | |
738f9e5a | 110 | { |
8cd6a9ad | 111 | wxFAIL_MSG( wxT("wxSpinCtrlGTKBase creation failed") ); |
8e13c1ec | 112 | return false; |
738f9e5a RR |
113 | } |
114 | ||
8cd6a9ad | 115 | m_widget = gtk_spin_button_new_with_range(min, max, inc); |
9ff9d30c | 116 | g_object_ref(m_widget); |
8cd6a9ad | 117 | |
4a783bb4 | 118 | gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), initial); |
8cd6a9ad | 119 | m_value = gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget)); |
3d257b8d | 120 | |
b02da6b1 VZ |
121 | gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget), |
122 | (int)(m_windowStyle & wxSP_WRAP) ); | |
738f9e5a | 123 | |
0bf36922 RR |
124 | g_signal_connect_after(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this); |
125 | g_signal_connect_after(m_widget, "changed", G_CALLBACK(gtk_changed), this); | |
3d257b8d | 126 | |
738f9e5a RR |
127 | m_parent->DoAddChild( this ); |
128 | ||
abdeb9e7 | 129 | PostCreation(size); |
db434467 | 130 | |
7f81dfa1 PC |
131 | if (!value.empty()) |
132 | { | |
133 | SetValue(value); | |
134 | } | |
ce89fdd2 | 135 | |
8e13c1ec | 136 | return true; |
738f9e5a RR |
137 | } |
138 | ||
8cd6a9ad | 139 | double wxSpinCtrlGTKBase::DoGetValue() const |
738f9e5a RR |
140 | { |
141 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
142 | ||
8cd6a9ad VZ |
143 | GtkDisableEvents(); |
144 | gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) ); | |
145 | wx_const_cast(wxSpinCtrlGTKBase*, this)->m_value = | |
146 | gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget)); | |
147 | GtkEnableEvents(); | |
148 | ||
149 | return m_value; | |
150 | } | |
151 | ||
152 | double wxSpinCtrlGTKBase::DoGetMin() const | |
153 | { | |
154 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
155 | ||
156 | double min = 0; | |
4a783bb4 | 157 | gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), &min, NULL); |
8cd6a9ad | 158 | return min; |
738f9e5a RR |
159 | } |
160 | ||
8cd6a9ad | 161 | double wxSpinCtrlGTKBase::DoGetMax() const |
738f9e5a RR |
162 | { |
163 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
164 | ||
8cd6a9ad | 165 | double max = 0; |
4a783bb4 | 166 | gtk_spin_button_get_range( GTK_SPIN_BUTTON(m_widget), NULL, &max); |
8cd6a9ad | 167 | return max; |
738f9e5a RR |
168 | } |
169 | ||
8cd6a9ad | 170 | double wxSpinCtrlGTKBase::DoGetIncrement() const |
738f9e5a RR |
171 | { |
172 | wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") ); | |
173 | ||
8cd6a9ad VZ |
174 | double inc = 0; |
175 | gtk_spin_button_get_increments( GTK_SPIN_BUTTON(m_widget), NULL, &inc); | |
176 | return inc; | |
177 | } | |
178 | ||
179 | bool wxSpinCtrlGTKBase::GetSnapToTicks() const | |
180 | { | |
181 | wxCHECK_MSG( m_widget, 0, "invalid spin button" ); | |
33720b2d | 182 | |
8cd6a9ad | 183 | return gtk_spin_button_get_snap_to_ticks( GTK_SPIN_BUTTON(m_widget) ); |
738f9e5a RR |
184 | } |
185 | ||
8cd6a9ad | 186 | void wxSpinCtrlGTKBase::SetValue( const wxString& value ) |
ce89fdd2 VZ |
187 | { |
188 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
189 | ||
8cd6a9ad VZ |
190 | double n; |
191 | if ( wxSscanf(value, "%lg", &n) == 1 ) | |
ce89fdd2 | 192 | { |
8cd6a9ad VZ |
193 | // a number - set it, let DoSetValue round for int value |
194 | DoSetValue(n); | |
195 | return; | |
ce89fdd2 | 196 | } |
8cd6a9ad VZ |
197 | |
198 | // invalid number - set text as is (wxMSW compatible) | |
199 | GtkDisableEvents(); | |
200 | gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) ); | |
201 | GtkEnableEvents(); | |
ce89fdd2 VZ |
202 | } |
203 | ||
8cd6a9ad | 204 | void wxSpinCtrlGTKBase::DoSetValue( double value ) |
738f9e5a RR |
205 | { |
206 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
207 | ||
8cd6a9ad VZ |
208 | if (wxIsKindOf(this, wxSpinCtrl)) |
209 | value = int(value + 0.5); | |
210 | ||
0bf36922 | 211 | GtkDisableEvents(); |
4a783bb4 | 212 | gtk_spin_button_set_value( GTK_SPIN_BUTTON(m_widget), value); |
8cd6a9ad | 213 | m_value = gtk_spin_button_get_value( GTK_SPIN_BUTTON(m_widget)); |
0bf36922 | 214 | GtkEnableEvents(); |
738f9e5a RR |
215 | } |
216 | ||
8cd6a9ad VZ |
217 | void wxSpinCtrlGTKBase::SetSnapToTicks(bool snap_to_ticks) |
218 | { | |
219 | wxCHECK_RET( (m_widget != NULL), "invalid spin button" ); | |
220 | ||
221 | gtk_spin_button_set_snap_to_ticks( GTK_SPIN_BUTTON(m_widget), snap_to_ticks); | |
222 | } | |
223 | ||
224 | void wxSpinCtrlGTKBase::SetSelection(long from, long to) | |
f8f9ec55 | 225 | { |
77ffb593 | 226 | // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the |
f8f9ec55 VZ |
227 | // entire range |
228 | if ( from == -1 && to == -1 ) | |
229 | { | |
230 | from = 0; | |
231 | to = INT_MAX; | |
232 | } | |
233 | ||
234 | gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to ); | |
235 | } | |
236 | ||
8cd6a9ad | 237 | void wxSpinCtrlGTKBase::DoSetRange(double minVal, double maxVal) |
738f9e5a RR |
238 | { |
239 | wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") ); | |
240 | ||
0bf36922 | 241 | GtkDisableEvents(); |
4a783bb4 | 242 | gtk_spin_button_set_range( GTK_SPIN_BUTTON(m_widget), minVal, maxVal); |
8cd6a9ad | 243 | m_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget)); |
0bf36922 RR |
244 | GtkEnableEvents(); |
245 | } | |
246 | ||
8cd6a9ad VZ |
247 | void wxSpinCtrlGTKBase::DoSetIncrement(double inc) |
248 | { | |
249 | wxCHECK_RET( m_widget, "invalid spin button" ); | |
250 | ||
251 | GtkDisableEvents(); | |
252 | gtk_spin_button_set_increments( GTK_SPIN_BUTTON(m_widget), inc, 10*inc); | |
253 | m_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(m_widget)); | |
254 | GtkEnableEvents(); | |
255 | } | |
0bf36922 | 256 | |
8cd6a9ad | 257 | void wxSpinCtrlGTKBase::GtkDisableEvents() const |
0bf36922 RR |
258 | { |
259 | g_signal_handlers_block_by_func( m_widget, | |
260 | (gpointer)gtk_value_changed, (void*) this); | |
00dc9772 | 261 | |
0bf36922 RR |
262 | g_signal_handlers_block_by_func(m_widget, |
263 | (gpointer)gtk_changed, (void*) this); | |
264 | } | |
265 | ||
8cd6a9ad | 266 | void wxSpinCtrlGTKBase::GtkEnableEvents() const |
0bf36922 RR |
267 | { |
268 | g_signal_handlers_unblock_by_func(m_widget, | |
269 | (gpointer)gtk_value_changed, (void*) this); | |
00dc9772 | 270 | |
0bf36922 RR |
271 | g_signal_handlers_unblock_by_func(m_widget, |
272 | (gpointer)gtk_changed, (void*) this); | |
738f9e5a RR |
273 | } |
274 | ||
8cd6a9ad | 275 | void wxSpinCtrlGTKBase::OnChar( wxKeyEvent &event ) |
da048e3d RR |
276 | { |
277 | wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") ); | |
278 | ||
12a3f227 | 279 | if (event.GetKeyCode() == WXK_RETURN) |
da048e3d | 280 | { |
7f81dfa1 | 281 | wxWindow *top_frame = wxGetTopLevelParent(m_parent); |
0e0d8857 | 282 | |
fa8a793a | 283 | if ( GTK_IS_WINDOW(top_frame->m_widget) ) |
da048e3d | 284 | { |
fa8a793a VZ |
285 | GtkWindow *window = GTK_WINDOW(top_frame->m_widget); |
286 | if ( window ) | |
287 | { | |
288 | GtkWidget *widgetDef = window->default_widget; | |
289 | ||
055e633d | 290 | if ( widgetDef ) |
fa8a793a VZ |
291 | { |
292 | gtk_widget_activate(widgetDef); | |
293 | return; | |
294 | } | |
295 | } | |
9750fc42 | 296 | } |
da048e3d RR |
297 | } |
298 | ||
8e13c1ec | 299 | if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER)) |
4a11cca2 RR |
300 | { |
301 | wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId ); | |
302 | evt.SetEventObject(this); | |
303 | GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget); | |
304 | wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) ); | |
305 | evt.SetString( val ); | |
937013e0 | 306 | if (HandleWindowEvent(evt)) return; |
4a11cca2 RR |
307 | } |
308 | ||
da048e3d RR |
309 | event.Skip(); |
310 | } | |
311 | ||
8cd6a9ad | 312 | GdkWindow *wxSpinCtrlGTKBase::GTKGetWindow(wxArrayGdkWindows& windows) const |
738f9e5a | 313 | { |
7f81dfa1 | 314 | GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget); |
ef5c70f9 VZ |
315 | |
316 | windows.push_back(spinbutton->entry.text_area); | |
317 | windows.push_back(spinbutton->panel); | |
318 | ||
319 | return NULL; | |
738f9e5a RR |
320 | } |
321 | ||
8cd6a9ad | 322 | wxSize wxSpinCtrlGTKBase::DoGetBestSize() const |
9d9b7755 | 323 | { |
0279e844 | 324 | wxSize ret( wxControl::DoGetBestSize() ); |
ef5c70f9 | 325 | wxSize best(95, ret.y); // FIXME: 95? |
9f884528 RD |
326 | CacheBestSize(best); |
327 | return best; | |
9d9b7755 VZ |
328 | } |
329 | ||
9d522606 RD |
330 | // static |
331 | wxVisualAttributes | |
8cd6a9ad | 332 | wxSpinCtrlGTKBase::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) |
9d522606 RD |
333 | { |
334 | // TODO: overload to accept functions like gtk_spin_button_new? | |
335 | // Until then use a similar type | |
336 | return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true); | |
337 | } | |
338 | ||
8cd6a9ad VZ |
339 | //----------------------------------------------------------------------------- |
340 | // wxSpinCtrl | |
341 | //----------------------------------------------------------------------------- | |
342 | ||
343 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxSpinCtrlGTKBase) | |
344 | ||
345 | //----------------------------------------------------------------------------- | |
346 | // wxSpinCtrlDouble | |
347 | //----------------------------------------------------------------------------- | |
348 | ||
349 | IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrlDouble, wxSpinCtrlGTKBase) | |
350 | ||
351 | unsigned wxSpinCtrlDouble::GetDigits() const | |
352 | { | |
353 | wxCHECK_MSG( m_widget, 0, "invalid spin button" ); | |
354 | ||
355 | return gtk_spin_button_get_digits( GTK_SPIN_BUTTON(m_widget) ); | |
356 | } | |
357 | ||
358 | void wxSpinCtrlDouble::SetDigits(unsigned digits) | |
359 | { | |
360 | wxCHECK_RET( m_widget, "invalid spin button" ); | |
361 | ||
362 | gtk_spin_button_set_digits( GTK_SPIN_BUTTON(m_widget), digits); | |
363 | } | |
364 | ||
365 | #endif // wxUSE_SPINCTRL |