Minor code improvements

This commit is contained in:
Markus Gans 2021-04-05 21:20:02 +02:00
parent 865e78d9de
commit d5ec54dcfd
18 changed files with 131 additions and 109 deletions

View File

@ -284,13 +284,13 @@ EventLog::~EventLog() noexcept = default; // destructor
//----------------------------------------------------------------------
void EventLog::onTimer (finalcut::FTimerEvent*)
{
if ( ! str().empty() )
{
scrolltext.append(str());
str("");
scrolltext.scrollToEnd();
redraw();
}
if ( str().empty() )
return;
scrolltext.append(str());
str("");
scrolltext.scrollToEnd();
redraw();
}
//----------------------------------------------------------------------

View File

@ -120,7 +120,7 @@ void ColorChooser::draw()
setColor();
drawBorder();
for (FColor c{FColor::Black}; c < 16; c++)
for (FColor c{FColor::Black}; c < 16; ++c)
{
print() << FPoint{2 + (int(c) / 8) * 3, 3 + int(c) % 8};

View File

@ -143,7 +143,7 @@ void AttribDlg::cb_next()
else if ( bgcolor == FColor::Default )
bgcolor = FColor::Black;
else
bgcolor++;
++bgcolor;
redraw();
}
@ -159,7 +159,7 @@ void AttribDlg::cb_back()
else if ( bgcolor == FColor::Default )
bgcolor = FColor(finalcut::FTerm::getMaxColor() - 1);
else
bgcolor--;
--bgcolor;
redraw();
}
@ -272,7 +272,7 @@ void AttribDemo::printColorLine()
{
const auto& parent = static_cast<AttribDlg*>(getParent());
for (FColor color{FColor::Black}; color < last_color; color++)
for (FColor color{FColor::Black}; color < last_color; ++color)
{
print() << FColorPair{color, parent->getBGColor()} << " # ";
}
@ -479,7 +479,7 @@ void AttribDemo::draw()
if ( bg == FColor::Default )
print (" default");
else
printf ( " %d", bg);
printf ( " %u", bg);
print() << FPoint{16, 17} << "Change background color ->";
}

View File

@ -22,7 +22,6 @@
#include <fstream>
#include <functional>
#include <map>
#include <iostream>
#include <string>
#include <vector>

View File

@ -529,7 +529,7 @@ void FApplication::cmdOptions (const Args& args)
CmdMap cmd_map{};
setCmdOptionsMap(cmd_map);
auto argc = int(args.size());
auto argc = args.size();
std::vector<const char*> argv(argc);
std::transform ( args.begin()
, args.end()
@ -547,14 +547,16 @@ void FApplication::cmdOptions (const Args& args)
std::vector<CmdOption> long_options{};
setLongOptions(long_options);
auto p = reinterpret_cast<const struct option*>(long_options.data());
auto argv_data = const_cast<char**>(argv.data());
const int opt = getopt_long (argc, argv_data, "", p, &idx);
auto argv_data = const_cast<char* const*>(argv.data());
const int opt = getopt_long (int(argc), argv_data, "", p, &idx);
if ( opt == -1 )
break;
if ( cmd_map.find(opt) != cmd_map.end() )
cmd_map[opt](optarg);
const auto& entry = cmd_map[opt];
if ( entry )
entry(optarg);
}
cmd_map.clear();

View File

