|
|
< Day Day Up > |
|
5.16 Coordinate Mapping and MirroringDisplay provides API to map points and rectangles from one control coordinate system to another. Coordinate mapping is useful when positioning controls with different parents in the same shell. Coordinate mapping is also needed to align controls between shells. The map() method is used to map coordinates. There are several forms of this method.
It is critical to use the map() method for transformations involving rectangles, rather than "doing the math" because of mirroring. When a coordinate system is mirrored, the origin, which is normally in the upper left corner of the control, is moved to the upper right. The x coordinates increase from right to left instead of left to right. Coordinate mirroring is used to support bidirectional languages, such as Hebrew and Arabic.[12]
Here is an example that attempts to align a shell below a push button when the user presses the button. The example does not use map() and fails because the shell is mirrored.
public static void main(String[] args) {
final Display display = new Display();
int style = SWT.SHELL_TRIM | SWT.RIGHT_TO_LEFT;
final Shell shell = new Shell(display, style);
final Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
button.pack();
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
Shell dialog = new Shell(shell, SWT.ON_TOP);
Rectangle rect = button.getBounds();
// WRONG - Transforms the top corner of the
// button (which is in the upper right) to
// the coordinate system of the Display
// which gives a point in the upper right
// but in Display coordinates
Point pt = shell.toDisplay (rect.x, rect.y);
pt.y += rect.height;
dialog.setBounds(pt.x, pt.y, rect.width, 200);
dialog.setVisible(true);
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
Figure 5.2 shows the results of running this code. As you can see, the shell is in the wrong place. Figure 5.2. A Shell that is incorrectly aligned with a button.
The following code uses map() to correct the alignment problem, as shown in Figure 5.3.
public static void main(String[] args) {
final Display display = new Display();
int style = SWT.SHELL_TRIM | SWT.RIGHT_TO_LEFT;
final Shell shell = new Shell(display, style);
final Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
button.pack();
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
Shell dialog = new Shell(shell, SWT.ON_TOP);
Rectangle rect = button.getBounds();
// RIGHT - Transforms the bounds of button
// (which is a rectangle with origin in the
// upper right) to the coordinate system of
// the Display. This gives a rectangle that
// is in the Display coordinates with origin
// on the left
rect = display.map (shell, null, rect);
rect.y += rect.height;
rect.height = 200;
dialog.setBounds(rect);
dialog.setVisible(true);
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
Figure 5.3. A Shell that is correctly aligned with a button.
|
|
|
< Day Day Up > |
|