2015-09-25 21:37:19 +02:00
|
|
|
// File: fprogressbar.cpp
|
|
|
|
// Provides: class FProgressbar
|
2015-05-23 13:35:12 +02:00
|
|
|
|
|
|
|
#include "fprogressbar.h"
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class FProgressbar
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// constructors and destructor
|
|
|
|
//----------------------------------------------------------------------
|
2015-09-22 04:18:20 +02:00
|
|
|
FProgressbar::FProgressbar(FWidget* parent)
|
|
|
|
: FWidget(parent)
|
|
|
|
, percentage(-1)
|
2016-09-27 00:46:05 +02:00
|
|
|
, bar_length(getWidth())
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2015-09-22 04:18:20 +02:00
|
|
|
unsetFocusable();
|
2016-09-04 18:31:31 +02:00
|
|
|
setShadow();
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
FProgressbar::~FProgressbar()
|
2015-09-22 04:18:20 +02:00
|
|
|
{ }
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-02 00:37:58 +01:00
|
|
|
|
|
|
|
// public methods of FProgressbar
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FProgressbar::setPercentage (int percentage_value)
|
|
|
|
{
|
|
|
|
if ( percentage_value <= percentage )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ( percentage_value > 100 )
|
|
|
|
percentage = 100;
|
|
|
|
else if ( percentage_value < 0 )
|
|
|
|
percentage = 0;
|
|
|
|
else
|
|
|
|
percentage = percentage_value;
|
|
|
|
|
|
|
|
if ( isVisible() )
|
|
|
|
{
|
|
|
|
drawPercentage();
|
|
|
|
drawBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTerminal();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FProgressbar::setGeometry (int x, int y, int w, int h, bool adjust)
|
|
|
|
{
|
|
|
|
FWidget::setGeometry (x, y, w, h, adjust);
|
|
|
|
bar_length = w;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
bool FProgressbar::setEnable (bool on)
|
|
|
|
{
|
|
|
|
FWidget::setEnable(on);
|
|
|
|
|
|
|
|
if ( on )
|
|
|
|
flags |= fc::active;
|
|
|
|
else
|
|
|
|
flags &= ~fc::active;
|
|
|
|
|
|
|
|
return on;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
bool FProgressbar::setShadow (bool on)
|
|
|
|
{
|
2016-12-18 23:34:11 +01:00
|
|
|
if ( on
|
2017-04-05 00:30:52 +02:00
|
|
|
&& Encoding != fc::VT100
|
2016-12-18 23:34:11 +01:00
|
|
|
&& Encoding != fc::ASCII )
|
2017-01-26 00:31:07 +01:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
flags |= fc::shadow;
|
2017-01-26 00:31:07 +01:00
|
|
|
setShadowSize(1,1);
|
|
|
|
}
|
2016-11-02 00:37:58 +01:00
|
|
|
else
|
2017-01-26 00:31:07 +01:00
|
|
|
{
|
2016-11-02 00:37:58 +01:00
|
|
|
flags &= ~fc::shadow;
|
2017-01-26 00:31:07 +01:00
|
|
|
setShadowSize(0,0);
|
|
|
|
}
|
2016-11-02 00:37:58 +01:00
|
|
|
|
|
|
|
return on;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FProgressbar::hide()
|
|
|
|
{
|
|
|
|
int s, size;
|
|
|
|
short fg, bg;
|
|
|
|
char* blank;
|
|
|
|
FWidget* parent_widget = getParentWidget();
|
|
|
|
|
|
|
|
FWidget::hide();
|
|
|
|
|
|
|
|
if ( parent_widget )
|
|
|
|
{
|
|
|
|
fg = parent_widget->getForegroundColor();
|
|
|
|
bg = parent_widget->getBackgroundColor();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fg = wc.dialog_fg;
|
|
|
|
bg = wc.dialog_bg;
|
|
|
|
}
|
|
|
|
|
|
|
|
setColor (fg, bg);
|
|
|
|
s = hasShadow() ? 1 : 0;
|
|
|
|
size = getWidth() + s;
|
|
|
|
|
|
|
|
if ( size < 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
blank = new char[size+1];
|
|
|
|
std::memset(blank, ' ', uLong(size));
|
|
|
|
blank[size] = '\0';
|
|
|
|
|
|
|
|
for (int y=0; y < getHeight()+s; y++)
|
|
|
|
{
|
|
|
|
setPrintPos (1, 1 + y);
|
|
|
|
print (blank);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] blank;
|
|
|
|
setPrintPos (getWidth() - 4, 0);
|
|
|
|
print (" "); // hide percentage
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FProgressbar::reset()
|
|
|
|
{
|
|
|
|
percentage = -1;
|
|
|
|
|
|
|
|
if ( isVisible() )
|
|
|
|
{
|
|
|
|
drawPercentage();
|
|
|
|
drawBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTerminal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
// private methods of FProgressbar
|
2016-11-02 00:37:58 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FProgressbar::draw()
|
|
|
|
{
|
|
|
|
drawPercentage();
|
|
|
|
drawBar();
|
|
|
|
|
|
|
|
if ( (flags & fc::shadow) != 0 )
|
|
|
|
drawShadow ();
|
|
|
|
|
|
|
|
flush_out();
|
|
|
|
}
|
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FProgressbar::drawPercentage()
|
|
|
|
{
|
2016-08-21 21:27:44 +02:00
|
|
|
FWidget* parent_widget = getParentWidget();
|
|
|
|
|
|
|
|
if ( parent_widget )
|
|
|
|
setColor ( parent_widget->getForegroundColor()
|
|
|
|
, parent_widget->getBackgroundColor() );
|
|
|
|
else
|
|
|
|
setColor ( wc.dialog_fg, wc.dialog_bg );
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-10-11 21:56:16 +02:00
|
|
|
if ( isMonochron() )
|
|
|
|
setReverse(true);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2016-10-11 04:57:36 +02:00
|
|
|
setPrintPos (getWidth() - 3, 0);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
if ( percentage < 0 || percentage > 100 )
|
|
|
|
print ("--- %");
|
|
|
|
else
|
|
|
|
printf ("%3d %%", percentage);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-10-11 21:56:16 +02:00
|
|
|
if ( isMonochron() )
|
|
|
|
setReverse(false);
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FProgressbar::drawBar()
|
|
|
|
{
|
2016-11-13 22:08:40 +01:00
|
|
|
int i = 0;
|
2016-09-27 00:46:05 +02:00
|
|
|
float length = float(bar_length * percentage) / 100;
|
2016-10-11 04:57:36 +02:00
|
|
|
setPrintPos (1,1);
|
2015-10-10 04:01:22 +02:00
|
|
|
setColor ( wc.progressbar_bg
|
|
|
|
, wc.progressbar_fg );
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
if ( isMonochron() )
|
2015-10-11 21:56:16 +02:00
|
|
|
setReverse(false);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
for (; i < trunc(length); i++)
|
|
|
|
print (' ');
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
if ( isMonochron() )
|
2015-10-11 21:56:16 +02:00
|
|
|
setReverse(true);
|
2015-05-23 13:35:12 +02:00
|
|
|
|
2016-11-13 22:08:40 +01:00
|
|
|
if ( percentage > 0.0f && trunc(length) < bar_length )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
2016-12-18 23:34:11 +01:00
|
|
|
if ( round(length) > trunc(length)
|
|
|
|
|| getMaxColor() < 16 )
|
2015-05-23 13:35:12 +02:00
|
|
|
{
|
|
|
|
if ( isMonochron() )
|
|
|
|
{
|
|
|
|
setReverse(false);
|
2015-10-11 21:56:16 +02:00
|
|
|
print (' ');
|
|
|
|
setReverse(true);
|
2015-05-23 13:35:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
print (' ');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-01-08 01:00:05 +01:00
|
|
|
setColor (wc.progressbar_fg, wc.progressbar_bg);
|
2015-05-23 13:35:12 +02:00
|
|
|
print (fc::LeftHalfBlock); // ▌
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2016-01-08 01:00:05 +01:00
|
|
|
setColor (wc.progressbar_fg, wc.progressbar_bg);
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
if ( getMaxColor() < 16 )
|
|
|
|
{
|
2016-09-27 00:46:05 +02:00
|
|
|
for (; i < bar_length; i++)
|
2015-05-23 13:35:12 +02:00
|
|
|
print (fc::MediumShade); // ▒
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-27 00:46:05 +02:00
|
|
|
for (; i < bar_length; i++)
|
2015-05-23 13:35:12 +02:00
|
|
|
print (' ');
|
|
|
|
}
|
2016-07-09 00:01:59 +02:00
|
|
|
|
2015-10-11 21:56:16 +02:00
|
|
|
if ( isMonochron() )
|
|
|
|
setReverse(false);
|
2015-10-23 23:57:00 +02:00
|
|
|
|
2015-05-23 13:35:12 +02:00
|
|
|
updateTerminal();
|
|
|
|
flush_out();
|
|
|
|
}
|