Logo ROOT   6.08/07
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  delete fTimer;
89  Cleanup();
90 }
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// Check if actual ROOT session is a remote one or a local one.
94 
95 void TGCommandPlugin::CheckRemote(const char * /*str*/)
96 {
97  Pixel_t pxl;
98  TApplication *app = gROOT->GetApplication();
99  if (!app->InheritsFrom("TRint"))
100  return;
101  TString sPrompt = ((TRint*)app)->GetPrompt();
102  Int_t end = sPrompt.Index(":root [", 0);
103  if (end > 0 && end != kNPOS) {
104  // remote session
105  sPrompt.Remove(end);
106  gClient->GetColorByName("#ff0000", pxl);
107  fLabel->SetTextColor(pxl);
108  fLabel->SetText(Form("Command (%s):", sPrompt.Data()));
109  }
110  else {
111  // local session
112  gClient->GetColorByName("#000000", pxl);
113  fLabel->SetTextColor(pxl);
114  fLabel->SetText("Command (local):");
115  }
116  fHf->Layout();
117 }
118 
119 ////////////////////////////////////////////////////////////////////////////////
120 /// Handle command line from the "command" combo box.
121 
123 {
124  const char *string = fCommandBuf->GetString();
125  if (strlen(string) > 1) {
126  // form temporary file path
127  TString sPrompt = "root []";
128  TString pathtmp = TString::Format("%s/command.%d.log",
130  TApplication *app = gROOT->GetApplication();
131  if (app->InheritsFrom("TRint"))
132  sPrompt = ((TRint*)gROOT->GetApplication())->GetPrompt();
133  FILE *lunout = fopen(pathtmp.Data(), "a+t");
134  if (lunout) {
135  fputs(Form("%s%s\n",sPrompt.Data(), string), lunout);
136  fclose(lunout);
137  }
138  gSystem->RedirectOutput(pathtmp.Data(), "a");
140  gROOT->ProcessLine(string);
141  fComboCmd->InsertEntry(string, 0, -1);
142  if (app->InheritsFrom("TRint"))
143  Gl_histadd((char *)string);
145  fStatus->LoadFile(pathtmp.Data());
146  fStatus->ShowBottom();
147  CheckRemote(string);
148  fCommand->Clear();
149  }
150 }
151 
152 ////////////////////////////////////////////////////////////////////////////////
153 /// Handle timer event.
154 
156 {
157  if (t != fTimer) return kTRUE;
158  CheckRemote("");
159  return kTRUE;
160 }
TGComboBox * fComboCmd
virtual int GetPid()
Get process id.
Definition: TSystem.cxx:712
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition: TGLabel.cxx:359
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:882
#define gROOT
Definition: TROOT.h:364
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:582
Basic string class.
Definition: TString.h:137
#define gClient
Definition: TGClient.h:174
virtual void InsertEntry(TGString *s, Int_t id, Int_t afterID)
Definition: TGComboBox.h:114
virtual ~TGCommandPlugin()
Destructor.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
R__EXTERN TApplication * gApplication
Definition: TApplication.h:171
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:739
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1037
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1347
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:2335
ULong_t Pixel_t
Definition: GuiTypes.h:41
virtual void SetText(TGString *newText)
Set new text in label.
Definition: TGLabel.cxx:177
const char * GetString() const
Definition: TGTextBuffer.h:49
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:1448
void HandleCommand()
Handle command line from the "command" combo box.
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
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:488
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:57
TGTextEntry * fCommand
Definition: TRint.h:35
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:1678
TString & Remove(Ssiz_t pos)
Definition: TString.h:616
#define ClassImp(name)
Definition: Rtypes.h:279
const Ssiz_t kNPOS
Definition: Rtypes.h:115
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:45
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: Rtypes.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:349