1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/other-licenses/7zstub/src/7zip/FileManager/Resource/ProgressDialog/ProgressDialog.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,175 @@ 1.4 +// ProgressDialog.cpp 1.5 + 1.6 +#include "StdAfx.h" 1.7 +#include "resource.h" 1.8 +#include "ProgressDialog.h" 1.9 +#include "Common/IntToString.h" 1.10 +#include "Common/IntToString.h" 1.11 + 1.12 +using namespace NWindows; 1.13 + 1.14 +static const UINT_PTR kTimerID = 3; 1.15 +static const UINT kTimerElapse = 50; 1.16 + 1.17 +#ifdef LANG 1.18 +#include "../../LangUtils.h" 1.19 +#endif 1.20 + 1.21 +#ifdef LANG 1.22 +static CIDLangPair kIDLangPairs[] = 1.23 +{ 1.24 + { IDCANCEL, 0x02000711 } 1.25 +}; 1.26 +#endif 1.27 + 1.28 +#ifndef _SFX 1.29 +CProgressDialog::~CProgressDialog() 1.30 +{ 1.31 + AddToTitle(TEXT("")); 1.32 +} 1.33 +void CProgressDialog::AddToTitle(LPCWSTR s) 1.34 +{ 1.35 + if (MainWindow != 0) 1.36 + ::MySetWindowText(MainWindow, UString(s) + MainTitle); 1.37 +} 1.38 +#endif 1.39 + 1.40 + 1.41 + 1.42 +bool CProgressDialog::OnInit() 1.43 +{ 1.44 + _range = UINT64(-1); 1.45 + _prevPercentValue = -1; 1.46 + 1.47 + #ifdef LANG 1.48 + // LangSetWindowText(HWND(*this), 0x02000C00); 1.49 + LangSetDlgItemsText(HWND(*this), kIDLangPairs, sizeof(kIDLangPairs) / sizeof(kIDLangPairs[0])); 1.50 + #endif 1.51 + 1.52 + m_ProgressBar.Attach(GetItem(IDC_PROGRESS1)); 1.53 + _timer = SetTimer(kTimerID, kTimerElapse); 1.54 + _dialogCreatedEvent.Set(); 1.55 + SetText(_title); 1.56 + return CModalDialog::OnInit(); 1.57 +} 1.58 + 1.59 +void CProgressDialog::OnCancel() 1.60 +{ 1.61 + ProgressSynch.SetStopped(true); 1.62 +} 1.63 + 1.64 +void CProgressDialog::SetRange(UINT64 range) 1.65 +{ 1.66 + _range = range; 1.67 + _peviousPos = (UInt64)(Int64)-1; 1.68 + _converter.Init(range); 1.69 + m_ProgressBar.SetRange32(0 , _converter.Count(range)); // Test it for 100% 1.70 +} 1.71 + 1.72 +void CProgressDialog::SetPos(UINT64 pos) 1.73 +{ 1.74 + bool redraw = true; 1.75 + if (pos < _range && pos > _peviousPos) 1.76 + { 1.77 + UINT64 posDelta = pos - _peviousPos; 1.78 + if (posDelta < (_range >> 10)) 1.79 + redraw = false; 1.80 + } 1.81 + if(redraw) 1.82 + { 1.83 + m_ProgressBar.SetPos(_converter.Count(pos)); // Test it for 100% 1.84 + _peviousPos = pos; 1.85 + } 1.86 +} 1.87 + 1.88 +bool CProgressDialog::OnTimer(WPARAM timerID, LPARAM callback) 1.89 +{ 1.90 + if (ProgressSynch.GetPaused()) 1.91 + return true; 1.92 + UINT64 total, completed; 1.93 + ProgressSynch.GetProgress(total, completed); 1.94 + if (total != _range) 1.95 + SetRange(total); 1.96 + SetPos(completed); 1.97 + 1.98 + if (total == 0) 1.99 + total = 1; 1.100 + 1.101 + int percentValue = (int)(completed * 100 / total); 1.102 + if (percentValue != _prevPercentValue) 1.103 + { 1.104 + wchar_t s[64]; 1.105 + ConvertUInt64ToString(percentValue, s); 1.106 + UString title = s; 1.107 + title += L"% "; 1.108 + SetText(title + _title); 1.109 + #ifndef _SFX 1.110 + AddToTitle(title + MainAddTitle); 1.111 + #endif 1.112 + _prevPercentValue = percentValue; 1.113 + } 1.114 + return true; 1.115 +} 1.116 + 1.117 + 1.118 +//////////////////// 1.119 +// CU64ToI32Converter 1.120 + 1.121 +static const UINT64 kMaxIntValue = 0x7FFFFFFF; 1.122 + 1.123 +void CU64ToI32Converter::Init(UINT64 range) 1.124 +{ 1.125 + _numShiftBits = 0; 1.126 + while(range > kMaxIntValue) 1.127 + { 1.128 + range >>= 1; 1.129 + _numShiftBits++; 1.130 + } 1.131 +} 1.132 + 1.133 +int CU64ToI32Converter::Count(UINT64 aValue) 1.134 +{ 1.135 + return int(aValue >> _numShiftBits); 1.136 +} 1.137 + 1.138 +const UINT CProgressDialog::kCloseMessage = WM_USER + 1; 1.139 + 1.140 +bool CProgressDialog::OnMessage(UINT message, WPARAM wParam, LPARAM lParam) 1.141 +{ 1.142 + switch(message) 1.143 + { 1.144 + case kCloseMessage: 1.145 + { 1.146 + KillTimer(_timer); 1.147 + _timer = 0; 1.148 + End(0); 1.149 + return true; 1.150 + } 1.151 + case WM_SETTEXT: 1.152 + { 1.153 + if (_timer == 0) 1.154 + return true; 1.155 + } 1.156 + } 1.157 + return CModalDialog::OnMessage(message, wParam, lParam); 1.158 +} 1.159 + 1.160 +bool CProgressDialog::OnButtonClicked(int buttonID, HWND buttonHWND) 1.161 +{ 1.162 + switch(buttonID) 1.163 + { 1.164 + case IDCANCEL: 1.165 + { 1.166 + bool paused = ProgressSynch.GetPaused();; 1.167 + ProgressSynch.SetPaused(true); 1.168 + int res = ::MessageBoxW(HWND(*this), 1.169 + L"Are you sure you want to cancel?", 1.170 + _title, MB_YESNO); 1.171 + ProgressSynch.SetPaused(paused); 1.172 + if (res == IDNO) 1.173 + return true; 1.174 + break; 1.175 + } 1.176 + } 1.177 + return CModalDialog::OnButtonClicked(buttonID, buttonHWND); 1.178 +}