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