How to keep the same ratio between width and height of a Shell when it is resized ? [message #490272] |
Thu, 08 October 2009 03:27  |
Eclipse User |
|
|
|
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 #491691 is a reply to message #491570] |
Thu, 15 October 2009 09:37  |
Eclipse User |
|
|
|
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...
|
|
|
Powered by
FUDForum. Page generated in 0.27394 seconds