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/textdlg.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 ) );
133 m_spinctrl
->SetRange((int)m_min
, (int)m_max
);
135 inputsizer
->Add( m_spinctrl
, 1, wxCENTER
| wxLEFT
| wxRIGHT
, 10 );
137 topsizer
->Add( inputsizer
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
141 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
145 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
147 SetSizer( topsizer
);
148 SetAutoLayout( TRUE
);
150 topsizer
->SetSizeHints( this );
151 topsizer
->Fit( this );
155 m_spinctrl
->SetFocus();
160 void wxNumberEntryDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
163 wxString tmp
= m_spinctrl
->GetValue();
164 if ( wxSscanf(tmp
, _T("%ld"), &m_value
) != 1 )
168 m_value
= m_spinctrl
->GetValue();
170 if ( m_value
< m_min
|| m_value
> m_max
)
172 // not a number or out of range
179 void wxNumberEntryDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
183 EndModal(wxID_CANCEL
);
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
190 // wxGetTextFromUser is in utilscmn.cpp
192 long wxGetNumberFromUser(const wxString
& msg
,
193 const wxString
& prompt
,
194 const wxString
& title
,
201 wxNumberEntryDialog
dialog(parent
, msg
, prompt
, title
,
202 value
, min
, max
, pos
);
203 (void)dialog
.ShowModal();
205 return dialog
.GetValue();