preselect the text in the dialog to allow replacing it easier (just as in the text...
[wxWidgets.git] / src / generic / numdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: numdlgg.cpp
3 // Purpose: wxGetNumberFromUser implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 23.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "numdlgg.cpp"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_NUMBERDLG
32
33 #ifndef WX_PRECOMP
34 #include <stdio.h>
35
36 #include "wx/utils.h"
37 #include "wx/dialog.h"
38 #include "wx/button.h"
39 #include "wx/stattext.h"
40 #include "wx/textctrl.h"
41 #include "wx/intl.h"
42 #include "wx/sizer.h"
43 #endif
44
45 #if wxUSE_STATLINE
46 #include "wx/statline.h"
47 #endif
48
49 #if !defined(__WIN16__) && wxUSE_SPINCTRL
50 #include "wx/spinctrl.h"
51 #endif
52
53 // this is where wxGetNumberFromUser() is declared
54 #include "wx/textdlg.h"
55
56 #if !wxUSE_SPINCTRL
57 // wxTextCtrl will do instead of wxSpinCtrl if we don't have it
58 #define wxSpinCtrl wxTextCtrl
59 #endif
60
61 // ----------------------------------------------------------------------------
62 // private classes
63 // ----------------------------------------------------------------------------
64
65 class WXDLLEXPORT wxNumberEntryDialog : public wxDialog
66 {
67 public:
68 wxNumberEntryDialog(wxWindow *parent,
69 const wxString& message,
70 const wxString& prompt,
71 const wxString& caption,
72 long value, long min, long max,
73 const wxPoint& pos);
74
75 long GetValue() const { return m_value; }
76
77 // implementation only
78 void OnOK(wxCommandEvent& event);
79 void OnCancel(wxCommandEvent& event);
80
81 protected:
82 wxSpinCtrl *m_spinctrl;
83
84 long m_value, m_min, m_max;
85
86 private:
87 DECLARE_EVENT_TABLE()
88 DECLARE_NO_COPY_CLASS(wxNumberEntryDialog)
89 };
90
91 // ============================================================================
92 // implementation
93 // ============================================================================
94
95 // ----------------------------------------------------------------------------
96 // wxNumberEntryDialog
97 // ----------------------------------------------------------------------------
98
99 BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
100 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
101 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
102 END_EVENT_TABLE()
103
104 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
105 const wxString& message,
106 const wxString& prompt,
107 const wxString& caption,
108 long value,
109 long min,
110 long max,
111 const wxPoint& pos)
112 : wxDialog(parent, -1, caption,
113 pos, wxDefaultSize,
114 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL )
115 {
116 m_value = value;
117 m_max = max;
118 m_min = min;
119
120 wxBeginBusyCursor();
121
122 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
123
124 // 1) text message
125 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
126
127 // 2) prompt and text ctrl
128 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
129 // prompt if any
130 if (!prompt.IsEmpty())
131 inputsizer->Add( new wxStaticText( this, -1, prompt ), 0, wxCENTER | wxLEFT, 10 );
132 // spin ctrl
133 wxString valStr;
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);
138 #endif
139 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
140 // add both
141 topsizer->Add( inputsizer, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
142
143 #if wxUSE_STATLINE
144 // 3) static line
145 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
146 #endif
147
148 // 4) buttons
149 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
150
151 SetSizer( topsizer );
152 SetAutoLayout( TRUE );
153
154 topsizer->SetSizeHints( this );
155 topsizer->Fit( this );
156
157 Centre( wxBOTH );
158
159 m_spinctrl->SetSelection(-1, -1);
160 m_spinctrl->SetFocus();
161
162 wxEndBusyCursor();
163 }
164
165 void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
166 {
167 #if !wxUSE_SPINCTRL
168 wxString tmp = m_spinctrl->GetValue();
169 if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 )
170 m_value = -1;
171 else
172 #else
173 m_value = m_spinctrl->GetValue();
174 #endif
175 if ( m_value < m_min || m_value > m_max )
176 {
177 // not a number or out of range
178 m_value = -1;
179 }
180
181 EndModal(wxID_OK);
182 }
183
184 void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
185 {
186 m_value = -1;
187
188 EndModal(wxID_CANCEL);
189 }
190
191 // ----------------------------------------------------------------------------
192 // global functions
193 // ----------------------------------------------------------------------------
194
195 // wxGetTextFromUser is in utilscmn.cpp
196
197 long wxGetNumberFromUser(const wxString& msg,
198 const wxString& prompt,
199 const wxString& title,
200 long value,
201 long min,
202 long max,
203 wxWindow *parent,
204 const wxPoint& pos)
205 {
206 wxNumberEntryDialog dialog(parent, msg, prompt, title,
207 value, min, max, pos);
208 (void)dialog.ShowModal();
209
210 return dialog.GetValue();
211 }
212
213 #endif // wxUSE_NUMBERDLG