ROOT  6.06/09
Reference Guide
TGStatusBar.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Fons Rademakers 23/01/98
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  This source is based on Xclass95, a Win95-looking GUI toolkit.
14  Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.
15 
16  Xclass95 is free software; you can redistribute it and/or
17  modify it under the terms of the GNU Library General Public
18  License as published by the Free Software Foundation; either
19  version 2 of the License, or (at your option) any later version.
20 
21 **************************************************************************/
22 
23 //////////////////////////////////////////////////////////////////////////
24 // //
25 // TGStatusBar //
26 // //
27 // Provides a StatusBar widget. //
28 // //
29 //////////////////////////////////////////////////////////////////////////
30 
31 #include "TGStatusBar.h"
32 #include "TGResourcePool.h"
33 #include "TList.h"
34 #include "Riostream.h"
35 
36 
37 const TGFont *TGStatusBar::fgDefaultFont = 0;
38 TGGC *TGStatusBar::fgDefaultGC = 0;
39 
40 
41 class TGStatusBarPart : public TGHorizontalFrame {
42 friend class TGStatusBar;
43 private:
44  TGString *fStatusInfo; // status text to be displayed in this part
45  Int_t fYt; // y position of text in frame
46  virtual void DoRedraw();
47 
48 public:
50  ~TGStatusBarPart() { delete fStatusInfo; DestroyWindow(); }
51  void SetText(TGString *text);
52  const TGString *GetText() const { return fStatusInfo; }
53 };
54 
55 ////////////////////////////////////////////////////////////////////////////////
56 /// Create statusbar part frame. This frame will contain the text for this
57 /// statusbar part.
58 
59 TGStatusBarPart::TGStatusBarPart(const TGWindow *p, Int_t h, Int_t y, ULong_t back)
61 {
62  fStatusInfo = 0;
63  fYt = y + 1;
64  fHeight = h;
65  MapWindow();
66 
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Set text in this part of the statusbar.
72 
73 void TGStatusBarPart::SetText(TGString *text)
74 {
75  if (fStatusInfo) delete fStatusInfo;
76  fStatusInfo = text;
77  fClient->NeedRedraw(this);
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// Draw string in statusbar part frame.
82 
83 void TGStatusBarPart::DoRedraw()
84 {
86 
87  if (fStatusInfo)
88  fStatusInfo->Draw(fId, TGStatusBar::GetDefaultGC()(), 3, fYt);
89 }
90 
91 
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Create a status bar widget. By default it consist of one part.
96 /// Multiple parts can be created using SetParts().
97 
99  UInt_t options, ULong_t back) :
100  TGHorizontalFrame(p, w, h, options, back)
101 {
102  fBorderWidth = 2;
103  fStatusPart = new TGStatusBarPart* [1];
104  fParts = new Int_t [1];
105  fXt = new Int_t [1];
106  fParts[0] = 100;
107  fNpart = 1;
108  f3DCorner = kTRUE;
109 
110  int max_ascent, max_descent;
111  gVirtualX->GetFontProperties(GetDefaultFontStruct(), max_ascent, max_descent);
112  int ht = max_ascent + max_descent;
113 
114  fYt = max_ascent;
115 
116  fStatusPart[0] = new TGStatusBarPart(this, ht, fYt);
117  AddFrame(fStatusPart[0]);
118  Resize(w, ht + 5);
119 
120  //fEditDisabled = kEditDisableLayout;
121 }
122 
123 ////////////////////////////////////////////////////////////////////////////////
124 /// Delete status bar widget.
125 
127 {
128  if (!MustCleanup()) {
129  for (int i = 0; i < fNpart; i++) {
130  delete fStatusPart[i];
131  }
132  }
133 
134  delete [] fStatusPart;
135  delete [] fParts;
136  delete [] fXt;
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// Set text in partition partidx in status bar. The TGString is
141 /// adopted by the status bar.
142 
143 void TGStatusBar::SetText(TGString *text, Int_t partidx)
144 {
145  if (partidx < 0 || partidx >= fNpart) {
146  Error("SetText", "partidx out of range (0,%d)", fNpart-1);
147  return;
148  }
149 
150  fStatusPart[partidx]->SetText(text);
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Set text in partion partidx in status bar.
155 
156 void TGStatusBar::SetText(const char *text, Int_t partidx)
157 {
158  SetText(new TGString(text), partidx);
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// return text in the part partidx
163 
164 const char *TGStatusBar::GetText(Int_t partidx) const
165 {
166  if (partidx < 0 || partidx >= fNpart) {
167  Error("GetText", "partidx out of range (0,%d)", fNpart-1);
168  return 0;
169  }
170 
171  const TGString *str = fStatusPart[partidx]->GetText();
172  return str->Data();
173 }
174 
175 ////////////////////////////////////////////////////////////////////////////////
176 /// Draw the status bar border (including cute 3d corner).
177 
179 {
180  // Current width is known at this stage so calculate fXt's.
181  int i;
182  for (i = 0; i < fNpart; i++) {
183  if (i == 0)
184  fXt[i] = 0;
185  else
186  fXt[i] = fXt[i-1] + (fWidth * fParts[i-1] / 100);
187  }
188 
189  //TGFrame::DrawBorder();
190  for (i = 0; i < fNpart; i++) {
191  int xmax, xmin = fXt[i];
192  if (i == fNpart-1)
193  xmax = fWidth;
194  else
195  xmax = fXt[i+1] - 2;
196 
197  if (i == fNpart-1) {
198  if (f3DCorner)
199  fStatusPart[i]->MoveResize(fXt[i]+2, 1, xmax - fXt[i] - 15, fHeight - 2);
200  else
201  fStatusPart[i]->MoveResize(fXt[i]+2, 1, xmax - fXt[i], fHeight - 2);
202  } else
203  fStatusPart[i]->MoveResize(fXt[i]+2, 1, xmax - fXt[i] - 4, fHeight - 2);
204 
205  gVirtualX->DrawLine(fId, GetShadowGC()(), xmin, 0, xmax-2, 0);
206  gVirtualX->DrawLine(fId, GetShadowGC()(), xmin, 0, xmin, fHeight-2);
207  gVirtualX->DrawLine(fId, GetHilightGC()(), xmin, fHeight-1, xmax-1, fHeight-1);
208  if (i == fNpart-1)
209  gVirtualX->DrawLine(fId, GetHilightGC()(), xmax-1, fHeight-1, xmax-1, 0);
210  else
211  gVirtualX->DrawLine(fId, GetHilightGC()(), xmax-1, fHeight-1, xmax-1, 1);
212  }
213 
214  // 3d corner...
215  if (f3DCorner) {
216  gVirtualX->DrawLine(fId, GetShadowGC()(), fWidth-3, fHeight-2, fWidth-2, fHeight-3);
217  gVirtualX->DrawLine(fId, GetShadowGC()(), fWidth-4, fHeight-2, fWidth-2, fHeight-4);
218  gVirtualX->DrawLine(fId, GetHilightGC()(), fWidth-5, fHeight-2, fWidth-2, fHeight-5);
219 
220  gVirtualX->DrawLine(fId, GetShadowGC()(), fWidth-7, fHeight-2, fWidth-2, fHeight-7);
221  gVirtualX->DrawLine(fId, GetShadowGC()(), fWidth-8, fHeight-2, fWidth-2, fHeight-8);
222  gVirtualX->DrawLine(fId, GetHilightGC()(), fWidth-9, fHeight-2, fWidth-2, fHeight-9);
223 
224  gVirtualX->DrawLine(fId, GetShadowGC()(), fWidth-11, fHeight-2, fWidth-2, fHeight-11);
225  gVirtualX->DrawLine(fId, GetShadowGC()(), fWidth-12, fHeight-2, fWidth-2, fHeight-12);
226  gVirtualX->DrawLine(fId, GetHilightGC()(), fWidth-13, fHeight-2, fWidth-2, fHeight-13);
227 
228  gVirtualX->DrawLine(fId, GetBckgndGC()(), fWidth-13, fHeight-1, fWidth-1, fHeight-1);
229  gVirtualX->DrawLine(fId, GetBckgndGC()(), fWidth-1, fHeight-1, fWidth-1, fHeight-13);
230  }
231 }
232 
233 ////////////////////////////////////////////////////////////////////////////////
234 /// Redraw status bar.
235 
237 {
238  // calls DrawBorder()
240 
241  for (int i = 0; i < fNpart; i++)
242  fStatusPart[i]->DoRedraw();
243 }
244 
245 ////////////////////////////////////////////////////////////////////////////////
246 /// Divide the status bar in nparts. Size of each part is given in parts
247 /// array (percentual).
248 
249 void TGStatusBar::SetParts(Int_t *parts, Int_t npart)
250 {
251  if (npart < 1) {
252  Warning("SetParts", "must be at least one part");
253  npart = 1;
254  }
255  if (npart > 15) {
256  Error("SetParts", "to many parts (limit is 15)");
257  return;
258  }
259 
260  int i;
261  for (i = 0; i < fNpart; i++)
262  delete fStatusPart[i];
263 
264  delete [] fStatusPart;
265  delete [] fParts;
266  delete [] fXt;
267  fList->Delete();
268 
269  fStatusPart = new TGStatusBarPart* [npart];
270  fParts = new Int_t [npart];
271  fXt = new Int_t [npart];
272 
273  int tot = 0;
274  for (i = 0; i < npart; i++) {
275  fStatusPart[i] = new TGStatusBarPart(this, fHeight, fYt);
276  AddFrame(fStatusPart[i]);
277  fParts[i] = parts[i];
278  tot += parts[i];
279  if (tot > 100)
280  Error("SetParts", "sum of part > 100");
281  }
282  if (tot < 100)
283  fParts[npart-1] += 100 - tot;
284  fNpart = npart;
285 }
286 
287 ////////////////////////////////////////////////////////////////////////////////
288 /// Divide the status bar in npart equal sized parts.
289 
291 {
292  if (npart < 1) {
293  Warning("SetParts", "must be at least one part");
294  npart = 1;
295  }
296  if (npart > 40) {
297  Error("SetParts", "to many parts (limit is 40)");
298  return;
299  }
300 
301  int i;
302  for (i = 0; i < fNpart; i++)
303  delete fStatusPart[i];
304 
305  delete [] fStatusPart;
306  delete [] fParts;
307  delete [] fXt;
308  fList->Delete();
309 
310  fStatusPart = new TGStatusBarPart* [npart];
311  fParts = new Int_t [npart];
312  fXt = new Int_t [npart];
313 
314  int sz = 100/npart;
315  int tot = 0;
316  for (i = 0; i < npart; i++) {
317  fStatusPart[i] = new TGStatusBarPart(this, fHeight, fYt);
318  AddFrame(fStatusPart[i]);
319  fParts[i] = sz;
320  tot += sz;
321  }
322  if (tot < 100)
323  fParts[npart-1] += 100 - tot;
324  fNpart = npart;
325 }
326 
327 ////////////////////////////////////////////////////////////////////////////////
328 /// Return default font structure in use.
329 
331 {
332  if (!fgDefaultFont)
333  fgDefaultFont = gClient->GetResourcePool()->GetStatusFont();
334  return fgDefaultFont->GetFontStruct();
335 }
336 
337 ////////////////////////////////////////////////////////////////////////////////
338 /// Return default graphics context in use.
339 
341 {
342  if (!fgDefaultGC) {
343  fgDefaultGC = new TGGC(*gClient->GetResourcePool()->GetFrameGC());
345  }
346  return *fgDefaultGC;
347 }
348 
349 ////////////////////////////////////////////////////////////////////////////////
350 /// Returns bar part. That allows to put in the bar part
351 /// something more interesting than text ;-)
352 
354 {
355  return ((npart<fNpart) && (npart>=0)) ? (TGCompositeFrame*)fStatusPart[npart] : 0;
356 }
357 
358 ////////////////////////////////////////////////////////////////////////////////
359 /// Return default size.
360 
362 {
363  UInt_t h = fHeight;
364 
365  for (int i = 0; i < fNpart; i++) {
366  h = TMath::Max(h,fStatusPart[i]->GetDefaultHeight()+1);
367  }
368  return TGDimension(fWidth, h);
369 }
370 
371 ////////////////////////////////////////////////////////////////////////////////
372 /// Save a status bar widget as a C++ statement(s) on output stream out.
373 
374 void TGStatusBar::SavePrimitive(std::ostream &out, Option_t *option /*= ""*/)
375 {
376  if (fBackground != GetDefaultFrameBackground()) SaveUserColor(out, option);
377 
378  out << std::endl;
379  out << " // status bar" << std::endl;
380 
381  out << " TGStatusBar *";
382  out << GetName() <<" = new TGStatusBar("<< fParent->GetName()
383  << "," << GetWidth() << "," << GetHeight();
384 
387  out <<");" << std::endl;
388  } else {
389  out << "," << GetOptionString() <<");" << std::endl;
390  }
391  } else {
392  out << "," << GetOptionString() << ",ucolor);" << std::endl;
393  }
394  if (option && strstr(option, "keep_names"))
395  out << " " << GetName() << "->SetName(\"" << GetName() << "\");" << std::endl;
396 
397  int i; char quote = '"';
398 
399  if (fNpart > 1) {
400  out << " Int_t parts" << GetName()+5 << "[] = {" << fParts[0];
401 
402  for (i=1; i<fNpart; i++) {
403  out << "," << fParts[i];
404  }
405  out << "};" << std::endl;
406 
407  out << " " << GetName() << "->SetParts(parts" << GetName()+5
408  << "," << fNpart << ");" <<std::endl;
409  }
410  for (i=0; i<fNpart; i++) {
411  if (fStatusPart[i]->GetText()) {
412  out << " " << GetName() << "->SetText(" << quote
413  << fStatusPart[i]->GetText()->GetString()
414  << quote << "," << i << ");" << std::endl;
415  } else {
416  if (!fStatusPart[i]->GetList()->First()) continue;
417  out << " TGCompositeFrame *" << fStatusPart[i]->GetName()
418  << " = " << GetName() << "->GetBarPart(" << i << ");" << std::endl;
419 
420  TGFrameElement *el;
421  TIter next(fStatusPart[i]->GetList());
422 
423  while ((el = (TGFrameElement *) next())) {
424  el->fFrame->SavePrimitive(out, option);
425  out << " " << fStatusPart[i]->GetName() << "->AddFrame("
426  << el->fFrame->GetName();
427  el->fLayout->SavePrimitive(out, option);
428  out << ");" << std::endl;
429  }
430  }
431  }
432 }
Handle_t FontStruct_t
Definition: GuiTypes.h:40
const TGWindow * fParent
Definition: TGWindow.h:43
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:587
float xmin
Definition: THbookFile.cxx:93
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition: TGWindow.cxx:221
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:404
Int_t fBorderWidth
Definition: TGFrame.h:156
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
const char Option_t
Definition: RtypesCore.h:62
static const TGGC & GetHilightGC()
Get highlight color graphics context.
Definition: TGFrame.cxx:737
TH1 * h
Definition: legend2.C:5
UInt_t GetWidth() const
Definition: TGFrame.h:287
FontH_t GetFontHandle() const
Definition: TGFont.h:198
static const TGGC & GetDefaultGC()
Return default graphics context in use.
virtual TList * GetList() const
Definition: TGFrame.h:385
Pixel_t fBackground
Definition: TGFrame.h:158
#define gClient
Definition: TGClient.h:174
int Int_t
Definition: RtypesCore.h:41
UInt_t GetHeight() const
Definition: TGFrame.h:288
friend class TGStatusBarPart
Definition: TGStatusBar.h:33
const char * Data() const
Definition: TString.h:349
virtual void DoRedraw()
Redraw the frame.
Definition: TGFrame.cxx:412
TGDimension GetDefaultSize() const
Return default size.
TGLayoutHints * fLayout
Definition: TGLayout.h:127
static Pixel_t GetDefaultFrameBackground()
Get default frame background.
Definition: TGFrame.cxx:665
virtual UInt_t GetOptions() const
Definition: TGFrame.h:260
Bool_t f3DCorner
Definition: TGStatusBar.h:45
virtual void SetText(TGString *text, Int_t partidx=0)
Set text in partition partidx in status bar.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
TGStatusBarPart ** fStatusPart
Definition: TGStatusBar.h:40
char * out
Definition: TBase64.cxx:29
Int_t * fParts
Definition: TGStatusBar.h:41
virtual Int_t MustCleanup() const
Definition: TGFrame.h:436
void SetFont(FontH_t v)
Set font.
Definition: TGGC.cxx:409
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a frame widget as a C++ statement(s) on output stream out.
Definition: TGFrame.cxx:3188
TList * fList
Definition: TGFrame.h:367
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a primitive as a C++ statement(s) on output stream "out".
Definition: TGLayout.cxx:1003
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:389
unsigned int UInt_t
Definition: RtypesCore.h:42
TGFrame * fFrame
Definition: TGLayout.h:125
float xmax
Definition: THbookFile.cxx:93
#define gVirtualX
Definition: TVirtualX.h:362
UInt_t fWidth
Definition: TGFrame.h:150
static TGGC * fgDefaultGC
Definition: TGStatusBar.h:48
TText * text
Definition: TGFont.h:155
unsigned long ULong_t
Definition: RtypesCore.h:51
Double_t y[n]
Definition: legend1.C:17
UInt_t fHeight
Definition: TGFrame.h:151
ClassImp(TGStatusBar) TGStatusBar
Create a status bar widget.
Definition: TGStatusBar.cxx:92
const char * GetText(Int_t partidx=0) const
return text in the part partidx
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save a status bar widget as a C++ statement(s) on output stream out.
virtual void DrawBorder()
Draw the status bar border (including cute 3d corner).
Handle_t fId
Definition: TGObject.h:40
virtual ~TGStatusBar()
Delete status bar widget.
static const TGFont * fgDefaultFont
Definition: TGStatusBar.h:47
FontStruct_t GetFontStruct() const
Definition: TGFont.h:199
virtual void MapWindow()
Definition: TGFrame.h:267
TGClient * fClient
Definition: TGObject.h:41
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:202
Int_t * fXt
Definition: TGStatusBar.h:44
void NeedRedraw(TGWindow *w, Bool_t force=kFALSE)
Set redraw flags.
Definition: TGClient.cxx:370
UInt_t fEditDisabled
Definition: TGWindow.h:47
virtual void SetParts(Int_t npart)
Divide the status bar in npart equal sized parts.
void SaveUserColor(std::ostream &out, Option_t *)
Save a user color in a C++ macro file - used in SavePrimitive().
Definition: TGFrame.cxx:2433
Int_t fNpart
Definition: TGStatusBar.h:42
virtual void DoRedraw()
Redraw status bar.
const Bool_t kTRUE
Definition: Rtypes.h:91
static FontStruct_t GetDefaultFontStruct()
Return default font structure in use.
Definition: TGGC.h:35
TString GetOptionString() const
Returns a frame option string - used in SavePrimitive().
Definition: TGFrame.cxx:2460
static const TGGC & GetShadowGC()
Get shadow color graphics context.
Definition: TGFrame.cxx:747
static const TGGC & GetBckgndGC()
Get background color graphics context.
Definition: TGFrame.cxx:757
TGCompositeFrame * GetBarPart(Int_t npart) const
Returns bar part.
virtual void DestroyWindow()
Definition: TGWindow.h:94
virtual void Draw(Drawable_t id, GContext_t gc, Int_t x, Int_t y)
Draw string.
Definition: TGString.cxx:51
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:904