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"
43 // this is where wxGetNumberFromUser() is declared
44 #include "wx/generic/textdlgg.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 class WXDLLEXPORT wxNumberEntryDialog
: public wxDialog
53 wxNumberEntryDialog(wxWindow
*parent
,
54 const wxString
& message
,
55 const wxString
& prompt
,
56 const wxString
& caption
,
57 long value
, long min
, long max
,
60 long GetValue() const { return m_value
; }
62 // implementation only
63 void OnOK(wxCommandEvent
& event
);
64 void OnCancel(wxCommandEvent
& event
);
67 wxTextCtrl
*m_spinctrl
; // TODO replace it with wxSpinCtrl once it's done
69 long m_value
, m_min
, m_max
;
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
80 // wxNumberEntryDialog
81 // ----------------------------------------------------------------------------
83 BEGIN_EVENT_TABLE(wxNumberEntryDialog
, wxDialog
)
84 EVT_BUTTON(wxID_OK
, wxNumberEntryDialog::OnOK
)
85 EVT_BUTTON(wxID_CANCEL
, wxNumberEntryDialog::OnCancel
)
88 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow
*parent
,
89 const wxString
& message
,
90 const wxString
& prompt
,
91 const wxString
& caption
,
96 : wxDialog(parent
, -1, caption
,
98 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_MODAL
)
107 // calculate the sizes
108 // -------------------
111 wxSize sizeText
= SplitTextMessage(message
, &lines
);
113 wxSize sizeBtn
= GetStandardButtonSize();
115 int wPrompt
, hPrompt
;
116 GetTextExtent(prompt
, &wPrompt
, &hPrompt
);
118 long wText
= wxMax(2*sizeBtn
.GetWidth(),
119 wxMax(wPrompt
, sizeText
.GetWidth()));
120 long hText
= GetStandardTextHeight();
122 long wDialog
= 5*LAYOUT_X_MARGIN
+ wText
+ wPrompt
;
123 long hDialog
= 2*LAYOUT_Y_MARGIN
+
124 sizeText
.GetHeight() * lines
.GetCount() +
128 sizeBtn
.GetHeight() +
131 // create the controls
132 // -------------------
135 long x
= 2*LAYOUT_X_MARGIN
;
136 long y
= CreateTextMessage(lines
,
137 wxPoint(x
, 2*LAYOUT_Y_MARGIN
),
140 y
+= 2*LAYOUT_X_MARGIN
;
143 (void)new wxStaticText(this, -1, prompt
,
145 wxSize(wPrompt
, hPrompt
));
149 valStr
.Printf(_T("%lu"), m_value
);
150 m_spinctrl
= new wxTextCtrl(this, -1, valStr
,
151 wxPoint(x
+ wPrompt
+ LAYOUT_X_MARGIN
, y
),
152 wxSize(wText
, hText
));
153 y
+= hText
+ 2*LAYOUT_X_MARGIN
;
156 CreateStandardButtons(wDialog
, y
, sizeBtn
.GetWidth(), sizeBtn
.GetHeight());
158 // set the dialog size and position
159 SetClientSize(wDialog
, hDialog
);
160 if ( pos
== wxDefaultPosition
)
162 // centre the dialog if no explicit position given
163 Centre(wxBOTH
| wxCENTER_FRAME
);
166 m_spinctrl
->SetFocus();
169 void wxNumberEntryDialog::OnOK(wxCommandEvent
& event
)
171 if ( (wxSscanf(m_spinctrl
->GetValue(), _T("%lu"), &m_value
) != 1) ||
172 (m_value
< m_min
) || (m_value
> m_max
) )
174 // not a number or out of range
181 void wxNumberEntryDialog::OnCancel(wxCommandEvent
& event
)
185 EndModal(wxID_CANCEL
);
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
192 // wxGetTextFromUser is in utilscmn.cpp
194 long wxGetNumberFromUser(const wxString
& msg
,
195 const wxString
& prompt
,
196 const wxString
& title
,
203 wxNumberEntryDialog
dialog(parent
, msg
, prompt
, title
,
204 value
, min
, max
, pos
);
205 (void)dialog
.ShowModal();
207 return dialog
.GetValue();