Imported Upstream version 9.20
[platform/upstream/7zip.git] / CPP / 7zip / UI / FileManager / ProgressDialog2.h
1 // ProgressDialog2.h\r
2 \r
3 #ifndef __PROGRESS_DIALOG2_H\r
4 #define __PROGRESS_DIALOG2_H\r
5 \r
6 #include "Windows/Synchronization.h"\r
7 #include "Windows/Thread.h"\r
8 \r
9 #include "Windows/Control/Dialog.h"\r
10 #include "Windows/Control/ListView.h"\r
11 #include "Windows/Control/ProgressBar.h"\r
12 \r
13 class CProgressSync\r
14 {\r
15   bool _stopped;\r
16   bool _paused;\r
17   bool _bytesProgressMode;\r
18 \r
19   UInt64 _totalBytes;\r
20   UInt64 _curBytes;\r
21   UInt64 _totalFiles;\r
22   UInt64 _curFiles;\r
23   UInt64 _inSize;\r
24   UInt64 _outSize;\r
25   \r
26   UString _titleFileName;\r
27   UString _currentFileName;\r
28 \r
29 public:\r
30   UStringVector Messages;\r
31   UString ErrorMessage;\r
32   UString ErrorMessageTitle;\r
33   \r
34   UString OkMessage;\r
35   UString OkMessageTitle;\r
36 \r
37   NWindows::NSynchronization::CCriticalSection _cs;\r
38 \r
39   CProgressSync():\r
40       _stopped(false), _paused(false),\r
41       _totalBytes((UInt64)(Int64)-1), _curBytes(0),\r
42       _totalFiles((UInt64)(Int64)-1), _curFiles(0),\r
43       _inSize((UInt64)(Int64)-1),\r
44       _outSize((UInt64)(Int64)-1),\r
45       _bytesProgressMode(true)\r
46       {}\r
47 \r
48   bool GetStopped()\r
49   {\r
50     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
51     return _stopped;\r
52   }\r
53   void SetStopped(bool value)\r
54   {\r
55     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
56     _stopped = value;\r
57   }\r
58   bool GetPaused()\r
59   {\r
60     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
61     return _paused;\r
62   }\r
63   void SetPaused(bool value)\r
64   {\r
65     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
66     _paused = value;\r
67   }\r
68   void SetBytesProgressMode(bool bytesProgressMode)\r
69   {\r
70     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
71     _bytesProgressMode = bytesProgressMode;\r
72   }\r
73   void SetProgress(UInt64 total, UInt64 completed)\r
74   {\r
75     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
76     _totalBytes = total;\r
77     _curBytes = completed;\r
78   }\r
79   void SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize)\r
80   {\r
81     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
82     if (inSize)\r
83       _inSize = *inSize;\r
84     if (outSize)\r
85       _outSize = *outSize;\r
86   }\r
87   void SetPos(UInt64 completed)\r
88   {\r
89     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
90     _curBytes = completed;\r
91   }\r
92   void SetNumBytesTotal(UInt64 value)\r
93   {\r
94     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
95     _totalBytes = value;\r
96   }\r
97   void SetNumFilesTotal(UInt64 value)\r
98   {\r
99     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
100     _totalFiles = value;\r
101   }\r
102   void SetNumFilesCur(UInt64 value)\r
103   {\r
104     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
105     _curFiles = value;\r
106   }\r
107   HRESULT ProcessStopAndPause();\r
108   HRESULT SetPosAndCheckPaused(UInt64 completed);\r
109   void GetProgress(UInt64 &total, UInt64 &completed,\r
110     UInt64 &totalFiles, UInt64 &curFiles,\r
111     UInt64 &inSize, UInt64 &outSize,\r
112     bool &bytesProgressMode)\r
113   {\r
114     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
115     total = _totalBytes;\r
116     completed = _curBytes;\r
117     totalFiles = _totalFiles;\r
118     curFiles = _curFiles;\r
119     inSize = _inSize;\r
120     outSize = _outSize;\r
121     bytesProgressMode = _bytesProgressMode;\r
122   }\r
123   void SetTitleFileName(const UString &fileName)\r
124   {\r
125     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
126     _titleFileName = fileName;\r
127   }\r
128   void GetTitleFileName(UString &fileName)\r
129   {\r
130     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
131     fileName = _titleFileName;\r
132   }\r
133   void SetCurrentFileName(const UString &fileName)\r
134   {\r
135     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
136     _currentFileName = fileName;\r
137   }\r
138   void GetCurrentFileName(UString &fileName)\r
139   {\r
140     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
141     fileName = _currentFileName;\r
142   }\r
143 \r
144   void AddErrorMessage(LPCWSTR message)\r
145   {\r
146     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
147     Messages.Add(message);\r
148   }\r
149 \r
150   void SetErrorMessage(const UString &message)\r
151   {\r
152     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
153     ErrorMessage = message;\r
154   }\r
155 \r
156   void SetOkMessage(const UString &message)\r
157   {\r
158     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
159     OkMessage = message;\r
160   }\r
161 \r
162   void SetOkMessageTitle(const UString &title)\r
163   {\r
164     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
165     OkMessageTitle = title;\r
166   }\r
167 \r
168   void SetErrorMessageTitle(const UString &title)\r
169   {\r
170     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
171     ErrorMessageTitle = title;\r
172   }\r
173 \r
174   bool ThereIsMessage() const\r
175   {\r
176     return !Messages.IsEmpty() || !ErrorMessage.IsEmpty() || !OkMessage.IsEmpty();\r
177   }\r
178 };\r
179 \r
180 class CU64ToI32Converter\r
181 {\r
182   UInt64 _numShiftBits;\r
183 public:\r
184   void Init(UInt64 range)\r
185   {\r
186     // Windows CE doesn't like big number here.\r
187     for (_numShiftBits = 0; range > (1 << 15); _numShiftBits++)\r
188       range >>= 1;\r
189   }\r
190   int Count(UInt64 value) { return int(value >> _numShiftBits); }\r
191 };\r
192 \r
193 enum ESpeedMode\r
194 {\r
195   kSpeedBytes,\r
196   kSpeedKBytes,\r
197   kSpeedMBytes\r
198 };\r
199 \r
200 class CProgressDialog: public NWindows::NControl::CModalDialog\r
201 {\r
202   UString _prevFileName;\r
203   UString _prevTitleName;\r
204 private:\r
205   UString backgroundString;\r
206   UString backgroundedString;\r
207   UString foregroundString;\r
208   UString pauseString;\r
209   UString continueString;\r
210   UString pausedString;\r
211 \r
212   int buttonSizeX;\r
213   int buttonSizeY;\r
214 \r
215   UINT_PTR _timer;\r
216 \r
217   UString _title;\r
218   CU64ToI32Converter _converter;\r
219   UInt64 _previousPos;\r
220   UInt64 _range;\r
221   NWindows::NControl::CProgressBar m_ProgressBar;\r
222   NWindows::NControl::CListView _messageList;\r
223 \r
224   UInt32 _prevPercentValue;\r
225   UInt32 _prevTime;\r
226   UInt32 _elapsedTime;\r
227   UInt32 _prevElapsedSec;\r
228   UInt64 _prevRemainingSec;\r
229   ESpeedMode _prevMode;\r
230   UInt64 _prevSpeed;\r
231 \r
232   bool _foreground;\r
233 \r
234   int _numReduceSymbols;\r
235 \r
236   bool _wasCreated;\r
237   bool _needClose;\r
238 \r
239   UInt32 _numPostedMessages;\r
240   UInt32 _numAutoSizeMessages;\r
241 \r
242   bool _errorsWereDisplayed;\r
243 \r
244   bool _waitCloseByCancelButton;\r
245   bool _cancelWasPressed;\r
246   \r
247   bool _inCancelMessageBox;\r
248   bool _externalCloseMessageWasReceived;\r
249 \r
250   void UpdateStatInfo(bool showAll);\r
251   bool OnTimer(WPARAM timerID, LPARAM callback);\r
252   void SetRange(UInt64 range);\r
253   void SetPos(UInt64 pos);\r
254   virtual bool OnInit();\r
255   virtual bool OnSize(WPARAM wParam, int xSize, int ySize);\r
256   virtual void OnCancel();\r
257   virtual void OnOK();\r
258   NWindows::NSynchronization::CManualResetEvent _createDialogEvent;\r
259   NWindows::NSynchronization::CManualResetEvent _dialogCreatedEvent;\r
260   #ifndef _SFX\r
261   void AddToTitle(LPCWSTR string);\r
262   #endif\r
263 \r
264   void SetPauseText();\r
265   void SetPriorityText();\r
266   void OnPauseButton();\r
267   void OnPriorityButton();\r
268   bool OnButtonClicked(int buttonID, HWND buttonHWND);\r
269 \r
270   void SetTitleText();\r
271   void ShowSize(int id, UInt64 value);\r
272 \r
273   void UpdateMessagesDialog();\r
274 \r
275   void AddMessageDirect(LPCWSTR message);\r
276   void AddMessage(LPCWSTR message);\r
277 \r
278   bool OnExternalCloseMessage();\r
279   void EnableErrorsControls(bool enable);\r
280 \r
281   void ShowAfterMessages(HWND wndParent);\r
282 \r
283   void CheckNeedClose();\r
284 public:\r
285   CProgressSync Sync;\r
286   bool CompressingMode;\r
287   bool WaitMode;\r
288   bool ShowCompressionInfo;\r
289   bool MessagesDisplayed; // = true if user pressed OK on all messages or there are no messages.\r
290   int IconID;\r
291 \r
292   #ifndef _SFX\r
293   HWND MainWindow;\r
294   UString MainTitle;\r
295   UString MainAddTitle;\r
296   ~CProgressDialog();\r
297   #endif\r
298 \r
299   CProgressDialog();\r
300   void WaitCreating()\r
301   {\r
302     _createDialogEvent.Set();\r
303     _dialogCreatedEvent.Lock();\r
304   }\r
305 \r
306 \r
307   INT_PTR Create(const UString &title, NWindows::CThread &thread, HWND wndParent = 0);\r
308 \r
309 \r
310   virtual bool OnMessage(UINT message, WPARAM wParam, LPARAM lParam);\r
311 \r
312   void ProcessWasFinished();\r
313 };\r
314 \r
315 \r
316 class CProgressCloser\r
317 {\r
318   CProgressDialog *_p;\r
319 public:\r
320   CProgressCloser(CProgressDialog &p) : _p(&p) {}\r
321   ~CProgressCloser() { _p->ProcessWasFinished(); }\r
322 };\r
323 \r
324 class CProgressThreadVirt\r
325 {\r
326 protected:\r
327   UString ErrorMessage;\r
328   UString ErrorPath1;\r
329   UString ErrorPath2;\r
330   UString OkMessage;\r
331   UString OkMessageTitle;\r
332 \r
333   // error if any of HRESULT, ErrorMessage, ErrorPath\r
334   virtual HRESULT ProcessVirt() = 0;\r
335   void Process();\r
336 public:\r
337   HRESULT Result;\r
338   bool ThreadFinishedOK; // if there is no fatal exception\r
339   CProgressDialog ProgressDialog;\r
340 \r
341   static THREAD_FUNC_DECL MyThreadFunction(void *param)\r
342   {\r
343     CProgressThreadVirt *p = (CProgressThreadVirt *)param;\r
344     try\r
345     {\r
346       p->Process();\r
347       p->ThreadFinishedOK = true;\r
348     }\r
349     catch (...) { p->Result = E_FAIL; }\r
350     return 0;\r
351   }\r
352 \r
353   HRESULT Create(const UString &title, HWND parentWindow = 0);\r
354   CProgressThreadVirt(): Result(E_FAIL), ThreadFinishedOK(false) {}\r
355 };\r
356 \r
357 UString HResultToMessage(HRESULT errorCode);\r
358 \r
359 #endif\r