ROOT logo

From $ROOTSYS/tutorials/gui/statusBar.C

//
// Author: Ilka Antcheva   1/12/2006

// 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 
// exactly as the status bar of any canvas window
// To run it do either:
// .x statusBar.C
// .x statusBar.C++

#include <TApplication.h>
#include <TGClient.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TFrame.h>
#include <TRootEmbeddedCanvas.h>
#include <TGStatusBar.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TRandom.h>
#include <TGraph.h>
#include <TAxis.h>


class MyMainFrame : public TGMainFrame {

private:
   TRootEmbeddedCanvas  *fEcan;
   TGStatusBar          *fStatusBar;
   
public:
   MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
   virtual ~MyMainFrame();
   void DoExit();
   void DoDraw();
   void SetStatusText(const char *txt, Int_t pi);
   void EventInfo(Int_t event, Int_t px, Int_t py, TObject *selected);
   
   ClassDef(MyMainFrame, 0)
};

void MyMainFrame::DoDraw()
{
   // Draw something in the canvas

   Printf("Slot DoDraw()");

   TCanvas *c1 = fEcan->GetCanvas();
   c1->SetFillColor(42);
   c1->SetGrid();
   const Int_t n = 20;
   Double_t x[n], y[n];
   for (Int_t i=0;i<n;i++) {
     x[i] = i*0.1;
     y[i] = 10*sin(x[i]+0.2);
     printf(" i %i %f %f \n",i,x[i],y[i]);
   }
   TGraph *gr = new TGraph(n,x,y);
   gr->SetLineColor(2);
   gr->SetLineWidth(4);
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(21);
   gr->SetTitle("a simple graph");
   gr->GetXaxis()->SetTitle("X title");
   gr->GetYaxis()->SetTitle("Y title");
   gr->Draw("ACP");
   
   // TCanvas::Update() draws the frame, after which it can be changed
   c1->Update();
   c1->GetFrame()->SetFillColor(21);
   c1->GetFrame()->SetBorderSize(12);
   c1->Modified();
   c1->Update();
}

void MyMainFrame::DoExit()
{
   printf("Exit application...");
   gApplication->Terminate(0);
}

void MyMainFrame::SetStatusText(const char *txt, Int_t pi)
{
   // Set text in status bar.
   fStatusBar->SetText(txt,pi);
}

void MyMainFrame::EventInfo(Int_t event, Int_t px, Int_t py, TObject *selected)
{
//  Writes the event status in the status bar parts

   const char *text0, *text1, *text3;
   char text2[50];
   text0 = selected->GetTitle();
   SetStatusText(text0,0);
   text1 = selected->GetName();
   SetStatusText(text1,1);
   if (event == kKeyPress)
      sprintf(text2, "%c", (char) px);
   else
      sprintf(text2, "%d,%d", px, py);
   SetStatusText(text2,2);
   text3 = selected->GetObjectInfo(px,py);
   SetStatusText(text3,3);
}

MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h) :
   TGMainFrame(p, w, h)
{
   // Create the embedded canvas
   fEcan = new TRootEmbeddedCanvas(0,this,500,400);
   Int_t wid = fEcan->GetCanvasWindowId();
   TCanvas *myc = new TCanvas("MyCanvas", 10,10,wid);
   fEcan->AdoptCanvas(myc);
   myc->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)","MyMainFrame",this, 
               "EventInfo(Int_t,Int_t,Int_t,TObject*)");

   AddFrame(fEcan, new TGLayoutHints(kLHintsTop | kLHintsLeft | 
                                     kLHintsExpandX  | kLHintsExpandY,0,0,1,1));
   // status bar
   Int_t parts[] = {45, 15, 10, 30};
   fStatusBar = new TGStatusBar(this, 50, 10, kVerticalFrame);
   fStatusBar->SetParts(parts, 4);
   fStatusBar->Draw3DCorner(kFALSE);
   AddFrame(fStatusBar, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
   
   // Create a horizontal frame containing two buttons
   TGHorizontalFrame *hframe = new TGHorizontalFrame(this, 200, 40);
  
   TGTextButton *draw = new TGTextButton(hframe, "&Draw");
   draw->Connect("Clicked()", "MyMainFrame", this, "DoDraw()");
   hframe->AddFrame(draw, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4));
   TGTextButton *exit = new TGTextButton(hframe, "&Exit ");
   exit->Connect("Pressed()", "MyMainFrame", this, "DoExit()");
   hframe->AddFrame(exit, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4));

   AddFrame(hframe, new TGLayoutHints(kLHintsCenterX, 2, 2, 2, 2));

   // Set a name to the main frame   
   SetWindowName("Embedded Canvas Status Info");
   MapSubwindows();

   // Initialize the layout algorithm via Resize()
   Resize(GetDefaultSize());

   // Map main frame
   MapWindow();
}


