]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
0f9d5d899f96de27d27567e9eb1dde2956184647
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"
44 # include "wx/timer.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(m_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;
97 wxLayoutConstraints
*c
;
100 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
102 dc
.GetTextExtent(message
, &widthText
, NULL
);
104 m_msg
= new wxStaticText(this, -1, message
);
105 c
= new wxLayoutConstraints
;
106 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
107 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
110 m_msg
->SetConstraints(c
);
112 wxSize sizeDlg
, sizeLabel
= m_msg
->GetSize();
113 sizeDlg
.y
= 2*LAYOUT_Y_MARGIN
+ sizeLabel
.y
;
115 wxWindow
*lastWindow
= m_msg
;
119 m_gauge
= new wxGauge(this, -1, maximum
,
120 wxDefaultPosition
, wxDefaultSize
,
121 wxGA_HORIZONTAL
| wxRAISED_BORDER
);
122 c
= new wxLayoutConstraints
;
123 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
124 c
->top
.Below(m_msg
, 2*LAYOUT_Y_MARGIN
);
125 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
127 m_gauge
->SetConstraints(c
);
128 m_gauge
->SetValue(0);
129 lastWindow
= m_gauge
;
131 wxSize sizeGauge
= m_gauge
->GetSize();
132 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeGauge
.y
;
135 m_gauge
= (wxGauge
*)NULL
;
137 // create the estimated/remaining/total time zones if requested
138 m_elapsed
= m_estimated
= m_remaining
= NULL
;
141 if ( style
& wxPD_ELAPSED_TIME
)
145 m_elapsed
= CreateLabel(_T("Elapsed time : "), &lastWindow
);
148 if ( style
& wxPD_ESTIMATED_TIME
)
152 m_estimated
= CreateLabel(_T("Estimated time : "), &lastWindow
);
155 if ( style
& wxPD_REMAINING_TIME
)
159 m_remaining
= CreateLabel(_T("Remaining time : "), &lastWindow
);
162 if ( nTimeLabels
> 0 )
164 // set it to the current time
165 m_timeStart
= wxGetCurrentTime();
167 sizeDlg
.y
+= nTimeLabels
* (sizeLabel
.y
+ LAYOUT_Y_MARGIN
);
170 if ( hasAbortButton
)
172 m_btnAbort
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
173 c
= new wxLayoutConstraints
;
175 // Windows dialogs usually have buttons in the lower right corner
177 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
179 c
->centreX
.SameAs(this, wxCentreX
);
181 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_Y_MARGIN
);
183 wxSize sizeBtn
= wxButton::GetDefaultSize();
184 c
->width
.Absolute(sizeBtn
.x
);
185 c
->height
.Absolute(sizeBtn
.y
);
187 m_btnAbort
->SetConstraints(c
);
189 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeBtn
.y
;
192 m_btnAbort
= (wxButton
*)NULL
;
197 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
;
199 // try to make the dialog not square but rectangular of reasonabel width
200 sizeDlg
.x
= wxMax(widthText
, 4*sizeDlg
.y
/3);
203 SetClientSize(sizeDlg
);
205 Centre(wxCENTER_FRAME
| wxBOTH
);
207 if (m_disableParentOnly
)
210 m_parent
->Enable(FALSE
);
214 wxEnableTopLevelWindows(FALSE
);
218 Enable(TRUE
); // enable this window
221 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
222 wxWindow
**lastWindow
)
224 wxLayoutConstraints
*c
;
226 wxStaticText
*label
= new wxStaticText(this, -1, _T("unknown"));
227 c
= new wxLayoutConstraints
;
229 // VZ: I like the labels be centered - if the others don't mind, you may
230 // remove "#ifdef __WXMSW__" and use it for all ports
232 c
->left
.SameAs(this, wxCentreX
, LAYOUT_X_MARGIN
);
234 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
236 c
->top
.Below(*lastWindow
, LAYOUT_Y_MARGIN
);
239 label
->SetConstraints(c
);
241 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
242 c
= new wxLayoutConstraints
;
243 c
->right
.LeftOf(label
);
244 c
->top
.SameAs(label
, wxTop
, 0);
247 dummy
->SetConstraints(c
);
255 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
257 wxASSERT_MSG( value
== -1 || m_gauge
, _T("cannot update non existent dialog") );
258 wxASSERT_MSG( value
<= m_maximum
, _T("invalid progress value") );
262 m_gauge
->SetValue(value
+ 1);
264 if( !newmsg
.IsEmpty() )
265 m_msg
->SetLabel(newmsg
);
267 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
269 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
270 unsigned long estimated
= elapsed
* m_maximum
/ value
;
271 unsigned long remaining
= estimated
- elapsed
;
273 SetTimeLabel(elapsed
, m_elapsed
);
274 SetTimeLabel(estimated
, m_estimated
);
275 SetTimeLabel(remaining
, m_remaining
);
278 if ( (value
== m_maximum
- 1) && !(GetWindowStyleFlag() & wxPD_AUTO_HIDE
) )
282 // tell the user what he should do...
283 m_btnAbort
->SetLabel(_("Close"));
288 // also provide the finishing message if the application didn't
289 m_msg
->SetLabel(_("Done."));
292 // so that we return TRUE below and that out [Cancel] handler knew what
302 // update the display
306 return m_state
!= Canceled
;
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
315 if ( m_state
== Finished
)
317 // this means that the count down is already finished and we're being
318 // shown as a modal dialog - so just let the default handler do the job
323 // request to cancel was received, the next time Update() is called we
329 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
331 if ( m_state
== Uncancelable
)
337 // ----------------------------------------------------------------------------
339 // ----------------------------------------------------------------------------
341 wxProgressDialog::~wxProgressDialog()
343 if ( m_disableParentOnly
)
346 m_parent
->Enable(TRUE
);
350 wxEnableTopLevelWindows(TRUE
);
354 // ----------------------------------------------------------------------------
356 // ----------------------------------------------------------------------------
358 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
363 unsigned long hours
= val
/ 3600;
364 unsigned long minutes
= (val
% 3600) / 60;
365 unsigned long seconds
= val
% 60;
366 s
.Printf(_T("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
368 if ( s
!= label
->GetLabel() )
373 #endif // wxUSE_PROGRESSDLG