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 // this is where wxGetNumberFromUser() is declared
49 #include "wx/generic/textdlgg.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 class WXDLLEXPORT wxNumberEntryDialog
: public wxDialog
58 wxNumberEntryDialog(wxWindow
*parent
,
59 const wxString
& message
,
60 const wxString
& prompt
,
61 const wxString
& caption
,
62 long value
, long min
, long max
,
65 long GetValue() const { return m_value
; }
67 // implementation only
68 void OnOK(wxCommandEvent
& event
);
69 void OnCancel(wxCommandEvent
& event
);
72 wxTextCtrl
*m_spinctrl
; // TODO replace it with wxSpinCtrl once it's done
74 long m_value
, m_min
, m_max
;
80 // ============================================================================
82 // ============================================================================
84 // ----------------------------------------------------------------------------
85 // wxNumberEntryDialog
86 // ----------------------------------------------------------------------------
88 BEGIN_EVENT_TABLE(wxNumberEntryDialog
, wxDialog
)
89 EVT_BUTTON(wxID_OK
, wxNumberEntryDialog::OnOK
)
90 EVT_BUTTON(wxID_CANCEL
, wxNumberEntryDialog::OnCancel
)
93 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow
*parent
,
94 const wxString
& message
,
95 const wxString
& prompt
,
96 const wxString
& caption
,
101 : wxDialog(parent
, -1, caption
,
103 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
)
111 wxBoxSizer
*topsizer
= new wxBoxSizer( wxVERTICAL
);
114 topsizer
->Add( CreateTextSizer( message
), 0, wxALL
, 10 );
116 // 2) prompt and text ctrl
117 wxBoxSizer
*inputsizer
= new wxBoxSizer( wxHORIZONTAL
);
119 if (!prompt
.IsEmpty())
120 inputsizer
->Add( new wxStaticText( this, -1, prompt
), 0, wxCENTER
| wxLEFT
, 10 );
123 valStr
.Printf(wxT("%lu"), m_value
);
124 m_spinctrl
= new wxTextCtrl(this, -1, valStr
, wxDefaultPosition
, wxSize( 140, -1 ) );
125 inputsizer
->Add( m_spinctrl
, 1, wxCENTER
| wxLEFT
| wxRIGHT
, 10 );
127 topsizer
->Add( inputsizer
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
131 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
135 topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL
), 0, wxCENTRE
| wxALL
, 10 );
137 SetSizer( topsizer
);
138 SetAutoLayout( TRUE
);
140 topsizer
->SetSizeHints( this );
141 topsizer
->Fit( this );
145 m_spinctrl
->SetFocus();
150 void wxNumberEntryDialog::OnOK(wxCommandEvent
& WXUNUSED(event
))
152 if ( (wxSscanf(m_spinctrl
->GetValue(), wxT("%lu"), &m_value
) != 1) ||
153 (m_value
< m_min
) || (m_value
> m_max
) )
155 // not a number or out of range
162 void wxNumberEntryDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
))
166 EndModal(wxID_CANCEL
);
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 // wxGetTextFromUser is in utilscmn.cpp
175 long wxGetNumberFromUser(const wxString
& msg
,
176 const wxString
& prompt
,
177 const wxString
& title
,
184 wxNumberEntryDialog
dialog(parent
, msg
, prompt
, title
,
185 value
, min
, max
, pos
);
186 (void)dialog
.ShowModal();
188 return dialog
.GetValue();