4.44 beta

This commit is contained in:
Igor Pavlov
2007-01-20 00:00:00 +00:00
committed by Kornel Lesiński
parent 804edc5756
commit d9666cf046
1331 changed files with 10535 additions and 13791 deletions

View File

@@ -0,0 +1,432 @@
// ProgressDialog.cpp
#include "StdAfx.h"
#include "resource.h"
#include "ProgressDialog.h"
#include "Common/IntToString.h"
#include "Common/IntToString.h"
using namespace NWindows;
static const UINT_PTR kTimerID = 3;
static const UINT kTimerElapse = 50;
#ifdef LANG
#include "../../LangUtils.h"
#endif
#ifdef LANG
static CIDLangPair kIDLangPairs[] =
{
{ IDCANCEL, 0x02000C00 },
{ IDC_PROGRESS_ELAPSED, 0x02000C01 },
{ IDC_PROGRESS_REMAINING, 0x02000C02 },
{ IDC_PROGRESS_TOTAL, 0x02000C03 },
{ IDC_PROGRESS_SPEED, 0x02000C04 },
{ IDC_BUTTON_PROGRESS_PRIORITY, 0x02000C10 },
{ IDC_BUTTON_PAUSE, 0x02000C12 },
{ IDCANCEL, 0x02000711 },
};
#endif
HRESULT CProgressSynch::SetPosAndCheckPaused(UInt64 completed)
{
for (;;)
{
if(GetStopped())
return E_ABORT;
if(!GetPaused())
break;
::Sleep(100);
}
SetPos(completed);
return S_OK;
}
#ifndef _SFX
CProgressDialog::~CProgressDialog()
{
AddToTitle(L"");
}
void CProgressDialog::AddToTitle(LPCWSTR s)
{
if (MainWindow != 0)
{
CWindow window(MainWindow);
window.SetText(s + UString(MainTitle));
}
}
static const int kTitleFileNameSizeLimit = 36;
static const int kCurrentFileNameSizeLimit = 68;
static void ReduceString(UString &s, int size)
{
if (s.Length() > size)
s = s.Left(size / 2) + UString(L" ... ") + s.Right(size / 2);
}
#endif
bool CProgressDialog::OnInit()
{
_range = (UInt64)(Int64)(-1);
_prevPercentValue = UInt32(-1);
_prevElapsedSec = UInt32(-1);
_prevRemainingSec = UInt32(-1);
_prevSpeed = UInt32(-1);
_prevMode = kSpeedBytes;
_prevTime = ::GetTickCount();
_elapsedTime = 0;
_foreground = true;
#ifdef LANG
// LangSetWindowText(HWND(*this), 0x02000C00);
LangSetDlgItemsText(HWND(*this), kIDLangPairs, sizeof(kIDLangPairs) / sizeof(kIDLangPairs[0]));
#endif
CWindow window(GetItem(IDC_BUTTON_PROGRESS_PRIORITY));
window.GetText(backgroundString);
backgroundedString = backgroundString;
backgroundedString.Replace(L"&", L"");
window = GetItem(IDC_BUTTON_PAUSE);
window.GetText(pauseString);
foregroundString = LangString(IDS_PROGRESS_FOREGROUND, 0x02000C11);
continueString = LangString(IDS_PROGRESS_CONTINUE, 0x02000C13);
pausedString = LangString(IDS_PROGRESS_PAUSED, 0x02000C20);
m_ProgressBar.Attach(GetItem(IDC_PROGRESS1));
_timer = SetTimer(kTimerID, kTimerElapse);
_dialogCreatedEvent.Set();
SetText(_title);
SetPauseText();
SetPriorityText();
return CModalDialog::OnInit();
}
void CProgressDialog::OnCancel()
{
ProgressSynch.SetStopped(true);
}
static void ConvertSizeToString(UInt64 value, wchar_t *s)
{
const wchar_t *kModif = L" KMGTP";
for (int i = 0; ; i++)
if (i == 5 || value < (UInt64(10000) << (i * 10)))
{
ConvertUInt64ToString(value >> (i * 10), s);
s += wcslen(s);
*s++ = ' ';
if (i != 0)
*s++ = kModif[i];
*s++ = L'B';
*s++ = L'\0';
return;
}
}
void CProgressDialog::SetRange(UInt64 range)
{
_range = range;
_previousPos = (UInt64)(Int64)-1;
_converter.Init(range);
m_ProgressBar.SetRange32(0 , _converter.Count(range)); // Test it for 100%
wchar_t s[32];
ConvertSizeToString(_range, s);
SetItemText(IDC_PROGRESS_SPEED_TOTAL_VALUE, s);
}
void CProgressDialog::SetPos(UInt64 pos)
{
bool redraw = true;
if (pos < _range && pos > _previousPos)
{
if (pos - _previousPos < (_range >> 10))
redraw = false;
}
if(redraw)
{
m_ProgressBar.SetPos(_converter.Count(pos)); // Test it for 100%
_previousPos = pos;
}
}
static void GetTimeString(UInt64 timeValue, TCHAR *s)
{
wsprintf(s, TEXT("%02d:%02d:%02d"),
UInt32(timeValue / 3600),
UInt32((timeValue / 60) % 60),
UInt32(timeValue % 60));
}
bool CProgressDialog::OnTimer(WPARAM /* timerID */, LPARAM /* callback */)
{
if (ProgressSynch.GetPaused())
return true;
UInt64 total, completed;
ProgressSynch.GetProgress(total, completed);
UInt32 curTime = ::GetTickCount();
if (total != _range)
SetRange(total);
if (total == (UInt64)(Int64)-1)
{
SetPos(0);
SetRange(completed);
}
else
SetPos(completed);
_elapsedTime += (curTime - _prevTime);
_prevTime = curTime;
UInt32 elapsedSec = _elapsedTime / 1000;
bool elapsedChanged = false;
if (elapsedSec != _prevElapsedSec)
{
TCHAR s[40];
GetTimeString(elapsedSec, s);
SetItemText(IDC_PROGRESS_ELAPSED_VALUE, s);
_prevElapsedSec = elapsedSec;
elapsedChanged = true;
}
if (completed != 0 && elapsedChanged)
{
if (total == (UInt64)(Int64)-1)
{
SetItemText(IDC_PROGRESS_REMAINING_VALUE, L"");
}
else
{
UInt64 remainingTime = 0;
if (completed < total)
remainingTime = _elapsedTime * (total - completed) / completed;
UInt64 remainingSec = remainingTime / 1000;
if (remainingSec != _prevRemainingSec)
{
TCHAR s[40];
GetTimeString(remainingSec, s);
SetItemText(IDC_PROGRESS_REMAINING_VALUE, s);
_prevRemainingSec = remainingSec;
}
}
// if (elapsedChanged)
{
UInt64 speedB = (completed * 1000) / _elapsedTime;
UInt64 speedKB = speedB / 1024;
UInt64 speedMB = speedKB / 1024;
const UInt32 kLimit1 = 10;
TCHAR s[40];
bool needRedraw = false;
if (speedMB >= kLimit1)
{
if (_prevMode != kSpeedMBytes || speedMB != _prevSpeed)
{
ConvertUInt64ToString(speedMB, s);
lstrcat(s, TEXT(" MB/s"));
_prevMode = kSpeedMBytes;
_prevSpeed = speedMB;
needRedraw = true;
}
}
else if (speedKB >= kLimit1)
{
if (_prevMode != kSpeedKBytes || speedKB != _prevSpeed)
{
ConvertUInt64ToString(speedKB, s);
lstrcat(s, TEXT(" KB/s"));
_prevMode = kSpeedKBytes;
_prevSpeed = speedKB;
needRedraw = true;
}
}
else
{
if (_prevMode != kSpeedBytes || speedB != _prevSpeed)
{
ConvertUInt64ToString(speedB, s);
lstrcat(s, TEXT(" B/s"));
_prevMode = kSpeedBytes;
_prevSpeed = speedB;
needRedraw = true;
}
}
if (needRedraw)
SetItemText(IDC_PROGRESS_SPEED_VALUE, s);
}
}
if (total == 0)
total = 1;
UInt32 percentValue = (UInt32)(completed * 100 / total);
UString titleName;
ProgressSynch.GetTitleFileName(titleName);
if (percentValue != _prevPercentValue || _prevTitleName != titleName)
{
_prevPercentValue = percentValue;
SetTitleText();
_prevTitleName = titleName;
}
UString fileName;
ProgressSynch.GetCurrentFileName(fileName);
if (_prevFileName != fileName)
{
ReduceString(fileName, kCurrentFileNameSizeLimit);
SetItemText(IDC_PROGRESS_FILE_NAME, fileName);
_prevFileName == fileName;
}
return true;
}
////////////////////
// CU64ToI32Converter
static const UInt64 kMaxIntValue = 0x7FFFFFFF;
void CU64ToI32Converter::Init(UInt64 range)
{
_numShiftBits = 0;
while(range > kMaxIntValue)
{
range >>= 1;
_numShiftBits++;
}
}
int CU64ToI32Converter::Count(UInt64 aValue)
{
return int(aValue >> _numShiftBits);
}
const UINT CProgressDialog::kCloseMessage = WM_USER + 1;
bool CProgressDialog::OnMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case kCloseMessage:
{
KillTimer(_timer);
_timer = 0;
End(0);
return true;
}
case WM_SETTEXT:
{
if (_timer == 0)
return true;
}
}
return CModalDialog::OnMessage(message, wParam, lParam);
}
void CProgressDialog::SetTitleText()
{
UString title;
if (ProgressSynch.GetPaused())
{
title = pausedString;
title += L" ";
}
if (_prevPercentValue != UInt32(-1))
{
wchar_t s[64];
ConvertUInt64ToString(_prevPercentValue, s);
title += s;
title += L"%";
}
if (!_foreground)
{
title += L" ";
title += backgroundedString;
}
title += L" ";
UString totalTitle = title + _title;
UString fileName;
ProgressSynch.GetTitleFileName(fileName);
if (!fileName.IsEmpty())
{
ReduceString(fileName, kTitleFileNameSizeLimit);
totalTitle += L" ";
totalTitle += fileName;
}
SetText(totalTitle);
#ifndef _SFX
AddToTitle(title + MainAddTitle);
#endif
}
void CProgressDialog::SetPauseText()
{
SetItemText(IDC_BUTTON_PAUSE, ProgressSynch.GetPaused() ?
continueString : pauseString);
SetTitleText();
}
void CProgressDialog::OnPauseButton()
{
bool paused = !ProgressSynch.GetPaused();
ProgressSynch.SetPaused(paused);
UInt32 curTime = ::GetTickCount();
if (paused)
_elapsedTime += (curTime - _prevTime);
_prevTime = curTime;
SetPauseText();
}
void CProgressDialog::SetPriorityText()
{
SetItemText(IDC_BUTTON_PROGRESS_PRIORITY, _foreground ?
backgroundString :
foregroundString);
SetTitleText();
}
void CProgressDialog::OnPriorityButton()
{
_foreground = !_foreground;
SetPriorityClass(GetCurrentProcess(), _foreground ?
NORMAL_PRIORITY_CLASS: IDLE_PRIORITY_CLASS);
SetPriorityText();
}
bool CProgressDialog::OnButtonClicked(int buttonID, HWND buttonHWND)
{
switch(buttonID)
{
case IDCANCEL:
{
bool paused = ProgressSynch.GetPaused();;
// ProgressSynch.SetPaused(true);
if (!paused)
OnPauseButton();
int res = ::MessageBoxW(HWND(*this),
LangString(IDS_PROGRESS_ASK_CANCEL, 0x02000C30),
_title, MB_YESNOCANCEL);
// ProgressSynch.SetPaused(paused);
if (!paused)
OnPauseButton();
if (res == IDCANCEL || res == IDNO)
return true;
break;
}
case IDC_BUTTON_PAUSE:
OnPauseButton();
return true;
case IDC_BUTTON_PROGRESS_PRIORITY:
{
OnPriorityButton();
return true;
}
}
return CModalDialog::OnButtonClicked(buttonID, buttonHWND);
}

