Logo ROOT  
Reference Guide
TInspectCanvas.cxx
Go to the documentation of this file.
1// @(#)root/gpad:$Id$
2// Author: Rene Brun 08/01/2000
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#include "TROOT.h"
13#include "TDatime.h"
14#include "TGuiFactory.h"
15#include "TInspectCanvas.h"
16#include "TButton.h"
17#include "TClass.h"
18#include "TLine.h"
19#include "TLink.h"
20#include "TDataMember.h"
21#include "TDataType.h"
22#include "TRealData.h"
23#include "strlcpy.h"
24#include "snprintf.h"
25
27
28
29/** \class TInspectorObject
30\ingroup gpad
31
32This class is designed to wrap a Foreign object in order to
33inject it into the Browse sub-system.
34*/
35
37{
38public:
39
40 TInspectorObject(void *obj, TClass *cl) : fObj(obj),fClass(cl) {};
42
43 void *GetObject() const { return fObj; };
44 void Inspect() const override
45 {
46 gGuiFactory->CreateInspectorImp(this, 400, 200);
47 }
48 TClass *IsA() const override { return fClass; }
49
50private:
51 void *fObj; //! pointer to the foreign object
52 TClass *fClass; //! pointer to class of the foreign object
53
54};
55
56
57/** \class TInspectCanvas
58\ingroup gpad
59
60A TInspectCanvas is a canvas specialized to inspect Root objects.
61*/
62
63////////////////////////////////////////////////////////////////////////////////
64/// InspectCanvas default constructor.
65
67{
68 fBackward = 0;
69 fForward = 0;
70 fCurObject = 0;
71 fObjects = 0;
72 fLogx = kFALSE;
73 fLogy = kFALSE;
74 SetFillColor(0);
75}
76
77////////////////////////////////////////////////////////////////////////////////
78/// InspectCanvas constructor.
79
81 : TCanvas("inspect","ROOT Object Inspector",ww,wh)
82{
83 fBackward = 0;
84 fForward = 0;
85 fCurObject = 0;
86 fObjects = new TList;
87 fLogx = kFALSE;
88 fLogy = kFALSE;
89 SetFillColor(0);
90}
91
92////////////////////////////////////////////////////////////////////////////////
93/// InspectCanvas default destructor.
94
96{
97 if (fObjects) {
98 fObjects->Clear("nodelete");
99 delete fObjects;
100 }
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Dump contents of obj in a graphics canvas.
105/// Same action as TObject::Dump but in a graphical form.
106/// In addition pointers to other objects can be followed.
107///
108/// The following picture is the Inspect of a histogram object:
109/// \image html gpad_inspect.png
110
112{
113 Int_t cdate = 0;
114 Int_t ctime = 0;
115 UInt_t *cdatime = 0;
116 Bool_t isdate = kFALSE;
117 Bool_t isbits = kFALSE;
118 const Int_t kname = 1;
119 const Int_t kvalue = 25;
120 const Int_t ktitle = 37;
121 const Int_t kline = 1024;
122 char line[kline];
123 char *pname;
124
125 TClass *cl = obj->IsA();
126 if (cl == 0) return;
127 TInspectorObject *proxy=0;
128 if (!cl->IsTObject()) {
129 // This is possible only if obj is actually a TInspectorObject
130 // wrapping a non-TObject.
131 proxy = (TInspectorObject*)obj;
132 obj = (TObject*)proxy->GetObject();
133 }
134
135 if (!cl->GetListOfRealData()) cl->BuildRealData(obj);
136
137 // Count number of data members in order to resize the canvas
138 TRealData *rd;
139 TIter next(cl->GetListOfRealData());
140 Int_t nreal = cl->GetListOfRealData()->GetSize();
141 if (nreal == 0) return;
142
143 Int_t nrows = 33;
144 if (nreal+7 > nrows) nrows = nreal+7;
145 Int_t nh = nrows*15;
146 Int_t nw = 700;
147 TVirtualPad *canvas = GetVirtCanvas();
148 if (canvas) {
149 canvas->Clear(); // remove primitives from canvas
150 canvas->SetCanvasSize(nw, nh); // set new size of drawing area
151 canvas->Range(0,-3,20,nreal+4);
152 }
153
154 Float_t xvalue = 5;
155 Float_t xtitle = 8;
156 Float_t dy = 1;
157 Float_t ytext = Float_t(nreal) - 1.5;
158 Float_t tsize = 0.99/ytext;
159 if (tsize < 0.02) tsize = 0.02;
160 if (tsize > 0.03) tsize = 0.03;
161
162 // Create text objects
163 TText tname, tvalue, ttitle;
164 TText *tval;
165 tname.SetTextFont(61);
166 tname.SetTextAngle(0);
167 tname.SetTextAlign(12);
168 tname.SetTextColor(1);
169 tname.SetTextSize(tsize);
170 tvalue.SetTextFont(61);
171 tvalue.SetTextAngle(0);
172 tvalue.SetTextAlign(12);
173 tvalue.SetTextColor(1);
174 tvalue.SetTextSize(tsize);
175 ttitle.SetTextFont(62);
176 ttitle.SetTextAngle(0);
177 ttitle.SetTextAlign(12);
178 ttitle.SetTextColor(1);
179 ttitle.SetTextSize(tsize);
180
181 Float_t x1 = 0.2;
182 Float_t x2 = 19.8;
183 Float_t y1 = -0.5;
184 Float_t y2 = Float_t(nreal) - 0.5;
185 Float_t y3 = y2 + 1;
186 Float_t y4 = y3 + 1.5;
187 Float_t db = 25./GetWh();
188 Float_t btop = 0.999;
189
190 // Draw buttons
191 fBackward = new TButton("backward","TInspectCanvas::GoBackward();",.01,btop-db,.15,btop);
192 fBackward->Draw();
193 fBackward->SetToolTipText("Inspect previous object");
194 fForward = new TButton("forward", "TInspectCanvas::GoForward();", .21,btop-db,.35,btop);
195 fForward->Draw();
196 fForward->SetToolTipText("Inspect next object");
197
198 // Draw surrounding box and title areas
199 TLine frame;
200 frame.SetLineColor(1);
201 frame.SetLineStyle(1);
202 frame.SetLineWidth(1);
203 frame.DrawLine(x1, y1, x2, y1);
204 frame.DrawLine(x2, y1, x2, y4);
205 frame.DrawLine(x2, y4, x1, y4);
206 frame.DrawLine(x1, y4, x1, y1);
207 frame.DrawLine(x1, y2, x2, y2);
208 frame.DrawLine(x1, y3, x2, y3);
209 frame.DrawLine(xvalue, y1, xvalue, y3);
210 frame.DrawLine(xtitle, y1, xtitle, y3);
211 ttitle.SetTextSize(0.8*tsize);
212 ttitle.SetTextAlign(21);
213 ttitle.DrawText(0.5*(x1+xvalue), y2+0.1, "Member Name");
214 ttitle.DrawText(0.5*(xvalue+xtitle), y2+0.1, "Value");
215 ttitle.DrawText(0.5*(xtitle+x2), y2+0.1, "Title");
216 ttitle.SetTextSize(1.2*tsize);
217 ttitle.SetTextColor(2);
218 ttitle.SetTextAlign(11);
219 ttitle.DrawText(x1+0.2, y3+0.1, cl->GetName());
220 if (proxy==0) {
221 ttitle.SetTextColor(4);
222 strlcpy(line,obj->GetName(),kline);
223 ttitle.DrawText(xvalue+0.2, y3+0.1, line);
224 ttitle.SetTextColor(6);
225 ttitle.DrawText(xtitle+2, y3+0.1, obj->GetTitle());
226 } else {
227 ttitle.SetTextColor(4);
228 snprintf(line,1023,"%s:%d","Foreign object",0);
229 ttitle.DrawText(xvalue+0.2, y3+0.1, line);
230 ttitle.SetTextColor(6);
231 ttitle.DrawText(xtitle+2, y3+0.1, "no title given");
232 }
233 ttitle.SetTextSize(tsize);
234 ttitle.SetTextColor(1);
235 ttitle.SetTextFont(11);
236 ttitle.SetTextAlign(12);
237
238 //---Now loop on data members-----------------------
239 // We make 3 passes. Faster than one single pass because changing
240 // font parameters is time consuming
241 for (Int_t pass = 0; pass < 3; pass++) {
242 ytext = y2 - 0.5;
243 next.Reset();
244 while ((rd = (TRealData*) next())) {
245 TDataMember *member = rd->GetDataMember();
246 if (!member) continue;
247 TDataType *membertype = member->GetDataType();
248 isdate = kFALSE;
249 if (strcmp(member->GetName(),"fDatime") == 0 && membertype && membertype->GetType() == kUInt_t) {
250 isdate = kTRUE;
251 }
252 isbits = kFALSE;
253 if (strcmp(member->GetName(),"fBits") == 0 && membertype && membertype->GetType() == kUInt_t) {
254 isbits = kTRUE;
255 }
256
257 // Encode data member name
258 pname = &line[kname];
259 for (Int_t i=0;i<kline;i++) line[i] = ' ';
260 line[kline-1] = 0;
261 strlcpy(pname,rd->GetName(),kline-kname);
262 if (strstr(member->GetFullTypeName(),"**")) strlcat(pname,"**",kline-kname);
263
264 // Encode data value or pointer value
265 tval = &tvalue;
266 Int_t offset = rd->GetThisOffset();
267 char *pointer = (char*)obj + offset;
268 char **ppointer = (char**)(pointer);
269 TLink *tlink = 0;
270
271 TClass *clm=0;
272 if (!membertype) {
273 clm = member->GetClass();
274 }
275
276 if (member->IsaPointer()) {
277 char **p3pointer = (char**)(*ppointer);
278 if (clm && !clm->IsStartingWithTObject() ) {
279 //NOTE: memory leak!
280 p3pointer = (char**)new TInspectorObject(p3pointer,clm);
281 }
282
283 if (!p3pointer) {
284 snprintf(&line[kvalue],kline-kvalue,"->0");
285 } else if (!member->IsBasic()) {
286 if (pass == 1) {
287 tlink = new TLink(xvalue+0.1, ytext, p3pointer);
288 }
289 } else if (membertype) {
290 if (!strcmp(membertype->GetTypeName(), "char"))
291 strlcpy(&line[kvalue], *ppointer,kline-kvalue);
292 else
293 strlcpy(&line[kvalue], membertype->AsString(p3pointer),kline-kvalue);
294 } else if (!strcmp(member->GetFullTypeName(), "char*") ||
295 !strcmp(member->GetFullTypeName(), "const char*")) {
296 strlcpy(&line[kvalue], *ppointer,kline-kvalue);
297 } else {
298 if (pass == 1) tlink = new TLink(xvalue+0.1, ytext, p3pointer);
299 }
300 } else if (membertype)
301 if (isdate) {
302 cdatime = (UInt_t*)pointer;
303 TDatime::GetDateTime(cdatime[0],cdate,ctime);
304 snprintf(&line[kvalue],kline-kvalue,"%d/%d",cdate,ctime);
305 } else if (isbits) {
306 snprintf(&line[kvalue],kline-kvalue,"0x%08x", *(UInt_t*)pointer);
307 } else {
308 strlcpy(&line[kvalue], membertype->AsString(pointer),kline-kvalue);
309 }
310 else
311 snprintf(&line[kvalue],kline-kvalue,"->%zx ", (size_t)pointer);
312
313 // Encode data member title
314 Int_t ltit = 0;
315 if (isdate == kFALSE && strcmp(member->GetFullTypeName(), "char*") &&
316 strcmp(member->GetFullTypeName(), "const char*")) {
317 Int_t lentit = strlen(member->GetTitle());
318 if (lentit >= kline-ktitle) lentit = kline-ktitle-1;
319 strlcpy(&line[ktitle],member->GetTitle(),kline-ktitle);
320 line[ktitle+lentit] = 0;
321 ltit = ktitle;
322 }
323
324 // Ready to draw the name, value and title columns
325 if (pass == 0)tname.DrawText( x1+0.1, ytext, &line[kname]);
326 if (pass == 1) {
327 if (tlink) {
328 tlink->SetTextFont(61);
329 tlink->SetTextAngle(0);
330 tlink->SetTextAlign(12);
331 tlink->SetTextColor(2);
332 tlink->SetTextSize(tsize);
333 tlink->SetBit(kCanDelete);
334 tlink->Draw();
335 if (strstr(member->GetFullTypeName(),"**")) tlink->SetBit(TLink::kIsStarStar);
336 tlink->SetName(member->GetTypeName());
337 } else {
338 tval->DrawText(xvalue+0.1, ytext, &line[kvalue]);
339 }
340 }
341 if (pass == 2 && ltit) ttitle.DrawText(xtitle+0.3, ytext, &line[ltit]);
342 ytext -= dy;
343 }
344 }
345 Update();
346 fCurObject = obj;
347}
348
349////////////////////////////////////////////////////////////////////////////////
350/// static function , inspect previous object
351
353{
354 TInspectCanvas *inspect = (TInspectCanvas*)(gROOT->GetListOfCanvases())->FindObject("inspect");
355 if (!inspect) return;
356 TObject *cur = inspect->GetCurObject();
357 TObject *obj = inspect->GetObjects()->Before(cur);
358 if (obj) inspect->InspectObject(obj);
359}
360
361////////////////////////////////////////////////////////////////////////////////
362/// static function , inspect next object
363
365{
366 TInspectCanvas *inspect = (TInspectCanvas*)(gROOT->GetListOfCanvases())->FindObject("inspect");
367 if (!inspect) return;
368 TObject *cur = inspect->GetCurObject();
369 TObject *obj = inspect->GetObjects()->After(cur);
370 if (obj) inspect->InspectObject(obj);
371}
372
373////////////////////////////////////////////////////////////////////////////////
374/// static function , interface to InspectObject.
375/// Create the InspectCanvas if it does not exist yet.
376
378{
379 TVirtualPad *padsav = gPad;
380 TInspectCanvas *inspect = (TInspectCanvas*)(gROOT->GetListOfCanvases())->FindObject("inspect");
381 if (!inspect) inspect = new TInspectCanvas(700,600);
382 else inspect->cd();
383
384 inspect->InspectObject(obj);
385 inspect->GetObjects()->Add(obj);
386 //obj->SetBit(kMustCleanup);
387
388 if (padsav) padsav->cd();
389}
390
391////////////////////////////////////////////////////////////////////////////////
392/// Recursively remove object from the list of objects.
393
395{
396 fObjects->Remove(obj);
398}
const Bool_t kFALSE
Definition: RtypesCore.h:101
float Float_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassImp(name)
Definition: Rtypes.h:375
@ kUInt_t
Definition: TDataType.h:30
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 Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char x2
Option_t Option_t TPoint TPoint const char x1
Option_t Option_t TPoint TPoint const char y2
Option_t Option_t TPoint TPoint const char y1
R__EXTERN TGuiFactory * gGuiFactory
Definition: TGuiFactory.h:66
#define gROOT
Definition: TROOT.h:404
#define gPad
Definition: TVirtualPad.h:288
#define snprintf
Definition: civetweb.c:1540
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void SetLineStyle(Style_t lstyle)
Set the line style.
Definition: TAttLine.h:42
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition: TAttText.h:42
virtual void SetTextAngle(Float_t tangle=0)
Set the text angle.
Definition: TAttText.h:43
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:44
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition: TAttText.h:46
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:47
A TButton object is a user interface object.
Definition: TButton.h:18
void Draw(Option_t *option="") override
Draw this button with its current attributes.
Definition: TButton.cxx:139
The Canvas class.
Definition: TCanvas.h:23
TVirtualPad * cd(Int_t subpadnumber=0) override
Set current canvas & pad.
Definition: TCanvas.cxx:706
void Update() override
Update canvas pad buffers.
Definition: TCanvas.cxx:2502
UInt_t GetWh() const override
Definition: TCanvas.h:162
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition: TClass.h:80
void BuildRealData(void *pointer=0, Bool_t isTransient=kFALSE)
Build a full list of persistent data members.
Definition: TClass.cxx:2031
Bool_t IsStartingWithTObject() const
Returns true if this class inherits from TObject and if the start of the TObject parts is at the very...
Definition: TClass.cxx:5917
TList * GetListOfRealData() const
Definition: TClass.h:450
Bool_t IsTObject() const
Return kTRUE is the class inherits from TObject.
Definition: TClass.cxx:5926
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:184
All ROOT classes may have RTTI (run time type identification) support added.
Definition: TDataMember.h:31
Bool_t IsBasic() const
Return true if data member is a basic type, e.g. char, int, long...
Bool_t IsaPointer() const
Return true if data member is a pointer.
TDataType * GetDataType() const
Definition: TDataMember.h:76
const char * GetTypeName() const
Get type of data member, e,g.: "class TDirectory*" -> "TDirectory".
const char * GetFullTypeName() const
Get full type description of data member, e,g.: "class TDirectory*".
TClass * GetClass() const
Definition: TDataMember.h:75
Basic data type descriptor (datatype information is obtained from CINT).
Definition: TDataType.h:44
Int_t GetType() const
Definition: TDataType.h:68
const char * AsString(void *buf) const
Return string containing value in buffer formatted according to the basic data type.
Definition: TDataType.cxx:235
TString GetTypeName()
Get basic type of typedef, e,g.: "class TDirectory*" -> "TDirectory".
Definition: TDataType.cxx:149
static void GetDateTime(UInt_t datetime, Int_t &date, Int_t &time)
Static function that returns the date and time.
Definition: TDatime.cxx:435
virtual TInspectorImp * CreateInspectorImp(const TObject *obj, UInt_t width, UInt_t height)
Create a batch version of TInspectorImp.
A TInspectCanvas is a canvas specialized to inspect Root objects.
TButton * fBackward
Pointer to the Backward button.
TInspectCanvas()
InspectCanvas default constructor.
static void Inspector(TObject *obj)
static function , interface to InspectObject.
TList * fObjects
List of objects inspected.
TObject * fCurObject
Pointer to object being inspected.
TList * GetObjects() const
void RecursiveRemove(TObject *obj) override
Recursively remove object from the list of objects.
TObject * GetCurObject() const
static void GoBackward()
static function , inspect previous object
TButton * fForward
Pointer to the Forward button.
static void GoForward()
static function , inspect next object
virtual void InspectObject(TObject *obj)
Dump contents of obj in a graphics canvas.
virtual ~TInspectCanvas()
InspectCanvas default destructor.
This class is designed to wrap a Foreign object in order to inject it into the Browse sub-system.
TClass * IsA() const override
void * GetObject() const
TInspectorObject(void *obj, TClass *cl)
void Inspect() const override
Dump contents of this object in a graphics canvas.
TClass * fClass
pointer to the foreign object
void Reset()
Definition: TCollection.h:254
Use the TLine constructor to create a simple line.
Definition: TLine.h:22
virtual TLine * DrawLine(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Draw this line with new coordinates.
Definition: TLine.cxx:102
A doubly linked list.
Definition: TList.h:38
TObject * After(const TObject *obj) const override
Returns the object after object obj.
Definition: TList.cxx:330
void Clear(Option_t *option="") override
Remove all objects from the list.
Definition: TList.cxx:402
TObject * Before(const TObject *obj) const override
Returns the object before object obj.
Definition: TList.cxx:371
void Add(TObject *obj) override
Definition: TList.h:81
TObject * Remove(TObject *obj) override
Remove object from the list.
Definition: TList.cxx:822
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition: TNamed.h:48
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
Mother of all ROOT objects.
Definition: TObject.h:37
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:696
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:403
virtual TClass * IsA() const
Definition: TObject.h:230
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition: TObject.cxx:197
@ kCanDelete
if object in a list can be deleted
Definition: TObject.h:58
void RecursiveRemove(TObject *obj) override
Recursively remove object from a pad and its sub-pads.
Definition: TPad.cxx:5267
void SetToolTipText(const char *text, Long_t delayms=1000) override
Set tool tip text associated with this pad.
Definition: TPad.cxx:6559
TVirtualPad * GetVirtCanvas() const override
Get virtual canvas.
Definition: TPad.cxx:2699
Int_t fLogx
(=0 if X linear scale, =1 if log scale)
Definition: TPad.h:87
TObject * FindObject(const char *name) const override
Search if object named name is inside this pad or in pads inside this pad.
Definition: TPad.cxx:2619
Int_t fLogy
(=0 if Y linear scale, =1 if log scale)
Definition: TPad.h:88
The TRealData class manages the effective list of all data members for a given class.
Definition: TRealData.h:30
const char * GetName() const override
Returns name of object.
Definition: TRealData.h:52
TDataMember * GetDataMember() const
Definition: TRealData.h:53
Long_t GetThisOffset() const
Definition: TRealData.h:55
Base class for several text objects.
Definition: TText.h:22
virtual TText * DrawText(Double_t x, Double_t y, const char *text)
Draw this text with new coordinates.
Definition: TText.cxx:175
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:51
virtual void SetCanvasSize(UInt_t ww, UInt_t wh)=0
virtual TVirtualPad * cd(Int_t subpadnumber=0)=0
virtual void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2)=0
void Clear(Option_t *option="") override=0
TLine * line