statusBar.C

Go to the documentation of this file.
00001 //
00002 // Author: Ilka Antcheva   1/12/2006
00003 
00004 // This macro gives an example of how to create a status bar 
00005 // related to an embedded canvas that shows the info of the selected object 
00006 // exactly as the status bar of any canvas window
00007 // To run it do either:
00008 // .x statusBar.C
00009 // .x statusBar.C++
00010 
00011 #include <TApplication.h>
00012 #include <TGClient.h>
00013 #include <TGButton.h>
00014 #include <TGFrame.h>
00015 #include <TFrame.h>
00016 #include <TRootEmbeddedCanvas.h>
00017 #include <TGStatusBar.h>
00018 #include <TCanvas.h>
00019 #include <TF1.h>
00020 #include <TRandom.h>
00021 #include <TGraph.h>
00022 #include <TAxis.h>
00023 
00024 
00025 class MyMainFrame : public TGMainFrame {
00026 
00027 private:
00028    TRootEmbeddedCanvas  *fEcan;
00029    TGStatusBar          *fStatusBar;
00030    
00031 public:
00032    MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
00033    virtual ~MyMainFrame();
00034    void DoExit();
00035    void DoDraw();
00036    void SetStatusText(const char *txt, Int_t pi);
00037    void EventInfo(Int_t event, Int_t px, Int_t py, TObject *selected);
00038    
00039    ClassDef(MyMainFrame, 0)
00040 };
00041 
00042 void MyMainFrame::DoDraw()
00043 {
00044    // Draw something in the canvas
00045 
00046    Printf("Slot DoDraw()");
00047 
00048    TCanvas *c1 = fEcan->GetCanvas();
00049    c1->SetFillColor(42);
00050    c1->SetGrid();
00051    const Int_t n = 20;
00052    Double_t x[n], y[n];
00053    for (Int_t i=0;i<n;i++) {
00054      x[i] = i*0.1;
00055      y[i] = 10*sin(x[i]+0.2);
00056      printf(" i %i %f %f \n",i,x[i],y[i]);
00057    }
00058    TGraph *gr = new TGraph(n,x,y);
00059    gr->SetLineColor(2);
00060    gr->SetLineWidth(4);
00061    gr->SetMarkerColor(4);
00062    gr->SetMarkerStyle(21);
00063    gr->SetTitle("a simple graph");
00064    gr->GetXaxis()->SetTitle("X title");
00065    gr->GetYaxis()->SetTitle("Y title");
00066    gr->Draw("ACP");
00067    
00068    // TCanvas::Update() draws the frame, after which it can be changed
00069    c1->Update();
00070    c1->GetFrame()->SetFillColor(21);
00071    c1->GetFrame()->SetBorderSize(12);
00072    c1->Modified();
00073    c1->Update();
00074 }
00075 
00076 void MyMainFrame::DoExit()
00077 {
00078    printf("Exit application...");
00079    gApplication->Terminate(0);
00080 }
00081 
00082 void MyMainFrame::SetStatusText(const char *txt, Int_t pi)
00083 {
00084    // Set text in status bar.
00085    fStatusBar->SetText(txt,pi);
00086 }
00087 
00088 void MyMainFrame::EventInfo(Int_t event, Int_t px, Int_t py, TObject *selected)
00089 {
00090 //  Writes the event status in the status bar parts
00091 
00092    const char *text0, *text1, *text3;
00093    char text2[50];
00094    text0 = selected->GetTitle();
00095    SetStatusText(text0,0);
00096    text1 = selected->GetName();
00097    SetStatusText(text1,1);
00098    if (event == kKeyPress)
00099       sprintf(text2, "%c", (char) px);
00100    else
00101       sprintf(text2, "%d,%d", px, py);
00102    SetStatusText(text2,2);
00103    text3 = selected->GetObjectInfo(px,py);
00104    SetStatusText(text3,3);
00105 }
00106 
00107 MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h) :
00108    TGMainFrame(p, w, h)
00109 {
00110    // Create the embedded canvas
00111    fEcan = new TRootEmbeddedCanvas(0,this,500,400);
00112    Int_t wid = fEcan->GetCanvasWindowId();
00113    TCanvas *myc = new TCanvas("MyCanvas", 10,10,wid);
00114    fEcan->AdoptCanvas(myc);
00115    myc->Connect("ProcessedEvent(Int_t,Int_t,Int_t,TObject*)","MyMainFrame",this, 
00116                "EventInfo(Int_t,Int_t,Int_t,TObject*)");
00117 
00118    AddFrame(fEcan, new TGLayoutHints(kLHintsTop | kLHintsLeft | 
00119                                      kLHintsExpandX  | kLHintsExpandY,0,0,1,1));
00120    // status bar
00121    Int_t parts[] = {45, 15, 10, 30};
00122    fStatusBar = new TGStatusBar(this, 50, 10, kVerticalFrame);
00123    fStatusBar->SetParts(parts, 4);
00124    fStatusBar->Draw3DCorner(kFALSE);
00125    AddFrame(fStatusBar, new TGLayoutHints(kLHintsExpandX, 0, 0, 10, 0));
00126    
00127    // Create a horizontal frame containing two buttons
00128    TGHorizontalFrame *hframe = new TGHorizontalFrame(this, 200, 40);
00129   
00130    TGTextButton *draw = new TGTextButton(hframe, "&Draw");
00131    draw->Connect("Clicked()", "MyMainFrame", this, "DoDraw()");
00132    hframe->AddFrame(draw, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4));
00133    TGTextButton *exit = new TGTextButton(hframe, "&Exit ");
00134    exit->Connect("Pressed()", "MyMainFrame", this, "DoExit()");
00135    hframe->AddFrame(exit, new TGLayoutHints(kLHintsCenterX, 5, 5, 3, 4));
00136 
00137    AddFrame(hframe, new TGLayoutHints(kLHintsCenterX, 2, 2, 2, 2));
00138 
00139    // Set a name to the main frame   
00140    SetWindowName("Embedded Canvas Status Info");
00141    MapSubwindows();
00142 
00143    // Initialize the layout algorithm via Resize()
00144    Resize(GetDefaultSize());
00145 
00146    // Map main frame
00147    MapWindow();
00148 }
00149 
00150 
00151 MyMainFrame::~MyMainFrame()
00152 {
00153    // Clean up main frame...
00154    Cleanup();
00155    delete fEcan;
00156 }
00157 
00158 
00159 void statusBar()
00160 {
00161    // Popup the GUI...
00162    new MyMainFrame(gClient->GetRoot(), 200, 200);
00163 }

Generated on Tue Jul 5 15:44:26 2011 for ROOT_528-00b_version by  doxygen 1.5.1