]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/spinctrl.cpp
Use generic collapsible pane for wxUniv based builds.
[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
RR
38{
39 if (g_isIdle) wxapp_install_idle_handler();
40
7f81dfa1
PC
41 win->m_pos = int(gtk_spin_button_get_value(spinbutton));
42 if (!win->m_hasVMT || g_blockEventsOnDrag || win->m_blockScrollEvent)
43 return;
738f9e5a 44
58c837a4 45 wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, win->GetId());
738f9e5a 46 event.SetEventObject( win );
4477936a
VZ
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)
7f81dfa1 54 event.SetInt(win->m_pos);
738f9e5a 55 win->GetEventHandler()->ProcessEvent( event );
738f9e5a 56}
865bb325 57}
738f9e5a 58
0a07a7d8
RR
59//-----------------------------------------------------------------------------
60// "changed"
61//-----------------------------------------------------------------------------
62
865bb325 63extern "C" {
0a07a7d8 64static void
7f81dfa1 65gtk_changed(GtkSpinButton* spinbutton, wxSpinCtrl* win)
0a07a7d8 66{
0a07a7d8
RR
67 if (g_isIdle)
68 wxapp_install_idle_handler();
69
7f81dfa1
PC
70 if (!win->m_hasVMT || win->m_blockScrollEvent)
71 return;
72
0a07a7d8
RR
73 wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, win->GetId() );
74 event.SetEventObject( win );
3d257b8d 75
ea46eba0 76 // see above
7f81dfa1 77 event.SetInt(win->m_pos);
0a07a7d8
RR
78 win->GetEventHandler()->ProcessEvent( event );
79}
865bb325 80}
0a07a7d8 81
738f9e5a
RR
82//-----------------------------------------------------------------------------
83// wxSpinCtrl
84//-----------------------------------------------------------------------------
85
86IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl,wxControl)
87
da048e3d
RR
88BEGIN_EVENT_TABLE(wxSpinCtrl, wxControl)
89 EVT_CHAR(wxSpinCtrl::OnChar)
90END_EVENT_TABLE()
91
7f81dfa1
PC
92wxSpinCtrl::wxSpinCtrl()
93{
94 m_pos = 0;
95}
96
738f9e5a 97bool wxSpinCtrl::Create(wxWindow *parent, wxWindowID id,
ce89fdd2
VZ
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)
738f9e5a 103{
8e13c1ec
WS
104 m_needParent = true;
105 m_acceptsFocus = true;
738f9e5a 106
0279e844
RR
107 if (!PreCreation( parent, pos, size ) ||
108 !CreateBase( parent, id, pos, size, style, wxDefaultValidator, name ))
738f9e5a
RR
109 {
110 wxFAIL_MSG( wxT("wxSpinCtrl creation failed") );
8e13c1ec 111 return false;
738f9e5a
RR
112 }
113
7f81dfa1
PC
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));
3d257b8d 117
b02da6b1
VZ
118 gtk_spin_button_set_wrap( GTK_SPIN_BUTTON(m_widget),
119 (int)(m_windowStyle & wxSP_WRAP) );
738f9e5a 120
7f81dfa1
PC
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);
3d257b8d 123
738f9e5a
RR
124 m_parent->DoAddChild( this );
125
abdeb9e7 126 PostCreation(size);
db434467 127
7f81dfa1
PC
128 if (!value.empty())
129 {
130 SetValue(value);
131 }
ce89fdd2 132
8e13c1ec 133 return true;
738f9e5a
RR
134}
135
136int wxSpinCtrl::GetMin() const
137{
138 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
139
7f81dfa1
PC
140 double min;
141 gtk_spin_button_get_range((GtkSpinButton*)m_widget, &min, NULL);
142 return int(min);
738f9e5a
RR
143}
144
145int wxSpinCtrl::GetMax() const
146{
147 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
148
7f81dfa1
PC
149 double max;
150 gtk_spin_button_get_range((GtkSpinButton*)m_widget, NULL, &max);
151 return int(max);
738f9e5a
RR
152}
153
154int wxSpinCtrl::GetValue() const
155{
156 wxCHECK_MSG( (m_widget != NULL), 0, wxT("invalid spin button") );
157
7f81dfa1 158 wx_const_cast(wxSpinCtrl*, this)->BlockScrollEvent();
33720b2d 159 gtk_spin_button_update( GTK_SPIN_BUTTON(m_widget) );
7f81dfa1 160 wx_const_cast(wxSpinCtrl*, this)->UnblockScrollEvent();
33720b2d 161
7f81dfa1 162 return m_pos;
738f9e5a
RR
163}
164
ce89fdd2
VZ
165void 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)
7f81dfa1 178 BlockScrollEvent();
fab591c5 179 gtk_entry_set_text( GTK_ENTRY(m_widget), wxGTK_CONV( value ) );
7f81dfa1 180 UnblockScrollEvent();
ce89fdd2
VZ
181 }
182}
183
738f9e5a
RR
184void wxSpinCtrl::SetValue( int value )
185{
186 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
187
7f81dfa1
PC
188 BlockScrollEvent();
189 gtk_spin_button_set_value((GtkSpinButton*)m_widget, value);
190 UnblockScrollEvent();
738f9e5a
RR
191}
192
f8f9ec55
VZ
193void wxSpinCtrl::SetSelection(long from, long to)
194{
77ffb593 195 // translate from wxWidgets conventions to GTK+ ones: (-1, -1) means the
f8f9ec55
VZ
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
738f9e5a
RR
206void wxSpinCtrl::SetRange(int minVal, int maxVal)
207{
208 wxCHECK_RET( (m_widget != NULL), wxT("invalid spin button") );
209
7f81dfa1
PC
210 BlockScrollEvent();
211 gtk_spin_button_set_range((GtkSpinButton*)m_widget, minVal, maxVal);
212 UnblockScrollEvent();
738f9e5a
RR
213}
214
da048e3d
RR
215void wxSpinCtrl::OnChar( wxKeyEvent &event )
216{
217 wxCHECK_RET( m_widget != NULL, wxT("invalid spin ctrl") );
218
12a3f227 219 if (event.GetKeyCode() == WXK_RETURN)
da048e3d 220 {
7f81dfa1 221 wxWindow *top_frame = wxGetTopLevelParent(m_parent);
0e0d8857 222
fa8a793a 223 if ( GTK_IS_WINDOW(top_frame->m_widget) )
da048e3d 224 {
fa8a793a
VZ
225 GtkWindow *window = GTK_WINDOW(top_frame->m_widget);
226 if ( window )
227 {
228 GtkWidget *widgetDef = window->default_widget;
229
055e633d 230 if ( widgetDef )
fa8a793a
VZ
231 {
232 gtk_widget_activate(widgetDef);
233 return;
234 }
235 }
9750fc42 236 }
da048e3d
RR
237 }
238
8e13c1ec 239 if ((event.GetKeyCode() == WXK_RETURN) && (m_windowStyle & wxTE_PROCESS_ENTER))
4a11cca2
RR
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
da048e3d
RR
249 event.Skip();
250}
251
ef5c70f9 252GdkWindow *wxSpinCtrl::GTKGetWindow(wxArrayGdkWindows& windows) const
738f9e5a 253{
7f81dfa1 254 GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(m_widget);
ef5c70f9
VZ
255
256 windows.push_back(spinbutton->entry.text_area);
257 windows.push_back(spinbutton->panel);
258
259 return NULL;
738f9e5a
RR
260}
261
9d9b7755
VZ
262wxSize wxSpinCtrl::DoGetBestSize() const
263{
0279e844 264 wxSize ret( wxControl::DoGetBestSize() );
ef5c70f9 265 wxSize best(95, ret.y); // FIXME: 95?
9f884528
RD
266 CacheBestSize(best);
267 return best;
9d9b7755
VZ
268}
269
9d522606
RD
270// static
271wxVisualAttributes
272wxSpinCtrl::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
273{
274 // TODO: overload to accept functions like gtk_spin_button_new?
275 // Until then use a similar type
276 return GetDefaultAttributesFromGTKWidget(gtk_entry_new, true);
277}
278
738f9e5a 279#endif
aec0ed2e 280 // wxUSE_SPINCTRL