Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 313357

Summary: Clipping on the GC with Printer device is not working on Win 7
Product: [Eclipse Project] Platform Reporter: Alex Boyko <aboyko>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3    
Version: 3.6   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard: stalebug
Bug Depends on:    
Bug Blocks: 311121    

Description Alex Boyko CLA 2010-05-18 10:01:30 EDT
Try the following snippet:

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.printing.*;

public class TestSnippet {

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.open();
		PrinterData data = Printer.getDefaultPrinterData();
		if (data == null) {
			System.out.println("Warning: No default printer.");
			display.dispose();
			return;
		}
		Printer printer = new Printer(data);
		if (printer.startJob("SWT Printing Snippet")) {
			Color black = printer.getSystemColor(SWT.COLOR_BLACK);
			Color white = printer.getSystemColor(SWT.COLOR_WHITE);
			Rectangle trim = printer.computeTrim(0, 0, 0, 0);
			Point dpi = printer.getDPI();
			int leftMargin = dpi.x + trim.x; // one inch from left side of paper
			if (leftMargin < 0)
				leftMargin = -trim.x; // make sure to print on theprintable area
			int topMargin = dpi.y / 2 + trim.y; // one-half inch from top edge
												// of paper
			if (topMargin < 0)
				topMargin = -trim.y; // make sure to print on the printable area
			GC gc = new GC(printer);
			if (printer.startPage()) {
				gc.setBackground(white);
				gc.setForeground(black);
				float scale = Math.round((float) printer.getDPI().x
						/ display.getDPI().x);
				int size = Math.round(scale * 50);
				int offset = Math.round(scale * 5);
				gc.setClipping(leftMargin, topMargin, size, size);
				gc.drawRectangle(leftMargin, topMargin, size, size);
				gc.setClipping(leftMargin + size + offset, topMargin, size,
						size);
				gc.drawRectangle(leftMargin + size + offset, topMargin, size,
						size);
				printer.endPage();
			}
			gc.dispose();
			printer.endJob();
		}
		printer.dispose();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}

Note that only one rectangle is printed, namely the second one. If the 2nd setClipping(int,int,int,int) call is removed and the first includes the area for both rectangles then it would work I think.
On Win XP it prints both rectangles.

Problem noticed in Draw2D PrintExample. Figures are usually drawn as follows:
1) Clip rectangle == bounds of the figure
2) paint the figure
Appears that only the last figure gets drawn everything else is wiped out for the GC built from Printer device. Works fine if the GC is built from the Display device.
Comment 1 Eclipse Webmaster CLA 2019-09-06 16:06:21 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.
Comment 2 Eclipse Genie CLA 2022-01-07 04:41:21 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.