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 static void wxSplitMessage2( const wxString
&message
, wxWindow
*parent
, wxSizer
* sizer
)
54 for (size_t pos
= 0; pos
< message
.Len(); pos
++)
56 if (message
[pos
] == _T('\n'))
60 wxStaticText
*s1
= new wxStaticText( parent
, -1, line
);
71 // remaining text behind last '\n'
74 wxStaticText
*s2
= new wxStaticText( parent
, -1, line
);
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 class WXDLLEXPORT wxNumberEntryDialog
: public wxDialog
87 wxNumberEntryDialog(wxWindow
*parent
,
88 const wxString
& message
,
89 const wxString
& prompt
,
90 const wxString
& caption
,
91 long value
, long min
, long max
,
94 long GetValue() const { return m_value
; }
96 // implementation only
97 void OnOK(wxCommandEvent
& event
);
98 void OnCancel(wxCommandEvent
& event
);
101 wxTextCtrl
*m_spinctrl
; // TODO replace it with wxSpinCtrl once it's done
103 long m_value
, m_min
, m_max
;
106 DECLARE_EVENT_TABLE()
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
114 // wxNumberEntryDialog
115 // ----------------------------------------------------------------------------
117 BEGIN_EVENT_TABLE(wxNumberEntryDialog
, wxDialog
)
118 EVT_BUTTON(wxID_OK
, wxNumberEntryDialog::OnOK
)
119 EVT_BUTTON(wxID_CANCEL
, wxNumberEntryDialog::OnCancel
)
122 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow
*parent
,
123 const wxString
& message
,
124 const wxString
& prompt
,
125 const wxString
& caption
,
130 : wxDialog(parent
, -1, caption
,
132 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
)
140 wxBox
*topsizer
= new wxBox( wxVERTICAL
);
143 wxBox
*textsizer
= new wxBox( wxVERTICAL
);
144 wxSplitMessage2( message
, this, textsizer
);
145 topsizer
->Add( textsizer
, 0, wxALL
, 10 );
147 // 2) prompt and text ctrl
148 wxBox
*inputsizer
= new wxBox( wxHORIZONTAL
);
150 if (!prompt
.IsEmpty())
151 inputsizer
->Add( new wxStaticText( this, -1, prompt
), 0, wxCENTER
| wxLEFT
, 10 );
154 valStr
.Printf(_T("%lu"), m_value
);
155 m_spinctrl
= new wxTextCtrl(this, -1, valStr
, wxDefaultPosition
, wxSize( 140, -1 ) );
156 inputsizer
->Add( m_spinctrl
, 1, wxCENTER
| wxLEFT
| wxRIGHT
, 10 );
158 topsizer
->Add( inputsizer
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
162 topsizer
->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
167 wxBox
*buttonsizer
= new wxBox( wxHORIZONTAL
);
169 wxButton
*ok
= (wxButton
*) NULL
;
172 ok
= new wxButton( this, wxID_OK
, _("OK") );
173 buttonsizer
->Add( ok
, 0, wxLEFT
|wxRIGHT
, 10 );
176 wxButton
*cancel
= (wxButton
*) NULL
;
177 // if (style & wxCANCEL)
179 cancel
= new wxButton( this, wxID_CANCEL
, _("Cancel") );
180 buttonsizer
->Add( cancel
, 0, wxLEFT
|wxRIGHT
, 10 );
183 topsizer
->Add( buttonsizer
, 0, wxCENTRE
| wxALL
, 10 );
185 SetSizer( topsizer
);
186 SetAutoLayout( TRUE
);
188 topsizer
->SetSizeHints( this );
189 topsizer
->Fit( this );
196 m_spinctrl
->SetFocus();
201 void wxNumberEntryDialog::OnOK(wxCommandEvent
& event
)
203 if ( (wxSscanf(m_spinctrl
->GetValue(), _T("%lu"), &m_value
) != 1) ||
204 (m_value
< m_min
) || (m_value
> m_max
) )
206 // not a number or out of range
213 void wxNumberEntryDialog::OnCancel(wxCommandEvent
& event
)
217 EndModal(wxID_CANCEL
);
220 // ----------------------------------------------------------------------------
222 // ----------------------------------------------------------------------------
224 // wxGetTextFromUser is in utilscmn.cpp
226 long wxGetNumberFromUser(const wxString
& msg
,
227 const wxString
& prompt
,
228 const wxString
& title
,
235 wxNumberEntryDialog
dialog(parent
, msg
, prompt
, title
,
236 value
, min
, max
, pos
);
237 (void)dialog
.ShowModal();
239 return dialog
.GetValue();