Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to keep the same ratio between width and height of a Shell when it is resized ?
How to keep the same ratio between width and height of a Shell when it is resized ? [message #490272] Thu, 08 October 2009 07:27 Go to next message
H.ORTIZ  is currently offline H.ORTIZ Friend
Messages: 22
Registered: July 2009
Junior Member
Hi,

I want to allow the users to resize a Shell but I want that the ratio between width and height remains the same.
I tried with a ControlListener but it didn't work.
Here is my snippet:

import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class SizedShell {

private Shell shell;
private int width = 600;
private double ratio = 0.5;

public static void main (String[] args) {

SizedShell sizedShell = new SizedShell();
sizedShell.init();

}

private void init() {
Display display = new Display();
shell = new Shell(display);
shell.setText("Shell");
shell.setSize(width, (int)(ratio*width));

shell.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent event) {
shell.setSize(shell.getBounds().width, (int)(ratio*shell.getBounds().width));
shell.layout(true, true);
}
});

shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) {
display.sleep ();
}
}
display.dispose ();
}
}



Thanks for your help,
Hélène
Re: How to keep the same ratio between width and height of a Shell when it is resized ? [message #490949 is a reply to message #490272] Mon, 12 October 2009 14:58 Go to previous messageGo to next message
H.ORTIZ  is currently offline H.ORTIZ Friend
Messages: 22
Registered: July 2009
Junior Member
Oups I've just realised that my snippet turns into an infinite loop...
Sorry !

Someone has an idea ?
Re: How to keep the same ratio between width and height of a Shell when it is resized ? [message #491251 is a reply to message #490949] Tue, 13 October 2009 19:18 Go to previous messageGo to next message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2151
Registered: July 2009
Senior Member
Hi H
Re: How to keep the same ratio between width and height of a Shell when it is resized ? [message #491570 is a reply to message #491251] Thu, 15 October 2009 06:59 Go to previous messageGo to next message
H.ORTIZ  is currently offline H.ORTIZ Friend
Messages: 22
Registered: July 2009
Junior Member
Grant it seems like there was a problem with your post...
Re: How to keep the same ratio between width and height of a Shell when it is resized ? [message #491606 is a reply to message #491570] Thu, 15 October 2009 08:44 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
H.ORTIZ wrote:
> Grant it seems like there was a problem with your post...

Which one? I could perfectly open the link and test the snippet.

Greetings from Bremen,

Daniel Krügler
Re: How to keep the same ratio between width and height of a Shell when it is resized ? [message #491691 is a reply to message #491570] Thu, 15 October 2009 13:37 Go to previous message
Grant Gayed is currently offline Grant GayedFriend
Messages: 2151
Registered: July 2009
Senior Member
It looks like the Eclipse Community Forum page got confused by the accented
characters in your name, though it had no problem with them in your original
post. I'll log a report for this problem. My response is re-pasted
below...

Your snippet works for me on Windows 2000 and OSX but has problems
elsewhere:

- Any Windows release that shows window content during dragging, so XP and
later: shell visual size flickers between the size that your snippet sets it
to and the size that the current pointer position indicates it "should be"
- Linux: loops infinitely because the window manager does not guarantee to
give a shell with the EXACT size requested by a setSize() invocation, so the
subsequent setSize() calls with the same dimension are not short-circuited
by the "am-I-already-that-size?" check

So I think your snippet is correct in theory, but is having problems as a
result of some window manager behaviours. Unfortunately these are difficult
to work around in app code because window managers can generally do what
they want, and swt's API in this area is minimal. I've done a couple of
quick experiments without success. Is there any way that your resizing can
be done with a Tracker instead of resizing the actual Shell (for an example
of this see
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.sni ppets/src/org/eclipse/swt/snippets/Snippet31.java?view=co
[change the Tracker's creation style to SWT.RESIZE])? If so then you can
re-set the Rectangle on each SWT.Resize callback to maintain the desired
ratio. I don't know of another solution to suggest to make this work on all
platforms.

HTH,
Grant


"H.ORTIZ" <hortiz@objetdirect.com> wrote in message
news:hb6h7q$odd$1@build.eclipse.org...
> Grant it seems like there was a problem with your post...
Previous Topic:Layout problem
Next Topic:Excluding files/folders from problem marking
Goto Forum:
  


Current Time: Sat Aug 24 02:52:21 GMT 2019

Powered by FUDForum. Page generated in 0.02344 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top