finalcut/examples/transparent.cpp

304 lines
8.1 KiB
C++
Raw Normal View History

2017-11-04 07:03:53 +01:00
/***********************************************************************
* transparent.cpp - Demonstrates transparent windows *
* *
* This file is part of the Final Cut widget toolkit *
* *
2018-09-02 03:57:57 +02:00
* Copyright 2016-2018 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/>. *
***********************************************************************/
2016-08-21 21:29:04 +02:00
#include <final/final.h>
2016-08-21 21:29:04 +02:00
//----------------------------------------------------------------------
// class Transparent
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
class Transparent : public finalcut::FDialog
2016-08-21 21:29:04 +02:00
{
2017-09-11 03:06:02 +02:00
public:
// Typedef and Enumeration
typedef enum ttype
{
transparent = 0,
shadow = 1,
inherit_background = 2
} trans_type;
2017-09-11 03:06:02 +02:00
// Constructor
explicit Transparent (finalcut::FWidget* = 0, trans_type = transparent);
2017-09-11 03:06:02 +02:00
// Destructor
~Transparent();
2016-08-21 21:29:04 +02:00
2017-09-11 03:06:02 +02:00
private:
// Disable copy constructor
Transparent (const Transparent&);
2017-09-11 03:06:02 +02:00
// Disable assignment operator (=)
Transparent& operator = (const Transparent&);
2017-09-11 03:06:02 +02:00
// Method
2018-09-24 04:02:35 +02:00
virtual void draw();
2017-09-11 03:06:02 +02:00
// Event handlers
2018-09-24 04:02:35 +02:00
virtual void onKeyPress (finalcut::FKeyEvent* ev);
2016-08-21 21:29:04 +02:00
2017-09-11 03:06:02 +02:00
// Data Members
trans_type type;
2016-08-21 21:29:04 +02:00
};
#pragma pack(pop)
//----------------------------------------------------------------------
Transparent::Transparent ( finalcut::FWidget* parent
, Transparent::trans_type tt )
: finalcut::FDialog(parent)
, type(tt)
2016-08-21 21:29:04 +02:00
{
setStatusbarMessage("Press Q to quit");
}
//----------------------------------------------------------------------
Transparent::~Transparent()
{ }
//----------------------------------------------------------------------
void Transparent::draw()
{
finalcut::FDialog::draw();
2016-08-21 21:29:04 +02:00
if ( isMonochron() )
setReverse(true);
if ( type == shadow )
2016-08-21 21:29:04 +02:00
{
setColor(wc.shadow_bg, wc.shadow_fg);
setTransShadow();
}
else if ( type == inherit_background )
{
if ( getMaxColor() > 8 )
setColor(finalcut::fc::Blue, finalcut::fc::Black);
else
setColor(finalcut::fc::Green, finalcut::fc::Black);
setInheritBackground();
}
2016-08-21 21:29:04 +02:00
else
setTransparent();
finalcut::FString line(getClientWidth(), wchar_t('.'));
2016-08-21 21:29:04 +02:00
for (int n = 1; n <= int(getClientHeight()); n++)
2016-08-21 21:29:04 +02:00
{
setPrintPos (2, 2 + n);
2016-08-21 21:29:04 +02:00
print(line);
}
if ( type == shadow )
2016-08-21 21:29:04 +02:00
unsetTransShadow();
else if ( type == inherit_background )
unsetInheritBackground();
2016-08-21 21:29:04 +02:00
else
unsetTransparent();
if ( isMonochron() )
setReverse(false);
}
//----------------------------------------------------------------------
void Transparent::onKeyPress (finalcut::FKeyEvent* ev)
{
if ( ! ev )
return;
if ( ev->key() == 'q' && getParentWidget() )
{
if ( getParentWidget()->close() )
ev->accept();
else
ev->ignore();
}
else
finalcut::FDialog::onKeyPress(ev);
}
2016-08-21 21:29:04 +02:00
//----------------------------------------------------------------------
// class MainWindow
//----------------------------------------------------------------------
#pragma pack(push)
#pragma pack(1)
class MainWindow : public finalcut::FDialog
2016-08-21 21:29:04 +02:00
{
public:
// Constructor
explicit MainWindow (finalcut::FWidget* = 0);
// Destructor
~MainWindow();
2017-09-11 03:06:02 +02:00
private:
// Disable copy constructor
MainWindow (const MainWindow&);
// Disable assignment operator (=)
MainWindow& operator = (const MainWindow&);
2018-09-24 04:02:35 +02:00
virtual void draw();
2017-09-11 03:06:02 +02:00
// Event handlers
2018-09-24 04:02:35 +02:00
virtual void onClose (finalcut::FCloseEvent*);
virtual void onShow (finalcut::FShowEvent*);
virtual void onTimer (finalcut::FTimerEvent*);
virtual void onKeyPress (finalcut::FKeyEvent* ev)
2017-09-11 03:06:02 +02:00
{
if ( ! ev )
return;
if ( ev->key() == 'q' )
{
close();
ev->accept();
}
else
finalcut::FDialog::onKeyPress(ev);
2017-09-11 03:06:02 +02:00
}
// Data Members
finalcut::FString line1;
finalcut::FString line2;
Transparent* transpwin;
Transparent* shadowwin;
Transparent* ibg;
finalcut::FStatusBar status_bar;
2016-08-21 21:29:04 +02:00
};
#pragma pack(pop)
//----------------------------------------------------------------------
MainWindow::MainWindow (finalcut::FWidget* parent)
2016-08-21 21:29:04 +02:00
: FDialog(parent)
, line1()
, line2()
, transpwin(0)
, shadowwin(0)
, ibg(0)
, status_bar(this)
2016-08-21 21:29:04 +02:00
{
line1 = " .-. .-. .-.";
line2 = "`._.' `._.' `._.' ";
// The memory allocation for the following three sub windows occurs
// with the operator new. The lifetime of the generated widget
// is managed by the parent object (this). The operator delete
// is not required in this scope and would result in a double free.
transpwin = new Transparent(this);
transpwin->setText("transparent");
transpwin->setGeometry (6, 3, 29, 12);
transpwin->unsetTransparentShadow();
shadowwin = new Transparent(this, Transparent::shadow);
shadowwin->setText("shadow");
shadowwin->setGeometry (46, 11, 29, 12);
shadowwin->unsetTransparentShadow();
ibg = new Transparent(this, Transparent::inherit_background);
ibg->setText("inherit background");
ibg->setGeometry (42, 3, 29, 7);
ibg->unsetTransparentShadow();
2016-08-21 21:29:04 +02:00
// Statusbar at the bottom
status_bar.setMessage("Press Q to quit");
2016-08-21 21:29:04 +02:00
addAccelerator('q');
unsetTransparentShadow();
activateDialog();
2016-08-21 21:29:04 +02:00
}
//----------------------------------------------------------------------
MainWindow::~MainWindow()
{ }
//----------------------------------------------------------------------
void MainWindow::draw()
{
finalcut::FDialog::draw();
2016-08-21 21:29:04 +02:00
if ( isMonochron() )
setReverse(true);
setColor();
2017-09-11 03:06:02 +02:00
setPrintPos (2, 4);
2016-08-21 21:29:04 +02:00
print(line1);
2017-09-11 03:06:02 +02:00
setPrintPos (2, 5);
2016-08-21 21:29:04 +02:00
print(line2);
if ( isMonochron() )
setReverse(false);
updateTerminal();
2016-08-21 21:29:04 +02:00
}
//----------------------------------------------------------------------
void MainWindow::onClose (finalcut::FCloseEvent* ev)
2016-08-21 21:29:04 +02:00
{
finalcut::FApplication::closeConfirmationDialog (this, ev);
2016-08-21 21:29:04 +02:00
}
//----------------------------------------------------------------------
void MainWindow::onShow (finalcut::FShowEvent*)
2016-08-21 21:29:04 +02:00
{
addTimer(100);
}
//----------------------------------------------------------------------
void MainWindow::onTimer (finalcut::FTimerEvent*)
2016-08-21 21:29:04 +02:00
{
wchar_t first_Char[2];
std::size_t length = line1.getLength();
2016-08-21 21:29:04 +02:00
first_Char[0] = line1[0];
first_Char[1] = line2[0];
line1 = line1.right(length - 1) + first_Char[0];
line2 = line2.right(length - 1) + first_Char[1];
redraw();
flush_out();
}
//----------------------------------------------------------------------
// main part
//----------------------------------------------------------------------
int main (int argc, char* argv[])
{
// Create the application object
finalcut::FApplication app (argc, argv);
2016-08-21 21:29:04 +02:00
// Create main dialog object
2016-08-21 21:29:04 +02:00
MainWindow main_dlg (&app);
main_dlg.setText ("non-transparent");
main_dlg.setGeometry (8, 16, 26, 7);
2016-08-21 21:29:04 +02:00
// Set dialog main_dlg as main widget
2016-08-21 21:29:04 +02:00
app.setMainWidget (&main_dlg);
// Show and start the application
main_dlg.show();
2016-08-21 21:29:04 +02:00
return app.exec();
}