The rounded corners look really dumb at this size.
[wxWidgets.git] / src / generic / numdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/numdlgg.cpp
3 // Purpose: wxGetNumberFromUser implementation
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 23.07.99
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_NUMBERDLG
27
28 #ifndef WX_PRECOMP
29 #include <stdio.h>
30
31 #include "wx/utils.h"
32 #include "wx/dialog.h"
33 #include "wx/button.h"
34 #include "wx/stattext.h"
35 #include "wx/textctrl.h"
36 #include "wx/intl.h"
37 #include "wx/sizer.h"
38 #endif
39
40 #if wxUSE_STATLINE
41 #include "wx/statline.h"
42 #endif
43
44 #if wxUSE_SPINCTRL
45 #include "wx/spinctrl.h"
46 #endif
47
48 // this is where wxGetNumberFromUser() is declared
49 #include "wx/numdlg.h"
50
51 #if !wxUSE_SPINCTRL
52 // wxTextCtrl will do instead of wxSpinCtrl if we don't have it
53 #define wxSpinCtrl wxTextCtrl
54 #endif
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // wxNumberEntryDialog
62 // ----------------------------------------------------------------------------
63
64 BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
65 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
66 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
67 END_EVENT_TABLE()
68
69 IMPLEMENT_CLASS(wxNumberEntryDialog, wxDialog)
70
71 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
72 const wxString& message,
73 const wxString& prompt,
74 const wxString& caption,
75 long value,
76 long min,
77 long max,
78 const wxPoint& pos)
79 : wxDialog(GetParentForModalDialog(parent, 0),
80 wxID_ANY, caption,
81 pos, wxDefaultSize)
82 {
83 m_value = value;
84 m_max = max;
85 m_min = min;
86
87 wxBeginBusyCursor();
88
89 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
90 #if wxUSE_STATTEXT
91 // 1) text message
92 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
93 #endif
94
95 // 2) prompt and text ctrl
96 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
97
98 #if wxUSE_STATTEXT
99 // prompt if any
100 if (!prompt.empty())
101 inputsizer->Add( new wxStaticText( this, wxID_ANY, prompt ), 0, wxCENTER | wxLEFT, 10 );
102 #endif
103
104 // spin ctrl
105 wxString valStr;
106 valStr.Printf(wxT("%ld"), m_value);
107 m_spinctrl = new wxSpinCtrl(this, wxID_ANY, valStr, wxDefaultPosition, wxSize( 140, wxDefaultCoord ), wxSP_ARROW_KEYS, (int)m_min, (int)m_max, (int)m_value);
108 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
109 // add both
110 topsizer->Add( inputsizer, 0, wxEXPAND | wxLEFT|wxRIGHT, 5 );
111
112 // 3) buttons if any
113 wxSizer *buttonSizer = CreateSeparatedButtonSizer(wxOK | wxCANCEL);
114 if ( buttonSizer )
115 {
116 topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder());
117 }
118
119 SetSizer( topsizer );
120 SetAutoLayout( true );
121
122 topsizer->SetSizeHints( this );
123 topsizer->Fit( this );
124
125 Centre( wxBOTH );
126
127 m_spinctrl->SetSelection(-1, -1);
128 m_spinctrl->SetFocus();
129
130 wxEndBusyCursor();
131 }
132
133 void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
134 {
135 #if !wxUSE_SPINCTRL
136 wxString tmp = m_spinctrl->GetValue();
137 if ( wxSscanf(tmp, wxT("%ld"), &m_value) != 1 )
138 EndModal(wxID_CANCEL);
139 else
140 #else
141 m_value = m_spinctrl->GetValue();
142 #endif
143 if ( m_value < m_min || m_value > m_max )
144 {
145 // not a number or out of range
146 m_value = -1;
147 EndModal(wxID_CANCEL);
148 }
149
150 EndModal(wxID_OK);
151 }
152
153 void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
154 {
155 EndModal(wxID_CANCEL);
156 }
157
158 // ----------------------------------------------------------------------------
159 // global functions
160 // ----------------------------------------------------------------------------
161
162 // wxGetTextFromUser is in utilscmn.cpp
163
164 long wxGetNumberFromUser(const wxString& msg,
165 const wxString& prompt,
166 const wxString& title,
167 long value,
168 long min,
169 long max,
170 wxWindow *parent,
171 const wxPoint& pos)
172 {
173 wxNumberEntryDialog dialog(parent, msg, prompt, title,
174 value, min, max, pos);
175 if (dialog.ShowModal() == wxID_OK)
176 return dialog.GetValue();
177
178 return -1;
179 }
180
181 #endif // wxUSE_NUMBERDLG