]> git.saurik.com Git - wxWidgets.git/blame - src/generic/numdlgg.cpp
fixed DrawTextFormatted to work in O(n) instead of O(n^2) if the text doesn't fit...
[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
20#ifdef __GNUG__
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
470d5a67 54#include "wx/textdlg.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// private classes
63// ----------------------------------------------------------------------------
64
65class WXDLLEXPORT wxNumberEntryDialog : public wxDialog
66{
67public:
68 wxNumberEntryDialog(wxWindow *parent,
69 const wxString& message,
70 const wxString& prompt,
71 const wxString& caption,
72 long value, long min, long max,
73 const wxPoint& pos);
74
75 long GetValue() const { return m_value; }
76
77 // implementation only
78 void OnOK(wxCommandEvent& event);
79 void OnCancel(wxCommandEvent& event);
80
81protected:
678cd6de 82 wxSpinCtrl *m_spinctrl;
31528cd3
VZ
83
84 long m_value, m_min, m_max;
85
86private:
87 DECLARE_EVENT_TABLE()
22f3361e 88 DECLARE_NO_COPY_CLASS(wxNumberEntryDialog)
31528cd3
VZ
89};
90
91// ============================================================================
92// implementation
93// ============================================================================
94
95// ----------------------------------------------------------------------------
96// wxNumberEntryDialog
97// ----------------------------------------------------------------------------
98
99BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
100 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
101 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
102END_EVENT_TABLE()
103
104wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
105 const wxString& message,
106 const wxString& prompt,
107 const wxString& caption,
108 long value,
109 long min,
110 long max,
111 const wxPoint& pos)
112 : wxDialog(parent, -1, caption,
113 pos, wxDefaultSize,
8e877c19 114 wxDEFAULT_DIALOG_STYLE | wxDIALOG_MODAL )
31528cd3 115{
31528cd3
VZ
116 m_value = value;
117 m_max = max;
118 m_min = min;
119
073478b3 120 wxBeginBusyCursor();
678cd6de 121
92afa2b1 122 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
31528cd3 123
073478b3 124 // 1) text message
92afa2b1 125 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
678cd6de 126
073478b3 127 // 2) prompt and text ctrl
92afa2b1 128 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
073478b3
RR
129 // prompt if any
130 if (!prompt.IsEmpty())
131 inputsizer->Add( new wxStaticText( this, -1, prompt ), 0, wxCENTER | wxLEFT, 10 );
31528cd3
VZ
132 // spin ctrl
133 wxString valStr;
223d09f6 134 valStr.Printf(wxT("%lu"), m_value);
678cd6de 135 m_spinctrl = new wxSpinCtrl(this, -1, valStr, wxDefaultPosition, wxSize( 140, -1 ) );
f6bcfd97 136#if !defined(__WIN16__) && wxUSE_SPINCTRL
479cd5de 137 m_spinctrl->SetRange((int)m_min, (int)m_max);
5dd26b08 138#endif
073478b3 139 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
678cd6de 140 // add both
073478b3
RR
141 topsizer->Add( inputsizer, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
142
143#if wxUSE_STATLINE
144 // 3) static line
145 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
146#endif
147
073478b3 148 // 4) buttons
92afa2b1 149 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
678cd6de 150
073478b3
RR
151 SetSizer( topsizer );
152 SetAutoLayout( TRUE );
153
0ae2df30
RR
154 topsizer->SetSizeHints( this );
155 topsizer->Fit( this );
156
073478b3
RR
157 Centre( wxBOTH );
158
31528cd3 159 m_spinctrl->SetFocus();
073478b3
RR
160
161 wxEndBusyCursor();
31528cd3
VZ
162}
163
a4c97004 164void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
31528cd3 165{
a7540f46
VZ
166#if !wxUSE_SPINCTRL
167 wxString tmp = m_spinctrl->GetValue();
168 if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 )
169 m_value = -1;
170 else
171#else
678cd6de 172 m_value = m_spinctrl->GetValue();
a7540f46 173#endif
678cd6de 174 if ( m_value < m_min || m_value > m_max )
31528cd3
VZ
175 {
176 // not a number or out of range
177 m_value = -1;
178 }
179
180 EndModal(wxID_OK);
181}
182
a4c97004 183void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
31528cd3
VZ
184{
185 m_value = -1;
186
187 EndModal(wxID_CANCEL);
188}
189
190// ----------------------------------------------------------------------------
191// global functions
192// ----------------------------------------------------------------------------
193
194// wxGetTextFromUser is in utilscmn.cpp
195
196long wxGetNumberFromUser(const wxString& msg,
197 const wxString& prompt,
198 const wxString& title,
199 long value,
200 long min,
201 long max,
202 wxWindow *parent,
203 const wxPoint& pos)
204{
205 wxNumberEntryDialog dialog(parent, msg, prompt, title,
206 value, min, max, pos);
207 (void)dialog.ShowModal();
208
209 return dialog.GetValue();
210}
1e6feb95
VZ
211
212#endif // wxUSE_NUMBERDLG