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