View File

@@ -0,0 +1,184 @@
// ProgressDialog.h
#ifndef __PROGRESSDIALOG_H
#define __PROGRESSDIALOG_H
#include "resource.h"
#include "Common/Types.h"
#include "Windows/Control/Dialog.h"
#include "Windows/Control/ProgressBar.h"
#include "Windows/Synchronization.h"
class CProgressSynch
{
NWindows::NSynchronization::CCriticalSection _criticalSection;
bool _stopped;
bool _paused;
UInt64 _total;
UInt64 _completed;
UString TitleFileName;
UString CurrentFileName;
public:
CProgressSynch(): _stopped(false), _paused(false), _total((UInt64)(Int64)-1), _completed(0) {}
bool GetStopped()
{
NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection);
return _stopped;
}
void SetStopped(bool value)
{
NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection);
_stopped = value;
}
bool GetPaused()
{
NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection);
return _paused;
}
void SetPaused(bool value)
{
NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection);
_paused = value;
}
void SetProgress(UInt64 total, UInt64 completed)
{
NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection);
_total = total;
_completed = completed;
}
void SetPos(UInt64 completed)
{
NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection);
_completed = completed;
}
HRESULT SetPosAndCheckPaused(UInt64 completed);
void GetProgress(UInt64 &total, UInt64 &completed)
{
NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection);
total = _total;
completed = _completed;
}
void SetTitleFileName(const UString &fileName)
{
NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection);
TitleFileName = fileName;
}
void GetTitleFileName(UString &fileName)
{
NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection);
fileName = TitleFileName;
}
void SetCurrentFileName(const UString &fileName)
{
NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection);
CurrentFileName = fileName;
}
void GetCurrentFileName(UString &fileName)
{
NWindows::NSynchronization::CCriticalSectionLock lock(_criticalSection);
fileName = CurrentFileName;
}
};
class CU64ToI32Converter
{
UInt64 _numShiftBits;
public:
void Init(UInt64 _range);
int Count(UInt64 aValue);
};
// class CProgressDialog: public NWindows::NControl::CModelessDialog
enum ESpeedMode
{
kSpeedBytes,
kSpeedKBytes,
kSpeedMBytes
};
class CProgressDialog: public NWindows::NControl::CModalDialog
{
UString _prevFileName;
UString _prevTitleName;
private:
UString backgroundString;
UString backgroundedString;
UString foregroundString;
UString pauseString;
UString continueString;
UString pausedString;
UINT_PTR _timer;
UString _title;
CU64ToI32Converter _converter;
UInt64 _previousPos;
UInt64 _range;
NWindows::NControl::CProgressBar m_ProgressBar;
UInt32 _prevPercentValue;
UInt32 _prevTime;
UInt32 _elapsedTime;
UInt32 _prevElapsedSec;
UInt64 _prevRemainingSec;
ESpeedMode _prevMode;
UInt64 _prevSpeed;
bool _foreground;
bool OnTimer(WPARAM timerID, LPARAM callback);
void SetRange(UInt64 range);
void SetPos(UInt64 pos);
virtual bool OnInit();
virtual void OnCancel();
NWindows::NSynchronization::CManualResetEvent _dialogCreatedEvent;
#ifndef _SFX
void AddToTitle(LPCWSTR string);
#endif
void SetPauseText();
void SetPriorityText();
void OnPauseButton();
void OnPriorityButton();
bool OnButtonClicked(int buttonID, HWND buttonHWND);
void SetTitleText();
public:
CProgressSynch ProgressSynch;
#ifndef _SFX
HWND MainWindow;
UString MainTitle;
UString MainAddTitle;
~CProgressDialog();
#endif
CProgressDialog(): _timer(0)
#ifndef _SFX
,MainWindow(0)
#endif
{}
void WaitCreating() { _dialogCreatedEvent.Lock(); }
INT_PTR Create(const UString &title, HWND wndParent = 0)
{
_title = title;
return CModalDialog::Create(IDD_DIALOG_PROGRESS, wndParent);
}
static const UINT kCloseMessage;
virtual bool OnMessage(UINT message, WPARAM wParam, LPARAM lParam);
void MyClose() { PostMessage(kCloseMessage); };
};
#endif

