1. corrected (but the fix is ugly) the multiple def button problem
[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 interface "numdlgg.h"
22 #pragma implementation "numdlgg.h"
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"
41 #include "wx/sizer.h"
42 #endif
43
44 #if wxUSE_STATLINE
45 #include "wx/statline.h"
46 #endif
47
48 #include "wx/spinctrl.h"
49
50 // this is where wxGetNumberFromUser() is declared
51 #include "wx/generic/textdlgg.h"
52
53 // ----------------------------------------------------------------------------
54 // private classes
55 // ----------------------------------------------------------------------------
56
57 class WXDLLEXPORT wxNumberEntryDialog : public wxDialog
58 {
59 public:
60 wxNumberEntryDialog(wxWindow *parent,
61 const wxString& message,
62 const wxString& prompt,
63 const wxString& caption,
64 long value, long min, long max,
65 const wxPoint& pos);
66
67 long GetValue() const { return m_value; }
68
69 // implementation only
70 void OnOK(wxCommandEvent& event);
71 void OnCancel(wxCommandEvent& event);
72
73 protected:
74 wxSpinCtrl *m_spinctrl;
75
76 long m_value, m_min, m_max;
77
78 private:
79 DECLARE_EVENT_TABLE()
80 };
81
82 // ============================================================================
83 // implementation
84 // ============================================================================
85
86 // ----------------------------------------------------------------------------
87 // wxNumberEntryDialog
88 // ----------------------------------------------------------------------------
89
90 BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
91 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
92 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
93 END_EVENT_TABLE()
94
95 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
96 const wxString& message,
97 const wxString& prompt,
98 const wxString& caption,
99 long value,
100 long min,
101 long max,
102 const wxPoint& pos)
103 : wxDialog(parent, -1, caption,
104 pos, wxDefaultSize,
105 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL )
106 {
107 m_value = value;
108 m_max = max;
109 m_min = min;
110
111 wxBeginBusyCursor();
112
113 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
114
115 // 1) text message
116 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
117
118 // 2) prompt and text ctrl
119 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
120 // prompt if any
121 if (!prompt.IsEmpty())
122 inputsizer->Add( new wxStaticText( this, -1, prompt ), 0, wxCENTER | wxLEFT, 10 );
123 // spin ctrl
124 wxString valStr;
125 valStr.Printf(wxT("%lu"), m_value);
126 m_spinctrl = new wxSpinCtrl(this, -1, valStr, wxDefaultPosition, wxSize( 140, -1 ) );
127 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
128 // add both
129 topsizer->Add( inputsizer, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
130
131 #if wxUSE_STATLINE
132 // 3) static line
133 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
134 #endif
135
136 // 4) buttons
137 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
138
139 SetSizer( topsizer );
140 SetAutoLayout( TRUE );
141
142 topsizer->SetSizeHints( this );
143 topsizer->Fit( this );
144
145 Centre( wxBOTH );
146
147 m_spinctrl->SetFocus();
148
149 wxEndBusyCursor();
150 }
151
152 void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
153 {
154 m_value = m_spinctrl->GetValue();
155 if ( m_value < m_min || m_value > m_max )
156 {
157 // not a number or out of range
158 m_value = -1;
159 }
160
161 EndModal(wxID_OK);
162 }
163
164 void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
165 {
166 m_value = -1;
167
168 EndModal(wxID_CANCEL);
169 }
170
171 // ----------------------------------------------------------------------------
172 // global functions
173 // ----------------------------------------------------------------------------
174
175 // wxGetTextFromUser is in utilscmn.cpp
176
177 long wxGetNumberFromUser(const wxString& msg,
178 const wxString& prompt,
179 const wxString& title,
180 long value,
181 long min,
182 long max,
183 wxWindow *parent,
184 const wxPoint& pos)
185 {
186 wxNumberEntryDialog dialog(parent, msg, prompt, title,
187 value, min, max, pos);
188 (void)dialog.ShowModal();
189
190 return dialog.GetValue();
191 }