1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
// ProgressDialog.cpp
#include "StdAfx.h"
#include "../../../Common/IntToString.h"
#include "resource.h"
#include "ProgressDialog.h"
using namespace NWindows;
extern HINSTANCE g_hInstance;
static const UINT_PTR kTimerID = 3;
static const UINT kTimerElapse = 100;
#ifdef Z7_LANG
#include "LangUtils.h"
#endif
HRESULT CProgressSync::ProcessStopAndPause()
{
for (;;)
{
if (GetStopped())
return E_ABORT;
if (!GetPaused())
break;
::Sleep(100);
}
return S_OK;
}
#ifndef Z7_SFX
CProgressDialog::~CProgressDialog()
{
AddToTitle(L"");
}
void CProgressDialog::AddToTitle(LPCWSTR s)
{
if (MainWindow != 0)
MySetWindowText(MainWindow, UString(s) + MainTitle);
}
#endif
#define UNDEFINED_VAL ((UInt64)(Int64)-1)
bool CProgressDialog::OnInit()
{
_range = UNDEFINED_VAL;
_prevPercentValue = UNDEFINED_VAL;
_wasCreated = true;
_dialogCreatedEvent.Set();
#ifdef Z7_LANG
LangSetDlgItems(*this, NULL, 0);
#endif
m_ProgressBar.Attach(GetItem(IDC_PROGRESS1));
if (IconID >= 0)
{
HICON icon = LoadIcon(g_hInstance, MAKEINTRESOURCE(IconID));
SetIcon(ICON_BIG, icon);
}
_timer = SetTimer(kTimerID, kTimerElapse);
SetText(_title);
CheckNeedClose();
return CModalDialog::OnInit();
}
void CProgressDialog::OnCancel() { Sync.SetStopped(true); }
void CProgressDialog::OnOK() { }
void CProgressDialog::SetRange(UInt64 range)
{
_range = range;
_peviousPos = (UInt64)(Int64)-1;
_converter.Init(range);
m_ProgressBar.SetRange32(0 , _converter.Count(range)); // Test it for 100%
}
void CProgressDialog::SetPos(UInt64 pos)
{
bool redraw = true;
if (pos < _range && pos > _peviousPos)
{
UInt64 posDelta = pos - _peviousPos;
if (posDelta < (_range >> 10))
redraw = false;
}
if (redraw)
{
m_ProgressBar.SetPos(_converter.Count(pos)); // Test it for 100%
_peviousPos = pos;
}
}
bool CProgressDialog::OnTimer(WPARAM /* timerID */, LPARAM /* callback */)
{
if (Sync.GetPaused())
return true;
CheckNeedClose();
UInt64 total, completed;
Sync.GetProgress(total, completed);
if (total != _range)
SetRange(total);
SetPos(completed);
if (total == 0)
total = 1;
const UInt64 percentValue = completed * 100 / total;
if (percentValue != _prevPercentValue)
{
wchar_t s[64];
ConvertUInt64ToString(percentValue, s);
UString title = s;
title += "% ";
SetText(title + _title);
#ifndef Z7_SFX
AddToTitle(title + MainAddTitle);
#endif
_prevPercentValue = percentValue;
}
return true;
}
bool CProgressDialog::OnMessage(UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case kCloseMessage:
{
if (_timer)
{
KillTimer(kTimerID);
_timer = 0;
}
if (_inCancelMessageBox)
{
_externalCloseMessageWasReceived = true;
break;
}
return OnExternalCloseMessage();
}
/*
case WM_SETTEXT:
{
if (_timer == 0)
return true;
}
*/
}
return CModalDialog::OnMessage(message, wParam, lParam);
}
bool CProgressDialog::OnButtonClicked(unsigned buttonID, HWND buttonHWND)
{
switch (buttonID)
{
case IDCANCEL:
{
bool paused = Sync.GetPaused();
Sync.SetPaused(true);
_inCancelMessageBox = true;
int res = ::MessageBoxW(*this, L"Are you sure you want to cancel?", _title, MB_YESNOCANCEL);
_inCancelMessageBox = false;
Sync.SetPaused(paused);
if (res == IDCANCEL || res == IDNO)
{
if (_externalCloseMessageWasReceived)
OnExternalCloseMessage();
return true;
}
break;
}
}
return CModalDialog::OnButtonClicked(buttonID, buttonHWND);
}
void CProgressDialog::CheckNeedClose()
{
if (_needClose)
{
PostMsg(kCloseMessage);
_needClose = false;
}
}
bool CProgressDialog::OnExternalCloseMessage()
{
End(0);
return true;
}
|