]>
Commit | Line | Data |
---|---|---|
31528cd3 | 1 | ///////////////////////////////////////////////////////////////////////////// |
897b24cf | 2 | // Name: src/generic/numdlgg.cpp |
31528cd3 VZ |
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 | |
65571936 | 9 | // Licence: wxWindows licence |
31528cd3 VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
31528cd3 VZ |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_NUMBERDLG |
28 | ||
31528cd3 VZ |
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" | |
073478b3 RR |
38 | #include "wx/sizer.h" |
39 | #endif | |
40 | ||
41 | #if wxUSE_STATLINE | |
42 | #include "wx/statline.h" | |
31528cd3 VZ |
43 | #endif |
44 | ||
3a5bcc4d | 45 | #if wxUSE_SPINCTRL |
678cd6de | 46 | #include "wx/spinctrl.h" |
f6bcfd97 | 47 | #endif |
678cd6de | 48 | |
31528cd3 | 49 | // this is where wxGetNumberFromUser() is declared |
fc5414a1 | 50 | #include "wx/numdlg.h" |
31528cd3 | 51 | |
a7540f46 VZ |
52 | #if !wxUSE_SPINCTRL |
53 | // wxTextCtrl will do instead of wxSpinCtrl if we don't have it | |
54 | #define wxSpinCtrl wxTextCtrl | |
55 | #endif | |
56 | ||
d2cdad17 WS |
57 | // --------------------------------------------------------------------------- |
58 | // macros | |
59 | // --------------------------------------------------------------------------- | |
60 | ||
61 | /* Macro for avoiding #ifdefs when value have to be different depending on size of | |
9a357011 | 62 | device we display on - take it from something like wxDesktopPolicy in the future |
d2cdad17 WS |
63 | */ |
64 | ||
65 | #if defined(__SMARTPHONE__) | |
66 | #define wxLARGESMALL(large,small) small | |
67 | #else | |
68 | #define wxLARGESMALL(large,small) large | |
69 | #endif | |
70 | ||
31528cd3 VZ |
71 | // ============================================================================ |
72 | // implementation | |
73 | // ============================================================================ | |
74 | ||
75 | // ---------------------------------------------------------------------------- | |
76 | // wxNumberEntryDialog | |
77 | // ---------------------------------------------------------------------------- | |
78 | ||
79 | BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog) | |
80 | EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK) | |
81 | EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel) | |
82 | END_EVENT_TABLE() | |
83 | ||
4b5e5cfb MB |
84 | IMPLEMENT_CLASS(wxNumberEntryDialog, wxDialog) |
85 | ||
31528cd3 VZ |
86 | wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent, |
87 | const wxString& message, | |
88 | const wxString& prompt, | |
89 | const wxString& caption, | |
90 | long value, | |
91 | long min, | |
92 | long max, | |
93 | const wxPoint& pos) | |
ca65c044 | 94 | : wxDialog(parent, wxID_ANY, caption, |
2a21ac15 | 95 | pos, wxDefaultSize) |
31528cd3 | 96 | { |
31528cd3 VZ |
97 | m_value = value; |
98 | m_max = max; | |
99 | m_min = min; | |
100 | ||
073478b3 | 101 | wxBeginBusyCursor(); |
678cd6de | 102 | |
92afa2b1 | 103 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); |
001e76a9 | 104 | #if wxUSE_STATTEXT |
073478b3 | 105 | // 1) text message |
92afa2b1 | 106 | topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 ); |
897b24cf | 107 | #endif |
678cd6de | 108 | |
073478b3 | 109 | // 2) prompt and text ctrl |
92afa2b1 | 110 | wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL ); |
132422c4 RN |
111 | |
112 | #if wxUSE_STATTEXT | |
073478b3 | 113 | // prompt if any |
897b24cf | 114 | if (!prompt.empty()) |
ca65c044 | 115 | inputsizer->Add( new wxStaticText( this, wxID_ANY, prompt ), 0, wxCENTER | wxLEFT, 10 ); |
132422c4 | 116 | #endif |
897b24cf | 117 | |
31528cd3 VZ |
118 | // spin ctrl |
119 | wxString valStr; | |
fc5414a1 | 120 | valStr.Printf(wxT("%ld"), m_value); |
422d0ff0 | 121 | m_spinctrl = new wxSpinCtrl(this, wxID_ANY, valStr, wxDefaultPosition, wxSize( 140, wxDefaultCoord ) ); |
3a5bcc4d | 122 | #if wxUSE_SPINCTRL |
479cd5de | 123 | m_spinctrl->SetRange((int)m_min, (int)m_max); |
5dd26b08 | 124 | #endif |
073478b3 | 125 | inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 ); |
678cd6de | 126 | // add both |
897b24cf | 127 | topsizer->Add( inputsizer, 0, wxEXPAND | wxLEFT|wxRIGHT, 5 ); |
678cd6de | 128 | |
897b24cf WS |
129 | // 3) buttons if any |
130 | wxSizer *buttonSizer = CreateButtonSizer( wxOK|wxCANCEL , true, wxLARGESMALL(10,0) ); | |
131 | if(buttonSizer->GetChildren().GetCount() > 0 ) | |
132 | { | |
133 | topsizer->Add( buttonSizer, 0, wxEXPAND | wxALL, wxLARGESMALL(10,0) ); | |
134 | } | |
135 | else | |
136 | { | |
137 | topsizer->AddSpacer( wxLARGESMALL(15,0) ); | |
138 | delete buttonSizer; | |
139 | } | |
d2cdad17 | 140 | |
073478b3 | 141 | SetSizer( topsizer ); |
ca65c044 | 142 | SetAutoLayout( true ); |
073478b3 | 143 | |
aa66250b | 144 | #if !defined(__SMARTPHONE__) && !defined(__POCKETPC__) |
0ae2df30 RR |
145 | topsizer->SetSizeHints( this ); |
146 | topsizer->Fit( this ); | |
147 | ||
073478b3 | 148 | Centre( wxBOTH ); |
aa66250b | 149 | #endif |
073478b3 | 150 | |
8282369a | 151 | m_spinctrl->SetSelection(-1, -1); |
31528cd3 | 152 | m_spinctrl->SetFocus(); |
073478b3 RR |
153 | |
154 | wxEndBusyCursor(); | |
31528cd3 VZ |
155 | } |
156 | ||
a4c97004 | 157 | void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event)) |
31528cd3 | 158 | { |
a7540f46 VZ |
159 | #if !wxUSE_SPINCTRL |
160 | wxString tmp = m_spinctrl->GetValue(); | |
161 | if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 ) | |
fc5414a1 | 162 | EndModal(wxID_CANCEL); |
a7540f46 VZ |
163 | else |
164 | #else | |
678cd6de | 165 | m_value = m_spinctrl->GetValue(); |
a7540f46 | 166 | #endif |
678cd6de | 167 | if ( m_value < m_min || m_value > m_max ) |
31528cd3 VZ |
168 | { |
169 | // not a number or out of range | |
cdec37ac | 170 | m_value = -1; |
fc5414a1 | 171 | EndModal(wxID_CANCEL); |
31528cd3 VZ |
172 | } |
173 | ||
174 | EndModal(wxID_OK); | |
175 | } | |
176 | ||
a4c97004 | 177 | void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) |
31528cd3 | 178 | { |
31528cd3 VZ |
179 | EndModal(wxID_CANCEL); |
180 | } | |
181 | ||
182 | // ---------------------------------------------------------------------------- | |
183 | // global functions | |
184 | // ---------------------------------------------------------------------------- | |
185 | ||
186 | // wxGetTextFromUser is in utilscmn.cpp | |
187 | ||
188 | long wxGetNumberFromUser(const wxString& msg, | |
189 | const wxString& prompt, | |
190 | const wxString& title, | |
191 | long value, | |
192 | long min, | |
193 | long max, | |
194 | wxWindow *parent, | |
195 | const wxPoint& pos) | |
196 | { | |
197 | wxNumberEntryDialog dialog(parent, msg, prompt, title, | |
198 | value, min, max, pos); | |
fc5414a1 | 199 | if (dialog.ShowModal() == wxID_OK) |
cdec37ac | 200 | return dialog.GetValue(); |
ca65c044 | 201 | |
fc5414a1 | 202 | return -1; |
31528cd3 | 203 | } |
1e6feb95 VZ |
204 | |
205 | #endif // wxUSE_NUMBERDLG |