Logo ROOT  
Reference Guide
guiWithCINT.C File Reference

Detailed Description

A simple example of entering CINT commands and having the CINT output in a ROOT GUI application window. An editable combo box is used as a CINT prompt, a text view widget displays the command output.

#include <iostream>
#include <TApplication.h>
#include <TRint.h>
#include <TROOT.h>
#include <TSystem.h>
#include <TGTextEntry.h>
#include <TGTextView.h>
#include <TGClient.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TGLayout.h>
#include <TGWindow.h>
#include <TGLabel.h>
#include <TString.h>
#include <TGComboBox.h>
#include <Getline.h>
class IDList {
private:
Int_t fID; //create widget Id(s)
public:
IDList() : fID(0) {}
~IDList() {}
Int_t GetUnID(void) { return ++fID; }
};
class MyApplication : public TGMainFrame {
private:
TGTextButton *fExit;
IDList fIDs;
TGComboBox *fComboCmd; // CINT command combobox
TGTextBuffer *fCommandBuf; // text buffer in use
TGTextEntry *fCommand; // text entry for CINT commands
TGTextView *fTextView; // display CINT output
TString fName; // name of temp created file
public:
MyApplication(const TGWindow *p, UInt_t w, UInt_t h);
virtual ~MyApplication();
void DoExit();
void DoEnteredCommand();
ClassDef(MyApplication, 0)
};
MyApplication::MyApplication(const TGWindow *p, UInt_t w, UInt_t h)
: TGMainFrame(p, w, h)
{
SetCleanup(kDeepCleanup);
Connect("CloseWindow()", "MyApplication", this, "DoExit()");
DontCallClose();
TGHorizontalFrame *fHL2 = new TGHorizontalFrame(this, 70, 100);
AddFrame(fHL2, new TGLayoutHints(kLHintsNormal, 5, 5, 5, 5));
TGLabel *fInlabel = new TGLabel(fHL2, "CINT Prompt:");
fHL2->AddFrame(fInlabel, new TGLayoutHints(kLHintsCenterY));
TGLabel *fOutlabel = new TGLabel(this, "Output Window:");
AddFrame(fOutlabel);
fCommandBuf = new TGTextBuffer(256);
fComboCmd = new TGComboBox(fHL2, "", fIDs.GetUnID());
fCommand = fComboCmd->GetTextEntry();
fComboCmd->Resize(450, fCommand->GetDefaultHeight());
fHL2->AddFrame(fComboCmd, new TGLayoutHints(kLHintsCenterY | kLHintsExpandX, 20,0,0,0));
TString hist(Form("%s/.root_hist", gSystem->UnixPathName(gSystem->HomeDirectory())));
FILE *fhist = fopen(hist.Data(), "rt");
if (fhist) {
char histline[256];
while (fgets(histline, 256, fhist)) {
histline[strlen(histline)-1] = 0; // remove trailing "\n"
fComboCmd->InsertEntry(histline, 0, -1);
}
fclose(fhist);
}
Pixel_t backpxl;
gClient->GetColorByName("#c0c0c0", backpxl);
fTextView = new TGTextView(this, 500, 94, fIDs.GetUnID(), kFixedWidth | kFixedHeight);
fTextView->SetBackground(backpxl);
AddFrame(fTextView, new TGLayoutHints(kLHintsExpandX));
TGHorizontalFrame *fHL3 = new TGHorizontalFrame(this, 70, 150, kFixedWidth);
fExit = new TGTextButton(fHL3, "&Exit", fIDs.GetUnID());
fExit->Connect("Clicked()", "MyApplication", this, "DoExit()");
AddFrame(fHL3, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, 1, 1, 1, 1));
SetWindowName("GUI with CINT Input/Output");
MapSubwindows();
Resize(GetDefaultSize());
MapWindow();
fCommand->Connect("ReturnPressed()", "MyApplication", this, "DoEnteredCommand()");
fName = Form("%soutput.log", gSystem->WorkingDirectory());
};
MyApplication::~MyApplication()
{
// Destructor.
Cleanup();
}
void MyApplication::DoExit()
{
// Close application window.
gSystem->Unlink(fName.Data());
}
void MyApplication::DoEnteredCommand()
{
// Execute the CINT command after the ENTER key was pressed.
const char *command = fCommand->GetTitle();
TString prompt;
if (strlen(command)) {
// form temporary file path
prompt = ((TRint*)gROOT->GetApplication())->GetPrompt();
FILE *cintout = fopen(fName.Data(), "a+t");
if (cintout) {
fputs(Form("%s%s\n",prompt.Data(), command), cintout);
fclose(cintout);
}
gSystem->RedirectOutput(fName.Data(), "a");
gROOT->ProcessLine(command);
fComboCmd->InsertEntry(command, 0, fIDs.GetUnID());
Gl_histadd((char *)command);
fTextView->LoadFile(fName.Data());
if (fTextView->ReturnLineCount() > 10)
fTextView->SetVsbPosition(fTextView->ReturnLineCount());
fCommand->Clear();
} else {
printf("No command entered\n");
}
fTextView->ShowBottom();
}
void guiWithCINT()
{
new MyApplication(gClient->GetRoot(),600,300);
}
Author
Ilka Antcheva 06/07/2007

Definition in file guiWithCINT.C.

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:38
kFixedWidth
@ kFixedWidth
Definition: GuiTypes.h:387
TGMainFrame
Definition: TGFrame.h:444
TString::Data
const char * Data() const
Definition: TString.h:369
Pixel_t
ULong_t Pixel_t
Definition: GuiTypes.h:39
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:423
TObject::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:403
TString
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:45
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:41
h
#define h(i)
Definition: RSha256.hxx:124
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:33
TGButton.h
kDeepCleanup
@ kDeepCleanup
Definition: TGFrame.h:51
kLHintsExpandX
@ kLHintsExpandX
Definition: TGLayout.h:43
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