GTK/Win32 diffs with wrapped labels and "pack" beh

SWT Designer allows you to create the views, editors, perspectives, pref pages, composites, etc. that comprise Eclipse SWT & RCP applications and plug-ins.

Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg

GTK/Win32 diffs with wrapped labels and "pack" beh

Postby elz_dad » Fri May 27, 2005 6:12 am

We're trying to build a GUI that will run on both Linux/GTK and Win32. One of the things we want to do is to size a window appropriately based on its contents, but we see different behaviour between the two platforms.

The following program shows two differences between the two platforms (I'm not sure if these have a common cause, but they both cause us a problem).

First, if the program is compiled as-is, then on GTK, the labels containing the text are wrapped, but on Windows they're not. I don't know how the GTK version decides where to wrap the lines, but it seems pretty acceptable.

Second, in both cases the window you get is much larger than necessary, with the controls bunched up in the top left corner, and empty space in the bottom right. Fair enough, there's no specific size mentioned in the program. But if you uncomment the "shell.pack()" call, then with GTK, the window gets shown at a sensible size (just large enough to contain the components). On Windows, the call to "pack" has no obvious effect (you still end up with a huge window).

Incidentally, using SWT designer (which I'm doing on Linux/GTK), neither design nor "preview" mode appears takes no account of the "pack" call, and although the "preview" shows the labels wrapped, the designer doesn't (they just show up as truncated).

I can provide screenshots of any of the above if it helps.

Developing on Linux GTK using Eclipse 3.0.2
WindowBuilderPro 4.0.0 compiled on 2005.05.06 for Eclipse 3.0.0

thanks for any suggestions

nick
Code: Select all

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class AutoSize {
   
    private Button okButton;
    protected Shell shell;
    private Text refreshTimeText;
    int finalValue = 10;
   
    public static void main(String[] args) {
   try {
       AutoSize window = new AutoSize();
       window.open();
   } catch (Exception e) {
       e.printStackTrace();
   }
    }
   
    public void open() {
   final Display display = Display.getDefault();
   createContents();
   shell.open();
   shell.layout();
   while (!shell.isDisposed()) {
       if (!display.readAndDispatch())
      display.sleep();
   }
    }
   
    protected void createContents() {
   shell = new Shell(SWT.CLOSE | SWT.RESIZE);
   final GridLayout gridLayout = new GridLayout();
   gridLayout.verticalSpacing = 6;
   gridLayout.numColumns = 3;
   gridLayout.marginWidth = 12;
   gridLayout.marginHeight = 12;
   gridLayout.horizontalSpacing = 6;
   shell.setLayout(gridLayout);
   
   Label t = new Label(shell, SWT.WRAP);
   t.setText("In this dialog box you can type in a value, but the OK button will only be enabled if the value you type is at least 5.");
   GridData gd = new GridData();
   gd.horizontalSpan = 3;
   t.setLayoutData(gd);
   
   t = new Label(shell, SWT.WRAP);
   t.setText("So feel free to type in a number, any number.  Don't type things that aren't numbers though, as they're not acceptable.");
   gd = new GridData();
   gd.horizontalSpan = 3;
   t.setLayoutData(gd);
   
   new Label(shell, SWT.NONE).setText("Number:");
   
   refreshTimeText = new Text(shell, SWT.BORDER);
   refreshTimeText.setToolTipText("Enter a value between 5 and 100");
   refreshTimeText.setText("10");
   Point p = refreshTimeText.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
   gd = new GridData();
   gd.widthHint = p.x;
   refreshTimeText.setLayoutData(gd);
   refreshTimeText.setText("" + finalValue);
   refreshTimeText.addVerifyListener(new VerifyListener() {
      public void verifyText(VerifyEvent e) {
         
          e.doit = false;
          String text = ((Text) e.widget).getText();
          char c = e.character;
         
          // Allow digits.
          //
          if (Character.isDigit(c) ) {
         e.doit = true;
          }
          // Allow DEL and backspace.
          //
          else if ( c == '\b' || c == 0x7f  ) {
         e.doit = true;
          }
      }
       });
   
   // Enable/disable the OK button depending on whether the text field
   // contains a time within the valid range
   refreshTimeText.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent e) {
          try {
         String text = ((Text)e.widget).getText();
         finalValue = Integer.parseInt(text);
          }
          catch (Exception ex) {
         finalValue = -1;
          }
          okButton.setEnabled((finalValue >= 5) && (finalValue < 100));
         
      }
       });
   new Label(shell, SWT.NONE).setText("seconds");
   
   shell.setText("SWT Application");
   
   okButton = new Button(shell, SWT.NONE);
   okButton.setText("OK");
   
   // On Linux/GTK, this makes the window size itself in a sensible way
   // but on Win32 it seems to have no effect
   // shell.pack();
    }
}

elz_dad
 
Posts: 11
Joined: Fri May 06, 2005 2:40 am

maybe "pack" does work on Windows...

Postby elz_dad » Fri May 27, 2005 7:36 am

My apologies; I just tried again and the "pack" call does appear to have an effect on Win32 after all (I'm sure I saw different behaviour before but can't now reproduce that so perhaps I was mistaken). But the question about wrapping still remains..

nick
elz_dad
 
Posts: 11
Joined: Fri May 06, 2005 2:40 am

Re: GTK/Win32 diffs with wrapped labels and "pack"

Postby Eric Clayberg » Sun May 29, 2005 5:06 am

elz_dad wrote:if the program is compiled as-is, then on GTK, the labels containing the text are wrapped, but on Windows they're not. I don't know how the GTK version decides where to wrap the lines, but it seems pretty acceptable.

I have no idea why the text would wrap differently on both platforms. That would most likey be the result of low-level SWT implementation details on both platforms. It could also be due to the following Eclipse bug...

https://bugs.eclipse.org/bugs/show_bug.cgi?id=9866

SWT does not guarantee that the same code will look exactly the same on different platforms, so there may very well be subtle differences like this.

If you want a detailed answer for why this might be the case, the Eclipse SWT newsgroup is the best place to ask SWT runtime questions.

You might also experiment with different layout managers as that may give you some control over the situation (since the above bug report seems to indicate that GridLayout may not work with wrapped Labels).
Eric Clayberg
Software Engineering Manager
Google
http://code.google.com/webtoolkit/download.html

Author: "Eclipse Plug-ins"
http://www.qualityeclipse.com
Eric Clayberg
Moderator
 
Posts: 4503
Joined: Tue Sep 30, 2003 6:39 am
Location: Boston, MA USA


Return to SWT Designer

Who is online

Users browsing this forum: No registered users and 1 guest