Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 332887 - SWT Snippets use incorrect positioning
Summary: SWT Snippets use incorrect positioning
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.7   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Felipe Heidrich CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-18 05:51 EST by Ralf Sternberg CLA
Modified: 2011-01-11 10:02 EST (History)
2 users (show)

See Also:


Attachments
SWT Snippet40 on RAP (2.61 KB, image/png)
2010-12-18 05:55 EST, Ralf Sternberg CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ralf Sternberg CLA 2010-12-18 05:51:59 EST
Quoting from Steve Northover's SWT book, section 4.2: "Very often, the location of the client area within the control is (0, 0), but this is not always the case. Figure 4.3 shows the client area of two group boxes from different platforms, with Windows on the left and GTK on the right. On Windows, the location of the client area is (3, 13). On GTK, it is (0, 0)".

Therefore, portable SWT code should never position widgets relative to (0, 0) but relative to the parent's client area. While on most platforms, the assumption is true, it is not in Eclipse RAP. Unfortunately, a couple of the official SWT snippets show this bad practice, which causes incorrect layouting in RAP. An example is Snippet40:

	Shell shell = new Shell (display);
	Composite c1 = new Composite (shell, SWT.BORDER);
	c1.setSize (100, 100);
	Composite c2 = new Composite (shell, SWT.BORDER);
	c2.setBounds (100, 0, 100, 100);

To be fully portable, this code should be rewritten to:

	Shell shell = new Shell (display);
	Rectangle clientArea = shell.getClientArea();
	Composite c1 = new Composite (shell, SWT.BORDER);
	c1.setBounds (clientArea.x, clientArea.y, 100, 100);
	Composite c2 = new Composite (shell, SWT.BORDER);
	c2.setBounds (clientArea.x + 100, clientArea.y, 100, 100);
Comment 1 Ralf Sternberg CLA 2010-12-18 05:55:22 EST
Created attachment 185474 [details]
SWT Snippet40 on RAP

This screenshot shows how Snippet40 is rendered in RAP. In this case, the Shell's client area starts at (5, 27).
Comment 2 Felipe Heidrich CLA 2010-12-20 10:58:14 EST
Fixed in HEAD, thank you.
Comment 3 Felipe Heidrich CLA 2010-12-20 10:59:12 EST
(In reply to comment #0)
> a couple of the official SWT snippets show this bad practice

which other snippets are wrong ?
Comment 4 Ralf Sternberg CLA 2010-12-21 04:33:11 EST
Thanks for the quick fix! A layout is of course much better than absolute positioning.

(In reply to comment #3)
> which other snippets are wrong ?

Hmm, there are quite a lot, let's start at the beginning (it seems to get better in the newer snippets):
Snippets 3, 4, 7, 9, 11, 12, 17, 18, 19, 20, 21, 22, 26, 32, 34, 35, 36, 41, 44, 45, 46, 47, 49, 52, 53, 54, 55, 56, 57, 58, 59, ...

I'm not 100% sure about Snippet 5, but I think it has the same problem.

BTW, just stumbled upon a "new Text (shell, 0)" in snippet 22, this should probably be replaced by a style flag.