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
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;
55 fNormGC = norm;
56 fFontStruct = font;
58 fBarColorGC.SetForeground(barcolor);
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{
159
160 fClient->NeedRedraw(this);
161}
162
163////////////////////////////////////////////////////////////////////////////////
164/// Set progress bar color.
165
166void 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
177void 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();
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
239 : TGProgressBar(p, w, type == kStandard ? kProgressBarStandardWidth :
240 kProgressBarTextWidth, type == kStandard ? GetDefaultFrameBackground() :
241 fgWhitePixel, fgDefaultSelectedBackground, GetDefaultGC()(),
242 GetDefaultFontStruct(),
243 type == kStandard ? kSunkenFrame : kDoubleBorder | kSunkenFrame)
244{
245 fBarType = type;
248}
249
250////////////////////////////////////////////////////////////////////////////////
251/// Show postion text, either in percent or formatted according format.
252
253void 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) +
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;
284 Int_t delta = kBlockSpace;
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
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
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
346 : TGProgressBar(p, type == kStandard ? kProgressBarStandardWidth :
347 kProgressBarTextWidth, h, type == kStandard ? GetDefaultFrameBackground() :
348 fgWhitePixel, fgDefaultSelectedBackground, GetDefaultGC()(),
349 GetDefaultFontStruct(),
350 type == kStandard ? kSunkenFrame : kDoubleBorder | kSunkenFrame)
351{
352 fBarType = type;
356}
357
358////////////////////////////////////////////////////////////////////////////////
359/// Draw vertical progress bar.
360
362{
363 if (!fDrawBar) {
364 // calls TGProgressBar::DrawBorder()
366 }
367
369 (fPos - fMin) / (fMax - fMin) +
371
372 if (fFillType == kSolidFill)
373 gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
376 else {
377 Int_t blocksize = kBlockSize;
378 Int_t delta = kBlockSpace;
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
395}
396
397////////////////////////////////////////////////////////////////////////////////
398/// Save progress bar parameters as a C++ statement(s) on output stream out.
399
400void 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
442void TGVProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
443{
444
445 out << " TGVProgressBar *";
446 out << GetName() << " = new TGVProgressBar(" << fParent->GetName();
447
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
468void TGHProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
469{
470 char quote = '"';
471
472 out <<" TGHProgressBar *";
473 out << GetName() <<" = new TGHProgressBar("<< fParent->GetName();
474
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}
@ 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:41
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
unsigned long ULong_t
Definition: RtypesCore.h:51
bool Bool_t
Definition: RtypesCore.h:59
float Float_t
Definition: RtypesCore.h:53
const Bool_t kTRUE
Definition: RtypesCore.h:87
const char Option_t
Definition: RtypesCore.h:62
#define ClassImp(name)
Definition: Rtypes.h:365
#define gClient
Definition: TGClient.h:166
@ kSunkenFrame
Definition: TGFrame.h:61
@ kDoubleBorder
Definition: TGFrame.h:63
@ kOwnBackground
Definition: TGFrame.h:69
int type
Definition: TGX11.cxx:120
#define gVirtualX
Definition: TVirtualX.h:345
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:2096
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:678
virtual void DoRedraw()
Redraw the frame.
Definition: TGFrame.cxx:412
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition: TGFrame.cxx:691
UInt_t fHeight
Definition: TGFrame.h:135
Int_t fBorderWidth
Definition: TGFrame.h:140
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition: TGFrame.cxx:665
virtual UInt_t GetOptions() const
Definition: TGFrame.h:244
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition: TGFrame.cxx:2462
UInt_t fWidth
Definition: TGFrame.h:134
UInt_t GetHeight() const
Definition: TGFrame.h:272
virtual Pixel_t GetBackground() const
Definition: TGFrame.h:239
UInt_t GetWidth() const
Definition: TGFrame.h:271
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition: TGFrame.cxx:2435
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.
@ kProgressBarStandardWidth
Definition: TGProgressBar.h:36
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.
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:221
const TGWindow * fParent
Definition: TGWindow.h:37
@ kEditDisableHeight
Definition: TGWindow.h:64
@ kEditDisableWidth
Definition: TGWindow.h:65
UInt_t fEditDisabled
Definition: TGWindow.h:41
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:880
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