Search in sources :

Example 76 with Canvas

use of org.eclipse.swt.widgets.Canvas in project eclipse.platform.swt by eclipse.

the class GradientDialog method createDialogControls.

/**
 * Creates the controls of the dialog.
 */
public void createDialogControls(final Shell parent) {
    final Display display = parent.getDisplay();
    // message
    Label message = new Label(parent, SWT.NONE);
    message.setText(GraphicsExample.getResourceString("GradientDlgMsg"));
    GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
    gridData.horizontalSpan = 2;
    message.setLayoutData(gridData);
    // default colors are white and black
    if (rgb1 == null || rgb2 == null) {
        rgb1 = display.getSystemColor(SWT.COLOR_WHITE).getRGB();
        rgb2 = display.getSystemColor(SWT.COLOR_BLACK).getRGB();
    }
    // canvas
    canvas = new Canvas(parent, SWT.NONE);
    gridData = new GridData(GridData.FILL_BOTH);
    gridData.widthHint = 200;
    gridData.heightHint = 100;
    canvas.setLayoutData(gridData);
    canvas.addListener(SWT.Paint, e -> {
        Image preview = null;
        Point size = canvas.getSize();
        Color color1 = new Color(display, rgb1);
        Color color2 = new Color(display, rgb2);
        preview = GraphicsExample.createImage(display, color1, color2, size.x, size.y);
        if (preview != null) {
            e.gc.drawImage(preview, 0, 0);
        }
        preview.dispose();
        color1.dispose();
        color2.dispose();
    });
    // composite used for both color buttons
    Composite colorButtonComp = new Composite(parent, SWT.NONE);
    // layout buttons
    RowLayout layout = new RowLayout();
    layout.type = SWT.VERTICAL;
    layout.pack = false;
    colorButtonComp.setLayout(layout);
    // position composite
    gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
    colorButtonComp.setLayoutData(gridData);
    ColorMenu colorMenu = new ColorMenu();
    // color controls: first color
    colorButton1 = new Button(colorButtonComp, SWT.PUSH);
    colorButton1.setText(GraphicsExample.getResourceString("GradientDlgButton1"));
    Color color1 = new Color(display, rgb1);
    Image img1 = GraphicsExample.createImage(display, color1);
    color1.dispose();
    colorButton1.setImage(img1);
    resources.add(img1);
    menu1 = colorMenu.createMenu(parent.getParent(), gb -> {
        rgb1 = gb.getBgColor1().getRGB();
        colorButton1.setImage(gb.getThumbNail());
        if (canvas != null)
            canvas.redraw();
    });
    colorButton1.addListener(SWT.Selection, event -> {
        final Button button = (Button) event.widget;
        final Composite parent1 = button.getParent();
        Rectangle bounds = button.getBounds();
        Point point = parent1.toDisplay(new Point(bounds.x, bounds.y));
        menu1.setLocation(point.x, point.y + bounds.height);
        menu1.setVisible(true);
    });
    // color controls: second color
    colorButton2 = new Button(colorButtonComp, SWT.PUSH);
    colorButton2.setText(GraphicsExample.getResourceString("GradientDlgButton2"));
    Color color2 = new Color(display, rgb2);
    Image img2 = GraphicsExample.createImage(display, color2);
    color2.dispose();
    colorButton2.setImage(img2);
    resources.add(img2);
    menu2 = colorMenu.createMenu(parent.getParent(), gb -> {
        rgb2 = gb.getBgColor1().getRGB();
        colorButton2.setImage(gb.getThumbNail());
        if (canvas != null)
            canvas.redraw();
    });
    colorButton2.addListener(SWT.Selection, event -> {
        final Button button = (Button) event.widget;
        final Composite parent1 = button.getParent();
        Rectangle bounds = button.getBounds();
        Point point = parent1.toDisplay(new Point(bounds.x, bounds.y));
        menu2.setLocation(point.x, point.y + bounds.height);
        menu2.setVisible(true);
    });
    // composite used for ok and cancel buttons
    Composite okCancelComp = new Composite(parent, SWT.NONE);
    // layout buttons
    RowLayout rowLayout = new RowLayout();
    rowLayout.pack = false;
    rowLayout.marginTop = 5;
    okCancelComp.setLayout(rowLayout);
    // position composite
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
    gridData.horizontalSpan = 2;
    okCancelComp.setLayoutData(gridData);
    // OK button
    okButton = new Button(okCancelComp, SWT.PUSH);
    okButton.setText("&OK");
    okButton.addListener(SWT.Selection, event -> {
        returnVal = SWT.OK;
        parent.close();
    });
    // cancel button
    cancelButton = new Button(okCancelComp, SWT.PUSH);
    cancelButton.setText("&Cancel");
    cancelButton.addListener(SWT.Selection, event -> parent.close());
}
Also used : Resource(org.eclipse.swt.graphics.Resource) Shell(org.eclipse.swt.widgets.Shell) Button(org.eclipse.swt.widgets.Button) Image(org.eclipse.swt.graphics.Image) Rectangle(org.eclipse.swt.graphics.Rectangle) Display(org.eclipse.swt.widgets.Display) Point(org.eclipse.swt.graphics.Point) Dialog(org.eclipse.swt.widgets.Dialog) ArrayList(java.util.ArrayList) List(java.util.List) RowLayout(org.eclipse.swt.layout.RowLayout) Color(org.eclipse.swt.graphics.Color) Composite(org.eclipse.swt.widgets.Composite) SWT(org.eclipse.swt.SWT) RGB(org.eclipse.swt.graphics.RGB) Canvas(org.eclipse.swt.widgets.Canvas) GridData(org.eclipse.swt.layout.GridData) Menu(org.eclipse.swt.widgets.Menu) Label(org.eclipse.swt.widgets.Label) GridLayout(org.eclipse.swt.layout.GridLayout) Composite(org.eclipse.swt.widgets.Composite) Canvas(org.eclipse.swt.widgets.Canvas) Color(org.eclipse.swt.graphics.Color) Label(org.eclipse.swt.widgets.Label) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) Image(org.eclipse.swt.graphics.Image) Button(org.eclipse.swt.widgets.Button) RowLayout(org.eclipse.swt.layout.RowLayout) GridData(org.eclipse.swt.layout.GridData) Display(org.eclipse.swt.widgets.Display)

