Logo ROOT  
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);
57 }
59 kLHintsExpandX | kLHintsExpandY, 3, 3, 3, 3));
60 fPid = gSystem->GetPid();
61 TString defhist(Form("%s/.root_hist", gSystem->UnixPathName(
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();
92 MapWindow();
93}
94
95////////////////////////////////////////////////////////////////////////////////
96/// Destructor.
97
99{
100 TString pathtmp = TString::Format("%s/command.%d.log",
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
112void 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
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",
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");
157 gROOT->ProcessLine(string);
158 fComboCmd->InsertEntry(string, 0, -1);
159 if (app->InheritsFrom("TRint"))
160 Gl_histadd((char *)string);
162 fStatus->LoadFile(pathtmp.Data());
164 CheckRemote(string);
165 fCommand->Clear();
166 }
167}
168
169////////////////////////////////////////////////////////////////////////////////
170/// Handle timer event.
171
173{
174 if ((fTimer == 0) || (t != fTimer)) return kTRUE;
175 CheckRemote("");
176 return kTRUE;
177}
ULong_t Pixel_t
Definition: GuiTypes.h:39
#define h(i)
Definition: RSha256.hxx:106
const Ssiz_t kNPOS
Definition: RtypesCore.h:111
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
unsigned long ULong_t
Definition: RtypesCore.h:51
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
#define gClient
Definition: TGClient.h:166
@ kDeepCleanup
Definition: TGFrame.h:51
@ kLHintsRight
Definition: TGLayout.h:33
@ kLHintsExpandY
Definition: TGLayout.h:38
@ kLHintsLeft
Definition: TGLayout.h:31
@ kLHintsCenterY
Definition: TGLayout.h:35
@ kLHintsTop
Definition: TGLayout.h:34
@ kLHintsExpandX
Definition: TGLayout.h:37
#define gROOT
Definition: TROOT.h:415
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:560
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:39
virtual TGTextEntry * GetTextEntry() const
Definition: TGComboBox.h:131
virtual void InsertEntry(TGString *s, Int_t id, Int_t afterID)
Definition: TGComboBox.h:112
TGHorizontalFrame * fHf
TGTextBuffer * fCommandBuf
void HandleCommand()
Handle command line from the "command" combo box.
TGTextEntry * fCommand
TGCommandPlugin(const TGWindow *p, UInt_t w, UInt_t h)
TGCommandPlugin Constructor.
virtual Bool_t HandleTimer(TTimer *t)
Handle timer event.
TGComboBox * fComboCmd
virtual ~TGCommandPlugin()
Destructor.
void CheckRemote(const char *)
Check if actual ROOT session is a remote one or a local one.
TGTextView * fStatus
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 Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition: TGFrame.cxx:949
virtual void Layout()
Layout the elements of the composite frame.
Definition: TGFrame.cxx:1239
virtual void SetCleanup(Int_t mode=kLocalCleanup)
Turn on automatic cleanup of child frames in dtor.
Definition: TGFrame.cxx:1054
virtual TGDimension GetDefaultSize() const
std::cout << fWidth << "x" << fHeight << std::endl;
Definition: TGFrame.h:375
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1146
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition: TGFrame.cxx:691
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:238
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:587
virtual void MapWindow()
Definition: TGFrame.h:251
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition: TGLabel.cxx:359
virtual void SetText(TGString *newText)
Set new text in label.
Definition: TGLabel.cxx:177
const char * GetString() const
Definition: TGTextBuffer.h:47
TGTextBuffer * GetBuffer() const
Definition: TGTextEntry.h:127
void Clear(Option_t *option="")
Clears up the text entry.
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 void SetSelectBack(Pixel_t p)
set selected text background color
Definition: TGTextView.cxx:177
virtual void ShowBottom()
Show bottom of the page.
virtual void SetSelectFore(Pixel_t p)
set selected text color
Definition: TGTextView.cxx:186
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:694
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:443
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
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
Definition: TRint.h:31
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
TString & Remove(Ssiz_t pos)
Definition: TString.h:668
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:2311
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition: TString.h:634
virtual int GetPid()
Get process id.
Definition: TSystem.cxx:717
virtual const char * HomeDirectory(const char *userName=0)
Return the user's home directory.
Definition: TSystem.cxx:895
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1054
virtual int Unlink(const char *name)
Unlink, i.e.
Definition: TSystem.cxx:1372
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:1703
virtual const char * TempDirectory() const
Return a user configured or systemwide directory to create temporary files in.
Definition: TSystem.cxx:1473
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
virtual void TurnOn()
Add the timer to the system timer list.
Definition: TTimer.cxx:241
void Reset()
Reset the timer.
Definition: TTimer.cxx:157