use native TAB traversal for GTK+ 2
[wxWidgets.git] / src / gtk / spinctrl.cpp
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/textctrl.h" // for wxEVT_COMMAND_TEXT_UPDATED
20 #include "wx/utils.h"
21 #endif
22
23 #include "wx/gtk/private.h"
24
25 //-----------------------------------------------------------------------------
26 // data
27 //-----------------------------------------------------------------------------
28
29 extern bool g_blockEventsOnDrag;
30
31 //-----------------------------------------------------------------------------
32 // "value_changed"
33 //-----------------------------------------------------------------------------
34
35 extern "C" {
36 static void
37 gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
38 {
39 if (g_isIdle) wxapp_install_idle_handler();
40
41 win->m_pos = int(gtk_spin_button_get_value(spinbutton));
42 if (!win->m_hasVMT || g_blockEventsOnDrag || win->m_blockScrollEvent)
43 return;
44
45 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
46 event.SetEventObject( win );
47
48 // note that we don't use wxSpinCtrl::GetValue() here because it would
49 // adjust the value to fit into the control range and this means that we
50 // would never be able to enter an "invalid" value in the control, even
51 // temporarily - and trying to enter 10 into the control which accepts the
52 // values in range 5..50 is then, ummm, quite challenging (hint: you can't
53 // enter 1!) (VZ)
54 event.SetInt(win->m_pos);
55 win->GetEventHandler()->ProcessEvent( event );
56 }
57 }
58
59 //-----------------------------------------------------------------------------
60 // "changed"
61 //-----------------------------------------------------------------------------
62
63 extern "C" {
64 static void
65 gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
66 {
67 if (g_isIdle)
68 wxapp_install_idle_handler();
69
70 if (!win->m_hasVMT || win->m_blockScrollEvent)
71 return;
72
73 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
74 event.SetEventObject( win );
75
76 // see above
77 event.SetInt(win->m_pos);
78 win->GetEventHandler()->ProcessEvent( event );
79 }
80 }
81
82 //-----------------------------------------------------------------------------
83 // wxSpinCtrl
84 //-----------------------------------------------------------------------------
85
86 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
87
88 BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
89 EVT_CHAR(wxSpinCtrl::OnChar)
90 END_EVENT_TABLE()
91
92 wxSpinCtrl::wxSpinCtrl()
93 {
94 m_pos = 0;
95 }
96
97 bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
98 const wxString& value,
99 const wxPoint& pos, const wxSize& size,
100 long style,
101 int min, int max, int initial,
102 const wxString& name)
103 {
104 m_needParent = 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_widget = gtk_spin_button_new_with_range(min, max, 1);
114 gtk_spin_button_set_value((GtkSpinButton*)m_widget, initial);
115 m_pos = int(gtk_spin_button_get_value((GtkSpinButton*)m_widget));
116
117 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
118 (int)(m_windowStyle & wxSP_WRAP) );
119
120 g_signal_connect(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
121 g_signal_connect(m_widget, "changed", G_CALLBACK(gtk_changed), this);
122
123 m_parent->DoAddChild( this );
124
125 PostCreation(size);
126
127 if (!value.empty())
128 {
129 SetValue(value);
130 }
131
132 return true;
133 }
134
135 int wxSpinCtrl::GetMin() const
136 {
137 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
138
139 double min;
140 gtk_spin_button_get_range((GtkSpinButton*)m_widget, &min, NULL);
141 return int(min);
142 }
143
144 int wxSpinCtrl::GetMax() const
145 {
146 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
147
148 double max;
149 gtk_spin_button_get_range((GtkSpinButton*)m_widget, NULL, &max);
150 return int(max);
151 }
152
153 int wxSpinCtrl::GetValue() const
154 {
155 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
156
157 wx_const_cast(wxSpinCtrl*, this)->BlockScrollEvent();
158 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
159 wx_const_cast(wxSpinCtrl*, this)->UnblockScrollEvent();
160
161 return m_pos;
162 }
163
164 void wxSpinCtrl::SetValue( const wxString& value )
165 {
166 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
167
168 int n;
169 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
170 {
171 // a number - set it
172 SetValue(n);
173 }
174 else
175 {
176 // invalid number - set text as is (wxMSW compatible)
177 BlockScrollEvent();
178 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
179 UnblockScrollEvent();
180 }
181 }
182
183 void wxSpinCtrl::SetValue( int value )
184 {
185 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
186
187 BlockScrollEvent();
188 gtk_spin_button_set_value((GtkSpinButton*)m_widget, value);
189 UnblockScrollEvent();
190 }
191
192 void wxSpinCtrl::SetSelection(long from, long to)
193 {
194 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
195 // entire range
196 if ( from == -1 && to == -1 )
197 {
198 from = 0;
199 to = INT_MAX;
200 }
201
202 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
203 }
204
205 void wxSpinCtrl::SetRange(int minVal, int maxVal)
206 {
207 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
208
209 BlockScrollEvent();
210 gtk_spin_button_set_range((GtkSpinButton*)m_widget, minVal, maxVal);
211 UnblockScrollEvent();
212 }
213
214 void wxSpinCtrl::OnChar( wxKeyEvent &event )
215 {
216 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
217
218 if (event.GetKeyCode() == WXK_RETURN)
219 {
220 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
221
222 if ( GTK_IS_WINDOW(top_frame->m_widget) )
223 {
224 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
225 if ( window )
226 {
227 GtkWidget *widgetDef = window->default_widget;
228
229 if ( widgetDef )
230 {
231 gtk_widget_activate(widgetDef);
232 return;
233 }
234 }
235 }
236 }
237
238 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
239 {
240 wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId );
241 evt.SetEventObject(this);
242 GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget);
243 wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) );
244 evt.SetString( val );
245 if (GetEventHandler()->ProcessEvent(evt)) return;
246 }
247
248 event.Skip();
249 }
250
251 GdkWindow *wxSpinCtrl::GTKGetWindow(wxArrayGdkWindows& windows) const
252 {
253 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
254
255 windows.push_back(spinbutton->entry.text_area);
256 windows.push_back(spinbutton->panel);
257
258 return NULL;
259 }
260
261 wxSize wxSpinCtrl::DoGetBestSize() const
262 {
263 wxSize ret( wxControl::DoGetBestSize() );
264 wxSize best(95, ret.y); // FIXME: 95?
265 CacheBestSize(best);
266 return best;
267 }
268
269 // static
270 wxVisualAttributes
271 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
272 {
273 // TODO: overload to accept functions like gtk_spin_button_new?
274 // Until then use a similar type
275 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
276 }
277
278 #endif
279 // wxUSE_SPINCTRL