]>
Commit | Line | Data |
---|---|---|
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->SetFocus(); | |
160 | ||
161 | wxEndBusyCursor(); | |
162 | } | |
163 | ||
164 | void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event)) | |
165 | { | |
166 | #if !wxUSE_SPINCTRL | |
167 | wxString tmp = m_spinctrl->GetValue(); | |
168 | if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 ) | |
169 | m_value = -1; | |
170 | else | |
171 | #else | |
172 | m_value = m_spinctrl->GetValue(); | |
173 | #endif | |
174 | if ( m_value < m_min || m_value > m_max ) | |
175 | { | |
176 | // not a number or out of range | |
177 | m_value = -1; | |
178 | } | |
179 | ||
180 | EndModal(wxID_OK); | |
181 | } | |
182 | ||
183 | void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) | |
184 | { | |
185 | m_value = -1; | |
186 | ||
187 | EndModal(wxID_CANCEL); | |
188 | } | |
189 | ||
190 | // ---------------------------------------------------------------------------- | |
191 | // global functions | |
192 | // ---------------------------------------------------------------------------- | |
193 | ||
194 | // wxGetTextFromUser is in utilscmn.cpp | |
195 | ||
196 | long wxGetNumberFromUser(const wxString& msg, | |
197 | const wxString& prompt, | |
198 | const wxString& title, | |
199 | long value, | |
200 | long min, | |
201 | long max, | |
202 | wxWindow *parent, | |
203 | const wxPoint& pos) | |
204 | { | |
205 | wxNumberEntryDialog dialog(parent, msg, prompt, title, | |
206 | value, min, max, pos); | |
207 | (void)dialog.ShowModal(); | |
208 | ||
209 | return dialog.GetValue(); | |
210 | } | |
211 | ||
212 | #endif // wxUSE_NUMBERDLG |