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 implementation "numdlgg.cpp"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/dialog.h"
36 #include "wx/button.h"
37 #include "wx/stattext.h"
38 #include "wx/textctrl.h"
44 #include "wx/statline.h"
47 #if !defined(__WIN16__) && wxUSE_SPINCTRL
48 #include "wx/spinctrl.h"
51 // this is where wxGetNumberFromUser() is declared
52 #include "wx/textdlg.h"
55 // wxTextCtrl will do instead of wxSpinCtrl if we don't have it
56 #define wxSpinCtrl wxTextCtrl
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 class WXDLLEXPORT wxNumberEntryDialog
: public wxDialog
66 wxNumberEntryDialog(wxWindow
*parent
,
67 const wxString
& message
,
68 const wxString
& prompt
,
69 const wxString
& caption
,
70 long value
, long min
, long max
,
73 long GetValue() const { return m_value
; }
75 // implementation only
76 void OnOK(wxCommandEvent
& event
);
77 void OnCancel(wxCommandEvent
& event
);
80 wxSpinCtrl
*m_spinctrl
;
82 long m_value
, m_min
, m_max
;
88 // ============================================================================
90 // ============================================================================
92 // ----------------------------------------------------------------------------
93 // wxNumberEntryDialog
94 // ----------------------------------------------------------------------------
96 BEGIN_EVENT_TABLE(wxNumberEntryDialog
, wxDialog
)
97 EVT_BUTTON(wxID_OK
, wxNumberEntryDialog::OnOK
)
98 EVT_BUTTON(wxID_CANCEL
, wxNumberEntryDialog::OnCancel
)
101 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow
*parent
,
102 const wxString
& message
,
103 const wxString
& prompt
,
104 const wxString
& caption
,
109 : wxDialog(parent
, -1, caption
,
111 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
)
119 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
122 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
124 // 2) prompt and text ctrl
125 wxBoxSizer
*inputsizer
= new wxBoxSizer( wxHORIZONTAL
);
127 if (!prompt
.IsEmpty())
128 inputsizer
->Add( new wxStaticText( this, -1, prompt
), 0, wxCENTER
| wxLEFT
, 10 );
131 valStr
.Printf(wxT("%lu"), m_value
);
132 m_spinctrl
= new wxSpinCtrl(this, -1, valStr
, wxDefaultPosition
, wxSize( 140, -1 ) );
133 #if !defined(__WIN16__) && wxUSE_SPINCTRL
134 m_spinctrl
->SetRange((int)m_min
, (int)m_max
);
136 inputsizer
->Add( m_spinctrl
, 1, wxCENTER
| wxLEFT
| wxRIGHT
, 10 );
138 topsizer
->Add( inputsizer
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
142 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
146 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
148 SetSizer( topsizer
);
149 SetAutoLayout( TRUE
);
151 topsizer
->SetSizeHints( this );
152 topsizer
->Fit( this );
156 m_spinctrl
->SetFocus();
161 void wxNumberEntryDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
164 wxString tmp
= m_spinctrl
->GetValue();
165 if ( wxSscanf(tmp
, _T("%ld"), &m_value
) != 1 )
169 m_value
= m_spinctrl
->GetValue();
171 if ( m_value
< m_min
|| m_value
> m_max
)
173 // not a number or out of range
180 void wxNumberEntryDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
184 EndModal(wxID_CANCEL
);
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
191 // wxGetTextFromUser is in utilscmn.cpp
193 long wxGetNumberFromUser(const wxString
& msg
,
194 const wxString
& prompt
,
195 const wxString
& title
,
202 wxNumberEntryDialog
dialog(parent
, msg
, prompt
, title
,
203 value
, min
, max
, pos
);
204 (void)dialog
.ShowModal();
206 return dialog
.GetValue();