MyMainFrame::~MyMainFrame()
{
   // Clean up main frame...
   Cleanup();
   delete fEcan;
}


void statusBar()
{
   // Popup the GUI...
   new MyMainFrame(gClient->GetRoot(), 200, 200);
}
 statusBar.C:1
 statusBar.C:2
 statusBar.C:3
 statusBar.C:4
 statusBar.C:5
 statusBar.C:6
 statusBar.C:7
 statusBar.C:8
 statusBar.C:9
 statusBar.C:10
 statusBar.C:11
 statusBar.C:12
 statusBar.C:13
 statusBar.C:14
 statusBar.C:15
 statusBar.C:16
 statusBar.C:17
 statusBar.C:18
 statusBar.C:19
 statusBar.C:20
 statusBar.C:21
 statusBar.C:22
 statusBar.C:23
 statusBar.C:24
 statusBar.C:25
 statusBar.C:26
 statusBar.C:27
 statusBar.C:28
 statusBar.C:29
 statusBar.C:30
 statusBar.C:31
 statusBar.C:32
 statusBar.C:33
 statusBar.C:34
 statusBar.C:35
 statusBar.C:36
 statusBar.C:37
 statusBar.C:38
 statusBar.C:39
 statusBar.C:40
 statusBar.C:41
 statusBar.C:42
 statusBar.C:43
 statusBar.C:44
 statusBar.C:45
 statusBar.C:46
 statusBar.C:47
 statusBar.C:48
 statusBar.C:49
 statusBar.C:50
 statusBar.C:51
 statusBar.C:52
 statusBar.C:53
 statusBar.C:54
 statusBar.C:55
 statusBar.C:56
 statusBar.C:57
 statusBar.C:58
 statusBar.C:59
 statusBar.C:60
 statusBar.C:61
 statusBar.C:62
 statusBar.C:63
 statusBar.C:64
 statusBar.C:65
 statusBar.C:66
 statusBar.C:67
 statusBar.C:68
 statusBar.C:69
 statusBar.C:70
 statusBar.C:71
 statusBar.C:72
 statusBar.C:73
 statusBar.C:74
 statusBar.C:75
 statusBar.C:76
 statusBar.C:77
 statusBar.C:78
 statusBar.C:79
 statusBar.C:80
 statusBar.C:81
 statusBar.C:82
 statusBar.C:83
 statusBar.C:84
 statusBar.C:85
 statusBar.C:86
 statusBar.C:87
 statusBar.C:88
 statusBar.C:89
 statusBar.C:90
 statusBar.C:91
 statusBar.C:92
 statusBar.C:93
 statusBar.C:94
 statusBar.C:95
 statusBar.C:96
 statusBar.C:97
 statusBar.C:98
 statusBar.C:99
 statusBar.C:100
 statusBar.C:101
 statusBar.C:102
 statusBar.C:103
 statusBar.C:104
 statusBar.C:105
 statusBar.C:106
 statusBar.C:107
 statusBar.C:108
 statusBar.C:109
 statusBar.C:110
 statusBar.C:111
 statusBar.C:112
 statusBar.C:113
 statusBar.C:114
 statusBar.C:115
 statusBar.C:116
 statusBar.C:117
 statusBar.C:118
 statusBar.C:119
 statusBar.C:120
 statusBar.C:121
 statusBar.C:122
 statusBar.C:123
 statusBar.C:124
 statusBar.C:125
 statusBar.C:126
 statusBar.C:127
 statusBar.C:128
 statusBar.C:129
 statusBar.C:130
 statusBar.C:131
 statusBar.C:132
 statusBar.C:133
 statusBar.C:134
 statusBar.C:135
 statusBar.C:136
 statusBar.C:137
 statusBar.C:138
 statusBar.C:139
 statusBar.C:140
 statusBar.C:141
 statusBar.C:142
 statusBar.C:143
 statusBar.C:144
 statusBar.C:145
 statusBar.C:146
 statusBar.C:147
 statusBar.C:148
 statusBar.C:149
 statusBar.C:150
 statusBar.C:151
 statusBar.C:152
 statusBar.C:153
 statusBar.C:154
 statusBar.C:155
 statusBar.C:156
 statusBar.C:157
 statusBar.C:158
 statusBar.C:159
 statusBar.C:160
 statusBar.C:161
 statusBar.C:162
 statusBar.C:163
 statusBar.C:164