]>
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 | ||
31528cd3 VZ |
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 | ||
4b5e5cfb MB |
70 | IMPLEMENT_CLASS(wxNumberEntryDialog, wxDialog) |
71 | ||
31528cd3 VZ |
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) | |
2229243b VZ |
80 | : wxDialog(GetParentForModalDialog(parent), |
81 | wxID_ANY, caption, | |
2a21ac15 | 82 | pos, wxDefaultSize) |
31528cd3 | 83 | { |
31528cd3 VZ |
84 | m_value = value; |
85 | m_max = max; | |
86 | m_min = min; | |
87 | ||
073478b3 | 88 | wxBeginBusyCursor(); |
678cd6de | 89 | |
92afa2b1 | 90 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); |
001e76a9 | 91 | #if wxUSE_STATTEXT |
073478b3 | 92 | // 1) text message |
92afa2b1 | 93 | topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 ); |
897b24cf | 94 | #endif |
678cd6de | 95 | |
073478b3 | 96 | // 2) prompt and text ctrl |
92afa2b1 | 97 | wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL ); |
132422c4 RN |
98 | |
99 | #if wxUSE_STATTEXT | |
073478b3 | 100 | // prompt if any |
897b24cf | 101 | if (!prompt.empty()) |
ca65c044 | 102 | inputsizer->Add( new wxStaticText( this, wxID_ANY, prompt ), 0, wxCENTER | wxLEFT, 10 ); |
132422c4 | 103 | #endif |
897b24cf | 104 | |
31528cd3 VZ |
105 | // spin ctrl |
106 | wxString valStr; | |
fc5414a1 | 107 | valStr.Printf(wxT("%ld"), m_value); |
eac3a228 | 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); |
073478b3 | 109 | inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 ); |
678cd6de | 110 | // add both |
897b24cf | 111 | topsizer->Add( inputsizer, 0, wxEXPAND | wxLEFT|wxRIGHT, 5 ); |
678cd6de | 112 | |
897b24cf | 113 | // 3) buttons if any |
bd9f3519 VZ |
114 | wxSizer *buttonSizer = CreateSeparatedButtonSizer(wxOK | wxCANCEL); |
115 | if ( buttonSizer ) | |
897b24cf | 116 | { |
bd9f3519 | 117 | topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder()); |
897b24cf | 118 | } |
d2cdad17 | 119 | |
073478b3 | 120 | SetSizer( topsizer ); |
ca65c044 | 121 | SetAutoLayout( true ); |
073478b3 | 122 | |
0ae2df30 RR |
123 | topsizer->SetSizeHints( this ); |
124 | topsizer->Fit( this ); | |
125 | ||
073478b3 RR |
126 | Centre( wxBOTH ); |
127 | ||
8282369a | 128 | m_spinctrl->SetSelection(-1, -1); |
31528cd3 | 129 | m_spinctrl->SetFocus(); |
073478b3 RR |
130 | |
131 | wxEndBusyCursor(); | |
31528cd3 VZ |
132 | } |
133 | ||
a4c97004 | 134 | void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event)) |
31528cd3 | 135 | { |
a7540f46 VZ |
136 | #if !wxUSE_SPINCTRL |
137 | wxString tmp = m_spinctrl->GetValue(); | |
138 | if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 ) | |
fc5414a1 | 139 | EndModal(wxID_CANCEL); |
a7540f46 VZ |
140 | else |
141 | #else | |
678cd6de | 142 | m_value = m_spinctrl->GetValue(); |
a7540f46 | 143 | #endif |
678cd6de | 144 | if ( m_value < m_min || m_value > m_max ) |
31528cd3 VZ |
145 | { |
146 | // not a number or out of range | |
cdec37ac | 147 | m_value = -1; |
fc5414a1 | 148 | EndModal(wxID_CANCEL); |
31528cd3 VZ |
149 | } |
150 | ||
151 | EndModal(wxID_OK); | |
152 | } | |
153 | ||
a4c97004 | 154 | void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) |
31528cd3 | 155 | { |
31528cd3 VZ |
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); | |
fc5414a1 | 176 | if (dialog.ShowModal() == wxID_OK) |
cdec37ac | 177 | return dialog.GetValue(); |
ca65c044 | 178 | |
fc5414a1 | 179 | return -1; |
31528cd3 | 180 | } |
1e6feb95 VZ |
181 | |
182 | #endif // wxUSE_NUMBERDLG |