View File

@@ -0,0 +1,16 @@
// stdafx.h
#ifndef __STDAFX_H
#define __STDAFX_H
#define _WIN32_WINNT 0x0400
// it's for Windows NT supporting (MENUITEMINFOW)
#define WINVER 0x0400
#include <windows.h>
#include <commctrl.h>
#include "Common/NewHandler.h"
#endif

View File

@@ -0,0 +1,17 @@
#define IDC_BUTTON_PAUSE 3
#define IDC_BUTTON_PROGRESS_PRIORITY 4
#define IDD_DIALOG_PROGRESS 500
#define IDS_PROGRESS_PAUSED 700
#define IDS_PROGRESS_FOREGROUND 701
#define IDS_PROGRESS_CONTINUE 702
#define IDS_PROGRESS_ASK_CANCEL 703
#define IDC_PROGRESS1 1000
#define IDC_PROGRESS_ELAPSED 1002
#define IDC_PROGRESS_ELAPSED_VALUE 1003
#define IDC_PROGRESS_REMAINING 1004
#define IDC_PROGRESS_REMAINING_VALUE 1005
#define IDC_PROGRESS_SPEED 1006
#define IDC_PROGRESS_SPEED_VALUE 1007
#define IDC_PROGRESS_TOTAL 1008
#define IDC_PROGRESS_SPEED_TOTAL_VALUE 1009
#define IDC_PROGRESS_FILE_NAME 1010

