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/*************************************************************************
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
13 \ingroup guiwidgets
14
15Class used to redirect the command line input/output.
16
17*/
18
19#include "TROOT.h"
20#include "TSystem.h"
21#include "TRint.h"
22#include "TApplication.h"
23#include "TGClient.h"
24#include "TGLabel.h"
25#include "TGFrame.h"
26#include "TGLayout.h"
27#include "TGComboBox.h"
28#include "TGTextView.h"
29#include "TGTextEntry.h"
30#include "TInterpreter.h"
31#include "Getline.h"
32#include "KeySymbols.h"
33
34#include "TGCommandPlugin.h"
35#include <vector>
36#include <string>
37
39
40////////////////////////////////////////////////////////////////////////////////
41/// TGCommandPlugin Constructor.
42
44 TGMainFrame(p, w, h)
45{
48 fPos = 0;
49 fTempString = "";
50 fHf = new TGHorizontalFrame(this, 100, 20);
51 fComboCmd = new TGComboBox(fHf, "", 1);
56 kLHintsRight | kLHintsExpandX, 5, 5, 1, 1));
57 fHf->AddFrame(fLabel = new TGLabel(fHf, "Command (local):"),
59 5, 5, 1, 1));
61 kLHintsExpandX, 3, 3, 3, 3));
62 fCommand->Connect("ReturnPressed()", "TGCommandPlugin", this,
63 "HandleCommand()");
64 fCommand->Connect("CursorOutUp()", "TGCommandPlugin", this,
65 "HandleArrows(=kKey_Up)");
66 fCommand->Connect("CursorOutDown()", "TGCommandPlugin", this,
67 "HandleArrows(=kKey_Down)");
68 fCommand->Connect("TabPressed()", "TGCommandPlugin", this,
69 "HandleTab()");
70 fCommand->Connect("TextChanged(const char *)", "TGCommandPlugin", this,
71 "HandleTextChanged(const char *)");
72 fStatus = new TGTextView(this, 10, 100, 1);
73 if (gClient->GetStyle() < 2) {
74 Pixel_t pxl;
75 gClient->GetColorByName("#a0a0a0", pxl);
78 }
80 kLHintsExpandX | kLHintsExpandY, 3, 3, 3, 3));
81 fPid = gSystem->GetPid();
82 TString defhist(Form("%s/.root_hist", gSystem->UnixPathName(
84 FILE *lunin = fopen(defhist.Data(), "rt");
85 if (lunin) {
86 ULong_t linecount = 0;
87 char histline[256];
88 rewind(lunin);
89 while (fgets(histline, 256, lunin))
90 ++linecount;
91 rewind(lunin);
92 if (linecount > 500) {
93 linecount -= 500;
94 while(--linecount > 0)
95 if (!fgets(histline, 256, lunin))
96 break;
97 }
98 linecount = 0;
99 while (fgets(histline, 256, lunin)) {
100 histline[strlen(histline)-1] = 0; // remove trailing "\n"
101 fComboCmd->InsertEntry(histline, linecount, -1);
102 // limit the history size to 500 lines
103 if (++linecount > 500)
104 break;
105 }
106 fclose(lunin);
107 }
108 fTimer = new TTimer(this, 1000);
109 fTimer->Reset();
110 fTimer->TurnOn();
113 MapWindow();
114}
115
116////////////////////////////////////////////////////////////////////////////////
117/// Destructor.
118
120{
121 TString pathtmp = TString::Format("%s/command.%d.log",
123 gSystem->Unlink(pathtmp);
124 fCommand->Disconnect("ReturnPressed()");
125 fCommand->Disconnect("CursorOutUp()");
126 fCommand->Disconnect("CursorOutDown()");
127 fCommand->Disconnect("TabPressed()");
128 fCommand->Disconnect("TextChanged(const char *)");
129 delete fTimer;
130 fTimer = 0;
131 Cleanup();
132}
133
134////////////////////////////////////////////////////////////////////////////////
135/// Check if actual ROOT session is a remote one or a local one.
136
137void TGCommandPlugin::CheckRemote(const char * /*str*/)
138{
139 Pixel_t pxl;
140 TApplication *app = gROOT->GetApplication();
141 if (!app->InheritsFrom("TRint"))
142 return;
143 TString sPrompt = ((TRint*)app)->GetPrompt();
144 Int_t end = sPrompt.Index(":root [", 0);
145 if (end > 0 && end != kNPOS) {
146 // remote session
147 sPrompt.Remove(end);
148 gClient->GetColorByName("#ff0000", pxl);
149 fLabel->SetTextColor(pxl);
150 fLabel->SetText(Form("Command (%s):", sPrompt.Data()));
151 }
152 else {
153 // local session
154 gClient->GetColorByName("#000000", pxl);
155 fLabel->SetTextColor(pxl);
156 fLabel->SetText("Command (local):");
157 }
158 fHf->Layout();
159}
160
161////////////////////////////////////////////////////////////////////////////////
162/// Handle the 'up' and 'down' arrow key events.
163
165{
167 switch ((EKeySym)keysym) {
168 case kKey_Up:
169 if (fPos < entries-1) ++fPos;
170 break;
171 case kKey_Down:
172 if (fPos > 0) --fPos;
173 break;
174 default:
175 break;
176 }
177 if (fPos > 0) {
179 if (te) {
181 }
182 } else {
183 if (fTempString.Length() > 0)
185 else
186 fCommand->Clear();
187 }
188}
189
190////////////////////////////////////////////////////////////////////////////////
191/// Handle command line from the "command" combo box.
192
194{
195 const char *string = fCommandBuf->GetString();
196 if (strlen(string) > 0) {
197 // form temporary file path
198 TString sPrompt = "root []";
199 TString pathtmp = TString::Format("%s/command.%d.log",
201 TApplication *app = gROOT->GetApplication();
202 if (app->InheritsFrom("TRint"))
203 sPrompt = ((TRint*)gROOT->GetApplication())->GetPrompt();
204 FILE *lunout = fopen(pathtmp.Data(), "a+t");
205 if (lunout) {
206 fputs(Form("%s%s\n",sPrompt.Data(), string), lunout);
207 fclose(lunout);
208 }
209 gSystem->RedirectOutput(pathtmp.Data(), "a");
211 gROOT->ProcessLine(string);
213 fComboCmd->InsertEntry(string, entries, -1);
214 fPos = 0;
215 if (app->InheritsFrom("TRint") || fHistAdd)
216 Gl_histadd((char *)string);
218 fStatus->LoadFile(pathtmp.Data());
220 CheckRemote(string);
221 fCommand->Clear();
223 }
224}
225
226////////////////////////////////////////////////////////////////////////////////
227/// Handle the 'TAB' key events.
228
230{
231 std::string line = fCommandBuf->GetString();
232 std::vector<std::string> result;
233 size_t cur = line.length();
234 gInterpreter->CodeComplete(line, cur, result);
235 if (result.size() == 1) {
236 // when there is only one result, complete the command line input
237 std::string found = result[0];
238 std::string what = line;
239 size_t colon = line.find_last_of("::");
240 if (colon != std::string::npos)
241 what = line.substr(colon+2);
242 size_t pos = found.find(what) + what.length();
243 std::string suffix = found.substr(pos);
244 fCommand->AppendText(suffix.c_str());
245 } else {
246 // otherwise print all results
247 std::string prompt = gInterpreter->GetPrompt();
248 if (prompt.find("root") == std::string::npos)
249 prompt = "root []";
250 prompt += " ";
251 prompt += line;
252 fStatus->AddLine(prompt.c_str());
254 for (auto& res : result) {
255 fStatus->AddLine(res.c_str());
257 }
258 }
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// Handle the text changed events.
263
265{
267}
268
269////////////////////////////////////////////////////////////////////////////////
270/// Handle timer event.
271
273{
274 if ((fTimer == 0) || (t != fTimer)) return kTRUE;
275 CheckRemote("");
276 return kTRUE;
277}
278
279////////////////////////////////////////////////////////////////////////////////
280/// Let user stop the internal timer when there is no need to check for remote.
281/// or start it again later on if needed. (on=False to stop, on=True to start)
282
284{
285 if (on)
286 fTimer->TurnOn();
287 else
288 fTimer->TurnOff();
289}
290
291////////////////////////////////////////////////////////////////////////////////
292/// The function SetHistAdd() is needed for a standalone TApplication to log the
293/// TGCommandPlugin commands into a ROOT history file.
294/// However, this function has no effect if the user does not explictly set on
295/// his standalone application the name of the ROOT history file.
296/// To log into the default ROOT history file, call this on the user-side of the
297/// code:
298/// Gl_histinit(gEnv->GetValue("Rint.History", gSystem->HomeDirectory()));
299/// Otherwise, replace the argument of Gl_histinit with a text file name you want
300/// to use for application-specific logging.
301
303{
304 fHistAdd = add;
305}
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:124
const Bool_t kFALSE
Definition: RtypesCore.h:101
const Bool_t kTRUE
Definition: RtypesCore.h:100
unsigned long ULong_t
Definition: RtypesCore.h:55
#define ClassImp(name)
Definition: Rtypes.h:375
R__EXTERN TApplication * gApplication
Definition: TApplication.h:165
#define gClient
Definition: TGClient.h:157
@ kDeepCleanup
Definition: TGFrame.h:42
@ kLHintsRight
Definition: TGLayout.h:26
@ kLHintsExpandY
Definition: TGLayout.h:31
@ kLHintsLeft
Definition: TGLayout.h:24
@ kLHintsCenterY
Definition: TGLayout.h:28
@ kLHintsTop
Definition: TGLayout.h:27
@ kLHintsExpandX
Definition: TGLayout.h:30
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
Option_t Option_t TPoint TPoint const char text
#define gInterpreter
Definition: TInterpreter.h:564
#define gROOT
Definition: TROOT.h:404
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2452
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
Definition: TApplication.h:39
A combobox (also known as a drop down listbox) allows the selection of one item out of a list of item...
Definition: TGComboBox.h:47
virtual TGTextEntry * GetTextEntry() const
Definition: TGComboBox.h:111
virtual TGListBox * GetListBox() const
Definition: TGComboBox.h:110
virtual void InsertEntry(TGString *s, Int_t id, Int_t afterID)
Definition: TGComboBox.h:92
virtual Int_t GetNumberOfEntries() const
Definition: TGComboBox.h:107
Class used to redirect the command line input/output.
TGHorizontalFrame * fHf
horizontal frame
void SetHistAdd(Bool_t add=kTRUE)
The function SetHistAdd() is needed for a standalone TApplication to log the TGCommandPlugin commands...
Bool_t fHistAdd
flag to add commands to history
TGTextBuffer * fCommandBuf
command text buffer
Int_t fPid
current process id
TTimer * fTimer
for local/remote update
void HandleCommand()
Handle command line from the "command" combo box.
TGTextEntry * fCommand
command text entry widget
void HandleTextChanged(const char *)
Handle the text changed events.
TGCommandPlugin(const TGWindow *p, UInt_t w, UInt_t h)
TGCommandPlugin Constructor.
TString fTempString
temporary command string
void ToggleTimer(Bool_t on)
Let user stop the internal timer when there is no need to check for remote.
void HandleTab()
Handle the 'TAB' key events.
Bool_t HandleTimer(TTimer *t) override
Handle timer event.
TGComboBox * fComboCmd
commands combobox
virtual ~TGCommandPlugin()
Destructor.
void CheckRemote(const char *)
Check if actual ROOT session is a remote one or a local one.
TGTextView * fStatus
output capture view
TGLabel * fLabel
"command :" label
Int_t fPos
current history position
void HandleArrows(Int_t keysym)
Handle the 'up' and 'down' arrow key events.
TGDimension GetDefaultSize() const override
std::cout << fWidth << "x" << fHeight << std::endl;
Definition: TGFrame.h:316
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1117
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition: TGFrame.cxx:967
void MapSubwindows() override
Map all sub windows that are part of the composite frame.
Definition: TGFrame.cxx:1164
void Layout() override
Layout the elements of the composite frame.
Definition: TGFrame.cxx:1257
void SetCleanup(Int_t mode=kLocalCleanup) override
Turn on automatic cleanup of child frames in dtor.
Definition: TGFrame.cxx:1072
void Resize(UInt_t w=0, UInt_t h=0) override
Resize the frame.
Definition: TGFrame.cxx:605
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition: TGFrame.cxx:709
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:191
void MapWindow() override
map window
Definition: TGFrame.h:204
A composite frame that layout their children in horizontal way.
Definition: TGFrame.h:385
This class handles GUI labels.
Definition: TGLabel.h:24
virtual void SetTextColor(Pixel_t color, Bool_t global=kFALSE)
Changes text color.
Definition: TGLabel.cxx:362
virtual void SetText(TGString *newText)
Set new text in label.
Definition: TGLabel.cxx:180
This class describes layout hints used by the layout classes.
Definition: TGLayout.h:50
virtual TGLBEntry * GetEntry(Int_t id) const
Returns list box entry with specified id.
Definition: TGListBox.cxx:1373
Defines top level windows that interact with the system Window Manager.
Definition: TGFrame.h:397
const char * GetString() const
Definition: TGString.h:30
const char * GetString() const
Definition: TGTextBuffer.h:32
void Clear(Option_t *option="") override
Clears up the text entry.
TGTextBuffer * GetBuffer() const
Definition: TGTextEntry.h:112
virtual void AppendText(const char *text)
Appends text to the end of text entry, clears the selection and moves the cursor to the end of the li...
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.
Text string listbox entries.
Definition: TGListBox.h:48
const TGString * GetText() const
Definition: TGListBox.h:79
A TGTextView is a text viewer widget.
Definition: TGTextView.h:22
virtual Bool_t LoadFile(const char *fname, long startpos=0, long length=-1)
Load a file in the text view widget.
Definition: TGTextView.cxx:454
virtual void AddLine(const char *string)
Add a line of text to the view widget.
Definition: TGTextView.cxx:229
virtual void SetSelectBack(Pixel_t p)
set selected text background color
Definition: TGTextView.cxx:179
virtual void ShowBottom()
Show bottom of the page.
virtual void SetSelectFore(Pixel_t p)
set selected text color
Definition: TGTextView.cxx:188
ROOT GUI Window base class.
Definition: TGWindow.h:23
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:698
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:449
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:869
Bool_t Disconnect(const char *signal=nullptr, void *receiver=nullptr, const char *slot=nullptr)
Disconnects signal of this object from slot of receiver.
Definition: TQObject.cxx:1027
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:1206
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:2341
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:1713
virtual int GetPid()
Get process id.
Definition: TSystem.cxx:710
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition: TSystem.cxx:1063
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition: TSystem.cxx:888
virtual int Unlink(const char *name)
Unlink, i.e.
Definition: TSystem.cxx:1381
virtual const char * TempDirectory() const
Return a user configured or systemwide directory to create temporary files in.
Definition: TSystem.cxx:1482
Handles synchronous and a-synchronous timer events.
Definition: TTimer.h:51
virtual void TurnOff()
Remove timer from system timer list.
Definition: TTimer.cxx:231
virtual void TurnOn()
Add the timer to the system timer list.
Definition: TTimer.cxx:243
void Reset()
Reset the timer.
Definition: TTimer.cxx:159
TLine * line
static const char * what
Definition: stlLoader.cc:6