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());
}
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()));
}
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;
}
}
}
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;
}
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;
}
Aggregations