]>
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 | |
31528cd3 | 7 | // Copyright: (c) Vadim Zeitlin |
65571936 | 8 | // Licence: wxWindows licence |
31528cd3 VZ |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
31528cd3 VZ |
19 | // For compilers that support precompilation, includes "wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
1e6feb95 VZ |
26 | #if wxUSE_NUMBERDLG |
27 | ||
31528cd3 VZ |
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" | |
073478b3 RR |
37 | #include "wx/sizer.h" |
38 | #endif | |
39 | ||
40 | #if wxUSE_STATLINE | |
41 | #include "wx/statline.h" | |
31528cd3 VZ |
42 | #endif |
43 | ||
3a5bcc4d | 44 | #if wxUSE_SPINCTRL |
678cd6de | 45 | #include "wx/spinctrl.h" |
f6bcfd97 | 46 | #endif |
678cd6de | 47 | |
31528cd3 | 48 | // this is where wxGetNumberFromUser() is declared |
fc5414a1 | 49 | #include "wx/numdlg.h" |
31528cd3 | 50 | |
a7540f46 VZ |
51 | #if !wxUSE_SPINCTRL |
52 | // wxTextCtrl will do instead of wxSpinCtrl if we don't have it | |
53 | #define wxSpinCtrl wxTextCtrl | |
54 | #endif | |
55 | ||
31528cd3 VZ |
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 | ||
4b5e5cfb MB |
69 | IMPLEMENT_CLASS(wxNumberEntryDialog, wxDialog) |
70 | ||
31528cd3 VZ |
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) | |
cdc48273 | 79 | : wxDialog(GetParentForModalDialog(parent, 0), |
2229243b | 80 | wxID_ANY, caption, |
2a21ac15 | 81 | pos, wxDefaultSize) |
31528cd3 | 82 | { |
31528cd3 VZ |
83 | m_value = value; |
84 | m_max = max; | |
85 | m_min = min; | |
86 | ||
073478b3 | 87 | wxBeginBusyCursor(); |
678cd6de | 88 | |
92afa2b1 | 89 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); |
001e76a9 | 90 | #if wxUSE_STATTEXT |
073478b3 | 91 | // 1) text message |
92afa2b1 | 92 | topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 ); |
897b24cf | 93 | #endif |
678cd6de | 94 | |
073478b3 | 95 | // 2) prompt and text ctrl |
92afa2b1 | 96 | wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL ); |
132422c4 RN |
97 | |
98 | #if wxUSE_STATTEXT | |
073478b3 | 99 | // prompt if any |
897b24cf | 100 | if (!prompt.empty()) |
ca65c044 | 101 | inputsizer->Add( new wxStaticText( this, wxID_ANY, prompt ), 0, wxCENTER | wxLEFT, 10 ); |
132422c4 | 102 | #endif |
897b24cf | 103 | |
31528cd3 VZ |
104 | // spin ctrl |
105 | wxString valStr; | |
fc5414a1 | 106 | valStr.Printf(wxT("%ld"), m_value); |
eac3a228 | 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); |
073478b3 | 108 | inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 ); |
678cd6de | 109 | // add both |
897b24cf | 110 | topsizer->Add( inputsizer, 0, wxEXPAND | wxLEFT|wxRIGHT, 5 ); |
678cd6de | 111 | |
897b24cf | 112 | // 3) buttons if any |
bd9f3519 VZ |
113 | wxSizer *buttonSizer = CreateSeparatedButtonSizer(wxOK | wxCANCEL); |
114 | if ( buttonSizer ) | |
897b24cf | 115 | { |
bd9f3519 | 116 | topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder()); |
897b24cf | 117 | } |
d2cdad17 | 118 | |
073478b3 | 119 | SetSizer( topsizer ); |
ca65c044 | 120 | SetAutoLayout( true ); |
073478b3 | 121 | |
0ae2df30 RR |
122 | topsizer->SetSizeHints( this ); |
123 | topsizer->Fit( this ); | |
124 | ||
073478b3 RR |
125 | Centre( wxBOTH ); |
126 | ||
8282369a | 127 | m_spinctrl->SetSelection(-1, -1); |
31528cd3 | 128 | m_spinctrl->SetFocus(); |
073478b3 RR |
129 | |
130 | wxEndBusyCursor(); | |
31528cd3 VZ |
131 | } |
132 | ||
a4c97004 | 133 | void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event)) |
31528cd3 | 134 | { |
a7540f46 VZ |
135 | #if !wxUSE_SPINCTRL |
136 | wxString tmp = m_spinctrl->GetValue(); | |
9a83f860 | 137 | if ( wxSscanf(tmp, wxT("%ld"), &m_value) != 1 ) |
fc5414a1 | 138 | EndModal(wxID_CANCEL); |
a7540f46 VZ |
139 | else |
140 | #else | |
678cd6de | 141 | m_value = m_spinctrl->GetValue(); |
a7540f46 | 142 | #endif |
678cd6de | 143 | if ( m_value < m_min || m_value > m_max ) |
31528cd3 VZ |
144 | { |
145 | // not a number or out of range | |
cdec37ac | 146 | m_value = -1; |
fc5414a1 | 147 | EndModal(wxID_CANCEL); |
31528cd3 VZ |
148 | } |
149 | ||
150 | EndModal(wxID_OK); | |
151 | } | |
152 | ||
a4c97004 | 153 | void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) |
31528cd3 | 154 | { |
31528cd3 VZ |
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); | |
fc5414a1 | 175 | if (dialog.ShowModal() == wxID_OK) |
cdec37ac | 176 | return dialog.GetValue(); |
ca65c044 | 177 | |
fc5414a1 | 178 | return -1; |
31528cd3 | 179 | } |
1e6feb95 VZ |
180 | |
181 | #endif // wxUSE_NUMBERDLG |