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