use of org.eclipse.swt.widgets.Canvas in project eclipse.platform.swt by eclipse.
the class ClipboardExample method createImageTransfer.
void createImageTransfer(Composite copyParent, Composite pasteParent) {
final Image[] copyImage = new Image[] { null };
Label l = new Label(copyParent, SWT.NONE);
// $NON-NLS-1$
l.setText("ImageTransfer:");
GridData data = new GridData();
data.verticalSpan = 2;
l.setLayoutData(data);
final Canvas copyImageCanvas = new Canvas(copyParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
data = new GridData(GridData.FILL_BOTH);
data.verticalSpan = 2;
data.widthHint = HSIZE;
data.heightHint = VSIZE;
copyImageCanvas.setLayoutData(data);
final Point copyOrigin = new Point(0, 0);
final ScrollBar copyHBar = copyImageCanvas.getHorizontalBar();
copyHBar.setEnabled(false);
copyHBar.addListener(SWT.Selection, e -> {
if (copyImage[0] != null) {
int hSelection = copyHBar.getSelection();
int destX = -hSelection - copyOrigin.x;
Rectangle rect = copyImage[0].getBounds();
copyImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
copyOrigin.x = -hSelection;
}
});
final ScrollBar copyVBar = copyImageCanvas.getVerticalBar();
copyVBar.setEnabled(false);
copyVBar.addListener(SWT.Selection, e -> {
if (copyImage[0] != null) {
int vSelection = copyVBar.getSelection();
int destY = -vSelection - copyOrigin.y;
Rectangle rect = copyImage[0].getBounds();
copyImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);
copyOrigin.y = -vSelection;
}
});
copyImageCanvas.addListener(SWT.Paint, e -> {
if (copyImage[0] != null) {
GC gc = e.gc;
gc.drawImage(copyImage[0], copyOrigin.x, copyOrigin.y);
Rectangle rect = copyImage[0].getBounds();
Rectangle client = copyImageCanvas.getClientArea();
int marginWidth = client.width - rect.width;
if (marginWidth > 0) {
gc.fillRectangle(rect.width, 0, marginWidth, client.height);
}
int marginHeight = client.height - rect.height;
if (marginHeight > 0) {
gc.fillRectangle(0, rect.height, client.width, marginHeight);
}
gc.dispose();
}
});
Button openButton = new Button(copyParent, SWT.PUSH);
openButton.setText("Open Image");
openButton.addSelectionListener(widgetSelectedAdapter(e -> {
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setText("Open an image file or cancel");
String string = dialog.open();
if (string != null) {
if (copyImage[0] != null) {
System.out.println("CopyImage");
copyImage[0].dispose();
}
copyImage[0] = new Image(e.display, string);
copyVBar.setEnabled(true);
copyHBar.setEnabled(true);
copyOrigin.x = 0;
copyOrigin.y = 0;
Rectangle rect = copyImage[0].getBounds();
Rectangle client = copyImageCanvas.getClientArea();
copyHBar.setMaximum(rect.width);
copyVBar.setMaximum(rect.height);
copyHBar.setThumb(Math.min(rect.width, client.width));
copyVBar.setThumb(Math.min(rect.height, client.height));
copyImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true);
copyVBar.setSelection(0);
copyHBar.setSelection(0);
copyImageCanvas.redraw();
}
}));
Button b = new Button(copyParent, SWT.PUSH);
b.setText("Copy");
b.addSelectionListener(widgetSelectedAdapter(e -> {
if (copyImage[0] != null) {
status.setText("");
// Fetch ImageData at current zoom and save in the clip-board.
clipboard.setContents(new Object[] { copyImage[0].getImageDataAtCurrentZoom() }, new Transfer[] { ImageTransfer.getInstance() });
} else {
status.setText("No image to copy");
}
}));
final Image[] pasteImage = new Image[] { null };
l = new Label(pasteParent, SWT.NONE);
l.setText("ImageTransfer:");
final Canvas pasteImageCanvas = new Canvas(pasteParent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
data = new GridData(GridData.FILL_BOTH);
data.widthHint = HSIZE;
data.heightHint = VSIZE;
pasteImageCanvas.setLayoutData(data);
final Point pasteOrigin = new Point(0, 0);
final ScrollBar pasteHBar = pasteImageCanvas.getHorizontalBar();
pasteHBar.setEnabled(false);
pasteHBar.addListener(SWT.Selection, e -> {
if (pasteImage[0] != null) {
int hSelection = pasteHBar.getSelection();
int destX = -hSelection - pasteOrigin.x;
Rectangle rect = pasteImage[0].getBounds();
pasteImageCanvas.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
pasteOrigin.x = -hSelection;
}
});
final ScrollBar pasteVBar = pasteImageCanvas.getVerticalBar();
pasteVBar.setEnabled(false);
pasteVBar.addListener(SWT.Selection, e -> {
if (pasteImage[0] != null) {
int vSelection = pasteVBar.getSelection();
int destY = -vSelection - pasteOrigin.y;
Rectangle rect = pasteImage[0].getBounds();
pasteImageCanvas.scroll(0, destY, 0, 0, rect.width, rect.height, false);
pasteOrigin.y = -vSelection;
}
});
pasteImageCanvas.addListener(SWT.Paint, e -> {
if (pasteImage[0] != null) {
GC gc = e.gc;
gc.drawImage(pasteImage[0], pasteOrigin.x, pasteOrigin.y);
Rectangle rect = pasteImage[0].getBounds();
Rectangle client = pasteImageCanvas.getClientArea();
int marginWidth = client.width - rect.width;
if (marginWidth > 0) {
gc.fillRectangle(rect.width, 0, marginWidth, client.height);
}
int marginHeight = client.height - rect.height;
if (marginHeight > 0) {
gc.fillRectangle(0, rect.height, client.width, marginHeight);
}
}
});
b = new Button(pasteParent, SWT.PUSH);
b.setText("Paste");
b.addSelectionListener(widgetSelectedAdapter(e -> {
ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance());
if (imageData != null) {
if (pasteImage[0] != null) {
System.out.println("PasteImage");
pasteImage[0].dispose();
}
status.setText("");
// Consume the ImageData at current zoom as-is.
pasteImage[0] = new Image(e.display, new AutoScaleImageDataProvider(imageData));
pasteVBar.setEnabled(true);
pasteHBar.setEnabled(true);
pasteOrigin.x = 0;
pasteOrigin.y = 0;
Rectangle rect = pasteImage[0].getBounds();
Rectangle client = pasteImageCanvas.getClientArea();
pasteHBar.setMaximum(rect.width);
pasteVBar.setMaximum(rect.height);
pasteHBar.setThumb(Math.min(rect.width, client.width));
pasteVBar.setThumb(Math.min(rect.height, client.height));
pasteImageCanvas.scroll(0, 0, 0, 0, rect.width, rect.height, true);
pasteVBar.setSelection(0);
pasteHBar.setSelection(0);
pasteImageCanvas.redraw();
} else {
status.setText("No image to paste");
}
}));
}
use of org.eclipse.swt.widgets.Canvas in project eclipse.platform.swt by eclipse.
the class AccessibleActionExample method main.
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("Accessible Action Example");
Button button = new Button(shell, SWT.PUSH);
button.setText("Button");
final Canvas customButton = new Canvas(shell, SWT.NONE) {
@Override
public Point computeSize(int wHint, int hHint, boolean changed) {
GC gc = new GC(this);
Point point = gc.stringExtent(buttonText);
gc.dispose();
point.x += MARGIN;
point.y += MARGIN;
return point;
}
};
customButton.addPaintListener(e -> {
Rectangle clientArea = customButton.getClientArea();
Point stringExtent = e.gc.stringExtent(buttonText);
int x = clientArea.x + (clientArea.width - stringExtent.x) / 2;
int y = clientArea.y + (clientArea.height - stringExtent.y) / 2;
e.gc.drawString(buttonText, x, y);
});
customButton.addMouseListener(MouseListener.mouseDownAdapter(e -> {
int actionIndex = (e.button == 1) ? 0 : 1;
customButtonAction(actionIndex);
}));
customButton.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
int modifierKeys = e.stateMask & SWT.MODIFIER_MASK;
if (modifierKeys == SWT.CTRL || modifierKeys == 0) {
if (e.character == '1')
customButtonAction(0);
else if (e.character == '2')
customButtonAction(1);
}
}
});
Accessible accessible = customButton.getAccessible();
accessible.addAccessibleListener(new AccessibleAdapter() {
@Override
public void getName(AccessibleEvent e) {
e.result = buttonText;
}
@Override
public void getKeyboardShortcut(AccessibleEvent e) {
// default action is 'action 1'
e.result = "CTRL+1";
}
});
accessible.addAccessibleControlListener(new AccessibleControlAdapter() {
@Override
public void getRole(AccessibleControlEvent e) {
e.detail = ACC.ROLE_PUSHBUTTON;
}
});
accessible.addAccessibleActionListener(new AccessibleActionAdapter() {
@Override
public void getActionCount(AccessibleActionEvent e) {
e.count = 2;
}
@Override
public void getName(AccessibleActionEvent e) {
if (0 <= e.index && e.index <= 1) {
if (e.localized) {
e.result = AccessibleActionExample.getResourceString("action" + e.index);
} else {
// $NON-NLS-1$
e.result = "Action" + e.index;
}
}
}
@Override
public void getDescription(AccessibleActionEvent e) {
if (0 <= e.index && e.index <= 1) {
e.result = AccessibleActionExample.getResourceString("action" + e.index + "description");
}
}
@Override
public void doAction(AccessibleActionEvent e) {
if (0 <= e.index && e.index <= 1) {
customButtonAction(e.index);
e.result = ACC.OK;
}
}
@Override
public void getKeyBinding(AccessibleActionEvent e) {
switch(e.index) {
case 0:
e.result = "1;CTRL+1";
break;
case 1:
e.result = "2;CTRL+2";
break;
default:
e.result = null;
}
}
});
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
use of org.eclipse.swt.widgets.Canvas in project eclipse.platform.swt by eclipse.
the class Test_situational method test_fastStringDrawing.
public void test_fastStringDrawing() {
PerformanceMeter meter = createMeterWithoutSummary("Draw strings using GC.drawString()");
int samples;
for (samples = 0; samples < 10; samples++) {
int width = 640;
int height = 480;
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Canvas c = new Canvas(shell, SWT.NONE);
GridData data = new GridData();
data.widthHint = width;
data.heightHint = height;
c.setLayoutData(data);
shell.pack();
shell.open();
while (display.readAndDispatch()) {
/*empty*/
}
try {
Thread.sleep(2000);
} catch (Exception e) {
}
while (display.readAndDispatch()) {
/*empty*/
}
Color color1 = new Color(display, 0xff, 0, 0xff);
Color color2 = new Color(display, 0, 0xff, 0xff);
Font font1 = new Font(display, "Helvetica", 20, SWT.NONE);
Font font2 = new Font(display, "Helvetica", 10, SWT.BOLD);
String testString = "The quick brown SWT jumped foxily over the lazy dog.";
int x1 = 0, y1 = height / 2, x2 = width / 2, y2 = 0;
meter.start();
GC gc = new GC(c);
for (int i = 0; i < 2000; i++) {
x1 = (x1 + 5) % width;
y1 = (y1 + 5) % height;
x2 = (x2 + 5) % width;
y2 = (y2 + 5) % height;
gc.setFont((i & 1) == 0 ? font1 : font2);
gc.setForeground((i & 1) == 0 ? color1 : color2);
gc.stringExtent(testString);
gc.drawString(testString, x1, y2);
gc.drawString(testString, x1, y1, true);
}
gc.dispose();
meter.stop();
shell.dispose();
color1.dispose();
color2.dispose();
font1.dispose();
font2.dispose();
while (display.readAndDispatch()) {
/*empty*/
}
}
disposeMeter(meter);
}
use of org.eclipse.swt.widgets.Canvas in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_widgets_Canvas method setUp.
@Override
@Before
public void setUp() {
super.setUp();
canvas = new Canvas(shell, 0);
super.setWidget(canvas);
}
use of org.eclipse.swt.widgets.Canvas in project eclipse.platform.swt by eclipse.
the class DNDExample method createDragWidget.
private void createDragWidget(Composite parent) {
parent.setLayout(new FormLayout());
Combo combo = new Combo(parent, SWT.READ_ONLY);
combo.setItems("Toggle Button", "Radio Button", "Checkbox", "Canvas", "Label", "List", "Table", "Tree", "Text", "StyledText", "Combo");
combo.select(LABEL);
dragControlType = combo.getSelectionIndex();
dragControl = createWidget(dragControlType, parent, "Drag Source");
combo.addSelectionListener(widgetSelectedAdapter(e -> {
Object data = dragControl.getLayoutData();
Composite dragParent = dragControl.getParent();
dragControl.dispose();
Combo c = (Combo) e.widget;
dragControlType = c.getSelectionIndex();
dragControl = createWidget(dragControlType, dragParent, "Drag Source");
dragControl.setLayoutData(data);
if (dragEnabled)
createDragSource();
dragParent.layout();
}));
Button b = new Button(parent, SWT.CHECK);
b.setText("DragSource");
b.addSelectionListener(widgetSelectedAdapter(e -> {
Button b1 = (Button) e.widget;
dragEnabled = b1.getSelection();
if (dragEnabled) {
createDragSource();
} else {
if (dragSource != null) {
dragSource.dispose();
}
dragSource = null;
}
}));
b.setSelection(true);
dragEnabled = true;
FormData data = new FormData();
data.top = new FormAttachment(0, 10);
data.bottom = new FormAttachment(combo, -10);
data.left = new FormAttachment(0, 10);
data.right = new FormAttachment(100, -10);
dragControl.setLayoutData(data);
data = new FormData();
data.bottom = new FormAttachment(100, -10);
data.left = new FormAttachment(0, 10);
combo.setLayoutData(data);
data = new FormData();
data.bottom = new FormAttachment(100, -10);
data.left = new FormAttachment(combo, 10);
b.setLayoutData(data);
}
Aggregations