Logo ROOT   6.10/09
Reference Guide
TGProgressBar.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Fons Rademakers 10/10/2000
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TGProgressBar, TGHProgressBar and TGVProgressBar //
15 // //
16 // The classes in this file implement progress bars. Progress bars can //
17 // be used to show progress of tasks taking more then a few seconds. //
18 // TGProgressBar is an abstract base class, use either TGHProgressBar //
19 // or TGVProgressBar. TGHProgressBar can in addition show the position //
20 // as text in the bar. //
21 // //
22 //////////////////////////////////////////////////////////////////////////
23 
24 #include "TGProgressBar.h"
25 #include "TGResourcePool.h"
26 #include "Riostream.h"
27 #include "TColor.h"
28 #include "TGMsgBox.h"
29 
30 
33 
34 
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Create progress bar.
41 
43  ULong_t back, ULong_t barcolor, GContext_t norm,
44  FontStruct_t font, UInt_t options) :
45  TGFrame(p, w, h, options | kOwnBackground, back)
46 {
47  fMin = 0;
48  fMax = 100;
49  fPos = 0;
50  fPosPix = 0;
51  fFillType = kSolidFill;
52  fBarType = kStandard;
53  fShowPos = kFALSE;
54  fPercent = kTRUE;
55  fNormGC = norm;
56  fFontStruct = font;
57  fBarColorGC.SetFillStyle(kFillSolid);
58  fBarColorGC.SetForeground(barcolor);
59  fBarWidth = kProgressBarStandardWidth;
60  fDrawBar = kFALSE;
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// Set min and max of progress bar.
65 
67 {
68  if (min >= max) {
69  Error("SetRange", "max must be > min");
70  return;
71  }
72 
73  Bool_t draw = kFALSE;
74  if (fPos > fMin) {
75  // already in progress... rescale
76  if (fPos < min) fPos = min;
77  if (fPos > max) fPos = max;
78  draw = kTRUE;
79  } else
80  fPos = min;
81 
82  fMin = min;
83  fMax = max;
84 
85  if (draw)
86  DoRedraw();
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Set progress position between [min,max].
91 
93 {
94  if (pos < fMin) pos = fMin;
95  if (pos > fMax) pos = fMax;
96 
97  if (fPos == pos)
98  return;
99 
100  fPos = pos;
101 
102  //fClient->NeedRedraw(this);
103  fDrawBar = kTRUE;
104  DoRedraw();
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Increment progress position.
109 
111 {
112  if (fPos == fMax)
113  return;
114 
115  fPos += inc;
116  if (fPos > fMax) fPos = fMax;
117 
118  //fClient->NeedRedraw(this);
119  fDrawBar = kTRUE;
120  DoRedraw();
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Reset progress bar (i.e. set pos to 0).
125 
127 {
128  fPos = 0;
129 
130  fClient->NeedRedraw(this);
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Set fill type.
135 
137 {
138  fFillType = type;
139 
140  fClient->NeedRedraw(this);
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Set bar type.
145 
147 {
148  fBarType = type;
149 
150  fClient->NeedRedraw(this);
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Set progress bar color.
155 
157 {
158  fBarColorGC.SetForeground(color);
159 
160  fClient->NeedRedraw(this);
161 }
162 
163 ////////////////////////////////////////////////////////////////////////////////
164 /// Set progress bar color.
165 
166 void TGProgressBar::SetBarColor(const char *color)
167 {
168  ULong_t ic;
169  fClient->GetColorByName(color, ic);
171  fClient->NeedRedraw(this);
172 }
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// Set format for displaying a value.
176 
177 void TGProgressBar::Format(const char *format)
178 {
179  fFormat = format;
180 
181  fClient->NeedRedraw(this);
182 }
183 
184 ////////////////////////////////////////////////////////////////////////////////
185 /// Return default font structure in use.
186 
188 {
189  if (!fgDefaultFont)
190  fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
191  return fgDefaultFont->GetFontStruct();
192 }
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 /// Return default graphics context in use.
196 
198 {
199  if (!fgDefaultGC)
200  fgDefaultGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
201  return *fgDefaultGC;
202 }
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Change text color drawing.
206 
208 {
209  TGGC *gc = gClient->GetResourcePool()->GetGCPool()->FindGC(fNormGC);
210 
211  if (!gc) {
212  return;
213  }
214  gc->SetForeground(pixel);
215  fNormGC = gc->GetGC();
216 
217  fClient->NeedRedraw(this);
218 }
219 
220 ////////////////////////////////////////////////////////////////////////////////
221 /// Horizontal progress bar constructor.
222 
224  Pixel_t back, Pixel_t barcolor,
225  GContext_t norm, FontStruct_t font, UInt_t options) :
226  TGProgressBar(p, w, h, back, barcolor, norm, font, options)
227 {
228  fBarWidth = h;
230 }
231 
232 ////////////////////////////////////////////////////////////////////////////////
233 /// Simple constructor allow you to create either a standard progress
234 /// bar, or a more fancy progress bar (fancy means: double sized border,
235 /// white background and a bit wider to allow for text to be printed
236 /// in the bar.
237 
244 {
245  fBarType = type;
248 }
249 
250 ////////////////////////////////////////////////////////////////////////////////
251 /// Show postion text, either in percent or formatted according format.
252 
253 void TGHProgressBar::ShowPosition(Bool_t set, Bool_t percent, const char *format)
254 {
255  fShowPos = set;
256  fPercent = percent;
257  fFormat = format;
258 
259  fClient->NeedRedraw(this);
260 }
261 
262 ////////////////////////////////////////////////////////////////////////////////
263 /// Draw horizontal progress bar.
264 
266 {
267  if (!fDrawBar) {
268  // calls TGProgressBar::DrawBorder()
270  }
271 
272  fPosPix = Int_t(((Float_t)fWidth - (fBorderWidth << 1)) *
273  (fPos - fMin) / (fMax - fMin) +
274  fBorderWidth);
275 
276  Int_t pospix = fPosPix;
277 
278  if (fFillType == kSolidFill)
279  gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
281  (fBorderWidth << 1));
282  else {
283  Int_t blocksize = kBlockSize;
285  Int_t pos = fBorderWidth;
286  while (pos < fPosPix) {
287  if (pos + blocksize > Int_t(fWidth)-fBorderWidth)
288  blocksize = fWidth-fBorderWidth-pos;
289  gVirtualX->FillRectangle(fId, fBarColorGC(), pos,
290  fBorderWidth, blocksize, fBarWidth -
291  (fBorderWidth << 1));
292  if (fDrawBar && fShowPos)
293  gVirtualX->ClearArea(fId, pos+blocksize, fBorderWidth,
294  delta, fBarWidth - (fBorderWidth << 1));
295 
296  pos += blocksize + delta;
297  }
298  pospix = pos - delta;
299  }
300 
301  if (fShowPos) {
302  TString buf;
303  if (fPercent)
304  buf = TString::Format("%d%%", Int_t((fPos-fMin)/(fMax-fMin)*100.));
305  else
306  buf = TString::Format(fFormat.Data(), fPos);
307 
308  Int_t x, y, max_ascent, max_descent;
309  UInt_t twidth = gVirtualX->TextWidth(fFontStruct, buf.Data(), buf.Length());
310  gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
311  UInt_t theight = max_ascent + max_descent;
312 
313  x = (fWidth - twidth) >> 1;
314  y = (fHeight - theight) >> 1;
315 
316  if (fDrawBar && fPosPix < Int_t(x+twidth))
317  gVirtualX->ClearArea(fId, pospix, fBorderWidth,
318  fWidth - pospix - fBorderWidth,
319  fBarWidth - (fBorderWidth << 1));
320 
321  gVirtualX->DrawString(fId, fNormGC, x, y + max_ascent, buf.Data(), buf.Length());
322  }
323 
324  fDrawBar = kFALSE;
325 }
326 
327 ////////////////////////////////////////////////////////////////////////////////
328 /// cconstructor
329 
331  Pixel_t back, Pixel_t barcolor, GContext_t norm,
332  FontStruct_t font,UInt_t options) :
333  TGProgressBar(p, w, h, back, barcolor, norm, font, options)
334 {
335  fBarWidth = w;
337 }
338 
339 ////////////////////////////////////////////////////////////////////////////////
340 /// Simple constructor allow you to create either a standard progress
341 /// bar, or a more fancy progress bar (fancy means: double sized border,
342 /// white background and a bit wider to allow for text to be printed
343 /// in the bar.
344 
351 {
352  fBarType = type;
354  fDrawBar = kFALSE;
356 }
357 
358 ////////////////////////////////////////////////////////////////////////////////
359 /// Draw vertical progress bar.
360 
362 {
363  if (!fDrawBar) {
364  // calls TGProgressBar::DrawBorder()
366  }
367 
368  fPosPix = Int_t(((Float_t)fHeight - (fBorderWidth << 1)) *
369  (fPos - fMin) / (fMax - fMin) +
370  fBorderWidth);
371 
372  if (fFillType == kSolidFill)
373  gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
376  else {
377  Int_t blocksize = kBlockSize;
379  Int_t pos = fBorderWidth;
380  while (pos < fPosPix) {
381  if (pos + blocksize > Int_t(fHeight)-fBorderWidth)
382  blocksize = fHeight-fBorderWidth-pos;
383  gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
384  fHeight - pos - blocksize, fBarWidth - (fBorderWidth << 1),
385  blocksize);
386  pos += blocksize + delta;
387  }
388  }
389 
390  if (fShowPos) {
391  // not text shown for vertical progress bars
392  }
393 
394  fDrawBar = kFALSE;
395 }
396 
397 ////////////////////////////////////////////////////////////////////////////////
398 /// Save progress bar parameters as a C++ statement(s) on output stream out.
399 
400 void TGProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
401 {
402  const char *barcolor;
403  char quote = '"';
404  switch (fBarType) {
405  case kFancy:
407  out << " " << GetName() << "->ChangeOptions(" << GetOptionString()
408  << ");" << std::endl;
409  if (GetBackground() != GetWhitePixel()) {
410  SaveUserColor(out, option);
411  out << " " << GetName() << "->SetBackgroundColor(ucolor);" << std::endl;
412  }
413  break;
414 
415  case kStandard:
417  out << " " << GetName() << "->ChangeOptions(" << GetOptionString()
418  << ");" << std::endl;
420  SaveUserColor(out, option);
421  out << " " << GetName() << "->SetBackgroundColor(ucolor);" << std::endl;
422  }
423  break;
424  }
425 
428  out << " " << GetName() <<"->SetBarColor(" << quote << barcolor << quote
429  << ");" << std::endl;
430  }
431 
432  if (fMin != 0 && fMax != 100)
433  out << " " << GetName() << "->SetRange(" << fMin << "," << fMax << ");" << std::endl;
434 
435  out <<" "<< GetName() <<"->SetPosition("<< fPos <<");"<< std::endl;
436 
437 }
438 
439 ////////////////////////////////////////////////////////////////////////////////
440 /// Save a vertical progress bar as a C++ statement(s) on output stream out.
441 
442 void TGVProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
443 {
444 
445  out << " TGVProgressBar *";
446  out << GetName() << " = new TGVProgressBar(" << fParent->GetName();
447 
448  if ((fBarType == kFancy) && (fBarWidth == kProgressBarTextWidth)) {
449  out << ",TGProgressBar::kFancy";
450  } else if ((fBarType == kStandard) && (fBarWidth == kProgressBarStandardWidth)){
451  out << ",TGProgressBar::kStandard";
452  }
453 
454  out << "," << GetHeight() <<");" << std::endl;
455 
456  if (option && strstr(option, "keep_names"))
457  out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
458 
459  if (GetFillType() == kBlockFill)
460  out << " " << GetName() <<"->SetFillType(TGProgressBar::kBlockFill);"<< std::endl;
461 
462  TGProgressBar::SavePrimitive(out, option);
463 }
464 
465 ////////////////////////////////////////////////////////////////////////////////
466 /// Save a horizontal progress bar as a C++ statement(s) on output stream out
467 
468 void TGHProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
469 {
470  char quote = '"';
471 
472  out <<" TGHProgressBar *";
473  out << GetName() <<" = new TGHProgressBar("<< fParent->GetName();
474 
475  if ((fBarType == kFancy) && (fBarWidth == kProgressBarTextWidth)) {
476  out << ",TGProgressBar::kFancy";
477  } else if ((fBarType == kStandard) && (fBarWidth == kProgressBarStandardWidth)){
478  out << ",TGProgressBar::kStandard";
479  }
480 
481  if (option && strstr(option, "keep_names"))
482  out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
483 
484  out << "," << GetWidth() << ");" << std::endl;
485 
486  if (GetFillType() == kBlockFill)
487  out << " " << GetName() <<"->SetFillType(TGProgressBar::kBlockFill);"<< std::endl;
488 
489  if (GetShowPos()) {
490  out << " " << GetName() <<"->ShowPosition(kTRUE,";
491  if (UsePercent()) {
492  out << "kTRUE,";
493  } else {
494  out << "kFALSE,";
495  }
496  out << quote << GetFormat() << quote << ");"<< std::endl;
497 
498  } else if (UsePercent() && !GetFillType()) {
499  out << " " << GetName() <<"->ShowPosition();" << std::endl;
500  }
501  TGProgressBar::SavePrimitive(out, option);
502 }
EBarType fBarType
Definition: TGProgressBar.h:46
Handle_t FontStruct_t
Definition: GuiTypes.h:38
const TGWindow * fParent
Definition: TGWindow.h:37
static const TGGC & GetDefaultGC()
Return default graphics context in use.
TString fFormat
Definition: TGProgressBar.h:47
virtual UInt_t GetOptions() const
Definition: TGFrame.h:244
Int_t fBorderWidth
Definition: TGFrame.h:140
void Increment(Float_t inc)
Increment progress position.
float Float_t
Definition: RtypesCore.h:53
const char Option_t
Definition: RtypesCore.h:62
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a vertical progress bar as a C++ statement(s) on output stream out.
static FontStruct_t GetDefaultFontStruct()
Return default font structure in use.
TGVProgressBar(const TGWindow *p=0, UInt_t w=kProgressBarTextWidth, UInt_t h=4, Pixel_t back=GetWhitePixel(), Pixel_t barcolor=GetDefaultSelectedBackground(), GContext_t norm=GetDefaultGC()(), FontStruct_t font=GetDefaultFontStruct(), UInt_t options=kDoubleBorder|kSunkenFrame)
cconstructor
void SetForeground(Pixel_t v)
Set foreground color.
Definition: TGGC.cxx:276
UInt_t GetHeight() const
Definition: TGFrame.h:272
TH1 * h
Definition: legend2.C:5
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition: TGFrame.cxx:691
virtual void DoRedraw()=0
Redraw the frame.
static const TGFont * fgDefaultFont
Definition: TGProgressBar.h:57
Handle_t GContext_t
Definition: GuiTypes.h:37
Basic string class.
Definition: TString.h:129
#define gClient
Definition: TGClient.h:166
static Pixel_t fgWhitePixel
Definition: TGFrame.h:150
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Bool_t GetShowPos() const
Definition: TGProgressBar.h:77
static Pixel_t fgDefaultSelectedBackground
Definition: TGFrame.h:149
Bool_t GetColorByName(const char *name, Pixel_t &pixel) const
Get a color by name.
Definition: TGClient.cxx:392
static std::string format(double x, double y, int digits, int width)
UInt_t GetWidth() const
Definition: TGFrame.h:271
Float_t delta
virtual void SetBarColor(Pixel_t color)
Set progress bar color.
static const char * PixelAsHexString(ULong_t pixel)
Convert machine dependent pixel value (obtained via RGB2Pixel or via Number2Pixel() or via TColor::Ge...
Definition: TColor.cxx:2010
virtual void DoRedraw()
Redraw the frame.
Definition: TGFrame.cxx:412
Double_t x[n]
Definition: legend1.C:17
TString GetFormat() const
Definition: TGProgressBar.h:78
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2345
ULong_t Pixel_t
Definition: GuiTypes.h:39
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition: TGFrame.cxx:665
virtual void Format(const char *format="%.2f")
Set format for displaying a value.
virtual void DoRedraw()
Draw horizontal progress bar.
virtual void SetForegroundColor(Pixel_t pixel)
Change text color drawing.
Pixel_t GetForeground() const
Definition: TGGC.h:82
void SetBarType(EBarType type)
Set bar type.
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition: TGWindow.cxx:221
unsigned int UInt_t
Definition: RtypesCore.h:42
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:873
Ssiz_t Length() const
Definition: TString.h:388
virtual Pixel_t GetBackground() const
Definition: TGFrame.h:239
GContext_t fNormGC
Definition: TGProgressBar.h:52
void ShowPosition(Bool_t set=kTRUE, Bool_t percent=kTRUE, const char *format="%.2f")
Show postion text, either in percent or formatted according format.
#define gVirtualX
Definition: TVirtualX.h:350
UInt_t fWidth
Definition: TGFrame.h:134
const Bool_t kFALSE
Definition: RtypesCore.h:92
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a horizontal progress bar as a C++ statement(s) on output stream out.
void SetRange(Float_t min, Float_t max)
Set min and max of progress bar.
EFillType GetFillType() const
Definition: TGProgressBar.h:75
#define ClassImp(name)
Definition: Rtypes.h:336
void SetPosition(Float_t pos)
Set progress position between [min,max].
GContext_t GetGC() const
Definition: TGGC.h:50
int type
Definition: TGX11.cxx:120
Definition: TGFont.h:149
unsigned long ULong_t
Definition: RtypesCore.h:51
FontStruct_t GetFontStruct() const
Definition: TGFont.h:193
Double_t y[n]
Definition: legend1.C:17
TGHProgressBar(const TGWindow *p=0, UInt_t w=4, UInt_t h=kProgressBarTextWidth, Pixel_t back=GetWhitePixel(), Pixel_t barcolor=GetDefaultSelectedBackground(), GContext_t norm=GetDefaultGC()(), FontStruct_t font=GetDefaultFontStruct(), UInt_t options=kDoubleBorder|kSunkenFrame)
Horizontal progress bar constructor.
EFillType fFillType
Definition: TGProgressBar.h:45
UInt_t fHeight
Definition: TGFrame.h:135
Handle_t fId
Definition: TGObject.h:36
virtual void Reset()
Reset progress bar (i.e. set pos to 0).
Bool_t UsePercent() const
Definition: TGProgressBar.h:80
TGClient * fClient
Definition: TGObject.h:37
static TGGC * fgDefaultGC
Definition: TGProgressBar.h:58
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save progress bar parameters as a C++ statement(s) on output stream out.
void NeedRedraw(TGWindow *w, Bool_t force=kFALSE)
Set redraw flags.
Definition: TGClient.cxx:370
UInt_t fEditDisabled
Definition: TGWindow.h:41
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition: TGFrame.cxx:2433
virtual void DoRedraw()
Draw vertical progress bar.
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition: TGFrame.cxx:2460
const Bool_t kTRUE
Definition: RtypesCore.h:91
double norm(double *x, double *p)
Definition: unuranDistr.cxx:40
Definition: TGGC.h:31
void SetFillType(EFillType type)
Set fill type.
static Pixel_t GetDefaultSelectedBackground()
Get default selected frame background.
Definition: TGFrame.cxx:678
FontStruct_t fFontStruct
Definition: TGProgressBar.h:53
const char * Data() const
Definition: TString.h:347