Skip to content

Commit

Permalink
Reduce the complexity of some internal methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gansm committed Feb 22, 2024
1 parent 3463b53 commit bd42e90
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 101 deletions.
19 changes: 12 additions & 7 deletions final/dialog/fdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2012-2023 Markus Gans *
* Copyright 2012-2024 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
Expand Down Expand Up @@ -1568,6 +1568,13 @@ inline void FDialog::handleLeftMouseDown (const MouseStates& ms)
resizeMouseDown(ms);
}

//----------------------------------------------------------------------
inline auto FDialog::isClickOnTitleBar ( const MouseStates& ms
, int first, int width ) const -> bool
{
return ms.mouse_x >= first && ms.mouse_x <= width && ms.mouse_y == 1;
}

//----------------------------------------------------------------------
inline void FDialog::handleRightAndMiddleMouseDown ( const MouseButton& button
, const MouseStates& ms )
Expand All @@ -1581,14 +1588,12 @@ inline void FDialog::handleRightAndMiddleMouseDown ( const MouseButton& button
const auto first = int(getMenuButtonWidth() + 1);

// Click on titlebar: just activate
if ( button == MouseButton::Right
&& ms.mouse_x >= first && ms.mouse_x <= width && ms.mouse_y == 1 )
activateDialog();
if ( button == MouseButton::Right && isClickOnTitleBar(ms, first, width) )
activateDialog();

// Click on titlebar: lower + activate
if ( button == MouseButton::Middle
&& ms.mouse_x >= first && ms.mouse_x <= width && ms.mouse_y == 1 )
lowerActivateDialog();
if ( button == MouseButton::Middle && isClickOnTitleBar(ms, first, width) )
lowerActivateDialog();
}

//----------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion final/dialog/fdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2012-2023 Markus Gans *
* Copyright 2012-2024 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
Expand Down Expand Up @@ -264,6 +264,7 @@ class FDialog : public FWindow
void passEventToSubMenu ( const MouseStates&
, const FMouseEvent& );
void handleLeftMouseDown (const MouseStates&);
auto isClickOnTitleBar (const MouseStates&, int, int) const -> bool;
void handleRightAndMiddleMouseDown (const MouseButton&, const MouseStates&);
void moveSizeKey (FKeyEvent*);
void raiseActivateDialog();
Expand Down
22 changes: 19 additions & 3 deletions final/dialog/fmessagebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2014-2023 Markus Gans *
* Copyright 2014-2024 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
Expand Down Expand Up @@ -356,10 +356,9 @@ void FMessageBox::draw()
}

//----------------------------------------------------------------------
void FMessageBox::resizeButtons() const
inline auto FMessageBox::getButtonLengthArray() const -> std::array<std::size_t, MAX_BUTTONS>
{
std::array<std::size_t, MAX_BUTTONS> len{};
std::size_t max_size{};

for (std::size_t n{0}; n < num_buttons && n < MAX_BUTTONS; n++)
{
Expand All @@ -372,6 +371,15 @@ void FMessageBox::resizeButtons() const
len[n]--;
}

return len;
}

//----------------------------------------------------------------------
inline auto FMessageBox::calculateMaxButtonSize() const -> std::size_t
{
std::size_t max_size{};
auto len = getButtonLengthArray();

if ( num_buttons < 2 )
max_size = len[0];
else
Expand All @@ -385,6 +393,14 @@ void FMessageBox::resizeButtons() const
if ( max_size < 7 )
max_size = 7;

return max_size;
}

