Logo ROOT   6.07/09
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 
virtual void Clear(Option_t *="")
Definition: TObject.h:96
virtual void Resize(UInt_t w=0, UInt_t h=0)
Resize the frame.
Definition: TGFrame.cxx:587
virtual const char * WorkingDirectory()
Return working directory.
Definition: TSystem.cxx:866
TH1 * h
Definition: legend2.C:5
virtual void SetBackground(Pixel_t p)
set background color
Definition: TGTextView.cxx:167
virtual const char * HomeDirectory(const char *userName=0)
Return the user&#39;s home directory.
Definition: TSystem.cxx:882
#define gROOT
Definition: TROOT.h:364
Basic string class.
Definition: TString.h:137
#define gClient
Definition: TGClient.h:174
int Int_t
Definition: RtypesCore.h:41
virtual void Terminate(Int_t status=0)
Terminate the application by call TSystem::Exit() unless application has been told to return from Run...
R__EXTERN TApplication * gApplication
Definition: TApplication.h:171
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1037
const char * Data() const
Definition: TString.h:349
virtual int Unlink(const char *name)
Unlink, i.e. remove, a file.
Definition: TSystem.cxx:1346
#define ClassDef(name, id)
Definition: Rtypes.h:254
ULong_t Pixel_t
Definition: GuiTypes.h:41
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:1137
R__EXTERN TSystem * gSystem
Definition: TSystem.h:549
virtual UInt_t GetDefaultHeight() const
Definition: TGFrame.h:389
unsigned int UInt_t
Definition: RtypesCore.h:42
char * Form(const char *fmt,...)
Definition: TRint.h:35
virtual Int_t RedirectOutput(const char *name, const char *mode="a", RedirectHandle_t *h=0)
Redirect standard output (stdout, stderr) to the specified file.
Definition: TSystem.cxx:1677
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:460
virtual TGTextEntry * GetTextEntry() const
Definition: TGComboBox.h:133