]> git.saurik.com Git - wxWidgets.git/blob - src/generic/numdlgg.cpp
#if => #if defined()
[wxWidgets.git] / src / generic / numdlgg.cpp
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 // this is where wxGetNumberFromUser() is declared
49 #include "wx/generic/textdlgg.h"
50
51 static void wxSplitMessage2( const wxString &message, wxWindow *parent, wxSizer* sizer )
52 {
53 wxString line;
54 for (size_t pos = 0; pos < message.Len(); pos++)
55 {
56 if (message[pos] == _T('\n'))
57 {
58 if (!line.IsEmpty())
59 {
60 wxStaticText *s1 = new wxStaticText( parent, -1, line );
61 sizer->Add( s1 );
62 line = _T("");
63 }
64 }
65 else
66 {
67 line += message[pos];
68 }
69 }
70
71 // remaining text behind last '\n'
72 if (!line.IsEmpty())
73 {
74 wxStaticText *s2 = new wxStaticText( parent, -1, line );
75 sizer->Add( s2 );
76 }
77 }
78
79
80 // ----------------------------------------------------------------------------
81 // private classes
82 // ----------------------------------------------------------------------------
83
84 class WXDLLEXPORT wxNumberEntryDialog : public wxDialog
85 {
86 public:
87 wxNumberEntryDialog(wxWindow *parent,
88 const wxString& message,
89 const wxString& prompt,
90 const wxString& caption,
91 long value, long min, long max,
92 const wxPoint& pos);
93
94 long GetValue() const { return m_value; }
95
96 // implementation only
97 void OnOK(wxCommandEvent& event);
98 void OnCancel(wxCommandEvent& event);
99
100 protected:
101 wxTextCtrl *m_spinctrl; // TODO replace it with wxSpinCtrl once it's done
102
103 long m_value, m_min, m_max;
104
105 private:
106 DECLARE_EVENT_TABLE()
107 };
108
109 // ============================================================================
110 // implementation
111 // ============================================================================
112
113 // ----------------------------------------------------------------------------
114 // wxNumberEntryDialog
115 // ----------------------------------------------------------------------------
116
117 BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
118 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
119 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
120 END_EVENT_TABLE()
121
122 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
123 const wxString& message,
124 const wxString& prompt,
125 const wxString& caption,
126 long value,
127 long min,
128 long max,
129 const wxPoint& pos)
130 : wxDialog(parent, -1, caption,
131 pos, wxDefaultSize,
132 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL)
133 {
134 m_value = value;
135 m_max = max;
136 m_min = min;
137
138 wxBeginBusyCursor();
139
140 wxBox *topsizer = new wxBox( wxVERTICAL );
141
142 // 1) text message
143 wxBox *textsizer = new wxBox( wxVERTICAL );
144 wxSplitMessage2( message, this, textsizer );
145 topsizer->Add( textsizer, 0, wxALL, 10 );
146
147 // 2) prompt and text ctrl
148 wxBox *inputsizer = new wxBox( wxHORIZONTAL );
149 // prompt if any
150 if (!prompt.IsEmpty())
151 inputsizer->Add( new wxStaticText( this, -1, prompt ), 0, wxCENTER | wxLEFT, 10 );
152 // spin ctrl
153 wxString valStr;
154 valStr.Printf(_T("%lu"), m_value);
155 m_spinctrl = new wxTextCtrl(this, -1, valStr, wxDefaultPosition, wxSize( 140, -1 ) );
156 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
157 // add both
158 topsizer->Add( inputsizer, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
159
160 #if wxUSE_STATLINE
161 // 3) static line
162 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
163 #endif
164
165
166 // 4) buttons
167 wxBox *buttonsizer = new wxBox( wxHORIZONTAL );
168
169 wxButton *ok = (wxButton *) NULL;
170 // if (style & wxOK)
171 {
172 ok = new wxButton( this, wxID_OK, _("OK") );
173 buttonsizer->Add( ok, 0, wxLEFT|wxRIGHT, 10 );
174 }
175
176 wxButton *cancel = (wxButton *) NULL;
177 // if (style & wxCANCEL)
178 {
179 cancel = new wxButton( this, wxID_CANCEL, _("Cancel") );
180 buttonsizer->Add( cancel, 0, wxLEFT|wxRIGHT, 10 );
181 }
182
183 topsizer->Add( buttonsizer, 0, wxCENTRE | wxALL, 10 );
184
185 SetSizer( topsizer );
186 SetAutoLayout( TRUE );
187
188 topsizer->SetSizeHints( this );
189 topsizer->Fit( this );
190
191 Centre( wxBOTH );
192
193 if (ok)
194 ok->SetDefault();
195
196 m_spinctrl->SetFocus();
197
198 wxEndBusyCursor();
199 }
200
201 void wxNumberEntryDialog::OnOK(wxCommandEvent& event)
202 {
203 if ( (wxSscanf(m_spinctrl->GetValue(), _T("%lu"), &m_value) != 1) ||
204 (m_value < m_min) || (m_value > m_max) )
205 {
206 // not a number or out of range
207 m_value = -1;
208 }
209
210 EndModal(wxID_OK);
211 }
212
213 void wxNumberEntryDialog::OnCancel(wxCommandEvent& event)
214 {
215 m_value = -1;
216
217 EndModal(wxID_CANCEL);
218 }
219
220 // ----------------------------------------------------------------------------
221 // global functions
222 // ----------------------------------------------------------------------------
223
224 // wxGetTextFromUser is in utilscmn.cpp
225
226 long wxGetNumberFromUser(const wxString& msg,
227 const wxString& prompt,
228 const wxString& title,
229 long value,
230 long min,
231 long max,
232 wxWindow *parent,
233 const wxPoint& pos)
234 {
235 wxNumberEntryDialog dialog(parent, msg, prompt, title,
236 value, min, max, pos);
237 (void)dialog.ShowModal();
238
239 return dialog.GetValue();
240 }