/****************************************************************************** * File: GUIbar.C * * Description: example macro * * This example creates a small application with three buttons:* * Show Dialog - invokes a dialog window for selecting an * * integer value in a number entry. The selected * * value is displayed in the label "Selected" * * Print Mass - prints last selected number in dialog window * * Exit - terminates the application * * Author: Ilka Antcheva (CERN/PH/SFT) * *Site Contact: roottalk@cern.ch * *ROOT Version: 5.09/01 * * Last Change: 10/02/2006 * ******************************************************************************/ #include #include #include #include #include #include #include #include #include Int_t gMass = 0; class MyDialog; class MyMainFrame { RQ_OBJECT("MyMainFrame") private: TGMainFrame *fMain; //main application frame TGCompositeFrame *fComp; //container farme TGTextButton *fDialog; //button invoking dialog TGTextButton *fPrint; //button for printing gMass value TGTextButton *fExit; //exit button public: MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h); virtual ~MyMainFrame(); // slots void CloseWindow(); void DoDialog(); void DoPrint(); }; class MyDialog { RQ_OBJECT("MyDialog") private: TGTransientFrame *fDMain; //dialog window TGCompositeFrame *fHor1; //container farme TGTextButton *fClose; //close button TGNumberEntry *fMassEntry; //text entry for user input TGLabel *fLabel; //label, used for feedback info public: MyDialog(const TGWindow *p, const TGWindow *main, UInt_t w, UInt_t h); virtual ~MyDialog(); // slots void CloseWindow(); void DoClose(); void DoSetMass(); }; void MyMainFrame::CloseWindow() { // Got close message for this MainFrame. Terminates the application. gApplication->Terminate(0); } void MyMainFrame::DoDialog() { new MyDialog(gClient->GetRoot(), fMain, 200, 200); Printf("Create MyDialog window..."); } void MyMainFrame::DoPrint() { printf("Selected Mass = %d\n",gMass); } MyMainFrame::MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h) { // Create a main frame fMain = new TGMainFrame(p, w, h); // Create a frame containing buttons fComp = new TGCompositeFrame(fMain,100,50,kFixedWidth); fDialog = new TGTextButton(fComp, " Show &Dialog "); fDialog->Connect("Clicked()", "MyMainFrame", this, "DoDialog()"); fDialog->SetToolTipText("Pops up a dialog for selecting Mass value"); fComp->AddFrame(fDialog,new TGLayoutHints(kLHintsExpandX,5,5,5,5)); fPrint = new TGTextButton(fComp, " Print &Mass"); fPrint->Connect("Clicked()", "MyMainFrame", this, "DoPrint()"); fPrint->SetToolTipText("Prints selected Mass value"); fComp->AddFrame(fPrint,new TGLayoutHints(kLHintsExpandX,5,5,5,5)); fExit = new TGTextButton(fComp, " &Exit "); fExit->SetCommand("gApplication->Terminate()"); fExit->SetToolTipText("Terminates the application"); fComp->AddFrame(fExit, new TGLayoutHints(kLHintsExpandX,5,5,5,5)); fMain->AddFrame(fComp); // Set a name to the main frame fMain->SetWindowName("Application Window"); fMain->MapSubwindows(); //initialize the layout algorithm via Resize() fMain->Resize(fMain->GetDefaultSize()); // Map main frame fMain->MapWindow(); } MyMainFrame::~MyMainFrame() { // Clean up in direction children --> parents fComp->Cleanup(); fMain->Cleanup(); delete fMain; } MyDialog::MyDialog(const TGWindow *p, const TGWindow *main, UInt_t w, UInt_t h) { // 1st: to create an window with respect to its parent (main) window fDMain = new TGTransientFrame(p, main, w, h); fDMain->Connect("CloseWindow()", "MyDialog", this, "CloseWindow()"); fHor1 = new TGHorizontalFrame(fDMain, 50, 20, kFixedWidth); fClose = new TGTextButton(fHor1, "Close", 1); fClose->Connect("Clicked()", "MyDialog", this, "DoClose()"); fHor1->Resize(100, fClose->GetDefaultHeight()); fHor1->AddFrame(fClose, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 4, 4, 4, 4)); fDMain->AddFrame(fHor1,new TGLayoutHints(kLHintsBottom | kLHintsRight, 2, 2, 5, 1)); // 2nd: create widgets in the dialog fMassEntry = new TGNumberEntry(fDMain, 0, 5, 999, TGNumberFormat::kNESInteger); fMassEntry->GetNumberEntry()->SetToolTipText("Enter the mass you would like to study"); fDMain->AddFrame(fMassEntry, new TGLayoutHints(kLHintsLeft | kLHintsCenterY, 10, 10, 10, 10)); fMassEntry->GetNumberEntry()->Connect("ReturnPressed()", "MyDialog", this, "DoSetMass()"); fMassEntry->Connect("ValueSet(Long_t)", "MyDialog", this, "DoSetMass()"); fMassEntry->SetIntNumber((Long_t)gMass); fLabel = new TGLabel(fDMain, ""); if (gMass) fLabel = new TGLabel(fDMain, Form("Selected: %d", gMass)); else fLabel = new TGLabel(fDMain, "No value set."); fDMain->AddFrame(fLabel, new TGLayoutHints(kLHintsTop | kLHintsLeft, 15, 15, 15, 15)); fDMain->MapSubwindows(); fDMain->Resize(fDMain->GetDefaultSize()); // position of the dialog relative to the parent's window Window_t wdum; int ax, ay; gVirtualX->TranslateCoordinates(main->GetId(), fDMain->GetParent()->GetId(), (Int_t)(((TGFrame *) main)->GetWidth() - fDMain->GetWidth()) >> 1, (Int_t)(((TGFrame *) main)->GetHeight() - fDMain->GetHeight()) >> 1, ax, ay, wdum); fDMain->Move(ax, ay); fDMain->SetWindowName("Select Mass"); fDMain->MapWindow(); } MyDialog::~MyDialog() { fMassEntry->Disconnect("ValueSet(Long_t)"); fMassEntry->GetNumberEntry()->Disconnect("ReturnPressed()"); fClose->Disconnect("Clicked()"); fDMain->Disconnect("CloseWindow()"); delete fMassEntry; delete fLabel; delete fClose; delete fHor1; delete fDMain; } void MyDialog::CloseWindow() { // Called when the window is closed via the window manager. delete this; } void MyDialog::DoSetMass() { gMass = (Int_t)fMassEntry->GetNumber(); fLabel->SetText(Form("Seleced: %d",gMass)); gClient->NeedRedraw(fDMain); } void MyDialog::DoClose() { printf("\nThe Close button is pressed\n"); fDMain->SendCloseMessage(); } void GUIbar() { new MyMainFrame(gClient->GetRoot(), 200, 200); } // --------------- main program --------------------- int main(int argc, char **argv) { TApplication theApp("App", &argc, argv); GUIbar(); theApp.Run(); return 0; }