]>
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 #if !USE_SHARED_LIBRARY
68 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
69 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog::OnCancel
)
70 EVT_CLOSE(wxProgressDialog::OnClose
)
73 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 wxProgressDialog::wxProgressDialog(wxString
const &title
,
85 wxString
const &message
,
89 : wxDialog(parent
, -1, title
)
91 bool hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
92 m_state
= hasAbortButton
? Continue
: Uncancelable
;
93 m_disableParentOnly
= (style
& wxPD_APP_MODAL
) == 0;
94 m_AutoHide
= (style
& wxPD_AUTO_HIDE
) != 0;
98 wxLayoutConstraints
*c
;
101 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
103 dc
.GetTextExtent(message
, &widthText
, NULL
);
105 m_msg
= new wxStaticText(this, -1, message
);
106 c
= new wxLayoutConstraints
;
107 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
108 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
111 m_msg
->SetConstraints(c
);
113 wxSize sizeDlg
, sizeLabel
= m_msg
->GetSize();
114 sizeDlg
.y
= 2*LAYOUT_Y_MARGIN
+ sizeLabel
.y
;
116 wxWindow
*lastWindow
= m_msg
;
120 m_gauge
= new wxGauge(this, -1, maximum
,
121 wxDefaultPosition
, wxDefaultSize
,
122 wxGA_HORIZONTAL
| wxRAISED_BORDER
);
123 c
= new wxLayoutConstraints
;
124 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
125 c
->top
.Below(m_msg
, 2*LAYOUT_Y_MARGIN
);
126 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
128 m_gauge
->SetConstraints(c
);
129 m_gauge
->SetValue(0);
130 lastWindow
= m_gauge
;
132 wxSize sizeGauge
= m_gauge
->GetSize();
133 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeGauge
.y
;
136 m_gauge
= (wxGauge
*)NULL
;
138 // create the estimated/remaining/total time zones if requested
139 m_elapsed
= m_estimated
= m_remaining
= NULL
;
142 if ( style
& wxPD_ELAPSED_TIME
)
146 m_elapsed
= CreateLabel(_T("Elapsed time : "), &lastWindow
);
149 if ( style
& wxPD_ESTIMATED_TIME
)
153 m_estimated
= CreateLabel(_T("Estimated time : "), &lastWindow
);
156 if ( style
& wxPD_REMAINING_TIME
)
160 m_remaining
= CreateLabel(_T("Remaining time : "), &lastWindow
);
163 if ( nTimeLabels
> 0 )
165 // set it to the current time
166 m_timeStart
= wxGetCurrentTime();
168 sizeDlg
.y
+= nTimeLabels
* (sizeLabel
.y
+ LAYOUT_Y_MARGIN
);
171 if ( hasAbortButton
)
173 m_btnAbort
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
174 c
= new wxLayoutConstraints
;
176 // Windows dialogs usually have buttons in the lower right corner
178 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
180 c
->centreX
.SameAs(this, wxCentreX
);
182 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_Y_MARGIN
);
184 wxSize sizeBtn
= wxButton::GetDefaultSize();
185 c
->width
.Absolute(sizeBtn
.x
);
186 c
->height
.Absolute(sizeBtn
.y
);
188 m_btnAbort
->SetConstraints(c
);
190 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeBtn
.y
;
193 m_btnAbort
= (wxButton
*)NULL
;
198 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
;
200 // try to make the dialog not square but rectangular of reasonabel width
201 sizeDlg
.x
= wxMax(widthText
, 4*sizeDlg
.y
/3);
204 SetClientSize(sizeDlg
);
206 Centre(wxCENTER_FRAME
| wxBOTH
);
208 if (m_disableParentOnly
)
211 m_parent
->Enable(FALSE
);
215 wxEnableTopLevelWindows(FALSE
);
219 Enable(TRUE
); // enable this window
222 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
223 wxWindow
**lastWindow
)
225 wxLayoutConstraints
*c
;
227 wxStaticText
*label
= new wxStaticText(this, -1, _T("unknown"));
228 c
= new wxLayoutConstraints
;
230 // VZ: I like the labels be centered - if the others don't mind, you may
231 // remove "#ifdef __WXMSW__" and use it for all ports
233 c
->left
.SameAs(this, wxCentreX
, LAYOUT_X_MARGIN
);
235 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
237 c
->top
.Below(*lastWindow
, LAYOUT_Y_MARGIN
);
240 label
->SetConstraints(c
);
242 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
243 c
= new wxLayoutConstraints
;
244 c
->right
.LeftOf(label
);
245 c
->top
.SameAs(label
, wxTop
, 0);
248 dummy
->SetConstraints(c
);
256 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
258 wxASSERT_MSG( value
== -1 || m_gauge
, _T("cannot update non existent dialog") );
259 wxASSERT_MSG( value
<= m_maximum
, _T("invalid progress value") );
263 m_gauge
->SetValue(value
+ 1);
265 if( !newmsg
.IsEmpty() )
266 m_msg
->SetLabel(newmsg
);
268 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
270 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
271 unsigned long estimated
= elapsed
* m_maximum
/ value
;
272 unsigned long remaining
= estimated
- elapsed
;
274 SetTimeLabel(elapsed
, m_elapsed
);
275 SetTimeLabel(estimated
, m_estimated
);
276 SetTimeLabel(remaining
, m_remaining
);
279 if ( (value
== m_maximum
) && !m_AutoHide
)
283 // tell the user what he should do...
284 m_btnAbort
->SetLabel(_("Close"));
289 // also provide the finishing message if the application didn't
290 m_msg
->SetLabel(_("Done."));
293 // so that we return TRUE below and that out [Cancel] handler knew what
303 // update the display
307 return m_state
!= Canceled
;
310 // ----------------------------------------------------------------------------
312 // ----------------------------------------------------------------------------
314 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
316 if ( m_state
== Finished
)
318 // this means that the count down is already finished and we're being
319 // shown as a modal dialog - so just let the default handler do the job
324 // request to cancel was received, the next time Update() is called we
330 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
332 if ( m_state
== Uncancelable
)
338 // ----------------------------------------------------------------------------
340 // ----------------------------------------------------------------------------
342 wxProgressDialog::~wxProgressDialog()
344 if ( m_disableParentOnly
)
347 m_parent
->Enable(TRUE
);
351 wxEnableTopLevelWindows(TRUE
);
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
359 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
364 unsigned long hours
= val
/ 3600;
365 unsigned long minutes
= (val
% 3600) / 60;
366 unsigned long seconds
= val
% 60;
367 s
.Printf(_T("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
369 if ( s
!= label
->GetLabel() )
374 #endif // wxUSE_PROGRESSDLG