Example 77 with Canvas

use of org.eclipse.swt.widgets.Canvas in project eclipse.platform.swt by eclipse.

the class GraphicsExample method createToolBar.

void createToolBar(final Composite parent) {
    final Display display = parent.getDisplay();
    toolBar = new ToolBar(parent, SWT.FLAT);
    ToolItem back = new ToolItem(toolBar, SWT.PUSH);
    // $NON-NLS-1$
    back.setText(getResourceString("Back"));
    // $NON-NLS-1$
    back.setImage(loadImage(display, "back.gif"));
    back.addListener(SWT.Selection, event -> {
        int index = tabs_in_order.indexOf(tab) - 1;
        if (index < 0)
            index = tabs_in_order.size() - 1;
        setTab(tabs_in_order.get(index));
    });
    ToolItem next = new ToolItem(toolBar, SWT.PUSH);
    // $NON-NLS-1$
    next.setText(getResourceString("Next"));
    // $NON-NLS-1$
    next.setImage(loadImage(display, "next.gif"));
    next.addListener(SWT.Selection, event -> {
        int index = (tabs_in_order.indexOf(tab) + 1) % tabs_in_order.size();
        setTab(tabs_in_order.get(index));
    });
    ColorMenu colorMenu = new ColorMenu();
    // setup items to be contained in the background menu
    colorMenu.setColorItems(true);
    colorMenu.setPatternItems(checkAdvancedGraphics());
    colorMenu.setGradientItems(checkAdvancedGraphics());
    // create the background menu
    backMenu = colorMenu.createMenu(parent, gb -> {
        background = gb;
        backItem.setImage(gb.getThumbNail());
        if (canvas != null)
            canvas.redraw();
    });
    // initialize the background to the first item in the menu
    background = (GraphicsBackground) backMenu.getItem(0).getData();
    // background tool item
    backItem = new ToolItem(toolBar, SWT.PUSH);
    // $NON-NLS-1$
    backItem.setText(getResourceString("Background"));
    backItem.setImage(background.getThumbNail());
    backItem.addListener(SWT.Selection, event -> {
        if (event.widget == backItem) {
            final ToolItem toolItem = (ToolItem) event.widget;
            final ToolBar toolBar = toolItem.getParent();
            Rectangle toolItemBounds = toolItem.getBounds();
            Point point = toolBar.toDisplay(new Point(toolItemBounds.x, toolItemBounds.y));
            backMenu.setLocation(point.x, point.y + toolItemBounds.height);
            backMenu.setVisible(true);
        }
    });
    // double buffer tool item
    dbItem = new ToolItem(toolBar, SWT.CHECK);
    // $NON-NLS-1$
    dbItem.setText(getResourceString("DoubleBuffer"));
    // $NON-NLS-1$
    dbItem.setImage(loadImage(display, "db.gif"));
    dbItem.addListener(SWT.Selection, event -> setDoubleBuffered(dbItem.getSelection()));
}
Also used : Arrays(java.util.Arrays) Path(org.eclipse.swt.graphics.Path) ToolBar(org.eclipse.swt.widgets.ToolBar) Image(org.eclipse.swt.graphics.Image) Rectangle(org.eclipse.swt.graphics.Rectangle) GC(org.eclipse.swt.graphics.GC) Point(org.eclipse.swt.graphics.Point) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ResourceBundle(java.util.ResourceBundle) Composite(org.eclipse.swt.widgets.Composite) SWTException(org.eclipse.swt.SWTException) Canvas(org.eclipse.swt.widgets.Canvas) Text(org.eclipse.swt.widgets.Text) Shell(org.eclipse.swt.widgets.Shell) FormLayout(org.eclipse.swt.layout.FormLayout) Device(org.eclipse.swt.graphics.Device) Pattern(org.eclipse.swt.graphics.Pattern) FormData(org.eclipse.swt.layout.FormData) MissingResourceException(java.util.MissingResourceException) Sash(org.eclipse.swt.widgets.Sash) IOException(java.io.IOException) FormAttachment(org.eclipse.swt.layout.FormAttachment) Display(org.eclipse.swt.widgets.Display) ToolItem(org.eclipse.swt.widgets.ToolItem) Group(org.eclipse.swt.widgets.Group) TreeItem(org.eclipse.swt.widgets.TreeItem) List(java.util.List) RowLayout(org.eclipse.swt.layout.RowLayout) Color(org.eclipse.swt.graphics.Color) Tree(org.eclipse.swt.widgets.Tree) SWT(org.eclipse.swt.SWT) MessageBox(org.eclipse.swt.widgets.MessageBox) Menu(org.eclipse.swt.widgets.Menu) Control(org.eclipse.swt.widgets.Control) InputStream(java.io.InputStream) ToolBar(org.eclipse.swt.widgets.ToolBar) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) ToolItem(org.eclipse.swt.widgets.ToolItem) Point(org.eclipse.swt.graphics.Point) Display(org.eclipse.swt.widgets.Display)

