]> git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
Applied fixes for AIX (GTK 1.0 compilation).
[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 | wxPD_REMAINING_TIME)) != 0) m_time = new wxTime;
69 else m_time = NULL;
70
71 wxFrame::Create(m_parent, -1, title, wxDefaultPosition,
72 wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
73 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
74
75 wxLayoutConstraints *c;
76
77 wxClientDC dc(this);
78 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
79 long widthText;
80 dc.GetTextExtent(message, &widthText, NULL);
81
82 m_msg = new wxStaticText(this, -1, message);
83 c = new wxLayoutConstraints;
84 c->left.SameAs(this, wxLeft, 10);
85 c->top.SameAs(this, wxTop, 10);
86 c->width.AsIs();
87 c->height.AsIs();
88 m_msg->SetConstraints(c);
89 lastWindow = m_msg;
90
91 if ( maximum > 0 )
92 {
93 m_gauge = new wxGauge(this, -1, maximum,
94 wxDefaultPosition, wxDefaultSize,
95 wxGA_HORIZONTAL | wxRAISED_BORDER);
96 c = new wxLayoutConstraints;
97 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
98 c->top.Below(m_msg, 2*LAYOUT_Y_MARGIN);
99 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
100 c->height.AsIs();
101 m_gauge->SetConstraints(c);
102 m_gauge->SetValue(0);
103 lastWindow = m_gauge;
104 }
105 else
106 m_gauge = (wxGauge *)NULL;
107
108
109 if ( style & wxPD_ELAPSED_TIME )
110 {
111 m_elapsed = new wxStaticText(this, -1, "");
112 c = new wxLayoutConstraints;
113 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
114 c->top.Below(lastWindow, LAYOUT_Y_MARGIN);
115 c->width.Absolute(60);
116 c->height.AsIs();
117 m_elapsed->SetConstraints(c);
118
119 wxStaticText *dummy = new wxStaticText(this, -1, _T("Elapsed time : "));
120 c = new wxLayoutConstraints;
121 c->right.LeftOf(m_elapsed);
122 c->top.SameAs(m_elapsed, wxTop, 0);
123 c->width.AsIs();
124 c->height.AsIs();
125 dummy->SetConstraints(c);
126
127 lastWindow = m_elapsed;
128 }
129
130 if ( style & wxPD_ESTIMATED_TIME )
131 {
132 m_estimated = new wxStaticText(this, -1, "");
133 c = new wxLayoutConstraints;
134 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
135 c->top.Below(lastWindow, 0);
136 c->width.Absolute(60);
137 c->height.AsIs();
138 m_estimated->SetConstraints(c);
139
140 wxStaticText *dummy = new wxStaticText(this, -1, _T("Estimated time : "));
141 c = new wxLayoutConstraints;
142 c->right.LeftOf(m_estimated);
143 c->top.SameAs(m_estimated, wxTop, 0);
144 c->width.AsIs();
145 c->height.AsIs();
146 dummy->SetConstraints(c);
147
148 lastWindow = m_estimated;
149 }
150
151 if ( style & wxPD_REMAINING_TIME )
152 {
153 m_remaining = new wxStaticText(this, -1, "");
154 c = new wxLayoutConstraints;
155 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
156 c->top.Below(lastWindow, 0);
157 c->width.Absolute(60);
158 c->height.AsIs();
159 m_remaining->SetConstraints(c);
160
161 wxStaticText *dummy = new wxStaticText(this, -1, _T("Remaining time : "));
162 c = new wxLayoutConstraints;
163 c->right.LeftOf(m_remaining);
164 c->top.SameAs(m_remaining, wxTop, 0);
165 c->width.AsIs();
166 c->height.AsIs();
167 dummy->SetConstraints(c);
168
169 lastWindow = m_remaining;
170 }
171
172 if ( hasAbortButton )
173 {
174 m_btnAbort = new wxButton(this, -1, _("Cancel"));
175 c = new wxLayoutConstraints;
176 c->centreX.SameAs(this, wxCentreX);
177 if(lastWindow)
178 c->top.Below(lastWindow, 2*LAYOUT_Y_MARGIN);
179 else
180 c->top.Below(m_btnAbort, 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 = 2*widthText;
198 if ( size.x < 2*size.y )
199 SetClientSize(2*size.y, size.y);
200
201 Show(TRUE);
202 Centre(wxCENTER_FRAME | wxBOTH);
203
204 if(m_disableParentOnly)
205 m_parent->Enable(FALSE);
206 else
207 wxEnableTopLevelWindows(FALSE);
208
209 Enable(TRUE); // enable this window
210 wxYield();
211 }
212
213
214 bool
215 wxProgressDialog::Update(int value, const wxString& newmsg)
216 {
217 wxASSERT_MSG( value == -1 || m_gauge, _T("can't update non existent dialog") );
218 wxASSERT_MSG( value < m_maximum, _T("invalid progress value") );
219
220
221 if( m_gauge )
222 m_gauge->SetValue(value + 1);
223
224 if( !newmsg.IsEmpty() )
225 m_msg->SetLabel(newmsg);
226
227 if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) )
228 {
229 wxTime timenow;
230 wxTime diff = timenow - *m_time;
231 unsigned long secs = diff.GetSecond() + 60 * diff.GetMinute() + 60 * 60 * diff.GetHour();
232 unsigned long estim = secs * m_maximum / value;
233 unsigned long remai = estim - secs;
234 wxString s;
235
236 if (m_elapsed)
237 {
238 s.Printf("%i:%02i:%02i", diff.GetHour(), diff.GetMinute(), diff.GetSecond());
239 if (s != m_elapsed->GetLabel()) m_elapsed->SetLabel(s);
240 }
241 if (m_estimated)
242 {
243 s.Printf("%i:%02i:%02i", estim / (60 * 60), (estim / 60) % 60, estim % 60);
244 if (s != m_estimated->GetLabel()) m_estimated->SetLabel(s);
245 }
246 if (m_remaining)
247 {
248 s.Printf("%i:%02i:%02i", remai / (60 * 60), (remai / 60) % 60, remai % 60);
249 if (s != m_remaining->GetLabel()) m_remaining->SetLabel(s);
250 }
251 }
252
253 if ( (value == m_maximum - 1) && !(GetWindowStyleFlag() & wxPD_AUTO_HIDE) )
254 {
255 if ( m_btnAbort )
256 {
257 // tell the user what he should do...
258 m_btnAbort->SetLabel(_("Close"));
259 }
260
261 if ( !newmsg )
262 {
263 // also provide the finishing message if the application didn't
264 m_msg->SetLabel(_("Done."));
265 }
266
267 m_state = Finished;
268
269 // so that we return TRUE below
270 m_state = Finished;
271 }
272
273 return m_state != Canceled;
274 }
275
276 void wxProgressDialog::OnClose(wxCloseEvent& event)
277 {
278 if ( m_state == Uncancelable )
279 event.Veto(TRUE);
280 else
281 m_state = Canceled;
282 }
283
284
285 wxProgressDialog::~wxProgressDialog()
286 {
287 if ( m_disableParentOnly )
288 m_parent->Enable(TRUE);
289 else
290 wxEnableTopLevelWindows(TRUE);
291 if (m_time) delete m_time;
292 }
293
294 #endif