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
|
// ProgressBox.cpp
#include "StdAfx.h"
#include <stdio.h>
#include "../../../Common/IntToString.h"
#include "FarUtils.h"
#include "ProgressBox.h"
static void CopySpaces(char *dest, int numSpaces)
{
int i;
for (i = 0; i < numSpaces; i++)
dest[i] = ' ';
dest[i] = '\0';
}
void ConvertUInt64ToStringAligned(UInt64 value, char *s, int alignSize)
{
char temp[32];
ConvertUInt64ToString(value, temp);
int len = (int)strlen(temp);
int numSpaces = 0;
if (len < alignSize)
{
numSpaces = alignSize - len;
CopySpaces(s, numSpaces);
}
strcpy(s + numSpaces, temp);
}
// ---------- CMessageBox ----------
static const int kMaxLen = 255;
void CMessageBox::Init(const AString &title, int width)
{
_title = title;
_width = MyMin(width, kMaxLen);
}
void CMessageBox::ShowMessages(const char *strings[], int numStrings)
{
const int kNumStaticStrings = 1;
const int kNumStringsMax = 10;
if (numStrings > kNumStringsMax)
numStrings = kNumStringsMax;
const char *msgItems[kNumStaticStrings + kNumStringsMax];
msgItems[0] = _title;
char formattedMessages[kNumStringsMax][kMaxLen + 1];
for (int i = 0; i < numStrings; i++)
{
char *formattedMessage = formattedMessages[i];
const char *s = strings[i];
int len = (int)strlen(s);
if (len < kMaxLen)
{
int size = MyMax(_width, len);
int startPos = (size - len) / 2;
CopySpaces(formattedMessage, startPos);
strcpy(formattedMessage + startPos, s);
CopySpaces(formattedMessage + startPos + len, size - startPos - len);
}
else
{
strncpy(formattedMessage, s, kMaxLen);
formattedMessage[kMaxLen] = 0;
}
msgItems[kNumStaticStrings + i] = formattedMessage;
}
NFar::g_StartupInfo.ShowMessage(0, NULL, msgItems, kNumStaticStrings + numStrings, 0);
}
// ---------- CProgressBox ----------
void CProgressBox::Init(const AString &title, int width)
{
CMessageBox::Init(title, width);
_prevMessage.Empty();
_prevPercentMessage.Empty();
_wasShown = false;
}
void CProgressBox::Progress(const UInt64 *total, const UInt64 *completed, const AString &message)
{
AString percentMessage;
if (total != 0 && completed != 0)
{
UInt64 totalVal = *total;
if (totalVal == 0)
totalVal = 1;
char buf[32];
ConvertUInt64ToStringAligned(*completed * 100 / totalVal, buf, 3);
strcat(buf, "%");
percentMessage = buf;
}
if (message != _prevMessage || percentMessage != _prevPercentMessage || !_wasShown)
{
_prevMessage = message;
_prevPercentMessage = percentMessage;
const char *strings[] = { message, percentMessage };
ShowMessages(strings, ARRAY_SIZE(strings));
_wasShown = true;
}
}
|