]> git.saurik.com Git - wxWidgets.git/blob - src/generic/numdlgg.cpp
d91c69e2a5deaed252f9f5666c359ea43f308578
[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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_NUMBERDLG
28
29 #ifndef WX_PRECOMP
30 #include <stdio.h>
31
32 #include "wx/utils.h"
33 #include "wx/dialog.h"
34 #include "wx/button.h"
35 #include "wx/stattext.h"
36 #include "wx/textctrl.h"
37 #include "wx/intl.h"
38 #include "wx/sizer.h"
39 #endif
40
41 #if wxUSE_STATLINE
42 #include "wx/statline.h"
43 #endif
44
45 #if wxUSE_SPINCTRL
46 #include "wx/spinctrl.h"
47 #endif
48
49 // this is where wxGetNumberFromUser() is declared
50 #include "wx/numdlg.h"
51
52 #if !wxUSE_SPINCTRL
53 // wxTextCtrl will do instead of wxSpinCtrl if we don't have it
54 #define wxSpinCtrl wxTextCtrl
55 #endif
56
57 // ---------------------------------------------------------------------------
58 // macros
59 // ---------------------------------------------------------------------------
60
61 /* Macro for avoiding #ifdefs when value have to be different depending on size of
62 device we display on - take it from something like wxDesktopPolicy in the future
63 */
64
65 #if defined(__SMARTPHONE__)
66 #define wxLARGESMALL(large,small) small
67 #else
68 #define wxLARGESMALL(large,small) large
69 #endif
70
71 // ============================================================================
72 // implementation
73 // ============================================================================
74
75 // ----------------------------------------------------------------------------
76 // wxNumberEntryDialog
77 // ----------------------------------------------------------------------------
78
79 BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
80 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
81 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
82 END_EVENT_TABLE()
83
84 IMPLEMENT_CLASS(wxNumberEntryDialog, wxDialog)
85
86 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
87 const wxString& message,
88 const wxString& prompt,
89 const wxString& caption,
90 long value,
91 long min,
92 long max,
93 const wxPoint& pos)
94 : wxDialog(parent, wxID_ANY, caption,
95 pos, wxDefaultSize)
96 {
97 m_value = value;
98 m_max = max;
99 m_min = min;
100
101 wxBeginBusyCursor();
102
103 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
104 #if wxUSE_STATTEXT
105 // 1) text message
106 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
107 #endif
108
109 // 2) prompt and text ctrl
110 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
111
112 #if wxUSE_STATTEXT
113 // prompt if any
114 if (!prompt.IsEmpty())
115 inputsizer->Add( new wxStaticText( this, wxID_ANY, prompt ), 0, wxCENTER | wxLEFT, 10 );
116 #endif
117
118 // spin ctrl
119 wxString valStr;
120 valStr.Printf(wxT("%ld"), m_value);
121 m_spinctrl = new wxSpinCtrl(this, wxID_ANY, valStr, wxDefaultPosition, wxSize( 140, wxDefaultCoord ) );
122 #if wxUSE_SPINCTRL
123 m_spinctrl->SetRange((int)m_min, (int)m_max);
124 #endif
125 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
126 // add both
127 topsizer->Add( inputsizer, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
128
129 // smart phones does not support or do not waste space for wxButtons
130 #ifdef __SMARTPHONE__
131
132 SetRightMenu(wxID_CANCEL, _("Cancel"));
133
134 #else // __SMARTPHONE__/!__SMARTPHONE__
135
136 #if wxUSE_STATLINE
137 // 3) static line
138 topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
139 #endif
140
141 // 4) buttons
142 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxEXPAND | wxALL, 10 );
143
144 #endif // !__SMARTPHONE__
145
146 SetSizer( topsizer );
147 SetAutoLayout( true );
148
149 #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__)
150 topsizer->SetSizeHints( this );
151 topsizer->Fit( this );
152
153 Centre( wxBOTH );
154 #endif
155
156 m_spinctrl->SetSelection(-1, -1);
157 m_spinctrl->SetFocus();
158
159 wxEndBusyCursor();
160 }
161
162 void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
163 {
164 #if !wxUSE_SPINCTRL
165 wxString tmp = m_spinctrl->GetValue();
166 if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 )
167 EndModal(wxID_CANCEL);
168 else
169 #else
170 m_value = m_spinctrl->GetValue();
171 #endif
172 if ( m_value < m_min || m_value > m_max )
173 {
174 // not a number or out of range
175 m_value = -1;
176 EndModal(wxID_CANCEL);
177 }
178
179 EndModal(wxID_OK);
180 }
181
182 void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
183 {
184 EndModal(wxID_CANCEL);
185 }
186
187 // ----------------------------------------------------------------------------
188 // global functions
189 // ----------------------------------------------------------------------------
190
191 // wxGetTextFromUser is in utilscmn.cpp
192
193 long 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 if (dialog.ShowModal() == wxID_OK)
205 return dialog.GetValue();
206
207 return -1;
208 }
209
210 #endif // wxUSE_NUMBERDLG