]> git.saurik.com Git - wxWidgets.git/blame - src/generic/numdlgg.cpp
made wxFFile a bit more safe: don't crash when Tell() and Length() are called on...
[wxWidgets.git] / src / generic / numdlgg.cpp
CommitLineData
31528cd3
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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
6aa89a22 9// Licence: wxWindows licence
31528cd3
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
ff83a770 21 #pragma implementation "numdlgg.cpp"
31528cd3
VZ
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
1e6feb95
VZ
31#if wxUSE_NUMBERDLG
32
31528cd3
VZ
33#ifndef WX_PRECOMP
34 #include <stdio.h>
35
36 #include "wx/utils.h"
37 #include "wx/dialog.h"
38 #include "wx/button.h"
39 #include "wx/stattext.h"
40 #include "wx/textctrl.h"
41 #include "wx/intl.h"
073478b3
RR
42 #include "wx/sizer.h"
43#endif
44
45#if wxUSE_STATLINE
46 #include "wx/statline.h"
31528cd3
VZ
47#endif
48
f6bcfd97 49#if !defined(__WIN16__) && wxUSE_SPINCTRL
678cd6de 50#include "wx/spinctrl.h"
f6bcfd97 51#endif
678cd6de 52
31528cd3 53// this is where wxGetNumberFromUser() is declared
fc5414a1 54#include "wx/numdlg.h"
31528cd3 55
a7540f46
VZ
56#if !wxUSE_SPINCTRL
57 // wxTextCtrl will do instead of wxSpinCtrl if we don't have it
58 #define wxSpinCtrl wxTextCtrl
59#endif
60
31528cd3
VZ
61// ============================================================================
62// implementation
63// ============================================================================
64
65// ----------------------------------------------------------------------------
66// wxNumberEntryDialog
67// ----------------------------------------------------------------------------
68
69BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
70 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
71 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
72END_EVENT_TABLE()
73
74wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
75 const wxString& message,
76 const wxString& prompt,
77 const wxString& caption,
78 long value,
79 long min,
80 long max,
81 const wxPoint& pos)
82 : wxDialog(parent, -1, caption,
83 pos, wxDefaultSize,
8e877c19 84 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL )
31528cd3 85{
31528cd3
VZ
86 m_value = value;
87 m_max = max;
88 m_min = min;
89
073478b3 90 wxBeginBusyCursor();
678cd6de 91
92afa2b1 92 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
31528cd3 93
073478b3 94 // 1) text message
92afa2b1 95 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
678cd6de 96
073478b3 97 // 2) prompt and text ctrl
92afa2b1 98 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
073478b3
RR
99 // prompt if any
100 if (!prompt.IsEmpty())
101 inputsizer->Add( new wxStaticText( this, -1, prompt ), 0, wxCENTER | wxLEFT, 10 );
31528cd3
VZ
102 // spin ctrl
103 wxString valStr;
fc5414a1 104 valStr.Printf(wxT("%ld"), m_value);
678cd6de 105 m_spinctrl = new wxSpinCtrl(this, -1, valStr, wxDefaultPosition, wxSize( 140, -1 ) );
f6bcfd97 106#if !defined(__WIN16__) && wxUSE_SPINCTRL
479cd5de 107 m_spinctrl->SetRange((int)m_min, (int)m_max);
5dd26b08 108#endif
073478b3 109 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
678cd6de 110 // add both
073478b3
RR
111 topsizer->Add( inputsizer, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
112
113#if wxUSE_STATLINE
114 // 3) static line
115 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
116#endif
117
073478b3 118 // 4) buttons
92afa2b1 119 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
678cd6de 120
073478b3
RR
121 SetSizer( topsizer );
122 SetAutoLayout( TRUE );
123
0ae2df30
RR
124 topsizer->SetSizeHints( this );
125 topsizer->Fit( this );
126
073478b3
RR
127 Centre( wxBOTH );
128
8282369a 129 m_spinctrl->SetSelection(-1, -1);
31528cd3 130 m_spinctrl->SetFocus();
073478b3
RR
131
132 wxEndBusyCursor();
31528cd3
VZ
133}
134
a4c97004 135void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
31528cd3 136{
a7540f46
VZ
137#if !wxUSE_SPINCTRL
138 wxString tmp = m_spinctrl->GetValue();
139 if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 )
fc5414a1 140 EndModal(wxID_CANCEL);
a7540f46
VZ
141 else
142#else
678cd6de 143 m_value = m_spinctrl->GetValue();
a7540f46 144#endif
678cd6de 145 if ( m_value < m_min || m_value > m_max )
31528cd3
VZ
146 {
147 // not a number or out of range
fc5414a1 148 EndModal(wxID_CANCEL);
31528cd3
VZ
149 }
150
151 EndModal(wxID_OK);
152}
153
a4c97004 154void 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
165long 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)
31528cd3 177 return dialog.GetValue();
fc5414a1
VZ
178
179 return -1;
31528cd3 180}
1e6feb95
VZ
181
182#endif // wxUSE_NUMBERDLG