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 
27 class IDList {
28 
29 private:
30  Int_t fID; //create widget Id(s)
31 
32 public:
33  IDList() : fID(0) {}
34  ~IDList() {}
35  Int_t GetUnID(void) { return ++fID; }
36 };
37 
38 class MyApplication : public TGMainFrame {
39 
40 private:
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
48 public:
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 
58 MyApplication::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 
110 MyApplication::~MyApplication()
111 {
112  // Destructor.
113 
114  Cleanup();
115 }
116 
117 void MyApplication::DoExit()
118 {
119  // Close application window.
120 
121  gSystem->Unlink(fName.Data());
123 }
124 
125 void 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 
155 void guiWithCINT()
156 {
157  new MyApplication(gClient->GetRoot(),600,300);
158 }
159 
TGComboBox.h
TSystem::Unlink
virtual int Unlink(const char *name)
Unlink, i.e.
Definition: TSystem.cxx:1379
TGWindow
Definition: TGWindow.h:31
kLHintsCenterX
@ kLHintsCenterX
Definition: TGLayout.h:32
kFixedWidth
@ kFixedWidth
Definition: GuiTypes.h:387
TGMainFrame
Definition: TGFrame.h:443
TString::Data
const char * Data() const
Definition: TString.h:369
Pixel_t
ULong_t Pixel_t
Pixel value.
Definition: GuiTypes.h:40
TGLayout.h
Form
char * Form(const char *fmt,...)
TGLabel
Definition: TGLabel.h:32
TGTextButton
Definition: TGButton.h:142
Int_t
int Int_t
Definition: RtypesCore.h:45
TGHorizontalFrame
Definition: TGFrame.h:422
TString
Basic string class.
Definition: TString.h:136
TString.h
TGWindow.h
TSystem::WorkingDirectory
virtual const char * WorkingDirectory()
Return working directory.
Definition: TSystem.cxx:870
TGTextBuffer
Definition: TGTextBuffer.h:30
TROOT.h
gClient
#define gClient
Definition: TGClient.h:166
TApplication.h
kLHintsNormal
@ kLHintsNormal
Definition: TGLayout.h:39
TRint.h
kFixedHeight
@ kFixedHeight
Definition: GuiTypes.h:389
TSystem::RedirectOutput
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
TSystem.h
kLHintsCenterY
@ kLHintsCenterY
Definition: TGLayout.h:35
h
#define h(i)
Definition: RSha256.hxx:106
TGTextEntry
Definition: TGTextEntry.h:39
TGFrame.h
TGLayoutHints
Definition: TGLayout.h:57
gApplication
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
TApplication::Terminate
virtual void Terminate(Int_t status=0)
TGClient.h
TGComboBox
Definition: TGComboBox.h:67
TGTextView.h
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TGLabel.h
TSystem::UnixPathName
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition: TSystem.cxx:1061
TGTextEntry.h
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TGTextView
Definition: TGTextView.h:34
TGButton.h
kDeepCleanup
@ kDeepCleanup
Definition: TGFrame.h:50
kLHintsExpandX
@ kLHintsExpandX
Definition: TGLayout.h:37
TSystem::HomeDirectory
virtual const char * HomeDirectory(const char *userName=nullptr)
Return the user's home directory.
Definition: TSystem.cxx:886
TGCompositeFrame::AddFrame
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1102
TRint
Definition: TRint.h:31
TGWindow::fName
TString fName
Definition: TGWindow.h:38
gROOT
#define gROOT
Definition: TROOT.h:406