1

As the title shows, I want to add a listener to my rcp user interface in order to detect maximization and minimization. Actually, it not that my real purpose, but I think it is a way to solve my problem. I have a view with some shapes in the center, and I wonna keep the drawing exactly in the center even if the window is resized. To do so, I used the following listener :

public void createPartControl(final Composite parent) {
    display = parent.getDisplay();
    white= display.getSystemColor(SWT.COLOR_WHITE);
    parent.setLayout(new FillLayout(SWT.VERTICAL));
    final ScrolledComposite sc = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    sc.setMinHeight(100);
    sc.setMinWidth(100);
    sc.setSize(565, 305);
    final Composite child = new Composite(sc,SWT.NONE);   
    child.setLayout(new FillLayout());
    // Set child as the scrolled content of the ScrolledComposite
    sc.setContent(child);
    child.setBackground(white);
    gc = new GC(child);
    parent.addListener (SWT.Resize,  new Listener () {
        public void handleEvent (Event e) {
            x = child.getBounds().width/2;
            y = child.getBounds().height/2;
            child.addPaintListener(new PaintListener() {
                public void paintControl(PaintEvent event) {
                    dessin(gc);  // draw my shapes
    }
    });
}

everything goes well except when I maximize the window and then minimize it, in this case I loose the drawing (it is in the corner). Any idea please? I'm I thinking in the right way?

jean24
  • 189
  • 3
  • 9
  • There seem to be missing some vital code in the example. But... when the resize event is seen for the parent, the child need not necessarily be resized yet. – Tonny Madsen Aug 27 '11 at 17:29
  • Oh yeah, you are right, I have to add the listener to the child and not the parent.. thanks – jean24 Aug 28 '11 at 12:33

3 Answers3

3

The two events to detect minimization and un-minimization (not necessarily maximization) are Iconify and Deiconify which only occur on the Shell. See the javadocs for Shell.

the.duckman
  • 6,376
  • 3
  • 23
  • 21
1

In order to keep something in the center of something else all you need is the SWT.Resize event, so this question is a classic case of the XY Problem. (Except that the OP in this case seems to already suspect that this may be an XY Problem.)

However, many people arrive at this question with a legitimate need to programmatically detect window minimized / maximized / restored events, for the following reason:

If you want to be able to save the bounds of your application window on exit, you cannot just save whatever is returned by Shell.getBounds(), because your application may be terminated while minimized or maximized or fullscreen, in which case its bounds should not be persisted. What should be persisted is the minimized/normal/maximized/fullscreen state of the shell, (I call it "posture",) and the bounds of the shell last time its posture was "normal". So, essentially, you need to keep track of when the posture is "normal", and for that you need to have a "posture changed" event.

The problem is that when SWT issues the "deiconified" event, it has not calculated the bounds of the shell yet, so the value that you get in that case is bogus.

So, here is the solution to that:

You are going to need a method which recalculates the posture as follows:

private void recalculatePosture()
{
    Posture posture = swtShell.getFullScreen()? Posture.FULLSCREEN 
        : swtShell.getMinimized()? Posture.MINIMIZED
        : swtShell.getMaximized()? Posture.MAXIMIZED
        : Posture.NORMAL;
    if( posture != previousPosture )
    {
        issue event...
        previousPosture = posture;
    }
}

In order to generate the "maximized", "restored (from maximized)" and "fullscreen" events you can use Shell.addListener() to listen for the SWT.Move and SWT.Resize event, and invoke recalculatePosture() when they occur.

In order to generate the "minimized" event you can use the shellIconified() method of the ShellListener as @the.duckman said, and again, invoke recalculatePosture().

In order to generate the "restored (from minimized)" event, you need to do the following in your ShellListener:

@Override
protected void onShellDeiconified( ShellEvent e )
{
    display.asyncExec( () -> recalculatePosture() );
}

This will cause the recalculation of posture a short time after the 'deiconified' event, at which point SWT will have gotten around to properly calculating the bounds of the shell.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
1

Consider moving the resize event is seen for the parent, as the child need not necessarily be resized yet.

Tonny Madsen
  • 12,628
  • 4
  • 31
  • 70