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