]> git.saurik.com Git - wxWidgets.git/blob - src/generic/numdlgg.cpp
Some doc tweaks
[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 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 };
89
90 // ============================================================================
91 // implementation
92 // ============================================================================
93
94 // ----------------------------------------------------------------------------
95 // wxNumberEntryDialog
96 // ----------------------------------------------------------------------------
97
98 BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
99 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
100 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
101 END_EVENT_TABLE()
102
103 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
104 const wxString& message,
105 const wxString& prompt,
106 const wxString& caption,
107 long value,
108 long min,
109 long max,
110 const wxPoint& pos)
111 : wxDialog(parent, -1, caption,
112 pos, wxDefaultSize,
113 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL )
114 {
115 m_value = value;
116 m_max = max;
117 m_min = min;
118
119 wxBeginBusyCursor();
120
121 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
122
123 // 1) text message
124 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
125
126 // 2) prompt and text ctrl
127 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
128 // prompt if any
129 if (!prompt.IsEmpty())
130 inputsizer->Add( new wxStaticText( this, -1, prompt ), 0, wxCENTER | wxLEFT, 10 );
131 // spin ctrl
132 wxString valStr;
133 valStr.Printf(wxT("%lu"), m_value);
134 m_spinctrl = new wxSpinCtrl(this, -1, valStr, wxDefaultPosition, wxSize( 140, -1 ) );
135 #if !defined(__WIN16__) && wxUSE_SPINCTRL
136 m_spinctrl->SetRange((int)m_min, (int)m_max);
137 #endif
138 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
139 // add both
140 topsizer->Add( inputsizer, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
141
142 #if wxUSE_STATLINE
143 // 3) static line
144 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
145 #endif
146
147 // 4) buttons
148 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
149
150 SetSizer( topsizer );
151 SetAutoLayout( TRUE );
152
153 topsizer->SetSizeHints( this );
154 topsizer->Fit( this );
155
156 Centre( wxBOTH );
157
158 m_spinctrl->SetFocus();
159
160 wxEndBusyCursor();
161 }
162
163 void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
164 {
165 #if !wxUSE_SPINCTRL
166 wxString tmp = m_spinctrl->GetValue();
167 if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 )
168 m_value = -1;
169 else
170 #else
171 m_value = m_spinctrl->GetValue();
172 #endif
173 if ( m_value < m_min || m_value > m_max )
174 {
175 // not a number or out of range
176 m_value = -1;
177 }
178
179 EndModal(wxID_OK);
180 }
181
182 void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
183 {
184 m_value = -1;
185
186 EndModal(wxID_CANCEL);
187 }
188
189 // ----------------------------------------------------------------------------
190 // global functions
191 // ----------------------------------------------------------------------------
192
193 // wxGetTextFromUser is in utilscmn.cpp
194
195 long wxGetNumberFromUser(const wxString& msg,
196 const wxString& prompt,
197 const wxString& title,
198 long value,
199 long min,
200 long max,
201 wxWindow *parent,
202 const wxPoint& pos)
203 {
204 wxNumberEntryDialog dialog(parent, msg, prompt, title,
205 value, min, max, pos);
206 (void)dialog.ShowModal();
207
208 return dialog.GetValue();
209 }
210
211 #endif // wxUSE_NUMBERDLG