Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
guiWithCLING.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gui
3/// A simple example of entering CLING commands and having the CLING output in a ROOT GUI application window.
4/// An editable combo box is used as a CLING prompt, a text view widget displays the command output.
5///
6/// \macro_code
7///
8/// \author Ilka Antcheva 06/07/2007
9
10#include <iostream>
11#include <TApplication.h>
12#include <TRint.h>
13#include <TROOT.h>
14#include <TSystem.h>
15#include <TGTextEntry.h>
16#include <TGTextView.h>
17#include <TGClient.h>
18#include <TGButton.h>
19#include <TGFrame.h>
20#include <TGLayout.h>
21#include <TGWindow.h>
22#include <TGLabel.h>
23#include <TString.h>
24#include <TGComboBox.h>
25#include <Getline.h>
26
27class IDList {
28
29private:
30 Int_t fID; // create widget Id(s)
31
32public:
33 IDList() : fID(0) {}
34 ~IDList() {}
35 Int_t GetUnID(void) { return ++fID; }
36};
37
38class MyApplication : public TGMainFrame {
39
40private:
41 TGTextButton *fExit;
42 IDList fIDs;
43 TGComboBox *fComboCmd; // CLING command combobox
44 TGTextBuffer *fCommandBuf; // text buffer in use
45 TGTextEntry *fCommand; // text entry for CLING commands
46 TGTextView *fTextView; // display CLING output
47 TString fName; // name of temp created file
48public:
49 MyApplication(const TGWindow *p, UInt_t w, UInt_t h);
50 ~MyApplication() override;
51
52 void DoExit();
53 void DoEnteredCommand();
54
55 ClassDefOverride(MyApplication, 0)
56};
57
58MyApplication::MyApplication(const TGWindow *p, UInt_t w, UInt_t h) : TGMainFrame(p, w, h)
59{
60 SetCleanup(kDeepCleanup);
61
62 Connect("CloseWindow()", "MyApplication", this, "DoExit()");
63 DontCallClose();
64
65 TGHorizontalFrame *fHL2 = new TGHorizontalFrame(this, 70, 100);
66 AddFrame(fHL2, new TGLayoutHints(kLHintsNormal, 5, 5, 5, 5));
67 TGLabel *fInlabel = new TGLabel(fHL2, "CLING Prompt:");
68 fHL2->AddFrame(fInlabel, new TGLayoutHints(kLHintsCenterY));
69
70 TGLabel *fOutlabel = new TGLabel(this, "Output Window:");
71 AddFrame(fOutlabel);
72
73 fCommandBuf = new TGTextBuffer(256);
74 fComboCmd = new TGComboBox(fHL2, "", fIDs.GetUnID());
75 fCommand = fComboCmd->GetTextEntry();
76 fComboCmd->Resize(450, fCommand->GetDefaultHeight());
77 fHL2->AddFrame(fComboCmd, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 20, 0, 0, 0));
78
79 TString hist(Form("%s/.root_hist", gSystem->UnixPathName(gSystem->HomeDirectory())));
80 FILE *fhist = fopen(hist.Data(), "rt");
81 if (fhist) {
82 char histline[256];
83 while (fgets(histline, 256, fhist)) {
84 histline[strlen(histline) - 1] = 0; // remove trailing "\n"
85 fComboCmd->InsertEntry(histline, 0, -1);
86 }
87 fclose(fhist);
88 }
89
90 Pixel_t backpxl;
91 gClient->GetColorByName("#c0c0c0", backpxl);
92 fTextView = new TGTextView(this, 500, 94, fIDs.GetUnID(), kFixedWidth | kFixedHeight);
93 fTextView->SetBackground(backpxl);
94 AddFrame(fTextView, new TGLayoutHints(kLHintsExpandX));
95 TGHorizontalFrame *fHL3 = new TGHorizontalFrame(this, 70, 150, kFixedWidth);
96 fExit = new TGTextButton(fHL3, "&Exit", fIDs.GetUnID());
97 fExit->Connect("Clicked()", "MyApplication", this, "DoExit()");
98 fHL3->AddFrame(fExit, new TGLayoutHints(kLHintsExpandX));
99 AddFrame(fHL3, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 1, 1, 1, 1));
100
101 SetWindowName("GUI with CLING Input/Output");
103 Resize(GetDefaultSize());
104 MapWindow();
105 fCommand->Connect("ReturnPressed()", "MyApplication", this, "DoEnteredCommand()");
106 fName = Form("%soutput.log", gSystem->WorkingDirectory());
107};
108
109MyApplication::~MyApplication()
110{
111 // Destructor.
112
113 Cleanup();
114}
115
116void MyApplication::DoExit()
117{
118 // Close application window.
119
120 gSystem->Unlink(fName.Data());
122}
123
124void MyApplication::DoEnteredCommand()
125{
126 // Execute the CLING command after the ENTER key was pressed.
127
128 const char *command = fCommand->GetTitle();
129 TString prompt;
130
131 if (strlen(command)) {
132 // form temporary file path
133 prompt = ((TRint *)gROOT->GetApplication())->GetPrompt();
134 FILE *cintout = fopen(fName.Data(), "a+t");
135 if (cintout) {
136 fputs(Form("%s%s\n", prompt.Data(), command), cintout);
137 fclose(cintout);
138 }
139 gSystem->RedirectOutput(fName.Data(), "a");
140 gROOT->ProcessLine(command);
141 fComboCmd->InsertEntry(command, 0, fIDs.GetUnID());
142 Gl_histadd((char *)command);
143 gSystem->RedirectOutput(nullptr);
144 fTextView->LoadFile(fName.Data());
145 if (fTextView->ReturnLineCount() > 10)
146 fTextView->SetVsbPosition(fTextView->ReturnLineCount());
147 fCommand->Clear();
148 } else {
149 printf("No command entered\n");
150 }
151 fTextView->ShowBottom();
152}
153
154void guiWithCLING()
155{
156 new MyApplication(gClient->GetRoot(), 600, 300);
157}
@ kFixedWidth
Definition GuiTypes.h:387
@ kFixedHeight
Definition GuiTypes.h:389
ULong_t Pixel_t
Pixel value.
Definition GuiTypes.h:40
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
R__EXTERN TApplication * gApplication
#define gClient
Definition TGClient.h:157
@ kDeepCleanup
Definition TGFrame.h:42
@ kLHintsCenterY
Definition TGLayout.h:28
@ kLHintsNormal
Definition TGLayout.h:32
@ kLHintsCenterX
Definition TGLayout.h:25
@ 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 MapSubwindows
#define gROOT
Definition TROOT.h:406
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
virtual void Terminate(Int_t status=0)
Terminate the application by call TSystem::Exit() unless application has been told to return from Run...
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 void AddFrame(TGFrame *f, TGLayoutHints *l=nullptr)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1117
A composite frame that layout their children in horizontal way.
Definition TGFrame.h:385
This class handles GUI labels.
Definition TGLabel.h:24
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:397
A text buffer is used in several widgets, like TGTextEntry, TGFileDialog, etc.
Yield an action as soon as it is clicked.
Definition TGButton.h:142
A TGTextEntry is a one line text input widget.
Definition TGTextEntry.h:24
A TGTextView is a text viewer widget.
Definition TGTextView.h:22
ROOT GUI Window base class.
Definition TGWindow.h:23
TString fName
name of the window used in SavePrimitive()
Definition TGWindow.h:30
Definition TRint.h:31
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
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:1715
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1063
virtual const char * WorkingDirectory()
Return working directory.
Definition TSystem.cxx:871
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition TSystem.cxx:887
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1381