//----------------------------------------------------------------------
void FMessageBox::resizeButtons() const
{
auto max_size = calculateMaxButtonSize();

for (std::size_t n{0}; n < num_buttons && n < MAX_BUTTONS; n++)
if ( button[n] )
button[n]->setWidth(max_size + 3, false);
Expand Down
4 changes: 3 additions & 1 deletion final/dialog/fmessagebox.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* *
* This file is part of the FINAL CUT widget toolkit *
* *
* Copyright 2014-2023 Markus Gans *
* Copyright 2014-2024 Markus Gans *
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
Expand Down Expand Up @@ -154,6 +154,8 @@ class FMessageBox : public FDialog
void initCallbacks();
void calculateDimensions();
void draw() override;
auto getButtonLengthArray() const -> std::array<std::size_t, MAX_BUTTONS>;
auto calculateMaxButtonSize() const -> std::size_t;
void resizeButtons() const;
void adjustButtons();

Expand Down
5 changes: 5 additions & 0 deletions final/font/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

Simply start your program with the parameter `--newfont`.

* [newfont-9x16](../../doc/newfont-9x16.txt)
* [newfont-8x16](../../doc/newfont-8x16.txt)


### How can I use the new graphical font under X11?

The use of the new graphical font under X11 is only possible with
Expand All @@ -17,6 +21,7 @@ because only these terminal emulators can change the font by
using an escape sequence. For an xterm, the "[Allow Font Ops](../../doc/xterm.txt)"
option must be set.


#### Install the gzip compressed X11 pcf bitmap font 8x16graph.pcf.gz on your xserver:

1. Create as user root the font directory<br />
Expand Down
4 changes: 2 additions & 2 deletions final/ftypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,14 @@ constexpr auto isFUnicodeEqual (const FUnicode& lhs, const FUnicode& rhs) noexce
return false;

// Perform a byte-wise comparison
return std::memcmp(lhs.data(), rhs.data(), lhs.size() * sizeof(wchar_t)) == 0;
return std::wmemcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
}
#else
inline auto isFUnicodeEqual (const FUnicode& lhs, const FUnicode& rhs) noexcept -> bool
{
static_assert ( sizeof(lhs) == sizeof(rhs) , "Both sides are different sizes.");
// Perform a byte-wise comparison
return std::memcmp(lhs.data(), rhs.data(), lhs.size() * sizeof(wchar_t)) == 0;
return std::wmemcmp(lhs.data(), rhs.data(), lhs.size()) == 0;
}
#endif

Expand Down
61 changes: 31 additions & 30 deletions final/output/tty/ftermoutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -961,16 +961,16 @@ auto FTermOutput::eraseCharacters (uInt& x, uInt xmax, uInt y) -> PrintState
// Erase a number of characters to draw simple whitespaces

const auto& ec = TCAP(t_erase_chars);
auto* print_char = &vterm->getFChar(int(x), int(y));
auto& print_char = vterm->getFChar(static_cast<int>(x), static_cast<int>(y));

if ( ! ec || print_char->ch[0] != L' ' )
if ( ! ec || print_char.ch[0] != L' ' )
return PrintState::NothingPrinted;

auto whitespace = countRepetitions(print_char, x, xmax);
const auto whitespace = countRepetitions(&print_char, x, xmax);

if ( whitespace == 1 )
{
appendCharacter (*print_char);
appendCharacter (print_char);
markAsPrinted (x, y);
return PrintState::WhitespacesPrinted;
}
Expand All @@ -980,16 +980,16 @@ auto FTermOutput::eraseCharacters (uInt& x, uInt xmax, uInt y) -> PrintState

if ( canUseEraseCharacters(print_char, whitespace) )
{
appendAttributes (*print_char);
appendAttributes (print_char);
appendOutputBuffer (FTermControl{FTermcap::encodeParameter(ec, whitespace)});

if ( end_pos <= xmax )
setCursor (FPoint{int(x + whitespace), int(y)});
setCursor (FPoint{static_cast<int>(x + whitespace), static_cast<int>(y)});
else
return PrintState::LineCompletelyPrinted;
}
else
appendCharacter_n (*print_char, whitespace);
appendCharacter_n (print_char, whitespace);

markAsPrinted (start_pos, end_pos, y);
x = end_pos;
Expand All @@ -1003,44 +1003,44 @@ auto FTermOutput::repeatCharacter (uInt& x, uInt xmax, uInt y) -> PrintState

const auto& rp = TCAP(t_repeat_char);
const auto& lr = TCAP(t_repeat_last_char);
auto* print_char = &vterm->getFChar(int(x), int(y));
auto& print_char = vterm->getFChar(static_cast<int>(x), static_cast<int>(y));

if ( ! rp && ! lr )
return PrintState::NothingPrinted;

auto repetitions = countRepetitions(print_char, x, xmax);
const auto repetitions = countRepetitions(&print_char, x, xmax);

if ( repetitions == 1 )
{
bool min_and_not_max( x != xmax );
printCharacter (x, y, min_and_not_max, *print_char);
printCharacter (x, y, min_and_not_max, print_char);
return PrintState::RepeatCharacterPrinted;
}

// Every full-width character is always followed by a padding
// character, so that two or more consecutive full-width characters
// cannot repeat in their byte sequence
const uInt start_pos = x;
const uInt end_pos = x + repetitions - 1;
const auto repetition_type = getRepetitionType(print_char, repetitions);

