]> git.saurik.com Git - wxWidgets.git/blame - src/generic/numdlgg.cpp
Needed some variation; got bored of seeing the compilation errors for this
[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__
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#endif
42
43// this is where wxGetNumberFromUser() is declared
44#include "wx/generic/textdlgg.h"
45
46// ----------------------------------------------------------------------------
47// private classes
48// ----------------------------------------------------------------------------
49
50class WXDLLEXPORT wxNumberEntryDialog : public wxDialog
51{
52public:
53 wxNumberEntryDialog(wxWindow *parent,
54 const wxString& message,
55 const wxString& prompt,
56 const wxString& caption,
57 long value, long min, long max,
58 const wxPoint& pos);
59
60 long GetValue() const { return m_value; }
61
62 // implementation only
63 void OnOK(wxCommandEvent& event);
64 void OnCancel(wxCommandEvent& event);
65
66protected:
67 wxTextCtrl *m_spinctrl; // TODO replace it with wxSpinCtrl once it's done
68
69 long m_value, m_min, m_max;
70
71private:
72 DECLARE_EVENT_TABLE()
73};
74
75// ============================================================================
76// implementation
77// ============================================================================
78
79// ----------------------------------------------------------------------------
80// wxNumberEntryDialog
81// ----------------------------------------------------------------------------
82
83BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
84 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
85 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
86END_EVENT_TABLE()
87
88wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
89 const wxString& message,
90 const wxString& prompt,
91 const wxString& caption,
92 long value,
93 long min,
94 long max,
95 const wxPoint& pos)
96 : wxDialog(parent, -1, caption,
97 pos, wxDefaultSize,
98 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL)
99{
100 // init members
101 // ------------
102
103 m_value = value;
104 m_max = max;
105 m_min = min;
106
107 // calculate the sizes
108 // -------------------
109
110 wxArrayString lines;
111 wxSize sizeText = SplitTextMessage(message, &lines);
112
113 wxSize sizeBtn = GetStandardButtonSize();
114
115 int wPrompt, hPrompt;
116 GetTextExtent(prompt, &wPrompt, &hPrompt);
117
118 long wText = wxMax(2*sizeBtn.GetWidth(),
119 wxMax(wPrompt, sizeText.GetWidth()));
120 long hText = GetStandardTextHeight();
121
122 long wDialog = 5*LAYOUT_X_MARGIN + wText + wPrompt;
123 long hDialog = 2*LAYOUT_Y_MARGIN +
124 sizeText.GetHeight() * lines.GetCount() +
125 2*LAYOUT_Y_MARGIN +
126 hText +
127 2*LAYOUT_Y_MARGIN +
128 sizeBtn.GetHeight() +
129 2*LAYOUT_Y_MARGIN;
130
131 // create the controls
132 // -------------------
133
134 // message
135 long x = 2*LAYOUT_X_MARGIN;
136 long y = CreateTextMessage(lines,
137 wxPoint(x, 2*LAYOUT_Y_MARGIN),
138 sizeText);
139
140 y += 2*LAYOUT_X_MARGIN;
141
142 // prompt
143 (void)new wxStaticText(this, -1, prompt,
144 wxPoint(x, y),
145 wxSize(wPrompt, hPrompt));
146
147 // spin ctrl
148 wxString valStr;
7e256c24 149 valStr.Printf(_T("%lu"), m_value);
31528cd3
VZ
150 m_spinctrl = new wxTextCtrl(this, -1, valStr,
151 wxPoint(x + wPrompt + LAYOUT_X_MARGIN, y),
152 wxSize(wText, hText));
153 y += hText + 2*LAYOUT_X_MARGIN;
154
155 // and buttons
156 CreateStandardButtons(wDialog, y, sizeBtn.GetWidth(), sizeBtn.GetHeight());
157
158 // set the dialog size and position
159 SetClientSize(wDialog, hDialog);
160 if ( pos == wxDefaultPosition )
161 {
162 // centre the dialog if no explicit position given
163 Centre(wxBOTH | wxCENTER_FRAME);
164 }
165
166 m_spinctrl->SetFocus();
167}
168
169void wxNumberEntryDialog::OnOK(wxCommandEvent& event)
170{
7e256c24 171 if ( (wxSscanf(m_spinctrl->GetValue(), _T("%lu"), &m_value) != 1) ||
31528cd3
VZ
172 (m_value < m_min) || (m_value > m_max) )
173 {
174 // not a number or out of range
175 m_value = -1;
176 }
177
178 EndModal(wxID_OK);
179}
180
181void wxNumberEntryDialog::OnCancel(wxCommandEvent& event)
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}