1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxGetNumberFromUser implementation 
   4 // Author:      Vadim Zeitlin 
   8 // Copyright:   (c) Vadim Zeitlin 
   9 // Licence:     wxWindows licence 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // ============================================================================ 
  14 // ============================================================================ 
  16 // ---------------------------------------------------------------------------- 
  18 // ---------------------------------------------------------------------------- 
  20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) 
  21     #pragma implementation "numdlgg.cpp" 
  24 // For compilers that support precompilation, includes "wx.h". 
  25 #include "wx/wxprec.h" 
  37     #include "wx/dialog.h" 
  38     #include "wx/button.h" 
  39     #include "wx/stattext.h" 
  40     #include "wx/textctrl.h" 
  46   #include "wx/statline.h" 
  49 #if !defined(__WIN16__) && wxUSE_SPINCTRL 
  50 #include "wx/spinctrl.h" 
  53 // this is where wxGetNumberFromUser() is declared 
  54 #include "wx/textdlg.h" 
  57     // wxTextCtrl will do instead of wxSpinCtrl if we don't have it 
  58     #define wxSpinCtrl wxTextCtrl 
  61 // ---------------------------------------------------------------------------- 
  63 // ---------------------------------------------------------------------------- 
  65 class WXDLLEXPORT wxNumberEntryDialog 
: public wxDialog
 
  68     wxNumberEntryDialog(wxWindow 
*parent
, 
  69                         const wxString
& message
, 
  70                         const wxString
& prompt
, 
  71                         const wxString
& caption
, 
  72                         long value
, long min
, long max
, 
  75     long GetValue() const { return m_value
; } 
  77     // implementation only 
  78     void OnOK(wxCommandEvent
& event
); 
  79     void OnCancel(wxCommandEvent
& event
); 
  82     wxSpinCtrl 
*m_spinctrl
; 
  84     long m_value
, m_min
, m_max
; 
  88     DECLARE_NO_COPY_CLASS(wxNumberEntryDialog
) 
  91 // ============================================================================ 
  93 // ============================================================================ 
  95 // ---------------------------------------------------------------------------- 
  96 // wxNumberEntryDialog 
  97 // ---------------------------------------------------------------------------- 
  99 BEGIN_EVENT_TABLE(wxNumberEntryDialog
, wxDialog
) 
 100     EVT_BUTTON(wxID_OK
, wxNumberEntryDialog::OnOK
) 
 101     EVT_BUTTON(wxID_CANCEL
, wxNumberEntryDialog::OnCancel
) 
 104 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow 
*parent
, 
 105                                          const wxString
& message
, 
 106                                          const wxString
& prompt
, 
 107                                          const wxString
& caption
, 
 112                    : wxDialog(parent
, -1, caption
, 
 114                               wxDEFAULT_DIALOG_STYLE 
| wxDIALOG_MODAL 
) 
 122     wxBoxSizer 
*topsizer 
= new wxBoxSizer( wxVERTICAL 
); 
 125     topsizer
->Add( CreateTextSizer( message 
), 0, wxALL
, 10 ); 
 127     // 2) prompt and text ctrl 
 128     wxBoxSizer 
*inputsizer 
= new wxBoxSizer( wxHORIZONTAL 
); 
 130     if (!prompt
.IsEmpty()) 
 131         inputsizer
->Add( new wxStaticText( this, -1, prompt 
), 0, wxCENTER 
| wxLEFT
, 10 ); 
 134     valStr
.Printf(wxT("%lu"), m_value
); 
 135     m_spinctrl 
= new wxSpinCtrl(this, -1, valStr
, wxDefaultPosition
, wxSize( 140, -1 ) ); 
 136 #if !defined(__WIN16__) && wxUSE_SPINCTRL 
 137     m_spinctrl
->SetRange((int)m_min
, (int)m_max
); 
 139     inputsizer
->Add( m_spinctrl
, 1, wxCENTER 
| wxLEFT 
| wxRIGHT
, 10 ); 
 141     topsizer
->Add( inputsizer
, 1, wxEXPAND 
| wxLEFT
|wxRIGHT
, 5 ); 
 145     topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND 
| wxLEFT
|wxRIGHT
|wxTOP
, 10 ); 
 149     topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL 
), 0, wxCENTRE 
| wxALL
, 10 ); 
 151     SetSizer( topsizer 
); 
 152     SetAutoLayout( TRUE 
); 
 154     topsizer
->SetSizeHints( this ); 
 155     topsizer
->Fit( this ); 
 159     m_spinctrl
->SetSelection(-1, -1); 
 160     m_spinctrl
->SetFocus(); 
 165 void wxNumberEntryDialog::OnOK(wxCommandEvent
& WXUNUSED(event
)) 
 168     wxString tmp 
= m_spinctrl
->GetValue(); 
 169     if ( wxSscanf(tmp
, _T("%ld"), &m_value
) != 1 ) 
 173     m_value 
= m_spinctrl
->GetValue(); 
 175     if ( m_value 
< m_min 
|| m_value 
> m_max 
) 
 177         // not a number or out of range 
 184 void wxNumberEntryDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
)) 
 188     EndModal(wxID_CANCEL
); 
 191 // ---------------------------------------------------------------------------- 
 193 // ---------------------------------------------------------------------------- 
 195 // wxGetTextFromUser is in utilscmn.cpp 
 197 long wxGetNumberFromUser(const wxString
& msg
, 
 198                          const wxString
& prompt
, 
 199                          const wxString
& title
, 
 206     wxNumberEntryDialog 
dialog(parent
, msg
, prompt
, title
, 
 207                                value
, min
, max
, pos
); 
 208     (void)dialog
.ShowModal(); 
 210     return dialog
.GetValue(); 
 213 #endif // wxUSE_NUMBERDLG