Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 261528

Summary: FigureCanvas doesn't compute size properly with SWT.BORDER style
Product: [Tools] GEF Reporter: Randy Hudson <hudsonr>
Component: GEF-Legacy Draw2dAssignee: Randy Hudson <hudsonr>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: ahunter.eclipse
Version: 3.4   
Target Milestone: 3.5.0 (Galileo) M5   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Randy Hudson CLA 2009-01-19 11:52:04 EST
This is related to the new SWT.BORDER style support from bug 211092.

FigureCanvas#computeSize(...) is only asking the root figure's size, but it should also add space required for the border.

The following modification to HelloWorld shows the problem:

public static void main(String[] args) {

	Display d = new Display();
	Shell shell = new Shell(d);
	shell.setLayout(new FillLayout());
	
	FigureCanvas canvas = new FigureCanvas(shell, SWT.BORDER);
	Label label = new Label("Some text wider than minimum Shell width.");
	canvas.setContents(label);
	label.setBorder(new LineBorder(ColorConstants.green));

	shell.setText("draw2d");
	shell.pack();
	shell.open();
	while (!shell.isDisposed())
		while (!d.readAndDispatch())
			d.sleep();

}
Comment 1 Randy Hudson CLA 2009-01-19 12:00:17 EST
Changed FigureCanvas#computeSize to determine the border size using SWT's reported "trim".  The trim is subtracted from the hints if necessary. Finally, the root figure's preferred size is augmented with trim and then maxed with the hints (according to SWT conventions).

public org.eclipse.swt.graphics.Point computeSize(int wHint, int hHint, boolean changed) {
	// TODO not accounting for scrollbars and trim
+	int borderSize = computeTrim(0, 0, 0, 0).x * -2;
+	if (wHint >= 0)
+		wHint = Math.max(0, wHint - borderSize);
+	if (hHint >= 0)
+		hHint = Math.max(0, hHint - borderSize);
~	Dimension size = getLightweightSystem()
~			.getRootFigure()
~			.getPreferredSize(wHint, hHint)
+			.getExpanded(borderSize, borderSize);
	size.union(new Dimension(wHint, hHint));
	return new org.eclipse.swt.graphics.Point(size.width, size.height);
}

Yuck, I also just noticed that the original code modified the root figure's preferred Dimension, which is potentially returned by reference.
Comment 2 Randy Hudson CLA 2009-01-19 12:06:41 EST
I've just updated the TODO comment in that method as well to reflect the current state of things.