Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TGCommandPlugin.cxx
Go to the documentation of this file.
1// @(#)root/gui:$Id$
2// Author: Bertrand Bellenot 26/09/2007
3
4/*************************************************************************
5 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12/** \class TGCommandPlugin
13Class used to redirect the command line input/output.
14**/
15
16#include "TROOT.h"
17#include "TSystem.h"
18#include "TRint.h"
19#include "TApplication.h"
20#include "TGClient.h"
21#include "TGLabel.h"
22#include "TGFrame.h"
23#include "TGLayout.h"
24#include "TGComboBox.h"
25#include "TGTextView.h"
26#include "TGTextEntry.h"
27#include "TInterpreter.h"
28#include "Getline.h"
29#include "KeySymbols.h"
30
31#include "TGCommandPlugin.h"
32#include <vector>
33#include <string>
34
36
37////////////////////////////////////////////////////////////////////////////////
38/// TGCommandPlugin Constructor.
39
41 TGMainFrame(p, w, h)
42{
45 fPos = 0;
46 fTempString = "";
47 fHf = new TGHorizontalFrame(this, 100, 20);
48 fComboCmd = new TGComboBox(fHf, "", 1);
53 kLHintsRight | kLHintsExpandX, 5, 5, 1, 1));
54 fHf->AddFrame(fLabel = new TGLabel(fHf, "Command (local):"),
56 5, 5, 1, 1));
58 kLHintsExpandX, 3, 3, 3, 3));
59 fCommand->Connect("ReturnPressed()", "TGCommandPlugin", this,
60 "HandleCommand()");
61 fCommand->Connect("CursorOutUp()", "TGCommandPlugin", this,
62 "HandleArrows(=kKey_Up)");
63 fCommand->Connect("CursorOutDown()", "TGCommandPlugin", this,
64 "HandleArrows(=kKey_Down)");
65 fCommand->Connect("TabPressed()", "TGCommandPlugin", this,
66 "HandleTab()");
67 fCommand->Connect("TextChanged(const char *)", "TGCommandPlugin", this,
68 "HandleTextChanged(const char *)");
69 fStatus = new TGTextView(this, 10, 100, 1);
70 if (gClient->GetStyle() < 2) {
71 Pixel_t pxl;
72 gClient->GetColorByName("#a0a0a0", pxl);
75 }
77 kLHintsExpandX | kLHintsExpandY, 3, 3, 3, 3));
78 fPid = gSystem->GetPid();
79 TString defhist(Form("%s/.root_hist", gSystem->UnixPathName(
81 FILE *lunin = fopen(defhist.Data(), "rt");
82 if (lunin) {
83 ULong_t linecount = 0;
84 char histline[256];
85 rewind(lunin);
86 while (fgets(histline, 256, lunin))
87 ++linecount;
88 rewind(lunin);
89 if (linecount > 500) {
90 linecount -= 500;
91 while(--linecount > 0)
92 if (!fgets(histline, 256, lunin))
93 break;
94 }
95 linecount = 0;
96 while (fgets(histline, 256, lunin)) {
97 histline[strlen(histline)-1] = 0; // remove trailing "\n"
98 fComboCmd->InsertEntry(histline, linecount, -1);
99 // limit the history size to 500 lines
100 if (++linecount > 500)
101 break;
102 }
103 fclose(lunin);
104 }
105 fTimer = new TTimer(this, 1000);
106 fTimer->Reset();
107 fTimer->TurnOn();
110 MapWindow();
111}
112
113////////////////////////////////////////////////////////////////////////////////
114/// Destructor.
115
117{
118 TString pathtmp = TString::Format("%s/command.%d.log",
120 gSystem->Unlink(pathtmp);
121 fCommand->Disconnect("ReturnPressed()");
122 fCommand->Disconnect("CursorOutUp()");
123 fCommand->Disconnect("CursorOutDown()");
124 fCommand->Disconnect("TabPressed()");
125 fCommand->Disconnect("TextChanged(const char *)");
126 delete fTimer;
127 fTimer = 0;
128 Cleanup();
129}
130
131////////////////////////////////////////////////////////////////////////////////
132/// Check if actual ROOT session is a remote one or a local one.
133
134void TGCommandPlugin::CheckRemote(const char * /*str*/)
135{
136 Pixel_t pxl;
137 TApplication *app = gROOT->GetApplication();
138 if (!app->InheritsFrom("TRint"))
139 return;
140 TString sPrompt = ((TRint*)app)->GetPrompt();
141 Int_t end = sPrompt.Index(":root [", 0);
142 if (end > 0 && end != kNPOS) {
143 // remote session
144 sPrompt.Remove(end);
145 gClient->GetColorByName("#ff0000", pxl);
146 fLabel->SetTextColor(pxl);
147 fLabel->SetText(Form("Command (%s):", sPrompt.Data()));
148 }
149 else {
150 // local session
151 gClient->GetColorByName("#000000", pxl);
152 fLabel->SetTextColor(pxl);
153 fLabel->SetText("Command (local):");
154 }
155 fHf->Layout();
156}
157
158////////////////////////////////////////////////////////////////////////////////
159/// Handle the 'up' and 'down' arrow key events.
160
162{
164 switch ((EKeySym)keysym) {
165 case kKey_Up:
166 if (fPos < entries-1) ++fPos;
167 break;
168 case kKey_Down:
169 if (fPos > 0) --fPos;
170 break;
171 default:
172 break;
173 }
174 if (fPos > 0) {
176 if (te) {
178 }
179 } else {
180 if (fTempString.Length() > 0)
182 else
183 fCommand->Clear();
184 }
185}
186
187////////////////////////////////////////////////////////////////////////////////
188/// Handle command line from the "command" combo box.
189
191{
192 const char *string = fCommandBuf->GetString();
193 if (strlen(string) > 0) {
194 // form temporary file path
195 TString sPrompt = "root []";
196 TString pathtmp = TString::Format("%s/command.%d.log",
198 TApplication *app = gROOT->GetApplication();
199 if (app->InheritsFrom("TRint"))
200 sPrompt = ((TRint*)gROOT->GetApplication())->GetPrompt();
201 FILE *lunout = fopen(pathtmp.Data(), "a+t");
202 if (lunout) {
203 fputs(Form("%s%s\n",sPrompt.Data(), string), lunout);
204 fclose(lunout);
205 }
206 gSystem->RedirectOutput(pathtmp.Data(), "a");
208 gROOT->ProcessLine(string);
210 fComboCmd->InsertEntry(string, entries, -1);
211 fPos = 0;
212 if (app->InheritsFrom("TRint") || fHistAdd)
213 Gl_histadd((char *)string);
215 fStatus->LoadFile(pathtmp.Data());
217 CheckRemote(string);
218 fCommand->Clear();
220 }
221}
222
223////////////////////////////////////////////////////////////////////////////////
224/// Handle the 'TAB' key events.
225
227{
228 std::string prompt = gInterpreter->GetPrompt();
229 std::string line = fCommandBuf->GetString();
230 if (prompt.find("root") == std::string::npos)
231 prompt = "root []";
232 prompt += " ";
233 prompt += line;
234 fStatus->AddLine(prompt.c_str());
236 std::vector<std::string> result;
237 size_t cur = line.length();
238 gInterpreter->CodeComplete(line, cur, result);
239 for (auto& res : result) {
240 fStatus->AddLine(res.c_str());
242 }
243}
244
245////////////////////////////////////////////////////////////////////////////////
246/// Handle the text changed events.
247
249{
251}
252
253////////////////////////////////////////////////////////////////////////////////
254/// Handle timer event.
255
257{
258 if ((fTimer == 0) || (t != fTimer)) return kTRUE;
259 CheckRemote("");
260 return kTRUE;
261}
262
263////////////////////////////////////////////////////////////////////////////////
264/// The function SetHistAdd() is needed for a standalone TApplication to log the
265/// TGCommandPlugin commands into a ROOT history file.
266/// However, this function has no effect if the user does not explictly set on
267/// his standalone application the name of the ROOT history file.
268/// To log into the default ROOT history file, call this on the user-side of the
269/// code:
270/// Gl_histinit(gEnv->GetValue("Rint.History", gSystem->HomeDirectory()));
271/// Otherwise, replace the argument of Gl_histinit with a text file name you want
272/// to use for application-specific logging.
273
275{
276 fHistAdd = add;
277}
ULong_t Pixel_t
Pixel value.
Definition GuiTypes.h:40
EKeySym
Definition KeySymbols.h:25
@ kKey_Down
Definition KeySymbols.h:43
@ kKey_Up
Definition KeySymbols.h:41
#define h(i)
Definition RSha256.hxx:106
const Ssiz_t kNPOS
Definition RtypesCore.h:115
const Bool_t kFALSE
Definition RtypesCore.h:92
unsigned long ULong_t
Definition RtypesCore.h:55
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassImp(name)
Definition Rtypes.h:364
R__EXTERN TApplication * gApplication
#define gClient
Definition TGClient.h:166
@ kDeepCleanup
Definition TGFrame.h:50
@ 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 gInterpreter
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
virtual TGTextEntry * GetTextEntry() const
Definition TGComboBox.h:131
virtual TGListBox * GetListBox() const
Definition TGComboBox.h:130
virtual void InsertEntry(TGString *s, Int_t id, Int_t afterID)
Definition TGComboBox.h:112
virtual Int_t GetNumberOfEntries() const
Definition TGComboBox.h:127
Class used to redirect the command line input/output.
TGHorizontalFrame * fHf
void SetHistAdd(Bool_t add=kTRUE)
The function SetHistAdd() is needed for a standalone TApplication to log the TGCommandPlugin commands...
TGTextBuffer * fCommandBuf
void HandleCommand()
Handle command line from the "command" combo box.
TGTextEntry * fCommand
void HandleTextChanged(const char *)
Handle the text changed events.
TGCommandPlugin(const TGWindow *p, UInt_t w, UInt_t h)
TGCommandPlugin Constructor.
void HandleTab()
Handle the 'TAB' key events.
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
void HandleArrows(Int_t keysym)
Handle the 'up' and 'down' arrow key events.
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1102
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition TGFrame.cxx:952
virtual void Layout()
Layout the elements of the composite frame.
Definition TGFrame.cxx:1242
virtual void SetCleanup(Int_t mode=kLocalCleanup)
Turn on automatic cleanup of child frames in dtor.
Definition TGFrame.cxx:1057
virtual TGDimension GetDefaultSize() const
std::cout << fWidth << "x" << fHeight << std::endl;
Definition TGFrame.h:352
virtual void MapSubwindows()
Map all sub windows that are part of the composite frame.
Definition TGFrame.cxx:1149
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition TGFrame.cxx:694
virtual UInt_t GetDefaultHeight() const
Definition TGFrame.h:215
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition TGFrame.cxx:590
virtual void MapWindow()
map window
Definition TGFrame.h:228
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition TGLabel.cxx:361
virtual void SetText(TGString *newText)
Set new text in label.
Definition TGLabel.cxx:179
virtual TGLBEntry * GetEntry(Int_t id) const
Returns list box entry with specified id.
const char * GetString() const
Definition TGString.h:40
const char * GetString() const
TGTextBuffer * GetBuffer() const
virtual void SetText(const char *text, Bool_t emit=kTRUE)
Sets text entry to text, clears the selection and moves the cursor to the end of the line.
void Clear(Option_t *option="")
Clears up the text entry.
const TGString * GetText() const
Definition TGListBox.h:114
virtual Bool_t LoadFile(const char *fname, long startpos=0, long length=-1)
Load a file in the text view widget.
virtual void AddLine(const char *string)
Add a line of text to the view widget.
virtual void SetSelectBack(Pixel_t p)
set selected text background color
virtual void ShowBottom()
Show bottom of the page.
virtual void SetSelectFore(Pixel_t p)
set selected text color
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:696
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:445
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:866
Bool_t Disconnect(const char *signal=0, void *receiver=0, const char *slot=0)
Disconnects signal of this object from slot of receiver.
Definition TRint.h:31
Basic string class.
Definition TString.h:136
Ssiz_t Length() const
Definition TString.h:410
void Clear()
Clear string without changing its capacity.
Definition TString.cxx:1196
const char * Data() const
Definition TString.h:369
TString & Remove(Ssiz_t pos)
Definition TString.h:673
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:2331
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:639
virtual Int_t RedirectOutput(const char *name, const char *mode="a", RedirectHandle_t *h=nullptr)
Redirect standard output (stdout, stderr) to the specified file.
Definition TSystem.cxx:1711
virtual int GetPid()
Get process id.
Definition TSystem.cxx:708
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1061
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition TSystem.cxx:886
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1379
virtual const char * TempDirectory() const
Return a user configured or systemwide directory to create temporary files in.
Definition TSystem.cxx:1480
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
TText * text
TLine * line