#include "TGProgressBar.h"
#include "TGResourcePool.h"
#include "Riostream.h"
#include "TColor.h"
#include "TGMsgBox.h"
const TGFont *TGProgressBar::fgDefaultFont = 0;
TGGC *TGProgressBar::fgDefaultGC = 0;
ClassImp(TGProgressBar)
ClassImp(TGHProgressBar)
ClassImp(TGVProgressBar)
TGProgressBar::TGProgressBar(const TGWindow *p, UInt_t w, UInt_t h,
ULong_t back, ULong_t barcolor, GContext_t norm,
FontStruct_t font, UInt_t options) :
TGFrame(p, w, h, options | kOwnBackground, back)
{
fMin = 0;
fMax = 100;
fPos = 0;
fPosPix = 0;
fFillType = kSolidFill;
fBarType = kStandard;
fShowPos = kFALSE;
fPercent = kTRUE;
fNormGC = norm;
fFontStruct = font;
fBarColorGC.SetFillStyle(kFillSolid);
fBarColorGC.SetForeground(barcolor);
fBarWidth = kProgressBarStandardWidth;
fDrawBar = kFALSE;
}
void TGProgressBar::SetRange(Float_t min, Float_t max)
{
if (min >= max) {
Error("SetRange", "max must be > min");
return;
}
Bool_t draw = kFALSE;
if (fPos > fMin) {
if (fPos < min) fPos = min;
if (fPos > max) fPos = max;
draw = kTRUE;
} else
fPos = min;
fMin = min;
fMax = max;
if (draw)
DoRedraw();
}
void TGProgressBar::SetPosition(Float_t pos)
{
if (pos < fMin) pos = fMin;
if (pos > fMax) pos = fMax;
if (fPos == pos)
return;
fPos = pos;
fDrawBar = kTRUE;
DoRedraw();
}
void TGProgressBar::Increment(Float_t inc)
{
if (fPos == fMax)
return;
fPos += inc;
if (fPos > fMax) fPos = fMax;
fDrawBar = kTRUE;
DoRedraw();
}
void TGProgressBar::Reset()
{
fPos = 0;
fClient->NeedRedraw(this);
}
void TGProgressBar::SetFillType(EFillType type)
{
fFillType = type;
fClient->NeedRedraw(this);
}
void TGProgressBar::SetBarType(EBarType type)
{
fBarType = type;
fClient->NeedRedraw(this);
}
void TGProgressBar::SetBarColor(ULong_t color)
{
fBarColorGC.SetForeground(color);
fClient->NeedRedraw(this);
}
void TGProgressBar::SetBarColor(const char *color)
{
ULong_t ic;
fClient->GetColorByName(color, ic);
fBarColorGC.SetForeground(ic);
fClient->NeedRedraw(this);
}
void TGProgressBar::Format(const char *format)
{
fFormat = format;
fClient->NeedRedraw(this);
}
FontStruct_t TGProgressBar::GetDefaultFontStruct()
{
if (!fgDefaultFont)
fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
return fgDefaultFont->GetFontStruct();
}
const TGGC &TGProgressBar::GetDefaultGC()
{
if (!fgDefaultGC)
fgDefaultGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
return *fgDefaultGC;
}
void TGProgressBar::SetForegroundColor(Pixel_t pixel)
{
TGGC *gc = gClient->GetResourcePool()->GetGCPool()->FindGC(fNormGC);
if (!gc) {
return;
}
gc->SetForeground(pixel);
fNormGC = gc->GetGC();
fClient->NeedRedraw(this);
}
TGHProgressBar::TGHProgressBar(const TGWindow *p, UInt_t w, UInt_t h,
Pixel_t back, Pixel_t barcolor,
GContext_t norm, FontStruct_t font, UInt_t options) :
TGProgressBar(p, w, h, back, barcolor, norm, font, options)
{
fBarWidth = h;
fEditDisabled = kEditDisableHeight;
}
TGHProgressBar::TGHProgressBar(const TGWindow *p, EBarType type, UInt_t w)
: TGProgressBar(p, w, type == kStandard ? kProgressBarStandardWidth :
kProgressBarTextWidth, type == kStandard ? GetDefaultFrameBackground() :
fgWhitePixel, fgDefaultSelectedBackground, GetDefaultGC()(),
GetDefaultFontStruct(),
type == kStandard ? kSunkenFrame : kDoubleBorder | kSunkenFrame)
{
fBarType = type;
fBarWidth = (type == kStandard) ? kProgressBarStandardWidth : kProgressBarTextWidth;
fEditDisabled = kEditDisableHeight;
}
void TGHProgressBar::ShowPosition(Bool_t set, Bool_t percent, const char *format)
{
fShowPos = set;
fPercent = percent;
fFormat = format;
fClient->NeedRedraw(this);
}
void TGHProgressBar::DoRedraw()
{
if (!fDrawBar) {
TGFrame::DoRedraw();
}
fPosPix = Int_t(((Float_t)fWidth - (fBorderWidth << 1)) *
(fPos - fMin) / (fMax - fMin) +
fBorderWidth);
Int_t pospix = fPosPix;
if (fFillType == kSolidFill)
gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
fBorderWidth, fPosPix - fBorderWidth, fBarWidth -
(fBorderWidth << 1));
else {
Int_t blocksize = kBlockSize;
Int_t delta = kBlockSpace;
Int_t pos = fBorderWidth;
while (pos < fPosPix) {
if (pos + blocksize > Int_t(fWidth)-fBorderWidth)
blocksize = fWidth-fBorderWidth-pos;
gVirtualX->FillRectangle(fId, fBarColorGC(), pos,
fBorderWidth, blocksize, fBarWidth -
(fBorderWidth << 1));
if (fDrawBar && fShowPos)
gVirtualX->ClearArea(fId, pos+blocksize, fBorderWidth,
delta, fBarWidth - (fBorderWidth << 1));
pos += blocksize + delta;
}
pospix = pos - delta;
}
if (fShowPos) {
char buf[256];
if (fPercent)
sprintf(buf, "%d%%", Int_t((fPos-fMin)/(fMax-fMin)*100.));
else
sprintf(buf, fFormat.Data(), fPos);
Int_t x, y, max_ascent, max_descent;
UInt_t twidth = gVirtualX->TextWidth(fFontStruct, buf, strlen(buf));
gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
UInt_t theight = max_ascent + max_descent;
x = (fWidth - twidth) >> 1;
y = (fHeight - theight) >> 1;
if (fDrawBar && fPosPix < Int_t(x+twidth))
gVirtualX->ClearArea(fId, pospix, fBorderWidth,
fWidth - pospix - fBorderWidth,
fBarWidth - (fBorderWidth << 1));
gVirtualX->DrawString(fId, fNormGC, x, y + max_ascent, buf, strlen(buf));
}
fDrawBar = kFALSE;
}
TGVProgressBar::TGVProgressBar(const TGWindow *p, UInt_t w, UInt_t h,
Pixel_t back, Pixel_t barcolor, GContext_t norm,
FontStruct_t font,UInt_t options) :
TGProgressBar(p, w, h, back, barcolor, norm, font, options)
{
fBarWidth = w;
fEditDisabled = kEditDisableWidth;
}
TGVProgressBar::TGVProgressBar(const TGWindow *p, EBarType type, UInt_t h)
: TGProgressBar(p, type == kStandard ? kProgressBarStandardWidth :
kProgressBarTextWidth, h, type == kStandard ? GetDefaultFrameBackground() :
fgWhitePixel, fgDefaultSelectedBackground, GetDefaultGC()(),
GetDefaultFontStruct(),
type == kStandard ? kSunkenFrame : kDoubleBorder | kSunkenFrame)
{
fBarType = type;
fBarWidth = (type == kStandard) ? kProgressBarStandardWidth : kProgressBarTextWidth;
fDrawBar = kFALSE;
fEditDisabled = kEditDisableWidth;
}
void TGVProgressBar::DoRedraw()
{
if (!fDrawBar) {
TGFrame::DoRedraw();
}
fPosPix = Int_t(((Float_t)fHeight - (fBorderWidth << 1)) *
(fPos - fMin) / (fMax - fMin) +
fBorderWidth);
if (fFillType == kSolidFill)
gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
fHeight - fPosPix, fBarWidth - (fBorderWidth << 1),
fPosPix - fBorderWidth);
else {
Int_t blocksize = kBlockSize;
Int_t delta = kBlockSpace;
Int_t pos = fBorderWidth;
while (pos < fPosPix) {
if (pos + blocksize > Int_t(fHeight)-fBorderWidth)
blocksize = fHeight-fBorderWidth-pos;
gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
fHeight - pos - blocksize, fBarWidth - (fBorderWidth << 1),
blocksize);
pos += blocksize + delta;
}
}
if (fShowPos) {
}
fDrawBar = kFALSE;
}
void TGProgressBar::SavePrimitive(ostream &out, Option_t *option )
{
const char *barcolor;
char quote = '"';
switch (fBarType) {
case kFancy:
if (GetOptions() != (kSunkenFrame | kDoubleBorder | kOwnBackground))
out << " " << GetName() << "->ChangeOptions(" << GetOptionString()
<< ");" << endl;
if (GetBackground() != GetWhitePixel()) {
SaveUserColor(out, option);
out << " " << GetName() << "->SetBackgroundColor(ucolor);" << endl;
}
break;
case kStandard:
if (GetOptions() != (kSunkenFrame | kOwnBackground))
out << " " << GetName() << "->ChangeOptions(" << GetOptionString()
<< ");" << endl;
if (GetBackground() != GetDefaultFrameBackground()) {
SaveUserColor(out, option);
out << " " << GetName() << "->SetBackgroundColor(ucolor);" << endl;
}
break;
}
if (fBarColorGC.GetForeground() != GetDefaultSelectedBackground()) {
barcolor = TColor::PixelAsHexString(fBarColorGC.GetForeground());
out << " " << GetName() <<"->SetBarColor(" << quote << barcolor << quote
<< ");" << endl;
}
if (fMin != 0 && fMax != 100)
out << " " << GetName() << "->SetRange(" << fMin << "," << fMax << ");" << endl;
out <<" "<< GetName() <<"->SetPosition("<< fPos <<");"<< endl;
}
void TGVProgressBar::SavePrimitive(ostream &out, Option_t *option )
{
out << " TGVProgressBar *";
out << GetName() << " = new TGVProgressBar(" << fParent->GetName();
if ((fBarType == kFancy) && (fBarWidth == kProgressBarTextWidth)) {
out << ",TGProgressBar::kFancy";
} else if ((fBarType == kStandard) && (fBarWidth == kProgressBarStandardWidth)){
out << ",TGProgressBar::kStandard";
}
out << "," << GetHeight() <<");" << endl;
if (GetFillType() == kBlockFill)
out << " " << GetName() <<"->SetFillType(TGProgressBar::kBlockFill);"<< endl;
TGProgressBar::SavePrimitive(out, option);
}
void TGHProgressBar::SavePrimitive(ostream &out, Option_t *option )
{
char quote = '"';
out <<" TGHProgressBar *";
out << GetName() <<" = new TGHProgressBar("<< fParent->GetName();
if ((fBarType == kFancy) && (fBarWidth == kProgressBarTextWidth)) {
out << ",TGProgressBar::kFancy";
} else if ((fBarType == kStandard) && (fBarWidth == kProgressBarStandardWidth)){
out << ",TGProgressBar::kStandard";
}
out << "," << GetWidth() << ");" << endl;
if (GetFillType() == kBlockFill)
out << " " << GetName() <<"->SetFillType(TGProgressBar::kBlockFill);"<< endl;
if (GetShowPos()) {
out << " " << GetName() <<"->ShowPosition(kTRUE,";
if (UsePercent()) {
out << "kTRUE,";
} else {
out << "kFALSE,";
}
out << quote << GetFormat() << quote << ");"<< endl;
} else if (UsePercent() && !GetFillType()) {
out << " " << GetName() <<"->ShowPosition();" << endl;
}
TGProgressBar::SavePrimitive(out, option);
}