finalcut/examples/term-attributes.cpp

514 lines
14 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* term-attributes.cpp - Test the video attributes of the terminal *
* *
* This file is part of the FINAL CUT widget toolkit *
2017-11-04 07:03:53 +01:00
* *
* Copyright 2015-2021 Markus Gans *
2017-11-04 07:03:53 +01:00
* *
* FINAL CUT is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 3 of *
2017-11-04 07:03:53 +01:00
* the License, or (at your option) any later version. *
* *
* FINAL CUT is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
2017-11-04 07:03:53 +01:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this program. If not, see *
* <http://www.gnu.org/licenses/>. *
***********************************************************************/
#include <functional>
2019-08-06 23:45:28 +02:00
#include <vector>
#include <final/final.h>
using finalcut::FPoint;
using finalcut::FSize;
using finalcut::FColorPair;
2020-12-31 20:45:10 +01:00
using finalcut::FColor;
//----------------------------------------------------------------------
// class AttribDlg
//----------------------------------------------------------------------
2020-04-14 23:46:42 +02:00
class AttribDlg final : public finalcut::FDialog
{
2017-09-11 03:06:02 +02:00
public:
// Constructor
explicit AttribDlg (finalcut::FWidget* = nullptr);
// Disable copy constructor
AttribDlg (const AttribDlg&) = delete;
2017-09-11 03:06:02 +02:00
// Destructor
2020-11-24 21:06:39 +01:00
~AttribDlg() override = default;
// Disable copy assignment operator (=)
AttribDlg& operator = (const AttribDlg&) = delete;
2020-04-13 12:40:11 +02:00
// Methods
2020-07-12 15:25:21 +02:00
FColor getBGColor() const;
2020-04-13 12:40:11 +02:00
2017-09-11 03:06:02 +02:00
// Event handlers
2019-08-06 23:45:28 +02:00
void onKeyPress (finalcut::FKeyEvent*) override;
void onWheel (finalcut::FWheelEvent*) override;
void onClose (finalcut::FCloseEvent*) override;
2017-09-11 03:06:02 +02:00
// Callback methods
2020-08-11 23:04:46 +02:00
void cb_next();
void cb_back();
2017-09-11 03:06:02 +02:00
private:
// Method
void initLayout() override;
2019-08-06 23:45:28 +02:00
void adjustSize() override;
2020-06-06 21:10:06 +02:00
void draw() override;
// Data members
2020-12-31 20:45:10 +01:00
FColor bgcolor{FColor::Undefined};
finalcut::FButton next_button{"&Next >", this};
finalcut::FButton back_button{"< &Back", this};
};
//----------------------------------------------------------------------
AttribDlg::AttribDlg (finalcut::FWidget* parent)
: finalcut::FDialog{parent}
{
2020-12-31 20:45:10 +01:00
next_button.addAccelerator (finalcut::FKey::Right);
back_button.addAccelerator (finalcut::FKey::Left);
// Add function callbacks
next_button.addCallback
(
"clicked",
2020-08-11 23:04:46 +02:00
this, &AttribDlg::cb_next
);
back_button.addCallback
(
"clicked",
2020-08-11 23:04:46 +02:00
this, &AttribDlg::cb_back
);
}
2020-04-13 12:40:11 +02:00
//----------------------------------------------------------------------
2020-07-12 15:25:21 +02:00
FColor AttribDlg::getBGColor() const
2020-04-13 12:40:11 +02:00
{
return bgcolor;
}
//----------------------------------------------------------------------
void AttribDlg::onKeyPress (finalcut::FKeyEvent* ev)
{
if ( ! ev )
return;
2020-12-31 20:45:10 +01:00
if ( ev->key() == finalcut::FKey('q') )
{
close();
ev->accept();
}
else
finalcut::FDialog::onKeyPress(ev);
}
2016-01-10 00:56:52 +01:00
//----------------------------------------------------------------------
void AttribDlg::onWheel (finalcut::FWheelEvent* ev)
2016-01-10 00:56:52 +01:00
{
2020-12-31 20:45:10 +01:00
const finalcut::MouseWheel wheel = ev->getWheel();
2016-01-10 00:56:52 +01:00
2020-12-31 20:45:10 +01:00
if ( wheel == finalcut::MouseWheel::Up )
2016-01-10 00:56:52 +01:00
cb_next();
2020-12-31 20:45:10 +01:00
else if ( wheel == finalcut::MouseWheel::Down )
2016-01-10 00:56:52 +01:00
cb_back();
}
//----------------------------------------------------------------------
void AttribDlg::onClose (finalcut::FCloseEvent* ev)
{
finalcut::FApplication::closeConfirmationDialog (this, ev);
}
//----------------------------------------------------------------------
2020-08-11 23:04:46 +02:00
void AttribDlg::cb_next()
{
if ( finalcut::FTerm::isMonochron() )
return;
if ( bgcolor == FColor(finalcut::FTerm::getMaxColor() - 1) )
2020-12-31 20:45:10 +01:00
bgcolor = FColor::Default;
else if ( bgcolor == FColor::Default )
bgcolor = FColor::Black;
2018-11-07 22:06:58 +01:00
else
2021-04-05 21:20:02 +02:00
++bgcolor;
redraw();
}
//----------------------------------------------------------------------
2020-08-11 23:04:46 +02:00
void AttribDlg::cb_back()
{
if ( finalcut::FTerm::isMonochron() )
return;
2018-11-07 22:06:58 +01:00
if ( bgcolor == 0 )
2020-12-31 20:45:10 +01:00
bgcolor = FColor::Default;
else if ( bgcolor == FColor::Default )
bgcolor = FColor(finalcut::FTerm::getMaxColor() - 1);
2018-11-07 22:06:58 +01:00
else
2021-04-05 21:20:02 +02:00
--bgcolor;
redraw();
}
//----------------------------------------------------------------------
void AttribDlg::initLayout()
{
next_button.setGeometry ( FPoint{int(getWidth()) - 13, int(getHeight()) - 4}
, FSize{10, 1} );
back_button.setGeometry ( FPoint{int(getWidth()) - 25, int(getHeight()) - 4}
, FSize{10, 1} );
FDialog::initLayout();
}
//----------------------------------------------------------------------
void AttribDlg::adjustSize()
{
2020-10-04 02:55:15 +02:00
auto x = int((getDesktopWidth() - getWidth()) / 2);
auto y = int((getDesktopHeight() - getHeight()) / 2) + 1;
2016-01-17 02:57:08 +01:00
if ( x < 1 )
x = 1;
2016-01-17 02:57:08 +01:00
if ( y < 1 )
y = 1;
setGeometry(FPoint{x, y}, FSize{69, 21}, false);
next_button.setGeometry ( FPoint{int(getWidth()) - 13, int(getHeight()) - 4}
, FSize{10, 1}, false );
back_button.setGeometry ( FPoint{int(getWidth()) - 25, int(getHeight()) - 4}
, FSize{10, 1}, false );
finalcut::FDialog::adjustSize();
}
2020-06-06 21:10:06 +02:00
//----------------------------------------------------------------------
void AttribDlg::draw()
{
2020-12-31 20:45:10 +01:00
if ( bgcolor == FColor::Undefined )
2020-06-06 21:10:06 +02:00
{
// Get the color after initializing the color theme in show()
if ( finalcut::FTerm::isMonochron() )
2020-12-31 20:45:10 +01:00
bgcolor = FColor::Default;
2020-06-06 21:10:06 +02:00
else
bgcolor = getColorTheme()->label_bg;
// Get the terminal type after the terminal detection in show()
FDialog::setText ( "A terminal attributes test ("
+ finalcut::FString{finalcut::FTerm::getTermType()}
+ ")");
}
FDialog::draw();
}
//----------------------------------------------------------------------
// class AttribDemo
//----------------------------------------------------------------------
2020-04-14 23:46:42 +02:00
class AttribDemo final : public finalcut::FWidget
{
2017-09-11 03:06:02 +02:00
public:
// Constructor
explicit AttribDemo (FWidget* = nullptr);
2017-09-11 03:06:02 +02:00
// Destructor
2020-11-24 21:06:39 +01:00
~AttribDemo() override = default;
2017-09-11 03:06:02 +02:00
// Event handler
2019-08-06 23:45:28 +02:00
void onWheel (finalcut::FWheelEvent* ev) override
2017-09-11 03:06:02 +02:00
{
auto p = static_cast<AttribDlg*>(getParentWidget());
2017-09-11 03:06:02 +02:00
if ( p )
p->onWheel(ev);
}
private:
// Methods
void printColorLine();
void printAltCharset();
2017-11-24 00:28:25 +01:00
void printDim();
void printNormal();
void printBold();
void printBoldDim();
void printItalic();
void printUnderline();
void printDblUnderline();
void printCrossesOut();
void printBlink();
void printReverse();
void printStandout();
void printInvisible();
void printProtected();
2019-08-06 23:45:28 +02:00
void draw() override;
2017-09-11 03:06:02 +02:00
// Data member
2020-12-31 20:45:10 +01:00
FColor last_color{FColor::Blue};
};
//----------------------------------------------------------------------
AttribDemo::AttribDemo (finalcut::FWidget* parent)
: finalcut::FWidget{parent}
{
unsetFocusable();
}
//----------------------------------------------------------------------
void AttribDemo::printColorLine()
{
const auto& parent = static_cast<AttribDlg*>(getParent());
2021-04-05 21:20:02 +02:00
for (FColor color{FColor::Black}; color < last_color; ++color)
{
print() << FColorPair{color, parent->getBGColor()} << " # ";
}
}
//----------------------------------------------------------------------
void AttribDemo::printAltCharset()
{
const auto& wc = getColorTheme();
const auto& parent = static_cast<AttribDlg*>(getParent());
if ( ! finalcut::FTerm::isMonochron() )
setColor (wc->label_fg, wc->label_bg);
print() << FPoint{1, 1} << "Alternate charset: ";
2020-12-31 20:45:10 +01:00
if ( parent->getBGColor() == FColor::Default )
{
2020-12-31 20:45:10 +01:00
setColor (FColor::Default, FColor::Default);
}
else
{
2020-04-13 12:40:11 +02:00
if ( (parent->getBGColor() <= 8)
|| (parent->getBGColor() >= 16 && parent->getBGColor() <= 231
&& (parent->getBGColor() - 16) % 36 <= 17)
|| (parent->getBGColor() >= 232 && parent->getBGColor() <= 243) )
2020-12-31 20:45:10 +01:00
setColor (FColor::White, parent->getBGColor());
else
2020-12-31 20:45:10 +01:00
setColor (FColor::Black, parent->getBGColor());
}
setAltCharset();
print("`abcdefghijklmnopqrstuvwxyz{|}~");
unsetAltCharset();
print(" ");
}
2017-11-24 00:28:25 +01:00
//----------------------------------------------------------------------
void AttribDemo::printDim()
{
print(" Dim: ");
setDim();
printColorLine();
unsetDim();
}
//----------------------------------------------------------------------
void AttribDemo::printNormal()
{
print(" Normal: ");
setNormal();
printColorLine();
}
//----------------------------------------------------------------------
void AttribDemo::printBold()
{
print(" Bold: ");
setBold();
printColorLine();
unsetBold();
}
//----------------------------------------------------------------------
void AttribDemo::printBoldDim()
{
print(" Bold+Dim: ");
setBold();
setDim();
printColorLine();
unsetDim();
unsetBold();
}
//----------------------------------------------------------------------
void AttribDemo::printItalic()
{
print(" Italic: ");
setItalic();
printColorLine();
unsetItalic();
}
//----------------------------------------------------------------------
void AttribDemo::printUnderline()
{
print(" Underline: ");
setUnderline();
printColorLine();
unsetUnderline();
}
//----------------------------------------------------------------------
void AttribDemo::printDblUnderline()
{
print(" Double underline: ");
setDoubleUnderline();
printColorLine();
unsetDoubleUnderline();
}
//----------------------------------------------------------------------
void AttribDemo::printCrossesOut()
{
print(" Crossed-out: ");
setCrossedOut();
printColorLine();
unsetCrossedOut();
}
//----------------------------------------------------------------------
void AttribDemo::printBlink()
{
print(" Blink: ");
setBlink();
printColorLine();
unsetBlink();
}
//----------------------------------------------------------------------
void AttribDemo::printReverse()
{
print(" Reverse: ");
setReverse();
printColorLine();
unsetReverse();
}
//----------------------------------------------------------------------
void AttribDemo::printStandout()
{
print(" Standout: ");
setStandout();
printColorLine();
unsetStandout();
}
//----------------------------------------------------------------------
void AttribDemo::printInvisible()
{
print(" Invisible: ");
setInvisible();
printColorLine();
unsetInvisible();
}
//----------------------------------------------------------------------
void AttribDemo::printProtected()
{
print(" Protected: ");
setProtected();
printColorLine();
unsetProtected();
}
//----------------------------------------------------------------------
void AttribDemo::draw()
{
const auto& wc = getColorTheme();
2020-06-06 21:10:06 +02:00
last_color = FColor(finalcut::FTerm::getMaxColor());
if ( finalcut::FTerm::isMonochron() )
2020-12-31 20:45:10 +01:00
last_color = FColor(1);
2020-06-06 21:10:06 +02:00
else if ( last_color > 16 )
2020-12-31 20:45:10 +01:00
last_color = FColor(16);
2020-06-06 21:10:06 +02:00
// test alternate character set
printAltCharset();
const std::vector<std::function<void()> > effect
{
2020-04-14 23:46:42 +02:00
[this] { printNormal(); },
[this] { printBold(); },
[this] { printBoldDim(); },
[this] { printItalic(); },
[this] { printUnderline(); },
[this] { printDblUnderline(); },
[this] { printCrossesOut(); },
[this] { printBlink(); },
[this] { printReverse(); },
[this] { printStandout(); },
[this] { printInvisible(); },
[this] { printProtected(); },
};
2019-08-25 22:16:00 +02:00
for (std::size_t y{0}; y < getParentWidget()->getHeight() - 7; y++)
{
print() << FPoint{1, 2 + int(y)};
if ( ! finalcut::FTerm::isMonochron() )
setColor (wc->label_fg, wc->label_bg);
if ( y < effect.size() )
effect[y]();
}
if ( ! finalcut::FTerm::isMonochron() )
setColor(wc->label_fg, wc->label_bg);
print() << FPoint{1, 15};
2020-04-13 12:40:11 +02:00
const FColor bg = static_cast<AttribDlg*>(getParent())->getBGColor();
2016-01-17 02:57:08 +01:00
print (" Background color:");
2020-12-31 20:45:10 +01:00
if ( bg == FColor::Default )
print (" default");
2016-01-17 02:57:08 +01:00
else
2021-04-05 21:20:02 +02:00
printf ( " %u", bg);
print() << FPoint{16, 17} << "Change background color ->";
}
2016-01-10 00:56:52 +01:00
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
// Create the application object
finalcut::FApplication app {argc, argv};
// Create a dialog box object.
// This object will be automatically deleted by
// the parent object "app" (FObject destructor).
AttribDlg dialog{&app};
dialog.setSize (FSize{69, 21});
dialog.setShadow(); // Instead of the transparent window shadow
// Create the attribute demo widget as a child object from the dialog
AttribDemo demo(&dialog);
demo.setGeometry (FPoint{1, 1}, FSize{67, 19});
// Set the dialog object as main widget
2020-04-13 12:40:11 +02:00
finalcut::FWidget::setMainWidget(&dialog);
// Show and start the application
dialog.show();
return app.exec();
}