]> git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
html filters has const methods now
[wxWidgets.git] / src / generic / progdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: progdlgg.h
3 // Purpose: wxProgressDialog class
4 // Author: Karsten Ballüder
5 // Modified by:
6 // Created: 09.05.1999
7 // RCS-ID: $Id$
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "progdlgg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/utils.h"
25 #include "wx/frame.h"
26 #include "wx/button.h"
27 #include "wx/stattext.h"
28 #include "wx/layout.h"
29 #include "wx/event.h"
30 #include "wx/gauge.h"
31 #include "wx/intl.h"
32 #include "wx/settings.h"
33 #include "wx/dcclient.h"
34 #endif
35
36 #if wxUSE_PROGRESSDLG
37
38 #include "wx/generic/progdlgg.h"
39
40 #define LAYOUT_X_MARGIN 8
41 #define LAYOUT_Y_MARGIN 8
42
43 // wxTextEntryDialog
44
45 #if !USE_SHARED_LIBRARY
46 BEGIN_EVENT_TABLE(wxProgressDialog, wxFrame)
47 EVT_BUTTON(-1, wxProgressDialog::OnCancel)
48 EVT_CLOSE(wxProgressDialog::OnClose)
49 END_EVENT_TABLE()
50
51 IMPLEMENT_CLASS(wxProgressDialog, wxFrame)
52 #endif
53
54 wxProgressDialog::wxProgressDialog(wxString const &title,
55 wxString const &message,
56 int maximum,
57 wxWindow *parent,
58 int style)
59 {
60 wxWindow *lastWindow = NULL;
61 bool hasAbortButton = (style & wxPD_CAN_ABORT) != 0;
62 m_state = hasAbortButton ? Continue : Uncancelable;
63 m_disableParentOnly = (style & wxPD_APP_MODAL) == 0;
64 m_parent = parent;
65 m_maximum = maximum;
66
67 m_elapsed = m_estimated = m_remaining = NULL;
68 if ((style & (wxPD_ELAPSED_TIME | wxPD_ESTIMATED_TIME |
69 wxPD_REMAINING_TIME)) != 0)
70 m_time = new wxTime;
71 else
72 m_time = NULL;
73
74 wxFrame::Create(m_parent, -1, title, wxDefaultPosition,
75 wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
76 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
77
78 wxLayoutConstraints *c;
79
80 wxClientDC dc(this);
81 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
82 long widthText;
83 dc.GetTextExtent(message, &widthText, NULL);
84
85 m_msg = new wxStaticText(this, -1, message);
86 c = new wxLayoutConstraints;
87 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
88 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
89 c->width.AsIs();
90 c->height.AsIs();
91 m_msg->SetConstraints(c);
92 lastWindow = m_msg;
93
94 if ( maximum > 0 )
95 {
96 m_gauge = new wxGauge(this, -1, maximum,
97 wxDefaultPosition, wxDefaultSize,
98 wxGA_HORIZONTAL | wxRAISED_BORDER);
99 c = new wxLayoutConstraints;
100 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
101 c->top.Below(m_msg, 2*LAYOUT_Y_MARGIN);
102 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
103 c->height.AsIs();
104 m_gauge->SetConstraints(c);
105 m_gauge->SetValue(0);
106 lastWindow = m_gauge;
107 }
108 else
109 m_gauge = (wxGauge *)NULL;
110
111
112 if ( style & wxPD_ELAPSED_TIME )
113 {
114 m_elapsed = new wxStaticText(this, -1, "");
115 c = new wxLayoutConstraints;
116 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
117 c->top.Below(lastWindow, LAYOUT_Y_MARGIN);
118 c->width.Absolute(60);
119 c->height.AsIs();
120 m_elapsed->SetConstraints(c);
121
122 wxStaticText *dummy = new wxStaticText(this, -1, _T("Elapsed time : "));
123 c = new wxLayoutConstraints;
124 c->right.LeftOf(m_elapsed);
125 c->top.SameAs(m_elapsed, wxTop, 0);
126 c->width.AsIs();
127 c->height.AsIs();
128 dummy->SetConstraints(c);
129
130 lastWindow = m_elapsed;
131 }
132
133 if ( style & wxPD_ESTIMATED_TIME )
134 {
135 m_estimated = new wxStaticText(this, -1, "");
136 c = new wxLayoutConstraints;
137 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
138 c->top.Below(lastWindow, 0);
139 c->width.Absolute(60);
140 c->height.AsIs();
141 m_estimated->SetConstraints(c);
142
143 wxStaticText *dummy = new wxStaticText(this, -1, _T("Estimated time : "));
144 c = new wxLayoutConstraints;
145 c->right.LeftOf(m_estimated);
146 c->top.SameAs(m_estimated, wxTop, 0);
147 c->width.AsIs();
148 c->height.AsIs();
149 dummy->SetConstraints(c);
150
151 lastWindow = m_estimated;
152 }
153
154 if ( style & wxPD_REMAINING_TIME )
155 {
156 m_remaining = new wxStaticText(this, -1, "");
157 c = new wxLayoutConstraints;
158 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
159 c->top.Below(lastWindow, 0);
160 c->width.Absolute(60);
161 c->height.AsIs();
162 m_remaining->SetConstraints(c);
163
164 wxStaticText *dummy = new wxStaticText(this, -1, _T("Remaining time : "));
165 c = new wxLayoutConstraints;
166 c->right.LeftOf(m_remaining);
167 c->top.SameAs(m_remaining, wxTop, 0);
168 c->width.AsIs();
169 c->height.AsIs();
170 dummy->SetConstraints(c);
171
172 lastWindow = m_remaining;
173 }
174
175 if ( hasAbortButton )
176 {
177 m_btnAbort = new wxButton(this, -1, _("Cancel"));
178 c = new wxLayoutConstraints;
179 c->centreX.SameAs(this, wxCentreX);
180 c->top.Below(lastWindow, 2*LAYOUT_Y_MARGIN);
181 c->width.AsIs();
182 c->height.AsIs();
183 m_btnAbort->SetConstraints(c);
184 }
185 else
186 m_btnAbort = (wxButton *)NULL;
187
188 SetAutoLayout(TRUE);
189 Layout();
190
191 // calc the height of the dialog
192 Fit();
193 // and set the width from it - unfortunately, Fit() makes the dialog way too
194 // wide under Windows, so try to find a reasonable value for the width, not
195 // too big and not too small
196 wxSize size = GetClientSize();
197 size.x = wxMax(3*widthText/2, 2*size.y);
198 SetClientSize(size);
199
200 Show(TRUE);
201 Centre(wxCENTER_FRAME | wxBOTH);
202
203 if(m_disableParentOnly)
204 {
205 if(m_parent) m_parent->Enable(FALSE);
206 }
207 else
208 wxEnableTopLevelWindows(FALSE);
209
210 Enable(TRUE); // enable this window
211 wxYield();
212 }
213
214
215 bool
216 wxProgressDialog::Update(int value, const wxString& newmsg)
217 {
218 wxASSERT_MSG( value == -1 || m_gauge, _T("cannot update non existent dialog") );
219 wxASSERT_MSG( value <= m_maximum, _T("invalid progress value") );
220
221
222 if( m_gauge )
223 m_gauge->SetValue(value + 1);
224
225 if( !newmsg.IsEmpty() )
226 m_msg->SetLabel(newmsg);
227
228 if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) )
229 {
230 wxTime timenow;
231 wxTime diff = timenow - *m_time;
232 unsigned long secs = diff.GetSecond() + 60 * diff.GetMinute() + 60 * 60 * diff.GetHour();
233 unsigned long estim = secs * m_maximum / value;
234 unsigned long remai = estim - secs;
235 wxString s;
236
237 if (m_elapsed)
238 {
239 s.Printf(_T("%i:%02i:%02i"), diff.GetHour(), diff.GetMinute(), diff.GetSecond());
240 if (s != m_elapsed->GetLabel()) m_elapsed->SetLabel(s);
241 }
242 if (m_estimated)
243 {
244 s.Printf(_T("%i:%02i:%02i"), estim / (60 * 60), (estim / 60) % 60, estim % 60);
245 if (s != m_estimated->GetLabel()) m_estimated->SetLabel(s);
246 }
247 if (m_remaining)
248 {
249 s.Printf(_T("%i:%02i:%02i"), remai / (60 * 60), (remai / 60) % 60, remai % 60);
250 if (s != m_remaining->GetLabel()) m_remaining->SetLabel(s);
251 }
252 }
253
254 if ( (value == m_maximum - 1) && !(GetWindowStyleFlag() & wxPD_AUTO_HIDE) )
255 {
256 if ( m_btnAbort )
257 {
258 // tell the user what he should do...
259 m_btnAbort->SetLabel(_("Close"));
260 }
261
262 /*I think the default should be the other way round. If the
263 application wants to set a "Done." message at the end, it should
264 supply it. Any serious objections to this change? Makes the
265 application programmers' work a little easier.
266 if ( !newmsg )
267 {
268 // also provide the finishing message if the application didn't
269 m_msg->SetLabel(_("Done."));
270 }
271 */
272 m_state = Finished;
273
274 // so that we return TRUE below
275 m_state = Finished;
276 }
277 wxYield();
278 return m_state != Canceled;
279 }
280
281 void wxProgressDialog::OnClose(wxCloseEvent& event)
282 {
283 if ( m_state == Uncancelable )
284 event.Veto(TRUE);
285 else
286 m_state = Canceled;
287 }
288
289
290 wxProgressDialog::~wxProgressDialog()
291 {
292 if ( m_disableParentOnly )
293 {
294 if(m_parent) m_parent->Enable(TRUE);
295 }
296 else
297 wxEnableTopLevelWindows(TRUE);
298 if (m_time) delete m_time;
299 }
300
301 #endif