TBrowser.cxx

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TBrowser.cxx 35589 2010-09-22 14:13:22Z rdm $
00002 // Author: Fons Rademakers   25/10/95
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // Using a TBrowser one can browse all ROOT objects. It shows in a list //
00015 // on the left side of the window all browsable ROOT classes. Selecting //
00016 // one of the classes displays, in the iconbox on the right side, all   //
00017 // objects in the class. Selecting one of the objects in the iconbox,   //
00018 // will place all browsable objects in a new list and draws the         //
00019 // contents of the selected class in the iconbox. And so on....         //
00020 //                                                                      //
00021 //Begin_Html <img src="gif/browser.gif"> End_Html                       //
00022 //                                                                      //
00023 //////////////////////////////////////////////////////////////////////////
00024 
00025 #include "TBrowser.h"
00026 #include "TGuiFactory.h"
00027 #include "TROOT.h"
00028 #include "TSystem.h"
00029 #include "TStyle.h"
00030 #include "TTimer.h"
00031 #include "TContextMenu.h"
00032 #include "TInterpreter.h"
00033 #include "TVirtualMutex.h"
00034 #include "TClass.h"
00035 #include "TApplication.h"
00036 
00037 //////////////////////////////////////////////////////////////////////////
00038 //                                                                      //
00039 // TBrowserTimer                                                        //
00040 //                                                                      //
00041 //////////////////////////////////////////////////////////////////////////
00042 
00043 class TBrowserTimer : public TTimer {
00044 
00045 protected:
00046    TBrowser *fBrowser;
00047    Bool_t    fActivate;
00048 
00049 public:
00050    TBrowserTimer(TBrowser *b, Long_t ms = 1000)
00051       : TTimer(ms, kTRUE), fBrowser(b), fActivate(kFALSE) { }
00052    Bool_t Notify();
00053 };
00054 
00055 //////////////////////////////////////////////////////////////////////////
00056 //                                                                      //
00057 // TBrowserObject                                                       //
00058 //                                                                      //
00059 //////////////////////////////////////////////////////////////////////////
00060 
00061 class TBrowserObject : public TNamed
00062 {
00063    // This class is designed to wrap a Foreign object in order to
00064    // inject it into the Browse sub-system.
00065 
00066 public:
00067 
00068    TBrowserObject(void *obj, TClass *cl, const char *brname);
00069    ~TBrowserObject(){;}
00070 
00071    void    Browse(TBrowser* b);
00072    Bool_t  IsFolder() const;
00073    TClass *IsA() const { return fClass; }
00074 
00075 private:
00076    void     *fObj;   //! pointer to the foreign object
00077    TClass   *fClass; //! pointer to class of the foreign object
00078 
00079 };
00080 
00081 
00082 ClassImp(TBrowser)
00083 
00084 //______________________________________________________________________________
00085 TBrowser::TBrowser(const char *name, const char *title, TBrowserImp *extimp,
00086                    Option_t *opt)
00087    : TNamed(name, title), fLastSelectedObject(0), fImp(extimp), fTimer(0),
00088      fContextMenu(0), fNeedRefresh(kFALSE)
00089 {
00090    // Create a new browser with a name, title. Width and height are by
00091    // default set to 640x400 and (optionally) adjusted by the screen factor
00092    // (depending on Rint.Canvas.UseScreenFactor to be true or false, default
00093    // is true).
00094 
00095    // make sure that the Gpad and GUI libs are loaded
00096    TApplication::NeedGraphicsLibs();
00097    gApplication->InitializeGraphics();
00098    if (TClass::IsCallingNew() != TClass::kRealNew) {
00099       fImp = 0;
00100    } else {
00101       Float_t cx = gStyle->GetScreenFactor();
00102       UInt_t w = UInt_t(cx*800);
00103       UInt_t h = UInt_t(cx*500);
00104       if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
00105       Create();
00106    }
00107 }
00108 
00109 //______________________________________________________________________________
00110 TBrowser::TBrowser(const char *name, const char *title, UInt_t width,
00111                    UInt_t height, TBrowserImp *extimp, Option_t *opt)
00112    : TNamed(name, title), fLastSelectedObject(0), fImp(extimp), fTimer(0), fContextMenu(0),
00113      fNeedRefresh(kFALSE)
00114 {
00115    // Create a new browser with a name, title, width and height.
00116 
00117    // make sure that the Gpad and GUI libs are loaded
00118    TApplication::NeedGraphicsLibs();
00119    gApplication->InitializeGraphics();
00120    if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, width, height, opt);
00121    Create();
00122 }
00123 
00124 //______________________________________________________________________________
00125 TBrowser::TBrowser(const char *name, const char *title, Int_t x, Int_t y,
00126                    UInt_t width, UInt_t height, TBrowserImp *extimp, Option_t *opt)
00127    : TNamed(name, title), fLastSelectedObject(0), fImp(extimp), fTimer(0), fContextMenu(0),
00128      fNeedRefresh(kFALSE)
00129 {
00130    // Create a new browser with a name, title, position, width and height.
00131 
00132    // make sure that the Gpad and GUI libs are loaded
00133    TApplication::NeedGraphicsLibs();
00134    gApplication->InitializeGraphics();
00135    fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
00136    Create();
00137 }
00138 
00139 //______________________________________________________________________________
00140 TBrowser::TBrowser(const char *name, TObject *obj, const char *title, Option_t *opt)
00141    : TNamed(name, title), fLastSelectedObject(0), fImp(0), fTimer(0), fContextMenu(0),
00142      fNeedRefresh(kFALSE)
00143 {
00144    // Create a new browser with a name, title, width and height for TObject *obj.
00145 
00146    // make sure that the Gpad and GUI libs are loaded
00147    TApplication::NeedGraphicsLibs();
00148    gApplication->InitializeGraphics();
00149    Float_t cx = gStyle->GetScreenFactor();
00150    UInt_t w = UInt_t(cx*800);
00151    UInt_t h = UInt_t(cx*500);
00152 
00153    if (!fImp) fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
00154    Create(obj);
00155 }
00156 
00157 //______________________________________________________________________________
00158 TBrowser::TBrowser(const char *name, TObject *obj, const char *title,
00159                    UInt_t width, UInt_t height, Option_t *opt)
00160    : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
00161      fNeedRefresh(kFALSE)
00162 {
00163    // Create a new browser with a name, title, width and height for TObject *obj.
00164 
00165    // make sure that the Gpad and GUI libs are loaded
00166    TApplication::NeedGraphicsLibs();
00167    gApplication->InitializeGraphics();
00168    fImp = gGuiFactory->CreateBrowserImp(this, title, width, height, opt);
00169    Create(obj);
00170 }
00171 
00172 //______________________________________________________________________________
00173 TBrowser::TBrowser(const char *name, TObject *obj, const char *title,
00174                    Int_t x, Int_t y,
00175                    UInt_t width, UInt_t height, Option_t *opt)
00176    : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
00177      fNeedRefresh(kFALSE)
00178 {
00179    // Create a new browser with a name, title, width and height for TObject *obj.
00180 
00181    // make sure that the Gpad and GUI libs are loaded
00182    TApplication::NeedGraphicsLibs();
00183    gApplication->InitializeGraphics();
00184    fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
00185    Create(obj);
00186 }
00187 
00188 //______________________________________________________________________________
00189 TBrowser::TBrowser(const char *name, void *obj, TClass *cl,
00190                    const char *objname, const char *title, Option_t *opt)
00191    : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
00192      fNeedRefresh(kFALSE)
00193 {
00194    // Create a new browser with a name, title, width and height for TObject *obj.
00195 
00196    // make sure that the Gpad and GUI libs are loaded
00197    TApplication::NeedGraphicsLibs();
00198    gApplication->InitializeGraphics();
00199    Float_t cx = gStyle->GetScreenFactor();
00200    UInt_t w = UInt_t(cx*800);
00201    UInt_t h = UInt_t(cx*500);
00202 
00203    fImp = gGuiFactory->CreateBrowserImp(this, title, w, h, opt);
00204 
00205    Create(new TBrowserObject(obj,cl,objname));
00206 }
00207 
00208 //______________________________________________________________________________
00209 TBrowser::TBrowser(const char *name, void *obj, TClass *cl,
00210                    const char *objname, const char *title,
00211                    UInt_t width, UInt_t height, Option_t *opt)
00212    : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
00213      fNeedRefresh(kFALSE)
00214 {
00215    // Create a new browser with a name, title, width and height for TObject *obj.
00216 
00217    // make sure that the Gpad and GUI libs are loaded
00218    TApplication::NeedGraphicsLibs();
00219    gApplication->InitializeGraphics();
00220    fImp = gGuiFactory->CreateBrowserImp(this, title, width, height, opt);
00221    Create(new TBrowserObject(obj,cl,objname));
00222 }
00223 
00224 //______________________________________________________________________________
00225 TBrowser::TBrowser(const char *name,void *obj,  TClass *cl,
00226                    const char *objname, const char *title,
00227                    Int_t x, Int_t y,
00228                    UInt_t width, UInt_t height, Option_t *opt)
00229    : TNamed(name, title), fLastSelectedObject(0), fTimer(0), fContextMenu(0),
00230      fNeedRefresh(kFALSE)
00231 {
00232    // Create a new browser with a name, title, width and height for TObject *obj.
00233 
00234    // make sure that the Gpad and GUI libs are loaded
00235    TApplication::NeedGraphicsLibs();
00236    gApplication->InitializeGraphics();
00237    fImp = gGuiFactory->CreateBrowserImp(this, title, x, y, width, height, opt);
00238    Create(new TBrowserObject(obj,cl,objname));
00239 }
00240 
00241 //______________________________________________________________________________
00242 TBrowser::~TBrowser()
00243 {
00244    // Delete the browser.
00245 
00246    R__LOCKGUARD2(gROOTMutex);
00247    gROOT->GetListOfBrowsers()->Remove(this);
00248    delete fContextMenu;
00249    delete fTimer;
00250    delete fImp;
00251 }
00252 
00253 //______________________________________________________________________________
00254 void TBrowser::Add(TObject *obj, const char *name, Int_t check)
00255 {
00256    // Add object with name to browser. If name not set the objects GetName()
00257    // is used. If check < 0 (default) no check box is drawn, if 0 then
00258    // unchecked checkbox is added, if 1 checked checkbox is added.
00259 
00260    if (obj && fImp) {
00261       fImp->Add(obj, name, check);
00262       obj->SetBit(kMustCleanup);
00263    }
00264 }
00265 
00266 //______________________________________________________________________________
00267 void TBrowser::Add(void *obj, TClass *cl, const char *name, Int_t check)
00268 {
00269    // Add foreign object with name to browser.
00270    // 'cl' is the type use to store the value of obj.
00271    // So literally the following pseudo code should be correct:
00272    //    `cl->GetName()` * ptr = (`cl->GetName()`*) obj;
00273    // and the value of obj is not necessarily the start of the object.
00274    // If check < 0 (default) no check box is drawn, if 0 then
00275    // unchecked checkbox is added, if 1 checked checkbox is added.
00276 
00277    if (!obj || !cl) return;
00278    TObject *to;
00279    if (cl->IsTObject()) to = (TObject*)cl->DynamicCast(TObject::Class(),obj,kTRUE);
00280    else                 to = new TBrowserObject(obj,cl,name);
00281 
00282    if (!to) return;
00283    Add(to,name,check);
00284 }
00285 
00286 //______________________________________________________________________________
00287 void TBrowser::AddCheckBox(TObject *obj, Bool_t check)
00288 {
00289    // Add checkbox for this item.
00290 
00291    if (obj && fImp) {
00292       fImp->AddCheckBox(obj, check);
00293    }
00294 }
00295 
00296 //______________________________________________________________________________
00297 void TBrowser::CheckObjectItem(TObject *obj, Bool_t check)
00298 {
00299    // Change status of checkbox for this item.
00300 
00301    if (obj && fImp) {
00302       fImp->CheckObjectItem(obj, check);
00303    }
00304 }
00305 
00306 //______________________________________________________________________________
00307 void TBrowser::RemoveCheckBox(TObject *obj)
00308 {
00309    // Remove checkbox for this item.
00310 
00311    if (obj && fImp) {
00312       fImp->RemoveCheckBox(obj);
00313    }
00314 }
00315 
00316 //______________________________________________________________________________
00317 void TBrowser::Create(TObject *obj)
00318 {
00319    // Create the browser, called by the ctors.
00320 
00321    fNeedRefresh = kFALSE;
00322 
00323    fTimer = new TBrowserTimer(this);
00324    gSystem->AddTimer(fTimer);
00325 
00326    R__LOCKGUARD2(gROOTMutex);
00327    gROOT->GetListOfBrowsers()->Add(this);
00328 
00329    // Get the list of globals
00330    gROOT->GetListOfGlobals(kTRUE);
00331    gROOT->GetListOfGlobalFunctions(kTRUE);
00332 
00333    fContextMenu = new TContextMenu("BrowserContextMenu") ;
00334 
00335    // Fill the first list from the present TObject obj
00336    if (obj) {
00337       Add(obj);
00338       if (fImp) fImp->BrowseObj(obj);
00339    } else if (fImp) {
00340       // Fill the first list with all browsable classes from TROOT
00341       fImp->BrowseObj(gROOT);
00342    }
00343 
00344    // The first list will be filled by TWin32BrowserImp ctor
00345    // with all browsable classes from TROOT
00346 }
00347 
00348 //______________________________________________________________________________
00349 void TBrowser::ExecuteDefaultAction(TObject *obj)
00350 {
00351    // Execute default action for selected object (action is specified
00352    // in the $HOME/.root.mimes or $ROOTSYS/etc/root.mimes file).
00353 
00354    if (obj && fImp)
00355       fImp->ExecuteDefaultAction(obj);
00356 }
00357 
00358 //______________________________________________________________________________
00359 void TBrowser::RecursiveRemove(TObject *obj)
00360 {
00361    // Recursively remove obj from browser.
00362 
00363    if (fImp && obj) {
00364       fImp->RecursiveRemove(obj);
00365       fNeedRefresh = kTRUE;
00366    }
00367 }
00368 
00369 //______________________________________________________________________________
00370 void TBrowser::Refresh()
00371 {
00372    // Refresh browser contents.
00373 
00374    fNeedRefresh = kTRUE;
00375    if (fImp) fImp->Refresh();
00376    fNeedRefresh = kFALSE;
00377 }
00378 
00379 //______________________________________________________________________________
00380 void TBrowser::SetSelected(TObject *clickedObject)
00381 {
00382    // Assign the last selected object.
00383 
00384    fLastSelectedObject = clickedObject;
00385 }
00386 
00387 //////////////////////////////////////////////////////////////////////////
00388 //                                                                      //
00389 //  TBrowserTimer                                                       //
00390 //                                                                      //
00391 //////////////////////////////////////////////////////////////////////////
00392 
00393 //______________________________________________________________________________
00394 Bool_t TBrowserTimer::Notify()
00395 {
00396    // Called whenever timer times out.
00397 
00398    if (fBrowser) {
00399       if (fBrowser->GetRefreshFlag()) {
00400          fBrowser->SetRefreshFlag(kFALSE);
00401          fActivate = kTRUE;
00402       } else if (fActivate) {
00403          fActivate = kFALSE;
00404          fBrowser->Refresh();
00405       }
00406    }
00407    Reset();
00408 
00409    return kFALSE;
00410 }
00411 
00412 //////////////////////////////////////////////////////////////////////////
00413 //                                                                      //
00414 //  TBrowserObject                                                      //
00415 //                                                                      //
00416 //  This is a wrapper class to emulate the TObject interface            //
00417 //  around an object of a non-TObject class                             //
00418 //                                                                      //
00419 //////////////////////////////////////////////////////////////////////////
00420 
00421 //______________________________________________________________________________
00422 TBrowserObject::TBrowserObject(void *obj, TClass *cl, const char *brname)
00423    : TNamed(brname, cl ? cl->GetName() : ""), fObj(obj), fClass(cl)
00424 {
00425    // Constructor.
00426 
00427    if (cl==0) Fatal("Constructor","Class parameter should not be null");
00428    SetBit(kCanDelete);
00429 }
00430 
00431 //______________________________________________________________________________
00432 Bool_t TBrowserObject::IsFolder() const
00433 {
00434    // Return kTRUE if the object is a folder (contains browsable objects).
00435 
00436    return fClass->IsFolder(fObj);
00437 }
00438 
00439 //______________________________________________________________________________
00440 void TBrowserObject::Browse(TBrowser* b)
00441 {
00442    // Browse the content of the underlying object.
00443 
00444    fClass->Browse(fObj, b);
00445 }

Generated on Tue Jul 5 14:11:19 2011 for ROOT_528-00b_version by  doxygen 1.5.1