1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTextEntryDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "textdlgg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
35 #include "wx/dialog.h"
36 #include "wx/button.h"
37 #include "wx/stattext.h"
38 #include "wx/textctrl.h"
43 #include "wx/statline.h"
46 #include "wx/generic/textdlgg.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 class WXDLLEXPORT wxNumberEntryDialog
: public wxDialog
55 wxNumberEntryDialog(wxWindow
*parent
,
56 const wxString
& message
,
57 const wxString
& prompt
,
58 const wxString
& caption
,
59 long value
, long min
, long max
,
62 long GetValue() const { return m_value
; }
64 // implementation only
65 void OnOK(wxCommandEvent
& event
);
66 void OnCancel(wxCommandEvent
& event
);
69 wxTextCtrl
*m_spinctrl
; // TODO replace it with wxSpinCtrl once it's done
71 long m_value
, m_min
, m_max
;
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 static const int wxID_TEXT
= 3000;
83 // ============================================================================
85 // ============================================================================
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
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
)
97 BEGIN_EVENT_TABLE(wxTextEntryDialog
, wxDialog
)
98 EVT_BUTTON(wxID_OK
, wxTextEntryDialog::OnOK
)
101 IMPLEMENT_CLASS(wxTextEntryDialog
, wxDialog
)
104 wxTextEntryDialog::wxTextEntryDialog(wxWindow
*parent
,
105 const wxString
& message
,
106 const wxString
& caption
,
107 const wxString
& value
,
110 : wxDialog(parent
, -1, caption
, pos
, wxDefaultSize
,
111 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
),
114 // calculate the sizes
115 // -------------------
118 wxSize sizeText
= SplitTextMessage(message
, &lines
);
120 wxSize sizeBtn
= GetStandardButtonSize();
122 long wText
= wxMax(4*sizeBtn
.GetWidth(), sizeText
.GetWidth());
123 long hText
= GetStandardTextHeight();
125 long wDialog
= 4*LAYOUT_X_MARGIN
+ wText
;
126 long hDialog
= 2*LAYOUT_Y_MARGIN
+
127 sizeText
.GetHeight() * lines
.GetCount() +
131 sizeBtn
.GetHeight() +
134 // create the controls
135 // -------------------
138 long x
= 2*LAYOUT_X_MARGIN
;
139 long y
= CreateTextMessage(lines
,
140 wxPoint(x
, 2*LAYOUT_Y_MARGIN
),
143 y
+= 2*LAYOUT_X_MARGIN
;
146 m_textctrl
= new wxTextCtrl(this, wxID_TEXT
, m_value
,
148 wxSize(wText
, hText
));
149 y
+= hText
+ 2*LAYOUT_X_MARGIN
;
152 CreateStandardButtons(wDialog
, y
, sizeBtn
.GetWidth(), sizeBtn
.GetHeight());
154 // set the dialog size and position
155 SetClientSize(wDialog
, hDialog
);
156 if ( pos
== wxDefaultPosition
)
158 // centre the dialog if no explicit position given
159 Centre(wxBOTH
| wxCENTER_FRAME
);
162 m_textctrl
->SetFocus();
165 void wxTextEntryDialog::OnOK(wxCommandEvent
& WXUNUSED(event
) )
167 m_value
= m_textctrl
->GetValue();
172 // ----------------------------------------------------------------------------
173 // wxNumberEntryDialog
174 // ----------------------------------------------------------------------------
176 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow
*parent
,
177 const wxString
& message
,
178 const wxString
& prompt
,
179 const wxString
& caption
,
184 : wxDialog(parent
, -1, caption
,
186 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
)
195 // calculate the sizes
196 // -------------------
199 wxSize sizeText
= SplitTextMessage(message
, &lines
);
201 wxSize sizeBtn
= GetStandardButtonSize();
203 int wPrompt
, hPrompt
;
204 GetTextExtent(prompt
, &wPrompt
, &hPrompt
);
206 long wText
= wxMax(2*sizeBtn
.GetWidth(),
207 wxMax(wPrompt
, sizeText
.GetWidth()));
208 long hText
= GetStandardTextHeight();
210 long wDialog
= 5*LAYOUT_X_MARGIN
+ wText
+ wPrompt
;
211 long hDialog
= 2*LAYOUT_Y_MARGIN
+
212 sizeText
.GetHeight() * lines
.GetCount() +
216 sizeBtn
.GetHeight() +
219 // create the controls
220 // -------------------
223 long x
= 2*LAYOUT_X_MARGIN
;
224 long y
= CreateTextMessage(lines
,
225 wxPoint(x
, 2*LAYOUT_Y_MARGIN
),
228 y
+= 2*LAYOUT_X_MARGIN
;
231 (void)new wxStaticText(this, -1, prompt
,
233 wxSize(wPrompt
, hPrompt
));
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
;
244 CreateStandardButtons(wDialog
, y
, sizeBtn
.GetWidth(), sizeBtn
.GetHeight());
246 // set the dialog size and position
247 SetClientSize(wDialog
, hDialog
);
248 if ( pos
== wxDefaultPosition
)
250 // centre the dialog if no explicit position given
251 Centre(wxBOTH
| wxCENTER_FRAME
);
254 m_spinctrl
->SetFocus();
257 void wxNumberEntryDialog::OnOK(wxCommandEvent
& event
)
259 if ( (sscanf(m_spinctrl
->GetValue(), "%lu", &m_value
) != 1) ||
260 (m_value
< m_min
) || (m_value
> m_max
) )
262 // not a number or out of range
269 void wxNumberEntryDialog::OnCancel(wxCommandEvent
& event
)
273 EndModal(wxID_CANCEL
);
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
280 // wxGetTextFromUser is in utilscmn.cpp
282 long wxGetNumberFromUser(const wxString
& msg
,
283 const wxString
& prompt
,
284 const wxString
& title
,
291 wxNumberEntryDialog
dialog(parent
, msg
, prompt
, title
,
292 value
, min
, max
, pos
);
293 (void)dialog
.ShowModal();
295 return dialog
.GetValue();