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