Search in sources :

Example 6 with Region

use of org.eclipse.swt.graphics.Region in project eclipse.platform.swt by eclipse.

the class Bug529126_TreeMouseDown method main.

// Static =================================================================
public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Custom gradient selection for Tree");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.MULTI | SWT.FULL_SELECTION | SWT.VIRTUAL);
    tree.setHeaderVisible(true);
    tree.setLinesVisible(true);
    final int columnCount = 4;
    for (int i = 0; i < columnCount; i++) {
        final TreeColumn column = new TreeColumn(tree, SWT.NONE);
        column.setText("Column " + i);
    }
    final int itemCount = 5;
    for (int i = 0; i < itemCount; i++) {
        final TreeItem item1 = new TreeItem(tree, SWT.NONE);
        for (int j = 0; j < i; j++) {
            final TreeItem item2 = new TreeItem(item1, SWT.NONE);
            for (int k = 0; k < j; k++) {
                new TreeItem(item2, SWT.NONE);
            }
        }
    }
    tree.addListener(SWT.SetData, event -> {
        final TreeItem item = (TreeItem) event.item;
        final TreeItem parentItem = item.getParentItem();
        final String text;
        if (parentItem != null) {
            final String parentText = (String) parentItem.getData();
            text = parentText + event.index + "/";
        } else {
            text = "/";
        }
        item.setData(text);
    });
    tree.addListener(SWT.PaintItem, event -> {
        final TreeItem item = (TreeItem) event.item;
        final String text = (String) item.getData();
        event.gc.drawText(text + " [" + event.index + "]", event.x, event.y, true);
    });
    /*
		 * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
		 * Therefore, it is critical for performance that these methods be
		 * as efficient as possible.
		 */
    tree.addListener(SWT.EraseItem, event -> {
        event.detail &= ~SWT.HOT;
        if ((event.detail & SWT.SELECTED) != 0) {
            final GC gc = event.gc;
            final Rectangle area = tree.getClientArea();
            /*
				 * If you wish to paint the selection beyond the end of
				 * last column, you must change the clipping region.
				 */
            final int columnCount1 = tree.getColumnCount();
            if (event.index == columnCount1 - 1 || columnCount1 == 0) {
                final int width = area.x + area.width - event.x;
                if (width > 0) {
                    final Region region = new Region();
                    gc.getClipping(region);
                    region.add(event.x, event.y, width, event.height);
                    gc.setClipping(region);
                    region.dispose();
                }
            }
            gc.setAdvanced(true);
            if (gc.getAdvanced()) {
                gc.setAlpha(127);
            }
            final Rectangle rect = event.getBounds();
            final Color foreground = gc.getForeground();
            final Color background = gc.getBackground();
            gc.setForeground(display.getSystemColor(SWT.COLOR_RED));
            gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
            gc.fillGradientRectangle(0, rect.y, 500, rect.height, false);
            // restore colors for subsequent drawing
            gc.setForeground(foreground);
            gc.setBackground(background);
            event.detail &= ~SWT.SELECTED;
        }
    });
    tree.getColumn(0).setWidth(200);
    for (int i = 1; i < columnCount; i++) {
        tree.getColumn(i).pack();
    }
    tree.setSelection(tree.getItem(0));
    tree.addListener(SWT.MouseDown, event -> System.out.println("event = " + event));
    shell.setSize(500, 500);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) TreeItem(org.eclipse.swt.widgets.TreeItem) TreeColumn(org.eclipse.swt.widgets.TreeColumn) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) Tree(org.eclipse.swt.widgets.Tree) Region(org.eclipse.swt.graphics.Region) FillLayout(org.eclipse.swt.layout.FillLayout) GC(org.eclipse.swt.graphics.GC) Display(org.eclipse.swt.widgets.Display)

Example 7 with Region

use of org.eclipse.swt.graphics.Region in project eclipse.platform.swt by eclipse.

the class PaintSurface method hideRubberband.

/**
 * Hides the rubberband (but does not eliminate it).
 * <p>
 * Increments by one the rubberband "hide" nesting count.  The rubberband
 * is hidden from view (but remains active) if it wasn't already hidden.
 * </p>
 */
public void hideRubberband() {
    if (rubberbandHiddenNestingCount++ <= 0) {
        Region region = new Region();
        rubberband.addDamagedRegion(displayFDC, region);
        Rectangle r = region.getBounds();
        paintCanvas.redraw(r.x, r.y, r.width, r.height, true);
        region.dispose();
    }
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) Region(org.eclipse.swt.graphics.Region)

Example 8 with Region

use of org.eclipse.swt.graphics.Region in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_Region method test_subtract$I.

