]>
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); |
422d0ff0 | 108 | m_spinctrl = new wxSpinCtrl(this, wxID_ANY, valStr, wxDefaultPosition, wxSize( 140, wxDefaultCoord ) ); |
3a5bcc4d | 109 | #if wxUSE_SPINCTRL |
479cd5de | 110 | m_spinctrl->SetRange((int)m_min, (int)m_max); |
5dd26b08 | 111 | #endif |
073478b3 | 112 | inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 ); |
678cd6de | 113 | // add both |
897b24cf | 114 | topsizer->Add( inputsizer, 0, wxEXPAND | wxLEFT|wxRIGHT, 5 ); |
678cd6de | 115 | |
897b24cf | 116 | // 3) buttons if any |
bd9f3519 VZ |
117 | wxSizer *buttonSizer = CreateSeparatedButtonSizer(wxOK | wxCANCEL); |
118 | if ( buttonSizer ) | |
897b24cf | 119 | { |
bd9f3519 | 120 | topsizer->Add(buttonSizer, wxSizerFlags().Expand().DoubleBorder()); |
897b24cf | 121 | } |
d2cdad17 | 122 | |
073478b3 | 123 | SetSizer( topsizer ); |
ca65c044 | 124 | SetAutoLayout( true ); |
073478b3 | 125 | |
0ae2df30 RR |
126 | topsizer->SetSizeHints( this ); |
127 | topsizer->Fit( this ); | |
128 | ||
073478b3 RR |
129 | Centre( wxBOTH ); |
130 | ||
8282369a | 131 | m_spinctrl->SetSelection(-1, -1); |
31528cd3 | 132 | m_spinctrl->SetFocus(); |
073478b3 RR |
133 | |
134 | wxEndBusyCursor(); | |
31528cd3 VZ |
135 | } |
136 | ||
a4c97004 | 137 | void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event)) |
31528cd3 | 138 | { |
a7540f46 VZ |
139 | #if !wxUSE_SPINCTRL |
140 | wxString tmp = m_spinctrl->GetValue(); | |
141 | if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 ) | |
fc5414a1 | 142 | EndModal(wxID_CANCEL); |
a7540f46 VZ |
143 | else |
144 | #else | |
678cd6de | 145 | m_value = m_spinctrl->GetValue(); |
a7540f46 | 146 | #endif |
678cd6de | 147 | if ( m_value < m_min || m_value > m_max ) |
31528cd3 VZ |
148 | { |
149 | // not a number or out of range | |
cdec37ac | 150 | m_value = -1; |
fc5414a1 | 151 | EndModal(wxID_CANCEL); |
31528cd3 VZ |
152 | } |
153 | ||
154 | EndModal(wxID_OK); | |
155 | } | |
156 | ||
a4c97004 | 157 | void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event)) |
31528cd3 | 158 | { |
31528cd3 VZ |
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); | |
fc5414a1 | 179 | if (dialog.ShowModal() == wxID_OK) |
cdec37ac | 180 | return dialog.GetValue(); |
ca65c044 | 181 | |
fc5414a1 | 182 | return -1; |
31528cd3 | 183 | } |
1e6feb95 VZ |
184 | |
185 | #endif // wxUSE_NUMBERDLG |