]> git.saurik.com Git - wxWidgets.git/blob - src/generic/numdlgg.cpp
fix from Robert
[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.cpp"
22 #pragma implementation "numdlgg.cpp"
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 #if !defined(__WIN16__) && wxUSE_SPINCTRL
49 #include "wx/spinctrl.h"
50 #endif
51
52 // this is where wxGetNumberFromUser() is declared
53 #include "wx/textdlg.h"
54
55 #if !wxUSE_SPINCTRL
56 // wxTextCtrl will do instead of wxSpinCtrl if we don't have it
57 #define wxSpinCtrl wxTextCtrl
58 #endif
59
60 // ----------------------------------------------------------------------------
61 // private classes
62 // ----------------------------------------------------------------------------
63
64 class WXDLLEXPORT wxNumberEntryDialog : public wxDialog
65 {
66 public:
67 wxNumberEntryDialog(wxWindow *parent,
68 const wxString& message,
69 const wxString& prompt,
70 const wxString& caption,
71 long value, long min, long max,
72 const wxPoint& pos);
73
74 long GetValue() const { return m_value; }
75
76 // implementation only
77 void OnOK(wxCommandEvent& event);
78 void OnCancel(wxCommandEvent& event);
79
80 protected:
81 wxSpinCtrl *m_spinctrl;
82
83 long m_value, m_min, m_max;
84
85 private:
86 DECLARE_EVENT_TABLE()
87 };
88
89 // ============================================================================
90 // implementation
91 // ============================================================================
92
93 // ----------------------------------------------------------------------------
94 // wxNumberEntryDialog
95 // ----------------------------------------------------------------------------
96
97 BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
98 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
99 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
100 END_EVENT_TABLE()
101
102 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
103 const wxString& message,
104 const wxString& prompt,
105 const wxString& caption,
106 long value,
107 long min,
108 long max,
109 const wxPoint& pos)
110 : wxDialog(parent, -1, caption,
111 pos, wxDefaultSize,
112 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL )
113 {
114 m_value = value;
115 m_max = max;
116 m_min = min;
117
118 wxBeginBusyCursor();
119
120 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
121
122 // 1) text message
123 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
124
125 // 2) prompt and text ctrl
126 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
127 // prompt if any
128 if (!prompt.IsEmpty())
129 inputsizer->Add( new wxStaticText( this, -1, prompt ), 0, wxCENTER | wxLEFT, 10 );
130 // spin ctrl
131 wxString valStr;
132 valStr.Printf(wxT("%lu"), m_value);
133 m_spinctrl = new wxSpinCtrl(this, -1, valStr, wxDefaultPosition, wxSize( 140, -1 ) );
134 #if !defined(__WIN16__) && wxUSE_SPINCTRL
135 m_spinctrl->SetRange((int)m_min, (int)m_max);
136 #endif
137 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
138 // add both
139 topsizer->Add( inputsizer, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
140
141 #if wxUSE_STATLINE
142 // 3) static line
143 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
144 #endif
145
146 // 4) buttons
147 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
148
149 SetSizer( topsizer );
150 SetAutoLayout( TRUE );
151
152 topsizer->SetSizeHints( this );
153 topsizer->Fit( this );
154
155 Centre( wxBOTH );
156
157 m_spinctrl->SetFocus();
158
159 wxEndBusyCursor();
160 }
161
162 void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
163 {
164 #if !wxUSE_SPINCTRL
165 wxString tmp = m_spinctrl->GetValue();
166 if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 )
167 m_value = -1;
168 else
169 #else
170 m_value = m_spinctrl->GetValue();
171 #endif
172 if ( m_value < m_min || m_value > m_max )
173 {
174 // not a number or out of range
175 m_value = -1;
176 }
177
178 EndModal(wxID_OK);
179 }
180
181 void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
182 {
183 m_value = -1;
184
185 EndModal(wxID_CANCEL);
186 }
187
188 // ----------------------------------------------------------------------------
189 // global functions
190 // ----------------------------------------------------------------------------
191
192 // wxGetTextFromUser is in utilscmn.cpp
193
194 long wxGetNumberFromUser(const wxString& msg,
195 const wxString& prompt,
196 const wxString& title,
197 long value,
198 long min,
199 long max,
200 wxWindow *parent,
201 const wxPoint& pos)
202 {
203 wxNumberEntryDialog dialog(parent, msg, prompt, title,
204 value, min, max, pos);
205 (void)dialog.ShowModal();
206
207 return dialog.GetValue();
208 }