Example 78 with Canvas

use of org.eclipse.swt.widgets.Canvas in project eclipse.platform.swt by eclipse.

the class Tab method refreshLayoutComposite.

/**
 * Refreshes the composite and draws all controls
 * in the layout example.
 */
void refreshLayoutComposite() {
    /* Remove children that are already laid out */
    children = layoutComposite.getChildren();
    for (Control child : children) {
        child.dispose();
    }
    /* Add all children listed in the table */
    TableItem[] items = table.getItems();
    children = new Control[items.length];
    String[] itemValues = new String[] { LayoutExample.getResourceString("Item", new String[] { "1" }), LayoutExample.getResourceString("Item", new String[] { "2" }), LayoutExample.getResourceString("Item", new String[] { "3" }) };
    for (int i = 0; i < items.length; i++) {
        String control = items[i].getText(1);
        String controlName = items[i].getText(0);
        if (control.equals("Button")) {
            Button button = new Button(layoutComposite, SWT.PUSH);
            button.setText(controlName);
            children[i] = button;
        } else if (control.equals("Canvas")) {
            Canvas canvas = new Canvas(layoutComposite, SWT.BORDER);
            children[i] = canvas;
        } else if (control.equals("Combo")) {
            Combo combo = new Combo(layoutComposite, SWT.NONE);
            combo.setItems(itemValues);
            combo.setText(controlName);
            children[i] = combo;
        } else if (control.equals("Composite")) {
            Composite composite = new Composite(layoutComposite, SWT.BORDER);
            children[i] = composite;
        } else if (control.equals("CoolBar")) {
            CoolBar coolBar = new CoolBar(layoutComposite, SWT.NONE);
            ToolBar toolBar = new ToolBar(coolBar, SWT.BORDER);
            ToolItem item = new ToolItem(toolBar, 0);
            item.setText(LayoutExample.getResourceString("Item", new String[] { "1" }));
            item = new ToolItem(toolBar, 0);
            item.setText(LayoutExample.getResourceString("Item", new String[] { "2" }));
            CoolItem coolItem1 = new CoolItem(coolBar, 0);
            coolItem1.setControl(toolBar);
            toolBar = new ToolBar(coolBar, SWT.BORDER);
            item = new ToolItem(toolBar, 0);
            item.setText(LayoutExample.getResourceString("Item", new String[] { "3" }));
            item = new ToolItem(toolBar, 0);
            item.setText(LayoutExample.getResourceString("Item", new String[] { "4" }));
            CoolItem coolItem2 = new CoolItem(coolBar, 0);
            coolItem2.setControl(toolBar);
            Point size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT);
            coolItem1.setSize(coolItem1.computeSize(size.x, size.y));
            coolItem2.setSize(coolItem2.computeSize(size.x, size.y));
            coolBar.setSize(coolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT));
            children[i] = coolBar;
        } else if (control.equals("Group")) {
            Group group = new Group(layoutComposite, SWT.NONE);
            group.setText(controlName);
            children[i] = group;
        } else if (control.equals("Label")) {
            Label label = new Label(layoutComposite, SWT.NONE);
            label.setText(controlName);
            children[i] = label;
        } else if (control.equals("Link")) {
            Link link = new Link(layoutComposite, SWT.NONE);
            link.setText(controlName);
            children[i] = link;
        } else if (control.equals("List")) {
            org.eclipse.swt.widgets.List list = new org.eclipse.swt.widgets.List(layoutComposite, SWT.BORDER);
            list.setItems(itemValues);
            children[i] = list;
        } else if (control.equals("ProgressBar")) {
            ProgressBar progress = new ProgressBar(layoutComposite, SWT.NONE);
            progress.setSelection(50);
            children[i] = progress;
        } else if (control.equals("Scale")) {
            Scale scale = new Scale(layoutComposite, SWT.NONE);
            children[i] = scale;
        } else if (control.equals("Slider")) {
            Slider slider = new Slider(layoutComposite, SWT.NONE);
            children[i] = slider;
        } else if (control.equals("StyledText")) {
            StyledText styledText = new StyledText(layoutComposite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
            styledText.setText(controlName);
            children[i] = styledText;
        } else if (control.equals("Table")) {
            Table table = new Table(layoutComposite, SWT.BORDER);
            table.setLinesVisible(true);
            TableItem item1 = new TableItem(table, 0);
            item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" }));
            TableItem item2 = new TableItem(table, 0);
            item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" }));
            children[i] = table;
        } else if (control.equals("Text")) {
            Text text = new Text(layoutComposite, SWT.BORDER);
            text.setText(controlName);
            children[i] = text;
        } else if (control.equals("ToolBar")) {
            ToolBar toolBar = new ToolBar(layoutComposite, SWT.BORDER);
            ToolItem item1 = new ToolItem(toolBar, 0);
            item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" }));
            ToolItem item2 = new ToolItem(toolBar, 0);
            item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" }));
            children[i] = toolBar;
        } else {
            Tree tree = new Tree(layoutComposite, SWT.BORDER);
            TreeItem item1 = new TreeItem(tree, 0);
            item1.setText(LayoutExample.getResourceString("Item", new String[] { "1" }));
            TreeItem item2 = new TreeItem(tree, 0);
            item2.setText(LayoutExample.getResourceString("Item", new String[] { "2" }));
            children[i] = tree;
        }
    }
}
Also used : Group(org.eclipse.swt.widgets.Group) Slider(org.eclipse.swt.widgets.Slider) TreeItem(org.eclipse.swt.widgets.TreeItem) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) CCombo(org.eclipse.swt.custom.CCombo) Combo(org.eclipse.swt.widgets.Combo) Control(org.eclipse.swt.widgets.Control) CoolBar(org.eclipse.swt.widgets.CoolBar) Button(org.eclipse.swt.widgets.Button) Tree(org.eclipse.swt.widgets.Tree) ArrayList(java.util.ArrayList) List(java.util.List) CoolItem(org.eclipse.swt.widgets.CoolItem) ProgressBar(org.eclipse.swt.widgets.ProgressBar) ToolItem(org.eclipse.swt.widgets.ToolItem) StyledText(org.eclipse.swt.custom.StyledText) Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) Canvas(org.eclipse.swt.widgets.Canvas) Scale(org.eclipse.swt.widgets.Scale) StyledText(org.eclipse.swt.custom.StyledText) Text(org.eclipse.swt.widgets.Text) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) ToolBar(org.eclipse.swt.widgets.ToolBar) Link(org.eclipse.swt.widgets.Link)

