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/** \class TGProgressBar
13 \ingroup guiwidgets
14
15The classes in this file implement progress bars. Progress bars can
16be used to show progress of tasks taking more then a few seconds.
17TGProgressBar is an abstract base class, use either TGHProgressBar
18or TGVProgressBar. TGHProgressBar can in addition show the position
19as text in the bar.
20
21*/
22
23
24#include "TGProgressBar.h"
25#include "TGResourcePool.h"
26#include "TColor.h"
27#include "TVirtualX.h"
28
29#include <iostream>
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)((fWidth - 2.0*fBorderWidth ) *(fPos - fMin) / (fMax - fMin)) + fBorderWidth;
273
274 Int_t pospix = fPosPix;
275
276 if (fFillType == kSolidFill)
277 gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
279 (fBorderWidth << 1));
280 else {
281 Int_t blocksize = kBlockSize;
282 Int_t delta = kBlockSpace;
283 Int_t pos = fBorderWidth;
284 while (pos < fPosPix) {
285 if (pos + blocksize > Int_t(fWidth)-fBorderWidth)
286 blocksize = fWidth-fBorderWidth-pos;
287 gVirtualX->FillRectangle(fId, fBarColorGC(), pos,
288 fBorderWidth, blocksize, fBarWidth -
289 (fBorderWidth << 1));
290 if (fDrawBar && fShowPos)
291 gVirtualX->ClearArea(fId, pos+blocksize, fBorderWidth,
292 delta, fBarWidth - (fBorderWidth << 1));
293
294 pos += blocksize + delta;
295 }
296 pospix = pos - delta;
297 }
298
299 if (fShowPos) {
300 TString buf;
301 if (fPercent)
302 buf = TString::Format("%d%%", Int_t((fPos-fMin)/(fMax-fMin)*100.));
303 else
305
306 Int_t x, y, max_ascent, max_descent;
307 UInt_t twidth = gVirtualX->TextWidth(fFontStruct, buf.Data(), buf.Length());
308 gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
309 UInt_t theight = max_ascent + max_descent;
310
311 x = (Int_t)((fWidth - twidth)*0.5);
312 y = (Int_t)((fHeight - theight)*0.5);
313
314 if (fDrawBar && fPosPix < Int_t(x+twidth))
315 gVirtualX->ClearArea(fId, pospix, fBorderWidth,
316 fWidth - pospix - fBorderWidth,
317 fBarWidth - (fBorderWidth << 1));
318
319 gVirtualX->DrawString(fId, fNormGC, x, y + max_ascent, buf.Data(), buf.Length());
320 }
321
323}
324
325////////////////////////////////////////////////////////////////////////////////
326/// Constructor
327
329 Pixel_t back, Pixel_t barcolor, GContext_t norm,
330 FontStruct_t font,UInt_t options) :
331 TGProgressBar(p, w, h, back, barcolor, norm, font, options)
332{
333 fBarWidth = w;
335}
336
337////////////////////////////////////////////////////////////////////////////////
338/// Simple constructor allow you to create either a standard progress
339/// bar, or a more fancy progress bar (fancy means: double sized border,
340/// white background and a bit wider to allow for text to be printed
341/// in the bar.
342
344 : TGProgressBar(p, type == kStandard ? kProgressBarStandardWidth :
345 kProgressBarTextWidth, h, type == kStandard ? GetDefaultFrameBackground() :
346 fgWhitePixel, fgDefaultSelectedBackground, GetDefaultGC()(),
347 GetDefaultFontStruct(),
348 type == kStandard ? kSunkenFrame : kDoubleBorder | kSunkenFrame)
349{
350 fBarType = type;
354}
355
356////////////////////////////////////////////////////////////////////////////////
357/// Draw vertical progress bar.
358
360{
361 if (!fDrawBar) {
362 // calls TGProgressBar::DrawBorder()
364 }
365
366 fPosPix = (Int_t)((fHeight - 2.0f*fBorderWidth) *(fPos - fMin) / (fMax - fMin)) + fBorderWidth;
367
368 if (fFillType == kSolidFill)
369 gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
372 else {
373 Int_t blocksize = kBlockSize;
374 Int_t delta = kBlockSpace;
375 Int_t pos = fBorderWidth;
376 while (pos < fPosPix) {
377 if (pos + blocksize > Int_t(fHeight)-fBorderWidth)
378 blocksize = fHeight-fBorderWidth-pos;
379 gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
380 fHeight - pos - blocksize, fBarWidth - (fBorderWidth << 1),
381 blocksize);
382 pos += blocksize + delta;
383 }
384 }
385
386 if (fShowPos) {
387 // not text shown for vertical progress bars
388 }
389
391}
392
393////////////////////////////////////////////////////////////////////////////////
394/// Save progress bar parameters as a C++ statement(s) on output stream out.
395
396void TGProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
397{
398 const char *barcolor;
399 char quote = '"';
400 switch (fBarType) {
401 case kFancy:
403 out << " " << GetName() << "->ChangeOptions(" << GetOptionString()
404 << ");" << std::endl;
405 if (GetBackground() != GetWhitePixel()) {
406 SaveUserColor(out, option);
407 out << " " << GetName() << "->SetBackgroundColor(ucolor);" << std::endl;
408 }
409 break;
410
411 case kStandard:
413 out << " " << GetName() << "->ChangeOptions(" << GetOptionString()
414 << ");" << std::endl;
416 SaveUserColor(out, option);
417 out << " " << GetName() << "->SetBackgroundColor(ucolor);" << std::endl;
418 }
419 break;
420 }
421
424 out << " " << GetName() <<"->SetBarColor(" << quote << barcolor << quote
425 << ");" << std::endl;
426 }
427
428 if (fMin != 0 && fMax != 100)
429 out << " " << GetName() << "->SetRange(" << fMin << "," << fMax << ");" << std::endl;
430
431 out <<" "<< GetName() <<"->SetPosition("<< fPos <<");"<< std::endl;
432
433}
434
435////////////////////////////////////////////////////////////////////////////////
436/// Save a vertical progress bar as a C++ statement(s) on output stream out.
437
438void TGVProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
439{
440
441 out << " TGVProgressBar *";
442 out << GetName() << " = new TGVProgressBar(" << fParent->GetName();
443
445 out << ",TGProgressBar::kFancy";
446 } else if ((fBarType == kStandard) && (fBarWidth == kProgressBarStandardWidth)){
447 out << ",TGProgressBar::kStandard";
448 }
449
450 out << "," << GetHeight() <<");" << std::endl;
451
452 if (option && strstr(option, "keep_names"))
453 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
454
455 if (GetFillType() == kBlockFill)
456 out << " " << GetName() <<"->SetFillType(TGProgressBar::kBlockFill);"<< std::endl;
457
458 TGProgressBar::SavePrimitive(out, option);
459}
460
461////////////////////////////////////////////////////////////////////////////////
462/// Save a horizontal progress bar as a C++ statement(s) on output stream out
463
464void TGHProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
465{
466 char quote = '"';
467
468 out <<" TGHProgressBar *";
469 out << GetName() <<" = new TGHProgressBar("<< fParent->GetName();
470
472 out << ",TGProgressBar::kFancy";
473 } else if ((fBarType == kStandard) && (fBarWidth == kProgressBarStandardWidth)){
474 out << ",TGProgressBar::kStandard";
475 }
476
477 if (option && strstr(option, "keep_names"))
478 out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
479
480 out << "," << GetWidth() << ");" << std::endl;
481
482 if (GetFillType() == kBlockFill)
483 out << " " << GetName() <<"->SetFillType(TGProgressBar::kBlockFill);"<< std::endl;
484
485 if (GetShowPos()) {
486 out << " " << GetName() <<"->ShowPosition(kTRUE,";
487 if (UsePercent()) {
488 out << "kTRUE,";
489 } else {
490 out << "kFALSE,";
491 }
492 out << quote << GetFormat() << quote << ");"<< std::endl;
493
494 } else if (UsePercent() && !GetFillType()) {
495 out << " " << GetName() <<"->ShowPosition();" << std::endl;
496 }
497 TGProgressBar::SavePrimitive(out, option);
498}
@ kSunkenFrame
Definition: GuiTypes.h:383
@ kDoubleBorder
Definition: GuiTypes.h:385
@ kOwnBackground
Definition: GuiTypes.h:391
@ kFillSolid
Definition: GuiTypes.h:51
Handle_t GContext_t
Graphics context handle.
Definition: GuiTypes.h:38
Handle_t FontStruct_t
Pointer to font structure.
Definition: GuiTypes.h:39
ULong_t Pixel_t
Pixel value.
Definition: GuiTypes.h:40
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:45
const Bool_t kFALSE
Definition: RtypesCore.h:101
unsigned long ULong_t
Definition: RtypesCore.h:55
float Float_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:100
const char Option_t
Definition: RtypesCore.h:66
#define ClassImp(name)
Definition: Rtypes.h:364
#define gClient
Definition: TGClient.h:157
int type
Definition: TGX11.cxx:121
#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:2162
Bool_t GetColorByName(const char *name, Pixel_t &pixel) const
Get a color by name.
Definition: TGClient.cxx:395
void NeedRedraw(TGWindow *w, Bool_t force=kFALSE)
Set redraw flags.
Definition: TGClient.cxx:372
Encapsulate fonts used in the GUI system.
Definition: TGFont.h:140
FontStruct_t GetFontStruct() const
Definition: TGFont.h:184
A subclasses of TGWindow, and is used as base class for some simple widgets (buttons,...
Definition: TGFrame.h:80
static Pixel_t GetDefaultSelectedBackground()
Get default selected frame background.
Definition: TGFrame.cxx:696
virtual void DoRedraw()
Redraw the frame.
Definition: TGFrame.cxx:430
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition: TGFrame.cxx:709
UInt_t fHeight
frame height
Definition: TGFrame.h:88
Int_t fBorderWidth
frame border width
Definition: TGFrame.h:93
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition: TGFrame.cxx:683
virtual UInt_t GetOptions() const
Definition: TGFrame.h:197
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition: TGFrame.cxx:2506
UInt_t fWidth
frame width
Definition: TGFrame.h:87
UInt_t GetHeight() const
Definition: TGFrame.h:225
virtual Pixel_t GetBackground() const
Definition: TGFrame.h:192
UInt_t GetWidth() const
Definition: TGFrame.h:224
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition: TGFrame.cxx:2479
Encapsulate a graphics context used in the low level graphics.
Definition: TGGC.h:22
GContext_t GetGC() const
Definition: TGGC.h:41
void SetFillStyle(Int_t v)
Set fill style (kFillSolid, kFillTiled, kFillStippled, kFillOpaeueStippled).
Definition: TGGC.cxx:345
Pixel_t GetForeground() const
Definition: TGGC.h:73
void SetForeground(Pixel_t v)
Set foreground color.
Definition: TGGC.cxx:278
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
Connection to display server.
Definition: TGObject.h:27
Handle_t fId
X11/Win32 Window identifier.
Definition: TGObject.h:26
The classes in this file implement progress bars.
Definition: TGProgressBar.h:19
static const TGFont * fgDefaultFont
Definition: TGProgressBar.h:45
Bool_t fPercent
show position in percent (default true)
Definition: TGProgressBar.h:37
void Increment(Float_t inc)
Increment progress position.
TString fFormat
format used to show position not in percent
Definition: TGProgressBar.h:35
static TGGC * fgDefaultGC
Definition: TGProgressBar.h:46
virtual void DoRedraw()=0
Redraw the frame.
EBarType fBarType
OPTION={GetMethod="GetBarType";SetMethod="SetBarType";Items=(kStandard="Standard",...
Definition: TGProgressBar.h:34
static const TGGC & GetDefaultGC()
Return default graphics context in use.
Float_t fPos
logical position [fMin,fMax]
Definition: TGProgressBar.h:30
Bool_t UsePercent() const
Definition: TGProgressBar.h:68
Float_t fMax
logical maximum value (default 100)
Definition: TGProgressBar.h:29
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
text drawing graphics context
Definition: TGProgressBar.h:40
Int_t fBarWidth
progress bar width
Definition: TGProgressBar.h:32
FontStruct_t fFontStruct
font used to draw position text
Definition: TGProgressBar.h:41
void SetPosition(Float_t pos)
Set progress position between [min,max].
Bool_t fDrawBar
if true draw only bar in DoRedraw()
Definition: TGProgressBar.h:38
Bool_t GetShowPos() const
Definition: TGProgressBar.h:65
TString GetFormat() const
Definition: TGProgressBar.h:66
static FontStruct_t GetDefaultFontStruct()
Return default font structure in use.
Float_t fMin
logical minimum value (default 0)
Definition: TGProgressBar.h:28
TGGC fBarColorGC
progress bar drawing context
Definition: TGProgressBar.h:39
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:24
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:63
Int_t fPosPix
position of progress bar in pixel coordinates
Definition: TGProgressBar.h:31
EFillType fFillType
OPTION={GetMethod="GetFillType";SetMethod="SetFillType";Items=(kSolidFill=Solid",kBlockFill="Block")}
Definition: TGProgressBar.h:33
Bool_t fShowPos
show position value (default false)
Definition: TGProgressBar.h:36
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)
Constructor.
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.
ROOT GUI Window base class.
Definition: TGWindow.h:23
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition: TGWindow.cxx:336
const TGWindow * fParent
Parent window.
Definition: TGWindow.h:28
@ kEditDisableHeight
window height cannot be edited
Definition: TGWindow.h:55
@ kEditDisableWidth
window width cannot be edited
Definition: TGWindow.h:56
UInt_t fEditDisabled
flags used for "guibuilding"
Definition: TGWindow.h:32
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:893
Basic string class.
Definition: TString.h:136
Ssiz_t Length() const
Definition: TString.h:410
const char * Data() const
Definition: TString.h:369
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:2336
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17