]>
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
)
70 EVT_SHOW(wxProgressDialog::OnShow
)
72 EVT_CLOSE(wxProgressDialog::OnClose
)
75 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
77 // ============================================================================
79 // ============================================================================
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 wxProgressDialog::wxProgressDialog(wxString
const &title
,
86 wxString
const &message
,
90 : wxDialog(parent
, -1, title
)
92 // we may disappear at any moment, let the others know about it
93 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
95 m_windowStyle
|= style
;
97 bool hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
99 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
100 // we have to remove the "Close" button from the title bar then as it is
101 // confusing to have it - it doesn't work anyhow
103 // FIXME: should probably have a (extended?) window style for this
104 if ( !hasAbortButton
)
106 EnableCloseButton(FALSE
);
110 m_state
= hasAbortButton
? Continue
: Uncancelable
;
114 // we can't have values > 65,536 in the progress control under Windows, so
115 // scale everything down
116 m_factor
= m_maximum
/ 65536 + 1;
117 m_maximum
/= m_factor
;
120 m_parentTop
= parent
;
121 while ( m_parentTop
&& m_parentTop
->GetParent() )
123 m_parentTop
= m_parentTop
->GetParent();
126 wxLayoutConstraints
*c
;
129 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
131 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
133 m_msg
= new wxStaticText(this, -1, message
);
134 c
= new wxLayoutConstraints
;
135 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
136 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
139 m_msg
->SetConstraints(c
);
142 sizeLabel
= m_msg
->GetSize();
143 sizeDlg
.y
= 2*LAYOUT_Y_MARGIN
+ sizeLabel
.y
;
145 wxWindow
*lastWindow
= m_msg
;
149 // note that we can't use wxGA_SMOOTH because it happens to also mean
150 // wxDIALOG_MODAL and will cause the dialog to be modal. Have an extra
151 // style argument to wxProgressDialog, perhaps.
152 m_gauge
= new wxGauge(this, -1, m_maximum
,
153 wxDefaultPosition
, wxDefaultSize
,
156 c
= new wxLayoutConstraints
;
157 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
158 c
->top
.Below(m_msg
, 2*LAYOUT_Y_MARGIN
);
159 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
161 m_gauge
->SetConstraints(c
);
162 m_gauge
->SetValue(0);
163 lastWindow
= m_gauge
;
165 wxSize sizeGauge
= m_gauge
->GetSize();
166 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeGauge
.y
;
169 m_gauge
= (wxGauge
*)NULL
;
171 // create the estimated/remaining/total time zones if requested
172 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
174 // if we are going to have at least one label, remmeber it in this var
175 wxStaticText
*label
= NULL
;
177 // also count how many labels we really have
178 size_t nTimeLabels
= 0;
180 if ( style
& wxPD_ELAPSED_TIME
)
185 m_elapsed
= CreateLabel(_("Elapsed time : "), &lastWindow
);
188 if ( style
& wxPD_ESTIMATED_TIME
)
193 m_estimated
= CreateLabel(_("Estimated time : "), &lastWindow
);
196 if ( style
& wxPD_REMAINING_TIME
)
201 m_remaining
= CreateLabel(_("Remaining time : "), &lastWindow
);
204 if ( nTimeLabels
> 0 )
206 // set it to the current time
207 m_timeStart
= wxGetCurrentTime();
208 sizeDlg
.y
+= nTimeLabels
* (label
->GetSize().y
+ LAYOUT_Y_MARGIN
);
211 if ( hasAbortButton
)
213 m_btnAbort
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
214 c
= new wxLayoutConstraints
;
216 // Windows dialogs usually have buttons in the lower right corner
218 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
220 c
->centreX
.SameAs(this, wxCentreX
);
222 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_Y_MARGIN
);
227 m_btnAbort
->SetConstraints(c
);
229 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ wxButton::GetDefaultSize().y
;
231 else // no "Cancel" button
233 m_btnAbort
= (wxButton
*)NULL
;
239 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
;
241 // try to make the dialog not square but rectangular of reasonabel width
242 sizeDlg
.x
= (wxCoord
)wxMax(widthText
, 4*sizeDlg
.y
/3);
245 SetClientSize(sizeDlg
);
247 Centre(wxCENTER_FRAME
| wxBOTH
);
249 if ( style
& wxPD_APP_MODAL
)
251 m_winDisabler
= new wxWindowDisabler(this);
256 m_parentTop
->Enable(FALSE
);
257 m_winDisabler
= NULL
;
261 Enable(TRUE
); // enable this window
263 // this one can be initialized even if the others are unknown for now
265 // NB: do it after calling Layout() to keep the labels correctly aligned
268 SetTimeLabel(0, m_elapsed
);
271 // Update the display (especially on X, GTK)
276 MacUpdateImmediately();
280 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
281 wxWindow
**lastWindow
)
283 wxLayoutConstraints
*c
;
285 wxStaticText
*label
= new wxStaticText(this, -1, _("unknown"));
286 c
= new wxLayoutConstraints
;
288 // VZ: I like the labels be centered - if the others don't mind, you may
289 // remove "#ifdef __WXMSW__" and use it for all ports
291 c
->left
.SameAs(this, wxCentreX
, LAYOUT_X_MARGIN
);
293 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
295 c
->top
.Below(*lastWindow
, LAYOUT_Y_MARGIN
);
298 label
->SetConstraints(c
);
300 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
301 c
= new wxLayoutConstraints
;
302 c
->right
.LeftOf(label
);
303 c
->top
.SameAs(label
, wxTop
, 0);
306 dummy
->SetConstraints(c
);
314 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
316 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
322 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
326 m_gauge
->SetValue(value
+ 1);
329 if ( !newmsg
.IsEmpty() )
331 m_msg
->SetLabel(newmsg
);
337 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
339 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
340 unsigned long estimated
= elapsed
* m_maximum
/ value
;
341 unsigned long remaining
= estimated
- elapsed
;
343 SetTimeLabel(elapsed
, m_elapsed
);
344 SetTimeLabel(estimated
, m_estimated
);
345 SetTimeLabel(remaining
, m_remaining
);
348 if ( (value
== m_maximum
) )
350 // so that we return TRUE below and that out [Cancel] handler knew what
353 if( !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
357 // tell the user what he should do...
358 m_btnAbort
->SetLabel(_("Close"));
360 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
361 else // enable the close button to give the user a way to close the dlg
363 EnableCloseButton(TRUE
);
369 // also provide the finishing message if the application didn't
370 m_msg
->SetLabel(_("Done."));
381 ReenableOtherWindows();
386 // update the display
392 MacUpdateImmediately();
395 return m_state
!= Canceled
;
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
404 if ( m_state
== Finished
)
406 // this means that the count down is already finished and we're being
407 // shown as a modal dialog - so just let the default handler do the job
412 // request to cancel was received, the next time Update() is called we
416 // update the button state immediately so that the user knows that the
417 // request has been noticed
418 m_btnAbort
->Disable();
422 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
424 if ( m_state
== Uncancelable
)
426 // can't close this dialog
429 else if ( m_state
== Finished
)
431 // let the default handler close the window as we already terminated
436 // next Update() will notice it
441 void wxProgressDialog::OnShow(wxShowEvent
& event
)
443 // if the dialog is being hidden, it was closed, so reenable other windows
445 if ( event
.GetShow() )
447 ReenableOtherWindows();
451 // ----------------------------------------------------------------------------
453 // ----------------------------------------------------------------------------
455 wxProgressDialog::~wxProgressDialog()
457 // normally this should have been already done, but just in case
458 ReenableOtherWindows();
461 void wxProgressDialog::ReenableOtherWindows()
463 if ( GetWindowStyle() & wxPD_APP_MODAL
)
465 delete m_winDisabler
;
466 m_winDisabler
= (wxWindowDisabler
*)NULL
;
471 m_parentTop
->Enable(TRUE
);
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
484 unsigned long hours
= val
/ 3600;
485 unsigned long minutes
= (val
% 3600) / 60;
486 unsigned long seconds
= val
% 60;
487 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
489 if ( s
!= label
->GetLabel() )
494 #endif // wxUSE_PROGRESSDLG