]> git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
dfd8873c600b22a3ca2dd99468692c4e8ba1e083
[wxWidgets.git] / src / generic / progdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: progdlgg.h
3 // Purpose: wxProgressDialog class
4 // Author: Karsten Ballüder
5 // Modified by:
6 // Created: 09.05.1999
7 // RCS-ID: $Id$
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "progdlgg.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/utils.h"
25 #include "wx/frame.h"
26 #include "wx/button.h"
27 #include "wx/stattext.h"
28 #include "wx/layout.h"
29 #include "wx/event.h"
30 #include "wx/gauge.h"
31 #include "wx/intl.h"
32 #include "wx/settings.h"
33 #endif
34
35 #if wxUSE_PROGRESSDLG
36
37 #include "wx/generic/progdlgg.h"
38
39 #define LAYOUT_X_MARGIN 8
40 #define LAYOUT_Y_MARGIN 8
41
42 // wxTextEntryDialog
43
44 #if !USE_SHARED_LIBRARY
45 BEGIN_EVENT_TABLE(wxProgressDialog, wxFrame)
46 EVT_BUTTON(-1, wxProgressDialog::OnCancel)
47 EVT_CLOSE(wxProgressDialog::OnClose)
48 END_EVENT_TABLE()
49
50 IMPLEMENT_CLASS(wxProgressDialog, wxFrame)
51 #endif
52
53 wxProgressDialog::wxProgressDialog(wxString const &title,
54 wxString const &message,
55 int maximum,
56 wxWindow *parent,
57 int style)
58 {
59 bool hasAbortButton = (style & wxPD_CAN_ABORT) != 0;
60 m_state = hasAbortButton ? Continue : Uncancelable;
61 m_disableParentOnly = (style & wxPD_APP_MODAL) == 0;
62 m_parent = parent;
63 m_maximum = maximum;
64
65 wxFrame::Create(m_parent, -1, title, wxDefaultPosition,
66 wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
67 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
68
69 wxLayoutConstraints *c;
70
71 wxClientDC dc(this);
72 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
73 long widthText;
74 dc.GetTextExtent(message, &widthText, NULL);
75
76 m_msg = new wxStaticText(this, -1, message);
77 c = new wxLayoutConstraints;
78 c->left.SameAs(this, wxLeft, 10);
79 c->top.SameAs(this, wxTop, 10);
80 c->width.AsIs();
81 c->height.AsIs();
82 m_msg->SetConstraints(c);
83
84 if ( maximum > 0 )
85 {
86 m_gauge = new wxGauge(this, -1, maximum,
87 wxDefaultPosition, wxDefaultSize,
88 wxGA_HORIZONTAL | wxRAISED_BORDER);
89 c = new wxLayoutConstraints;
90 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
91 c->top.Below(m_msg, 2*LAYOUT_Y_MARGIN);
92 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
93 c->height.AsIs();
94 m_gauge->SetConstraints(c);
95 m_gauge->SetValue(0);
96 }
97 else
98 m_gauge = (wxGauge *)NULL;
99
100 if ( hasAbortButton )
101 {
102 m_btnAbort = new wxButton(this, -1, _("Cancel"));
103 c = new wxLayoutConstraints;
104 c->centreX.SameAs(this, wxCentreX);
105 if(m_gauge)
106 c->top.Below(m_gauge, 2*LAYOUT_Y_MARGIN);
107 else
108 c->top.Below(m_btnAbort, 2*LAYOUT_Y_MARGIN);
109 c->width.AsIs();
110 c->height.AsIs();
111 m_btnAbort->SetConstraints(c);
112 }
113 else
114 m_btnAbort = (wxButton *)NULL;
115
116 SetAutoLayout(TRUE);
117 Layout();
118
119 // calc the height of the dialog
120 Fit();
121 // and set the width from it - unfortunately, Fit() makes the dialog way too
122 // wide under Windows, so try to find a reasonable value for the width, not
123 // too big and not too small
124 wxSize size = GetClientSize();
125 size.x = 2*widthText;
126 if ( size.x < 2*size.y )
127 SetClientSize(2*size.y, size.y);
128
129 Show(TRUE);
130 Centre(wxCENTER_FRAME | wxBOTH);
131
132 if(m_disableParentOnly)
133 m_parent->Enable(FALSE);
134 else
135 wxEnableTopLevelWindows(FALSE);
136
137 Enable(TRUE); // enable this window
138 wxYield();
139 }
140
141
142 bool
143 wxProgressDialog::Update(int value, const wxString& newmsg)
144 {
145 wxASSERT_MSG( value == -1 || m_gauge, _T("can't update non existent dialog") );
146 wxASSERT_MSG( value < m_maximum, _T("invalid progress value") );
147
148
149 if( m_gauge )
150 m_gauge->SetValue(value + 1);
151
152 if( !newmsg.IsEmpty() )
153 m_msg->SetLabel(newmsg);
154
155 if ( (value == m_maximum - 1) && !(GetWindowStyleFlag() & wxPD_AUTO_HIDE) )
156 {
157 if ( m_btnAbort )
158 {
159 // tell the user what he should do...
160 m_btnAbort->SetLabel(_("Close"));
161 }
162
163 if ( !newmsg )
164 {
165 // also provide the finishing message if the application didn't
166 m_msg->SetLabel(_("Done."));
167 }
168
169 m_state = Finished;
170
171 // so that we return TRUE below
172 m_state = Finished;
173 }
174
175 return m_state != Canceled;
176 }
177
178 void wxProgressDialog::OnClose(wxCloseEvent& event)
179 {
180 if ( m_state == Uncancelable )
181 event.Veto(TRUE);
182 else
183 m_state = Canceled;
184 }
185
186
187 wxProgressDialog::~wxProgressDialog()
188 {
189 if ( m_disableParentOnly )
190 m_parent->Enable(TRUE);
191 else
192 wxEnableTopLevelWindows(TRUE);
193 }
194
195 #endif