Logo ROOT   6.12/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 {
37  fHf = new TGHorizontalFrame(this, 100, 20);
38  fComboCmd = new TGComboBox(fHf, "", 1);
43  kLHintsRight | kLHintsExpandX, 5, 5, 1, 1));
44  fHf->AddFrame(fLabel = new TGLabel(fHf, "Command (local):"),
46  5, 5, 1, 1));
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);
57  }
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();
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 }
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:587
virtual TGTextEntry * GetTextEntry() const
Definition: TGComboBox.h:131
TGComboBox * fComboCmd
virtual int GetPid()
Get process id.
Definition: TSystem.cxx:715
void Reset()
Reset the timer.
Definition: TTimer.cxx:157
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition: TGLabel.cxx:359
const Ssiz_t kNPOS
Definition: RtypesCore.h:111
TGTextBuffer * fCommandBuf
virtual void SetSelectBack(Pixel_t p)
set selected text background color
Definition: TGTextView.cxx:177
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:885
#define gROOT
Definition: TROOT.h:402
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:585
Basic string class.
Definition: TString.h:125
#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:694
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1044
TGCommandPlugin(const TGWindow *p, UInt_t w, UInt_t h)
TGCommandPlugin Constructor.
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1357
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:2365
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:1458
void HandleCommand()
Handle command line from the "command" combo box.
TGTextBuffer * GetBuffer() const
Definition: TGTextEntry.h:127
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot...
Definition: TQObject.cxx:867
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
virtual TGDimension GetDefaultSize() const
std::cout << fWidth << "x" << fHeight << std::endl;
Definition: TGFrame.h:375
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:443
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 void SetSelectFore(Pixel_t p)
set selected text color
Definition: TGTextView.cxx:186
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:1688
TString & Remove(Ssiz_t pos)
Definition: TString.h:619
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:238
#define ClassImp(name)
Definition: Rtypes.h:359
virtual void SetCleanup(Int_t mode=kLocalCleanup)
Turn on automatic cleanup of child frames in dtor.
Definition: TGFrame.cxx:1054
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
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1146
virtual void MapWindow()
Definition: TGFrame.h:251
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.
virtual void TurnOn()
Add the timer to the system timer list.
Definition: TTimer.cxx:241
const Bool_t kTRUE
Definition: RtypesCore.h:87
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:345