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](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:
54 TBrowser *fBrowser{nullptr};
56
57public:
59 Bool_t Notify() override
60 {
61 if (fBrowser) {
62 if (fBrowser->GetRefreshFlag()) {
65 } else if (fActivate) {
68 }
69 }
70 Reset();
71
72 return kFALSE;
73 }
74};
75
76/** \class TBrowserObject
77This class is designed to wrap a Foreign object in order to inject it into the Browse sub-system.
78*/
79
80class TBrowserObject : public TNamed {
81
82public:
83 TBrowserObject(void *obj, TClass *cl, const char *brname)
84 : TNamed(brname, cl ? cl->GetName() : ""), fObj(obj), fClass(cl)
85 {
86 if (!cl)
87 Fatal("Constructor", "Class parameter should not be null");
89 }
90
92
93 void Browse(TBrowser *b) override { fClass->Browse(fObj, b); }
94 Bool_t IsFolder() const override { return fClass->IsFolder(fObj); }
95 TClass *IsA() const override { return fClass; }
96
97private:
98 void *fObj; ///<! pointer to the foreign object
99 TClass *fClass; ///<! pointer to class of the foreign object
100};
101
103
104////////////////////////////////////////////////////////////////////////////////
105// Make sure the application environment exists and the GUI libs are loaded
106
108{
109 // Make sure the application environment exists. It is need for graphics
110 // (colors are initialized in the TApplication ctor).
111 if (!gApplication)
113 // make sure that the Gpad and GUI libs are loaded
115 if (gApplication)
117
118 if (gROOT->IsWebDisplay() && !gROOT->IsWebDisplayBatch())
119 return kTRUE;
120
121 if (!gROOT->IsBatch())
122 return kTRUE;
123
124 Warning("TBrowser", "The ROOT browser cannot run in batch mode");
125 return kFALSE;
126}
127
128////////////////////////////////////////////////////////////////////////////////
129/// Create a new browser with a name, title. Width and height are by
130/// default set to 640x400 and (optionally) adjusted by the screen factor
131/// (depending on Rint.Canvas.UseScreenFactor to be true or false, default
132/// is true).
133
134TBrowser::TBrowser(const char *name, const char *title, TBrowserImp *extimp,
135 Option_t *opt)
136 : TNamed(name, title), fLastSelectedObject(nullptr), fImp(extimp), fTimer(nullptr),
137 fContextMenu(nullptr), fNeedRefresh(kFALSE)
138{
139 if (!InitGraphics())
140 return;
142 fImp = nullptr;
143 } else {
145 UInt_t w = UInt_t(cx*800);
146 UInt_t h = UInt_t(cx*500);
147 if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
148 Create();
149 }
150}
151
152////////////////////////////////////////////////////////////////////////////////
153/// Create a new browser with a name, title, width and height.
154
155TBrowser::TBrowser(const char *name, const char *title, UInt_t width,
156 UInt_t height, TBrowserImp *extimp, Option_t *opt)
157 : TNamed(name, title), fLastSelectedObject(nullptr), fImp(extimp), fTimer(nullptr), fContextMenu(nullptr),
158 fNeedRefresh(kFALSE)
159{
160 if (!InitGraphics())
161 return;
162 if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, width, height, opt);
163 Create();
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// Create a new browser with a name, title, position, width and height.
168
169TBrowser::TBrowser(const char *name, const char *title, Int_t x, Int_t y,
171 : TNamed(name, title), fLastSelectedObject(nullptr), fImp(extimp), fTimer(nullptr), fContextMenu(nullptr),
172 fNeedRefresh(kFALSE)
173{
174 if (!InitGraphics())
175 return;
176 fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
177 Create();
178}
179
180////////////////////////////////////////////////////////////////////////////////
181/// Create a new browser with a name, title, width and height for TObject *obj.
182
183TBrowser::TBrowser(const char *name, TObject *obj, const char *title, Option_t *opt)
184 : TNamed(name, title), fLastSelectedObject(nullptr), fImp(nullptr), fTimer(nullptr), fContextMenu(nullptr),
185 fNeedRefresh(kFALSE)
186{
187 if (!InitGraphics())
188 return;
190 UInt_t w = UInt_t(cx*800);
191 UInt_t h = UInt_t(cx*500);
192
193 if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
194 Create(obj);
195}
196
197////////////////////////////////////////////////////////////////////////////////
198/// Create a new browser with a name, title, width and height for TObject *obj.
199
200TBrowser::TBrowser(const char *name, TObject *obj, const char *title,
202 : TNamed(name, title), fLastSelectedObject(nullptr), fImp(nullptr), fTimer(nullptr), fContextMenu(nullptr),
203 fNeedRefresh(kFALSE)
204{
205 if (!InitGraphics())
206 return;
207 fImp = gGuiFactory->CreateBrowserImp(this, title, width, height, opt);
208 Create(obj);
209}
210
211////////////////////////////////////////////////////////////////////////////////
212/// Create a new browser with a name, title, width and height for TObject *obj.
213
214TBrowser::TBrowser(const char *name, TObject *obj, const char *title,
215 Int_t x, Int_t y,
217 : TNamed(name, title), fLastSelectedObject(nullptr), fImp(nullptr), fTimer(nullptr), fContextMenu(nullptr),
218 fNeedRefresh(kFALSE)
219{
220 if (!InitGraphics())
221 return;
222 fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
223 Create(obj);
224}
225
226////////////////////////////////////////////////////////////////////////////////
227/// Create a new browser with a name, title, width and height for TObject *obj.
228
229TBrowser::TBrowser(const char *name, void *obj, TClass *cl,
230 const char *objname, const char *title, Option_t *opt)
231 : TNamed(name, title), fLastSelectedObject(nullptr), fImp(nullptr), fTimer(nullptr), fContextMenu(nullptr),
232 fNeedRefresh(kFALSE)
233{
234 if (!InitGraphics())
235 return;
237 UInt_t w = UInt_t(cx*800);
238 UInt_t h = UInt_t(cx*500);
239
240 fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
241
242 Create(new TBrowserObject(obj,cl,objname));
243}
244
245////////////////////////////////////////////////////////////////////////////////
246/// Create a new browser with a name, title, width and height for TObject *obj.
247
248TBrowser::TBrowser(const char *name, void *obj, TClass *cl,
249 const char *objname, const char *title,
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, width, height, opt);
257 Create(new TBrowserObject(obj,cl,objname));
258}
259
260////////////////////////////////////////////////////////////////////////////////
261/// Create a new browser with a name, title, width and height for TObject *obj.
262
263TBrowser::TBrowser(const char *name,void *obj, TClass *cl,
264 const char *objname, const char *title,
265 Int_t x, Int_t y,
267 : TNamed(name, title), fLastSelectedObject(nullptr), fImp(nullptr), fTimer(nullptr), fContextMenu(nullptr),
268 fNeedRefresh(kFALSE)
269{
270 if (!InitGraphics())
271 return;
272 fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
273 Create(new TBrowserObject(obj,cl,objname));
274}
275
276////////////////////////////////////////////////////////////////////////////////
277/// Delete the browser.
278
280{
281 Destructor();
282}
283
284////////////////////////////////////////////////////////////////////////////////
285/// Actual browser destructor.
286
288{
289 if (fImp) fImp->CloseTabs();
291 gROOT->GetListOfBrowsers()->Remove(this);
295}
296
297////////////////////////////////////////////////////////////////////////////////
298/// Add object with name to browser. If name not set the objects GetName()
299/// is used. If check < 0 (default) no check box is drawn, if 0 then
300/// unchecked checkbox is added, if 1 checked checkbox is added.
301
302void TBrowser::Add(TObject *obj, const char *name, Int_t check)
303{
304 if (obj && fImp) {
305 fImp->Add(obj, name, check);
306 obj->SetBit(kMustCleanup);
307 }
308}
309
310////////////////////////////////////////////////////////////////////////////////
311/// Add foreign object with name to browser.
312/// 'cl' is the type use to store the value of obj.
313/// So literally the following pseudo code should be correct:
314///~~~ {.cpp}
315/// `cl->GetName()` * ptr = (`cl->GetName()`*) obj;
316///~~~
317/// and the value of obj is not necessarily the start of the object.
318/// If check < 0 (default) no check box is drawn, if 0 then
319/// unchecked checkbox is added, if 1 checked checkbox is added.
320
321void TBrowser::Add(void *obj, TClass *cl, const char *name, Int_t check)
322{
323 if (!obj || !cl) return;
324 TObject *to;
325 if (cl->IsTObject()) to = (TObject*)cl->DynamicCast(TObject::Class(),obj,kTRUE);
326 else to = new TBrowserObject(obj,cl,name);
327
328 if (!to) return;
329 Add(to,name,check);
330}
331
332////////////////////////////////////////////////////////////////////////////////
333/// Add checkbox for this item.
334
336{
337 if (obj && fImp) {
338 fImp->AddCheckBox(obj, check);
339 }
340}
341
342////////////////////////////////////////////////////////////////////////////////
343/// Change status of checkbox for this item.
344
346{
347 if (obj && fImp) {
348 fImp->CheckObjectItem(obj, check);
349 }
350}
351
352////////////////////////////////////////////////////////////////////////////////
353/// Remove checkbox for this item.
354
356{
357 if (obj && fImp) {
358 fImp->RemoveCheckBox(obj);
359 }
360}
361
362////////////////////////////////////////////////////////////////////////////////
363/// Create the browser, called by the ctors.
364
366{
368
369 fTimer = new TBrowserTimer(this);
371
373 gROOT->GetListOfBrowsers()->Add(this);
374
375 // Get the list of globals
376 gROOT->GetListOfGlobals(kTRUE);
377 gROOT->GetListOfGlobalFunctions(kTRUE);
378
379 fContextMenu = new TContextMenu("BrowserContextMenu") ;
380
381 // Fill the first list from the present TObject obj
382 if (obj) {
383 Add(obj);
384 if (fImp) fImp->BrowseObj(obj);
385 } else if (fImp) {
386 // Fill the first list with all browsable classes from TROOT
388 }
389
390 // The first list will be filled by TWin32BrowserImp ctor
391 // with all browsable classes from TROOT
392}
393
394////////////////////////////////////////////////////////////////////////////////
395/// Execute default action for selected object (action is specified
396/// in the `$HOME/.root.mimes` or `$ROOTSYS/etc/root.mimes file`).
397
399{
400 if (obj && fImp)
402}
403
404////////////////////////////////////////////////////////////////////////////////
405/// Recursively remove obj from browser.
406
408{
409 if (fImp && obj) {
410 fImp->RecursiveRemove(obj);
412 }
413}
414
415////////////////////////////////////////////////////////////////////////////////
416/// Refresh browser contents.
417
419{
421 if (fImp) fImp->Refresh();
423}
424
425////////////////////////////////////////////////////////////////////////////////
426/// Assign the last selected object.
427
428void TBrowser::SetSelected(TObject *clickedObject)
429{
430 fLastSelectedObject = clickedObject;
431}
#define SafeDelete(p)
Definition: RConfig.hxx:537
#define h(i)
Definition: RSha256.hxx:106
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
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:560
#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:80
TBrowserObject(void *obj, TClass *cl, const char *brname)
Definition: TBrowser.cxx:83
TClass * IsA() const override
Definition: TBrowser.cxx:95
Bool_t IsFolder() const override
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects).
Definition: TBrowser.cxx:94
TClass * fClass
! pointer to class of the foreign object
Definition: TBrowser.cxx:99
void * fObj
! pointer to the foreign object
Definition: TBrowser.cxx:98
void Browse(TBrowser *b) override
Browse object. May be overridden for another default action.
Definition: TBrowser.cxx:93
Called whenever timer times out.
Definition: TBrowser.cxx:51
Bool_t Notify() override
Notify when timer times out.
Definition: TBrowser.cxx:59
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:407
void CheckObjectItem(TObject *obj, Bool_t check=kFALSE)
Change status of checkbox for this item.
Definition: TBrowser.cxx:345
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:279
TContextMenu * fContextMenu
Browser's timer.
Definition: TBrowser.h:48
virtual void Destructor()
Actual browser destructor.
Definition: TBrowser.cxx:287
Bool_t InitGraphics()
Definition: TBrowser.cxx:107
void RemoveCheckBox(TObject *obj)
Remove checkbox for this item.
Definition: TBrowser.cxx:355
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:365
void Refresh()
Refresh browser contents.
Definition: TBrowser.cxx:418
TBrowser(const TBrowser &)=delete
The last TObject selected by user.
void SetSelected(TObject *clickedObject)
Assign the last selected object.
Definition: TBrowser.cxx:428
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:398
void AddCheckBox(TObject *obj, Bool_t check=kFALSE)
Add checkbox for this item.
Definition: TBrowser.cxx:335
void Add(TObject *obj, const char *name=nullptr, Int_t check=-1)
Add object with name to browser.
Definition: TBrowser.cxx:302
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:81
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:4904
@ kRealNew
Definition: TClass.h:108
static ENewType IsCallingNew()
Static method returning the defConstructor flag passed to TClass::New().
Definition: TClass.cxx:5891
Bool_t IsTObject() const
Return kTRUE is the class inherits from TObject.
Definition: TClass.cxx:5927
Bool_t IsFolder() const override
Returns kTRUE in case object contains browsable objects (like containers or lists of other objects).
Definition: TClass.h:513
This class provides an interface to context sensitive popup menus.
Definition: TContextMenu.h:44
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:92
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:41
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
Definition: TObject.cxx:956
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:775
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition: TObject.cxx:998
@ kCanDelete
if object in a list can be deleted
Definition: TObject.h:62
@ kMustCleanup
if object destructor must call RecursiveRemove()
Definition: TObject.h:64
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