Skip to content

Commit

Permalink
Some renaming in preparation for fixing Hex mode width calculations.
Browse files Browse the repository at this point in the history
- What is `80` in `80 - ObjWidth()`?
- What happens in `KEY_CTRLSHIFTRIGHT` handler?
  • Loading branch information
MKadaner committed Feb 20, 2024
1 parent 5246488 commit 7d2c732
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 62 deletions.
114 changes: 57 additions & 57 deletions far/viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ enum class Viewer::SearchDisposition

static int ViewerID=0;

static constexpr int s_BytesPerStripe = 8;
static constexpr int s_HexModeBytesPerStripe = 8;

static bool IsCodePageSupportedInViewer(uintptr_t cp)
{
Expand Down Expand Up @@ -209,33 +209,33 @@ wchar_t Viewer::ZeroChar() const
return ViOpt.Visible0x00 && ViOpt.ZeroChar > 0 ? static_cast<wchar_t>(ViOpt.ZeroChar) : 0;
}

int Viewer::CalculateMaxBytesPerLineByScreenWidth() const
int Viewer::CalculateHexModeMaxBytesPerLine() const
{
const int OffsetWidth = 12; // includes offset plus colon and space
const int ByteWidth = 4; // two hex digits, one space, one character in dump
const int BytesGroupSeparatorWidth = 2;
const int MininumBytesCount = s_BytesPerStripe;
const int MininumBytesCount = s_HexModeBytesPerStripe;

auto BytesCount = MininumBytesCount;
for (auto width = XX2 - (OffsetWidth + MininumBytesCount * ByteWidth + BytesGroupSeparatorWidth); width >= ByteWidth; width -= ByteWidth)
for (auto width = ScrollbarAdjustedRight - (OffsetWidth + MininumBytesCount * ByteWidth + BytesGroupSeparatorWidth); width >= ByteWidth; width -= ByteWidth)
{
++BytesCount;

if (!(BytesCount % s_BytesPerStripe))
if (!(BytesCount % s_HexModeBytesPerStripe))
width -= BytesGroupSeparatorWidth;
}

return BytesCount;
}

void Viewer::AdjustBytesPerLine(int const Amount)
void Viewer::AdjustHexModeBytesPerLine(int const Amount)
{
const size_t NewValue = std::clamp(static_cast<int>(m_BytesPerLine) + Amount, s_BytesPerStripe, CalculateMaxBytesPerLineByScreenWidth());
const size_t NewValue = std::clamp(static_cast<int>(m_HexModeBytesPerLine) + Amount, s_HexModeBytesPerStripe, CalculateHexModeMaxBytesPerLine());

if (NewValue == m_BytesPerLine)
if (NewValue == m_HexModeBytesPerLine)
return;

m_BytesPerLine = NewValue;
m_HexModeBytesPerLine = NewValue;
Show();
}

Expand Down Expand Up @@ -456,13 +456,13 @@ bool Viewer::isBinaryFile(uintptr_t cp) // very approximate: looks for '\0' in f

void Viewer::AdjustWidth()
{
Width = m_Where.width();
XX2 = m_Where.right;
ScrollbarAdjustedWidth = m_Where.width();
ScrollbarAdjustedRight = m_Where.right;

if (ViOpt.ShowScrollbar && !m_bQuickView)
{
Width--;
XX2--;
ScrollbarAdjustedWidth--;
ScrollbarAdjustedRight--;
}
}

Expand Down Expand Up @@ -512,7 +512,7 @@ void Viewer::ShowPage(int nMode)
SetScreen(m_Where, L' ', colors::PaletteColorToFarColor(COL_VIEWERTEXT));
GotoXY(m_Where.left, m_Where.top);
SetColor(COL_WARNDIALOGTEXT);
Text(cut_right(msg(lng::MViewerCannotOpenFile), XX2 - m_Where.left + 1));
Text(cut_right(msg(lng::MViewerCannotOpenFile), ScrollbarAdjustedRight - m_Where.left + 1));
ShowStatus();
}

Expand Down Expand Up @@ -598,11 +598,11 @@ void Viewer::ShowPage(int nMode)

