finalcut/examples/term-attributes.cpp

496 lines
12 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 *
* *
* Copyright 2015-2019 Markus Gans *
2017-11-04 07:03:53 +01:00
* *
* The 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 *
* the License, or (at your option) any later version. *
* *
* The Final Cut is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* 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>
namespace fc = finalcut::fc;
using finalcut::FPoint;
using finalcut::FSize;
using finalcut::FColorPair;
//----------------------------------------------------------------------
// class AttribDlg
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
class AttribDlg : 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
~AttribDlg();
// Disable assignment operator (=)
AttribDlg& operator = (const AttribDlg&) = delete;
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
2018-12-27 00:14:46 +01:00
void cb_next (finalcut::FWidget* = nullptr, FDataPtr = nullptr);
void cb_back (finalcut::FWidget* = nullptr, FDataPtr = nullptr);
2017-09-11 03:06:02 +02:00
// Data Members
2018-11-07 22:06:58 +01:00
FColor bgcolor;
2017-09-11 03:06:02 +02:00
private:
// Method
2019-08-06 23:45:28 +02:00
void adjustSize() override;
2017-09-11 03:06:02 +02:00
// Data Members
finalcut::FButton next_button{"&Next >", this};
finalcut::FButton back_button{"< &Back", this};
};
#pragma pack(pop)
//----------------------------------------------------------------------
AttribDlg::AttribDlg (finalcut::FWidget* parent)
: finalcut::FDialog(parent)
, bgcolor(wc.label_bg)
{
setText ( "A terminal attributes test ("
+ finalcut::FString(getTermType())
+ ")");
next_button.setGeometry ( FPoint(int(getWidth()) - 13, int(getHeight()) - 4)
, FSize(10, 1) );
next_button.addAccelerator (fc::Fkey_right);
back_button.setGeometry ( FPoint(int(getWidth()) - 25, int(getHeight()) - 4)
, FSize(10, 1) );
back_button.addAccelerator (fc::Fkey_left);
// Add function callbacks
next_button.addCallback
(
"clicked",
F_METHOD_CALLBACK (this, &AttribDlg::cb_next)
);
back_button.addCallback
(
"clicked",
F_METHOD_CALLBACK (this, &AttribDlg::cb_back)
);
}
//----------------------------------------------------------------------
AttribDlg::~AttribDlg()
{ }
//----------------------------------------------------------------------
void AttribDlg::onKeyPress (finalcut::FKeyEvent* ev)
{
if ( ! ev )
return;
if ( ev->key() == '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
{
int wheel = ev->getWheel();
if ( wheel == fc::WheelUp )
2016-01-10 00:56:52 +01:00
cb_next();
else if ( wheel == fc::WheelDown )
2016-01-10 00:56:52 +01:00
cb_back();
}
//----------------------------------------------------------------------
void AttribDlg::onClose (finalcut::FCloseEvent* ev)
{
finalcut::FApplication::closeConfirmationDialog (this, ev);
}
//----------------------------------------------------------------------
2018-12-27 00:14:46 +01:00
void AttribDlg::cb_next (finalcut::FWidget*, FDataPtr)
{
if ( isMonochron() )
return;
2018-11-07 22:06:58 +01:00
if ( bgcolor == FColor(getMaxColor() - 1) )
bgcolor = fc::Default;
else if ( bgcolor == fc::Default )
2018-11-07 22:06:58 +01:00
bgcolor = 0;
else
bgcolor++;
redraw();
}
//----------------------------------------------------------------------
2018-12-27 00:14:46 +01:00
void AttribDlg::cb_back (finalcut::FWidget*, FDataPtr)
{
if ( isMonochron() )
return;
2018-11-07 22:06:58 +01:00
if ( bgcolor == 0 )
bgcolor = fc::Default;
else if ( bgcolor == fc::Default )
2018-11-07 22:06:58 +01:00
bgcolor = FColor(getMaxColor() - 1);
else
bgcolor--;
redraw();
}
//----------------------------------------------------------------------
void AttribDlg::adjustSize()
{
int x = int((getDesktopWidth() - getWidth()) / 2);
int 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();
}
//----------------------------------------------------------------------
// class AttribDemo
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
class AttribDemo : 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
~AttribDemo()
{ }
// 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
int colors;
};
#pragma pack(pop)
//----------------------------------------------------------------------
AttribDemo::AttribDemo (finalcut::FWidget* parent)
: finalcut::FWidget(parent)
2017-09-11 03:06:02 +02:00
, colors(getMaxColor())
{
if ( isMonochron() )
colors = 1;
else if ( colors > 16 )
colors = 16;
unsetFocusable();
}
//----------------------------------------------------------------------
void AttribDemo::printColorLine()
{
auto parent = static_cast<AttribDlg*>(getParent());
2019-08-25 22:16:00 +02:00
for (FColor color{0}; color < colors; color++)
{
print() << FColorPair(color, parent->bgcolor) << " # ";
}
}
//----------------------------------------------------------------------
void AttribDemo::printAltCharset()
{
auto parent = static_cast<AttribDlg*>(getParent());
if ( ! isMonochron() )
setColor (wc.label_fg, wc.label_bg);
print() << FPoint(1, 1) << "alternate charset: ";
if ( parent->bgcolor == fc::Default )
{
setColor (fc::Default, fc::Default);
}
else
{
if ( (parent->bgcolor <= 8)
|| (parent->bgcolor >= 16 && parent->bgcolor <= 231
&& (parent->bgcolor - 16) % 36 <= 17)
|| (parent->bgcolor >= 232 && parent->bgcolor <= 243) )
setColor (fc::White, parent->bgcolor);
else
setColor (fc::Black, parent->bgcolor);
}
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()
{
// test alternate character set
printAltCharset();
std::vector<std::function<void()> > effect
{
[&] { printDim(); },
[&] { printNormal(); },
[&] { printBold(); },
[&] { printBoldDim(); },
[&] { printItalic(); },
[&] { printUnderline(); },
[&] { printDblUnderline(); },
[&] { printCrossesOut(); },
[&] { printBlink(); },
[&] { printReverse(); },
[&] { printStandout(); },
[&] { printInvisible(); },
[&] { 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 ( ! isMonochron() )
setColor (wc.label_fg, wc.label_bg);
if ( y < effect.size() )
effect[y]();
}
if ( ! isMonochron() )
setColor(wc.label_fg, wc.label_bg);
print() << FPoint(1, 15);
2018-11-07 22:06:58 +01:00
FColor bg = static_cast<AttribDlg*>(getParent())->bgcolor;
2016-01-17 02:57:08 +01:00
print (" Background color:");
if ( bg == fc::Default )
print (" default");
2016-01-17 02:57:08 +01:00
else
printf ( " %d", 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.setGeometry (FPoint(6, 2), FSize(69, 21));
dialog.setShadow();
// 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
app.setMainWidget(&dialog);
// Show and start the application
dialog.show();
return app.exec();
}