TGProgressBar.cxx

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TGProgressBar.cxx 35582 2010-09-22 13:38:27Z bellenot $
00002 // Author: Fons Rademakers   10/10/2000
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TGProgressBar, TGHProgressBar and TGVProgressBar                     //
00015 //                                                                      //
00016 // The classes in this file implement progress bars. Progress bars can  //
00017 // be used to show progress of tasks taking more then a few seconds.    //
00018 // TGProgressBar is an abstract base class, use either TGHProgressBar   //
00019 // or TGVProgressBar. TGHProgressBar can in addition show the position  //
00020 // as text in the bar.                                                  //
00021 //                                                                      //
00022 //////////////////////////////////////////////////////////////////////////
00023 
00024 #include "TGProgressBar.h"
00025 #include "TGResourcePool.h"
00026 #include "Riostream.h"
00027 #include "TColor.h"
00028 #include "TGMsgBox.h"
00029 
00030 
00031 const TGFont *TGProgressBar::fgDefaultFont = 0;
00032 TGGC         *TGProgressBar::fgDefaultGC = 0;
00033 
00034 
00035 ClassImp(TGProgressBar)
00036 ClassImp(TGHProgressBar)
00037 ClassImp(TGVProgressBar)
00038 
00039 //______________________________________________________________________________
00040 TGProgressBar::TGProgressBar(const TGWindow *p, UInt_t w, UInt_t h,
00041                              ULong_t back, ULong_t barcolor, GContext_t norm,
00042                              FontStruct_t font, UInt_t options) :
00043    TGFrame(p, w, h, options | kOwnBackground, back)
00044 {
00045    // Create progress bar.
00046 
00047    fMin        = 0;
00048    fMax        = 100;
00049    fPos        = 0;
00050    fPosPix     = 0;
00051    fFillType   = kSolidFill;
00052    fBarType    = kStandard;
00053    fShowPos    = kFALSE;
00054    fPercent    = kTRUE;
00055    fNormGC     = norm;
00056    fFontStruct = font;
00057    fBarColorGC.SetFillStyle(kFillSolid);
00058    fBarColorGC.SetForeground(barcolor);
00059    fBarWidth   = kProgressBarStandardWidth;
00060    fDrawBar    = kFALSE;
00061 }
00062 
00063 //______________________________________________________________________________
00064 void TGProgressBar::SetRange(Float_t min, Float_t max)
00065 {
00066    // Set min and max of progress bar.
00067 
00068    if (min >= max) {
00069       Error("SetRange", "max must be > min");
00070       return;
00071    }
00072 
00073    Bool_t draw = kFALSE;
00074    if (fPos > fMin) {
00075       // already in progress... rescale
00076       if (fPos < min) fPos = min;
00077       if (fPos > max) fPos = max;
00078       draw = kTRUE;
00079    } else
00080       fPos = min;
00081 
00082    fMin = min;
00083    fMax = max;
00084 
00085    if (draw)
00086       DoRedraw();
00087 }
00088 
00089 //______________________________________________________________________________
00090 void TGProgressBar::SetPosition(Float_t pos)
00091 {
00092    // Set progress position between [min,max].
00093 
00094    if (pos < fMin) pos = fMin;
00095    if (pos > fMax) pos = fMax;
00096 
00097    if (fPos == pos)
00098       return;
00099 
00100    fPos = pos;
00101 
00102    //fClient->NeedRedraw(this);
00103    fDrawBar = kTRUE;
00104    DoRedraw();
00105 }
00106 
00107 //______________________________________________________________________________
00108 void TGProgressBar::Increment(Float_t inc)
00109 {
00110    // Increment progress position.
00111 
00112    if (fPos == fMax)
00113       return;
00114 
00115    fPos += inc;
00116    if (fPos > fMax) fPos = fMax;
00117 
00118    //fClient->NeedRedraw(this);
00119    fDrawBar = kTRUE;
00120    DoRedraw();
00121 }
00122 
00123 //______________________________________________________________________________
00124 void TGProgressBar::Reset()
00125 {
00126    // Reset progress bar (i.e. set pos to 0).
00127 
00128    fPos = 0;
00129 
00130    fClient->NeedRedraw(this);
00131 }
00132 
00133 //______________________________________________________________________________
00134 void TGProgressBar::SetFillType(EFillType type)
00135 {
00136    // Set fill type.
00137 
00138    fFillType = type;
00139 
00140    fClient->NeedRedraw(this);
00141 }
00142 
00143 //______________________________________________________________________________
00144 void TGProgressBar::SetBarType(EBarType type)
00145 {
00146    // Set bar type.
00147 
00148    fBarType = type;
00149 
00150    fClient->NeedRedraw(this);
00151 }
00152 
00153 //______________________________________________________________________________
00154 void TGProgressBar::SetBarColor(ULong_t color)
00155 {
00156    // Set progress bar color.
00157 
00158    fBarColorGC.SetForeground(color);
00159 
00160    fClient->NeedRedraw(this);
00161 }
00162 
00163 //______________________________________________________________________________
00164 void TGProgressBar::SetBarColor(const char *color)
00165 {
00166    // Set progress bar color.
00167 
00168    ULong_t ic;
00169    fClient->GetColorByName(color, ic);
00170    fBarColorGC.SetForeground(ic);
00171    fClient->NeedRedraw(this);
00172 }
00173 
00174 //______________________________________________________________________________
00175 void TGProgressBar::Format(const char *format)
00176 {
00177    // Set format for displaying a value.
00178 
00179    fFormat = format;
00180 
00181    fClient->NeedRedraw(this);
00182 }
00183 
00184 //______________________________________________________________________________
00185 FontStruct_t TGProgressBar::GetDefaultFontStruct()
00186 {
00187    // Return default font structure in use.
00188 
00189    if (!fgDefaultFont)
00190       fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
00191    return fgDefaultFont->GetFontStruct();
00192 }
00193 
00194 //______________________________________________________________________________
00195 const TGGC &TGProgressBar::GetDefaultGC()
00196 {
00197    // Return default graphics context in use.
00198 
00199    if (!fgDefaultGC)
00200       fgDefaultGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
00201    return *fgDefaultGC;
00202 }
00203 
00204 //______________________________________________________________________________
00205 void TGProgressBar::SetForegroundColor(Pixel_t pixel)
00206 {
00207    // Change text color drawing.
00208 
00209    TGGC *gc = gClient->GetResourcePool()->GetGCPool()->FindGC(fNormGC);
00210 
00211    if (!gc) {
00212       return;
00213    }
00214    gc->SetForeground(pixel);
00215    fNormGC = gc->GetGC();
00216 
00217    fClient->NeedRedraw(this);
00218 }
00219 
00220 //______________________________________________________________________________
00221 TGHProgressBar::TGHProgressBar(const TGWindow *p, UInt_t w, UInt_t h,
00222                               Pixel_t back, Pixel_t barcolor,
00223                               GContext_t norm, FontStruct_t font, UInt_t options) :
00224       TGProgressBar(p, w, h, back, barcolor, norm, font, options)
00225 {
00226    // Horizontal progress bar constructor.
00227 
00228    fBarWidth = h;
00229    fEditDisabled = kEditDisableHeight;
00230 }
00231 
00232 //______________________________________________________________________________
00233 TGHProgressBar::TGHProgressBar(const TGWindow *p, EBarType type, UInt_t w)
00234    : TGProgressBar(p, w, type == kStandard ? kProgressBarStandardWidth :
00235                    kProgressBarTextWidth, type == kStandard ? GetDefaultFrameBackground() :
00236                    fgWhitePixel, fgDefaultSelectedBackground, GetDefaultGC()(),
00237                    GetDefaultFontStruct(),
00238                    type == kStandard ? kSunkenFrame : kDoubleBorder | kSunkenFrame)
00239 {
00240    // Simple constructor allow you to create either a standard progress
00241    // bar, or a more fancy progress bar (fancy means: double sized border,
00242    // white background and a bit wider to allow for text to be printed
00243    // in the bar.
00244 
00245    fBarType  = type;
00246    fBarWidth = (type == kStandard) ? kProgressBarStandardWidth : kProgressBarTextWidth;
00247    fEditDisabled = kEditDisableHeight;
00248 }
00249 
00250 //______________________________________________________________________________
00251 void TGHProgressBar::ShowPosition(Bool_t set, Bool_t percent, const char *format)
00252 {
00253    // Show postion text, either in percent or formatted according format.
00254 
00255    fShowPos = set;
00256    fPercent = percent;
00257    fFormat  = format;
00258 
00259    fClient->NeedRedraw(this);
00260 }
00261 
00262 //______________________________________________________________________________
00263 void TGHProgressBar::DoRedraw()
00264 {
00265    // Draw horizontal progress bar.
00266 
00267    if (!fDrawBar) {
00268       // calls TGProgressBar::DrawBorder()
00269       TGFrame::DoRedraw();
00270    }
00271 
00272    fPosPix = Int_t(((Float_t)fWidth - (fBorderWidth << 1)) *
00273              (fPos - fMin) / (fMax - fMin) +
00274              fBorderWidth);
00275 
00276    Int_t pospix = fPosPix;
00277 
00278    if (fFillType == kSolidFill)
00279       gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
00280                                fBorderWidth, fPosPix - fBorderWidth, fBarWidth -
00281                                (fBorderWidth << 1));
00282    else {
00283       Int_t blocksize = kBlockSize;
00284       Int_t delta     = kBlockSpace;
00285       Int_t pos       = fBorderWidth;
00286       while (pos < fPosPix) {
00287          if (pos + blocksize > Int_t(fWidth)-fBorderWidth)
00288             blocksize = fWidth-fBorderWidth-pos;
00289          gVirtualX->FillRectangle(fId, fBarColorGC(), pos,
00290                                   fBorderWidth, blocksize, fBarWidth -
00291                                   (fBorderWidth << 1));
00292          if (fDrawBar && fShowPos)
00293             gVirtualX->ClearArea(fId, pos+blocksize, fBorderWidth,
00294                                  delta, fBarWidth - (fBorderWidth << 1));
00295 
00296          pos += blocksize + delta;
00297       }
00298       pospix = pos - delta;
00299    }
00300 
00301    if (fShowPos) {
00302       TString buf;
00303       if (fPercent)
00304          buf = TString::Format("%d%%", Int_t((fPos-fMin)/(fMax-fMin)*100.));
00305       else
00306          buf = TString::Format(fFormat.Data(), fPos);
00307 
00308       Int_t x, y, max_ascent, max_descent;
00309       UInt_t twidth  = gVirtualX->TextWidth(fFontStruct, buf.Data(), buf.Length());
00310       gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
00311       UInt_t theight = max_ascent + max_descent;
00312 
00313       x = (fWidth - twidth) >> 1;
00314       y = (fHeight - theight) >> 1;
00315 
00316       if (fDrawBar && fPosPix < Int_t(x+twidth))
00317          gVirtualX->ClearArea(fId, pospix, fBorderWidth,
00318                               fWidth - pospix - fBorderWidth,
00319                               fBarWidth - (fBorderWidth << 1));
00320 
00321       gVirtualX->DrawString(fId, fNormGC, x, y + max_ascent, buf.Data(), buf.Length());
00322    }
00323 
00324    fDrawBar = kFALSE;
00325 }
00326 
00327 //______________________________________________________________________________
00328 TGVProgressBar::TGVProgressBar(const TGWindow *p, UInt_t w, UInt_t h,
00329                               Pixel_t back, Pixel_t barcolor, GContext_t norm,
00330                               FontStruct_t font,UInt_t options) :
00331       TGProgressBar(p, w, h, back, barcolor, norm, font, options) 
00332 {
00333    // cconstructor
00334 
00335    fBarWidth = w;
00336    fEditDisabled = kEditDisableWidth;
00337 }
00338 
00339 //______________________________________________________________________________
00340 TGVProgressBar::TGVProgressBar(const TGWindow *p, EBarType type, UInt_t h)
00341    : TGProgressBar(p, type == kStandard ? kProgressBarStandardWidth :
00342                    kProgressBarTextWidth, h, type == kStandard ? GetDefaultFrameBackground() :
00343                    fgWhitePixel, fgDefaultSelectedBackground, GetDefaultGC()(),
00344                    GetDefaultFontStruct(),
00345                    type == kStandard ? kSunkenFrame : kDoubleBorder | kSunkenFrame)
00346 {
00347    // Simple constructor allow you to create either a standard progress
00348    // bar, or a more fancy progress bar (fancy means: double sized border,
00349    // white background and a bit wider to allow for text to be printed
00350    // in the bar.
00351 
00352    fBarType  = type;
00353    fBarWidth = (type == kStandard) ? kProgressBarStandardWidth : kProgressBarTextWidth;
00354    fDrawBar  = kFALSE;
00355    fEditDisabled = kEditDisableWidth;
00356 }
00357 
00358 //______________________________________________________________________________
00359 void TGVProgressBar::DoRedraw()
00360 {
00361    // Draw vertical progress bar.
00362 
00363    if (!fDrawBar) {
00364       // calls TGProgressBar::DrawBorder()
00365       TGFrame::DoRedraw();
00366    }
00367 
00368    fPosPix = Int_t(((Float_t)fHeight - (fBorderWidth << 1)) *
00369              (fPos - fMin) / (fMax - fMin) +
00370              fBorderWidth);
00371 
00372    if (fFillType == kSolidFill)
00373       gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
00374                                fHeight - fPosPix, fBarWidth - (fBorderWidth << 1),
00375                                fPosPix - fBorderWidth);
00376    else {
00377       Int_t blocksize = kBlockSize;
00378       Int_t delta     = kBlockSpace;
00379       Int_t pos       = fBorderWidth;
00380       while (pos < fPosPix) {
00381          if (pos + blocksize > Int_t(fHeight)-fBorderWidth)
00382             blocksize = fHeight-fBorderWidth-pos;
00383          gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
00384                                   fHeight - pos - blocksize, fBarWidth - (fBorderWidth << 1),
00385                                   blocksize);
00386          pos += blocksize + delta;
00387       }
00388    }
00389 
00390    if (fShowPos) {
00391       // not text shown for vertical progress bars
00392    }
00393 
00394    fDrawBar = kFALSE;
00395 }
00396 
00397 //______________________________________________________________________________
00398 void TGProgressBar::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
00399 {
00400    // Save progress bar parameters as a C++ statement(s) on output stream out.
00401 
00402    const char *barcolor;
00403    char quote = '"';
00404    switch (fBarType) {
00405       case kFancy:
00406          if (GetOptions() != (kSunkenFrame | kDoubleBorder | kOwnBackground))
00407             out << "   " << GetName() << "->ChangeOptions(" << GetOptionString()
00408                 << ");" << endl;
00409          if (GetBackground() != GetWhitePixel()) {
00410             SaveUserColor(out, option);
00411             out << "   " << GetName() << "->SetBackgroundColor(ucolor);" << endl;
00412          }
00413          break;
00414 
00415       case kStandard:
00416          if (GetOptions() != (kSunkenFrame | kOwnBackground))
00417             out << "   " << GetName() << "->ChangeOptions(" << GetOptionString()
00418                 << ");" << endl;
00419          if (GetBackground() != GetDefaultFrameBackground()) {
00420             SaveUserColor(out, option);
00421             out << "   " << GetName() << "->SetBackgroundColor(ucolor);" << endl;
00422          }
00423          break;
00424    }
00425 
00426    if (fBarColorGC.GetForeground() != GetDefaultSelectedBackground()) {
00427       barcolor = TColor::PixelAsHexString(fBarColorGC.GetForeground());
00428       out << "   " << GetName() <<"->SetBarColor(" << quote << barcolor << quote
00429           << ");"  << endl;
00430    }
00431 
00432    if (fMin != 0 && fMax != 100)
00433       out << "   " << GetName() << "->SetRange(" << fMin << "," << fMax << ");" << endl;
00434 
00435    out <<"   "<< GetName() <<"->SetPosition("<< fPos <<");"<< endl;
00436 
00437 }
00438 
00439 //______________________________________________________________________________
00440 void TGVProgressBar::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
00441 {
00442    // Save a vertical progress bar as a C++ statement(s) on output stream out.
00443 
00444 
00445    out << "   TGVProgressBar *";
00446    out << GetName() << " = new TGVProgressBar(" << fParent->GetName();
00447 
00448    if ((fBarType == kFancy) && (fBarWidth == kProgressBarTextWidth)) {
00449       out << ",TGProgressBar::kFancy";
00450    } else if ((fBarType == kStandard) && (fBarWidth == kProgressBarStandardWidth)){
00451       out << ",TGProgressBar::kStandard";
00452    }
00453 
00454    out << "," << GetHeight() <<");" << endl;
00455 
00456    if (option && strstr(option, "keep_names"))
00457       out << "   " << GetName() << "->SetName(\"" << GetName() << "\");" << endl;
00458 
00459    if (GetFillType() == kBlockFill)
00460       out << "   " << GetName() <<"->SetFillType(TGProgressBar::kBlockFill);"<< endl;
00461 
00462    TGProgressBar::SavePrimitive(out, option);
00463 }
00464 
00465 //______________________________________________________________________________
00466 void TGHProgressBar::SavePrimitive(ostream &out, Option_t *option /*= ""*/)
00467 {
00468     // Save a horizontal progress bar as a C++ statement(s) on output stream out
00469 
00470    char quote = '"';
00471 
00472    out <<"   TGHProgressBar *";
00473    out << GetName() <<" = new TGHProgressBar("<< fParent->GetName();
00474 
00475    if ((fBarType == kFancy) && (fBarWidth == kProgressBarTextWidth)) {
00476       out << ",TGProgressBar::kFancy";
00477    } else if ((fBarType == kStandard) && (fBarWidth == kProgressBarStandardWidth)){
00478       out << ",TGProgressBar::kStandard";
00479    }
00480 
00481    if (option && strstr(option, "keep_names"))
00482       out << "   " << GetName() << "->SetName(\"" << GetName() << "\");" << endl;
00483 
00484    out << "," << GetWidth() << ");" << endl;
00485 
00486    if (GetFillType() == kBlockFill)
00487       out << "   " << GetName() <<"->SetFillType(TGProgressBar::kBlockFill);"<< endl;
00488 
00489    if (GetShowPos()) {
00490       out << "   " << GetName() <<"->ShowPosition(kTRUE,";
00491       if (UsePercent()) {
00492          out << "kTRUE,";
00493       } else {
00494          out << "kFALSE,";
00495       }
00496       out << quote << GetFormat() << quote << ");"<< endl;
00497 
00498    } else if (UsePercent() && !GetFillType()) {
00499       out << "   " << GetName() <<"->ShowPosition();" << endl;
00500    }
00501    TGProgressBar::SavePrimitive(out, option);
00502 }

Generated on Tue Jul 5 14:22:03 2011 for ROOT_528-00b_version by  doxygen 1.5.1