]> git.saurik.com Git - wxWidgets.git/blame - src/generic/spinctlg.cpp
destroying native and wx controls
[wxWidgets.git] / src / generic / spinctlg.cpp
CommitLineData
21709999
JS
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/generic/spinctlg.cpp
3// Purpose: implements wxSpinCtrl as a composite control
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 29.01.01
7// RCS-ID: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 9// License: wxWindows licence
21709999
JS
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21709999
JS
21 #pragma implementation "spinctlg.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
571d14b2
RD
31// There are port-specific versions for MSW, GTK, OS/2 and Mac, so exclude the
32// contents of this file in those cases
33#if !(defined(__WXMSW__) || defined(__WXGTK__) || defined(__WXPM__) || \
34 defined(__WXMAC__)) || defined(__WXUNIVERSAL__)
21709999
JS
35
36#ifndef WX_PRECOMP
37 #include "wx/textctrl.h"
38#endif //WX_PRECOMP
39
c67d6888
JS
40#if wxUSE_SPINCTRL
41
21709999
JS
42#include "wx/spinbutt.h"
43#include "wx/spinctrl.h"
44
45// ----------------------------------------------------------------------------
46// constants
47// ----------------------------------------------------------------------------
48
49// the margin between the text control and the spin
50static const wxCoord MARGIN = 2;
51
52// ----------------------------------------------------------------------------
53// wxSpinCtrlText: text control used by spin control
54// ----------------------------------------------------------------------------
55
56class wxSpinCtrlText : public wxTextCtrl
57{
58public:
59 wxSpinCtrlText(wxSpinCtrl *spin, const wxString& value)
3fb79396 60 : wxTextCtrl(spin->GetParent(), wxID_ANY, value)
21709999
JS
61 {
62 m_spin = spin;
ca65c044 63
e459a794 64 // remove the default minsize, the spinctrl will have one instead
422d0ff0 65 SetSizeHints(wxDefaultCoord,wxDefaultCoord);
21709999
JS
66 }
67
68protected:
69 void OnTextChange(wxCommandEvent& event)
70 {
71 int val;
72 if ( m_spin->GetTextValue(&val) )
73 {
74 m_spin->GetSpinButton()->SetValue(val);
75 }
76
77 event.Skip();
78 }
71e03035 79
23d416bf
RR
80 bool ProcessEvent(wxEvent &event)
81 {
82 // Hand button down events to wxSpinCtrl. Doesn't work.
83 if (event.GetEventType() == wxEVT_LEFT_DOWN && m_spin->ProcessEvent( event ))
3fb79396 84 return true;
71e03035 85
23d416bf
RR
86 return wxTextCtrl::ProcessEvent( event );
87 }
21709999
JS
88
89private:
90 wxSpinCtrl *m_spin;
91
92 DECLARE_EVENT_TABLE()
93};
94
95BEGIN_EVENT_TABLE(wxSpinCtrlText, wxTextCtrl)
3fb79396 96 EVT_TEXT(wxID_ANY, wxSpinCtrlText::OnTextChange)
21709999
JS
97END_EVENT_TABLE()
98
99// ----------------------------------------------------------------------------
100// wxSpinCtrlButton: spin button used by spin control
101// ----------------------------------------------------------------------------
102
103class wxSpinCtrlButton : public wxSpinButton
104{
105public:
106 wxSpinCtrlButton(wxSpinCtrl *spin, int style)
107 : wxSpinButton(spin->GetParent())
108 {
109 m_spin = spin;
110
71e03035 111 SetWindowStyle(style | wxSP_VERTICAL);
e459a794
RD
112
113 // remove the default minsize, the spinctrl will have one instead
422d0ff0 114 SetSizeHints(wxDefaultCoord,wxDefaultCoord);
21709999
JS
115 }
116
117protected:
63efc55f 118 void OnSpinButton(wxSpinEvent& eventSpin)
21709999 119 {
c1eb1102 120 m_spin->SetTextValue(eventSpin.GetPosition());
63efc55f 121
c1eb1102
VZ
122 wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, m_spin->GetId());
123 event.SetEventObject(m_spin);
124 event.SetInt(eventSpin.GetPosition());
125
126 m_spin->GetEventHandler()->ProcessEvent(event);
21709999 127
5d0000b5 128 eventSpin.Skip();
21709999
JS
129 }
130
131private:
132 wxSpinCtrl *m_spin;
133
134 DECLARE_EVENT_TABLE()
135};
136
137BEGIN_EVENT_TABLE(wxSpinCtrlButton, wxSpinButton)
3fb79396 138 EVT_SPIN(wxID_ANY, wxSpinCtrlButton::OnSpinButton)
21709999
JS
139END_EVENT_TABLE()
140
141IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
739555e3 142
21709999
JS
143// ============================================================================
144// implementation
145// ============================================================================
146
147// ----------------------------------------------------------------------------
148// wxSpinCtrl creation
149// ----------------------------------------------------------------------------
150
151void wxSpinCtrl::Init()
152{
153 m_text = NULL;
154 m_btn = NULL;
155}
156
157bool wxSpinCtrl::Create(wxWindow *parent,
158 wxWindowID id,
159 const wxString& value,
3fb79396 160 const wxPoint& WXUNUSED(pos),
21709999
JS
161 const wxSize& size,
162 long style,
163 int min,
164 int max,
165 int initial,
166 const wxString& name)
167{
094ca819 168 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style,
21709999
JS
169 wxDefaultValidator, name) )
170 {
3fb79396 171 return false;
21709999
JS
172 }
173
71e03035
VZ
174 // the string value overrides the numeric one (for backwards compatibility
175 // reasons and also because it is simpler to satisfy the string value which
176 // comes much sooner in the list of arguments and leave the initial
177 // parameter unspecified)
178 if ( !value.empty() )
179 {
180 long l;
181 if ( value.ToLong(&l) )
182 initial = l;
183 }
184
21709999
JS
185 m_text = new wxSpinCtrlText(this, value);
186 m_btn = new wxSpinCtrlButton(this, style);
ca65c044 187
21709999
JS
188 m_btn->SetRange(min, max);
189 m_btn->SetValue(initial);
e459a794 190 SetBestSize(size);
ca65c044 191
21709999
JS
192 // have to disable this window to avoid interfering it with message
193 // processing to the text and the button... but pretend it is enabled to
3fb79396
WS
194 // make IsEnabled() return true
195 wxControl::Enable(false); // don't use non virtual Disable() here!
196 m_isEnabled = true;
21709999
JS
197
198 // we don't even need to show this window itself - and not doing it avoids
199 // that it overwrites the text control
3fb79396
WS
200 wxControl::Show(false);
201 m_isShown = true;
202 return true;
21709999
JS
203}
204
205wxSpinCtrl::~wxSpinCtrl()
206{
e459a794 207 // delete the controls now, don't leave them alive even though they would
21709999
JS
208 // still be eventually deleted by our parent - but it will be too late, the
209 // user code expects them to be gone now
210 delete m_text;
e459a794 211 m_text = NULL ;
21709999 212 delete m_btn;
e459a794 213 m_btn = NULL ;
21709999
JS
214}
215
216// ----------------------------------------------------------------------------
217// geometry
218// ----------------------------------------------------------------------------
219
dba00620 220wxSize wxSpinCtrl::DoGetBestSize() const
21709999
JS
221{
222 wxSize sizeBtn = m_btn->GetBestSize(),
223 sizeText = m_text->GetBestSize();
224
225 return wxSize(sizeBtn.x + sizeText.x + MARGIN, sizeText.y);
226}
227
228void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height)
229{
230 wxControl::DoMoveWindow(x, y, width, height);
231
232 // position the subcontrols inside the client area
3379ed37 233 wxSize sizeBtn = m_btn->GetSize();
21709999
JS
234
235 wxCoord wText = width - sizeBtn.x;
e466fe5f 236 m_text->SetSize(x, y, wText, height);
422d0ff0 237 m_btn->SetSize(x + wText + MARGIN, y, wxDefaultCoord, height);
21709999
JS
238}
239
240// ----------------------------------------------------------------------------
241// operations forwarded to the subcontrols
242// ----------------------------------------------------------------------------
243
244bool wxSpinCtrl::Enable(bool enable)
245{
246 if ( !wxControl::Enable(enable) )
3fb79396 247 return false;
21709999
JS
248
249 m_btn->Enable(enable);
250 m_text->Enable(enable);
251
3fb79396 252 return true;
21709999
JS
253}
254
255bool wxSpinCtrl::Show(bool show)
256{
257 if ( !wxControl::Show(show) )
3fb79396 258 return false;
21709999 259
3379ed37
VZ
260 // under GTK Show() is called the first time before we are fully
261 // constructed
262 if ( m_btn )
263 {
264 m_btn->Show(show);
265 m_text->Show(show);
266 }
21709999 267
3fb79396 268 return true;
21709999
JS
269}
270
271// ----------------------------------------------------------------------------
272// value and range access
273// ----------------------------------------------------------------------------
274
275bool wxSpinCtrl::GetTextValue(int *val) const
276{
277 long l;
278 if ( !m_text->GetValue().ToLong(&l) )
279 {
280 // not a number at all
3fb79396 281 return false;
21709999
JS
282 }
283
284 if ( l < GetMin() || l > GetMax() )
285 {
286 // out of range
3fb79396 287 return false;
21709999
JS
288 }
289
290 *val = l;
291
3fb79396 292 return true;
21709999
JS
293}
294
295int wxSpinCtrl::GetValue() const
296{
297 return m_btn ? m_btn->GetValue() : 0;
298}
299
300int wxSpinCtrl::GetMin() const
301{
302 return m_btn ? m_btn->GetMin() : 0;
303}
304
305int wxSpinCtrl::GetMax() const
306{
307 return m_btn ? m_btn->GetMax() : 0;
308}
309
310// ----------------------------------------------------------------------------
311// changing value and range
312// ----------------------------------------------------------------------------
313
314void wxSpinCtrl::SetTextValue(int val)
315{
316 wxCHECK_RET( m_text, _T("invalid call to wxSpinCtrl::SetTextValue") );
317
318 m_text->SetValue(wxString::Format(_T("%d"), val));
319
320 // select all text
321 m_text->SetSelection(0, -1);
322
323 // and give focus to the control!
d45906e0 324 // m_text->SetFocus(); Why???? TODO.
21709999
JS
325}
326
327void wxSpinCtrl::SetValue(int val)
328{
329 wxCHECK_RET( m_btn, _T("invalid call to wxSpinCtrl::SetValue") );
330
331 SetTextValue(val);
332
333 m_btn->SetValue(val);
334}
335
336void wxSpinCtrl::SetValue(const wxString& text)
337{
338 wxCHECK_RET( m_text, _T("invalid call to wxSpinCtrl::SetValue") );
339
340 long val;
341 if ( text.ToLong(&val) && ((val > INT_MIN) && (val < INT_MAX)) )
342 {
343 SetValue((int)val);
344 }
345 else // not a number at all or out of range
346 {
347 m_text->SetValue(text);
348 m_text->SetSelection(0, -1);
349 }
350}
351
352void wxSpinCtrl::SetRange(int min, int max)
353{
354 wxCHECK_RET( m_btn, _T("invalid call to wxSpinCtrl::SetRange") );
355
356 m_btn->SetRange(min, max);
357}
358
739555e3
VZ
359void wxSpinCtrl::SetSelection(long from, long to)
360{
361 wxCHECK_RET( m_text, _T("invalid call to wxSpinCtrl::SetSelection") );
362
363 m_text->SetSelection(from, to);
364}
365
c67d6888 366#endif // wxUSE_SPINCTRL
3379ed37 367#endif // !wxPort-with-native-spinctrl