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