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