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*) void EventLog::onTimer (finalcut::FTimerEvent*)
{ {
if ( ! str().empty() ) if ( str().empty() )
{ return;
scrolltext.append(str()); scrolltext.append(str());
str(""); str("");
scrolltext.scrollToEnd(); scrolltext.scrollToEnd();
redraw(); redraw();
}
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------

View File

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

View File

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

View File

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

View File

@ -1417,10 +1417,10 @@ inline void FDialog::passEventToSubMenu ( const MouseStates& ms
//---------------------------------------------------------------------- //----------------------------------------------------------------------
inline void FDialog::moveSizeKey (FKeyEvent* ev) 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() ) if ( entry )
key_map[key](); entry();
// Accept for all, so that parent widgets will not receive keystrokes // Accept for all, so that parent widgets will not receive keystrokes
ev->accept(); ev->accept();

View File

@ -297,10 +297,11 @@ void FLineEdit::onKeyPress (FKeyEvent* ev)
return; return;
const auto key = ev->key(); 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(); ev->accept();
} }
else if ( key == FKey::Tab ) else if ( key == FKey::Tab )

View File

@ -660,21 +660,27 @@ inline void FListBox::mapKeyFunctions()
void FListBox::processKeyAction (FKeyEvent* ev) void FListBox::processKeyAction (FKeyEvent* ev)
{ {
const auto idx = ev->key(); 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(); 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(); ev->accept();
} }
else if ( keyIncSearchInput(ev->key()) ) else if ( keyIncSearchInput(idx) )
{ {
ev->accept(); ev->accept();
} }
}
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------

View File

@ -1445,21 +1445,27 @@ inline void FListView::mapKeyFunctions()
void FListView::processKeyAction (FKeyEvent* ev) void FListView::processKeyAction (FKeyEvent* ev)
{ {
const auto idx = ev->key(); 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(); 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(); ev->accept();
} }
else else
{ {
ev->ignore(); ev->ignore();
} }
}
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------

View File

@ -473,11 +473,11 @@ void FScrollView::drawBorder()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FScrollView::onKeyPress (FKeyEvent* ev) 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(); 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 FTerm::charEncode (wchar_t c, Encoding enc)
{ {
wchar_t ch_enc = c; 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 ( found != fc::character.end() )
{ ch_enc = getCharacter(*found, enc);
if ( entry.unicode == c )
{
ch_enc = getCharacter(entry, enc);
break;
}
}
if ( enc == Encoding::PC && ch_enc == c ) if ( enc == Encoding::PC && ch_enc == c )
ch_enc = finalcut::unicode_to_cp437(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 CP437 = 0;
constexpr std::size_t UNICODE = 1; constexpr std::size_t UNICODE = 1;
wchar_t ucs = c; 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 ( found != fc::cp437_ucs.end() )
{ ucs = (*found)[UNICODE];
if ( entry[CP437] == c ) // found
{
ucs = entry[UNICODE];
break;
}
}
return ucs; return ucs;
} }
@ -305,14 +305,15 @@ uChar unicode_to_cp437 (wchar_t ucs)
constexpr std::size_t UNICODE = 1; constexpr std::size_t UNICODE = 1;
uChar c{'?'}; uChar c{'?'};
for (auto&& entry : fc::cp437_ucs) auto found = std::find_if ( fc::cp437_ucs.begin()
, fc::cp437_ucs.end()
, [&ucs] (const std::array<wchar_t, 2>& entry)
{ {
if ( entry[UNICODE] == ucs ) // found return entry[UNICODE] == ucs;
{ } );
c = uChar(entry[CP437]);
break; if ( found != fc::cp437_ucs.end() )
} c = static_cast<uChar>((*found)[CP437]);
}
return c; return c;
} }
@ -327,12 +328,15 @@ FString getFullWidth (const FString& str)
{ {
constexpr std::size_t HALF = 0; constexpr std::size_t HALF = 0;
constexpr std::size_t FULL = 1; constexpr std::size_t FULL = 1;
auto found = std::find_if ( fc::halfwidth_fullwidth.begin()
for (auto&& entry : fc::halfwidth_fullwidth) , fc::halfwidth_fullwidth.end()
, [&c] (const std::array<wchar_t, 2>& entry)
{ {
if ( entry[HALF] == c ) // found return entry[HALF] == c;
c = entry[FULL]; } );
}
if ( found != fc::halfwidth_fullwidth.end() )
c = (*found)[FULL];
}; };
for (auto&& c : s) for (auto&& c : s)
@ -356,12 +360,15 @@ FString getHalfWidth (const FString& str)
{ {
constexpr std::size_t HALF = 0; constexpr std::size_t HALF = 0;
constexpr std::size_t FULL = 1; constexpr std::size_t FULL = 1;
auto found = std::find_if ( fc::halfwidth_fullwidth.begin()
for (auto&& entry : fc::halfwidth_fullwidth) , fc::halfwidth_fullwidth.end()
, [&c] (const std::array<wchar_t, 2>& entry)
{ {
if ( entry[FULL] == c ) // found return entry[FULL] == c;
c = entry[HALF]; } );
}
if ( found != fc::halfwidth_fullwidth.end() )
c = (*found)[HALF];
}; };
for (auto&& c : s) for (auto&& c : s)