Example 79 with Canvas

use of org.eclipse.swt.widgets.Canvas in project eclipse.platform.swt by eclipse.

the class Bug421127_Clipping_is_wrong method showCaseClipping.

private static Composite[] showCaseClipping(Composite main) {
    Composite composite = new Composite(main, SWT.NONE);
    composite.setLayout(new FillLayout());
    Composite left = new Composite(composite, SWT.NONE);
    left.setLayout(new FillLayout(SWT.VERTICAL));
    Composite upperLeft = new Composite(left, SWT.BORDER);
    Composite bottomLeft = new Composite(left, SWT.BORDER);
    Composite right = new Composite(composite, SWT.NONE);
    right.setLayout(new FillLayout(SWT.VERTICAL));
    Composite upperRight = new Composite(right, SWT.BORDER);
    Composite bottomRight = new Composite(right, SWT.BORDER);
    Composite[] squares = { upperLeft, bottomLeft, upperRight, bottomRight };
    for (Composite square : squares) {
        square.setLayout(new FillLayout(SWT.VERTICAL));
        final Canvas canvas = new Canvas(square, SWT.BORDER);
        final Canvas text = new Canvas(square, SWT.BORDER);
        class PaintCanvas implements PaintListener {

            @Override
            public void paintControl(PaintEvent event) {
                paintCanvas(event, canvas);
            }
        }
        class PrintClipping implements PaintListener {

            @Override
            public void paintControl(PaintEvent event) {
                clippingText(event, text);
            }
        }
        canvas.addPaintListener(new PaintCanvas());
        text.addPaintListener(new PrintClipping());
    }
    return squares;
}
Also used : PaintEvent(org.eclipse.swt.events.PaintEvent) Composite(org.eclipse.swt.widgets.Composite) PaintListener(org.eclipse.swt.events.PaintListener) Canvas(org.eclipse.swt.widgets.Canvas) FillLayout(org.eclipse.swt.layout.FillLayout)

