]>
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
)
69 EVT_CLOSE(wxProgressDialog::OnClose
)
72 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 wxProgressDialog::wxProgressDialog(wxString
const &title
,
83 wxString
const &message
,
87 : wxDialog(parent
, -1, title
)
89 bool hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
90 m_state
= hasAbortButton
? Continue
: Uncancelable
;
91 m_disableParentOnly
= (style
& wxPD_APP_MODAL
) == 0;
92 m_AutoHide
= (style
& wxPD_AUTO_HIDE
) != 0;
96 wxLayoutConstraints
*c
;
99 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
101 #if defined(__VISAGECPP__)
102 // have two versions of this in wxWindowDC tp avoid function hiding
103 // since there are two of these in wxDCBase, and in turn in wxDC.
104 // VA cannot resolve this so:
105 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
107 dc
.GetTextExtent(message
, &widthText
, (long*)NULL
);
110 m_msg
= new wxStaticText(this, -1, message
);
111 c
= new wxLayoutConstraints
;
112 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
113 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
116 m_msg
->SetConstraints(c
);
118 wxSize sizeDlg
, sizeLabel
= m_msg
->GetSize();
119 sizeDlg
.y
= 2*LAYOUT_Y_MARGIN
+ sizeLabel
.y
;
121 wxWindow
*lastWindow
= m_msg
;
125 m_gauge
= new wxGauge(this, -1, maximum
,
126 wxDefaultPosition
, wxDefaultSize
,
127 wxGA_HORIZONTAL
| wxRAISED_BORDER
);
128 c
= new wxLayoutConstraints
;
129 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
130 c
->top
.Below(m_msg
, 2*LAYOUT_Y_MARGIN
);
131 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
133 m_gauge
->SetConstraints(c
);
134 m_gauge
->SetValue(0);
135 lastWindow
= m_gauge
;
137 wxSize sizeGauge
= m_gauge
->GetSize();
138 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeGauge
.y
;
141 m_gauge
= (wxGauge
*)NULL
;
143 // create the estimated/remaining/total time zones if requested
144 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
147 if ( style
& wxPD_ELAPSED_TIME
)
151 m_elapsed
= CreateLabel(wxT("Elapsed time : "), &lastWindow
);
154 if ( style
& wxPD_ESTIMATED_TIME
)
158 m_estimated
= CreateLabel(wxT("Estimated time : "), &lastWindow
);
161 if ( style
& wxPD_REMAINING_TIME
)
165 m_remaining
= CreateLabel(wxT("Remaining time : "), &lastWindow
);
168 if ( nTimeLabels
> 0 )
170 // set it to the current time
171 m_timeStart
= wxGetCurrentTime();
173 sizeDlg
.y
+= nTimeLabels
* (sizeLabel
.y
+ LAYOUT_Y_MARGIN
);
176 if ( hasAbortButton
)
178 m_btnAbort
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
179 c
= new wxLayoutConstraints
;
181 // Windows dialogs usually have buttons in the lower right corner
183 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
185 c
->centreX
.SameAs(this, wxCentreX
);
187 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_Y_MARGIN
);
189 wxSize sizeBtn
= wxButton::GetDefaultSize();
190 c
->width
.Absolute(sizeBtn
.x
);
191 c
->height
.Absolute(sizeBtn
.y
);
193 m_btnAbort
->SetConstraints(c
);
195 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeBtn
.y
;
198 m_btnAbort
= (wxButton
*)NULL
;
203 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
;
205 // try to make the dialog not square but rectangular of reasonabel width
206 sizeDlg
.x
= wxMax(widthText
, 4*sizeDlg
.y
/3);
209 SetClientSize(sizeDlg
);
211 Centre(wxCENTER_FRAME
| wxBOTH
);
213 if (m_disableParentOnly
)
216 m_parent
->Enable(FALSE
);
220 wxEnableTopLevelWindows(FALSE
);
224 Enable(TRUE
); // enable this window
226 // Update the display (especially on X, GTK)
229 MacUpdateImmediately() ;
233 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
234 wxWindow
**lastWindow
)
236 wxLayoutConstraints
*c
;
238 wxStaticText
*label
= new wxStaticText(this, -1, wxT("unknown"));
239 c
= new wxLayoutConstraints
;
241 // VZ: I like the labels be centered - if the others don't mind, you may
242 // remove "#ifdef __WXMSW__" and use it for all ports
244 c
->left
.SameAs(this, wxCentreX
, LAYOUT_X_MARGIN
);
246 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
248 c
->top
.Below(*lastWindow
, LAYOUT_Y_MARGIN
);
251 label
->SetConstraints(c
);
253 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
254 c
= new wxLayoutConstraints
;
255 c
->right
.LeftOf(label
);
256 c
->top
.SameAs(label
, wxTop
, 0);
259 dummy
->SetConstraints(c
);
267 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
269 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
270 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
274 m_gauge
->SetValue(value
+ 1);
276 if( !newmsg
.IsEmpty() )
277 m_msg
->SetLabel(newmsg
);
279 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
281 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
282 unsigned long estimated
= elapsed
* m_maximum
/ value
;
283 unsigned long remaining
= estimated
- elapsed
;
285 SetTimeLabel(elapsed
, m_elapsed
);
286 SetTimeLabel(estimated
, m_estimated
);
287 SetTimeLabel(remaining
, m_remaining
);
290 if ( (value
== m_maximum
) && !m_AutoHide
)
294 // tell the user what he should do...
295 m_btnAbort
->SetLabel(_("Close"));
300 // also provide the finishing message if the application didn't
301 m_msg
->SetLabel(_("Done."));
304 // so that we return TRUE below and that out [Cancel] handler knew what
314 // update the display
318 MacUpdateImmediately() ;
321 return m_state
!= Canceled
;
324 // ----------------------------------------------------------------------------
326 // ----------------------------------------------------------------------------
328 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
330 if ( m_state
== Finished
)
332 // this means that the count down is already finished and we're being
333 // shown as a modal dialog - so just let the default handler do the job
338 // request to cancel was received, the next time Update() is called we
344 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
346 if ( m_state
== Uncancelable
)
352 // ----------------------------------------------------------------------------
354 // ----------------------------------------------------------------------------
356 wxProgressDialog::~wxProgressDialog()
358 if ( m_disableParentOnly
)
361 m_parent
->Enable(TRUE
);
365 wxEnableTopLevelWindows(TRUE
);
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
378 unsigned long hours
= val
/ 3600;
379 unsigned long minutes
= (val
% 3600) / 60;
380 unsigned long seconds
= val
% 60;
381 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
383 if ( s
!= label
->GetLabel() )
388 #endif // wxUSE_PROGRESSDLG