Logo ROOT  
Reference Guide
guiWithCINT.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gui
3/// A simple example of entering CINT commands and having the CINT output in a ROOT GUI application window.
4/// An editable combo box is used as a CINT 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; // CINT command combobox
44 TGTextBuffer *fCommandBuf; // text buffer in use
45 TGTextEntry *fCommand; // text entry for CINT commands
46 TGTextView *fTextView; // display CINT output
47 TString fName; // name of temp created file
48public:
49 MyApplication(const TGWindow *p, UInt_t w, UInt_t h);
50 virtual ~MyApplication();
51
52 void DoExit();
53 void DoEnteredCommand();
54
55 ClassDef(MyApplication, 0)
56};
57
58MyApplication::MyApplication(const TGWindow *p, UInt_t w, UInt_t h)
59 : TGMainFrame(p, w, h)
60{
61 SetCleanup(kDeepCleanup);
62
63 Connect("CloseWindow()", "MyApplication", this, "DoExit()");
64 DontCallClose();
65
66 TGHorizontalFrame *fHL2 = new TGHorizontalFrame(this, 70, 100);
67 AddFrame(fHL2, new TGLayoutHints(kLHintsNormal, 5, 5, 5, 5));
68 TGLabel *fInlabel = new TGLabel(fHL2, "CINT Prompt:");
69 fHL2->AddFrame(fInlabel, new TGLayoutHints(kLHintsCenterY));
70
71 TGLabel *fOutlabel = new TGLabel(this, "Output Window:");
72 AddFrame(fOutlabel);
73
74 fCommandBuf = new TGTextBuffer(256);
75 fComboCmd = new TGComboBox(fHL2, "", fIDs.GetUnID());
76 fCommand = fComboCmd->GetTextEntry();
77 fComboCmd->Resize(450, fCommand->GetDefaultHeight());
78 fHL2->AddFrame(fComboCmd, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 20,0,0,0));
79
80 TString hist(Form("%s/.root_hist", gSystem->UnixPathName(gSystem->HomeDirectory())));
81 FILE *fhist = fopen(hist.Data(), "rt");
82 if (fhist) {
83 char histline[256];
84 while (fgets(histline, 256, fhist)) {
85 histline[strlen(histline)-1] = 0; // remove trailing "\n"
86 fComboCmd->InsertEntry(histline, 0, -1);
87 }
88 fclose(fhist);
89 }
90
91 Pixel_t backpxl;
92 gClient->GetColorByName("#c0c0c0", backpxl);
93 fTextView = new TGTextView(this, 500, 94, fIDs.GetUnID(), kFixedWidth | kFixedHeight);
94 fTextView->SetBackground(backpxl);
95 AddFrame(fTextView, new TGLayoutHints(kLHintsExpandX));
96 TGHorizontalFrame *fHL3 = new TGHorizontalFrame(this, 70, 150, kFixedWidth);
97 fExit = new TGTextButton(fHL3, "&Exit", fIDs.GetUnID());
98 fExit->Connect("Clicked()", "MyApplication", this, "DoExit()");
99 fHL3->AddFrame(fExit, new TGLayoutHints(kLHintsExpandX));
100 AddFrame(fHL3, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 1, 1, 1, 1));
101
102 SetWindowName("GUI with CINT Input/Output");
103 MapSubwindows();
104 Resize(GetDefaultSize());
105 MapWindow();
106 fCommand->Connect("ReturnPressed()", "MyApplication", this, "DoEnteredCommand()");
107 fName = Form("%soutput.log", gSystem->WorkingDirectory());
108};
109
110MyApplication::~MyApplication()
111{
112 // Destructor.
113
114 Cleanup();
115}
116
117void MyApplication::DoExit()
118{
119 // Close application window.
120
121 gSystem->Unlink(fName.Data());
123}
124
125void MyApplication::DoEnteredCommand()
126{
127 // Execute the CINT command after the ENTER key was pressed.
128
129 const char *command = fCommand->GetTitle();
130 TString prompt;
131
132 if (strlen(command)) {
133 // form temporary file path
134 prompt = ((TRint*)gROOT->GetApplication())->GetPrompt();
135 FILE *cintout = fopen(fName.Data(), "a+t");
136 if (cintout) {
137 fputs(Form("%s%s\n",prompt.Data(), command), cintout);
138 fclose(cintout);
139 }
140 gSystem->RedirectOutput(fName.Data(), "a");
141 gROOT->ProcessLine(command);
142 fComboCmd->InsertEntry(command, 0, fIDs.GetUnID());
143 Gl_histadd((char *)command);
145 fTextView->LoadFile(fName.Data());
146 if (fTextView->ReturnLineCount() > 10)
147 fTextView->SetVsbPosition(fTextView->ReturnLineCount());
148 fCommand->Clear();
149 } else {
150 printf("No command entered\n");
151 }
152 fTextView->ShowBottom();
153}
154
155void guiWithCINT()
156{
157 new MyApplication(gClient->GetRoot(),600,300);
158}
159
@ kFixedWidth
Definition: GuiTypes.h:387
@ kFixedHeight
Definition: GuiTypes.h:389
ULong_t Pixel_t
Definition: GuiTypes.h:39
#define h(i)
Definition: RSha256.hxx:106
int Int_t
Definition: RtypesCore.h:43
unsigned int UInt_t
Definition: RtypesCore.h:44
#define ClassDef(name, id)
Definition: Rtypes.h:322
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
#define gClient
Definition: TGClient.h:166
@ kDeepCleanup
Definition: TGFrame.h:51
@ kLHintsCenterY
Definition: TGLayout.h:35
@ kLHintsNormal
Definition: TGLayout.h:39
@ kLHintsCenterX
Definition: TGLayout.h:32
@ kLHintsExpandX
Definition: TGLayout.h:37
#define gROOT
Definition: TROOT.h:406
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
Definition: TSystem.h:556
virtual void Terminate(Int_t status=0)
Terminate the application by call TSystem::Exit() unless application has been told to return from Run...
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1101
TString fName
Definition: TGWindow.h:38
Definition: TRint.h:31
Basic string class.
Definition: TString.h:131
const char * Data() const
Definition: TString.h:364
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:1708
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition: TSystem.cxx:1058
virtual const char * WorkingDirectory()
Return working directory.
Definition: TSystem.cxx:867
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition: TSystem.cxx:883
virtual int Unlink(const char *name)
Unlink, i.e.
Definition: TSystem.cxx:1376