#include "ViewerMain.h" //ClassImp(ViewerMain) OptionWindow::OptionWindow(const TGWindow* p,const TGWindow* main,const char* title, const char* msg,TString* option):TGTransientFrame(p,main,300,100,kVerticalFrame){ fOptionEntry = new TGTextEntry(this, fOptionBuffer = new TGTextBuffer(100)); fLayout = new TGLayoutHints(kLHintsNormal|kLHintsCenterX,2,2,5,5); fLabel= new TGLabel(this,msg); fOkButton = new TGTextButton(this,"&Ok",1); fOkButton->Associate(this); SetWindowName(title); fOption = option; AddFrame(fLabel ,fLayout); AddFrame(fOptionEntry,fLayout); AddFrame(fOkButton,fLayout); fOptionEntry->Resize(150,fOptionEntry->GetDefaultHeight()); MapSubwindows(); Resize(GetDefaultSize()); Window_t wdum; int ax, ay; gGXW->TranslateCoordinates(main->GetId(), GetParent()->GetId(), (((TGFrame *) main)->GetWidth() - fWidth) >> 1, (((TGFrame *) main)->GetHeight() - fHeight) >> 1, ax, ay, wdum); Move(ax, ay); MapWindow(); fClient->WaitFor(this); } OptionWindow::~OptionWindow(){ delete fOptionBuffer; delete fOptionEntry; delete fOkButton; delete fLabel; delete fLayout; } optionWindow::CloseWindow(){ delete this; } Bool_t OptionWindow::ProcessMessage(Long_t msg,Long_t parm1,Long_t){ // Process message dialog box event. switch (GET_MSG(msg)) { case kC_COMMAND: switch (GET_SUBMSG(msg)) { case kCM_BUTTON: switch (parm1){ case 1: *fOption = fOptionBuffer->GetString(); CloseWindow(); break; default: break; } break; default: break; } break; default: break; } return kTRUE; } ViewerMain::ViewerMain(const TGWindow *p, UInt_t w, UInt_t h): TGMainFrame(p, w, h) { //Constructor opens window with no available histograms. fDrawnObjects = new TObjArray(MAX_VIEWABLE); fObjectList = new TObjArray(MAX_HISTS); fNoHists=0; SetUpWindow(); MapSubwindows(); Resize(GetDefaultSize()); MapWindow(); } ViewerMain::ViewerMain(const TGWindow *p, UInt_t w, UInt_t h,TCollection* collection): TGMainFrame(p, w, h) { //Constructor Opens the main window with the contents of collection available for drawing TIter next(collection); OptionObject* optObj; TObject* obj; fDrawnObjects = new TObjArray(MAX_VIEWABLE); fObjectList = new TObjArray(MAX_HISTS); while((obj=next())){ optObj=new OptionObject(obj,""); fObjectList->AddLast(optObj); } fNoHists=0; fMapFile=kFALSE; SetUpWindow(); MapSubwindows(); Resize(GetDefaultSize()); MapWindow(); } void ViewerMain::SetUpWindow(){ //Sets up the various GUI objects including the Drawing Canvas // Create Layouthints for menu bar and menubar Items fMenuBarLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 0, 0, 1, 1); fMenuBarItemLayout = new TGLayoutHints(kLHintsTop | kLHintsLeft, 0, 4, 0, 0); fMenuBarHelpLayout = new TGLayoutHints(kLHintsTop | kLHintsRight); fButtonLayout = new TGLayoutHints(kLHintsTop|kLHintsExpandX,2,2,2,2); TGLayoutHints* fLayout1 = new TGLayoutHints(kLHintsTop|kLHintsLeft|kLHintsExpandX|kLHintsExpandY,0,0,0,0); TGLayoutHints* fLayout2 = new TGLayoutHints(kLHintsTop|kLHintsLeft|kLHintsExpandY,2,2,2,2); Printf("MenuLayoutsCreated\n"); // Create File Menu fFileMenu = new TGPopupMenu(fClient->GetRoot()); fFileMenu->AddEntry("&Open...", M_FILE_OPEN); fFileMenu->AddEntry("Open &MapFile...", M_MAP_OPEN); fFileMenu->AddEntry("&Save", M_FILE_SAVE); fFileMenu->AddEntry("S&ave as...", M_FILE_SAVEAS); fFileMenu->AddEntry("&Close", -1); fFileMenu->AddSeparator(); fFileMenu->AddEntry("&Print", -1); fFileMenu->AddEntry("P&rint setup...", -1); fFileMenu->AddSeparator(); fFileMenu->AddEntry("E&xit", M_FILE_EXIT); fFileMenu->DisableEntry(M_FILE_SAVEAS); //Create Change Grid Cascade Menu //Create Options Menu fOptionsMenu = new TGPopupMenu(fClient->GetRoot()); fOptionsMenu->AddEntry("File &Viewer",M_FILE_VIEWER); //Create Help Menu fHelpMenu = new TGPopupMenu(fClient->GetRoot()); fHelpMenu->AddEntry("&Contents", M_HELP_CONTENTS); fHelpMenu->AddEntry("&Search...", M_HELP_SEARCH); fHelpMenu->AddSeparator(); fHelpMenu->AddEntry("&About", M_HELP_ABOUT); Printf("Menus Created\n"); //Associate all menu messages with ViewerMain fFileMenu->Associate(this); fOptionsMenu->Associate(this); fHelpMenu->Associate(this); //Add menus to menu bar and menu bar to ViewerMain fMenuBar = new TGMenuBar(this, 1, 1, kHorizontalFrame); fMenuBar->AddPopup("&File", fFileMenu, fMenuBarItemLayout); fMenuBar->AddPopup("&Options", fOptionsMenu, fMenuBarItemLayout); fMenuBar->AddPopup("&Help", fHelpMenu, fMenuBarHelpLayout); //Main Window Areas fMainFrame = new TGCompositeFrame(this,10,10,kHorizontalFrame); fButtonFrame = new TGCompositeFrame(fMainFrame,100,100,kVerticalFrame|kFixedWidth); fCanvas=new MultiCanvas(fMainFrame,CANVAS_WIDTH,CANVAS_HEIGHT); //Add Buttons fClearButton = new TGTextButton(fButtonFrame,"&Clear",M_CLEAR_BUTTON); fSelectButton = new TGTextButton(fButtonFrame,"&Select..",M_SELECT_BUTTON); fOptionsButton = new TGTextButton(fButtonFrame,"&Options..",M_OPTIONS_BUTTON); fUpdateButton = new TGTextButton(fButtonFrame,"&Update",M_UPDATE_BUTTON); fClearButton->Associate(this); fSelectButton->Associate(this); fOptionsButton->Associate(this); fUpdateButton->Associate(this); fButtonFrame->AddFrame(fClearButton,fButtonLayout); fButtonFrame->AddFrame(fSelectButton,fButtonLayout); fButtonFrame->AddFrame(fOptionsButton,fButtonLayout); fButtonFrame->AddFrame(fUpdateButton,fButtonLayout); //Layout Frames AddFrame(fMenuBar, fMenuBarLayout); fMainFrame->AddFrame(fCanvas,fLayout1); fMainFrame->AddFrame(fButtonFrame,fLayout2); AddFrame(fMainFrame,fLayout1); SetWindowName("Histogram Viewer"); } ViewerMain::~ViewerMain() { // Delete all created widgets delete fCanvas; delete fMenuBarLayout; delete fMenuBarItemLayout; delete fMenuBarHelpLayout; delete fButtonLayout; delete fClearButton; delete fSelectButton; delete fOptionsButton; delete fUpdateButton; delete fFileMenu; delete fOptionsMenu; delete fHelpMenu; delete fMainFrame; delete fButtonFrame; fDrawnObjects->Delete(); fObjectList->Delete(); delete fDrawnObjects; delete fObjectList; } void ViewerMain::DrawObjects(){ //Draws all the currently selected Objects fCanvas->Reset(); fNoHists = (fDrawnObjects->GetLast())+1; for(int i=0;iAdd(fDrawnObjects->At(i)); fCanvas->ArrangeCanvases(); } void ViewerMain::CloseWindow() { // Calls parent CloseWindow() // (which destroys the window) and terminates the application. TGMainFrame::CloseWindow(); gApplication->Terminate(0); } void ViewerMain::OpenFile(){ //Opens a TMapFile and makes the contents available for drawing const char* name; TObject* obj; fDrawnObjects->Delete(); fObjectList->Delete(); fNoHists=0; if(fMapFileOpen) fMapFile->Close(); TString* filename = new TString(); new OptionWindow(fClient->GetRoot(),this,"Open Map File","Enter Name of File",filename); fMapFile = TMapFile::Create(filename->Data()); printf("opened file"); fMapFileOpen = kTRUE; TMapRec* mRec = fMapFile->GetFirst(); while(mRec && fMapFile->OrgAddress(mRec)){ name=mRec->GetName(); obj=fMapFile->Get(name); fObjectList->Add(new OptionObject(obj,"")); mRec = mRec->GetNext(); } fCanvas->Reset(); } void ViewerMain::UpdateMappedObjects(){ //Updates any objects which came from a TMapFile TIter next(fObjectList); TIter next2(fDrawnObjects); OptionObject* obj; TObject* object; while((obj=(OptionObject*)next())){ object = obj->GetObject(); object = fMapFile->Get(obj->GetName(),object); } while((obj=(OptionObject*)next2())){ object = obj->GetObject(); object = fMapFile->Get(obj->GetName(),object); } DrawObjects(); } //TString* filename; /* TGFileInfo fi; fi.fFileTypes = (char **)filetypes; new TGFileDialog(fClient->GetRoot(), this, kFDOpen,&fi); return fi.fFilename; */ void ViewerMain::ClearDisplay(){ //Removes all objects from the DrawnObjects list and clears the drawing canvas TObject* obj; TIter next(fDrawnObjects); while((obj=next())){ fObjectList->AddLast(obj); } fDrawnObjects->Clear(); fNoHists=0; fCanvas->Reset(); fCanvas->ArrangeCanvases(); } Bool_t ViewerMain::ProcessMessage(Long_t msg, Long_t parm1, Long_t) { // Handle messages send to the ViewerMain object. E.g. all menu button // messages. Int_t padNo; OptionObject* obj; switch (GET_MSG(msg)) { case kC_COMMAND: switch (GET_SUBMSG(msg)) { case kCM_BUTTON: switch(parm1) { case M_CLEAR_BUTTON: ClearDisplay(); break; case M_SELECT_BUTTON: new ListManager(fClient->GetRoot(),this,fObjectList,fDrawnObjects,new TGString("Available"),new TGString("Selected")); DrawObjects(); break; case M_OPTIONS_BUTTON: //Open Dialogue for selecting Histogram Draw options padNo = fCanvas->SelectedPadNo(); //Gets the number of currently selected pad from canvas if(padNo==0){ //Special case due to pad numbering system obj = (OptionObject*)fDrawnObjects->At(padNo); printf("Gat object"); HistogramDialogue* x = new HistogramDialogue(fClient->GetRoot(),this,(TH1*)(obj->GetObject()),obj->GetOption()); delete x; printf("Got draw option"); } else { obj = (OptionObject*)fDrawnObjects->At(padNo-1); printf("Gat object"); new HistogramDialogue(fClient->GetRoot(),this,(TH1*)(obj->GetObject()),obj->GetOption()); printf("Got draw option"); } DrawObjects(); //fCanvas->ArrangeCanvases(); /* printf("option %s", obj->GetOption()->Data()); fCanvas->AddToPad(obj,padNo); printf("Added to canvas"); fCanvas->UpdateCanvases(); printf("Updated Pad"); break; **/ break; case M_UPDATE_BUTTON: UpdateMappedObjects(); break; default: break; } break; case kCM_MENU: switch (parm1) { case M_FILE_OPEN: case M_MAP_OPEN: OpenFile(); break; case M_FILE_VIEWER: new ListManager(fClient->GetRoot(),this,fObjectList,fDrawnObjects,new TGString("Available"),new TGString("Selected")); DrawObjects(); break; case M_FILE_EXIT: CloseWindow(); // this also terminates theApp break; default: break; } default: break; } default: break; } return kTRUE; } //---- Main program ------------------------------------------------------------ extern void InitGui(); VoidFuncPtr_t initfuncs[] = { InitGui, 0 }; TROOT root("GUI", "GUI test environement", initfuncs); int main(int argc, char **argv) { TApplication theApp("App", &argc, argv); printf("Application created"); float px,py; TH1* hist; TObjArray *HistList = new TObjArray(67); for(int i = 0;i<65;i++){ char tmp[20]; sprintf(tmp,"Histogram %i",i); HistList->AddLast( new TH1F(tmp,tmp,100,-(4+i),4+i)); hist =(TH1*)HistList->Last(); for(int j=0;j<2000;j++){ gRandom->Rannor(px,py); hist->Fill(px); hist->SetFillColor(kRed+i); } } HistList->AddLast(new TH2F("2D Histogram","2D Histogram",100,-5,5,100,-5,5)); HistList->AddLast(new TH2F("2D Histogram2","2D Histogram2",50,0,5,30,0,5)); TH2F* hist2 = (TH2F*)(HistList->Last()); TH2F* hist3 = (TH2F*)(HistList->Last()); for(int j=0;j<2000;j++){ gRandom->Rannor(px,py); hist2->Fill(px,py); hist3->Fill(py,px); } hist2->SetFillColor(kRed); hist3->SetFillColor(kYellow); HistList->AddLast(new TPaveLabel(0.2,0.4,0.8,0.6,"Hello World")); ViewerMain mainWindow(gClient->GetRoot(),800,800, HistList); theApp.Run(); return 0; }