guiWithCINT.C

Go to the documentation of this file.
00001 // A simple example of entering  CINT commands and having the CINT output in a 
00002 // ROOT GUI application window. 
00003 // An editable combo box is used as a CINT prompt, a text view widget displays 
00004 // the comand output.
00005 // Author: Ilka Antcheva   06/07/2007
00006 
00007 #include <iostream>
00008 #include <TApplication.h>
00009 #include <TRint.h>
00010 #include <TROOT.h>
00011 #include <TSystem.h>
00012 #include <TGTextEntry.h>
00013 #include <TGTextView.h>
00014 #include <TGClient.h>
00015 #include <TGButton.h>
00016 #include <TGFrame.h>
00017 #include <TGLayout.h>
00018 #include <TGWindow.h>
00019 #include <TGLabel.h>
00020 #include <TString.h>
00021 #include <TGComboBox.h>
00022 #include <Getline.h>
00023 
00024 class IDList {
00025 
00026 private:
00027    Int_t fID;  //create widget Id(s)
00028 
00029 public:
00030    IDList() : fID(0) {}
00031    ~IDList() {}
00032    Int_t GetUnID(void) { return ++fID; }
00033 };
00034 
00035 class MyApplication : public TGMainFrame {
00036 
00037 private:
00038    TGTextButton        *fExit;
00039    IDList               fIDs;
00040    TGComboBox          *fComboCmd;   // CINT command combobox
00041    TGTextBuffer        *fCommandBuf; // text buffer in use 
00042    TGTextEntry         *fCommand;    // text entry for CINT commands
00043    TGTextView          *fTextView;   // display CINT output
00044    TString              fName;       // name of temp created file
00045 public:
00046    MyApplication(const TGWindow *p, UInt_t w, UInt_t h);
00047    virtual ~MyApplication();
00048 
00049    void DoExit();
00050    void DoEnteredCommand();
00051 
00052    ClassDef(MyApplication, 0)
00053 };
00054                           
00055 MyApplication::MyApplication(const TGWindow *p, UInt_t w, UInt_t h) 
00056    : TGMainFrame(p, w, h)   
00057 {
00058    SetCleanup(kDeepCleanup);
00059    
00060    Connect("CloseWindow()", "MyApplication", this, "DoExit()");
00061    DontCallClose();
00062 
00063    TGHorizontalFrame *fHL2 = new TGHorizontalFrame(this, 70, 100);
00064    AddFrame(fHL2, new TGLayoutHints(kLHintsNormal, 5, 5, 5, 5));
00065    TGLabel *fInlabel = new TGLabel(fHL2, "CINT Prompt:");
00066    fHL2->AddFrame(fInlabel, new TGLayoutHints(kLHintsCenterY));
00067 
00068    TGLabel *fOutlabel = new TGLabel(this, "Output Window:");
00069    AddFrame(fOutlabel);
00070 
00071    fCommandBuf = new TGTextBuffer(256);
00072    fComboCmd = new TGComboBox(fHL2, "", fIDs.GetUnID());
00073    fCommand = fComboCmd->GetTextEntry();
00074    fComboCmd->Resize(450, fCommand->GetDefaultHeight());
00075    fHL2->AddFrame(fComboCmd, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 20,0,0,0));
00076 
00077    TString hist(Form("%s/.root_hist", gSystem->UnixPathName(gSystem->HomeDirectory())));
00078    FILE *fhist = fopen(hist.Data(), "rt");
00079    if (fhist) {
00080       char histline[256];
00081       while (fgets(histline, 256, fhist)) {
00082          histline[strlen(histline)-1] = 0; // remove trailing "\n"
00083          fComboCmd->InsertEntry(histline, 0, -1);
00084       }
00085       fclose(fhist);
00086    }
00087 
00088    Pixel_t backpxl;
00089    gClient->GetColorByName("#c0c0c0", backpxl);
00090    fTextView = new TGTextView(this, 500, 94, fIDs.GetUnID(), kFixedWidth | kFixedHeight);
00091    fTextView->SetBackground(backpxl);
00092    AddFrame(fTextView, new TGLayoutHints(kLHintsExpandX));
00093    TGHorizontalFrame *fHL3 = new TGHorizontalFrame(this, 70, 150, kFixedWidth);
00094    fExit = new TGTextButton(fHL3, "&Exit", fIDs.GetUnID());
00095    fExit->Connect("Clicked()", "MyApplication", this, "DoExit()");
00096    fHL3->AddFrame(fExit, new TGLayoutHints(kLHintsExpandX));
00097    AddFrame(fHL3, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 1, 1, 1, 1));
00098 
00099    SetWindowName("GUI with CINT Input/Output");
00100    MapSubwindows();
00101    Resize(GetDefaultSize());
00102    MapWindow();
00103    fCommand->Connect("ReturnPressed()", "MyApplication", this, "DoEnteredCommand()");
00104    fName = Form("%soutput.log", gSystem->WorkingDirectory());
00105 };
00106 
00107 MyApplication::~MyApplication()
00108 {
00109    // Destructor.
00110    
00111    Cleanup();
00112 }
00113 
00114 void MyApplication::DoExit()
00115 {
00116    // Close application window.
00117    
00118    gSystem->Unlink(fName.Data());   
00119    gApplication->Terminate();
00120 }
00121 
00122 void MyApplication::DoEnteredCommand()
00123 {
00124    // Execute the CINT command after the ENTER key was pressed.
00125 
00126    const char *command = fCommand->GetTitle();
00127    TString prompt;
00128    
00129    if (strlen(command)) {
00130       // form temporary file path
00131       prompt = ((TRint*)gROOT->GetApplication())->GetPrompt();
00132       FILE *cintout = fopen(fName.Data(), "a+t");
00133       if (cintout) {
00134          fputs(Form("%s%s\n",prompt.Data(), command), cintout);
00135          fclose(cintout);
00136       }
00137       gSystem->RedirectOutput(fName.Data(), "a");
00138       gROOT->ProcessLine(command);
00139       fComboCmd->InsertEntry(command, 0, fIDs.GetUnID());
00140       Gl_histadd((char *)command);
00141       gSystem->RedirectOutput(0);
00142       fTextView->LoadFile(fName.Data());
00143       if (fTextView->ReturnLineCount() > 10)
00144          fTextView->SetVsbPosition(fTextView->ReturnLineCount());
00145       fCommand->Clear();
00146    } else {
00147       printf("No command entered\n");
00148    }
00149    fTextView->ShowBottom();
00150 }
00151 
00152 void guiWithCINT() 
00153 {
00154    new MyApplication(gClient->GetRoot(),600,300);
00155 }
00156 

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