Skip to content

Exception when printing to a gc by the control class #501

@al-noori

Description

@al-noori

Description
Printing a control to an image with ImageGcDrawer raises an exception. Thereby, the drawImage method is called repeatedly (see the stacktrace) for a label that uses the image from above. Strange is, that the label is not in the hierarchy of the control that is printed.

Image

Reproduction
Steps to reproduce the behavior:

  1. Replace Snippet292 with the following:
package org.eclipse.swt.snippets;

/*
 * Take a snapshot of a control
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 *
 * @since 3.4
 */
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class Snippet292 {
	public static void main(String[] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setText("Snippet 292");
		final Group group = new Group(shell, SWT.NONE);
		group.setText("Group");
		group.setLayout(new GridLayout());
		final Tree tree = new Tree(group, SWT.BORDER);
		for (int i = 0; i < 5; i++) {
			TreeItem treeItem = new TreeItem(tree, SWT.NONE);
			treeItem.setText("TreeItem " + i);
			for (int j = 0; j < 3; j++) {
				TreeItem subItem = new TreeItem(treeItem, SWT.NONE);
				subItem.setText("SubItem " + i + "-" + j);
			}
			if (i % 3 == 0)
				treeItem.setExpanded(true);
		}
		new Button(group, SWT.PUSH).setText("Button");
		final Label label = new Label(shell, SWT.NONE);
		label.addListener(SWT.Dispose, e -> {
			Image image = label.getImage();
			if (image != null)
				image.dispose();
		});
		Button button = new Button(shell, SWT.PUSH);
		button.setText("Snapshot");
		button.addListener(SWT.Selection, e -> {
			Image image = label.getImage();
			if (image != null)
				image.dispose();
			Rectangle rect = group.getBounds();
			ImageGcDrawer imageGcDrawer = (gc, imageWidth, imageHeight) -> {
				boolean success = group.print(gc);
				if (!success) {
					MessageBox messageBox = new MessageBox(shell, SWT.OK | SWT.PRIMARY_MODAL);
					messageBox.setMessage("Sorry, taking a snapshot is not supported on your platform");
					messageBox.open();
				}
			};
			image = new Image(display, imageGcDrawer, rect.width, rect.height);
			label.setImage(image);
		});
		GridLayout layout = new GridLayout(2, true);
		shell.setLayout(layout);
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		button.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}

Expected Behavior
A clear and concise description of what you expected to happen.

The expected behaviour is that the group prints itself once to the gc as in the current implementation of the snippet.

Necessary configuration:
Enabled flags, monitor setup etc.

Additional knowledge
Any additional information such as solution ideas

Metadata

Metadata

Assignees

Labels

HiDPIA HiDPI-Related Issue or FeatureSWTIssue for SWT

Type

No type

Projects

Status

🏗 In Work: Short

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions