1. added wxGetNumberFromUser (to textdlgg.cpp and dialogs sample)
[wxWidgets.git] / src / generic / textdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textdlgg.cpp
3 // Purpose: wxTextEntryDialog
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "textdlgg.h"
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 #ifndef WX_PRECOMP
32 #include <stdio.h>
33
34 #include "wx/utils.h"
35 #include "wx/dialog.h"
36 #include "wx/button.h"
37 #include "wx/stattext.h"
38 #include "wx/textctrl.h"
39 #include "wx/intl.h"
40 #endif
41
42 #if wxUSE_STATLINE
43 #include "wx/statline.h"
44 #endif
45
46 #include "wx/generic/textdlgg.h"
47
48 // ----------------------------------------------------------------------------
49 // private classes
50 // ----------------------------------------------------------------------------
51
52 class WXDLLEXPORT wxNumberEntryDialog : public wxDialog
53 {
54 public:
55 wxNumberEntryDialog(wxWindow *parent,
56 const wxString& message,
57 const wxString& prompt,
58 const wxString& caption,
59 long value, long min, long max,
60 const wxPoint& pos);
61
62 long GetValue() const { return m_value; }
63
64 // implementation only
65 void OnOK(wxCommandEvent& event);
66 void OnCancel(wxCommandEvent& event);
67
68 protected:
69 wxTextCtrl *m_spinctrl; // TODO replace it with wxSpinCtrl once it's done
70
71 long m_value, m_min, m_max;
72
73 private:
74 DECLARE_EVENT_TABLE()
75 };
76
77 // ----------------------------------------------------------------------------
78 // constants
79 // ----------------------------------------------------------------------------
80
81 static const int wxID_TEXT = 3000;
82
83 // ============================================================================
84 // implementation
85 // ============================================================================
86
87 // ----------------------------------------------------------------------------
88 // wxTextEntryDialog
89 // ----------------------------------------------------------------------------
90
91 #if !USE_SHARED_LIBRARY
92 BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
93 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
94 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
95 END_EVENT_TABLE()
96
97 BEGIN_EVENT_TABLE(wxTextEntryDialog, wxDialog)
98 EVT_BUTTON(wxID_OK, wxTextEntryDialog::OnOK)
99 END_EVENT_TABLE()
100
101 IMPLEMENT_CLASS(wxTextEntryDialog, wxDialog)
102 #endif
103
104 wxTextEntryDialog::wxTextEntryDialog(wxWindow *parent,
105 const wxString& message,
106 const wxString& caption,
107 const wxString& value,
108 long style,
109 const wxPoint& pos)
110 : wxDialog(parent, -1, caption, pos, wxDefaultSize,
111 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL),
112 m_value(value)
113 {
114 // calculate the sizes
115 // -------------------
116
117 wxArrayString lines;
118 wxSize sizeText = SplitTextMessage(message, &lines);
119
120 wxSize sizeBtn = GetStandardButtonSize();
121
122 long wText = wxMax(4*sizeBtn.GetWidth(), sizeText.GetWidth());
123 long hText = GetStandardTextHeight();
124
125 long wDialog = 4*LAYOUT_X_MARGIN + wText;
126 long hDialog = 2*LAYOUT_Y_MARGIN +
127 sizeText.GetHeight() * lines.GetCount() +
128 2*LAYOUT_Y_MARGIN +
129 hText +
130 2*LAYOUT_Y_MARGIN +
131 sizeBtn.GetHeight() +
132 2*LAYOUT_Y_MARGIN;
133
134 // create the controls
135 // -------------------
136
137 // message
138 long x = 2*LAYOUT_X_MARGIN;
139 long y = CreateTextMessage(lines,
140 wxPoint(x, 2*LAYOUT_Y_MARGIN),
141 sizeText);
142
143 y += 2*LAYOUT_X_MARGIN;
144
145 // text ctrl
146 m_textctrl = new wxTextCtrl(this, wxID_TEXT, m_value,
147 wxPoint(x, y),
148 wxSize(wText, hText));
149 y += hText + 2*LAYOUT_X_MARGIN;
150
151 // and buttons
152 CreateStandardButtons(wDialog, y, sizeBtn.GetWidth(), sizeBtn.GetHeight());
153
154 // set the dialog size and position
155 SetClientSize(wDialog, hDialog);
156 if ( pos == wxDefaultPosition )
157 {
158 // centre the dialog if no explicit position given
159 Centre(wxBOTH | wxCENTER_FRAME);
160 }
161
162 m_textctrl->SetFocus();
163 }
164
165 void wxTextEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event) )
166 {
167 m_value = m_textctrl->GetValue();
168
169 EndModal(wxID_OK);
170 }
171
172 // ----------------------------------------------------------------------------
173 // wxNumberEntryDialog
174 // ----------------------------------------------------------------------------
175
176 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
177 const wxString& message,
178 const wxString& prompt,
179 const wxString& caption,
180 long value,
181 long min,
182 long max,
183 const wxPoint& pos)
184 : wxDialog(parent, -1, caption,
185 pos, wxDefaultSize,
186 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL)
187 {
188 // init members
189 // ------------
190
191 m_value = value;
192 m_max = max;
193 m_min = min;
194
195 // calculate the sizes
196 // -------------------
197
198 wxArrayString lines;
199 wxSize sizeText = SplitTextMessage(message, &lines);
200
201 wxSize sizeBtn = GetStandardButtonSize();
202
203 int wPrompt, hPrompt;
204 GetTextExtent(prompt, &wPrompt, &hPrompt);
205
206 long wText = wxMax(2*sizeBtn.GetWidth(),
207 wxMax(wPrompt, sizeText.GetWidth()));
208 long hText = GetStandardTextHeight();
209
210 long wDialog = 5*LAYOUT_X_MARGIN + wText + wPrompt;
211 long hDialog = 2*LAYOUT_Y_MARGIN +
212 sizeText.GetHeight() * lines.GetCount() +
213 2*LAYOUT_Y_MARGIN +
214 hText +
215 2*LAYOUT_Y_MARGIN +
216 sizeBtn.GetHeight() +
217 2*LAYOUT_Y_MARGIN;
218
219 // create the controls
220 // -------------------
221
222 // message
223 long x = 2*LAYOUT_X_MARGIN;
224 long y = CreateTextMessage(lines,
225 wxPoint(x, 2*LAYOUT_Y_MARGIN),
226 sizeText);
227
228 y += 2*LAYOUT_X_MARGIN;
229
230 // prompt
231 (void)new wxStaticText(this, -1, prompt,
232 wxPoint(x, y),
233 wxSize(wPrompt, hPrompt));
234
235 // spin ctrl
236 wxString valStr;
237 valStr.Printf("%lu", m_value);
238 m_spinctrl = new wxTextCtrl(this, -1, valStr,
239 wxPoint(x + wPrompt + LAYOUT_X_MARGIN, y),
240 wxSize(wText, hText));
241 y += hText + 2*LAYOUT_X_MARGIN;
242
243 // and buttons
244 CreateStandardButtons(wDialog, y, sizeBtn.GetWidth(), sizeBtn.GetHeight());
245
246 // set the dialog size and position
247 SetClientSize(wDialog, hDialog);
248 if ( pos == wxDefaultPosition )
249 {
250 // centre the dialog if no explicit position given
251 Centre(wxBOTH | wxCENTER_FRAME);
252 }
253
254 m_spinctrl->SetFocus();
255 }
256
257 void wxNumberEntryDialog::OnOK(wxCommandEvent& event)
258 {
259 if ( (sscanf(m_spinctrl->GetValue(), "%lu", &m_value) != 1) ||
260 (m_value < m_min) || (m_value > m_max) )
261 {
262 // not a number or out of range
263 m_value = -1;
264 }
265
266 EndModal(wxID_OK);
267 }
268
269 void wxNumberEntryDialog::OnCancel(wxCommandEvent& event)
270 {
271 m_value = -1;
272
273 EndModal(wxID_CANCEL);
274 }
275
276 // ----------------------------------------------------------------------------
277 // global functions
278 // ----------------------------------------------------------------------------
279
280 // wxGetTextFromUser is in utilscmn.cpp
281
282 long wxGetNumberFromUser(const wxString& msg,
283 const wxString& prompt,
284 const wxString& title,
285 long value,
286 long min,
287 long max,
288 wxWindow *parent,
289 const wxPoint& pos)
290 {
291 wxNumberEntryDialog dialog(parent, msg, prompt, title,
292 value, min, max, pos);
293 (void)dialog.ShowModal();
294
295 return dialog.GetValue();
296 }