TGCommandPlugin.cxx

Go to the documentation of this file.
00001 // @(#)root/gui:$Id: TGCommandPlugin.cxx 27475 2009-02-18 10:04:21Z bellenot $
00002 // Author: Bertrand Bellenot   26/09/2007
00003 
00004 #include "TROOT.h"
00005 #include "TSystem.h"
00006 #include "TRint.h"
00007 #include "TApplication.h"
00008 #include "TGClient.h"
00009 #include "TGLabel.h"
00010 #include "TGFrame.h"
00011 #include "TGLayout.h"
00012 #include "TGComboBox.h"
00013 #include "TGTextView.h"
00014 #include "TGTextEntry.h"
00015 #include "TGTextEdit.h"
00016 #include "TInterpreter.h"
00017 #include "Getline.h"
00018 
00019 #include "TGCommandPlugin.h"
00020 
00021 //_____________________________________________________________________________
00022 //
00023 // TGCommandPlugin
00024 //
00025 // Class used to redirect command line input/output.
00026 //_____________________________________________________________________________
00027 
00028 ClassImp(TGCommandPlugin)
00029 
00030 //______________________________________________________________________________
00031 TGCommandPlugin::TGCommandPlugin(const TGWindow *p, UInt_t w, UInt_t h) :
00032       TGMainFrame(p, w, h)
00033 {
00034    // TGCommandPlugin Constructor.
00035 
00036    SetCleanup(kDeepCleanup);
00037    fHf = new TGHorizontalFrame(this, 100, 20);
00038    fComboCmd   = new TGComboBox(fHf, "", 1);
00039    fCommand    = fComboCmd->GetTextEntry();
00040    fCommandBuf = fCommand->GetBuffer();
00041    fComboCmd->Resize(200, fCommand->GetDefaultHeight());
00042    fHf->AddFrame(fComboCmd, new TGLayoutHints(kLHintsCenterY |
00043                  kLHintsRight | kLHintsExpandX, 5, 5, 1, 1));
00044    fHf->AddFrame(fLabel = new TGLabel(fHf, "Command (local):"),
00045                  new TGLayoutHints(kLHintsCenterY | kLHintsRight, 
00046                  5, 5, 1, 1));
00047    AddFrame(fHf, new TGLayoutHints(kLHintsLeft | kLHintsTop | 
00048             kLHintsExpandX, 3, 3, 3, 3));
00049    fCommand->Connect("ReturnPressed()", "TGCommandPlugin", this,
00050                      "HandleCommand()");
00051 
00052    Pixel_t pxl;
00053    gClient->GetColorByName("#ccccff", pxl);
00054    fStatus = new TGTextView(this, 10, 100, 1);
00055    fStatus->SetSelectBack(pxl);
00056    fStatus->SetSelectFore(TGFrame::GetBlackPixel());
00057    AddFrame(fStatus, new TGLayoutHints(kLHintsLeft | kLHintsTop | 
00058             kLHintsExpandX | kLHintsExpandY, 3, 3, 3, 3));
00059    fPid = gSystem->GetPid();
00060    TString defhist(Form("%s/.root_hist", gSystem->UnixPathName(
00061                         gSystem->HomeDirectory())));
00062    FILE *lunin = fopen(defhist.Data(), "rt");
00063    if (lunin) {
00064       char histline[256];
00065       while (fgets(histline, 256, lunin)) {
00066          histline[strlen(histline)-1] = 0; // remove trailing "\n"
00067          fComboCmd->InsertEntry(histline, 0, -1);
00068       }
00069       fclose(lunin);
00070    }
00071    fTimer = new TTimer(this, 1000);
00072    fTimer->Reset();
00073    fTimer->TurnOn();
00074    MapSubwindows();
00075    Resize(GetDefaultSize());
00076    MapWindow();
00077 }
00078 
00079 //______________________________________________________________________________
00080 TGCommandPlugin::~TGCommandPlugin()
00081 {
00082    // Destructor.
00083 
00084    TString pathtmp = TString::Format("%s/command.%d.log", 
00085                                      gSystem->TempDirectory(), fPid);
00086    gSystem->Unlink(pathtmp);
00087    delete fTimer;
00088    Cleanup();
00089 }
00090 
00091 //______________________________________________________________________________
00092 void TGCommandPlugin::CheckRemote(const char * /*str*/)
00093 {
00094    // Check if actual ROOT session is a remote one or a local one.
00095 
00096    Pixel_t pxl;
00097    TApplication *app = gROOT->GetApplication();
00098    if (!app->InheritsFrom("TRint"))
00099       return;
00100    TString sPrompt = ((TRint*)app)->GetPrompt();
00101    Int_t end = sPrompt.Index(":root [", 0);
00102    if (end > 0 && end != kNPOS) {
00103       // remote session
00104       sPrompt.Remove(end);
00105       gClient->GetColorByName("#ff0000", pxl);
00106       fLabel->SetTextColor(pxl);
00107       fLabel->SetText(Form("Command (%s):", sPrompt.Data()));
00108    }
00109    else {
00110       // local session
00111       gClient->GetColorByName("#000000", pxl);
00112       fLabel->SetTextColor(pxl);
00113       fLabel->SetText("Command (local):");
00114    }
00115    fHf->Layout();
00116 }
00117 
00118 //______________________________________________________________________________
00119 void TGCommandPlugin::HandleCommand()
00120 {
00121    // Handle command line from the "command" combo box.
00122 
00123    const char *string = fCommandBuf->GetString();
00124    if (strlen(string) > 1) {
00125       // form temporary file path
00126       TString sPrompt = "root []";
00127       TString pathtmp = TString::Format("%s/command.%d.log", 
00128                                         gSystem->TempDirectory(), fPid);
00129       TApplication *app = gROOT->GetApplication();
00130       if (app->InheritsFrom("TRint"))
00131          sPrompt = ((TRint*)gROOT->GetApplication())->GetPrompt();
00132       FILE *lunout = fopen(pathtmp.Data(), "a+t");
00133       if (lunout) {
00134          fputs(Form("%s%s\n",sPrompt.Data(), string), lunout);
00135          fclose(lunout);
00136       }
00137       gSystem->RedirectOutput(pathtmp.Data(), "a");
00138       gApplication->SetBit(TApplication::kProcessRemotely);
00139       gROOT->ProcessLine(string);
00140       fComboCmd->InsertEntry(string, 0, -1);
00141       if (app->InheritsFrom("TRint"))
00142          Gl_histadd((char *)string);
00143       gSystem->RedirectOutput(0);
00144       fStatus->LoadFile(pathtmp.Data());
00145       fStatus->ShowBottom();
00146       CheckRemote(string);
00147       fCommand->Clear();
00148    }
00149 }
00150 
00151 //______________________________________________________________________________
00152 Bool_t TGCommandPlugin::HandleTimer(TTimer *t)
00153 {
00154    // Handle timer event.
00155 
00156    if (t != fTimer) return kTRUE;
00157    CheckRemote("");
00158    return kTRUE;
00159 }

Generated on Tue Jul 5 14:21:58 2011 for ROOT_528-00b_version by  doxygen 1.5.1