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.h"
22 #pragma implementation "numdlgg.h"
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"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 class WXDLLEXPORT wxNumberEntryDialog
: public wxDialog
60 wxNumberEntryDialog(wxWindow
*parent
,
61 const wxString
& message
,
62 const wxString
& prompt
,
63 const wxString
& caption
,
64 long value
, long min
, long max
,
67 long GetValue() const { return m_value
; }
69 // implementation only
70 void OnOK(wxCommandEvent
& event
);
71 void OnCancel(wxCommandEvent
& event
);
74 wxSpinCtrl
*m_spinctrl
;
76 long m_value
, m_min
, m_max
;
82 // ============================================================================
84 // ============================================================================
86 // ----------------------------------------------------------------------------
87 // wxNumberEntryDialog
88 // ----------------------------------------------------------------------------
90 BEGIN_EVENT_TABLE(wxNumberEntryDialog
, wxDialog
)
91 EVT_BUTTON(wxID_OK
, wxNumberEntryDialog::OnOK
)
92 EVT_BUTTON(wxID_CANCEL
, wxNumberEntryDialog::OnCancel
)
95 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow
*parent
,
96 const wxString
& message
,
97 const wxString
& prompt
,
98 const wxString
& caption
,
103 : wxDialog(parent
, -1, caption
,
105 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
)
113 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
116 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
118 // 2) prompt and text ctrl
119 wxBoxSizer
*inputsizer
= new wxBoxSizer( wxHORIZONTAL
);
121 if (!prompt
.IsEmpty())
122 inputsizer
->Add( new wxStaticText( this, -1, prompt
), 0, wxCENTER
| wxLEFT
, 10 );
125 valStr
.Printf(wxT("%lu"), m_value
);
126 m_spinctrl
= new wxSpinCtrl(this, -1, valStr
, wxDefaultPosition
, wxSize( 140, -1 ) );
127 inputsizer
->Add( m_spinctrl
, 1, wxCENTER
| wxLEFT
| wxRIGHT
, 10 );
129 topsizer
->Add( inputsizer
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
133 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
137 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
139 SetSizer( topsizer
);
140 SetAutoLayout( TRUE
);
142 topsizer
->SetSizeHints( this );
143 topsizer
->Fit( this );
147 m_spinctrl
->SetFocus();
152 void wxNumberEntryDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
154 m_value
= m_spinctrl
->GetValue();
155 if ( m_value
< m_min
|| m_value
> m_max
)
157 // not a number or out of range
164 void wxNumberEntryDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
168 EndModal(wxID_CANCEL
);
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 // wxGetTextFromUser is in utilscmn.cpp
177 long wxGetNumberFromUser(const wxString
& msg
,
178 const wxString
& prompt
,
179 const wxString
& title
,
186 wxNumberEntryDialog
dialog(parent
, msg
, prompt
, title
,
187 value
, min
, max
, pos
);
188 (void)dialog
.ShowModal();
190 return dialog
.GetValue();