Imported Upstream version 9.20
[platform/upstream/7zip.git] / CPP / 7zip / UI / FileManager / ProgressDialog.h
1 // ProgressDialog.h\r
2 \r
3 #ifndef __PROGRESS_DIALOG_H\r
4 #define __PROGRESS_DIALOG_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/ProgressBar.h"\r
11 \r
12 #include "ProgressDialogRes.h"\r
13 \r
14 class CProgressSync\r
15 {\r
16   NWindows::NSynchronization::CCriticalSection _cs;\r
17   bool _stopped;\r
18   bool _paused;\r
19   UInt64 _total;\r
20   UInt64 _completed;\r
21 public:\r
22   CProgressSync(): _stopped(false), _paused(false), _total(1), _completed(0) {}\r
23 \r
24   HRESULT ProcessStopAndPause();\r
25   bool GetStopped()\r
26   {\r
27     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
28     return _stopped;\r
29   }\r
30   void SetStopped(bool value)\r
31   {\r
32     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
33     _stopped = value;\r
34   }\r
35   bool GetPaused()\r
36   {\r
37     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
38     return _paused;\r
39   }\r
40   void SetPaused(bool value)\r
41   {\r
42     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
43     _paused = value;\r
44   }\r
45   void SetProgress(UInt64 total, UInt64 completed)\r
46   {\r
47     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
48     _total = total;\r
49     _completed = completed;\r
50   }\r
51   void SetPos(UInt64 completed)\r
52   {\r
53     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
54     _completed = completed;\r
55   }\r
56   void GetProgress(UInt64 &total, UInt64 &completed)\r
57   {\r
58     NWindows::NSynchronization::CCriticalSectionLock lock(_cs);\r
59     total = _total;\r
60     completed = _completed;\r
61   }\r
62 };\r
63 \r
64 class CU64ToI32Converter\r
65 {\r
66   UInt64 _numShiftBits;\r
67 public:\r
68   void Init(UInt64 range)\r
69   {\r
70     // Windows CE doesn't like big number here.\r
71     for (_numShiftBits = 0; range > (1 << 15); _numShiftBits++)\r
72       range >>= 1;\r
73   }\r
74   int Count(UInt64 value) { return int(value >> _numShiftBits); }\r
75 };\r
76 \r
77 class CProgressDialog: public NWindows::NControl::CModalDialog\r
78 {\r
79 private:\r
80   UINT_PTR _timer;\r
81 \r
82   UString _title;\r
83   CU64ToI32Converter _converter;\r
84   UInt64 _peviousPos;\r
85   UInt64 _range;\r
86   NWindows::NControl::CProgressBar m_ProgressBar;\r
87 \r
88   int _prevPercentValue;\r
89 \r
90   bool _wasCreated;\r
91   bool _needClose;\r
92   bool _inCancelMessageBox;\r
93   bool _externalCloseMessageWasReceived;\r
94 \r
95   bool OnTimer(WPARAM timerID, LPARAM callback);\r
96   void SetRange(UInt64 range);\r
97   void SetPos(UInt64 pos);\r
98   virtual bool OnInit();\r
99   virtual void OnCancel();\r
100   virtual void OnOK();\r
101   NWindows::NSynchronization::CManualResetEvent _dialogCreatedEvent;\r
102   #ifndef _SFX\r
103   void AddToTitle(LPCWSTR string);\r
104   #endif\r
105   bool OnButtonClicked(int buttonID, HWND buttonHWND);\r
106 \r
107   void WaitCreating() { _dialogCreatedEvent.Lock(); }\r
108   void CheckNeedClose();\r
109   bool OnExternalCloseMessage();\r
110 public:\r
111   CProgressSync Sync;\r
112   int IconID;\r
113 \r
114   #ifndef _SFX\r
115   HWND MainWindow;\r
116   UString MainTitle;\r
117   UString MainAddTitle;\r
118   ~CProgressDialog();\r
119   #endif\r
120 \r
121   CProgressDialog(): _timer(0)\r
122     #ifndef _SFX\r
123     ,MainWindow(0)\r
124     #endif\r
125   {\r
126     IconID = -1;\r
127     _wasCreated = false;\r
128     _needClose = false;\r
129     _inCancelMessageBox = false;\r
130     _externalCloseMessageWasReceived = false;\r
131 \r
132     if (_dialogCreatedEvent.Create() != S_OK)\r
133       throw 1334987;\r
134   }\r
135 \r
136   INT_PTR Create(const UString &title, NWindows::CThread &thread, HWND wndParent = 0)\r
137   {\r
138     _title = title;\r
139     INT_PTR res = CModalDialog::Create(IDD_DIALOG_PROGRESS, wndParent);\r
140     thread.Wait();\r
141     return res;\r
142   }\r
143 \r
144   enum\r
145   {\r
146     kCloseMessage = WM_USER + 1\r
147   };\r
148 \r
149   virtual bool OnMessage(UINT message, WPARAM wParam, LPARAM lParam);\r
150 \r
151   void ProcessWasFinished()\r
152   {\r
153     WaitCreating();\r
154     if (_wasCreated)\r
155       PostMessage(kCloseMessage);\r
156     else\r
157       _needClose = true;\r
158   };\r
159 };\r
160 \r
161 \r
162 class CProgressCloser\r
163 {\r
164   CProgressDialog *_p;\r
165 public:\r
166   CProgressCloser(CProgressDialog &p) : _p(&p) {}\r
167   ~CProgressCloser() { _p->ProcessWasFinished(); }\r
168 };\r
169 \r
170 #endif\r