Turning off TCanvas auto resize

Hello,

I am currently working on a GUI that has a TCanvas embedded in a frame with its menu bar hidden. I’m working on reconstructing that menu bar in the main window of the GUI. The part that I have a question on is the Auto Resize Canvas under the Options menu. Right now, it doesn’t work. In TRootCanvas, this toggled on and off by the following:

case kOptionAutoResize: { fAutoFit = fAutoFit ? kFALSE : kTRUE; int opt = fCanvasContainer->GetOptions(); if (fAutoFit) { opt &= ~kFixedSize; fOptionMenu->CheckEntry(kOptionAutoResize); } else { opt |= kFixedSize; fOptionMenu->UnCheckEntry(kOptionAutoResize); } fCanvasContainer->ChangeOptions(opt); // in case of autofit this will generate a configure // event for the container and this will force the // update of the TCanvas //Layout(); } Layout(); break;
I noticed that fCanvasContainer is a private member of TRootCanvas and I didn’t see a function that returned it so I’m not sure how to modify this part. I also tried the following (a shot in the dark) and it didn’t work:

case kOptionAutoResize: { TRootCanvas *rac = (TRootCanvas *)fCanvas->GetCanvasImp(); fAutoFit = fAutoFit ? kFALSE : kTRUE; int opt = rac->GetOptions(); if (fAutoFit) { opt &= ~kFixedSize; fOptionMenu->CheckEntry(kOptionAutoResize); } else { opt |= kFixedSize; fOptionMenu->UnCheckEntry(kOptionAutoResize); } rac->ChangeOptions(opt); // in case of autofit this will generate a configure // event for the container and this will force the // update of the TCanvas //Layout(); } Layout(); break;

(fCanvas is my embedded TCanvas). Any suggestions are greatly appreciated. I’m having the same issue with Resize Canvas selection since it also uses fCanvasContainer in the FitCanvas function. Thanks!

Hi,

And what about embedding the canvas in a fixed size composite frame? For example (from one of your previous posts):

[code]#include <TGClient.h>
#include <TCanvas.h>
#include <TF1.h>
#include <TRandom.h>
#include <TGButton.h>
#include <TGFrame.h>
#include <TRootEmbeddedCanvas.h>

class MyMainFrame : public TGMainFrame {
private:
TCanvas *fCanvas;
public:
MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h);
virtual ~MyMainFrame();
void ToggleToolbar();
void ToggleStatusbar();
};

MyMainFrame::MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h) :
TGMainFrame(p,w,h)
{
// Create a horizontal frame as canvas container
TGHorizontalFrame *hcontainer = new TGHorizontalFrame(this, 600, 400, );
AddFrame(hcontainer, new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,2,2,2,2));
// make the composite frame fixed size
hcontainer->ChangeOptions(hcontainer->GetOptions()|kFixedSize);
MapSubwindows();

// this is where the actual embedding is done
hcontainer->SetEditable();
fCanvas = new TCanvas(“Foo”, “Bar”, w, h);
hcontainer->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);
TGTextButton *toolbar = new TGTextButton(hframe,"&ToolBar");
toolbar->Connect(“Clicked()”,“MyMainFrame”,this,“ToggleToolbar()”);
hframe->AddFrame(toolbar, new TGLayoutHints(kLHintsCenterX, 5,5,3,4));
TGTextButton *statusbar = new TGTextButton(hframe,"&StatusBar");
statusbar->Connect(“Clicked()”,“MyMainFrame”,this,“ToggleStatusbar()”);
hframe->AddFrame(statusbar, 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;
}

MyMainFrame::~MyMainFrame() {
// Clean up used widgets: frames, buttons, layout hints
Cleanup();
}

void fixedSizeCanvas() {
// Popup the GUI…
new MyMainFrame(gClient->GetRoot(),600,400);
}
[/code]

Cheers, Bertrand.

Hi Bertrand,

Thank you for the modified code. :slight_smile: My apologies though because I don’t think I explained my question very well or maybe I don’t fully understand the code you gave me. I would like to be able to toggle on and off the Auto resize canvas just as can be done in TBrowser. Right now in my GUI, the auto resize canvas is always on so I’m trying to turn it off, but the end goal is to toggle it on and off.

So in turning it off first, I wanted the canvas frame to expand/retract but keep the histogram inside at a fixed size. Does that make sense?
As far as I understand the macro posted, it fixes the canvas frame size.

Thanks!

[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]

Hi Bertrand,

My apologies for this late reply. Thank you so much for the example. It’s not quite what I was trying to get to, but I’m going to work with it to see if I can get it to work with my GUI. I really appreciate it. Thanks!