if (static_cast<long long>(i.Data.size()) > LeftPos)
{
Text(fit_to_left(i.Data.substr(LeftPos), Width));
Text(fit_to_left(i.Data.substr(LeftPos), ScrollbarAdjustedWidth));
}
else
{
Text(string(Width, L' '));
Text(string(ScrollbarAdjustedWidth, L' '));
}

if (SelectSize >= 0 && i.bSelection)
Expand All @@ -614,7 +614,7 @@ void Viewer::ShowPage(int nMode)
else
SelX1 = i.nSelStart - LeftPos;

if (!m_Wrap && (i.nSelEnd < LeftPos || i.nSelStart > LeftPos + XX2 - m_Where.left))
if (!m_Wrap && (i.nSelEnd < LeftPos || i.nSelStart > LeftPos + ScrollbarAdjustedRight - m_Where.left))
{
if (AdjustSelPosition)
{
Expand All @@ -640,9 +640,9 @@ void Viewer::ShowPage(int nMode)
}
}

if (static_cast<long long>(i.Data.size()) > LeftPos + Width && ViOpt.ShowArrows)
if (static_cast<long long>(i.Data.size()) > LeftPos + ScrollbarAdjustedWidth && ViOpt.ShowArrows)
{
GotoXY(XX2,Y);
GotoXY(ScrollbarAdjustedRight,Y);
SetColor(COL_VIEWERARROWS);
Text(L'»');
}
Expand Down Expand Up @@ -706,7 +706,7 @@ int Viewer::GetModeDependentCharSize() const

int Viewer::GetModeDependentLineSize() const
{
return static_cast<int>(m_DisplayMode == VMT_HEX? m_BytesPerLine : Width * getChSize(m_Codepage));
return static_cast<int>(m_DisplayMode == VMT_HEX? m_HexModeBytesPerLine : ScrollbarAdjustedWidth * getChSize(m_Codepage));
}

