Skip to content

Fix zoom issue during copy paste in clipboard example #2158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.ole.win32.*;
import org.eclipse.swt.internal.win32.*;

Expand Down Expand Up @@ -184,8 +183,9 @@ public Object nativeToJava(TransferData transferData) {
pSourceBits -= scanline;
}
}
Image image = Image.win32_new(null, SWT.BITMAP, memDib, DPIUtil.getNativeDeviceZoom());
ImageData data = image.getImageData (DPIUtil.getDeviceZoom ());
final int DEFAULT_IMAGE_STORAGE_ZOOM = 100;
Image image = Image.win32_new(null, SWT.BITMAP, memDib, DEFAULT_IMAGE_STORAGE_ZOOM);
ImageData data = image.getImageData ();
OS.DeleteObject(memDib);
image.dispose();
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageDataProvider;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
Expand Down Expand Up @@ -62,38 +61,6 @@ public class ClipboardExample {
Label status;
static final int HSIZE = 100, VSIZE = 60;

static final class AutoScaleImageDataProvider implements ImageDataProvider {
ImageData imageData;
int currentZoom;
public AutoScaleImageDataProvider (ImageData data) {
this.imageData = data;
this.currentZoom = getDeviceZoom ();
}

@Override
public ImageData getImageData (int zoom) {
return autoScaleImageData(imageData, zoom, currentZoom);
}

static ImageData autoScaleImageData (ImageData imageData, int targetZoom, int currentZoom) {
if (imageData == null || targetZoom == currentZoom) return imageData;
float scaleFactor = ((float) targetZoom)/((float) currentZoom);
return imageData.scaledTo (Math.round (imageData.width * scaleFactor), Math.round (imageData.height * scaleFactor));
}

static int getDeviceZoom () {
int zoom = 100;
String value = System.getProperty ("org.eclipse.swt.internal.deviceZoom");
if (value != null) {
try {
zoom = Integer.parseInt(value);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
return zoom;
}
}

public static void main( String[] args) {
Display display = new Display();
Expand Down Expand Up @@ -477,7 +444,7 @@ void createImageTransfer(Composite copyParent, Composite pasteParent){
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()});
clipboard.setContents(new Object[] {copyImage[0].getImageData()}, new Transfer[] {ImageTransfer.getInstance()});
} else {
status.setText("No image to copy");
}
Expand Down Expand Up @@ -541,7 +508,7 @@ void createImageTransfer(Composite copyParent, Composite pasteParent){
}
status.setText("");
// Consume the ImageData at current zoom as-is.
pasteImage[0] = new Image(e.display, new AutoScaleImageDataProvider(imageData));
pasteImage[0] = new Image(e.display, imageData);
pasteVBar.setEnabled(true);
pasteHBar.setEnabled(true);
pasteOrigin.x = 0; pasteOrigin.y = 0;
Expand Down
Loading