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