View File

@@ -0,0 +1,56 @@
#include "resource.h"
#include "../../../GuiCommon.rc"
#define xSize2 290
#define ySize2 76
#define xSize (xSize2 + marg + marg)
#define ySize (ySize2 + marg + marg)
#define bYPos (ySize - marg - bYSize)
#undef bXSize
#define bXSize 72
#define bXPos1 (xSize - marg - bXSize)
#define bXPos2 (bXPos1 - 10 - bXSize)
#define bXPos3 (bXPos2 - 10 - bXSize)
#define valSize 42
#define timeSize 91
#define labelPos 178
#define valPos1 (marg + timeSize)
#define valPos2 (xSize - marg - valSize)
#define labelSize (valPos2 - labelPos)
#undef yPos
#define yPos (marg + 11)
IDD_DIALOG_PROGRESS DIALOG 0, 0, xSize, ySize MY_MODAL_DIALOG_STYLE | WS_MINIMIZEBOX
CAPTION "Progress"
MY_FONT
BEGIN
PUSHBUTTON "&Background", IDC_BUTTON_PROGRESS_PRIORITY, bXPos3, bYPos, bXSize, bYSize
PUSHBUTTON "&Pause", IDC_BUTTON_PAUSE, bXPos2, bYPos, bXSize, bYSize
PUSHBUTTON "Cancel", IDCANCEL, bXPos1, bYPos, bXSize, bYSize
LTEXT "Elapsed time:", IDC_PROGRESS_ELAPSED, marg, marg, timeSize, 8
LTEXT "Remaining time:", IDC_PROGRESS_REMAINING, marg, yPos, timeSize, 8
LTEXT "Size:", IDC_PROGRESS_TOTAL, labelPos, marg, labelSize, 8
LTEXT "Speed:", IDC_PROGRESS_SPEED, labelPos, yPos, labelSize, 8
RTEXT "00:00:00", IDC_PROGRESS_ELAPSED_VALUE, valPos1, marg, valSize, 8
RTEXT "", IDC_PROGRESS_REMAINING_VALUE, valPos1, yPos, valSize, 8
RTEXT "", IDC_PROGRESS_SPEED_TOTAL_VALUE, valPos2, marg, valSize, 8
RTEXT "", IDC_PROGRESS_SPEED_VALUE, valPos2, yPos, valSize, 8
LTEXT "", IDC_PROGRESS_FILE_NAME, marg, yPos + 16, xSize2, 8, SS_NOPREFIX
CONTROL "Progress1", IDC_PROGRESS1, "msctls_progress32", PBS_SMOOTH | WS_BORDER, marg, bYPos - 20, xSize2, 13
END
STRINGTABLE DISCARDABLE
BEGIN
IDS_PROGRESS_PAUSED "Paused"
IDS_PROGRESS_FOREGROUND "&Foreground"
IDS_PROGRESS_CONTINUE "&Continue"
IDS_PROGRESS_ASK_CANCEL "Are you sure you want to cancel?"
END