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 #include "wx/spinctrl.h"
50 // this is where wxGetNumberFromUser() is declared
51 #include "wx/generic/textdlgg.h"
54 // wxTextCtrl will do instead of wxSpinCtrl if we don't have it
55 #define wxSpinCtrl wxTextCtrl
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 class WXDLLEXPORT wxNumberEntryDialog
: public wxDialog
65 wxNumberEntryDialog(wxWindow
*parent
,
66 const wxString
& message
,
67 const wxString
& prompt
,
68 const wxString
& caption
,
69 long value
, long min
, long max
,
72 long GetValue() const { return m_value
; }
74 // implementation only
75 void OnOK(wxCommandEvent
& event
);
76 void OnCancel(wxCommandEvent
& event
);
79 wxSpinCtrl
*m_spinctrl
;
81 long m_value
, m_min
, m_max
;
87 // ============================================================================
89 // ============================================================================
91 // ----------------------------------------------------------------------------
92 // wxNumberEntryDialog
93 // ----------------------------------------------------------------------------
95 BEGIN_EVENT_TABLE(wxNumberEntryDialog
, wxDialog
)
96 EVT_BUTTON(wxID_OK
, wxNumberEntryDialog::OnOK
)
97 EVT_BUTTON(wxID_CANCEL
, wxNumberEntryDialog::OnCancel
)
100 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow
*parent
,
101 const wxString
& message
,
102 const wxString
& prompt
,
103 const wxString
& caption
,
108 : wxDialog(parent
, -1, caption
,
110 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
)
118 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
121 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
123 // 2) prompt and text ctrl
124 wxBoxSizer
*inputsizer
= new wxBoxSizer( wxHORIZONTAL
);
126 if (!prompt
.IsEmpty())
127 inputsizer
->Add( new wxStaticText( this, -1, prompt
), 0, wxCENTER
| wxLEFT
, 10 );
130 valStr
.Printf(wxT("%lu"), m_value
);
131 m_spinctrl
= new wxSpinCtrl(this, -1, valStr
, wxDefaultPosition
, wxSize( 140, -1 ) );
132 m_spinctrl
-> SetRange(m_min
, m_max
);
133 inputsizer
->Add( m_spinctrl
, 1, wxCENTER
| wxLEFT
| wxRIGHT
, 10 );
135 topsizer
->Add( inputsizer
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
139 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
143 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
145 SetSizer( topsizer
);
146 SetAutoLayout( TRUE
);
148 topsizer
->SetSizeHints( this );
149 topsizer
->Fit( this );
153 m_spinctrl
->SetFocus();
158 void wxNumberEntryDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
161 wxString tmp
= m_spinctrl
->GetValue();
162 if ( wxSscanf(tmp
, _T("%ld"), &m_value
) != 1 )
166 m_value
= m_spinctrl
->GetValue();
168 if ( m_value
< m_min
|| m_value
> m_max
)
170 // not a number or out of range
177 void wxNumberEntryDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
181 EndModal(wxID_CANCEL
);
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 // wxGetTextFromUser is in utilscmn.cpp
190 long wxGetNumberFromUser(const wxString
& msg
,
191 const wxString
& prompt
,
192 const wxString
& title
,
199 wxNumberEntryDialog
dialog(parent
, msg
, prompt
, title
,
200 value
, min
, max
, pos
);
201 (void)dialog
.ShowModal();
203 return dialog
.GetValue();