]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxProgressDialog class
4 // Author: Karsten Ballüder
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "progdlgg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/button.h"
37 #include "wx/stattext.h"
38 #include "wx/layout.h"
42 #include "wx/settings.h"
43 #include "wx/dcclient.h"
47 #include "wx/generic/progdlgg.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #define LAYOUT_X_MARGIN 8
54 #define LAYOUT_Y_MARGIN 8
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // update the label to show the given time (in seconds)
61 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
);
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
68 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog::OnCancel
)
69 EVT_CLOSE(wxProgressDialog::OnClose
)
72 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 wxProgressDialog::wxProgressDialog(wxString
const &title
,
83 wxString
const &message
,
87 : wxDialog(parent
, -1, title
)
89 m_windowStyle
|= style
;
91 bool hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
92 m_state
= hasAbortButton
? Continue
: Uncancelable
;
96 while ( m_parentTop
&& m_parentTop
->GetParent() )
98 m_parentTop
= m_parentTop
->GetParent();
101 wxLayoutConstraints
*c
;
104 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
106 #if defined(__VISAGECPP__)
107 // have two versions of this in wxWindowDC tp avoid function hiding
108 // since there are two of these in wxDCBase, and in turn in wxDC.
109 // VA cannot resolve this so:
110 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
112 dc
.GetTextExtent(message
, &widthText
, (long*)NULL
);
115 m_msg
= new wxStaticText(this, -1, message
);
116 c
= new wxLayoutConstraints
;
117 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
118 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
121 m_msg
->SetConstraints(c
);
123 wxSize sizeDlg
, sizeLabel
= m_msg
->GetSize();
124 sizeDlg
.y
= 2*LAYOUT_Y_MARGIN
+ sizeLabel
.y
;
126 wxWindow
*lastWindow
= m_msg
;
130 m_gauge
= new wxGauge(this, -1, maximum
,
131 wxDefaultPosition
, wxDefaultSize
,
132 wxGA_HORIZONTAL
| wxRAISED_BORDER
);
133 // Sorry, but wxGA_SMOOTH happens to also mean wxDIALOG_MODAL and will
134 // cause the dialog to be modal. Have an extra style argument to wxProgressDialog,
136 // wxGA_HORIZONTAL | wxRAISED_BORDER | (style & wxGA_SMOOTH));
137 c
= new wxLayoutConstraints
;
138 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
139 c
->top
.Below(m_msg
, 2*LAYOUT_Y_MARGIN
);
140 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
142 m_gauge
->SetConstraints(c
);
143 m_gauge
->SetValue(0);
144 lastWindow
= m_gauge
;
146 wxSize sizeGauge
= m_gauge
->GetSize();
147 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeGauge
.y
;
150 m_gauge
= (wxGauge
*)NULL
;
152 // create the estimated/remaining/total time zones if requested
153 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
156 if ( style
& wxPD_ELAPSED_TIME
)
160 m_elapsed
= CreateLabel(_("Elapsed time : "), &lastWindow
);
163 if ( style
& wxPD_ESTIMATED_TIME
)
167 m_estimated
= CreateLabel(_("Estimated time : "), &lastWindow
);
170 if ( style
& wxPD_REMAINING_TIME
)
174 m_remaining
= CreateLabel(_("Remaining time : "), &lastWindow
);
177 if ( nTimeLabels
> 0 )
179 // set it to the current time
180 m_timeStart
= wxGetCurrentTime();
181 sizeDlg
.y
+= nTimeLabels
* (sizeLabel
.y
+ LAYOUT_Y_MARGIN
);
184 if ( hasAbortButton
)
186 m_btnAbort
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
187 c
= new wxLayoutConstraints
;
189 // Windows dialogs usually have buttons in the lower right corner
191 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
193 c
->centreX
.SameAs(this, wxCentreX
);
195 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_Y_MARGIN
);
197 wxSize sizeBtn
= wxButton::GetDefaultSize();
198 c
->width
.Absolute(sizeBtn
.x
);
199 c
->height
.Absolute(sizeBtn
.y
);
201 m_btnAbort
->SetConstraints(c
);
203 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeBtn
.y
;
206 m_btnAbort
= (wxButton
*)NULL
;
211 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
;
213 // try to make the dialog not square but rectangular of reasonabel width
214 sizeDlg
.x
= (wxCoord
)wxMax(widthText
, 4*sizeDlg
.y
/3);
217 SetClientSize(sizeDlg
);
219 Centre(wxCENTER_FRAME
| wxBOTH
);
221 if ( !(style
& wxPD_APP_MODAL
) )
224 m_parentTop
->Enable(FALSE
);
228 wxEnableTopLevelWindows(FALSE
);
232 Enable(TRUE
); // enable this window
234 // Update the display (especially on X, GTK)
238 MacUpdateImmediately();
242 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
243 wxWindow
**lastWindow
)
245 wxLayoutConstraints
*c
;
247 wxStaticText
*label
= new wxStaticText(this, -1, _("unknown"));
248 c
= new wxLayoutConstraints
;
250 // VZ: I like the labels be centered - if the others don't mind, you may
251 // remove "#ifdef __WXMSW__" and use it for all ports
253 c
->left
.SameAs(this, wxCentreX
, LAYOUT_X_MARGIN
);
255 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
257 c
->top
.Below(*lastWindow
, LAYOUT_Y_MARGIN
);
260 label
->SetConstraints(c
);
262 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
263 c
= new wxLayoutConstraints
;
264 c
->right
.LeftOf(label
);
265 c
->top
.SameAs(label
, wxTop
, 0);
268 dummy
->SetConstraints(c
);
276 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
278 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
279 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
282 m_gauge
->SetValue(value
+ 1);
284 if ( !newmsg
.IsEmpty() )
287 // this seems to be necessary or garbage is left when the new label is
288 // longer than the old one
289 m_msg
->SetLabel(wxEmptyString
);
292 m_msg
->SetLabel(newmsg
);
297 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
299 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
300 unsigned long estimated
= elapsed
* m_maximum
/ value
;
301 unsigned long remaining
= estimated
- elapsed
;
303 SetTimeLabel(elapsed
, m_elapsed
);
304 SetTimeLabel(estimated
, m_estimated
);
305 SetTimeLabel(remaining
, m_remaining
);
308 if ( (value
== m_maximum
) && !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
312 // tell the user what he should do...
313 m_btnAbort
->SetLabel(_("Close"));
318 // also provide the finishing message if the application didn't
319 m_msg
->SetLabel(_("Done."));
322 // so that we return TRUE below and that out [Cancel] handler knew what
332 // update the display
337 MacUpdateImmediately();
340 return m_state
!= Canceled
;
343 // ----------------------------------------------------------------------------
345 // ----------------------------------------------------------------------------
347 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
349 if ( m_state
== Finished
)
351 // this means that the count down is already finished and we're being
352 // shown as a modal dialog - so just let the default handler do the job
357 // request to cancel was received, the next time Update() is called we
361 // update the button state immediately so that the user knows that the
362 // request has been noticed
363 m_btnAbort
->Disable();
367 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
369 if ( m_state
== Uncancelable
)
371 // can't close this dialog
374 else if ( m_state
== Finished
)
376 // let the default handler close the window as we already terminated
381 // next Update() will notice it
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 wxProgressDialog::~wxProgressDialog()
392 if ( !(GetWindowStyle() & wxPD_APP_MODAL
) )
395 m_parentTop
->Enable(TRUE
);
399 wxEnableTopLevelWindows(TRUE
);
403 // ----------------------------------------------------------------------------
405 // ----------------------------------------------------------------------------
407 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
412 unsigned long hours
= val
/ 3600;
413 unsigned long minutes
= (val
% 3600) / 60;
414 unsigned long seconds
= val
% 60;
415 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
417 if ( s
!= label
->GetLabel() )
422 #endif // wxUSE_PROGRESSDLG