站内搜索: 请输入搜索关键词
当前页面: 图书首页 > SWT: The Standard Widget Toolkit

5.16 Coordinate Mapping and Mirroring - SWT: The Standard Widget Toolkit

Previous Section  < Day Day Up >  Next Section

5.16 Coordinate Mapping and Mirroring

Display 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.

map(Control from, Control to, int x, int y) Returns the equivalent point relative to the to control, of the point (x, y) relative to the from control. If either from or to is null, the associated point is treated as being relative to the display.

map(Control from, Control to, Point point) Equivalent to map(from, to, point.x, point.y).

map(Control from, Control to, int x, int y, int width, int height) Returns the equivalent rectangle relative to the to control of the rectangle located at (x, y) relative to the from control, with the same width and height. If either from or to is null, the associated rectangle is treated as being relative to the display.

map(Control from, Control to, Rectangle rectangle) Equivalent to map(from, to, rectangle.x, rectangle.y, rectangle.width, rectangle.height).

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]

[12] A widget that is created with the style SWT.RIGHT_TO_LEFT is mirrored.

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.

graphics/05fig02.gif


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.

graphics/05fig03.gif


    Previous Section  < Day Day Up >  Next Section