]> git.saurik.com Git - wxWidgets.git/blame - src/generic/numdlgg.cpp
Patch from Neil H.
[wxWidgets.git] / src / generic / numdlgg.cpp
CommitLineData
31528cd3
VZ
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__
ff83a770 21 #pragma implementation "numdlgg.cpp"
31528cd3
VZ
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
1e6feb95
VZ
31#if wxUSE_NUMBERDLG
32
31528cd3
VZ
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"
073478b3
RR
42 #include "wx/sizer.h"
43#endif
44
45#if wxUSE_STATLINE
46 #include "wx/statline.h"
31528cd3
VZ
47#endif
48
f6bcfd97 49#if !defined(__WIN16__) && wxUSE_SPINCTRL
678cd6de 50#include "wx/spinctrl.h"
f6bcfd97 51#endif
678cd6de 52
31528cd3 53// this is where wxGetNumberFromUser() is declared
470d5a67 54#include "wx/textdlg.h"
31528cd3 55
a7540f46
VZ
56#if !wxUSE_SPINCTRL
57 // wxTextCtrl will do instead of wxSpinCtrl if we don't have it
58 #define wxSpinCtrl wxTextCtrl
59#endif
60
31528cd3
VZ
61// ----------------------------------------------------------------------------
62// private classes
63// ----------------------------------------------------------------------------
64
65class WXDLLEXPORT wxNumberEntryDialog : public wxDialog
66{
67public:
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
81protected:
678cd6de 82 wxSpinCtrl *m_spinctrl;
31528cd3
VZ
83
84 long m_value, m_min, m_max;
85
86private:
87 DECLARE_EVENT_TABLE()
88};
89
90// ============================================================================
91// implementation
92// ============================================================================
93
94// ----------------------------------------------------------------------------
95// wxNumberEntryDialog
96// ----------------------------------------------------------------------------
97
98BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
99 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
100 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
101END_EVENT_TABLE()
102
103wxNumberEntryDialog::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,
8e877c19 113 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL )
31528cd3 114{
31528cd3
VZ
115 m_value = value;
116 m_max = max;
117 m_min = min;
118
073478b3 119 wxBeginBusyCursor();
678cd6de 120
92afa2b1 121 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
31528cd3 122
073478b3 123 // 1) text message
92afa2b1 124 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
678cd6de 125
073478b3 126 // 2) prompt and text ctrl
92afa2b1 127 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
073478b3
RR
128 // prompt if any
129 if (!prompt.IsEmpty())
130 inputsizer->Add( new wxStaticText( this, -1, prompt ), 0, wxCENTER | wxLEFT, 10 );
31528cd3
VZ
131 // spin ctrl
132 wxString valStr;
223d09f6 133 valStr.Printf(wxT("%lu"), m_value);
678cd6de 134 m_spinctrl = new wxSpinCtrl(this, -1, valStr, wxDefaultPosition, wxSize( 140, -1 ) );
f6bcfd97 135#if !defined(__WIN16__) && wxUSE_SPINCTRL
479cd5de 136 m_spinctrl->SetRange((int)m_min, (int)m_max);
5dd26b08 137#endif
073478b3 138 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
678cd6de 139 // add both
073478b3
RR
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
073478b3 147 // 4) buttons
92afa2b1 148 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
678cd6de 149
073478b3
RR
150 SetSizer( topsizer );
151 SetAutoLayout( TRUE );
152
0ae2df30
RR
153 topsizer->SetSizeHints( this );
154 topsizer->Fit( this );
155
073478b3
RR
156 Centre( wxBOTH );
157
31528cd3 158 m_spinctrl->SetFocus();
073478b3
RR
159
160 wxEndBusyCursor();
31528cd3
VZ
161}
162
a4c97004 163void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
31528cd3 164{
a7540f46
VZ
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
678cd6de 171 m_value = m_spinctrl->GetValue();
a7540f46 172#endif
678cd6de 173 if ( m_value < m_min || m_value > m_max )
31528cd3
VZ
174 {
175 // not a number or out of range
176 m_value = -1;
177 }
178
179 EndModal(wxID_OK);
180}
181
a4c97004 182void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
31528cd3
VZ
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
195long 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}
1e6feb95
VZ
210
211#endif // wxUSE_NUMBERDLG