@ -1417,10 +1417,10 @@ inline void FDialog::passEventToSubMenu ( const MouseStates& ms
//----------------------------------------------------------------------
inline void FDialog::moveSizeKey (FKeyEvent* ev)
{
const auto key = ev->key();
const auto& entry = key_map[ev->key()];
if ( key_map.find(key) != key_map.end() )
key_map[key]();
if ( entry )
entry();
// Accept for all, so that parent widgets will not receive keystrokes
ev->accept();

View File

@ -297,10 +297,11 @@ void FLineEdit::onKeyPress (FKeyEvent* ev)
return;
const auto key = ev->key();
const auto& entry = key_map[key];
if ( key_map.find(key) != key_map.end() )
if ( entry )
{
key_map[key]();
entry();
ev->accept();
}
else if ( key == FKey::Tab )

View File

@ -660,20 +660,26 @@ inline void FListBox::mapKeyFunctions()
void FListBox::processKeyAction (FKeyEvent* ev)
{
const auto idx = ev->key();
const auto& entry = key_map[idx];
if ( key_map.find(idx) != key_map.end() )
if ( entry )
{
key_map[idx]();
entry();
ev->accept();
}
else if ( key_map_result.find(idx) != key_map_result.end() )
else
{
if ( key_map_result[idx]() )
const auto& entry_result = key_map_result[idx];
if ( entry_result )
{
if ( entry_result() )
ev->accept();
}
else if ( keyIncSearchInput(idx) )
{
ev->accept();
}
else if ( keyIncSearchInput(ev->key()) )
{
ev->accept();
}
}
}

View File

@ -1445,20 +1445,26 @@ inline void FListView::mapKeyFunctions()
void FListView::processKeyAction (FKeyEvent* ev)
{
const auto idx = ev->key();
const auto& entry = key_map[idx];
if ( key_map.find(idx) != key_map.end() )
if ( entry )
{
key_map[idx]();
entry();
ev->accept();
}
else if ( key_map_result.find(idx) != key_map_result.end() )
{
if ( key_map_result[idx]() )
ev->accept();
}
else
{
ev->ignore();
const auto& entry_result = key_map_result[idx];
if ( entry_result )
{
if ( entry_result() )
ev->accept();
}
else
{
ev->ignore();
}
}
}

View File

@ -473,11 +473,11 @@ void FScrollView::drawBorder()
//----------------------------------------------------------------------
void FScrollView::onKeyPress (FKeyEvent* ev)
{
const auto idx = ev->key();
const auto& entry = key_map[ev->key()];
if ( key_map.find(idx) != key_map.end() )
if ( entry )
{
key_map[idx]();
entry();
ev->accept();
}
}

View File

@ -1065,15 +1065,15 @@ wchar_t FTerm::charEncode (wchar_t c)
wchar_t FTerm::charEncode (wchar_t c, Encoding enc)
{
wchar_t ch_enc = c;
auto found = std::find_if ( fc::character.begin()
, fc::character.end()
, [&c] (const fc::CharEncodeMap& entry)
{
return entry.unicode == c;
} );
for (auto&& entry : fc::character)
{
if ( entry.unicode == c )
{
ch_enc = getCharacter(entry, enc);
break;
}
}
if ( found != fc::character.end() )
ch_enc = getCharacter(*found, enc);
if ( enc == Encoding::PC && ch_enc == c )
ch_enc = finalcut::unicode_to_cp437(c);

View File

@ -285,15 +285,15 @@ wchar_t cp437_to_unicode (uChar c)
constexpr std::size_t CP437 = 0;
constexpr std::size_t UNICODE = 1;
wchar_t ucs = c;
auto found = std::find_if ( fc::cp437_ucs.begin()
, fc::cp437_ucs.end()
, [&c] (const std::array<wchar_t, 2>& entry)
{
return entry[CP437] == c;
} );
for (auto&& entry : fc::cp437_ucs)
{
if ( entry[CP437] == c ) // found
{
ucs = entry[UNICODE];
break;
}
}
if ( found != fc::cp437_ucs.end() )
ucs = (*found)[UNICODE];
return ucs;
}
@ -305,14 +305,15 @@ uChar unicode_to_cp437 (wchar_t ucs)
constexpr std::size_t UNICODE = 1;
uChar c{'?'};
for (auto&& entry : fc::cp437_ucs)
{
if ( entry[UNICODE] == ucs ) // found
{
c = uChar(entry[CP437]);
break;
}
}
auto found = std::find_if ( fc::cp437_ucs.begin()
, fc::cp437_ucs.end()
, [&ucs] (const std::array<wchar_t, 2>& entry)
{
return entry[UNICODE] == ucs;
} );
if ( found != fc::cp437_ucs.end() )
c = static_cast<uChar>((*found)[CP437]);
return c;
}
@ -327,12 +328,15 @@ FString getFullWidth (const FString& str)
{
constexpr std::size_t HALF = 0;
constexpr std::size_t FULL = 1;
auto found = std::find_if ( fc::halfwidth_fullwidth.begin()
, fc::halfwidth_fullwidth.end()
, [&c] (const std::array<wchar_t, 2>& entry)
{
return entry[HALF] == c;
} );
for (auto&& entry : fc::halfwidth_fullwidth)
{
if ( entry[HALF] == c ) // found
c = entry[FULL];
}
if ( found != fc::halfwidth_fullwidth.end() )
c = (*found)[FULL];
};
for (auto&& c : s)
@ -356,12 +360,15 @@ FString getHalfWidth (const FString& str)
{
constexpr std::size_t HALF = 0;
constexpr std::size_t FULL = 1;
auto found = std::find_if ( fc::halfwidth_fullwidth.begin()
, fc::halfwidth_fullwidth.end()
, [&c] (const std::array<wchar_t, 2>& entry)
{
return entry[FULL] == c;
} );
for (auto&& entry : fc::halfwidth_fullwidth)
{
if ( entry[FULL] == c ) // found
c = entry[HALF];
}
if ( found != fc::halfwidth_fullwidth.end() )
c = (*found)[HALF];
};
for (auto&& c : s)

View File

@ -448,11 +448,11 @@ FKey FTermLinux::modifierKeyCorrection (const FKey& key_id)
// Get the current modifier key state
const Pair pair{getModifierKey(), key_id};
const auto iter = key_map.find(pair);
const auto& key = key_map[pair];
if ( iter == key_map.cend() ) // Not found
if ( key == FKey(0) ) // Not found
return key_id;
else // Found
else // Found
return key_map[pair];
}
@ -896,12 +896,14 @@ void FTermLinux::setVGADefaultPalette()
{0x55, 0xff, 0xff}, {0xff, 0xff, 0xff}
}};
for (std::size_t index{0}; index < 16; index++)
{
cmap.color[index].red = defaultColor[index].red;
cmap.color[index].green = defaultColor[index].green;
cmap.color[index].blue = defaultColor[index].blue;
}
std::transform ( defaultColor.begin()
, defaultColor.end()
, cmap.color.begin()
, [] (const RGB& rgb)
{
return rgb;
}
);
}
//----------------------------------------------------------------------

