[code]#include “TGClient.h”
#include “TCanvas.h”
#include “TF1.h”
#include “TRandom.h”
#include “TGButton.h”
#include “TGFrame.h”
#include “TRootCanvas.h”
#include “TRootEmbeddedCanvas.h”
#include “TGMenu.h”
class MyMainFrame : public TGMainFrame {
private:
TCanvas *fCanvas;
TGHorizontalFrame *fContainer;
TGTextButton *fToolbarButton, *fStatusbarButton, *fFixedsizeButton;
Bool_t fFixedSize;
public:
MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h);
virtual ~MyMainFrame();
void ToggleToolbar();
void ToggleStatusbar();
void ToggleFixedSize();
};
//______________________________________________________________________________
MyMainFrame::MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h) :
TGMainFrame(p,w,h), fFixedSize(kFALSE)
{
// Create a horizontal frame as canvas container
fContainer = new TGHorizontalFrame(this, 600, 400);
AddFrame(fContainer, new TGLayoutHints(kLHintsExpandX|kLHintsExpandY,2,2,2,2));
MapSubwindows();
// this is where the actual embedding is done
fContainer->SetEditable();
fCanvas = new TCanvas(“Foo”, “Bar”, w, h);
fContainer->SetEditable(kFALSE);
TRootCanvas *rc = (TRootCanvas *)fCanvas->GetCanvasImp();
// remove (hide the menu bar)
TGFrameElement *el = 0;
TIter next(rc->GetList());
while ((el = (TGFrameElement *)next())) {
if (el->fFrame->InheritsFrom(“TGMenuBar”)) {
TGMenuBar *mb = (TGMenuBar *)el->fFrame;
TGCompositeFrame *cf = (TGCompositeFrame *) mb->GetParent();
if (cf) cf->HideFrame(mb);
break;
}
}
// Create a horizontal frame widget with buttons
TGHorizontalFrame *hframe = new TGHorizontalFrame(this,200,40);
fToolbarButton = new TGTextButton(hframe,"&ToolBar");
fToolbarButton->Connect(“Clicked()”,“MyMainFrame”,this,“ToggleToolbar()”);
hframe->AddFrame(fToolbarButton, new TGLayoutHints(kLHintsCenterX, 5,5,3,4));
fStatusbarButton = new TGTextButton(hframe,"&StatusBar");
fStatusbarButton->Connect(“Clicked()”,“MyMainFrame”,this,“ToggleStatusbar()”);
hframe->AddFrame(fStatusbarButton, new TGLayoutHints(kLHintsCenterX, 5,5,3,4));
fFixedsizeButton = new TGTextButton(hframe,"&FixedSize");
fFixedsizeButton->Connect(“Clicked()”,“MyMainFrame”,this,“ToggleFixedSize()”);
hframe->AddFrame(fFixedsizeButton, new TGLayoutHints(kLHintsCenterX, 5,5,3,4));
AddFrame(hframe, new TGLayoutHints(kLHintsCenterX, 2,2,2,2));
// Set a name to the main frame
SetWindowName(“Simple Example”);
// Map all subwindows of main frame
hframe->MapSubwindows();
hframe->MapWindow();
Layout();
// Initialize the layout algorithm
Resize(GetDefaultSize());
// Map main frame
MapWindow();
}
//______________________________________________________________________________
void MyMainFrame::ToggleToolbar()
{
// Toggles the toolbar
fCanvas->ToggleToolBar();
Layout();
cout << "Tool bar: " << fCanvas->GetShowToolBar() << endl;
}
//______________________________________________________________________________
void MyMainFrame::ToggleStatusbar()
{
// Toggles the toolbar
fCanvas->ToggleEventStatus();
Layout();
cout << "Status bar: " << fCanvas->GetShowEventStatus() << endl;
}
//______________________________________________________________________________
void MyMainFrame::ToggleFixedSize()
{
TGFrameElement *fe;
fFixedSize = ! fFixedSize;
int opt = fContainer->GetOptions();
fe = (TGFrameElement *)fContainer->GetFrameElement();
ULong_t lh = fe->fLayout->GetLayoutHints();
if (fFixedSize) {
opt |= kFixedSize;
fContainer->ChangeOptions(opt);
fe->fLayout->SetLayoutHints(kLHintsCenterX|kLHintsCenterY);
} else {
opt &= ~kFixedSize;
fContainer->ChangeOptions(opt);
fe->fLayout->SetLayoutHints(kLHintsExpandX|kLHintsExpandY);
}
Layout();
fContainer->Resize(fContainer->GetDefaultSize());
}
//______________________________________________________________________________
MyMainFrame::~MyMainFrame()
{
// Clean up used widgets: frames, buttons, layout hints
Cleanup();
}
void fixedSizeCanvas()
{
// Popup the GUI…
new MyMainFrame(gClient->GetRoot(),600,400);
}
[/code]