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