]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
64dd3b5f2c3c7a5c03b5ee39154594513fd6176c
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"
46 #include "wx/generic/progdlgg.h"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 #define LAYOUT_X_MARGIN 8
53 #define LAYOUT_Y_MARGIN 8
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // update the label to show the given time (in seconds)
60 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
);
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 #if !USE_SHARED_LIBRARY
67 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
68 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog::OnCancel
)
69 EVT_CLOSE(wxProgressDialog::OnClose
)
72 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 wxProgressDialog::wxProgressDialog(wxString
const &title
,
84 wxString
const &message
,
88 : wxDialog(m_parent
, -1, title
)
90 bool hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
91 m_state
= hasAbortButton
? Continue
: Uncancelable
;
92 m_disableParentOnly
= (style
& wxPD_APP_MODAL
) == 0;
96 wxLayoutConstraints
*c
;
99 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
101 dc
.GetTextExtent(message
, &widthText
, NULL
);
103 m_msg
= new wxStaticText(this, -1, message
);
104 c
= new wxLayoutConstraints
;
105 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
106 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
109 m_msg
->SetConstraints(c
);
111 wxSize sizeDlg
, sizeLabel
= m_msg
->GetSize();
112 sizeDlg
.y
= 2*LAYOUT_Y_MARGIN
+ sizeLabel
.y
;
114 wxWindow
*lastWindow
= m_msg
;
118 m_gauge
= new wxGauge(this, -1, maximum
,
119 wxDefaultPosition
, wxDefaultSize
,
120 wxGA_HORIZONTAL
| wxRAISED_BORDER
);
121 c
= new wxLayoutConstraints
;
122 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
123 c
->top
.Below(m_msg
, 2*LAYOUT_Y_MARGIN
);
124 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
126 m_gauge
->SetConstraints(c
);
127 m_gauge
->SetValue(0);
128 lastWindow
= m_gauge
;
130 wxSize sizeGauge
= m_gauge
->GetSize();
131 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeGauge
.y
;
134 m_gauge
= (wxGauge
*)NULL
;
136 // create the estimated/remaining/total time zones if requested
137 m_elapsed
= m_estimated
= m_remaining
= NULL
;
140 if ( style
& wxPD_ELAPSED_TIME
)
144 m_elapsed
= CreateLabel(_T("Elapsed time : "), &lastWindow
);
147 if ( style
& wxPD_ESTIMATED_TIME
)
151 m_estimated
= CreateLabel(_T("Estimated time : "), &lastWindow
);
154 if ( style
& wxPD_REMAINING_TIME
)
158 m_remaining
= CreateLabel(_T("Remaining time : "), &lastWindow
);
161 if ( nTimeLabels
> 0 )
163 // set it to the current time
164 m_timeStart
= wxGetCurrentTime();
166 sizeDlg
.y
+= nTimeLabels
* (sizeLabel
.y
+ LAYOUT_Y_MARGIN
);
169 if ( hasAbortButton
)
171 m_btnAbort
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
172 c
= new wxLayoutConstraints
;
174 // Windows dialogs usually have buttons in the lower right corner
176 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
178 c
->centreX
.SameAs(this, wxCentreX
);
180 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_Y_MARGIN
);
182 wxSize sizeBtn
= wxButton::GetDefaultSize();
183 c
->width
.Absolute(sizeBtn
.x
);
184 c
->height
.Absolute(sizeBtn
.y
);
186 m_btnAbort
->SetConstraints(c
);
188 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeBtn
.y
;
191 m_btnAbort
= (wxButton
*)NULL
;
196 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
;
198 // try to make the dialog not square but rectangular of reasonabel width
199 sizeDlg
.x
= wxMax(widthText
, 4*sizeDlg
.y
/3);
202 SetClientSize(sizeDlg
);
204 Centre(wxCENTER_FRAME
| wxBOTH
);
206 if (m_disableParentOnly
)
209 m_parent
->Enable(FALSE
);
213 wxEnableTopLevelWindows(FALSE
);
217 Enable(TRUE
); // enable this window
220 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
221 wxWindow
**lastWindow
)
223 wxLayoutConstraints
*c
;
225 wxStaticText
*label
= new wxStaticText(this, -1, _T("unknown"));
226 c
= new wxLayoutConstraints
;
228 // VZ: I like the labels be centered - if the others don't mind, you may
229 // remove "#ifdef __WXMSW__" and use it for all ports
231 c
->left
.SameAs(this, wxCentreX
, LAYOUT_X_MARGIN
);
233 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
235 c
->top
.Below(*lastWindow
, LAYOUT_Y_MARGIN
);
238 label
->SetConstraints(c
);
240 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
241 c
= new wxLayoutConstraints
;
242 c
->right
.LeftOf(label
);
243 c
->top
.SameAs(label
, wxTop
, 0);
246 dummy
->SetConstraints(c
);
254 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
256 wxASSERT_MSG( value
== -1 || m_gauge
, _T("cannot update non existent dialog") );
257 wxASSERT_MSG( value
<= m_maximum
, _T("invalid progress value") );
261 m_gauge
->SetValue(value
+ 1);
263 if( !newmsg
.IsEmpty() )
264 m_msg
->SetLabel(newmsg
);
266 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
268 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
269 unsigned long estimated
= elapsed
* m_maximum
/ value
;
270 unsigned long remaining
= estimated
- elapsed
;
272 SetTimeLabel(elapsed
, m_elapsed
);
273 SetTimeLabel(estimated
, m_estimated
);
274 SetTimeLabel(remaining
, m_remaining
);
277 if ( (value
== m_maximum
- 1) && !(GetWindowStyleFlag() & wxPD_AUTO_HIDE
) )
281 // tell the user what he should do...
282 m_btnAbort
->SetLabel(_("Close"));
287 // also provide the finishing message if the application didn't
288 m_msg
->SetLabel(_("Done."));
291 // so that we return TRUE below and that out [Cancel] handler knew what
301 // update the display
305 return m_state
!= Canceled
;
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
314 if ( m_state
== Finished
)
316 // this means that the count down is already finished and we're being
317 // shown as a modal dialog - so just let the default handler do the job
322 // request to cancel was received, the next time Update() is called we
328 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
330 if ( m_state
== Uncancelable
)
336 // ----------------------------------------------------------------------------
338 // ----------------------------------------------------------------------------
340 wxProgressDialog::~wxProgressDialog()
342 if ( m_disableParentOnly
)
345 m_parent
->Enable(TRUE
);
349 wxEnableTopLevelWindows(TRUE
);
353 // ----------------------------------------------------------------------------
355 // ----------------------------------------------------------------------------
357 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
362 unsigned long hours
= val
/ 3600;
363 unsigned long minutes
= (val
% 3600) / 60;
364 unsigned long seconds
= val
% 60;
365 s
.Printf(_T("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
367 if ( s
!= label
->GetLabel() )
372 #endif // wxUSE_PROGRESSDLG