]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
e21460c41a072031df57659d70394a07072d5e54
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 #if defined(__VISAGECPP__)
104 // have two versions of this in wxWindowDC tp avoid function hiding
105 // since there are two of these in wxDCBase, and in turn in wxDC.
106 // VA cannot resolve this so:
107 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
, FALSE
);
109 dc
.GetTextExtent(message
, &widthText
, NULL
);
112 m_msg
= new wxStaticText(this, -1, message
);
113 c
= new wxLayoutConstraints
;
114 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
115 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
118 m_msg
->SetConstraints(c
);
120 wxSize sizeDlg
, sizeLabel
= m_msg
->GetSize();
121 sizeDlg
.y
= 2*LAYOUT_Y_MARGIN
+ sizeLabel
.y
;
123 wxWindow
*lastWindow
= m_msg
;
127 m_gauge
= new wxGauge(this, -1, maximum
,
128 wxDefaultPosition
, wxDefaultSize
,
129 wxGA_HORIZONTAL
| wxRAISED_BORDER
);
130 c
= new wxLayoutConstraints
;
131 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
132 c
->top
.Below(m_msg
, 2*LAYOUT_Y_MARGIN
);
133 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
135 m_gauge
->SetConstraints(c
);
136 m_gauge
->SetValue(0);
137 lastWindow
= m_gauge
;
139 wxSize sizeGauge
= m_gauge
->GetSize();
140 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeGauge
.y
;
143 m_gauge
= (wxGauge
*)NULL
;
145 // create the estimated/remaining/total time zones if requested
146 m_elapsed
= m_estimated
= m_remaining
= NULL
;
149 if ( style
& wxPD_ELAPSED_TIME
)
153 m_elapsed
= CreateLabel(_T("Elapsed time : "), &lastWindow
);
156 if ( style
& wxPD_ESTIMATED_TIME
)
160 m_estimated
= CreateLabel(_T("Estimated time : "), &lastWindow
);
163 if ( style
& wxPD_REMAINING_TIME
)
167 m_remaining
= CreateLabel(_T("Remaining time : "), &lastWindow
);
170 if ( nTimeLabels
> 0 )
172 // set it to the current time
173 m_timeStart
= wxGetCurrentTime();
175 sizeDlg
.y
+= nTimeLabels
* (sizeLabel
.y
+ LAYOUT_Y_MARGIN
);
178 if ( hasAbortButton
)
180 m_btnAbort
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
181 c
= new wxLayoutConstraints
;
183 // Windows dialogs usually have buttons in the lower right corner
185 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
187 c
->centreX
.SameAs(this, wxCentreX
);
189 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_Y_MARGIN
);
191 wxSize sizeBtn
= wxButton::GetDefaultSize();
192 c
->width
.Absolute(sizeBtn
.x
);
193 c
->height
.Absolute(sizeBtn
.y
);
195 m_btnAbort
->SetConstraints(c
);
197 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeBtn
.y
;
200 m_btnAbort
= (wxButton
*)NULL
;
205 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
;
207 // try to make the dialog not square but rectangular of reasonabel width
208 sizeDlg
.x
= wxMax(widthText
, 4*sizeDlg
.y
/3);
211 SetClientSize(sizeDlg
);
213 Centre(wxCENTER_FRAME
| wxBOTH
);
215 if (m_disableParentOnly
)
218 m_parent
->Enable(FALSE
);
222 wxEnableTopLevelWindows(FALSE
);
226 Enable(TRUE
); // enable this window
229 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
230 wxWindow
**lastWindow
)
232 wxLayoutConstraints
*c
;
234 wxStaticText
*label
= new wxStaticText(this, -1, _T("unknown"));
235 c
= new wxLayoutConstraints
;
237 // VZ: I like the labels be centered - if the others don't mind, you may
238 // remove "#ifdef __WXMSW__" and use it for all ports
240 c
->left
.SameAs(this, wxCentreX
, LAYOUT_X_MARGIN
);
242 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
244 c
->top
.Below(*lastWindow
, LAYOUT_Y_MARGIN
);
247 label
->SetConstraints(c
);
249 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
250 c
= new wxLayoutConstraints
;
251 c
->right
.LeftOf(label
);
252 c
->top
.SameAs(label
, wxTop
, 0);
255 dummy
->SetConstraints(c
);
263 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
265 wxASSERT_MSG( value
== -1 || m_gauge
, _T("cannot update non existent dialog") );
266 wxASSERT_MSG( value
<= m_maximum
, _T("invalid progress value") );
270 m_gauge
->SetValue(value
+ 1);
272 if( !newmsg
.IsEmpty() )
273 m_msg
->SetLabel(newmsg
);
275 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
277 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
278 unsigned long estimated
= elapsed
* m_maximum
/ value
;
279 unsigned long remaining
= estimated
- elapsed
;
281 SetTimeLabel(elapsed
, m_elapsed
);
282 SetTimeLabel(estimated
, m_estimated
);
283 SetTimeLabel(remaining
, m_remaining
);
286 if ( (value
== m_maximum
) && !m_AutoHide
)
290 // tell the user what he should do...
291 m_btnAbort
->SetLabel(_("Close"));
296 // also provide the finishing message if the application didn't
297 m_msg
->SetLabel(_("Done."));
300 // so that we return TRUE below and that out [Cancel] handler knew what
310 // update the display
314 return m_state
!= Canceled
;
317 // ----------------------------------------------------------------------------
319 // ----------------------------------------------------------------------------
321 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
323 if ( m_state
== Finished
)
325 // this means that the count down is already finished and we're being
326 // shown as a modal dialog - so just let the default handler do the job
331 // request to cancel was received, the next time Update() is called we
337 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
339 if ( m_state
== Uncancelable
)
345 // ----------------------------------------------------------------------------
347 // ----------------------------------------------------------------------------
349 wxProgressDialog::~wxProgressDialog()
351 if ( m_disableParentOnly
)
354 m_parent
->Enable(TRUE
);
358 wxEnableTopLevelWindows(TRUE
);
362 // ----------------------------------------------------------------------------
364 // ----------------------------------------------------------------------------
366 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
371 unsigned long hours
= val
/ 3600;
372 unsigned long minutes
= (val
% 3600) / 60;
373 unsigned long seconds
= val
% 60;
374 s
.Printf(_T("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
376 if ( s
!= label
->GetLabel() )
381 #endif // wxUSE_PROGRESSDLG