Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
76 ClassImp(TBrowser);
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
90  TApplication::NeedGraphicsLibs();
91  gApplication->InitializeGraphics();
92  if (TClass::IsCallingNew() != TClass::kRealNew) {
93  fImp = 0;
94  } else {
95  Float_t cx = gStyle->GetScreenFactor();
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
112  TApplication::NeedGraphicsLibs();
113  gApplication->InitializeGraphics();
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),
124  fNeedRefresh(kFALSE)
125 {
126  // make sure that the Gpad and GUI libs are loaded
127  TApplication::NeedGraphicsLibs();
128  gApplication->InitializeGraphics();
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),
138  fNeedRefresh(kFALSE)
139 {
140  // make sure that the Gpad and GUI libs are loaded
141  TApplication::NeedGraphicsLibs();
142  gApplication->InitializeGraphics();
143  Float_t cx = gStyle->GetScreenFactor();
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),
157  fNeedRefresh(kFALSE)
158 {
159  // make sure that the Gpad and GUI libs are loaded
160  TApplication::NeedGraphicsLibs();
161  gApplication->InitializeGraphics();
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),
173  fNeedRefresh(kFALSE)
174 {
175  // make sure that the Gpad and GUI libs are loaded
176  TApplication::NeedGraphicsLibs();
177  gApplication->InitializeGraphics();
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),
188  fNeedRefresh(kFALSE)
189 {
190  // make sure that the Gpad and GUI libs are loaded
191  TApplication::NeedGraphicsLibs();
192  gApplication->InitializeGraphics();
193  Float_t cx = gStyle->GetScreenFactor();
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),
209  fNeedRefresh(kFALSE)
210 {
211  // make sure that the Gpad and GUI libs are loaded
212  TApplication::NeedGraphicsLibs();
213  gApplication->InitializeGraphics();
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),
226  fNeedRefresh(kFALSE)
227 {
228  // make sure that the Gpad and GUI libs are loaded
229  TApplication::NeedGraphicsLibs();
230  gApplication->InitializeGraphics();
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 
238 TBrowser::~TBrowser()
239 {
240  Destructor();
241 }
242 
243 ////////////////////////////////////////////////////////////////////////////////
244 /// Actual browser destructor.
245 
246 void TBrowser::Destructor()
247 {
248  if (fImp) fImp->CloseTabs();
249  R__LOCKGUARD(gROOTMutex);
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 
294 void TBrowser::AddCheckBox(TObject *obj, Bool_t check)
295 {
296  if (obj && fImp) {
297  fImp->AddCheckBox(obj, check);
298  }
299 }
300 
301 ////////////////////////////////////////////////////////////////////////////////
302 /// Change status of checkbox for this item.
303 
304 void TBrowser::CheckObjectItem(TObject *obj, Bool_t check)
305 {
306  if (obj && fImp) {
307  fImp->CheckObjectItem(obj, check);
308  }
309 }
310 
311 ////////////////////////////////////////////////////////////////////////////////
312 /// Remove checkbox for this item.
313 
314 void TBrowser::RemoveCheckBox(TObject *obj)
315 {
316  if (obj && fImp) {
317  fImp->RemoveCheckBox(obj);
318  }
319 }
320 
321 ////////////////////////////////////////////////////////////////////////////////
322 /// Create the browser, called by the ctors.
323 
324 void TBrowser::Create(TObject *obj)
325 {
326  fNeedRefresh = kFALSE;
327 
328  fTimer = new TBrowserTimer(this);
329  gSystem->AddTimer(fTimer);
330 
331  R__LOCKGUARD(gROOTMutex);
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 
357 void TBrowser::ExecuteDefaultAction(TObject *obj)
358 {
359  if (obj && fImp)
360  fImp->ExecuteDefaultAction(obj);
361 }
362 
363 ////////////////////////////////////////////////////////////////////////////////
364 /// Recursively remove obj from browser.
365 
366 void TBrowser::RecursiveRemove(TObject *obj)
367 {
368  if (fImp && obj) {
369  fImp->RecursiveRemove(obj);
370  fNeedRefresh = kTRUE;
371  }
372 }
373 
374 ////////////////////////////////////////////////////////////////////////////////
375 /// Refresh browser contents.
376 
377 void TBrowser::Refresh()
378 {
379  fNeedRefresh = kTRUE;
380  if (fImp) fImp->Refresh();
381  fNeedRefresh = kFALSE;
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");
424  SetBit(kCanDelete);
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 }