removed Win16 code (patch 905241), last version with remains of Win16 support tagged...
[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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 wxUSE_SPINCTRL
50 #include "wx/spinctrl.h"
51 #endif
52
53 // this is where wxGetNumberFromUser() is declared
54 #include "wx/numdlg.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 // implementation
63 // ============================================================================
64
65 // ----------------------------------------------------------------------------
66 // wxNumberEntryDialog
67 // ----------------------------------------------------------------------------
68
69 BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
70 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
71 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
72 END_EVENT_TABLE()
73
74 IMPLEMENT_CLASS(wxNumberEntryDialog, wxDialog)
75
76 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
77 const wxString& message,
78 const wxString& prompt,
79 const wxString& caption,
80 long value,
81 long min,
82 long max,
83 const wxPoint& pos)
84 : wxDialog(parent, -1, caption,
85 pos, wxDefaultSize,
86 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL )
87 {
88 m_value = value;
89 m_max = max;
90 m_min = min;
91
92 wxBeginBusyCursor();
93
94 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
95
96 // 1) text message
97 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
98
99 // 2) prompt and text ctrl
100 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
101 // prompt if any
102 if (!prompt.IsEmpty())
103 inputsizer->Add( new wxStaticText( this, -1, prompt ), 0, wxCENTER | wxLEFT, 10 );
104 // spin ctrl
105 wxString valStr;
106 valStr.Printf(wxT("%ld"), m_value);
107 m_spinctrl = new wxSpinCtrl(this, -1, valStr, wxDefaultPosition, wxSize( 140, -1 ) );
108 #if wxUSE_SPINCTRL
109 m_spinctrl->SetRange((int)m_min, (int)m_max);
110 #endif
111 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
112 // add both
113 topsizer->Add( inputsizer, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
114
115 #if wxUSE_STATLINE
116 // 3) static line
117 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
118 #endif
119
120 // 4) buttons
121 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
122
123 SetSizer( topsizer );
124 SetAutoLayout( TRUE );
125
126 topsizer->SetSizeHints( this );
127 topsizer->Fit( this );
128
129 Centre( wxBOTH );
130
131 m_spinctrl->SetSelection(-1, -1);
132 m_spinctrl->SetFocus();
133
134 wxEndBusyCursor();
135 }
136
137 void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
138 {
139 #if !wxUSE_SPINCTRL
140 wxString tmp = m_spinctrl->GetValue();
141 if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 )
142 EndModal(wxID_CANCEL);
143 else
144 #else
145 m_value = m_spinctrl->GetValue();
146 #endif
147 if ( m_value < m_min || m_value > m_max )
148 {
149 // not a number or out of range
150 EndModal(wxID_CANCEL);
151 }
152
153 EndModal(wxID_OK);
154 }
155
156 void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
157 {
158 EndModal(wxID_CANCEL);
159 }
160
161 // ----------------------------------------------------------------------------
162 // global functions
163 // ----------------------------------------------------------------------------
164
165 // wxGetTextFromUser is in utilscmn.cpp
166
167 long wxGetNumberFromUser(const wxString& msg,
168 const wxString& prompt,
169 const wxString& title,
170 long value,
171 long min,
172 long max,
173 wxWindow *parent,
174 const wxPoint& pos)
175 {
176 wxNumberEntryDialog dialog(parent, msg, prompt, title,
177 value, min, max, pos);
178 if (dialog.ShowModal() == wxID_OK)
179 return dialog.GetValue();
180
181 return -1;
182 }
183
184 #endif // wxUSE_NUMBERDLG