if ( rp && repetition_type == Repetition::ASCII )
{
newFontChanges (*print_char);
charsetChanges (*print_char);
appendAttributes (*print_char);
appendOutputBuffer (FTermControl{FTermcap::encodeParameter(rp, print_char->ch[0], repetitions)});
newFontChanges (print_char);
charsetChanges (print_char);
appendAttributes (print_char);
appendOutputBuffer (FTermControl{FTermcap::encodeParameter(rp, print_char.ch[0], repetitions)});
term_pos->x_ref() += static_cast<int>(repetitions);
}
else if ( lr && repetition_type == Repetition::UTF8 )
{
appendChar (*print_char);
appendChar (print_char);
appendOutputBuffer (FTermControl{FTermcap::encodeParameter(lr, repetitions)});
term_pos->x_ref() += static_cast<int>(repetitions);
}
else
appendCharacter_n (*print_char, repetitions);
appendCharacter_n (print_char, repetitions);

const uInt start_pos = x;
const uInt end_pos = x + repetitions - 1;
markAsPrinted (start_pos, end_pos, y);
x = end_pos;
return PrintState::RepeatCharacterPrinted;
Expand All @@ -1051,26 +1051,27 @@ inline auto FTermOutput::countRepetitions ( const FChar* print_char
, uInt from, uInt to ) const -> uInt
{
uInt repetitions{1};
const auto* ch = print_char + 1;
auto first = print_char + 1;
auto last = print_char + to - from + 1;

for (uInt i{from + 1}; i <= to; i++)
while ( first != last )
{
if ( *print_char == *ch )
if ( *first == *print_char )
repetitions++;
else
break;

++ch;
++first;
}

return repetitions;
}

//----------------------------------------------------------------------
inline auto FTermOutput::canUseEraseCharacters ( const FChar* print_char
inline auto FTermOutput::canUseEraseCharacters ( const FChar& print_char
, uInt whitespace ) const -> bool
{
const auto normal = FOptiAttr::isNormal(*print_char);
const auto normal = FOptiAttr::isNormal(print_char);
const auto ut = FTermcap::background_color_erase;
const uInt total_length_required = erase_char_length
+ cursor_address_length;
Expand All @@ -1079,23 +1080,23 @@ inline auto FTermOutput::canUseEraseCharacters ( const FChar* print_char
}

//----------------------------------------------------------------------
inline auto FTermOutput::canUseCharacterRepetitions ( const FChar* print_char
inline auto FTermOutput::canUseCharacterRepetitions ( const FChar& print_char
, uInt repetitions ) const -> bool
{
return repetitions > repeat_char_length
&& print_char->ch[0] != L'\0' && print_char->ch[1] == L'\0';
&& print_char.ch[0] != L'\0' && print_char.ch[1] == L'\0';
}

//----------------------------------------------------------------------
inline auto FTermOutput::getRepetitionType ( const FChar* print_char
inline auto FTermOutput::getRepetitionType ( const FChar& print_char
, uInt repetitions ) const -> Repetition
{
if ( canUseCharacterRepetitions(print_char, repetitions) )
{
if ( is7bit(print_char->ch[0]) )
if ( is7bit(print_char.ch[0]) )
return Repetition::ASCII;

if ( isPrintable(print_char->ch[0]) )
if ( isPrintable(print_char.ch[0]) )
return Repetition::UTF8;
}

Expand Down Expand Up @@ -1290,7 +1291,7 @@ inline void FTermOutput::markAsPrinted (uInt from, uInt to, uInt y) const
const auto* end = ch + to - from + 1;

// Unroll the loop for better performance
while (ch + 4 <= end)
while ( ch + 4 <= end )
{
ch[0].attr.bit.printed = true;
ch[1].attr.bit.printed = true;
Expand Down
6 changes: 3 additions & 3 deletions final/output/tty/ftermoutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ class FTermOutput final : public FOutput
auto eraseCharacters (uInt&, uInt, uInt) -> PrintState;
auto repeatCharacter (uInt&, uInt, uInt) -> PrintState;
auto countRepetitions (const FChar*, uInt, uInt) const -> uInt;
auto canUseEraseCharacters (const FChar*, uInt) const -> bool;
auto canUseCharacterRepetitions (const FChar*, uInt) const -> bool;
auto getRepetitionType (const FChar*, uInt) const -> Repetition;
auto canUseEraseCharacters (const FChar&, uInt) const -> bool;
auto canUseCharacterRepetitions (const FChar&, uInt) const -> bool;
auto getRepetitionType (const FChar&, uInt) const -> Repetition;
auto isFullWidthChar (const FChar&) const -> bool;
auto isFullWidthPaddingChar (const FChar&) const -> bool;
void cursorWrap() const;
Expand Down
Loading

0 comments on commit bd42e90

Please sign in to comment.