Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
35
36////////////////////////////////////////////////////////////////////////////////
37/// Create progress bar.
38
59
60////////////////////////////////////////////////////////////////////////////////
61/// Set min and max of progress bar.
62
64{
65 if (min >= max) {
66 Error("SetRange", "max must be > min");
67 return;
68 }
69
71 if (fPos > fMin) {
72 // already in progress... rescale
73 if (fPos < min) fPos = min;
74 if (fPos > max) fPos = max;
75 draw = kTRUE;
76 } else
77 fPos = min;
78
79 fMin = min;
80 fMax = max;
81
82 if (draw)
83 DoRedraw();
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// Set progress position between [min,max].
88
90{
91 if (pos < fMin) pos = fMin;
92 if (pos > fMax) pos = fMax;
93
94 if (fPos == pos)
95 return;
96
97 fPos = pos;
98
99 //fClient->NeedRedraw(this);
100 fDrawBar = kTRUE;
101 DoRedraw();
102}
103
104////////////////////////////////////////////////////////////////////////////////
105/// Increment progress position.
106
108{
109 if (fPos == fMax)
110 return;
111
112 fPos += inc;
113 if (fPos > fMax) fPos = fMax;
114
115 //fClient->NeedRedraw(this);
116 fDrawBar = kTRUE;
117 DoRedraw();
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// Reset progress bar (i.e. set pos to 0).
122
124{
125 fPos = 0;
126
127 fClient->NeedRedraw(this);
128}
129
130////////////////////////////////////////////////////////////////////////////////
131/// Set fill type.
132
139
140////////////////////////////////////////////////////////////////////////////////
141/// Set bar type.
142
149
150////////////////////////////////////////////////////////////////////////////////
151/// Set progress bar color.
152
154{
156
157 fClient->NeedRedraw(this);
158}
159
160////////////////////////////////////////////////////////////////////////////////
161/// Set progress bar color.
162
163void TGProgressBar::SetBarColor(const char *color)
164{
165 ULong_t ic;
166 fClient->GetColorByName(color, ic);
168 fClient->NeedRedraw(this);
169}
170
171////////////////////////////////////////////////////////////////////////////////
172/// Set format for displaying a value.
173
175{
176 fFormat = format;
177
178 fClient->NeedRedraw(this);
179}
180
181////////////////////////////////////////////////////////////////////////////////
182/// Return default font structure in use.
183
185{
186 if (!fgDefaultFont)
187 fgDefaultFont = gClient->GetResourcePool()->GetDefaultFont();
188 return fgDefaultFont->GetFontStruct();
189}
190
191////////////////////////////////////////////////////////////////////////////////
192/// Return default graphics context in use.
193
195{
196 if (!fgDefaultGC)
197 fgDefaultGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
198 return *fgDefaultGC;
199}
200
201////////////////////////////////////////////////////////////////////////////////
202/// Change text color drawing.
203
205{
206 TGGC *gc = gClient->GetResourcePool()->GetGCPool()->FindGC(fNormGC);
207
208 if (!gc) {
209 return;
210 }
211 gc->SetForeground(pixel);
212 fNormGC = gc->GetGC();
213
214 fClient->NeedRedraw(this);
215}
216
217////////////////////////////////////////////////////////////////////////////////
218/// Horizontal progress bar constructor.
219
222 GContext_t norm, FontStruct_t font, UInt_t options) :
223 TGProgressBar(p, w, h, back, barcolor, norm, font, options)
224{
225 fBarWidth = h;
227}
228
229////////////////////////////////////////////////////////////////////////////////
230/// Simple constructor allow you to create either a standard progress
231/// bar, or a more fancy progress bar (fancy means: double sized border,
232/// white background and a bit wider to allow for text to be printed
233/// in the bar.
234
236 : TGProgressBar(p, w, type == kStandard ? kProgressBarStandardWidth :
237 kProgressBarTextWidth, type == kStandard ? GetDefaultFrameBackground() :
238 fgWhitePixel, fgDefaultSelectedBackground, GetDefaultGC()(),
239 GetDefaultFontStruct(),
240 type == kStandard ? kSunkenFrame : kDoubleBorder | kSunkenFrame)
241{
242 fBarType = type;
245}
246
247////////////////////////////////////////////////////////////////////////////////
248/// Show postion text, either in percent or formatted according format.
249
251{
252 fShowPos = set;
254 fFormat = format;
255
256 fClient->NeedRedraw(this);
257}
258
259////////////////////////////////////////////////////////////////////////////////
260/// Draw horizontal progress bar.
261
263{
264 if (!fDrawBar) {
265 // calls TGProgressBar::DrawBorder()
267 }
268
269 fPosPix = (Int_t)((fWidth - 2.0*fBorderWidth ) *(fPos - fMin) / (fMax - fMin)) + fBorderWidth;
270
272
273 if (fFillType == kSolidFill)
274 gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
276 (fBorderWidth << 1));
277 else {
279 Int_t delta = kBlockSpace;
280 Int_t pos = fBorderWidth;
281 while (pos < fPosPix) {
282 if (pos + blocksize > Int_t(fWidth)-fBorderWidth)
284 gVirtualX->FillRectangle(fId, fBarColorGC(), pos,
286 (fBorderWidth << 1));
287 if (fDrawBar && fShowPos)
288 gVirtualX->ClearArea(fId, pos+blocksize, fBorderWidth,
289 delta, fBarWidth - (fBorderWidth << 1));
290
291 pos += blocksize + delta;
292 }
293 pospix = pos - delta;
294 }
295
296 if (fShowPos) {
297 TString buf;
298 if (fPercent)
299 buf = TString::Format("%d%%", Int_t((fPos-fMin)/(fMax-fMin)*100.));
300 else
302
304 UInt_t twidth = gVirtualX->TextWidth(fFontStruct, buf.Data(), buf.Length());
305 gVirtualX->GetFontProperties(fFontStruct, max_ascent, max_descent);
307
308 x = (Int_t)((fWidth - twidth)*0.5);
309 y = (Int_t)((fHeight - theight)*0.5);
310
311 if (fDrawBar && fPosPix < Int_t(x+twidth))
312 gVirtualX->ClearArea(fId, pospix, fBorderWidth,
314 fBarWidth - (fBorderWidth << 1));
315
316 gVirtualX->DrawString(fId, fNormGC, x, y + max_ascent, buf.Data(), buf.Length());
317 }
318
320}
321
322////////////////////////////////////////////////////////////////////////////////
323/// Constructor
324
327 FontStruct_t font,UInt_t options) :
328 TGProgressBar(p, w, h, back, barcolor, norm, font, options)
329{
330 fBarWidth = w;
332}
333
334////////////////////////////////////////////////////////////////////////////////
335/// Simple constructor allow you to create either a standard progress
336/// bar, or a more fancy progress bar (fancy means: double sized border,
337/// white background and a bit wider to allow for text to be printed
338/// in the bar.
339
341 : TGProgressBar(p, type == kStandard ? kProgressBarStandardWidth :
342 kProgressBarTextWidth, h, type == kStandard ? GetDefaultFrameBackground() :
343 fgWhitePixel, fgDefaultSelectedBackground, GetDefaultGC()(),
344 GetDefaultFontStruct(),
345 type == kStandard ? kSunkenFrame : kDoubleBorder | kSunkenFrame)
346{
347 fBarType = type;
351}
352
353////////////////////////////////////////////////////////////////////////////////
354/// Draw vertical progress bar.
355
357{
358 if (!fDrawBar) {
359 // calls TGProgressBar::DrawBorder()
361 }
362
363 fPosPix = (Int_t)((fHeight - 2.0f*fBorderWidth) *(fPos - fMin) / (fMax - fMin)) + fBorderWidth;
364
365 if (fFillType == kSolidFill)
366 gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
369 else {
371 Int_t delta = kBlockSpace;
372 Int_t pos = fBorderWidth;
373 while (pos < fPosPix) {
374 if (pos + blocksize > Int_t(fHeight)-fBorderWidth)
376 gVirtualX->FillRectangle(fId, fBarColorGC(), fBorderWidth,
377 fHeight - pos - blocksize, fBarWidth - (fBorderWidth << 1),
378 blocksize);
379 pos += blocksize + delta;
380 }
381 }
382
383 if (fShowPos) {
384 // not text shown for vertical progress bars
385 }
386
388}
389
390////////////////////////////////////////////////////////////////////////////////
391/// Save progress bar parameters as a C++ statement(s) on output stream out.
392
393void TGProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
394{
395 switch (fBarType) {
396 case kFancy:
398 out << " " << GetName() << "->ChangeOptions(" << GetOptionString() << ");\n";
399 if (GetBackground() != GetWhitePixel()) {
400 SaveUserColor(out, option);
401 out << " " << GetName() << "->SetBackgroundColor(ucolor);\n";
402 }
403 break;
404
405 case kStandard:
407 out << " " << GetName() << "->ChangeOptions(" << GetOptionString() << ");\n";
409 SaveUserColor(out, option);
410 out << " " << GetName() << "->SetBackgroundColor(ucolor);\n";
411 }
412 break;
413 }
414
416 out << " " << GetName() << "->SetBarColor(\"" << TColor::PixelAsHexString(fBarColorGC.GetForeground())
417 << "\");\n";
418 }
419
420 if (fMin != 0 && fMax != 100)
421 out << " " << GetName() << "->SetRange(" << fMin << "," << fMax << ");\n";
422
423 out << " " << GetName() << "->SetPosition(" << fPos << ");\n";
424}
425
426////////////////////////////////////////////////////////////////////////////////
427/// Save a vertical progress bar as a C++ statement(s) on output stream out.
428
429void TGVProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
430{
431 out << " TGVProgressBar *" << GetName() << " = new TGVProgressBar(" << fParent->GetName();
432
434 out << ", TGProgressBar::kFancy";
436 out << ", TGProgressBar::kStandard";
437 out << ", " << GetHeight() << ");\n";
438
439 if (option && strstr(option, "keep_names"))
440 out << " " << GetName() << "->SetName(\"" << GetName() << "\");\n";
441
442 if (GetFillType() == kBlockFill)
443 out << " " << GetName() << "->SetFillType(TGProgressBar::kBlockFill);\n";
444
446}
447
448////////////////////////////////////////////////////////////////////////////////
449/// Save a horizontal progress bar as a C++ statement(s) on output stream out
450
451void TGHProgressBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
452{
453 out << " TGHProgressBar *" << GetName() << " = new TGHProgressBar(" << fParent->GetName();
454
456 out << ",TGProgressBar::kFancy";
457 } else if ((fBarType == kStandard) && (fBarWidth == kProgressBarStandardWidth)) {
458 out << ",TGProgressBar::kStandard";
459 }
460 out << "," << GetWidth() << ");\n";
461
462 if (option && strstr(option, "keep_names"))
463 out << " " << GetName() << "->SetName(\"" << GetName() << "\");\n";
464
465 if (GetFillType() == kBlockFill)
466 out << " " << GetName() << "->SetFillType(TGProgressBar::kBlockFill);\n";
467
468 if (GetShowPos()) {
469 out << " " << GetName() << "->ShowPosition(kTRUE,";
470 if (UsePercent()) {
471 out << "kTRUE,";
472 } else {
473 out << "kFALSE,";
474 }
475 out << "\"" << GetFormat() << "\");\n";
476
477 } else if (UsePercent() && !GetFillType()) {
478 out << " " << GetName() << "->ShowPosition();\n";
479 }
481}
Handle_t GContext_t
Graphics context handle.
Definition GuiTypes.h:38
@ kSunkenFrame
Definition GuiTypes.h:383
@ kDoubleBorder
Definition GuiTypes.h:385
@ kOwnBackground
Definition GuiTypes.h:391
@ kFillSolid
Definition GuiTypes.h:51
ULong_t Pixel_t
Pixel value.
Definition GuiTypes.h:40
Handle_t FontStruct_t
Pointer to font structure.
Definition GuiTypes.h:39
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:59
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
float Float_t
Float 4 bytes (float)
Definition RtypesCore.h:71
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gClient
Definition TGClient.h:157
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void pixel
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t format
Option_t Option_t TPoint TPoint percent
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void gc
#define gVirtualX
Definition TVirtualX.h:337
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:2542
Bool_t GetColorByName(const char *name, Pixel_t &pixel) const
Get a color by name.
Definition TGClient.cxx:403
void NeedRedraw(TGWindow *w, Bool_t force=kFALSE)
Set redraw flags.
Definition TGClient.cxx:380
Encapsulate fonts used in the GUI system.
Definition TGFont.h:140
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:688
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition TGFrame.cxx:701
UInt_t fHeight
frame height
Definition TGFrame.h:88
Int_t fBorderWidth
frame border width
Definition TGFrame.h:93
void DoRedraw() override
Redraw the frame.
Definition TGFrame.cxx:422
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition TGFrame.cxx:675
virtual UInt_t GetOptions() const
Definition TGFrame.h:199
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition TGFrame.cxx:2512
UInt_t fWidth
frame width
Definition TGFrame.h:87
UInt_t GetHeight() const
Definition TGFrame.h:227
virtual Pixel_t GetBackground() const
Definition TGFrame.h:194
UInt_t GetWidth() const
Definition TGFrame.h:226
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition TGFrame.cxx:2471
Encapsulate a graphics context used in the low level graphics.
Definition TGGC.h:22
void SetFillStyle(Int_t v)
Set fill style (kFillSolid, kFillTiled, kFillStippled, kFillOpaeueStippled).
Definition TGGC.cxx:344
Pixel_t GetForeground() const
Definition TGGC.h:73
void SetForeground(Pixel_t v)
Set foreground color.
Definition TGGC.cxx:277
void ShowPosition(Bool_t set=kTRUE, Bool_t percent=kTRUE, const char *format="%.2f")
Show postion text, either in percent or formatted according format.
void DoRedraw() override
Draw horizontal progress bar.
TGHProgressBar(const TGWindow *p=nullptr, 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.
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save a horizontal progress bar as a C++ statement(s) on output stream out.
TGClient * fClient
Connection to display server.
Definition TGObject.h:25
Handle_t fId
X11/Win32 Window identifier.
Definition TGObject.h:24
The classes in this file implement progress bars.
static const TGFont * fgDefaultFont
Bool_t fPercent
show position in percent (default true)
void Increment(Float_t inc)
Increment progress position.
TString fFormat
format used to show position not in percent
void SetForegroundColor(Pixel_t pixel) override
Change text color drawing.
static TGGC * fgDefaultGC
EBarType fBarType
OPTION={GetMethod="GetBarType";SetMethod="SetBarType";Items=(kStandard="Standard",...
static const TGGC & GetDefaultGC()
Return default graphics context in use.
Float_t fPos
logical position [fMin,fMax]
Bool_t UsePercent() const
Float_t fMax
logical maximum value (default 100)
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.
GContext_t fNormGC
text drawing graphics context
Int_t fBarWidth
progress bar width
FontStruct_t fFontStruct
font used to draw position text
void SetPosition(Float_t pos)
Set progress position between [min,max].
Bool_t fDrawBar
if true draw only bar in DoRedraw()
Bool_t GetShowPos() const
TString GetFormat() const
static FontStruct_t GetDefaultFontStruct()
Return default font structure in use.
Float_t fMin
logical minimum value (default 0)
TGGC fBarColorGC
progress bar drawing context
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 DoRedraw() override=0
Redraw the frame.
void SetRange(Float_t min, Float_t max)
Set min and max of progress bar.
EFillType GetFillType() const
Int_t fPosPix
position of progress bar in pixel coordinates
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save progress bar parameters as a C++ statement(s) on output stream out.
EFillType fFillType
OPTION={GetMethod="GetFillType";SetMethod="SetFillType";Items=(kSolidFill=Solid",kBlockFill="Block")}
Bool_t fShowPos
show position value (default false)
virtual void Reset()
Reset progress bar (i.e. set pos to 0).
void DoRedraw() override
Draw vertical progress bar.
void SavePrimitive(std::ostream &out, Option_t *option="") override
Save a vertical progress bar as a C++ statement(s) on output stream out.
TGVProgressBar(const TGWindow *p=nullptr, 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.
ROOT GUI Window base class.
Definition TGWindow.h:23
const TGWindow * fParent
Parent window.
Definition TGWindow.h:28
@ kEditDisableHeight
window height cannot be edited
Definition TGWindow.h:62
@ kEditDisableWidth
window width cannot be edited
Definition TGWindow.h:63
const char * GetName() const override
Return unique name, used in SavePrimitive methods.
Definition TGWindow.cxx:334
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:1071
Basic string class.
Definition TString.h:138
Ssiz_t Length() const
Definition TString.h:425
const char * Data() const
Definition TString.h:384
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:2384
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17