View File

@ -333,11 +333,11 @@ void FTextView::clear()
//----------------------------------------------------------------------
void FTextView::onKeyPress (FKeyEvent* ev)
{
const auto idx = ev->key();
const auto& entry = key_map[ev->key()];
if ( key_map.find(idx) != key_map.end() )
if ( entry )
{
key_map[idx]();
entry();
ev->accept();
}
}

View File

@ -465,8 +465,7 @@ int FVTerm::print (FTermArea* area, wchar_t c)
if ( ! area )
return -1;
FChar nc{}; // next character
nc = FVTerm::getAttribute();
FChar nc = FVTerm::getAttribute(); // next character
nc.ch[0] = c;
nc.attr.byte[2] = 0;
nc.attr.byte[3] = 0;
@ -1114,7 +1113,7 @@ void FVTerm::scrollAreaForward (FTermArea* area) const
auto bottom_right = std::size_t((y_max * total_width) - area->right_shadow - 1);
const auto& lc = area->data[bottom_right]; // last character
std::memcpy (&nc, &lc, sizeof(nc));
nc.ch[0] = ' ';
nc.ch[0] = L' ';
auto& dc = area->data[y_max * total_width]; // destination character
std::fill_n (&dc, area->width, nc);
area->changes[y_max].xmin = 0;
@ -1166,7 +1165,7 @@ void FVTerm::scrollAreaReverse (FTermArea* area) const
FChar nc{}; // next character
const auto& lc = area->data[total_width]; // last character
std::memcpy (&nc, &lc, sizeof(nc));
nc.ch[0] = ' ';
nc.ch[0] = L' ';
auto& dc = area->data[0]; // destination character
std::fill_n (&dc, area->width, nc);
area->changes[0].xmin = 0;
@ -1313,7 +1312,7 @@ inline void FVTerm::resetTextAreaToDefault ( const FTermArea* area
FChar default_char;
FLineChanges unchanged;
default_char.ch[0] = ' ';
default_char.ch[0] = L' ';
default_char.fg_color = FColor::Default;
default_char.bg_color = FColor::Default;
default_char.attr.byte[0] = 0;
@ -1454,7 +1453,7 @@ inline void FVTerm::updateOverlappedColor ( const FChar& area_char
|| nc.ch[0] == UniChar::RightHalfBlock
|| nc.ch[0] == UniChar::MediumShade
|| nc.ch[0] == UniChar::FullBlock )
nc.ch[0] = ' ';
nc.ch[0] = L' ';
nc.attr.bit.no_changes = bool(vterm_char.attr.bit.printed && vterm_char == nc);
std::memcpy (&vterm_char, &nc, sizeof(vterm_char));
@ -1489,7 +1488,7 @@ inline void FVTerm::updateShadedCharacter ( const FChar& area_char
|| cover_char.ch[0] == UniChar::RightHalfBlock
|| cover_char.ch[0] == UniChar::MediumShade
|| cover_char.ch[0] == UniChar::FullBlock )
cover_char.ch[0] = ' ';
cover_char.ch[0] = L' ';
cover_char.attr.bit.no_changes = \
bool(vterm_char.attr.bit.printed && vterm_char == cover_char);
@ -1728,7 +1727,7 @@ FChar FVTerm::generateCharacter (const FPoint& pos)
|| s_ch.ch[0] == UniChar::RightHalfBlock
|| s_ch.ch[0] == UniChar::MediumShade
|| s_ch.ch[0] == UniChar::FullBlock )
s_ch.ch[0] = ' ';
s_ch.ch[0] = L' ';
sc = &s_ch;
}
@ -1981,7 +1980,7 @@ void FVTerm::putAreaCharacter ( const FPoint& pos, const FTermArea* area
|| ch.ch[0] == UniChar::RightHalfBlock
|| ch.ch[0] == UniChar::MediumShade
|| ch.ch[0] == UniChar::FullBlock )
ch.ch[0] = ' ';
ch.ch[0] = L' ';
std::memcpy (&vterm_char, &ch, sizeof(vterm_char));
}
@ -2146,7 +2145,7 @@ bool FVTerm::canClearToEOL (uInt xmin, uInt y)
const auto& ce = TCAP(t_clr_eol);
const auto& min_char = vt->data[y * uInt(vt->width) + xmin];
if ( ce && min_char.ch[0] == ' ' )
if ( ce && min_char.ch[0] == L' ' )
{
uInt beginning_whitespace = 1;
const bool normal = FTerm::isNormal(min_char);
@ -2181,7 +2180,7 @@ bool FVTerm::canClearLeadingWS (uInt& xmin, uInt y)
const auto& cb = TCAP(t_clr_bol);
const auto& first_char = vt->data[y * uInt(vt->width)];
if ( cb && first_char.ch[0] == ' ' )
if ( cb && first_char.ch[0] == L' ' )
{
uInt leading_whitespace = 1;
const bool normal = FTerm::isNormal(first_char);
@ -2219,7 +2218,7 @@ bool FVTerm::canClearTrailingWS (uInt& xmax, uInt y)
const auto& ce = TCAP(t_clr_eol);
const auto& last_char = vt->data[(y + 1) * uInt(vt->width) - 1];
if ( ce && last_char.ch[0] == ' ' )
if ( ce && last_char.ch[0] == L' ' )
{
uInt trailing_whitespace = 1;
const bool normal = FTerm::isNormal(last_char);
@ -2299,7 +2298,7 @@ void FVTerm::printRange ( uInt xmin, uInt xmax, uInt y
continue;
// Erase character
if ( ec && print_char.ch[0] == ' ' )
if ( ec && print_char.ch[0] == L' ' )
{
PrintState erase_state = \
eraseCharacters(x, xmax, y, draw_trailing_ws);
@ -2510,7 +2509,7 @@ FVTerm::PrintState FVTerm::eraseCharacters ( uInt& x, uInt xmax, uInt y
const auto& ec = TCAP(t_erase_chars);
auto& print_char = vt->data[y * uInt(vt->width) + x];
if ( ! ec || print_char.ch[0] != ' ' )
if ( ! ec || print_char.ch[0] != L' ' )
return PrintState::NothingPrinted;
uInt whitespace{1};
@ -3140,10 +3139,11 @@ void FVTerm::appendLowerRight (FChar& last_char) const
//----------------------------------------------------------------------
inline void FVTerm::characterFilter (FChar& next_char) const
{
charSubstitution& sub_map = fterm->getCharSubstitutionMap();
auto& sub_map = fterm->getCharSubstitutionMap();
const auto& entry = sub_map[next_char.encoded_char[0]];
if ( sub_map.find(next_char.encoded_char[0]) != sub_map.end() )
next_char.encoded_char[0] = sub_map[next_char.encoded_char[0]];
if ( entry )
next_char.encoded_char[0] = entry;
}
//----------------------------------------------------------------------

View File

@ -2006,7 +2006,7 @@ void FWidget::drawWindows() const
{
// redraw windows
FChar default_char{};
default_char.ch[0] = ' ';
default_char.ch[0] = L' ';
default_char.fg_color = FColor::Black;
default_char.bg_color = FColor::Black;
default_char.attr.byte[0] = 0;

View File

@ -801,9 +801,9 @@ enum class FKey : uInt32
struct FKeyHash
{
std::size_t operator () (const FKey& p) const noexcept
std::size_t operator () (const FKey& k) const noexcept
{
return std::hash<uInt32>()(uInt32(p));
return std::hash<uInt32>()(uInt32(k));
}
};

View File

@ -111,7 +111,6 @@
#include <cmath>
#include <csignal>
#include <functional>
#include <map>
#include <memory>
#include <queue>
#include <utility>