Logo ROOT  
Reference Guide
TBrowser.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Fons Rademakers 25/10/95
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 TBrowser
13 \ingroup Base
14 
15 Using a TBrowser one can browse all ROOT objects. It shows in a list
16 on the left side of the window all browsable ROOT classes. Selecting
17 one of the classes displays, in the icon-box on the right side, all
18 objects in the class. Selecting one of the objects in the icon-box,
19 will place all browsable objects in a new list and draws the
20 contents of the selected class in the icon-box. And so on....
21 
22 \image html base_browser.png
23 */
24 
25 #include "TBrowser.h"
26 #include "TGuiFactory.h"
27 #include "TROOT.h"
28 #include "TSystem.h"
29 #include "TStyle.h"
30 #include "TTimer.h"
31 #include "TContextMenu.h"
32 #include "TInterpreter.h"
33 #include "TVirtualMutex.h"
34 #include "TClass.h"
35 #include "TApplication.h"
36 
37 /** \class TBrowserTimer
38 */
39 
40 class TBrowserTimer : public TTimer {
41 
42 protected:
43  TBrowser *fBrowser;
44  Bool_t fActivate;
45 
46 public:
47  TBrowserTimer(TBrowser *b, Long_t ms = 1000)
48  : TTimer(ms, kTRUE), fBrowser(b), fActivate(kFALSE) { }
49  Bool_t Notify();
50 };
51 
52 /** \class TBrowserObject
53 This class is designed to wrap a Foreign object in order to
54 inject it into the Browse sub-system.
55 */
56 
57 class TBrowserObject : public TNamed
58 {
59 
60 public:
61 
62  TBrowserObject(void *obj, TClass *cl, const char *brname);
63  ~TBrowserObject(){;}
64 
65  void Browse(TBrowser* b);
66  Bool_t IsFolder() const;
67  TClass *IsA() const { return fClass; }
68 
69 private:
70  void *fObj; //! pointer to the foreign object
71  TClass *fClass; //! pointer to class of the foreign object
72 
73 };
74 
75 
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 // Make sure the application environment exists and the GUI libs are loaded
80 
82 {
83  // Make sure the application environment exists. It is need for graphics
84  // (colors are initialized in the TApplication ctor).
85  if (!gApplication)
87  // make sure that the Gpad and GUI libs are loaded
89  if (gApplication)
91  if (gROOT->IsBatch()) {
92  Warning("TBrowser", "The ROOT browser cannot run in batch mode");
93  return kFALSE;
94  }
95  return kTRUE;
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Create a new browser with a name, title. Width and height are by
100 /// default set to 640x400 and (optionally) adjusted by the screen factor
101 /// (depending on Rint.Canvas.UseScreenFactor to be true or false, default
102 /// is true).
103 
104 TBrowser::TBrowser(const char *name, const char *title, TBrowserImp *extimp,
105  Option_t *opt)
106  : TNamed(name, title), fLastSelectedObject(0), fImp(extimp), fTimer(0),
107  fContextMenu(0), fNeedRefresh(kFALSE)
108 {
109  if (!InitGraphics())
110  return;
112  fImp = 0;
113  } else {
115  UInt_t w = UInt_t(cx*800);
116  UInt_t h = UInt_t(cx*500);
117  if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
118  Create();
119  }
120 }
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Create a new browser with a name, title, width and height.
124 
125 TBrowser::TBrowser(const char *name, const char *title, UInt_t width,
126  UInt_t height, TBrowserImp *extimp, Option_t *opt)
127  : TNamed(name, title), fLastSelectedObject(0), fImp(extimp), fTimer(0), fContextMenu(0),
128  fNeedRefresh(kFALSE)
129 {
130  if (!InitGraphics())
131  return;
132  if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, width, height, opt);
133  Create();
134 }
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// Create a new browser with a name, title, position, width and height.
138 
139 TBrowser::TBrowser(const char *name, const char *title, Int_t x, Int_t y,
140  UInt_t width, UInt_t height, TBrowserImp *extimp, Option_t *opt)
141  : TNamed(name, title), fLastSelectedObject(0), fImp(extimp), fTimer(0), fContextMenu(0),
142  fNeedRefresh(kFALSE)
143 {
144  if (!InitGraphics())
145  return;
146  fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
147  Create();
148 }
149 
150 ////////////////////////////////////////////////////////////////////////////////
151 /// Create a new browser with a name, title, width and height for TObject *obj.
152 
153 TBrowser::TBrowser(const char *name, TObject *obj, const char *title, Option_t *opt)
154  : TNamed(name, title), fLastSelectedObject(0), fImp(0), fTimer(0), fContextMenu(0),
155  fNeedRefresh(kFALSE)
156 {
157  if (!InitGraphics())
158  return;
160  UInt_t w = UInt_t(cx*800);
161  UInt_t h = UInt_t(cx*500);
162 
163  if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
164  Create(obj);
165 }
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// Create a new browser with a name, title, width and height for TObject *obj.
169 
170 TBrowser::TBrowser(const char *name, TObject *obj, const char *title,
171  UInt_t width, UInt_t height, Option_t *opt)
172  : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
173  fNeedRefresh(kFALSE)
174 {
175  if (!InitGraphics())
176  return;
177  fImp = gGuiFactory->CreateBrowserImp(this, title, width, height, opt);
178  Create(obj);
179 }
180 
181 ////////////////////////////////////////////////////////////////////////////////
182 /// Create a new browser with a name, title, width and height for TObject *obj.
183 
184 TBrowser::TBrowser(const char *name, TObject *obj, const char *title,
185  Int_t x, Int_t y,
186  UInt_t width, UInt_t height, Option_t *opt)
187  : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
188  fNeedRefresh(kFALSE)
189 {
190  if (!InitGraphics())
191  return;
192  fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
193  Create(obj);
194 }
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 /// Create a new browser with a name, title, width and height for TObject *obj.
198 
199 TBrowser::TBrowser(const char *name, void *obj, TClass *cl,
200  const char *objname, const char *title, Option_t *opt)
201  : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
202  fNeedRefresh(kFALSE)
203 {
204  if (!InitGraphics())
205  return;
207  UInt_t w = UInt_t(cx*800);
208  UInt_t h = UInt_t(cx*500);
209 
210  fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
211 
212  Create(new TBrowserObject(obj,cl,objname));
213 }
214 
215 ////////////////////////////////////////////////////////////////////////////////
216 /// Create a new browser with a name, title, width and height for TObject *obj.
217 
218 TBrowser::TBrowser(const char *name, void *obj, TClass *cl,
219  const char *objname, const char *title,
220  UInt_t width, UInt_t height, Option_t *opt)
221  : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
222  fNeedRefresh(kFALSE)
223 {
224  if (!InitGraphics())
225  return;
226  fImp = gGuiFactory->CreateBrowserImp(this, title, width, height, opt);
227  Create(new TBrowserObject(obj,cl,objname));
228 }
229 
230 ////////////////////////////////////////////////////////////////////////////////
231 /// Create a new browser with a name, title, width and height for TObject *obj.
232 
233 TBrowser::TBrowser(const char *name,void *obj, TClass *cl,
234  const char *objname, const char *title,
235  Int_t x, Int_t y,
236  UInt_t width, UInt_t height, Option_t *opt)
237  : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
238  fNeedRefresh(kFALSE)
239 {
240  if (!InitGraphics())
241  return;
242  fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
243  Create(new TBrowserObject(obj,cl,objname));
244 }
245 
246 ////////////////////////////////////////////////////////////////////////////////
247 /// Delete the browser.
248 
250 {
251  Destructor();
252 }
253 
254 ////////////////////////////////////////////////////////////////////////////////
255 /// Actual browser destructor.
256 
258 {
259  if (fImp) fImp->CloseTabs();
261  gROOT->GetListOfBrowsers()->Remove(this);
264  SafeDelete(fImp);
265 }
266 
267 ////////////////////////////////////////////////////////////////////////////////
268 /// Add object with name to browser. If name not set the objects GetName()
269 /// is used. If check < 0 (default) no check box is drawn, if 0 then
270 /// unchecked checkbox is added, if 1 checked checkbox is added.
271 
272 void TBrowser::Add(TObject *obj, const char *name, Int_t check)
273 {
274  if (obj && fImp) {
275  fImp->Add(obj, name, check);
276  obj->SetBit(kMustCleanup);
277  }
278 }
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 /// Add foreign object with name to browser.
282 /// 'cl' is the type use to store the value of obj.
283 /// So literally the following pseudo code should be correct:
284 ///~~~ {.cpp}
285 /// `cl->GetName()` * ptr = (`cl->GetName()`*) obj;
286 ///~~~
287 /// and the value of obj is not necessarily the start of the object.
288 /// If check < 0 (default) no check box is drawn, if 0 then
289 /// unchecked checkbox is added, if 1 checked checkbox is added.
290 
291 void TBrowser::Add(void *obj, TClass *cl, const char *name, Int_t check)
292 {
293  if (!obj || !cl) return;
294  TObject *to;
295  if (cl->IsTObject()) to = (TObject*)cl->DynamicCast(TObject::Class(),obj,kTRUE);
296  else to = new TBrowserObject(obj,cl,name);
297 
298  if (!to) return;
299  Add(to,name,check);
300 }
301 
302 ////////////////////////////////////////////////////////////////////////////////
303 /// Add checkbox for this item.
304 
306 {
307  if (obj && fImp) {
308  fImp->AddCheckBox(obj, check);
309  }
310 }
311 
312 ////////////////////////////////////////////////////////////////////////////////
313 /// Change status of checkbox for this item.
314 
316 {
317  if (obj && fImp) {
318  fImp->CheckObjectItem(obj, check);
319  }
320 }
321 
322 ////////////////////////////////////////////////////////////////////////////////
323 /// Remove checkbox for this item.
324 
326 {
327  if (obj && fImp) {
328  fImp->RemoveCheckBox(obj);
329  }
330 }
331 
332 ////////////////////////////////////////////////////////////////////////////////
333 /// Create the browser, called by the ctors.
334 
336 {
338 
339  fTimer = new TBrowserTimer(this);
341 
343  gROOT->GetListOfBrowsers()->Add(this);
344 
345  // Get the list of globals
346  gROOT->GetListOfGlobals(kTRUE);
347  gROOT->GetListOfGlobalFunctions(kTRUE);
348 
349  fContextMenu = new TContextMenu("BrowserContextMenu") ;
350 
351  // Fill the first list from the present TObject obj
352  if (obj) {
353  Add(obj);
354  if (fImp) fImp->BrowseObj(obj);
355  } else if (fImp) {
356  // Fill the first list with all browsable classes from TROOT
357  fImp->BrowseObj(gROOT);
358  }
359 
360  // The first list will be filled by TWin32BrowserImp ctor
361  // with all browsable classes from TROOT
362 }
363 
364 ////////////////////////////////////////////////////////////////////////////////
365 /// Execute default action for selected object (action is specified
366 /// in the `$HOME/.root.mimes` or `$ROOTSYS/etc/root.mimes file`).
367 
369 {
370  if (obj && fImp)
372 }
373 
374 ////////////////////////////////////////////////////////////////////////////////
375 /// Recursively remove obj from browser.
376 
378 {
379  if (fImp && obj) {
380  fImp->RecursiveRemove(obj);
382  }
383 }
384 
385 ////////////////////////////////////////////////////////////////////////////////
386 /// Refresh browser contents.
387 
389 {
391  if (fImp) fImp->Refresh();
393 }
394 
395 ////////////////////////////////////////////////////////////////////////////////
396 /// Assign the last selected object.
397 
398 void TBrowser::SetSelected(TObject *clickedObject)
399 {
400  fLastSelectedObject = clickedObject;
401 }
402 
403 /** \class TBrowserTimer
404 Called whenever timer times out.
405 */
406 
407 Bool_t TBrowserTimer::Notify()
408 {
409  if (fBrowser) {
410  if (fBrowser->GetRefreshFlag()) {
411  fBrowser->SetRefreshFlag(kFALSE);
412  fActivate = kTRUE;
413  } else if (fActivate) {
414  fActivate = kFALSE;
415  fBrowser->Refresh();
416  }
417  }
418  Reset();
419 
420  return kFALSE;
421 }
422 
423 /** \class TBrowserObject
424 This is a wrapper class to emulate the TObject interface
425 around an object of a non-TObject class
426 */
427 
428 ////////////////////////////////////////////////////////////////////////////////
429 /// Constructor.
430 
431 TBrowserObject::TBrowserObject(void *obj, TClass *cl, const char *brname)
432  : TNamed(brname, cl ? cl->GetName() : ""), fObj(obj), fClass(cl)
433 {
434  if (cl==0) Fatal("Constructor","Class parameter should not be null");
436 }
437 
438 ////////////////////////////////////////////////////////////////////////////////
439 /// Return kTRUE if the object is a folder (contains browsable objects).
440 
441 Bool_t TBrowserObject::IsFolder() const
442 {
443  return fClass->IsFolder(fObj);
444 }
445 
446 ////////////////////////////////////////////////////////////////////////////////
447 /// Browse the content of the underlying object.
448 
449 void TBrowserObject::Browse(TBrowser* b)
450 {
451  fClass->Browse(fObj, b);
452 }
TObject::kMustCleanup
@ kMustCleanup
if object destructor must call RecursiveRemove()
Definition: TObject.h:60
TBrowser::SetSelected
void SetSelected(TObject *clickedObject)
Assign the last selected object.
Definition: TBrowser.cxx:398
TBrowserImp::BrowseObj
virtual void BrowseObj(TObject *)
Definition: TBrowserImp.h:48
TBrowser
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:100
TBrowser::fContextMenu
TContextMenu * fContextMenu
Browser's timer.
Definition: TBrowser.h:48
Option_t
const char Option_t
Definition: RtypesCore.h:66
TBrowser::~TBrowser
virtual ~TBrowser()
Delete the browser.
Definition: TBrowser.cxx:249
TBrowserImp::RemoveCheckBox
virtual void RemoveCheckBox(TObject *)
Definition: TBrowserImp.h:47
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TBrowser::RemoveCheckBox
void RemoveCheckBox(TObject *obj)
Remove checkbox for this item.
Definition: TBrowser.cxx:325
TApplication::InitializeGraphics
void InitializeGraphics()
Initialize the graphics environment.
Definition: TApplication.cxx:260
Float_t
float Float_t
Definition: RtypesCore.h:57
width
include TDocParser_001 C image html pict1_TDocParser_001 png width
Definition: TDocParser.cxx:121
TStyle.h
TObject::Fatal
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition: TObject.cxx:921
TBrowser.h
SafeDelete
#define SafeDelete(p)
Definition: RConfig.hxx:536
x
Double_t x[n]
Definition: legend1.C:17
TClass.h
TBrowserImp::CloseTabs
virtual void CloseTabs()
Definition: TBrowserImp.h:50
TTimer.h
TBrowser::Create
virtual void Create(TObject *obj=0)
Create the browser, called by the ctors.
Definition: TBrowser.cxx:335
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TBrowserImp::RecursiveRemove
virtual void RecursiveRemove(TObject *)
Definition: TBrowserImp.h:53
b
#define b(i)
Definition: RSha256.hxx:100
TSystem::AddTimer
virtual void AddTimer(TTimer *t)
Add timer to list of system timers.
Definition: TSystem.cxx:473
bool
TROOT.h
TApplication.h
TContextMenu
This class provides an interface to context sensitive popup menus.
Definition: TContextMenu.h:44
TObject::Browse
virtual void Browse(TBrowser *b)
Browse object. May be overridden for another default action.
Definition: TObject.cxx:121
TContextMenu.h
TTimer
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
TBrowser::CheckObjectItem
void CheckObjectItem(TObject *obj, Bool_t check=kFALSE)
Change status of checkbox for this item.
Definition: TBrowser.cxx:315
TStyle::GetScreenFactor
Float_t GetScreenFactor() const
Definition: TStyle.h:247
TBrowserImp
ABC describing GUI independent browser implementation protocol.
Definition: TBrowserImp.h:29
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TSystem.h
TBrowser::TBrowser
TBrowser(const TBrowser &)=delete
The last TObject selected by user.
gROOTMutex
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:61
h
#define h(i)
Definition: RSha256.hxx:106
TObject::SetBit
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:696
TNamed
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:101
TBrowser::fImp
TBrowserImp * fImp
Definition: TBrowser.h:46
TBrowserImp::Add
virtual void Add(TObject *, const char *, Int_t)
Definition: TBrowserImp.h:44
Long_t
long Long_t
Definition: RtypesCore.h:54
gGuiFactory
R__EXTERN TGuiFactory * gGuiFactory
Definition: TGuiFactory.h:66
gApplication
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
R__LOCKGUARD
#define R__LOCKGUARD(mutex)
Definition: TVirtualMutex.h:104
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
y
Double_t y[n]
Definition: legend1.C:17
TVirtualMutex.h
TObject::Warning
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:879
TClass::IsTObject
Bool_t IsTObject() const
Return kTRUE is the class inherits from TObject.
Definition: TClass.cxx:5922
TObject::kCanDelete
@ kCanDelete
if object in a list can be deleted
Definition: TObject.h:58
fClass
Cppyy::TCppType_t fClass
Definition: DeclareConverters.h:260
unsigned int
TBrowser::Refresh
void Refresh()
Refresh browser contents.
Definition: TBrowser.cxx:388
TBrowser::InitGraphics
Bool_t InitGraphics()
Definition: TBrowser.cxx:81
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TBrowser::fTimer
TBrowserTimer * fTimer
Window system specific browser implementation.
Definition: TBrowser.h:47
TBrowser::RecursiveRemove
virtual void RecursiveRemove(TObject *obj)
Recursively remove obj from browser.
Definition: TBrowser.cxx:377
TApplication::NeedGraphicsLibs
static void NeedGraphicsLibs()
Static method.
Definition: TApplication.cxx:252
TClass::DynamicCast
void * DynamicCast(const TClass *base, void *obj, Bool_t up=kTRUE)
Cast obj of this class type up to baseclass cl if up is true.
Definition: TClass.cxx:4899
TBrowser::Destructor
virtual void Destructor()
Actual browser destructor.
Definition: TBrowser.cxx:257
TBrowser::ExecuteDefaultAction
void ExecuteDefaultAction(TObject *obj)
Execute default action for selected object (action is specified in the $HOME/.root....
Definition: TBrowser.cxx:368
TInterpreter.h
TClass
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
TBrowserImp::Refresh
virtual void Refresh(Bool_t=kFALSE)
Definition: TBrowserImp.h:54
TBrowser::fNeedRefresh
Bool_t fNeedRefresh
Context menu pointer.
Definition: TBrowser.h:49
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
TBrowserImp::AddCheckBox
virtual void AddCheckBox(TObject *, Bool_t=kFALSE)
Definition: TBrowserImp.h:45
name
char name[80]
Definition: TGX11.cxx:110
TTimer::Notify
virtual Bool_t Notify()
Notify when timer times out.
Definition: TTimer.cxx:143
TApplication::CreateApplication
static void CreateApplication()
TClass::IsCallingNew
static ENewType IsCallingNew()
Static method returning the defConstructor flag passed to TClass::New().
Definition: TClass.cxx:5886
TGeant4Unit::ms
static constexpr double ms
Definition: TGeant4SystemOfUnits.h:163
TGuiFactory::CreateBrowserImp
virtual TBrowserImp * CreateBrowserImp(TBrowser *b, const char *title, UInt_t width, UInt_t height, Option_t *opt="")
Create a batch version of TBrowserImp.
Definition: TGuiFactory.cxx:72
TBrowser::fLastSelectedObject
TObject * fLastSelectedObject
Definition: TBrowser.h:40
Class
void Class()
Definition: Class.C:29
TBrowser::AddCheckBox
void AddCheckBox(TObject *obj, Bool_t check=kFALSE)
Add checkbox for this item.
Definition: TBrowser.cxx:305
TBrowser::Add
void Add(TObject *obj, const char *name=0, Int_t check=-1)
Add object with name to browser.
Definition: TBrowser.cxx:272
TClass::kRealNew
@ kRealNew
Definition: TClass.h:107
TObject::IsFolder
virtual Bool_t IsFolder() const
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects).
Definition: TObject.cxx:475
TGuiFactory.h
TBrowserImp::CheckObjectItem
virtual void CheckObjectItem(TObject *, Bool_t=kFALSE)
Definition: TBrowserImp.h:46
TBrowserImp::ExecuteDefaultAction
virtual void ExecuteDefaultAction(TObject *)
Definition: TBrowserImp.h:51
gROOT
#define gROOT
Definition: TROOT.h:406
int