[SOLVED] Signal after canvas resize

Hi all,

Part of my application draws meshes onto a TPad using TPolyLine. This draws fine when the canvas is square, but if the user resizes the window the image becomes distorted as the aspect ratio needs to change. To address this I have a function called from a button to correct the aspect ratio and call Range(x1,y1,x2,y2) on the TPad.

This works fine but it is a pain having to have this button there and doesn’t look very good from the end-user’s point of view. I would like for this function to be called automatically whenever the TCanvas, TPad or TGMainFrame are resized.

I have tried connecting the ProcessedEvent(Event_t*) signal from the TGMainFrame to my aspect ratio method (which then checks that it is a resize event) but this seems to run just before the resize, not just after so the image remains distorted.

I have tried editing the TGFrame::Resize function to add Emit(“message(char*)”,“r”) at the end but it turns out TGFrame::Resize is only called when a resize is implemented via code, not via mouse input.

Any ideas what I can try?

Thanks,
Andy

Hi Andy,

Here is a possible solution (using a single shot timer):

[code]
#include “TCanvas.h”
#include “TRootCanvas.h”
#include “TTimer.h”

TCanvas *c1;

void HandleTimer()
{
Int_t w, h;
static Int_t cw = 0, ch = 0;
w = c1->GetWw(); h = c1->GetWh();
if ((w == cw) && (h == ch)) return;
printf(“After Timer:\n”);
printf(“Canvas Width: %d, Height: %d\n”, c1->GetWw(), c1->GetWh());
printf(“Window Width: %d, Height: %d\n”, c1->GetWindowWidth(), c1->GetWindowHeight());
cw = w; ch = h;
}

void EventProcessed(Event_t *ev)
{
if (ev->fType != kConfigureNotify) return;
printf(“EventProcessed(Event_t *ev):\n”);
printf(“ev->fX: %d\n”, ev->fX);
printf(“ev->fY: %d\n”, ev->fY);
printf(“ev->fWidth: %d\n”, ev->fWidth);
printf(“ev->fHeight: %d\n”, ev->fHeight);
printf(“Before Timer:\n”);
printf(“Canvas Width: %d, Height: %d\n”, c1->GetWw(), c1->GetWh());
printf(“Window Width: %d, Height: %d\n”, c1->GetWindowWidth(), c1->GetWindowHeight());
TTimer::SingleShot(200, 0, 0, “HandleTimer()”);
}

void test_events()
{
c1 = new TCanvas(“c1”, “c1”, 800, 600);
((TRootCanvas )c1->GetCanvasImp())->Connect("ProcessedEvent(Event_t)", 0, 0, “EventProcessed(Event_t*)”);
}[/code]
And FYI, the method called when resizing a main frame with the mouse is TGFrame::HandleConfigureNotify(Event_t *event)

Cheers, Bertrand.

Thanks Bertrand,

The TTimer works great, I just have one worry though. If the end-users machine is too slow is there a risk that the HandleTimer() method could run before the canvas has finished resizing? However, I don’t want to have to set the wait time to be really long to cover this eventuality.
I’m wondering if there is a way to dynamically set a wait time based on the end-user’s machine to ensure a short wait period for high-end machines and a long enough wait for slow machines.

Let me know your thoughts.

Thanks again,
Andy

Hi Andy,

It should not matter too much, but if you want, you can get the machine information with TSystem::GetSysInfo(SysInfo_t *info) and use the fCpuSpeed member of the SysInfo_t struct.

Cheers, Bertrand.

Fantastic, I may be able to do something with that :slight_smile:

Cheers, Bertrand.