Logo ROOT   6.10/09
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 /// Create a new browser with a name, title. Width and height are by
80 /// default set to 640x400 and (optionally) adjusted by the screen factor
81 /// (depending on Rint.Canvas.UseScreenFactor to be true or false, default
82 /// is true).
83 
84 TBrowser::TBrowser(const char *name, const char *title, TBrowserImp *extimp,
85  Option_t *opt)
86  : TNamed(name, title), fLastSelectedObject(0), fImp(extimp), fTimer(0),
87  fContextMenu(0), fNeedRefresh(kFALSE)
88 {
89  // make sure that the Gpad and GUI libs are loaded
93  fImp = 0;
94  } else {
96  UInt_t w = UInt_t(cx*800);
97  UInt_t h = UInt_t(cx*500);
98  if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
99  Create();
100  }
101 }
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// Create a new browser with a name, title, width and height.
105 
106 TBrowser::TBrowser(const char *name, const char *title, UInt_t width,
107  UInt_t height, TBrowserImp *extimp, Option_t *opt)
108  : TNamed(name, title), fLastSelectedObject(0), fImp(extimp), fTimer(0), fContextMenu(0),
109  fNeedRefresh(kFALSE)
110 {
111  // make sure that the Gpad and GUI libs are loaded
114  if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, width, height, opt);
115  Create();
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Create a new browser with a name, title, position, width and height.
120 
121 TBrowser::TBrowser(const char *name, const char *title, Int_t x, Int_t y,
122  UInt_t width, UInt_t height, TBrowserImp *extimp, Option_t *opt)
123  : TNamed(name, title), fLastSelectedObject(0), fImp(extimp), fTimer(0), fContextMenu(0),
125 {
126  // make sure that the Gpad and GUI libs are loaded
129  fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
130  Create();
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// Create a new browser with a name, title, width and height for TObject *obj.
135 
136 TBrowser::TBrowser(const char *name, TObject *obj, const char *title, Option_t *opt)
137  : TNamed(name, title), fLastSelectedObject(0), fImp(0), fTimer(0), fContextMenu(0),
139 {
140  // make sure that the Gpad and GUI libs are loaded
144  UInt_t w = UInt_t(cx*800);
145  UInt_t h = UInt_t(cx*500);
146 
147  if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
148  Create(obj);
149 }
150 
151 ////////////////////////////////////////////////////////////////////////////////
152 /// Create a new browser with a name, title, width and height for TObject *obj.
153 
154 TBrowser::TBrowser(const char *name, TObject *obj, const char *title,
155  UInt_t width, UInt_t height, Option_t *opt)
156  : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
158 {
159  // make sure that the Gpad and GUI libs are loaded
162  fImp = gGuiFactory->CreateBrowserImp(this, title, width, height, opt);
163  Create(obj);
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// Create a new browser with a name, title, width and height for TObject *obj.
168 
169 TBrowser::TBrowser(const char *name, TObject *obj, const char *title,
170  Int_t x, Int_t y,
171  UInt_t width, UInt_t height, Option_t *opt)
172  : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
174 {
175  // make sure that the Gpad and GUI libs are loaded
178  fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
179  Create(obj);
180 }
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 /// Create a new browser with a name, title, width and height for TObject *obj.
184 
185 TBrowser::TBrowser(const char *name, void *obj, TClass *cl,
186  const char *objname, const char *title, Option_t *opt)
187  : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
189 {
190  // make sure that the Gpad and GUI libs are loaded
194  UInt_t w = UInt_t(cx*800);
195  UInt_t h = UInt_t(cx*500);
196 
197  fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
198 
199  Create(new TBrowserObject(obj,cl,objname));
200 }
201 
202 ////////////////////////////////////////////////////////////////////////////////
203 /// Create a new browser with a name, title, width and height for TObject *obj.
204 
205 TBrowser::TBrowser(const char *name, void *obj, TClass *cl,
206  const char *objname, const char *title,
207  UInt_t width, UInt_t height, Option_t *opt)
208  : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
210 {
211  // make sure that the Gpad and GUI libs are loaded
214  fImp = gGuiFactory->CreateBrowserImp(this, title, width, height, opt);
215  Create(new TBrowserObject(obj,cl,objname));
216 }
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 /// Create a new browser with a name, title, width and height for TObject *obj.
220 
221 TBrowser::TBrowser(const char *name,void *obj, TClass *cl,
222  const char *objname, const char *title,
223  Int_t x, Int_t y,
224  UInt_t width, UInt_t height, Option_t *opt)
225  : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
227 {
228  // make sure that the Gpad and GUI libs are loaded
231  fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
232  Create(new TBrowserObject(obj,cl,objname));
233 }
234 
235 ////////////////////////////////////////////////////////////////////////////////
236 /// Delete the browser.
237 
239 {
240  Destructor();
241 }
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// Actual browser destructor.
245 
247 {
248  if (fImp) fImp->CloseTabs();
250  gROOT->GetListOfBrowsers()->Remove(this);
251  delete fContextMenu;
252  delete fTimer;
253  if (fImp) delete fImp;
254 }
255 
256 ////////////////////////////////////////////////////////////////////////////////
257 /// Add object with name to browser. If name not set the objects GetName()
258 /// is used. If check < 0 (default) no check box is drawn, if 0 then
259 /// unchecked checkbox is added, if 1 checked checkbox is added.
260 
261 void TBrowser::Add(TObject *obj, const char *name, Int_t check)
262 {
263  if (obj && fImp) {
264  fImp->Add(obj, name, check);
265  obj->SetBit(kMustCleanup);
266  }
267 }
268 
269 ////////////////////////////////////////////////////////////////////////////////
270 /// Add foreign object with name to browser.
271 /// 'cl' is the type use to store the value of obj.
272 /// So literally the following pseudo code should be correct:
273 ///~~~ {.cpp}
274 /// `cl->GetName()` * ptr = (`cl->GetName()`*) obj;
275 ///~~~
276 /// and the value of obj is not necessarily the start of the object.
277 /// If check < 0 (default) no check box is drawn, if 0 then
278 /// unchecked checkbox is added, if 1 checked checkbox is added.
279 
280 void TBrowser::Add(void *obj, TClass *cl, const char *name, Int_t check)
281 {
282  if (!obj || !cl) return;
283  TObject *to;
284  if (cl->IsTObject()) to = (TObject*)cl->DynamicCast(TObject::Class(),obj,kTRUE);
285  else to = new TBrowserObject(obj,cl,name);
286 
287  if (!to) return;
288  Add(to,name,check);
289 }
290 
291 ////////////////////////////////////////////////////////////////////////////////
292 /// Add checkbox for this item.
293 
295 {
296  if (obj && fImp) {
297  fImp->AddCheckBox(obj, check);
298  }
299 }
300 
301 ////////////////////////////////////////////////////////////////////////////////
302 /// Change status of checkbox for this item.
303 
305 {
306  if (obj && fImp) {
307  fImp->CheckObjectItem(obj, check);
308  }
309 }
310 
311 ////////////////////////////////////////////////////////////////////////////////
312 /// Remove checkbox for this item.
313 
315 {
316  if (obj && fImp) {
317  fImp->RemoveCheckBox(obj);
318  }
319 }
320 
321 ////////////////////////////////////////////////////////////////////////////////
322 /// Create the browser, called by the ctors.
323 
325 {
327 
328  fTimer = new TBrowserTimer(this);
330 
332  gROOT->GetListOfBrowsers()->Add(this);
333 
334  // Get the list of globals
335  gROOT->GetListOfGlobals(kTRUE);
336  gROOT->GetListOfGlobalFunctions(kTRUE);
337 
338  fContextMenu = new TContextMenu("BrowserContextMenu") ;
339 
340  // Fill the first list from the present TObject obj
341  if (obj) {
342  Add(obj);
343  if (fImp) fImp->BrowseObj(obj);
344  } else if (fImp) {
345  // Fill the first list with all browsable classes from TROOT
346  fImp->BrowseObj(gROOT);
347  }
348 
349  // The first list will be filled by TWin32BrowserImp ctor
350  // with all browsable classes from TROOT
351 }
352 
353 ////////////////////////////////////////////////////////////////////////////////
354 /// Execute default action for selected object (action is specified
355 /// in the `$HOME/.root.mimes` or `$ROOTSYS/etc/root.mimes file`).
356 
358 {
359  if (obj && fImp)
361 }
362 
363 ////////////////////////////////////////////////////////////////////////////////
364 /// Recursively remove obj from browser.
365 
367 {
368  if (fImp && obj) {
369  fImp->RecursiveRemove(obj);
371  }
372 }
373 
374 ////////////////////////////////////////////////////////////////////////////////
375 /// Refresh browser contents.
376 
378 {
380  if (fImp) fImp->Refresh();
382 }
383 
384 ////////////////////////////////////////////////////////////////////////////////
385 /// Assign the last selected object.
386 
387 void TBrowser::SetSelected(TObject *clickedObject)
388 {
389  fLastSelectedObject = clickedObject;
390 }
391 
392 /** \class TBrowserTimer
393 Called whenever timer times out.
394 */
395 
396 Bool_t TBrowserTimer::Notify()
397 {
398  if (fBrowser) {
399  if (fBrowser->GetRefreshFlag()) {
400  fBrowser->SetRefreshFlag(kFALSE);
401  fActivate = kTRUE;
402  } else if (fActivate) {
403  fActivate = kFALSE;
404  fBrowser->Refresh();
405  }
406  }
407  Reset();
408 
409  return kFALSE;
410 }
411 
412 /** \class TBrowserObject
413 This is a wrapper class to emulate the TObject interface
414 around an object of a non-TObject class
415 */
416 
417 ////////////////////////////////////////////////////////////////////////////////
418 /// Constructor.
419 
420 TBrowserObject::TBrowserObject(void *obj, TClass *cl, const char *brname)
421  : TNamed(brname, cl ? cl->GetName() : ""), fObj(obj), fClass(cl)
422 {
423  if (cl==0) Fatal("Constructor","Class parameter should not be null");
425 }
426 
427 ////////////////////////////////////////////////////////////////////////////////
428 /// Return kTRUE if the object is a folder (contains browsable objects).
429 
430 Bool_t TBrowserObject::IsFolder() const
431 {
432  return fClass->IsFolder(fObj);
433 }
434 
435 ////////////////////////////////////////////////////////////////////////////////
436 /// Browse the content of the underlying object.
437 
438 void TBrowserObject::Browse(TBrowser* b)
439 {
440  fClass->Browse(fObj, b);
441 }
void Add(TObject *obj, const char *name=0, Int_t check=-1)
Add object with name to browser.
Definition: TBrowser.cxx:261
virtual void CheckObjectItem(TObject *, Bool_t=kFALSE)
Definition: TBrowserImp.h:49
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
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
virtual ~TBrowser()
Delete the browser.
Definition: TBrowser.cxx:238
TBrowserTimer * fTimer
Window system specific browser implementation.
Definition: TBrowser.h:47
virtual void BrowseObj(TObject *)
Definition: TBrowserImp.h:51
float Float_t
Definition: RtypesCore.h:53
TObject * fLastSelectedObject
Definition: TBrowser.h:40
const char Option_t
Definition: RtypesCore.h:62
TBrowser(const TBrowser &)
The last TObject selected by user.
virtual void ExecuteDefaultAction(TObject *)
Definition: TBrowserImp.h:54
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
TH1 * h
Definition: legend2.C:5
#define gROOT
Definition: TROOT.h:375
void ExecuteDefaultAction(TObject *obj)
Execute default action for selected object (action is specified in the $HOME/.root.mimes or $ROOTSYS/etc/root.mimes file).
Definition: TBrowser.cxx:357
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
R__EXTERN TVirtualMutex * gROOTMutex
Definition: TROOT.h:57
void CheckObjectItem(TObject *obj, Bool_t check=kFALSE)
Change status of checkbox for this item.
Definition: TBrowser.cxx:304
virtual void CloseTabs()
Definition: TBrowserImp.h:53
R__EXTERN TApplication * gApplication
Definition: TApplication.h:165
Float_t GetScreenFactor() const
Definition: TStyle.h:240
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:687
if object in a list can be deleted
Definition: TObject.h:58
void SetSelected(TObject *clickedObject)
Assign the last selected object.
Definition: TBrowser.cxx:387
void AddCheckBox(TObject *obj, Bool_t check=kFALSE)
Add checkbox for this item.
Definition: TBrowser.cxx:294
virtual void Create(TObject *obj=0)
Create the browser, called by the ctors.
Definition: TBrowser.cxx:324
Double_t x[n]
Definition: legend1.C:17
void Class()
Definition: Class.C:29
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual void RecursiveRemove(TObject *)
Definition: TBrowserImp.h:56
static ENewType IsCallingNew()
Static method returning the defConstructor flag passed to TClass::New().
Definition: TClass.cxx:5539
R__EXTERN TGuiFactory * gGuiFactory
Definition: TGuiFactory.h:66
virtual Bool_t Notify()
Notify when timer times out.
Definition: TTimer.cxx:143
Using a TBrowser one can browse all ROOT objects.
Definition: TBrowser.h:37
static void NeedGraphicsLibs()
Static method.
Bool_t fNeedRefresh
Context menu pointer.
Definition: TBrowser.h:49
R__EXTERN TSystem * gSystem
Definition: TSystem.h:539
This class provides an interface to context sensitive popup menus.
Definition: TContextMenu.h:40
unsigned int UInt_t
Definition: RtypesCore.h:42
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:71
void Reset(Detail::TBranchProxy *x)
virtual void Refresh(Bool_t=kFALSE)
Definition: TBrowserImp.h:57
void RemoveCheckBox(TObject *obj)
Remove checkbox for this item.
Definition: TBrowser.cxx:314
if object destructor must call RecursiveRemove()
Definition: TObject.h:59
#define R__LOCKGUARD2(mutex)
const Bool_t kFALSE
Definition: RtypesCore.h:92
void InitializeGraphics()
Initialize the graphics environment.
long Long_t
Definition: RtypesCore.h:50
#define ClassImp(name)
Definition: Rtypes.h:336
virtual void Add(TObject *, const char *, Int_t)
Definition: TBrowserImp.h:47
TNamed()
Definition: TNamed.h:36
Double_t y[n]
Definition: legend1.C:17
Mother of all ROOT objects.
Definition: TObject.h:37
Bool_t IsTObject() const
Return kTRUE is the class inherits from TObject.
Definition: TClass.cxx:5575
virtual void Destructor()
Actual browser destructor.
Definition: TBrowser.cxx:246
virtual void AddTimer(TTimer *t)
Add timer to list of system timers.
Definition: TSystem.cxx:478
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
virtual void AddCheckBox(TObject *, Bool_t=kFALSE)
Definition: TBrowserImp.h:48
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:4643
TBrowserImp * fImp
Definition: TBrowser.h:46
TContextMenu * fContextMenu
Browser&#39;s timer.
Definition: TBrowser.h:48
virtual void Fatal(const char *method, const char *msgfmt,...) const
Issue fatal error message.
Definition: TObject.cxx:901
const Bool_t kTRUE
Definition: RtypesCore.h:91
void Refresh()
Refresh browser contents.
Definition: TBrowser.cxx:377
virtual void RemoveCheckBox(TObject *)
Definition: TBrowserImp.h:50
virtual void RecursiveRemove(TObject *obj)
Recursively remove obj from browser.
Definition: TBrowser.cxx:366
ABC describing GUI independent browser implementation protocol.
Definition: TBrowserImp.h:29