/****************************************************************************** * File: dialogExample.C * * Description: macro file to test main application and dialog windows * * This macro gives an example of how to create an application * * window and how to show up a dialog window if a button is * * clicked * * Author: Ilka Antcheva (CERN/PH/SFT) * *Site Contact: roottalk@cern.ch * *ROOT Version: 4.03/01 * * Last Change: 08/02/2005 * ******************************************************************************/ #include #include #include #include #include #include #include #include #include class MyMainFrame { RQ_OBJECT("MyMainFrame") private: TGMainFrame *fMain; TGCompositeFrame *fComp; TGTextButton *fDialog, *fSave, *fExit; public: MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h); virtual ~MyMainFrame(); // slots void CloseWindow(); void DoDialog(); }; class MyDialog { RQ_OBJECT("MyDialog") private: TGTransientFrame *fMain; TGCompositeFrame *fHor1; TGButton *fOk; TGGroupFrame *fGframe; TGTextEntry *fText; TGLabel *fLabel; public: MyDialog(const TGWindow *p, const TGWindow *main, UInt_t w, UInt_t h, UInt_t options = kVerticalFrame); virtual ~MyDialog(); // slots void CloseWindow(); void DoOK(); void DoSetlabel(); }; void MyMainFrame::CloseWindow() { // Got close message for this MainFrame. Terminates the application. gApplication->Terminate(0); } void MyMainFrame::DoDialog() { new MyDialog(gClient->GetRoot(), fMain, 400, 200); Printf("Create MyDialog window..."); } 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 a button fComp = new TGCompositeFrame(fMain,60,30,kHorizontalFrame | kSunkenFrame); fDialog = new TGTextButton(fComp, " Show &Dialog "); fDialog->Connect("Clicked()", "MyMainFrame", this, "DoDialog()"); fDialog->SetToolTipText("This is a button widget that pops up a dialog"); fComp->AddFrame(fDialog,new TGLayoutHints(kLHintsTop | kLHintsLeft,5,5,5,5)); fExit = new TGTextButton(fComp, " &Exit "); fExit->SetCommand("gApplication->Terminate()"); fComp->AddFrame(fExit, new TGLayoutHints(kLHintsTop | kLHintsRight,5,10,5,5)); fMain->AddFrame(fComp,new TGLayoutHints(kLHintsBottom | kLHintsExpandX,0,0,1,0)); // Set a name to the main frame fMain->SetWindowName("Application Window"); fMain->MapSubwindows(); //initialize the layout algorithm via Resize() fMain->Resize(fMain->GetDefaultSize()); // Give min and max window size + a step of x,y incrementing between the given sizes fMain->SetWMSizeHints(350, 350, 700, 700, 0, 0); // Map main frame fMain->MapWindow(); } MyMainFrame::~MyMainFrame() { // Clean up in direction children --> parents delete fExit; delete fSave; delete fDialog; delete fComp; delete fMain; } MyDialog::MyDialog(const TGWindow *p, const TGWindow *main, UInt_t w, UInt_t h, UInt_t options) { // 1st: to create an window with respect to its parent (main) window fMain = new TGTransientFrame(p, main, w, h, options); fMain->Connect("CloseWindow()", "MyDialog", this, "CloseWindow()"); fHor1 = new TGHorizontalFrame(fMain, 80, 20, kFixedWidth); fOk = new TGTextButton(fHor1, " &Ok ", 1); fOk->Connect("Clicked()", "MyDialog", this, "DoOK()"); fHor1->AddFrame(fOk, new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX, 4, 4, 4, 4)); fHor1->Resize(150, fOk->GetDefaultHeight()); fMain->AddFrame(fHor1,new TGLayoutHints(kLHintsBottom | kLHintsRight, 2, 2, 5, 1)); // 2nd: create widgets in the dialog fText = new TGTextEntry(fMain, new TGTextBuffer(100)); fText->SetToolTipText("Enter the label and hit Enter key"); fText->Connect("ReturnPressed()", "MyDialog", this, "DoSetlabel()"); fMain->AddFrame(fText, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5)); fGframe = new TGGroupFrame(fMain, "Last Input"); fLabel = new TGLabel(fGframe, "No Intut "); fGframe->AddFrame(fLabel, new TGLayoutHints(kLHintsTop | kLHintsLeft, 5, 5, 5, 5)); fMain->AddFrame(fGframe, new TGLayoutHints(kLHintsExpandX, 2, 2, 1, 1)); fText->Resize(150, fText->GetDefaultHeight()); fMain->MapSubwindows(); fMain->Resize(fMain->GetDefaultSize()); // position of the dialog relative to the parent's window Window_t wdum; int ax, ay; gVirtualX->TranslateCoordinates(main->GetId(), fMain->GetParent()->GetId(), (Int_t)(((TGFrame *) main)->GetWidth() - fMain->GetWidth()) >> 1, (Int_t)(((TGFrame *) main)->GetHeight() - fMain->GetHeight()) >> 1, ax, ay, wdum); fMain->Move(ax, ay); fMain->SetWindowName("My Dialog"); fMain->MapWindow(); } MyDialog::~MyDialog() { delete fText; delete fLabel; delete fOk; delete fHor1; delete fGframe; delete fMain; } void MyDialog::CloseWindow() { // Called when the window is closed via the window manager. delete this; } void MyDialog::DoSetlabel() { printf("\nThe Enter key is pressed\n"); fLabel->SetText(fText->GetBuffer()->GetString()); fGframe->Layout(); } void MyDialog::DoOK() { TQObject::Disconnect(fText, "ReturnPressed()", this, "DoSetlabel()"); printf("\nThe OK button is pressed\n"); fMain->SendCloseMessage(); } void dialogExample() { new MyMainFrame(gClient->GetRoot(), 300, 300); } // --------------- main program --------------------- int main(int argc, char **argv) { TApplication theApp("App", &argc, argv); dialogExample(); theApp.Run(); return 0; }