#include #include #include #include #include #include #include class MultiEmbeddedCanvas { RQ_OBJECT("MultiEmbeddedCanvas") private: TGMainFrame *fMain; UInt_t fNumberOfCanvases; TGVerticalFrame *fVer; TGComboBox *fCombo; TFile* file; TRootEmbeddedCanvas *fEcan; TCanvas* fCurrentCanvas; //the current canvas char currentpd[20]; public: MultiEmbeddedCanvas(const TGWindow *p, UInt_t w, UInt_t h); virtual ~MultiEmbeddedCanvas(); // slots void DoSelection(); void AddCanvas(); void Exit(); //methods void AddCanvasGraph(); }; void MultiEmbeddedCanvas::Exit() { Printf("Close file"); file->Close(); fMain->UnmapWindow(); } void MultiEmbeddedCanvas::AddCanvasGraph() { Double_t x[10], y[10]; for(int i=0; i< 10; i++) x[i]=y[i]=i; TGraph *graph = new TGraph(10,x,y); graph->SetTitle(currentpd); graph->GetXaxis()->SetTitle("energy"); graph->GetYaxis()->SetTitle("count"); graph->Draw("AL"); } void MultiEmbeddedCanvas::DoSelection(Int_t id) { TCanvas* oldCanvas = fCurrentCanvas; // Select a canvas from the list if(oldCanvas) { Printf("write current canvas into file %s", oldCanvas->GetTitle()); oldCanvas->Write(); delete oldCanvas; } Printf("Select a canvas from the list..."); TGTextLBEntry* ent = fCombo->GetSelectedEntry(); char* padname = ent->GetText()->GetString(); Printf("combo entry %s", padname); //Get a canvas from the file ----------------------- fCurrentCanvas = file->Get(padname); fEcan->AdoptCanvas(fCurrentCanvas); fCurrentCanvas->Draw(); fCurrentCanvas->cd(); fCurrentCanvas->Update(); fCurrentCanvas->Draw(); } void MultiEmbeddedCanvas::AddCanvas() { TCanvas* oldCanvas = fCurrentCanvas; sprintf(currentpd, "Pad %i", fNumberOfCanvases); Printf("Add a canvas %s", currentpd); if(oldCanvas) { Printf("write current canvas into file %s", oldCanvas->GetTitle()); oldCanvas->Write(); delete oldCanvas; } //Create a new canvas Int_t wid = fEcan->GetCanvasWindowId(); fCurrentCanvas = new TCanvas(currentpd, 10, 10, wid); fEcan->AdoptCanvas(fCurrentCanvas); fCurrentCanvas->Draw(); fCurrentCanvas->cd(); AddCanvasGraph(); //add the canvas in the combo box fCombo->AddEntry(currentpd, fNumberOfCanvases); fNumberOfCanvases++; fCurrentCanvas->Update(); fCurrentCanvas->Draw(); } MultiEmbeddedCanvas::MultiEmbeddedCanvas(const TGWindow *p, UInt_t w, UInt_t h) { // Create a main frame fMain = new TGMainFrame(p, w, h); // Create a vertical frame containing the canvases fVer = new TGVerticalFrame(fMain, 10, 5, kSunkenFrame); fMain->AddFrame(fVer, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX | kLHintsExpandY, 5, 5, 5, 5)); // Create an embedded canvas and add it to the main frame fEcan = new TRootEmbeddedCanvas ("MAIN", fVer, 700, 700); fVer->AddFrame (fEcan, new TGLayoutHints (kLHintsExpandX | kLHintsExpandY, 10, 10, 10, 10)); fEcan->GetCanvas()->Range(0,0,1,1); fEcan->GetCanvas()->SetBorderSize(2); fEcan->GetCanvas()->SetFrameFillColor(10); fCurrentCanvas = fEcan->GetCanvas(); fCurrentCanvas->cd(); // Create a horizontal frame containing a combo box and two text buttons TGHorizontalFrame *fHor = new TGHorizontalFrame(fMain, 10, 5); fNumberOfCanvases = 0; //cambo box with the list of canvases created fCombo = new TGComboBox(fHor, 98); fCombo->Connect("Selected(Int_t)", "MultiEmbeddedCanvas", this, "DoSelection(Int_t)"); fCombo->Resize(120, 20); fHor->AddFrame(fCombo, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5)); fAdd = new TGTextButton(fHor, " &Add "); fAdd->SetToolTipText("Add a TRootEmbeddedCanvas"); fHor->AddFrame(fAdd, new TGLayoutHints(kLHintsTop | kLHintsLeft, 200, 5, 5, 5)); fAdd->Connect("Clicked()", "MultiEmbeddedCanvas", this, "AddCanvas()"); fExit = new TGTextButton(fHor, " &Exit "); //fExit->SetCommand("gApplication->Terminate()"); fExit->SetToolTipText("Click on the button to terminate the application"); fHor->AddFrame(fExit, new TGLayoutHints(kLHintsTop | kLHintsRight, 200, 5, 5, 5)); fExit->Connect("Clicked()", "MultiEmbeddedCanvas", this, "Exit()"); fMain->AddFrame(fHor, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 5, 5, 5, 5)); fCurrentCanvas = 0; // Set a name to the main frame fMain->SetWMPosition(0,0); fMain->SetWindowName("Test of TRootEmbeddedCanvas creation"); fMain->MapSubwindows(); fMain->Resize(fMain->GetDefaultSize()); fMain->MapWindow(); //open the root file Printf("Open the file"); file = new TFile("./root1.root", "UPDATE"); } MultiEmbeddedCanvas::~MultiEmbeddedCanvas() { // Clean up in direction children --> parents fHor->Cleanup(); fVer->Cleanup(); fMain->Cleanup(); delete fMain; } void multicanvas_file() { a = new MultiEmbeddedCanvas(gClient->GetRoot(), 1024, 768); }