Logo ROOT  
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#include "TVirtualX.h"
30
31
34
35
39
40////////////////////////////////////////////////////////////////////////////////
41/// Create progress bar.
42
44 ULong_t back, ULong_t barcolor, GContext_t norm,
45 FontStruct_t font, UInt_t options) :
46 TGFrame(p, w, h, options | kOwnBackground, back)
47{
48 fMin = 0;
49 fMax = 100;
50 fPos = 0;
51 fPosPix = 0;
56 fNormGC = norm;
57 fFontStruct = font;
59 fBarColorGC.SetForeground(barcolor);
62}
63
64////////////////////////////////////////////////////////////////////////////////
65/// Set min and max of progress bar.
66
68{
69 if (min >= max) {
70 Error("SetRange", "max must be > min");
71 return;
72 }
73
74 Bool_t draw = kFALSE;
75 if (fPos > fMin) {
76 // already in progress... rescale
77 if (fPos < min) fPos = min;
78 if (fPos > max) fPos = max;
79 draw = kTRUE;
80 } else
81 fPos = min;
82
83 fMin = min;
84 fMax = max;
85
86 if (draw)
87 DoRedraw();
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// Set progress position between [min,max].
92
94{
95 if (pos < fMin) pos = fMin;
96 if (pos > fMax) pos = fMax;
97
98 if (fPos == pos)
99 return;
100
101 fPos = pos;
102
103 //fClient->NeedRedraw(this);
104 fDrawBar = kTRUE;
105 DoRedraw();
106}
107
108////////////////////////////////////////////////////////////////////////////////
109/// Increment progress position.
110
112{
113 if (fPos == fMax)
114 return;
115
116 fPos += inc;
117 if (fPos > fMax) fPos = fMax;
118
119 //fClient->NeedRedraw(this);
120 fDrawBar = kTRUE;
121 DoRedraw();
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// Reset progress bar (i.e. set pos to 0).
126
128{
129 fPos = 0;
130
131 fClient->NeedRedraw(this);
132}
133
134////////////////////////////////////////////////////////////////////////////////
135/// Set fill type.
136
138{
139 fFillType = type;
140
141 fClient->NeedRedraw(this);
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Set bar type.
146
148{
149 fBarType = type;
150
151 fClient->NeedRedraw(this);
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Set progress bar color.
156
158{
160
161 fClient->NeedRedraw(this);
162}
163
164////////////////////////////////////////////////////////////////////////////////
165/// Set progress bar color.
166
167void TGProgressBar::SetBarColor(const char *color)
168{
169 ULong_t ic;
170 fClient->GetColorByName(color, ic);
172 fClient->NeedRedraw(this);
173}
174
175////////////////////////////////////////////////////////////////////////////////
176/// Set format for displaying a value.
177
178void TGProgressBar::Format(const char *format)
179{
180 fFormat = format;
181
182 fClient->NeedRedraw(this);
183}
184
185////////////////////////////////////////////////////////////////////////////////
186/// Return default font structure in use.
187
189{
190 if (!fgDefaultFont)
191 fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Return default graphics context in use.
197
199{
200 if (!fgDefaultGC)
201 fgDefaultGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
202 return *fgDefaultGC;
203}
204
205////////////////////////////////////////////////////////////////////////////////
206/// Change text color drawing.
207
209{
210 TGGC *gc = gClient->GetResourcePool()->GetGCPool()->FindGC(fNormGC);
211
212 if (!gc) {
213 return;
214 }
215 gc->SetForeground(pixel);
216 fNormGC = gc->GetGC();
217
218 fClient->NeedRedraw(this);
219}
220
221////////////////////////////////////////////////////////////////////////////////
222/// Horizontal progress bar constructor.
223
225 Pixel_t back, Pixel_t barcolor,
226 GContext_t norm, FontStruct_t font, UInt_t options) :
227 TGProgressBar(p, w, h, back, barcolor, norm, font, options)
228{
229 fBarWidth = h;
231}
232
233////////////////////////////////////////////////////////////////////////////////
234/// Simple constructor allow you to create either a standard progress
235/// bar, or a more fancy progress bar (fancy means: double sized border,
236/// white background and a bit wider to allow for text to be printed
237/// in the bar.
238
240 : TGProgressBar(p, w, type == kStandard ? kProgressBarStandardWidth :
241 kProgressBarTextWidth, type == kStandard ? GetDefaultFrameBackground() :
242 fgWhitePixel, fgDefaultSelectedBackground, GetDefaultGC()(),
243 GetDefaultFontStruct(),
244 type == kStandard ? kSunkenFrame : kDoubleBorder | kSunkenFrame)
245{
246 fBarType = type;
249}
250
251////////////////////////////////////////////////////////////////////////////////
252/// Show postion text, either in percent or formatted according format.
253
254void TGHProgressBar::ShowPosition(Bool_t set, Bool_t percent, const char *format)
255{
256 fShowPos = set;
257 fPercent = percent;
258 fFormat = format;
259
260 fClient->NeedRedraw(this);
261}
262
263////////////////////////////////////////////////////////////////////////////////
264/// Draw horizontal progress bar.
265
267{
268 if (!fDrawBar) {
269 // calls TGProgressBar::DrawBorder()
271 }
272
273 fPosPix = Int_t(((Float_t)fWidth - (fBorderWidth << 1)) *
274 (fPos - fMin) / (fMax - fMin) +
276
277 Int_t pospix = fPosPix;
278
279 if (fFillType == kSolidFill)
280 gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
282 (fBorderWidth << 1));
283 else {
284 Int_t blocksize = kBlockSize;
285 Int_t delta = kBlockSpace;
286 Int_t pos = fBorderWidth;
287 while (pos < fPosPix) {
288 if (pos + blocksize > Int_t(fWidth)-fBorderWidth)
289 blocksize = fWidth-fBorderWidth-pos;
290 gVirtualX->FillRectangle(fId, fBarColorGC(), pos,
291 fBorderWidth, blocksize, fBarWidth -
292 (fBorderWidth << 1));
293 if (fDrawBar && fShowPos)
294 gVirtualX->ClearArea(fId, pos+blocksize, fBorderWidth,
295 delta, fBarWidth - (fBorderWidth << 1));
296
297 pos += blocksize + delta;
298 }
299 pospix = pos - delta;
300 }
301
302 if (fShowPos) {
303 TString buf;
304 if (fPercent)
305 buf = TString::Format("%d%%", Int_t((fPos-fMin)/(fMax-fMin)*100.));
306 else
308
309 Int_t x, y, max_ascent, max_descent;
310 UInt_t twidth = gVirtualX->TextWidth(fFontStruct, buf.Data(), buf.Length());
311 gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
312 UInt_t theight = max_ascent + max_descent;
313
314 x = (fWidth - twidth) >> 1;
315 y = (fHeight - theight) >> 1;
316
317 if (fDrawBar && fPosPix < Int_t(x+twidth))
318 gVirtualX->ClearArea(fId, pospix, fBorderWidth,
319 fWidth - pospix - fBorderWidth,
320 fBarWidth - (fBorderWidth << 1));
321
322 gVirtualX->DrawString(fId, fNormGC, x, y + max_ascent, buf.Data(), buf.Length());
323 }
324
326}
327
328////////////////////////////////////////////////////////////////////////////////
329/// cconstructor
330
332 Pixel_t back, Pixel_t barcolor, GContext_t norm,
333 FontStruct_t font,UInt_t options) :
334 TGProgressBar(p, w, h, back, barcolor, norm, font, options)
335{
336 fBarWidth = w;
338}
339
340////////////////////////////////////////////////////////////////////////////////
341/// Simple constructor allow you to create either a standard progress
342/// bar, or a more fancy progress bar (fancy means: double sized border,
343/// white background and a bit wider to allow for text to be printed
344/// in the bar.
345
347 : TGProgressBar(p, type == kStandard ? kProgressBarStandardWidth :
348 kProgressBarTextWidth, h, type == kStandard ? GetDefaultFrameBackground() :
349 fgWhitePixel, fgDefaultSelectedBackground, GetDefaultGC()(),
350 GetDefaultFontStruct(),
351 type == kStandard ? kSunkenFrame : kDoubleBorder | kSunkenFrame)
352{
353 fBarType = type;
357}
358
359////////////////////////////////////////////////////////////////////////////////
360/// Draw vertical progress bar.
361
363{
364 if (!fDrawBar) {
365 // calls TGProgressBar::DrawBorder()
367 }
368
370 (fPos - fMin) / (fMax - fMin) +
372
373 if (fFillType == kSolidFill)
374 gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
377 else {
378 Int_t blocksize = kBlockSize;
379 Int_t delta = kBlockSpace;
380 Int_t pos = fBorderWidth;
381 while (pos < fPosPix) {
382 if (pos + blocksize > Int_t(fHeight)-fBorderWidth)
383 blocksize = fHeight-fBorderWidth-pos;
384 gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
385 fHeight - pos - blocksize, fBarWidth - (fBorderWidth << 1),
386 blocksize);
387 pos += blocksize + delta;
388 }
389 }
390
391 if (fShowPos) {
392 // not text shown for vertical progress bars
393 }
394
396}
397
398////////////////////////////////////////////////////////////////////////////////
399/// Save progress bar parameters as a C++ statement(s) on output stream out.
400
401void TGProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
402{
403 const char *barcolor;
404 char quote = '"';
405 switch (fBarType) {
406 case kFancy:
408 out << " " << GetName() << "->ChangeOptions(" << GetOptionString()
409 << ");" << std::endl;
410 if (GetBackground() != GetWhitePixel()) {
411 SaveUserColor(out, option);
412 out << " " << GetName() << "->SetBackgroundColor(ucolor);" << std::endl;
413 }
414 break;
415
416 case kStandard:
418 out << " " << GetName() << "->ChangeOptions(" << GetOptionString()
419 << ");" << std::endl;
421 SaveUserColor(out, option);
422 out << " " << GetName() << "->SetBackgroundColor(ucolor);" << std::endl;
423 }
424 break;
425 }
426
429 out << " " << GetName() <<"->SetBarColor(" << quote << barcolor << quote
430 << ");" << std::endl;
431 }
432
433 if (fMin != 0 && fMax != 100)
434 out << " " << GetName() << "->SetRange(" << fMin << "," << fMax << ");" << std::endl;
435
436 out <<" "<< GetName() <<"->SetPosition("<< fPos <<");"<< std::endl;
437
438}
439
440////////////////////////////////////////////////////////////////////////////////
441/// Save a vertical progress bar as a C++ statement(s) on output stream out.
442
443void TGVProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
444{
445
446 out << " TGVProgressBar *";
447 out << GetName() << " = new TGVProgressBar(" << fParent->GetName();
448
450 out << ",TGProgressBar::kFancy";
451 } else if ((fBarType == kStandard) && (fBarWidth == kProgressBarStandardWidth)){
452 out << ",TGProgressBar::kStandard";
453 }
454
455 out << "," << GetHeight() <<");" << std::endl;
456
457 if (option && strstr(option, "keep_names"))
458 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
459
460 if (GetFillType() == kBlockFill)
461 out << " " << GetName() <<"->SetFillType(TGProgressBar::kBlockFill);"<< std::endl;
462
463 TGProgressBar::SavePrimitive(out, option);
464}
465
466////////////////////////////////////////////////////////////////////////////////
467/// Save a horizontal progress bar as a C++ statement(s) on output stream out
468
469void TGHProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
470{
471 char quote = '"';
472
473 out <<" TGHProgressBar *";
474 out << GetName() <<" = new TGHProgressBar("<< fParent->GetName();
475
477 out << ",TGProgressBar::kFancy";
478 } else if ((fBarType == kStandard) && (fBarWidth == kProgressBarStandardWidth)){
479 out << ",TGProgressBar::kStandard";
480 }
481
482 if (option && strstr(option, "keep_names"))
483 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
484
485 out << "," << GetWidth() << ");" << std::endl;
486
487 if (GetFillType() == kBlockFill)
488 out << " " << GetName() <<"->SetFillType(TGProgressBar::kBlockFill);"<< std::endl;
489
490 if (GetShowPos()) {
491 out << " " << GetName() <<"->ShowPosition(kTRUE,";
492 if (UsePercent()) {
493 out << "kTRUE,";
494 } else {
495 out << "kFALSE,";
496 }
497 out << quote << GetFormat() << quote << ");"<< std::endl;
498
499 } else if (UsePercent() && !GetFillType()) {
500 out << " " << GetName() <<"->ShowPosition();" << std::endl;
501 }
502 TGProgressBar::SavePrimitive(out, option);
503}
@ kSunkenFrame
Definition: GuiTypes.h:383
@ kDoubleBorder
Definition: GuiTypes.h:385
@ kOwnBackground
Definition: GuiTypes.h:391
@ kFillSolid
Definition: GuiTypes.h:50
Handle_t GContext_t
Definition: GuiTypes.h:37
Handle_t FontStruct_t
Definition: GuiTypes.h:38
ULong_t Pixel_t
Definition: GuiTypes.h:39
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:43
const Bool_t kFALSE
Definition: RtypesCore.h:90
unsigned long ULong_t
Definition: RtypesCore.h:53
float Float_t
Definition: RtypesCore.h:55
const Bool_t kTRUE
Definition: RtypesCore.h:89
const char Option_t
Definition: RtypesCore.h:64
#define ClassImp(name)
Definition: Rtypes.h:361
#define gClient
Definition: TGClient.h:166
int type
Definition: TGX11.cxx:120
#define gVirtualX
Definition: TVirtualX.h:338
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:2109
Bool_t GetColorByName(const char *name, Pixel_t &pixel) const
Get a color by name.
Definition: TGClient.cxx:394
void NeedRedraw(TGWindow *w, Bool_t force=kFALSE)
Set redraw flags.
Definition: TGClient.cxx:372
Definition: TGFont.h:149
FontStruct_t GetFontStruct() const
Definition: TGFont.h:193
static Pixel_t GetDefaultSelectedBackground()
Get default selected frame background.
Definition: TGFrame.cxx:680
virtual void DoRedraw()
Redraw the frame.
Definition: TGFrame.cxx:414
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition: TGFrame.cxx:693
UInt_t fHeight
Definition: TGFrame.h:113
Int_t fBorderWidth
Definition: TGFrame.h:118
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition: TGFrame.cxx:667
virtual UInt_t GetOptions() const
Definition: TGFrame.h:222
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition: TGFrame.cxx:2464
UInt_t fWidth
Definition: TGFrame.h:112
UInt_t GetHeight() const
Definition: TGFrame.h:250
virtual Pixel_t GetBackground() const
Definition: TGFrame.h:217
UInt_t GetWidth() const
Definition: TGFrame.h:249
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition: TGFrame.cxx:2437
Definition: TGGC.h:31
GContext_t GetGC() const
Definition: TGGC.h:50
void SetFillStyle(Int_t v)
Set fill style (kFillSolid, kFillTiled, kFillStippled, kFillOpaeueStippled).
Definition: TGGC.cxx:343
Pixel_t GetForeground() const
Definition: TGGC.h:82
void SetForeground(Pixel_t v)
Set foreground color.
Definition: TGGC.cxx:276
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.
virtual void DoRedraw()
Draw horizontal progress bar.
void ShowPosition(Bool_t set=kTRUE, Bool_t percent=kTRUE, const char *format="%.2f")
Show postion text, either in percent or formatted according format.
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a horizontal progress bar as a C++ statement(s) on output stream out.
TGClient * fClient
Definition: TGObject.h:37
Handle_t fId
Definition: TGObject.h:36
static const TGFont * fgDefaultFont
Definition: TGProgressBar.h:57
void Increment(Float_t inc)
Increment progress position.
TString fFormat
Definition: TGProgressBar.h:47
static TGGC * fgDefaultGC
Definition: TGProgressBar.h:58
virtual void DoRedraw()=0
Redraw the frame.
EBarType fBarType
Definition: TGProgressBar.h:46
static const TGGC & GetDefaultGC()
Return default graphics context in use.
Bool_t UsePercent() const
Definition: TGProgressBar.h:80
TGProgressBar(const TGWindow *p, UInt_t w, UInt_t h, Pixel_t back=GetWhitePixel(), Pixel_t barcolor=GetDefaultSelectedBackground(), GContext_t norm=GetDefaultGC()(), FontStruct_t font=GetDefaultFontStruct(), UInt_t options=kDoubleBorder|kSunkenFrame)
Create progress bar.
virtual void SetForegroundColor(Pixel_t pixel)
Change text color drawing.
GContext_t fNormGC
Definition: TGProgressBar.h:52
FontStruct_t fFontStruct
Definition: TGProgressBar.h:53
void SetPosition(Float_t pos)
Set progress position between [min,max].
Bool_t GetShowPos() const
Definition: TGProgressBar.h:77
TString GetFormat() const
Definition: TGProgressBar.h:78
static FontStruct_t GetDefaultFontStruct()
Return default font structure in use.
virtual void Format(const char *format="%.2f")
Set format for displaying a value.
void SetFillType(EFillType type)
Set fill type.
virtual void SetBarColor(Pixel_t color)
Set progress bar color.
void SetBarType(EBarType type)
Set bar type.
@ kProgressBarStandardWidth
Definition: TGProgressBar.h:36
void SetRange(Float_t min, Float_t max)
Set min and max of progress bar.
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save progress bar parameters as a C++ statement(s) on output stream out.
EFillType GetFillType() const
Definition: TGProgressBar.h:75
EFillType fFillType
Definition: TGProgressBar.h:45
virtual void Reset()
Reset progress bar (i.e. set pos to 0).
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
virtual void DoRedraw()
Draw vertical progress bar.
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a vertical progress bar as a C++ statement(s) on output stream out.
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition: TGWindow.cxx:326
const TGWindow * fParent
Definition: TGWindow.h:36
@ kEditDisableHeight
Definition: TGWindow.h:63
@ kEditDisableWidth
Definition: TGWindow.h:64
UInt_t fEditDisabled
Definition: TGWindow.h:40
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:891
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
const char * Data() const
Definition: TString.h:364
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:2311
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17