Logo ROOT   6.16/01
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
30class MyMainFrame : public TGMainFrame {
31
32private:
34 TGStatusBar *fStatusBar;
35
36public:
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
47void 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);
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
81void MyMainFrame::DoExit()
82{
83 printf("Exit application...");
85}
86
87void MyMainFrame::SetStatusText(const char *txt, Int_t pi)
88{
89 // Set text in status bar.
90 fStatusBar->SetText(txt,pi);
91}
92
93void 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
112MyMainFrame::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
156MyMainFrame::~MyMainFrame()
157{
158 // Clean up main frame...
159 Cleanup();
160 delete fEcan;
161}
162
163
164void statusBar()
165{
166 // Popup the GUI...
167 new MyMainFrame(gClient->GetRoot(), 200, 200);
168}
@ kKeyPress
Definition: Buttons.h:20
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
const Bool_t kFALSE
Definition: RtypesCore.h:88
double Double_t
Definition: RtypesCore.h:55
#define ClassDef(name, id)
Definition: Rtypes.h:324
R__EXTERN TApplication * gApplication
Definition: TApplication.h:165
#define gClient
Definition: TGClient.h:166
@ kVerticalFrame
Definition: TGFrame.h:59
@ kLHintsExpandY
Definition: TGLayout.h:38
@ kLHintsLeft
Definition: TGLayout.h:31
@ kLHintsCenterX
Definition: TGLayout.h:32
@ kLHintsTop
Definition: TGLayout.h:34
@ kLHintsExpandX
Definition: TGLayout.h:37
#define Printf
Definition: TGeoToOCC.h:18
double sin(double)
virtual void Terminate(Int_t status=0)
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 SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
The Canvas class.
Definition: TCanvas.h:31
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2232
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:747
TAxis * GetXaxis() const
Get x axis of the graph.
Definition: TGraph.cxx:1594
TAxis * GetYaxis() const
Get y axis of the graph.
Definition: TGraph.cxx:1604
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
Mother of all ROOT objects.
Definition: TObject.h:37
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
virtual char * GetObjectInfo(Int_t px, Int_t py) const
Returns string containing info about the object at position (px,py).
Definition: TObject.cxx:386
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:401
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:867
return c1
Definition: legend1.C:41
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
TGraphErrors * gr
Definition: legend1.C:25
static constexpr double pi