]> git.saurik.com Git - wxWidgets.git/blob - src/generic/numdlgg.cpp
reusing existing API
[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(GetParentForModalDialog(parent, 0),
81 wxID_ANY, caption,
82 pos, wxDefaultSize)
83 {
84 m_value = value;
85 m_max = max;
86 m_min = min;
87
88 wxBeginBusyCursor();
89
90 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
91 #if wxUSE_STATTEXT
92 // 1) text message
93 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
94 #endif
95
96 // 2) prompt and text ctrl
97 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
98
99 #if wxUSE_STATTEXT
100 // prompt if any
101 if (!prompt.empty())
102 inputsizer->Add( new wxStaticText( this, wxID_ANY, prompt ), 0, wxCENTER | wxLEFT, 10 );
103 #endif
104
105 // spin ctrl
106 wxString valStr;
107 valStr.Printf(wxT("%ld"), m_value);
108 m_spinctrl = new wxSpinCtrl(this, wxID_ANY, valStr, wxDefaultPosition, wxSize( 140, wxDefaultCoord ), wxSP_ARROW_KEYS, (int)m_min, (int)m_max, (int)m_value);
109 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
110 // add both
111 topsizer->Add( inputsizer, 0, wxEXPAND | wxLEFT|wxRIGHT, 5 );
112
113 // 3) buttons if any
114 wxSizer *buttonSizer = CreateSeparatedButtonSizer(wxOK | wxCANCEL);
115 if ( buttonSizer )
116 {
117 topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder());
118 }
119
120 SetSizer( topsizer );
121 SetAutoLayout( true );
122
123 topsizer->SetSizeHints( this );
124 topsizer->Fit( this );
125
126 Centre( wxBOTH );
127
128 m_spinctrl->SetSelection(-1, -1);
129 m_spinctrl->SetFocus();
130
131 wxEndBusyCursor();
132 }
133
134 void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
135 {
136 #if !wxUSE_SPINCTRL
137 wxString tmp = m_spinctrl->GetValue();
138 if ( wxSscanf(tmp, wxT("%ld"), &m_value) != 1 )
139 EndModal(wxID_CANCEL);
140 else
141 #else
142 m_value = m_spinctrl->GetValue();
143 #endif
144 if ( m_value < m_min || m_value > m_max )
145 {
146 // not a number or out of range
147 m_value = -1;
148 EndModal(wxID_CANCEL);
149 }
150
151 EndModal(wxID_OK);
152 }
153
154 void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
155 {
156 EndModal(wxID_CANCEL);
157 }
158
159 // ----------------------------------------------------------------------------
160 // global functions
161 // ----------------------------------------------------------------------------
162
163 // wxGetTextFromUser is in utilscmn.cpp
164
165 long wxGetNumberFromUser(const wxString& msg,
166 const wxString& prompt,
167 const wxString& title,
168 long value,
169 long min,
170 long max,
171 wxWindow *parent,
172 const wxPoint& pos)
173 {
174 wxNumberEntryDialog dialog(parent, msg, prompt, title,
175 value, min, max, pos);
176 if (dialog.ShowModal() == wxID_OK)
177 return dialog.GetValue();
178
179 return -1;
180 }
181
182 #endif // wxUSE_NUMBERDLG