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" 
  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     inputsizer
->Add( m_spinctrl
, 1, wxCENTER 
| wxLEFT 
| wxRIGHT
, 10 ); 
 134     topsizer
->Add( inputsizer
, 1, wxEXPAND 
| wxLEFT
|wxRIGHT
, 5 ); 
 138     topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND 
| wxLEFT
|wxRIGHT
|wxTOP
, 10 ); 
 142     topsizer
->Add( CreateButtonSizer( wxOK
|wxCANCEL 
), 0, wxCENTRE 
| wxALL
, 10 ); 
 144     SetSizer( topsizer 
); 
 145     SetAutoLayout( TRUE 
); 
 147     topsizer
->SetSizeHints( this ); 
 148     topsizer
->Fit( this ); 
 152     m_spinctrl
->SetFocus(); 
 157 void wxNumberEntryDialog::OnOK(wxCommandEvent
& WXUNUSED(event
)) 
 160     wxString tmp 
= m_spinctrl
->GetValue(); 
 161     if ( wxSscanf(tmp
, _T("%ld"), &m_value
) != 1 ) 
 165     m_value 
= m_spinctrl
->GetValue(); 
 167     if ( m_value 
< m_min 
|| m_value 
> m_max 
) 
 169         // not a number or out of range 
 176 void wxNumberEntryDialog::OnCancel(wxCommandEvent
& WXUNUSED(event
)) 
 180     EndModal(wxID_CANCEL
); 
 183 // ---------------------------------------------------------------------------- 
 185 // ---------------------------------------------------------------------------- 
 187 // wxGetTextFromUser is in utilscmn.cpp 
 189 long wxGetNumberFromUser(const wxString
& msg
, 
 190                          const wxString
& prompt
, 
 191                          const wxString
& title
, 
 198     wxNumberEntryDialog 
dialog(parent
, msg
, prompt
, title
, 
 199                                value
, min
, max
, pos
); 
 200     (void)dialog
.ShowModal(); 
 202     return dialog
.GetValue();