Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGCommandPlugin.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Bertrand Bellenot 26/09/2007
3 
4 #include "TROOT.h"
5 #include "TSystem.h"
6 #include "TRint.h"
7 #include "TApplication.h"
8 #include "TGClient.h"
9 #include "TGLabel.h"
10 #include "TGFrame.h"
11 #include "TGLayout.h"
12 #include "TGComboBox.h"
13 #include "TGTextView.h"
14 #include "TGTextEntry.h"
15 #include "TGTextEdit.h"
16 #include "TInterpreter.h"
17 #include "Getline.h"
18 
19 #include "TGCommandPlugin.h"
20 
21 //_____________________________________________________________________________
22 //
23 // TGCommandPlugin
24 //
25 // Class used to redirect command line input/output.
26 //_____________________________________________________________________________
27 
28 ClassImp(TGCommandPlugin);
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// TGCommandPlugin Constructor.
32 
33 TGCommandPlugin::TGCommandPlugin(const TGWindow *p, UInt_t w, UInt_t h) :
34  TGMainFrame(p, w, h)
35 {
36  SetCleanup(kDeepCleanup);
37  fHf = new TGHorizontalFrame(this, 100, 20);
38  fComboCmd = new TGComboBox(fHf, "", 1);
39  fCommand = fComboCmd->GetTextEntry();
40  fCommandBuf = fCommand->GetBuffer();
41  fComboCmd->Resize(200, fCommand->GetDefaultHeight());
42  fHf->AddFrame(fComboCmd, new TGLayoutHints(kLHintsCenterY |
43  kLHintsRight | kLHintsExpandX, 5, 5, 1, 1));
44  fHf->AddFrame(fLabel = new TGLabel(fHf, "Command (local):"),
45  new TGLayoutHints(kLHintsCenterY | kLHintsRight,
46  5, 5, 1, 1));
47  AddFrame(fHf, new TGLayoutHints(kLHintsLeft | kLHintsTop |
48  kLHintsExpandX, 3, 3, 3, 3));
49  fCommand->Connect("ReturnPressed()", "TGCommandPlugin", this,
50  "HandleCommand()");
51  fStatus = new TGTextView(this, 10, 100, 1);
52  if (gClient->GetStyle() < 2) {
53  Pixel_t pxl;
54  gClient->GetColorByName("#a0a0a0", pxl);
55  fStatus->SetSelectBack(pxl);
56  fStatus->SetSelectFore(TGFrame::GetWhitePixel());
57  }
58  AddFrame(fStatus, new TGLayoutHints(kLHintsLeft | kLHintsTop |
59  kLHintsExpandX | kLHintsExpandY, 3, 3, 3, 3));
60  fPid = gSystem->GetPid();
61  TString defhist(Form("%s/.root_hist", gSystem->UnixPathName(
62  gSystem->HomeDirectory())));
63  FILE *lunin = fopen(defhist.Data(), "rt");
64  if (lunin) {
65  ULong_t linecount = 0;
66  char histline[256];
67  rewind(lunin);
68  while (fgets(histline, 256, lunin))
69  ++linecount;
70  rewind(lunin);
71  if (linecount > 500) {
72  linecount -= 500;
73  while(--linecount > 0)
74  if (!fgets(histline, 256, lunin))
75  break;
76  }
77  linecount = 0;
78  while (fgets(histline, 256, lunin)) {
79  histline[strlen(histline)-1] = 0; // remove trailing "\n"
80  fComboCmd->InsertEntry(histline, 0, -1);
81  // limit the history size to 500 lines
82  if (++linecount > 500)
83  break;
84  }
85  fclose(lunin);
86  }
87  fTimer = new TTimer(this, 1000);
88  fTimer->Reset();
89  fTimer->TurnOn();
90  MapSubwindows();
91  Resize(GetDefaultSize());
92  MapWindow();
93 }
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// Destructor.
97 
98 TGCommandPlugin::~TGCommandPlugin()
99 {
100  TString pathtmp = TString::Format("%s/command.%d.log",
101  gSystem->TempDirectory(), fPid);
102  gSystem->Unlink(pathtmp);
103  fCommand->Disconnect("ReturnPressed()");
104  delete fTimer;
105  fTimer = 0;
106  Cleanup();
107 }
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// Check if actual ROOT session is a remote one or a local one.
111 
112 void TGCommandPlugin::CheckRemote(const char * /*str*/)
113 {
114  Pixel_t pxl;
115  TApplication *app = gROOT->GetApplication();
116  if (!app->InheritsFrom("TRint"))
117  return;
118  TString sPrompt = ((TRint*)app)->GetPrompt();
119  Int_t end = sPrompt.Index(":root [", 0);
120  if (end > 0 && end != kNPOS) {
121  // remote session
122  sPrompt.Remove(end);
123  gClient->GetColorByName("#ff0000", pxl);
124  fLabel->SetTextColor(pxl);
125  fLabel->SetText(Form("Command (%s):", sPrompt.Data()));
126  }
127  else {
128  // local session
129  gClient->GetColorByName("#000000", pxl);
130  fLabel->SetTextColor(pxl);
131  fLabel->SetText("Command (local):");
132  }
133  fHf->Layout();
134 }
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// Handle command line from the "command" combo box.
138 
139 void TGCommandPlugin::HandleCommand()
140 {
141  const char *string = fCommandBuf->GetString();
142  if (strlen(string) > 1) {
143  // form temporary file path
144  TString sPrompt = "root []";
145  TString pathtmp = TString::Format("%s/command.%d.log",
146  gSystem->TempDirectory(), fPid);
147  TApplication *app = gROOT->GetApplication();
148  if (app->InheritsFrom("TRint"))
149  sPrompt = ((TRint*)gROOT->GetApplication())->GetPrompt();
150  FILE *lunout = fopen(pathtmp.Data(), "a+t");
151  if (lunout) {
152  fputs(Form("%s%s\n",sPrompt.Data(), string), lunout);
153  fclose(lunout);
154  }
155  gSystem->RedirectOutput(pathtmp.Data(), "a");
156  gApplication->SetBit(TApplication::kProcessRemotely);
157  gROOT->ProcessLine(string);
158  fComboCmd->InsertEntry(string, 0, -1);
159  if (app->InheritsFrom("TRint"))
160  Gl_histadd((char *)string);
161  gSystem->RedirectOutput(0);
162  fStatus->LoadFile(pathtmp.Data());
163  fStatus->ShowBottom();
164  CheckRemote(string);
165  fCommand->Clear();
166  }
167 }
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// Handle timer event.
171 
172 Bool_t TGCommandPlugin::HandleTimer(TTimer *t)
173 {
174  if ((fTimer == 0) || (t != fTimer)) return kTRUE;
175  CheckRemote("");
176  return kTRUE;
177 }