]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinctrl.cpp
final revision
[wxWidgets.git] / src / gtk / spinctrl.cpp
CommitLineData
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
WS
20 #include "wx/utils.h"
21#endif
aec0ed2e 22
e1910715 23#include "wx/gtk/private.h"
738f9e5a 24
738f9e5a
RR
25//-----------------------------------------------------------------------------
26// data
27//-----------------------------------------------------------------------------
28
29extern bool g_blockEventsOnDrag;
30
738f9e5a
RR
31//-----------------------------------------------------------------------------
32// "value_changed"
33//-----------------------------------------------------------------------------
34
865bb325 35extern "C" {
7f81dfa1
PC
36static void
37gtk_value_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
738f9e5a 38{
7f81dfa1
PC
39 win->m_pos = int(gtk_spin_button_get_value(spinbutton));
40 if (!win->m_hasVMT || g_blockEventsOnDrag || win->m_blockScrollEvent)
41 return;
738f9e5a 42
58c837a4 43 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
738f9e5a 44 event.SetEventObject( win );
4477936a
VZ
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)
7f81dfa1 52 event.SetInt(win->m_pos);
738f9e5a 53 win->GetEventHandler()->ProcessEvent( event );
738f9e5a 54}
865bb325 55}
738f9e5a 56
0a07a7d8
RR
57//-----------------------------------------------------------------------------
58// "changed"
59//-----------------------------------------------------------------------------
60
865bb325 61extern "C" {
0a07a7d8 62static void
7f81dfa1 63gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
0a07a7d8 64{
7f81dfa1
PC
65 if (!win->m_hasVMT || win->m_blockScrollEvent)
66 return;
67
0a07a7d8
RR
68 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
69 event.SetEventObject( win );
cf5d5057 70 event.SetString( GTK_ENTRY(spinbutton)->text );
3d257b8d 71
ea46eba0 72 // see above
7f81dfa1 73 event.SetInt(win->m_pos);
0a07a7d8
RR
74 win->GetEventHandler()->ProcessEvent( event );
75}
865bb325 76}
0a07a7d8 77
738f9e5a
RR
78//-----------------------------------------------------------------------------
79// wxSpinCtrl
80//-----------------------------------------------------------------------------
81
82IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
83
da048e3d
RR
84BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
85 EVT_CHAR(wxSpinCtrl::OnChar)
86END_EVENT_TABLE()
87
7f81dfa1
PC
88wxSpinCtrl::wxSpinCtrl()
89{
90 m_pos = 0;
91}
92
738f9e5a 93bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
ce89fdd2
VZ
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)
738f9e5a 99{
0279e844
RR
100 if (!PreCreation( parent, pos, size ) ||
101 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
738f9e5a
RR
102 {
103 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
8e13c1ec 104 return false;
738f9e5a
RR
105 }
106
7f81dfa1
PC
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));
3d257b8d 110
b02da6b1
VZ
111 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
112 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a 113
7f81dfa1
PC
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);
3d257b8d 116
738f9e5a
RR
117 m_parent->DoAddChild( this );
118
abdeb9e7 119 PostCreation(size);
db434467 120
7f81dfa1
PC
121 if (!value.empty())
122 {
123 SetValue(value);
124 }
ce89fdd2 125
8e13c1ec 126 return true;
738f9e5a
RR
127}
128
129int wxSpinCtrl::GetMin() const
130{
131 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
132
7f81dfa1
PC
133 double min;
134 gtk_spin_button_get_range((GtkSpinButton*)m_widget, &min, NULL);
135 return int(min);
738f9e5a
RR
136}
137
138int wxSpinCtrl::GetMax() const
139{
140 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
141
7f81dfa1
PC
142 double max;
143 gtk_spin_button_get_range((GtkSpinButton*)m_widget, NULL, &max);
144 return int(max);
738f9e5a
RR
145}
146
147int wxSpinCtrl::GetValue() const
148{
149 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
150
7f81dfa1 151 wx_const_cast(wxSpinCtrl*, this)->BlockScrollEvent();
33720b2d 152 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
7f81dfa1 153 wx_const_cast(wxSpinCtrl*, this)->UnblockScrollEvent();
33720b2d 154
7f81dfa1 155 return m_pos;
738f9e5a
RR
156}
157
ce89fdd2
VZ
158void 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)
7f81dfa1 171 BlockScrollEvent();
fab591c5 172 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
7f81dfa1 173 UnblockScrollEvent();
ce89fdd2
VZ
174 }
175}
176
738f9e5a
RR
177void wxSpinCtrl::SetValue( int value )
178{
179 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
180
7f81dfa1
PC
181 BlockScrollEvent();
182 gtk_spin_button_set_value((GtkSpinButton*)m_widget, value);
183 UnblockScrollEvent();
738f9e5a
RR
184}
185
f8f9ec55
VZ
186void wxSpinCtrl::SetSelection(long from, long to)
187{
77ffb593 188 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
f8f9ec55
VZ
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
738f9e5a
RR
199void wxSpinCtrl::SetRange(int minVal, int maxVal)
200{
201 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
202
7f81dfa1
PC
203 BlockScrollEvent();
204 gtk_spin_button_set_range((GtkSpinButton*)m_widget, minVal, maxVal);
205 UnblockScrollEvent();
738f9e5a
RR
206}
207
da048e3d
RR
208void wxSpinCtrl::OnChar( wxKeyEvent &event )
209{
210 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
211
12a3f227 212 if (event.GetKeyCode() == WXK_RETURN)
da048e3d 213 {
7f81dfa1 214 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
0e0d8857 215
fa8a793a 216 if ( GTK_IS_WINDOW(top_frame->m_widget) )
da048e3d 217 {
fa8a793a
VZ
218 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
219 if ( window )
220 {
221 GtkWidget *widgetDef = window->default_widget;
222
055e633d 223 if ( widgetDef )
fa8a793a
VZ
224 {
225 gtk_widget_activate(widgetDef);
226 return;
227 }
228 }
9750fc42 229 }
da048e3d
RR
230 }
231
8e13c1ec 232 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
4a11cca2
RR
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
da048e3d
RR
242 event.Skip();
243}
244
ef5c70f9 245GdkWindow *wxSpinCtrl::GTKGetWindow(wxArrayGdkWindows& windows) const
738f9e5a 246{
7f81dfa1 247 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
ef5c70f9
VZ
248
249 windows.push_back(spinbutton->entry.text_area);
250 windows.push_back(spinbutton->panel);
251
252 return NULL;
738f9e5a
RR
253}
254
9d9b7755
VZ
255wxSize wxSpinCtrl::DoGetBestSize() const
256{
0279e844 257 wxSize ret( wxControl::DoGetBestSize() );
ef5c70f9 258 wxSize best(95, ret.y); // FIXME: 95?
9f884528
RD
259 CacheBestSize(best);
260 return best;
9d9b7755
VZ
261}
262
9d522606
RD
263// static
264wxVisualAttributes
265wxSpinCtrl::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
738f9e5a 272#endif
aec0ed2e 273 // wxUSE_SPINCTRL