@Test
public void test_subtract$I() {
    Region reg = new Region(display);
    try {
        reg.subtract((int[]) null);
        reg.dispose();
        fail("no exception thrown for subtract a null array");
    } catch (IllegalArgumentException e) {
    }
    reg.dispose();
    try {
        reg.subtract(new int[] {});
        reg.dispose();
        fail("no exception thrown on disposed region");
    } catch (SWTException e) {
    }
    reg.dispose();
    reg = new Region(display);
    reg.add(new int[] { 0, 0, 50, 0, 50, 25, 0, 25 });
    reg.subtract(new int[] { 0, 0, 50, 0, 50, 20, 0, 20 });
    Rectangle box = reg.getBounds();
    reg.dispose();
    Rectangle expected = new Rectangle(0, 20, 50, 5);
    if (!box.equals(expected)) {
        fail("subtract 1 failed - expected: " + expected + " actual: " + box);
    }
    reg = new Region(display);
    reg.add(new int[] { 0, 0, 50, 0, 50, 25, 0, 25 });
    reg.add(new int[] { 0, 25, 50, 25, 50, 40, 0, 40 });
    reg.subtract(new int[] { 0, 25, 50, 25, 50, 40, 0, 40 });
    box = reg.getBounds();
    reg.dispose();
    expected = new Rectangle(0, 0, 50, 25);
    if (!box.equals(expected)) {
        fail("subtract 2 failed - expected: " + expected + " actual: " + box);
    }
}
Also used : SWTException(org.eclipse.swt.SWTException) Rectangle(org.eclipse.swt.graphics.Rectangle) Region(org.eclipse.swt.graphics.Region) Test(org.junit.Test)

Example 9 with Region

use of org.eclipse.swt.graphics.Region in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_Region method test_isEmpty.

@Test
public void test_isEmpty() {
    Region reg = new Region(display);
    Rectangle emptyRect1 = new Rectangle(10, 20, 0, 200);
    Rectangle emptyRect2 = new Rectangle(10, 20, 10, 0);
    Rectangle rect = new Rectangle(10, 20, 50, 100);
    if (!reg.isEmpty()) {
        reg.dispose();
        fail("isEmpty didn't return true on empty region");
    }
    reg.add(emptyRect1);
    if (!reg.isEmpty()) {
        reg.dispose();
        fail("isEmpty didn't return true on empty region");
    }
    reg.add(emptyRect2);
    if (!reg.isEmpty()) {
        reg.dispose();
        fail("isEmpty didn't return true on empty region");
    }
    reg.add(rect);
    if (reg.isEmpty()) {
        reg.dispose();
        fail("isEmpty returned true on non empty region");
    }
    reg.dispose();
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) Region(org.eclipse.swt.graphics.Region) Test(org.junit.Test)

Example 10 with Region

use of org.eclipse.swt.graphics.Region in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_Region method test_add$I.

@Test
public void test_add$I() {
    Region reg = new Region(display);
    try {
        reg.add((int[]) null);
        reg.dispose();
        fail("no exception thrown for adding a null rectangle");
    } catch (IllegalArgumentException e) {
    }
    reg.dispose();
    try {
        reg.add(new int[] {});
        reg.dispose();
        fail("no exception thrown on disposed region");
    } catch (SWTException e) {
    }
    reg.dispose();
    reg = new Region(display);
    reg.add(new int[] { 0, 0, 50, 0, 50, 25, 0, 25 });
    Rectangle box = reg.getBounds();
    reg.dispose();
    Rectangle expected = new Rectangle(0, 0, 50, 25);
    if (!box.equals(expected)) {
        fail("add 1 failed - expected: " + expected + " actual: " + box);
    }
    reg = new Region(display);
    reg.add(new int[] { 0, 0, 50, 0, 50, 25, 0, 25 });
    reg.add(new int[] { 0, 25, 50, 25, 50, 40, 0, 40 });
    box = reg.getBounds();
    reg.dispose();
    expected = new Rectangle(0, 0, 50, 40);
    if (!box.equals(expected)) {
        fail("add 2 failed - expected: " + expected + " actual: " + box);
    }
// reg = new Region(display);
// reg.add(new int[] {10,10, 40,30, 20,60, 5,55});
// box = reg.getBounds();
// reg.dispose();
// expected = new Rectangle (5,10,35,50);
// if (!box.equals(expected)) {
// fail("add 3 failed - expected: "+expected+" actual: "+box);
// }
}
Also used : SWTException(org.eclipse.swt.SWTException) Rectangle(org.eclipse.swt.graphics.Rectangle) Region(org.eclipse.swt.graphics.Region) Test(org.junit.Test)

Aggregations

Region (org.eclipse.swt.graphics.Region)42 Rectangle (org.eclipse.swt.graphics.Rectangle)29 Test (org.junit.Test)21 SWTException (org.eclipse.swt.SWTException)13 Point (org.eclipse.swt.graphics.Point)12 Color (org.eclipse.swt.graphics.Color)8 GC (org.eclipse.swt.graphics.GC)6 Font (org.eclipse.swt.graphics.Font)5 Image (org.eclipse.swt.graphics.Image)5 Cursor (org.eclipse.swt.graphics.Cursor)4 Path (org.eclipse.swt.graphics.Path)4 Display (org.eclipse.swt.widgets.Display)4 Shell (org.eclipse.swt.widgets.Shell)4 FontData (org.eclipse.swt.graphics.FontData)3 Pattern (org.eclipse.swt.graphics.Pattern)3 TextLayout (org.eclipse.swt.graphics.TextLayout)2 Transform (org.eclipse.swt.graphics.Transform)2 Event (org.eclipse.swt.widgets.Event)2 Listener (org.eclipse.swt.widgets.Listener)2 Tree (org.eclipse.swt.widgets.Tree)2