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
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
38
39////////////////////////////////////////////////////////////////////////////////
40/// TGCommandPlugin Constructor.
41
43 TGMainFrame(p, w, h)
44{
47 fPos = 0;
48 fTempString = "";
49 fHf = new TGHorizontalFrame(this, 100, 20);
50 fComboCmd = new TGComboBox(fHf, "", 1);
55 kLHintsRight | kLHintsExpandX, 5, 5, 1, 1));
56 fHf->AddFrame(fLabel = new TGLabel(fHf, "Command (local):"),
58 5, 5, 1, 1));
60 kLHintsExpandX, 3, 3, 3, 3));
61 fCommand->Connect("ReturnPressed()", "TGCommandPlugin", this,
62 "HandleCommand()");
63 fCommand->Connect("CursorOutUp()", "TGCommandPlugin", this,
64 "HandleArrows(=kKey_Up)");
65 fCommand->Connect("CursorOutDown()", "TGCommandPlugin", this,
66 "HandleArrows(=kKey_Down)");
67 fCommand->Connect("TabPressed()", "TGCommandPlugin", this,
68 "HandleTab()");
69 fCommand->Connect("TextChanged(const char *)", "TGCommandPlugin", this,
70 "HandleTextChanged(const char *)");
71 fStatus = new TGTextView(this, 10, 100, 1);
72 if (gClient->GetStyle() < 2) {
74 gClient->GetColorByName("#a0a0a0", pxl);
77 }
79 kLHintsExpandX | kLHintsExpandY, 3, 3, 3, 3));
80 fPid = gSystem->GetPid();
81 TString defhist(Form("%s/.root_hist", gSystem->UnixPathName(
83 FILE *lunin = fopen(defhist.Data(), "rt");
84 if (lunin) {
86 char histline[256];
88 while (fgets(histline, 256, lunin))
89 ++linecount;
91 if (linecount > 500) {
92 linecount -= 500;
93 while(--linecount > 0)
94 if (!fgets(histline, 256, lunin))
95 break;
96 }
97 linecount = 0;
98 while (fgets(histline, 256, lunin)) {
99 histline[strlen(histline)-1] = 0; // remove trailing "\n"
101 // limit the history size to 500 lines
102 if (++linecount > 500)
103 break;
104 }
105 fclose(lunin);
106 }
107 fTimer = new TTimer(this, 1000);
108 fTimer->Reset();
109 fTimer->TurnOn();
112 MapWindow();
113}
114
115////////////////////////////////////////////////////////////////////////////////
116/// Destructor.
117
119{
120 TString pathtmp = TString::Format("%s/command.%d.log",
123 fCommand->Disconnect("ReturnPressed()");
124 fCommand->Disconnect("CursorOutUp()");
125 fCommand->Disconnect("CursorOutDown()");
126 fCommand->Disconnect("TabPressed()");
127 fCommand->Disconnect("TextChanged(const char *)");
128 delete fTimer;
129 fTimer = 0;
130 Cleanup();
131}
132
133////////////////////////////////////////////////////////////////////////////////
134/// Check if actual ROOT session is a remote one or a local one.
135
136void TGCommandPlugin::CheckRemote(const char * /*str*/)
137{
138 Pixel_t pxl;
139 TApplication *app = gROOT->GetApplication();
140 if (!app->InheritsFrom("TRint"))
141 return;
142 TString sPrompt = ((TRint*)app)->GetPrompt();
143 Int_t end = sPrompt.Index(":root [", 0);
144 if (end > 0 && end != kNPOS) {
145 // remote session
146 sPrompt.Remove(end);
147 gClient->GetColorByName("#ff0000", pxl);
149 fLabel->SetText(Form("Command (%s):", sPrompt.Data()));
150 }
151 else {
152 // local session
153 gClient->GetColorByName("#000000", pxl);
155 fLabel->SetText("Command (local):");
156 }
157 fHf->Layout();
158}
159
160////////////////////////////////////////////////////////////////////////////////
161/// Handle the 'up' and 'down' arrow key events.
162
164{
166 switch ((EKeySym)keysym) {
167 case kKey_Up:
168 if (fPos < entries-1) ++fPos;
169 break;
170 case kKey_Down:
171 if (fPos > 0) --fPos;
172 break;
173 default:
174 break;
175 }
176 if (fPos > 0) {
177 TGTextLBEntry *te = (TGTextLBEntry *)fComboCmd->GetListBox()->GetEntry(entries-fPos);
178 if (te) {
179 fCommand->SetText(te->GetText()->GetString(), kFALSE);
180 }
181 } else {
182 if (fTempString.Length() > 0)
184 else
185 fCommand->Clear();
186 }
187}
188
189////////////////////////////////////////////////////////////////////////////////
190/// Handle command line from the "command" combo box.
191
193{
194 const char *string = fCommandBuf->GetString();
195 if (strlen(string) > 0) {
196 // form temporary file path
197 TString sPrompt = "root []";
198 TString pathtmp = TString::Format("%s/command.%d.log",
200 TApplication *app = gROOT->GetApplication();
201 if (app->InheritsFrom("TRint"))
202 sPrompt = ((TRint*)gROOT->GetApplication())->GetPrompt();
203 FILE *lunout = fopen(pathtmp.Data(), "a+t");
204 if (lunout) {
205 fputs(Form("%s%s\n",sPrompt.Data(), string), lunout);
206 fclose(lunout);
207 }
208 gSystem->RedirectOutput(pathtmp.Data(), "a");
210 gROOT->ProcessLine(string);
212 fComboCmd->InsertEntry(string, entries, -1);
213 fPos = 0;
214 if (app->InheritsFrom("TRint") || fHistAdd)
215 Gl_histadd((char *)string);
217 fStatus->LoadFile(pathtmp.Data());
219 CheckRemote(string);
220 fCommand->Clear();
222 }
223}
224
225////////////////////////////////////////////////////////////////////////////////
226/// Handle the 'TAB' key events.
227
229{
230 std::string line = fCommandBuf->GetString();
231 std::vector<std::string> result;
232 size_t cur = line.length();
233 gInterpreter->CodeComplete(line, cur, result);
234 if (result.size() == 1) {
235 // when there is only one result, complete the command line input
236 std::string found = result[0];
237 std::string what = line;
238 size_t colon = line.find_last_of("::");
239 if (colon != std::string::npos)
240 what = line.substr(colon+2);
241 size_t pos = found.find(what) + what.length();
242 std::string suffix = found.substr(pos);
243 fCommand->AppendText(suffix.c_str());
244 } else {
245 // otherwise print all results
246 std::string prompt = gInterpreter->GetPrompt();
247 if (prompt.find("root") == std::string::npos)
248 prompt = "root []";
249 prompt += " ";
250 prompt += line;
251 fStatus->AddLine(prompt.c_str());
253 for (auto& res : result) {
254 fStatus->AddLine(res.c_str());
256 }
257 }
258}
259
260////////////////////////////////////////////////////////////////////////////////
261/// Handle the text changed events.
262
264{
266}
267
268////////////////////////////////////////////////////////////////////////////////
269/// Handle timer event.
270
272{
273 if ((fTimer == 0) || (t != fTimer)) return kTRUE;
274 CheckRemote("");
275 return kTRUE;
276}
277
278////////////////////////////////////////////////////////////////////////////////
279/// Let user stop the internal timer when there is no need to check for remote.
280/// or start it again later on if needed. (on=False to stop, on=True to start)
281
283{
284 if (on)
285 fTimer->TurnOn();
286 else
287 fTimer->TurnOff();
288}
289
290////////////////////////////////////////////////////////////////////////////////
291/// The function SetHistAdd() is needed for a standalone TApplication to log the
292/// TGCommandPlugin commands into a ROOT history file.
293/// However, this function has no effect if the user does not explictly set on
294/// his standalone application the name of the ROOT history file.
295/// To log into the default ROOT history file, call this on the user-side of the
296/// code:
297/// Gl_histinit(gEnv->GetValue("Rint.History", gSystem->HomeDirectory()));
298/// Otherwise, replace the argument of Gl_histinit with a text file name you want
299/// to use for application-specific logging.
300
302{
303 fHistAdd = add;
304}
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
unsigned long ULong_t
Unsigned long integer 4 bytes (unsigned long). Size depends on architecture.
Definition RtypesCore.h:69
constexpr Bool_t kFALSE
Definition RtypesCore.h:108
constexpr Ssiz_t kNPOS
The equivalent of std::string::npos for the ROOT class TString.
Definition RtypesCore.h:131
constexpr Bool_t kTRUE
Definition RtypesCore.h:107
R__EXTERN TApplication * gApplication
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#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
#define gROOT
Definition TROOT.h:411
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2495
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
This class creates the ROOT Application Environment that interfaces to the windowing system eventloop...
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
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
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
~TGCommandPlugin() override
Destructor.
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:318
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1109
virtual void Cleanup()
Cleanup and delete all objects contained in this composite frame.
Definition TGFrame.cxx:959
void MapSubwindows() override
Map all sub windows that are part of the composite frame.
Definition TGFrame.cxx:1156
void Layout() override
Layout the elements of the composite frame.
Definition TGFrame.cxx:1249
void SetCleanup(Int_t mode=kLocalCleanup) override
Turn on automatic cleanup of child frames in dtor.
Definition TGFrame.cxx:1064
void Resize(UInt_t w=0, UInt_t h=0) override
Resize the frame.
Definition TGFrame.cxx:597
static Pixel_t GetWhitePixel()
Get white pixel value.
Definition TGFrame.cxx:701
virtual UInt_t GetDefaultHeight() const
Definition TGFrame.h:193
void MapWindow() override
map window
Definition TGFrame.h:206
A composite frame that layout their children in horizontal way.
Definition TGFrame.h:387
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:361
virtual void SetText(TGString *newText)
Set new text in label.
Definition TGLabel.cxx:179
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
Defines top level windows that interact with the system Window Manager.
Definition TGFrame.h:399
const char * GetString() const
void Clear(Option_t *option="") override
Clears up the text entry.
TGTextBuffer * GetBuffer() const
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
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.
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
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:864
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:865
Bool_t Disconnect(const char *signal=nullptr, void *receiver=nullptr, const char *slot=nullptr)
Disconnects signal of this object from slot of receiver.
Definition TRint.h:31
Basic string class.
Definition TString.h:138
Ssiz_t Length() const
Definition TString.h:425
void Clear()
Clear string without changing its capacity.
Definition TString.cxx:1241
const char * Data() const
Definition TString.h:384
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:2384
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:1726
virtual int GetPid()
Get process id.
Definition TSystem.cxx:716
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1073
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition TSystem.cxx:897
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1392
virtual const char * TempDirectory() const
Return a user configured or systemwide directory to create temporary files in.
Definition TSystem.cxx:1493
Handles synchronous and a-synchronous timer events.
Definition TTimer.h:51
virtual void TurnOff()
Remove timer from system timer list.
Definition TTimer.cxx:234
virtual void TurnOn()
Add the timer to the system timer list.
Definition TTimer.cxx:246
void Reset()
Reset the timer.
Definition TTimer.cxx:162
TLine * line
static const char * what
Definition stlLoader.cc:5