int Viewer::txt_dump(std::string_view const Str, size_t ClientWidth, string& OutStr, wchar_t ZeroChar, int tail) const
Expand Down Expand Up @@ -791,8 +791,8 @@ void Viewer::ShowDump()
{
const int CharSize = getChSize(m_Codepage);
const int xl = m_Codepage == CP_UTF8? 4 - 1 : m_Codepage == MB.GetCP()? static_cast<int>(MB.GetSize() - 1) : 0;
std::vector<char> line(Width * CharSize + xl);
const DWORD mb = Width * CharSize;
std::vector<char> line(ScrollbarAdjustedWidth * CharSize + xl);
const DWORD mb = ScrollbarAdjustedWidth * CharSize;

FilePos -= FilePos % CharSize;
vseek(SecondPos = FilePos, FILE_BEGIN);
Expand Down Expand Up @@ -822,13 +822,13 @@ void Viewer::ShowDump()
else
LastPage = EndFile = veof();

tail = txt_dump({ line.data(), BytesRead }, Width, OutStr, ZeroChar(), tail);
tail = txt_dump({ line.data(), BytesRead }, ScrollbarAdjustedWidth, OutStr, ZeroChar(), tail);

Text(fit_to_left(OutStr, ObjWidth()));
if ( SelectSize > 0 && bpos < SelectPos+SelectSize && bpos+mb > SelectPos )
{
const int bsel = SelectPos > bpos? static_cast<int>(SelectPos - bpos) / CharSize : 0;
const int esel = SelectPos + SelectSize < bpos + mb? (static_cast<int>(SelectPos + SelectSize - bpos) + CharSize - 1) / CharSize : Width;
const int esel = SelectPos + SelectSize < bpos + mb? (static_cast<int>(SelectPos + SelectSize - bpos) + CharSize - 1) / CharSize : ScrollbarAdjustedWidth;
SetColor(COL_VIEWERSELECTEDTEXT);
GotoXY(bsel, Y);
Text(cut_right(OutStr.substr(bsel), esel - bsel));
Expand All @@ -846,13 +846,13 @@ void Viewer::ShowHex()

LastPage = false;

if (m_PrevXX2 != XX2)
if (m_PrevHexModeScrollbarAdjustedRight != ScrollbarAdjustedRight)
{
m_PrevXX2 = XX2;
m_PrevHexModeScrollbarAdjustedRight = ScrollbarAdjustedRight;
// TODO: Add an option
if constexpr ((false))
{
m_BytesPerLine = std::min(static_cast<size_t>(CalculateMaxBytesPerLineByScreenWidth()), m_BytesPerLine);
m_HexModeBytesPerLine = std::min(static_cast<size_t>(CalculateHexModeMaxBytesPerLine()), m_HexModeBytesPerLine);
}
}

Expand Down Expand Up @@ -886,12 +886,12 @@ void Viewer::ShowHex()
if ( SelectSize < 0 )
bSelStartFound = bSelEndFound = false;

std::vector<char> RawBuffer(m_BytesPerLine + 3, 0);
std::vector<char> RawBuffer(m_HexModeBytesPerLine + 3, 0);
size_t BytesRead = 0;
const auto BytesToRead = CP_UTF8 == m_Codepage ? m_BytesPerLine + 4 - 1 : (m_Codepage == MB.GetCP() ? m_BytesPerLine + MB.GetSize() - 1 : m_BytesPerLine);
const auto BytesToRead = CP_UTF8 == m_Codepage ? m_HexModeBytesPerLine + 4 - 1 : (m_Codepage == MB.GetCP() ? m_HexModeBytesPerLine + MB.GetSize() - 1 : m_HexModeBytesPerLine);
Reader.Read(RawBuffer.data(), BytesToRead, &BytesRead);
if (BytesRead > m_BytesPerLine)
Reader.Unread(BytesRead - m_BytesPerLine);
if (BytesRead > m_HexModeBytesPerLine)
Reader.Unread(BytesRead - m_HexModeBytesPerLine);
else
LastPage = EndFile = veof();

Expand All @@ -904,20 +904,20 @@ void Viewer::ShowHex()
{
if ( SelectSize >= 0 )
{
if (SelectPos >= fpos && SelectPos < fpos + static_cast<long long>(m_BytesPerLine))
if (SelectPos >= fpos && SelectPos < fpos + static_cast<long long>(m_HexModeBytesPerLine))
{
const auto off = static_cast<int>(SelectPos - fpos);
bSelStartFound = true;
SelStart = static_cast<int>(OutStr.size() + 3 * off + (off < s_BytesPerStripe? 0 : BorderLine.size()));
SelStart = static_cast<int>(OutStr.size() + 3 * off + (off < s_HexModeBytesPerStripe? 0 : BorderLine.size()));
if (!SelectSize)
--SelStart;
}
const auto selectEnd = SelectPos + SelectSize - 1;
if (selectEnd >= fpos && selectEnd < fpos + static_cast<long long>(m_BytesPerLine))
if (selectEnd >= fpos && selectEnd < fpos + static_cast<long long>(m_HexModeBytesPerLine))
{
const auto off = static_cast<int>(selectEnd - fpos);
bSelEndFound = true;
SelEnd = SelectSize ? static_cast<int>(OutStr.size() + 3 * off + (off < s_BytesPerStripe? 0 : BorderLine.size()) + 1) : SelStart;
SelEnd = SelectSize ? static_cast<int>(OutStr.size() + 3 * off + (off < s_HexModeBytesPerStripe? 0 : BorderLine.size()) + 1) : SelStart;
}
else if ( SelectSize == 0 && SelectPos == fpos )
{
Expand All @@ -926,17 +926,17 @@ void Viewer::ShowHex()
}
}

for (const auto X: std::views::iota(0uz, m_BytesPerLine))
for (const auto X: std::views::iota(0uz, m_HexModeBytesPerLine))
{
if (X < BytesRead)
far::format_to(OutStr, L"{:02X} "sv, static_cast<int>(RawBuffer[X]));
else
OutStr.append(3, L' ');

if (X + 1 != m_BytesPerLine && (X + 1) % s_BytesPerStripe == 0)
if (X + 1 != m_HexModeBytesPerLine && (X + 1) % s_HexModeBytesPerStripe == 0)
OutStr += BorderLine;
}
tail = txt_dump({ RawBuffer.data(), BytesRead }, m_BytesPerLine, TextStr, ZeroChar(), tail);
tail = txt_dump({ RawBuffer.data(), BytesRead }, m_HexModeBytesPerLine, TextStr, ZeroChar(), tail);
}

if ((SelEnd <= SelStart) && bSelStartFound && bSelEndFound && SelectSize > 0 )
Expand Down Expand Up @@ -1102,18 +1102,18 @@ void Viewer::ReadString(ViewerString *pString, int MaxSize, bool update_cache)
if ( !m_Wrap )
continue;

if (m_WordWrap && OutPtr <= Width && CanWrapLineAt(ch))
if (m_WordWrap && OutPtr <= ScrollbarAdjustedWidth && CanWrapLineAt(ch))
{
wrap_out = OutPtr;
wrap_pos = fpos1;
}

if ( OutPtr < Width )
if ( OutPtr < ScrollbarAdjustedWidth )
continue;
if ( !m_WordWrap )
break;

if ( OutPtr > Width )
if ( OutPtr > ScrollbarAdjustedWidth )
{
if ( wrap_out <= 0 || IsBlankOrEos(ch) )
{
Expand Down Expand Up @@ -1222,7 +1222,7 @@ long long Viewer::EndOfScreen(int line)
{
vseek(Strings.back().nFilePos, FILE_BEGIN);
int col = 0;
const auto rmargin = static_cast<int>(LeftPos) + Width;
const auto rmargin = static_cast<int>(LeftPos) + ScrollbarAdjustedWidth;
wchar_t ch;
while (vgetc(&ch))
{
Expand Down Expand Up @@ -1295,16 +1295,16 @@ long long Viewer::XYfilepos(int col, int row)
switch (m_DisplayMode)
{
case VMT_DUMP:
pos = FilePos + csz*(Width*row + col);
pos = FilePos + csz*(ScrollbarAdjustedWidth*row + col);
break;

case VMT_HEX:
//0000000000: 32 30 2E 30 31 2E 32 30 | 31 35 20 31 30 3A 33 39 20.01.2015 10:39 - 1-byte
//0000000020: 31 00 2E 00 30 00 22 00 | 20 00 65 00 6E 00 63 00 1.0" enc - 2-byte
if (col < 11) col = 0;
else if (col < 35) col = (col-11)/3;
else if (col < 37) col = s_BytesPerStripe;
else if (col < 61) col = s_BytesPerStripe + (col-37)/3;
else if (col < 37) col = s_HexModeBytesPerStripe;
else if (col < 61) col = s_HexModeBytesPerStripe + (col-37)/3;
else if (col < 63) col = 0;
else if (col < 63 + 16 / csz) col = (col-63) * csz;
else col = 16;
Expand Down Expand Up @@ -1806,8 +1806,8 @@ bool Viewer::process_key(const Manager::Key& Key)
{
if (LeftPos>0 && ViewFile)
{
if (m_DisplayMode == VMT_HEX && LeftPos > 80 - Width)
LeftPos=std::max(80-Width,1);
if (m_DisplayMode == VMT_HEX && LeftPos > 80 - ScrollbarAdjustedWidth)
LeftPos=std::max(80-ScrollbarAdjustedWidth,1);

LeftPos--;
Show();
Expand Down Expand Up @@ -1874,22 +1874,22 @@ bool Viewer::process_key(const Manager::Key& Key)

case KEY_ALTLEFT:
case KEY_RALTLEFT:
AdjustBytesPerLine(-1);
AdjustHexModeBytesPerLine(-1);
return true;

case KEY_CTRLALTLEFT: case KEY_RCTRLALTLEFT:
case KEY_CTRLRALTLEFT: case KEY_RCTRLRALTLEFT:
AdjustBytesPerLine(-16);
AdjustHexModeBytesPerLine(-16);
return true;

case KEY_ALTRIGHT:
case KEY_RALTRIGHT:
AdjustBytesPerLine(1);
AdjustHexModeBytesPerLine(1);
return true;

case KEY_CTRLALTRIGHT: case KEY_RCTRLALTRIGHT:
case KEY_CTRLRALTRIGHT: case KEY_RCTRLRALTRIGHT:
AdjustBytesPerLine(16);
AdjustHexModeBytesPerLine(16);
return true;

case KEY_CTRLSHIFTLEFT: case KEY_CTRLSHIFTNUMPAD4:
Expand All @@ -1914,7 +1914,7 @@ bool Viewer::process_key(const Manager::Key& Key)
{
return std::max(Value, i.Data.size());
});
LeftPos = (MaxLen > static_cast<size_t>(Width))? MaxLen - Width : 0;
LeftPos = (MaxLen > static_cast<size_t>(ScrollbarAdjustedWidth))? MaxLen - ScrollbarAdjustedWidth : 0;
Show();
}

Expand Down Expand Up @@ -2121,7 +2121,7 @@ void Viewer::CacheLine( long long start, int length, bool have_eol )
return;

if ( lcache_ready
&& (lcache_wrap != static_cast<int>(m_Wrap) || lcache_wwrap != static_cast<int>(m_WordWrap) || lcache_width != Width)
&& (lcache_wrap != static_cast<int>(m_Wrap) || lcache_wwrap != static_cast<int>(m_WordWrap) || lcache_width != ScrollbarAdjustedWidth)
){
lcache_ready = false;
}
Expand All @@ -2136,7 +2136,7 @@ void Viewer::CacheLine( long long start, int length, bool have_eol )
lcache_lines[0] = (have_eol ? -start : +start);
lcache_lines[1] = start + length;

lcache_wrap = m_Wrap; lcache_wwrap = m_WordWrap; lcache_width = Width;
lcache_wrap = m_Wrap; lcache_wwrap = m_WordWrap; lcache_width = ScrollbarAdjustedWidth;
lcache_ready = true;
}
else if (start == lcache_last)
Expand Down Expand Up @@ -2197,7 +2197,7 @@ void Viewer::CacheLine( long long start, int length, bool have_eol )
int Viewer::CacheFindUp( long long start )
{
if ( lcache_ready
&& (lcache_wrap != static_cast<int>(m_Wrap) || lcache_wwrap != static_cast<int>(m_WordWrap) || lcache_width != Width)
&& (lcache_wrap != static_cast<int>(m_Wrap) || lcache_wwrap != static_cast<int>(m_WordWrap) || lcache_width != ScrollbarAdjustedWidth)
){
lcache_ready = false;
}
Expand Down Expand Up @@ -3759,8 +3759,8 @@ void Viewer::SelectText(const long long& MatchPos,const long long& SearchLength,
if ( vString.eol_length == 0 )
{
const auto found_offset = static_cast<int>(vString.Data.size());
if ( found_offset > Width-10 )
LeftPos = (Width <= 10 ? found_offset : found_offset + 10 - Width);
if ( found_offset > ScrollbarAdjustedWidth-10 )
LeftPos = (ScrollbarAdjustedWidth <= 10 ? found_offset : found_offset + 10 - ScrollbarAdjustedWidth);
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions far/viewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ class Viewer:public SimpleScreenObject
wchar_t ZeroChar() const;
size_t MaxViewLineSize() const { return ViOpt.MaxLineSize; }
size_t MaxViewLineBufferSize() const { return ViOpt.MaxLineSize + 15; }
int CalculateMaxBytesPerLineByScreenWidth() const;
void AdjustBytesPerLine(int Amount);
int CalculateHexModeMaxBytesPerLine() const;
void AdjustHexModeBytesPerLine(int Amount);

friend class FileViewer;

Expand Down Expand Up @@ -234,7 +234,10 @@ class Viewer:public SimpleScreenObject
std::list<ViewerUndoData> UndoData;

int LastKeyUndo{};
int Width{}, XX2{}; // используется при расчете ширины при скролбаре
// используется при расчете ширины при скролбаре
int ScrollbarAdjustedWidth{};
int ScrollbarAdjustedRight{};

int ViewerID;
bool OpenFailed{};
bool bVE_READ_Sent{};
Expand Down Expand Up @@ -307,8 +310,8 @@ class Viewer:public SimpleScreenObject
std::vector<wchar_t> ReadBuffer;
F8CP f8cps{true};
std::optional<bool> m_GotoHex;
int m_PrevXX2{};
size_t m_BytesPerLine{ 16 };
int m_PrevHexModeScrollbarAdjustedRight{};
size_t m_HexModeBytesPerLine{ 16 };
};

class ViewerContainer
Expand Down

0 comments on commit 7d2c732

Please sign in to comment.