Logo ROOT   6.10/09
Reference Guide
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 
29 
30 ////////////////////////////////////////////////////////////////////////////////
31 /// TGCommandPlugin Constructor.
32 
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):"),
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  char histline[256];
66  while (fgets(histline, 256, lunin)) {
67  histline[strlen(histline)-1] = 0; // remove trailing "\n"
68  fComboCmd->InsertEntry(histline, 0, -1);
69  }
70  fclose(lunin);
71  }
72  fTimer = new TTimer(this, 1000);
73  fTimer->Reset();
74  fTimer->TurnOn();
75  MapSubwindows();
76  Resize(GetDefaultSize());
77  MapWindow();
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// Destructor.
82 
84 {
85  TString pathtmp = TString::Format("%s/command.%d.log",
87  gSystem->Unlink(pathtmp);
88  fCommand->Disconnect("ReturnPressed()");
89  delete fTimer;
90  fTimer = 0;
91  Cleanup();
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Check if actual ROOT session is a remote one or a local one.
96 
97 void TGCommandPlugin::CheckRemote(const char * /*str*/)
98 {
99  Pixel_t pxl;
100  TApplication *app = gROOT->GetApplication();
101  if (!app->InheritsFrom("TRint"))
102  return;
103  TString sPrompt = ((TRint*)app)->GetPrompt();
104  Int_t end = sPrompt.Index(":root [", 0);
105  if (end > 0 && end != kNPOS) {
106  // remote session
107  sPrompt.Remove(end);
108  gClient->GetColorByName("#ff0000", pxl);
109  fLabel->SetTextColor(pxl);
110  fLabel->SetText(Form("Command (%s):", sPrompt.Data()));
111  }
112  else {
113  // local session
114  gClient->GetColorByName("#000000", pxl);
115  fLabel->SetTextColor(pxl);
116  fLabel->SetText("Command (local):");
117  }
118  fHf->Layout();
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Handle command line from the "command" combo box.
123 
125 {
126  const char *string = fCommandBuf->GetString();
127  if (strlen(string) > 1) {
128  // form temporary file path
129  TString sPrompt = "root []";
130  TString pathtmp = TString::Format("%s/command.%d.log",
132  TApplication *app = gROOT->GetApplication();
133  if (app->InheritsFrom("TRint"))
134  sPrompt = ((TRint*)gROOT->GetApplication())->GetPrompt();
135  FILE *lunout = fopen(pathtmp.Data(), "a+t");
136  if (lunout) {
137  fputs(Form("%s%s\n",sPrompt.Data(), string), lunout);
138  fclose(lunout);
139  }
140  gSystem->RedirectOutput(pathtmp.Data(), "a");
142  gROOT->ProcessLine(string);
143  fComboCmd->InsertEntry(string, 0, -1);
144  if (app->InheritsFrom("TRint"))
145  Gl_histadd((char *)string);
147  fStatus->LoadFile(pathtmp.Data());
148  fStatus->ShowBottom();
149  CheckRemote(string);
150  fCommand->Clear();
151  }
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Handle timer event.
156 
158 {
159  if ((fTimer == 0) || (t != fTimer)) return kTRUE;
160  CheckRemote("");
161  return kTRUE;
162 }
TGComboBox * fComboCmd
virtual int GetPid()
Get process id.
Definition: TSystem.cxx:714
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition: TGLabel.cxx:359
const Ssiz_t kNPOS
Definition: RtypesCore.h:115
TGTextBuffer * fCommandBuf
TH1 * h
Definition: legend2.C:5
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition: TGFrame.cxx:691
virtual const char * HomeDirectory(const char *userName=0)
Return the user&#39;s home directory.
Definition: TSystem.cxx:884
#define gROOT
Definition: TROOT.h:375
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:587
Basic string class.
Definition: TString.h:129
#define gClient
Definition: TGClient.h:166
virtual void InsertEntry(TGString *s, Int_t id, Int_t afterID)
Definition: TGComboBox.h:112
virtual ~TGCommandPlugin()
Destructor.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
R__EXTERN TApplication * gApplication
Definition: TApplication.h:165
virtual void Layout()
Layout the elements of the composite frame.
Definition: TGFrame.cxx:1239
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:687
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1043
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1353
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2345
ULong_t Pixel_t
Definition: GuiTypes.h:39
virtual void SetText(TGString *newText)
Set new text in label.
Definition: TGLabel.cxx:177
const char * GetString() const
Definition: TGTextBuffer.h:47
virtual void ShowBottom()
Show bottom of the page.
virtual Bool_t LoadFile(const char *fname, long startpos=0, long length=-1)
Load a file in the text view widget.
Definition: TGTextView.cxx:452
virtual const char * TempDirectory() const
Return a user configured or systemwide directory to create temporary files in.
Definition: TSystem.cxx:1454
void HandleCommand()
Handle command line from the "command" combo box.
R__EXTERN TSystem * gSystem
Definition: TSystem.h:539
virtual Bool_t HandleTimer(TTimer *t)
Handle timer event.
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:436
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
TGTextEntry * fCommand
Definition: TRint.h:31
virtual Int_t RedirectOutput(const char *name, const char *mode="a", RedirectHandle_t *h=0)
Redirect standard output (stdout, stderr) to the specified file.
Definition: TSystem.cxx:1684
TString & Remove(Ssiz_t pos)
Definition: TString.h:621
#define ClassImp(name)
Definition: Rtypes.h:336
Bool_t Disconnect(const char *signal=0, void *receiver=0, const char *slot=0)
Disconnects signal of this object from slot of receiver.
Definition: TQObject.cxx:1025
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:39
TGTextView * fStatus
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition: TGFrame.cxx:949
void Clear(Option_t *option="")
Clears up the text entry.
const Bool_t kTRUE
Definition: RtypesCore.h:91
TGHorizontalFrame * fHf
void CheckRemote(const char *)
Check if actual ROOT session is a remote one or a local one.
const char * Data() const
Definition: TString.h:347