View File

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

View File

@ -333,11 +333,11 @@ void FTextView::clear()
//---------------------------------------------------------------------- //----------------------------------------------------------------------
void FTextView::onKeyPress (FKeyEvent* ev) 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(); ev->accept();
} }
} }

View File

@ -465,8 +465,7 @@ int FVTerm::print (FTermArea* area, wchar_t c)
if ( ! area ) if ( ! area )
return -1; return -1;
FChar nc{}; // next character FChar nc = FVTerm::getAttribute(); // next character
nc = FVTerm::getAttribute();
nc.ch[0] = c; nc.ch[0] = c;
nc.attr.byte[2] = 0; nc.attr.byte[2] = 0;
nc.attr.byte[3] = 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); auto bottom_right = std::size_t((y_max * total_width) - area->right_shadow - 1);
const auto& lc = area->data[bottom_right]; // last character const auto& lc = area->data[bottom_right]; // last character
std::memcpy (&nc, &lc, sizeof(nc)); std::memcpy (&nc, &lc, sizeof(nc));
nc.ch[0] = ' '; nc.ch[0] = L' ';
auto& dc = area->data[y_max * total_width]; // destination character auto& dc = area->data[y_max * total_width]; // destination character
std::fill_n (&dc, area->width, nc); std::fill_n (&dc, area->width, nc);
area->changes[y_max].xmin = 0; area->changes[y_max].xmin = 0;
@ -1166,7 +1165,7 @@ void FVTerm::scrollAreaReverse (FTermArea* area) const
FChar nc{}; // next character FChar nc{}; // next character
const auto& lc = area->data[total_width]; // last character const auto& lc = area->data[total_width]; // last character
std::memcpy (&nc, &lc, sizeof(nc)); std::memcpy (&nc, &lc, sizeof(nc));
nc.ch[0] = ' '; nc.ch[0] = L' ';
auto& dc = area->data[0]; // destination character auto& dc = area->data[0]; // destination character
std::fill_n (&dc, area->width, nc); std::fill_n (&dc, area->width, nc);
area->changes[0].xmin = 0; area->changes[0].xmin = 0;
@ -1313,7 +1312,7 @@ inline void FVTerm::resetTextAreaToDefault ( const FTermArea* area
FChar default_char; FChar default_char;
FLineChanges unchanged; FLineChanges unchanged;
default_char.ch[0] = ' '; default_char.ch[0] = L' ';
default_char.fg_color = FColor::Default; default_char.fg_color = FColor::Default;
default_char.bg_color = FColor::Default; default_char.bg_color = FColor::Default;
default_char.attr.byte[0] = 0; 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::RightHalfBlock
|| nc.ch[0] == UniChar::MediumShade || nc.ch[0] == UniChar::MediumShade
|| nc.ch[0] == UniChar::FullBlock ) || 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); nc.attr.bit.no_changes = bool(vterm_char.attr.bit.printed && vterm_char == nc);
std::memcpy (&vterm_char, &nc, sizeof(vterm_char)); 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::RightHalfBlock
|| cover_char.ch[0] == UniChar::MediumShade || cover_char.ch[0] == UniChar::MediumShade
|| cover_char.ch[0] == UniChar::FullBlock ) || cover_char.ch[0] == UniChar::FullBlock )
cover_char.ch[0] = ' '; cover_char.ch[0] = L' ';
cover_char.attr.bit.no_changes = \ cover_char.attr.bit.no_changes = \
bool(vterm_char.attr.bit.printed && vterm_char == cover_char); 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::RightHalfBlock
|| s_ch.ch[0] == UniChar::MediumShade || s_ch.ch[0] == UniChar::MediumShade
|| s_ch.ch[0] == UniChar::FullBlock ) || s_ch.ch[0] == UniChar::FullBlock )
s_ch.ch[0] = ' '; s_ch.ch[0] = L' ';
sc = &s_ch; 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::RightHalfBlock
|| ch.ch[0] == UniChar::MediumShade || ch.ch[0] == UniChar::MediumShade
|| ch.ch[0] == UniChar::FullBlock ) || ch.ch[0] == UniChar::FullBlock )
ch.ch[0] = ' '; ch.ch[0] = L' ';
std::memcpy (&vterm_char, &ch, sizeof(vterm_char)); 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& ce = TCAP(t_clr_eol);
const auto& min_char = vt->data[y * uInt(vt->width) + xmin]; 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; uInt beginning_whitespace = 1;
const bool normal = FTerm::isNormal(min_char); 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& cb = TCAP(t_clr_bol);
const auto& first_char = vt->data[y * uInt(vt->width)]; 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; uInt leading_whitespace = 1;
const bool normal = FTerm::isNormal(first_char); 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& ce = TCAP(t_clr_eol);
const auto& last_char = vt->data[(y + 1) * uInt(vt->width) - 1]; 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; uInt trailing_whitespace = 1;
const bool normal = FTerm::isNormal(last_char); const bool normal = FTerm::isNormal(last_char);
@ -2299,7 +2298,7 @@ void FVTerm::printRange ( uInt xmin, uInt xmax, uInt y
continue; continue;
// Erase character // Erase character
if ( ec && print_char.ch[0] == ' ' ) if ( ec && print_char.ch[0] == L' ' )
{ {
PrintState erase_state = \ PrintState erase_state = \
eraseCharacters(x, xmax, y, draw_trailing_ws); 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); const auto& ec = TCAP(t_erase_chars);
auto& print_char = vt->data[y * uInt(vt->width) + x]; 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; return PrintState::NothingPrinted;
uInt whitespace{1}; uInt whitespace{1};
@ -3140,10 +3139,11 @@ void FVTerm::appendLowerRight (FChar& last_char) const
//---------------------------------------------------------------------- //----------------------------------------------------------------------
inline void FVTerm::characterFilter (FChar& next_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() ) if ( entry )
next_char.encoded_char[0] = sub_map[next_char.encoded_char[0]]; next_char.encoded_char[0] = entry;
} }
//---------------------------------------------------------------------- //----------------------------------------------------------------------

View File

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

View File

@ -801,9 +801,9 @@ enum class FKey : uInt32
struct FKeyHash 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 <cmath>
#include <csignal> #include <csignal>
#include <functional> #include <functional>
#include <map>
#include <memory> #include <memory>
#include <queue> #include <queue>
#include <utility> #include <utility>