1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGetNumberFromUser implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma interface "numdlgg.cpp"
22 #pragma implementation "numdlgg.cpp"
25 // For compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
36 #include "wx/dialog.h"
37 #include "wx/button.h"
38 #include "wx/stattext.h"
39 #include "wx/textctrl.h"
45 #include "wx/statline.h"
48 #if !defined(__WIN16__) && wxUSE_SPINCTRL
49 #include "wx/spinctrl.h"
52 // this is where wxGetNumberFromUser() is declared
53 #include "wx/textdlg.h"
56 // wxTextCtrl will do instead of wxSpinCtrl if we don't have it
57 #define wxSpinCtrl wxTextCtrl
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 class WXDLLEXPORT wxNumberEntryDialog
: public wxDialog
67 wxNumberEntryDialog(wxWindow
*parent
,
68 const wxString
& message
,
69 const wxString
& prompt
,
70 const wxString
& caption
,
71 long value
, long min
, long max
,
74 long GetValue() const { return m_value
; }
76 // implementation only
77 void OnOK(wxCommandEvent
& event
);
78 void OnCancel(wxCommandEvent
& event
);
81 wxSpinCtrl
*m_spinctrl
;
83 long m_value
, m_min
, m_max
;
89 // ============================================================================
91 // ============================================================================
93 // ----------------------------------------------------------------------------
94 // wxNumberEntryDialog
95 // ----------------------------------------------------------------------------
97 BEGIN_EVENT_TABLE(wxNumberEntryDialog
, wxDialog
)
98 EVT_BUTTON(wxID_OK
, wxNumberEntryDialog::OnOK
)
99 EVT_BUTTON(wxID_CANCEL
, wxNumberEntryDialog::OnCancel
)
102 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow
*parent
,
103 const wxString
& message
,
104 const wxString
& prompt
,
105 const wxString
& caption
,
110 : wxDialog(parent
, -1, caption
,
112 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
)
120 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
123 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
125 // 2) prompt and text ctrl
126 wxBoxSizer
*inputsizer
= new wxBoxSizer( wxHORIZONTAL
);
128 if (!prompt
.IsEmpty())
129 inputsizer
->Add( new wxStaticText( this, -1, prompt
), 0, wxCENTER
| wxLEFT
, 10 );
132 valStr
.Printf(wxT("%lu"), m_value
);
133 m_spinctrl
= new wxSpinCtrl(this, -1, valStr
, wxDefaultPosition
, wxSize( 140, -1 ) );
134 #if !defined(__WIN16__) && wxUSE_SPINCTRL
135 m_spinctrl
->SetRange((int)m_min
, (int)m_max
);
137 inputsizer
->Add( m_spinctrl
, 1, wxCENTER
| wxLEFT
| wxRIGHT
, 10 );
139 topsizer
->Add( inputsizer
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
143 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
147 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
149 SetSizer( topsizer
);
150 SetAutoLayout( TRUE
);
152 topsizer
->SetSizeHints( this );
153 topsizer
->Fit( this );
157 m_spinctrl
->SetFocus();
162 void wxNumberEntryDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
165 wxString tmp
= m_spinctrl
->GetValue();
166 if ( wxSscanf(tmp
, _T("%ld"), &m_value
) != 1 )
170 m_value
= m_spinctrl
->GetValue();
172 if ( m_value
< m_min
|| m_value
> m_max
)
174 // not a number or out of range
181 void wxNumberEntryDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
185 EndModal(wxID_CANCEL
);
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 // wxGetTextFromUser is in utilscmn.cpp
194 long wxGetNumberFromUser(const wxString
& msg
,
195 const wxString
& prompt
,
196 const wxString
& title
,
203 wxNumberEntryDialog
dialog(parent
, msg
, prompt
, title
,
204 value
, min
, max
, pos
);
205 (void)dialog
.ShowModal();
207 return dialog
.GetValue();