Logo ROOT  
Reference Guide
textviewostream.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_gui
3/// This macro gives an example of how to use the TGTextViewostream widget.
4/// Simply type a command in the "Command" text entry, then the output is redirected to theTGTextViewostream.
5/// To run it do either:
6/// ~~~
7/// .x textviewostream.C
8/// .x textviewostream.C++
9/// ~~~
10///
11/// \macro_code
12///
13/// \author Bertrand Bellenot 06/01/2015
14
15#include "TGButton.h"
16#include "TGButtonGroup.h"
17#include "TGLabel.h"
18#include "TGNumberEntry.h"
19#include "TGTextViewStream.h"
20#include "TApplication.h"
21#include "TGFrame.h"
22#include "TSystem.h"
23
24////////////////////////////////////////////////////////////////////////////////
25class TextViewMainFrame : public TGMainFrame
26{
27protected:
28 TGTextButton *fReset, *fExit;
29 TGTextViewostream *fTextView;
30 TGVerticalFrame *fContents;
31 TGHorizontalFrame *fButtons, *fCommandFrame;
32 TGTextEntry *fCommand;
33
34public:
35 TextViewMainFrame();
36 virtual ~TextViewMainFrame() {}
37 void Reset();
38 void HandleReturn();
39
40 ClassDef(TextViewMainFrame, 0)
41};
42
43
44//______________________________________________________________________________
45TextViewMainFrame::TextViewMainFrame() : TGMainFrame(gClient->GetRoot())
46{
47 // Main test window.
48
49 SetCleanup(kDeepCleanup);
50
51 // Contents
52 fContents = new TGVerticalFrame(this);
53 fButtons = new TGHorizontalFrame(fContents);
54
55 // TextView
56 fTextView = new TGTextViewostream(fContents, 500, 300);
57 fContents->AddFrame(fTextView, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY, 5, 5, 5, 0));
58
59 fCommandFrame = new TGHorizontalFrame(fContents);
60 fCommand = new TGTextEntry(fCommandFrame, (const char *)"", 20);
61 fCommand->Connect("ReturnPressed()", "TextViewMainFrame", this, "HandleReturn()");
62 fCommandFrame->AddFrame(new TGLabel(fCommandFrame, "Command: "),
63 new TGLayoutHints(kLHintsCenterY | kLHintsLeft, 5, 5, 5, 5));
64 fCommandFrame->AddFrame(fCommand, new TGLayoutHints(kLHintsExpandX, 5, 5, 5, 5));
65 fContents->AddFrame(fCommandFrame, new TGLayoutHints(kLHintsExpandX, 5, 5, 5, 5));
66
67 // The button for test
68 fReset = new TGTextButton(fButtons, "&Reset");
69 fReset->SetToolTipText("Press to clear the command entry\nand the TGTextView", 200);
70 fReset->Connect("Clicked()", "TextViewMainFrame", this, "Reset()");
71 fButtons->AddFrame(fReset, new TGLayoutHints(kLHintsExpandX | kLHintsTop, 5, 5, 5, 5));
72
73 fExit = new TGTextButton(fButtons, "&Exit");
74 fExit->SetToolTipText("Terminate the application", 200);
75 fButtons->AddFrame(fExit, new TGLayoutHints(kLHintsExpandX | kLHintsTop, 5, 5, 5, 5));
76 fExit->Connect("Pressed()", "TApplication", gApplication, "Terminate()");
77
78 fContents->AddFrame(fButtons, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 0, 0, 0, 0));
79 Connect("CloseWindow()", "TApplication", gApplication, "Terminate()");
80 DontCallClose();
81
82 AddFrame(fContents, new TGLayoutHints(kLHintsTop | kLHintsExpandX | kLHintsExpandY));
83 MapSubwindows();
84 Resize(GetDefaultSize());
85
86 SetWindowName("TGTextView Demo");
87 MapRaised();
88}
89
90//______________________________________________________________________________
91void TextViewMainFrame::Reset()
92{
93 fCommand->Clear();
94 fTextView->Clear();
95}
96
97//______________________________________________________________________________
98void TextViewMainFrame::HandleReturn()
99{
100 std::string line;
101 std::string command = fCommand->GetText();
102 *fTextView << gSystem->GetFromPipe(command.c_str()).Data() << std::endl;
103 fTextView->ShowBottom();
104 fCommand->Clear();
105}
106
107//______________________________________________________________________________
108void textviewostream()
109{
110 // Main program.
111
112 new TextViewMainFrame();
113}
114
115
#define ClassDef(name, id)
Definition: Rtypes.h:326
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
#define gClient
Definition: TGClient.h:166
@ kDeepCleanup
Definition: TGFrame.h:51
@ kLHintsExpandY
Definition: TGLayout.h:38
@ kLHintsLeft
Definition: TGLayout.h:31
@ kLHintsCenterY
Definition: TGLayout.h:35
@ kLHintsTop
Definition: TGLayout.h:34
@ kLHintsExpandX
Definition: TGLayout.h:37
R__EXTERN TSystem * gSystem
Definition: TSystem.h:560
const char * Data() const
Definition: TString.h:364
virtual TString GetFromPipe(const char *command)
Execute command and return output in TString.
Definition: TSystem.cxx:690
TLine * line