Logo ROOT  
Reference Guide
statusBar.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_gui
3 /// This macro gives an example of how to create a status bar related to an embedded canvas that shows the info of the selected object,
4 /// exactly as the status bar of any canvas window.
5 /// To run it do either:
6 /// ~~~
7 /// .x statusBar.C
8 /// .x statusBar.C++
9 /// ~~~
10 ///
11 /// \macro_code
12 ///
13 /// \author Ilka Antcheva 1/12/2006
14 
15 
16 #include <TApplication.h>
17 #include <TGClient.h>
18 #include <TGButton.h>
19 #include <TGFrame.h>
20 #include <TFrame.h>
21 #include <TRootEmbeddedCanvas.h>
22 #include <TGStatusBar.h>
23 #include <TCanvas.h>
24 #include <TF1.h>
25 #include <TRandom.h>
26 #include <TGraph.h>
27 #include <TAxis.h>
28 
29 
30 class MyMainFrame : public TGMainFrame {
31 
32 private:
33  TRootEmbeddedCanvas *fEcan;
34  TGStatusBar *fStatusBar;
35 
36 public:
37  MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
38  virtual ~MyMainFrame();
39  void DoExit();
40  void DoDraw();
41  void SetStatusText(const char *txt, Int_t pi);
42  void EventInfo(Int_t event, Int_t px, Int_t py, TObject *selected);
43 
44  ClassDef(MyMainFrame, 0)
45 };
46 
47 void MyMainFrame::DoDraw()
48 {
49  // Draw something in the canvas
50 
51  Printf("Slot DoDraw()");
52 
53  TCanvas *c1 = fEcan->GetCanvas();
54  c1->SetFillColor(42);
55  c1->SetGrid();
56  const Int_t n = 20;
57  Double_t x[n], y[n];
58  for (Int_t i=0;i<n;i++) {
59  x[i] = i*0.1;
60  y[i] = 10*sin(x[i]+0.2);
61  printf(" i %i %f %f \n",i,x[i],y[i]);
62  }
63  TGraph *gr = new TGraph(n,x,y);
64  gr->SetLineColor(2);
65  gr->SetLineWidth(4);
66  gr->SetMarkerColor(4);
67  gr->SetMarkerStyle(21);
68  gr->SetTitle("a simple graph");
69  gr->GetXaxis()->SetTitle("X title");
70  gr->GetYaxis()->SetTitle("Y title");
71  gr->Draw("ACP");
72 
73  // TCanvas::Update() draws the frame, after which it can be changed
74  c1->Update();
75  c1->GetFrame()->SetFillColor(21);
76  c1->GetFrame()->SetBorderSize(12);
77  c1->Modified();
78  c1->Update();
79 }
80 
81 void MyMainFrame::DoExit()
82 {
83  printf("Exit application...");
85 }
86 
87 void MyMainFrame::SetStatusText(const char *txt, Int_t pi)
88 {
89  // Set text in status bar.
90  fStatusBar->SetText(txt,pi);
91 }
92 
93 void MyMainFrame::EventInfo(Int_t event, Int_t px, Int_t py, TObject *selected)
94 {
95 // Writes the event status in the status bar parts
96 
97  const char *text0, *text1, *text3;
98  char text2[50];
99  text0 = selected->GetTitle();
100  SetStatusText(text0,0);
101  text1 = selected->GetName();
102  SetStatusText(text1,1);
103  if (event == kKeyPress)
104  sprintf(text2, "%c", (char) px);
105  else
106  sprintf(text2, "%d,%d", px, py);
107  SetStatusText(text2,2);
108  text3 = selected->GetObjectInfo(px,py);
109  SetStatusText(text3,3);
110 }
111 
112 MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h) :
113  TGMainFrame(p, w, h)
114 {
115  // Create the embedded canvas
116  fEcan = new TRootEmbeddedCanvas(0,this,500,400);
117  Int_t wid = fEcan->GetCanvasWindowId();
118  TCanvas *myc = new TCanvas("MyCanvas", 10,10,wid);
119  fEcan->AdoptCanvas(myc);
120  myc->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)","MyMainFrame",this,
121  "EventInfo(Int_t,Int_t,Int_t,TObject*)");
122 
123  AddFrame(fEcan, new TGLayoutHints(kLHintsTop | kLHintsLeft |
124  kLHintsExpandX | kLHintsExpandY,0,0,1,1));
125  // status bar
126  Int_t parts[] = {45, 15, 10, 30};
127  fStatusBar = new TGStatusBar(this, 50, 10, kVerticalFrame);
128  fStatusBar->SetParts(parts, 4);
129  fStatusBar->Draw3DCorner(kFALSE);
130  AddFrame(fStatusBar, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
131 
132  // Create a horizontal frame containing two buttons
133  TGHorizontalFrame *hframe = new TGHorizontalFrame(this, 200, 40);
134 
135  TGTextButton *draw = new TGTextButton(hframe, "&Draw");
136  draw->Connect("Clicked()", "MyMainFrame", this, "DoDraw()");
137  hframe->AddFrame(draw, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4));
138  TGTextButton *exit = new TGTextButton(hframe, "&Exit ");
139  exit->Connect("Pressed()", "MyMainFrame", this, "DoExit()");
140  hframe->AddFrame(exit, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4));
141 
142  AddFrame(hframe, new TGLayoutHints(kLHintsCenterX, 2, 2, 2, 2));
143 
144  // Set a name to the main frame
145  SetWindowName("Embedded Canvas Status Info");
146  MapSubwindows();
147 
148  // Initialize the layout algorithm via Resize()
149  Resize(GetDefaultSize());
150 
151  // Map main frame
152  MapWindow();
153 }
154 
155 
156 MyMainFrame::~MyMainFrame()
157 {
158  // Clean up main frame...
159  Cleanup();
160  delete fEcan;
161 }
162 
163 
164 void statusBar()
165 {
166  // Popup the GUI...
167  new MyMainFrame(gClient->GetRoot(), 200, 200);
168 }
n
const Int_t n
Definition: legend1.C:16
TGWindow
Definition: TGWindow.h:31
kLHintsCenterX
@ kLHintsCenterX
Definition: TGLayout.h:32
TGMainFrame
Definition: TGFrame.h:443
TGraph::SetTitle
virtual void SetTitle(const char *title="")
Change (i.e.
Definition: TGraph.cxx:2329
TGraph::Draw
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:760
kLHintsTop
@ kLHintsTop
Definition: TGLayout.h:34
kVerticalFrame
@ kVerticalFrame
Definition: GuiTypes.h:381
TGraph.h
TGStatusBar.h
kLHintsLeft
@ kLHintsLeft
Definition: TGLayout.h:31
TRandom.h
TObject::GetObjectInfo
virtual char * GetObjectInfo(Int_t px, Int_t py) const
Returns string containing info about the object at position (px,py).
Definition: TObject.cxx:388
TGTextButton
Definition: TGButton.h:142
Int_t
int Int_t
Definition: RtypesCore.h:45
sin
double sin(double)
x
Double_t x[n]
Definition: legend1.C:17
TGHorizontalFrame
Definition: TGFrame.h:422
TAttLine::SetLineColor
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
TObject::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:403
TCanvas.h
TGraph::GetYaxis
TAxis * GetYaxis() const
Get y axis of the graph.
Definition: TGraph.cxx:1641
TGStatusBar
Definition: TGStatusBar.h:29
gClient
#define gClient
Definition: TGClient.h:166
TApplication.h
TObject::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
TGraph::GetXaxis
TAxis * GetXaxis() const
Get x axis of the graph.
Definition: TGraph.cxx:1631
h
#define h(i)
Definition: RSha256.hxx:106
gr
TGraphErrors * gr
Definition: legend1.C:25
TAttMarker::SetMarkerColor
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TGFrame.h
TGLayoutHints
Definition: TGLayout.h:57
gApplication
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
y
Double_t y[n]
Definition: legend1.C:17
TApplication::Terminate
virtual void Terminate(Int_t status=0)
kLHintsExpandY
@ kLHintsExpandY
Definition: TGLayout.h:38
TGClient.h
TNamed::SetTitle
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
TRootEmbeddedCanvas.h
Printf
void Printf(const char *fmt,...)
TAttLine::SetLineWidth
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
Double_t
double Double_t
Definition: RtypesCore.h:59
TQObject::Connect
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot.
Definition: TQObject.cxx:866
TGraph
A TGraph is an object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
TF1.h
TCanvas
The Canvas class.
Definition: TCanvas.h:23
TGeant4Unit::pi
static constexpr double pi
Definition: TGeant4SystemOfUnits.h:67
TAxis.h
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TGButton.h
TFrame.h
kLHintsExpandX
@ kLHintsExpandX
Definition: TGLayout.h:37
kKeyPress
@ kKeyPress
Definition: Buttons.h:20
TAttMarker::SetMarkerStyle
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
TGCompositeFrame::AddFrame
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1102
TRootEmbeddedCanvas
Definition: TRootEmbeddedCanvas.h:32
c1
return c1
Definition: legend1.C:41