Example 80 with Canvas

use of org.eclipse.swt.widgets.Canvas in project eclipse.platform.swt by eclipse.

the class Bug531667_CanvasPrint_does_not_work method canvas.

private static Composite canvas(Display display, Shell shell) {
    Composite composite = new Composite(shell, SWT.NONE);
    composite.setLayout(new FillLayout());
    Canvas canvas = new Canvas(composite, SWT.NONE);
    canvas.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
    Color white = display.getSystemColor(SWT.COLOR_WHITE);
    canvas.addPaintListener(e -> {
        Rectangle clientArea = canvas.getClientArea();
        e.gc.setBackground(white);
        e.gc.fillOval(0, 0, clientArea.width, clientArea.height);
    });
    return composite;
}
Also used : Composite(org.eclipse.swt.widgets.Composite) Canvas(org.eclipse.swt.widgets.Canvas) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) FillLayout(org.eclipse.swt.layout.FillLayout)

Aggregations

Canvas (org.eclipse.swt.widgets.Canvas)111 GridData (org.eclipse.swt.layout.GridData)47 GridLayout (org.eclipse.swt.layout.GridLayout)38 Composite (org.eclipse.swt.widgets.Composite)38 PaintEvent (org.eclipse.swt.events.PaintEvent)36 PaintListener (org.eclipse.swt.events.PaintListener)35 Point (org.eclipse.swt.graphics.Point)32 Rectangle (org.eclipse.swt.graphics.Rectangle)32 Label (org.eclipse.swt.widgets.Label)31 Text (org.eclipse.swt.widgets.Text)24 Button (org.eclipse.swt.widgets.Button)22 Color (org.eclipse.swt.graphics.Color)20 Shell (org.eclipse.swt.widgets.Shell)20 MouseEvent (org.eclipse.swt.events.MouseEvent)18 GC (org.eclipse.swt.graphics.GC)17 FillLayout (org.eclipse.swt.layout.FillLayout)17 Event (org.eclipse.swt.widgets.Event)17 SelectionEvent (org.eclipse.swt.events.SelectionEvent)16 Listener (org.eclipse.swt.widgets.Listener)16 Display (org.eclipse.swt.widgets.Display)15