]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/spinctrl.cpp
[ 1508778 ] Fix for wxOwnerDrawnComboBox list selection rendering.
[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 m_acceptsFocus = true;
106
107 if (!PreCreation( parent, pos, size ) ||
108 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
109 {
110 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
111 return false;
112 }
113
114 m_widget = gtk_spin_button_new_with_range(min, max, 1);
115 gtk_spin_button_set_value((GtkSpinButton*)m_widget, initial);
116 m_pos = int(gtk_spin_button_get_value((GtkSpinButton*)m_widget));
117
118 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
119 (int)(m_windowStyle & wxSP_WRAP) );
120
121 g_signal_connect(m_widget, "value_changed", G_CALLBACK(gtk_value_changed), this);
122 g_signal_connect(m_widget, "changed", G_CALLBACK(gtk_changed), this);
123
124 m_parent->DoAddChild( this );
125
126 PostCreation(size);
127
128 if (!value.empty())
129 {
130 SetValue(value);
131 }
132
133 return true;
134 }
135
136 int wxSpinCtrl::GetMin() const
137 {
138 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
139
140 double min;
141 gtk_spin_button_get_range((GtkSpinButton*)m_widget, &min, NULL);
142 return int(min);
143 }
144
145 int wxSpinCtrl::GetMax() const
146 {
147 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
148
149 double max;
150 gtk_spin_button_get_range((GtkSpinButton*)m_widget, NULL, &max);
151 return int(max);
152 }
153
154 int wxSpinCtrl::GetValue() const
155 {
156 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
157
158 wx_const_cast(wxSpinCtrl*, this)->BlockScrollEvent();
159 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
160 wx_const_cast(wxSpinCtrl*, this)->UnblockScrollEvent();
161
162 return m_pos;
163 }
164
165 void wxSpinCtrl::SetValue( const wxString& value )
166 {
167 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
168
169 int n;
170 if ( (wxSscanf(value, wxT("%d"), &n) == 1) )
171 {
172 // a number - set it
173 SetValue(n);
174 }
175 else
176 {
177 // invalid number - set text as is (wxMSW compatible)
178 BlockScrollEvent();
179 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
180 UnblockScrollEvent();
181 }
182 }
183
184 void wxSpinCtrl::SetValue( int value )
185 {
186 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
187
188 BlockScrollEvent();
189 gtk_spin_button_set_value((GtkSpinButton*)m_widget, value);
190 UnblockScrollEvent();
191 }
192
193 void wxSpinCtrl::SetSelection(long from, long to)
194 {
195 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
196 // entire range
197 if ( from == -1 && to == -1 )
198 {
199 from = 0;
200 to = INT_MAX;
201 }
202
203 gtk_editable_select_region( GTK_EDITABLE(m_widget), (gint)from, (gint)to );
204 }
205
206 void wxSpinCtrl::SetRange(int minVal, int maxVal)
207 {
208 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
209
210 BlockScrollEvent();
211 gtk_spin_button_set_range((GtkSpinButton*)m_widget, minVal, maxVal);
212 UnblockScrollEvent();
213 }
214
215 void wxSpinCtrl::OnChar( wxKeyEvent &event )
216 {
217 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
218
219 if (event.GetKeyCode() == WXK_RETURN)
220 {
221 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
222
223 if ( GTK_IS_WINDOW(top_frame->m_widget) )
224 {
225 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
226 if ( window )
227 {
228 GtkWidget *widgetDef = window->default_widget;
229
230 if ( widgetDef )
231 {
232 gtk_widget_activate(widgetDef);
233 return;
234 }
235 }
236 }
237 }
238
239 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
240 {
241 wxCommandEvent evt( wxEVT_COMMAND_TEXT_ENTER, m_windowId );
242 evt.SetEventObject(this);
243 GtkSpinButton *gsb = GTK_SPIN_BUTTON(m_widget);
244 wxString val = wxGTK_CONV_BACK( gtk_entry_get_text( &gsb->entry ) );
245 evt.SetString( val );
246 if (GetEventHandler()->ProcessEvent(evt)) return;
247 }
248
249 event.Skip();
250 }
251
252 bool wxSpinCtrl::IsOwnGtkWindow( GdkWindow *window )
253 {
254 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
255 return window == spinbutton->entry.text_area || window == spinbutton->panel;
256 }
257
258 wxSize wxSpinCtrl::DoGetBestSize() const
259 {
260 wxSize ret( wxControl::DoGetBestSize() );
261 wxSize best(95, ret.y);
262 CacheBestSize(best);
263 return best;
264 }
265
266 // static
267 wxVisualAttributes
268 wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
269 {
270 // TODO: overload to accept functions like gtk_spin_button_new?
271 // Until then use a similar type
272 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
273 }
274
275 #endif
276 // wxUSE_SPINCTRL