]> git.saurik.com Git - wxWidgets.git/blob - src/generic/numdlgg.cpp
Optimized sizers to not call CalcMin more often than neccessary
[wxWidgets.git] / src / generic / numdlgg.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "numdlgg.cpp"
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
31 #if wxUSE_NUMBERDLG
32
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"
42 #include "wx/sizer.h"
43 #endif
44
45 #if wxUSE_STATLINE
46 #include "wx/statline.h"
47 #endif
48
49 #if wxUSE_SPINCTRL
50 #include "wx/spinctrl.h"
51 #endif
52
53 // this is where wxGetNumberFromUser() is declared
54 #include "wx/numdlg.h"
55
56 #if !wxUSE_SPINCTRL
57 // wxTextCtrl will do instead of wxSpinCtrl if we don't have it
58 #define wxSpinCtrl wxTextCtrl
59 #endif
60
61 // ============================================================================
62 // implementation
63 // ============================================================================
64
65 // ----------------------------------------------------------------------------
66 // wxNumberEntryDialog
67 // ----------------------------------------------------------------------------
68
69 BEGIN_EVENT_TABLE(wxNumberEntryDialog, wxDialog)
70 EVT_BUTTON(wxID_OK, wxNumberEntryDialog::OnOK)
71 EVT_BUTTON(wxID_CANCEL, wxNumberEntryDialog::OnCancel)
72 END_EVENT_TABLE()
73
74 IMPLEMENT_CLASS(wxNumberEntryDialog, wxDialog)
75
76 wxNumberEntryDialog::wxNumberEntryDialog(wxWindow *parent,
77 const wxString& message,
78 const wxString& prompt,
79 const wxString& caption,
80 long value,
81 long min,
82 long max,
83 const wxPoint& pos)
84 : wxDialog(parent, wxID_ANY, caption,
85 pos, wxDefaultSize)
86 {
87 m_value = value;
88 m_max = max;
89 m_min = min;
90
91 wxBeginBusyCursor();
92
93 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
94
95 // 1) text message
96 topsizer->Add( CreateTextSizer( message ), 0, wxALL, 10 );
97
98 // 2) prompt and text ctrl
99 wxBoxSizer *inputsizer = new wxBoxSizer( wxHORIZONTAL );
100 // prompt if any
101 if (!prompt.IsEmpty())
102 inputsizer->Add( new wxStaticText( this, wxID_ANY, prompt ), 0, wxCENTER | wxLEFT, 10 );
103 // spin ctrl
104 wxString valStr;
105 valStr.Printf(wxT("%ld"), m_value);
106 m_spinctrl = new wxSpinCtrl(this, wxID_ANY, valStr, wxDefaultPosition, wxSize( 140, wxDefaultSize.y ) );
107 #if wxUSE_SPINCTRL
108 m_spinctrl->SetRange((int)m_min, (int)m_max);
109 #endif
110 inputsizer->Add( m_spinctrl, 1, wxCENTER | wxLEFT | wxRIGHT, 10 );
111 // add both
112 topsizer->Add( inputsizer, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
113
114 #if wxUSE_STATLINE
115 // 3) static line
116 topsizer->Add( new wxStaticLine( this, wxID_ANY ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
117 #endif
118
119 // 4) buttons
120 topsizer->Add( CreateButtonSizer( wxOK|wxCANCEL ), 0, wxCENTRE | wxALL, 10 );
121
122 SetSizer( topsizer );
123 SetAutoLayout( true );
124
125 topsizer->SetSizeHints( this );
126 topsizer->Fit( this );
127
128 Centre( wxBOTH );
129
130 m_spinctrl->SetSelection(-1, -1);
131 m_spinctrl->SetFocus();
132
133 wxEndBusyCursor();
134 }
135
136 void wxNumberEntryDialog::OnOK(wxCommandEvent& WXUNUSED(event))
137 {
138 #if !wxUSE_SPINCTRL
139 wxString tmp = m_spinctrl->GetValue();
140 if ( wxSscanf(tmp, _T("%ld"), &m_value) != 1 )
141 EndModal(wxID_CANCEL);
142 else
143 #else
144 m_value = m_spinctrl->GetValue();
145 #endif
146 if ( m_value < m_min || m_value > m_max )
147 {
148 // not a number or out of range
149 EndModal(wxID_CANCEL);
150 }
151
152 EndModal(wxID_OK);
153 }
154
155 void wxNumberEntryDialog::OnCancel(wxCommandEvent& WXUNUSED(event))
156 {
157 EndModal(wxID_CANCEL);
158 }
159
160 // ----------------------------------------------------------------------------
161 // global functions
162 // ----------------------------------------------------------------------------
163
164 // wxGetTextFromUser is in utilscmn.cpp
165
166 long wxGetNumberFromUser(const wxString& msg,
167 const wxString& prompt,
168 const wxString& title,
169 long value,
170 long min,
171 long max,
172 wxWindow *parent,
173 const wxPoint& pos)
174 {
175 wxNumberEntryDialog dialog(parent, msg, prompt, title,
176 value, min, max, pos);
177 if (dialog.ShowModal() == wxID_OK)
178 return dialog.GetValue();
179
180 return -1;
181 }
182
183 #endif // wxUSE_NUMBERDLG