]> git.saurik.com Git - wxWidgets.git/blob - src/generic/numdlgg.cpp
Adding support for item font/style/color customization, unfortunately, it does not...
[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 // 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 // implementation
59 // ============================================================================
60
61 // ----------------------------------------------------------------------------
62 // wxNumberEntryDialog
63 // ----------------------------------------------------------------------------
64
65 BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
66 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
67 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
68 END_EVENT_TABLE()
69
70 IMPLEMENT_CLASS(wxNumberEntryDialog, wxDialog)
71
72 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
73 const wxString& message,
74 const wxString& prompt,
75 const wxString& caption,
76 long value,
77 long min,
78 long max,
79 const wxPoint& pos)
80 : wxDialog(parent, 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 ) );
108 #if wxUSE_SPINCTRL
109 m_spinctrl->SetRange((int)m_min, (int)m_max);
110 #endif
111 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
112 // add both
113 topsizer->Add( inputsizer, 0, wxEXPAND | wxLEFT|wxRIGHT, 5 );
114
115 // 3) buttons if any
116 wxSizer *buttonSizer = CreateSeparatedButtonSizer(wxOK | wxCANCEL);
117 if ( buttonSizer )
118 {
119 topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder());
120 }
121
122 SetSizer( topsizer );
123 SetAutoLayout( true );
124
125 topsizer->SetSizeHints( this );
126 topsizer->Fit( this );
127
128 Centre( wxBOTH );
129
130 m_spinctrl->SetSelection(-1, -1);
131 m_spinctrl->SetFocus();
132
133 wxEndBusyCursor();
134 }
135
136 void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
137 {
138 #if !wxUSE_SPINCTRL
139 wxString tmp = m_spinctrl->GetValue();
140 if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 )
141 EndModal(wxID_CANCEL);
142 else
143 #else
144 m_value = m_spinctrl->GetValue();
145 #endif
146 if ( m_value < m_min || m_value > m_max )
147 {
148 // not a number or out of range
149 m_value = -1;
150 EndModal(wxID_CANCEL);
151 }
152
153 EndModal(wxID_OK);
154 }
155
156 void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
157 {
158 EndModal(wxID_CANCEL);
159 }
160
161 // ----------------------------------------------------------------------------
162 // global functions
163 // ----------------------------------------------------------------------------
164
165 // wxGetTextFromUser is in utilscmn.cpp
166
167 long wxGetNumberFromUser(const wxString& msg,
168 const wxString& prompt,
169 const wxString& title,
170 long value,
171 long min,
172 long max,
173 wxWindow *parent,
174 const wxPoint& pos)
175 {
176 wxNumberEntryDialog dialog(parent, msg, prompt, title,
177 value, min, max, pos);
178 if (dialog.ShowModal() == wxID_OK)
179 return dialog.GetValue();
180
181 return -1;
182 }
183
184 #endif // wxUSE_NUMBERDLG