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