Skip to content

Commit

Permalink
WIP made image scale to autoScaleProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
amartya4256 committed Aug 16, 2024
1 parent 6047226 commit cf6ade2
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2519,6 +2519,7 @@ void updateButtons() {
if (showMax) {
if (minMaxTb == null) {
minMaxTb = new ToolBar(this, SWT.FLAT);
minMaxTb.setBackground(getDisplay().getSystemColor(SWT.COLOR_DARK_MAGENTA));
initAccessibleMinMaxTb();
addTabControl(minMaxTb, SWT.TRAIL, 0, false);
}
Expand All @@ -2542,6 +2543,7 @@ void updateButtons() {
if (showMin) {
if (minMaxTb == null) {
minMaxTb = new ToolBar(this, SWT.FLAT);
minMaxTb.setBackground(getDisplay().getSystemColor(SWT.COLOR_DARK_MAGENTA));
initAccessibleMinMaxTb();
addTabControl(minMaxTb, SWT.TRAIL, 0, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,7 @@ private void drag(Event dragEvent) {
hwndDrag = 0;
topControl = null;
if (image != null) {
int zoom = nativeZoom;
imagelist = new ImageList(SWT.NONE, zoom);
imagelist = new ImageList(SWT.NONE, nativeZoom);
imagelist.add(image);
topControl = control.getShell();
/*
Expand Down Expand Up @@ -520,7 +519,7 @@ private void drag(Event dragEvent) {
null);
OS.ShowWindow (hwndDrag, OS.SW_SHOW);
}
OS.ImageList_BeginDrag(imagelist.getHandle(zoom), 0, offsetX, event.offsetY);
OS.ImageList_BeginDrag(imagelist.getHandle(nativeZoom), 0, offsetX, event.offsetY);
/*
* Feature in Windows. When ImageList_DragEnter() is called,
* it takes a snapshot of the screen If a drag is started
Expand All @@ -532,8 +531,8 @@ private void drag(Event dragEvent) {
int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
OS.RedrawWindow (topControl.handle, null, 0, flags);
POINT pt = new POINT ();
pt.x = DPIUtil.scaleUp(dragEvent.x, zoom);// To Pixels
pt.y = DPIUtil.scaleUp(dragEvent.y, zoom);// To Pixels
pt.x = DPIUtil.scaleUp(dragEvent.x, nativeZoom);// To Pixels
pt.y = DPIUtil.scaleUp(dragEvent.y, nativeZoom);// To Pixels
OS.MapWindowPoints (control.handle, 0, pt, 1);
RECT rect = new RECT ();
OS.GetWindowRect (hwndDrag, rect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,8 @@ int DragEnter_64(long pDataObject, int grfKeyState, long pt, long pdwEffect) {
}

int DragEnter(long pDataObject, int grfKeyState, int pt_x, int pt_y, long pdwEffect) {
int zoom = nativeZoom;
pt_x = DPIUtil.scaleDown(pt_x, zoom);// To Points
pt_y = DPIUtil.scaleDown(pt_y, zoom);// To Points
pt_x = DPIUtil.scaleDown(pt_x, nativeZoom);// To Points
pt_y = DPIUtil.scaleDown(pt_y, nativeZoom);// To Points
selectedDataType = null;
selectedOperation = DND.DROP_NONE;
if (iDataObject != null) iDataObject.Release();
Expand Down Expand Up @@ -349,9 +348,8 @@ int DragOver_64(int grfKeyState, long pt, long pdwEffect) {
}

int DragOver(int grfKeyState, int pt_x, int pt_y, long pdwEffect) {
int zoom = nativeZoom;
pt_x = DPIUtil.scaleDown(pt_x, zoom);// To Points
pt_y = DPIUtil.scaleDown(pt_y, zoom);// To Points
pt_x = DPIUtil.scaleDown(pt_x, nativeZoom);// To Points
pt_y = DPIUtil.scaleDown(pt_y, nativeZoom);// To Points
if (iDataObject == null) return COM.S_FALSE;
int oldKeyOperation = keyOperation;

Expand Down Expand Up @@ -405,9 +403,8 @@ int Drop_64(long pDataObject, int grfKeyState, long pt, long pdwEffect) {

int Drop(long pDataObject, int grfKeyState, int pt_x, int pt_y, long pdwEffect) {
try {
int zoom = nativeZoom;
pt_x = DPIUtil.scaleDown(pt_x, zoom);// To Points
pt_y = DPIUtil.scaleDown(pt_y, zoom);// To Points
pt_x = DPIUtil.scaleDown(pt_x, nativeZoom);// To Points
pt_y = DPIUtil.scaleDown(pt_y, nativeZoom);// To Points
DNDEvent event = new DNDEvent();
event.widget = this;
event.time = OS.GetMessageTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1369,12 +1369,11 @@ void setBorderSpace(RECT newBorderwidth) {
setBounds();
}
void setBounds() {
int zoom = nativeZoom;
Rectangle area = DPIUtil.scaleDown(frame.getClientArea(), zoom); // To Pixels
setBounds(DPIUtil.scaleDown(borderWidths.left, zoom),
DPIUtil.scaleDown(borderWidths.top, zoom),
DPIUtil.scaleDown(area.width - borderWidths.left - borderWidths.right, zoom),
DPIUtil.scaleDown(area.height - borderWidths.top - borderWidths.bottom, zoom));
Rectangle area = DPIUtil.scaleDown(frame.getClientArea(), nativeZoom); // To Pixels
setBounds(DPIUtil.scaleDown(borderWidths.left, nativeZoom),
DPIUtil.scaleDown(borderWidths.top, nativeZoom),
DPIUtil.scaleDown(area.width - borderWidths.left - borderWidths.right, nativeZoom),
DPIUtil.scaleDown(area.height - borderWidths.top - borderWidths.bottom, nativeZoom));
setObjectRects();
}
private void setExtent(int width, int height){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,9 +976,12 @@ public void drawImage (Image image, int srcX, int srcY, int srcWidth, int srcHei
if (image == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);

int deviceZoom = getZoom();
int deviceZoom = image.useDeviceZoom ? DPIUtil.getZoomForAutoscaleProperty(getZoom()) : getZoom();
Rectangle src = DPIUtil.scaleUp(drawable, new Rectangle(srcX, srcY, srcWidth, srcHeight), deviceZoom);
Rectangle dest = DPIUtil.scaleUp(drawable, new Rectangle(destX, destY, destWidth, destHeight), deviceZoom);
destX = DPIUtil.scaleUp(destX, getZoom());
destY = DPIUtil.scaleUp(destY, getZoom());
destWidth = DPIUtil.scaleUp(destWidth, deviceZoom);
destHeight = DPIUtil.scaleUp(destHeight, deviceZoom);
if (deviceZoom != 100) {
/*
* This is a HACK! Due to rounding errors at fractional scale factors,
Expand All @@ -996,13 +999,13 @@ public void drawImage (Image image, int srcX, int srcY, int srcWidth, int srcHei
}
}
}
drawImage(image, src.x, src.y, src.width, src.height, dest.x, dest.y, dest.width, dest.height, false);
drawImage(image, src.x, src.y, src.width, src.height, destX, destY, destWidth, destHeight, false);
}

void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
if (data.gdipGraphics != 0) {
//TODO - cache bitmap
long [] gdipImage = srcImage.createGdipImage(getZoom());
long [] gdipImage = srcImage.createGdipImage(DPIUtil.getZoomForAutoscaleProperty(getZoom()));
long img = gdipImage[0];
int imgWidth = Gdip.Image_GetWidth(img);
int imgHeight = Gdip.Image_GetHeight(img);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public final class Image extends Resource implements Drawable {

private HashMap<Integer, Long> zoomLevelToHandle = new HashMap<>();

boolean useDeviceZoom = false;

/**
* Prevents uninitialized instances from being created outside the package.
*/
Expand Down Expand Up @@ -256,6 +258,7 @@ public Image(Device device, Image srcImage, int flag) {
this.type = srcImage.type;
this.imageDataProvider = srcImage.imageDataProvider;
this.imageFileNameProvider = srcImage.imageFileNameProvider;
this.useDeviceZoom = srcImage.useDeviceZoom;
this.styleFlag = srcImage.styleFlag | flag;
initialNativeZoom = srcImage.initialNativeZoom;
this.dataAtBaseZoom = srcImage.dataAtBaseZoom;
Expand Down Expand Up @@ -350,6 +353,7 @@ public Image(Device device, Image srcImage, int flag) {
*/
public Image(Device device, Rectangle bounds) {
super(device);
useDeviceZoom = true;
if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
bounds = DPIUtil.scaleUp (bounds, getZoom());
Expand Down Expand Up @@ -492,6 +496,7 @@ public Image(Device device, ImageData source, ImageData mask) {
public Image (Device device, InputStream stream) {
super(device);
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
useDeviceZoom = true;
this.dataAtBaseZoom = new ElementAtZoom<>(new ImageData (stream), 100);
ImageData data = DPIUtil.autoScaleUp(device, this.dataAtBaseZoom);
init(data, getZoom());
Expand Down Expand Up @@ -534,6 +539,7 @@ public Image (Device device, String filename) {
super(device);
if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
useDeviceZoom = true;
this.dataAtBaseZoom = new ElementAtZoom<>(new ImageData (filename), 100);
ImageData data = DPIUtil.autoScaleUp(device, this.dataAtBaseZoom);
init(data, getZoom());
Expand Down Expand Up @@ -573,6 +579,7 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
super(device);
this.imageFileNameProvider = imageFileNameProvider;
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
useDeviceZoom = true;
ElementAtZoom<String> fileName = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, getZoom());
if (fileName.zoom() == getZoom()) {
long handle = initNative (fileName.element(), getZoom());
Expand Down Expand Up @@ -620,6 +627,7 @@ public Image(Device device, ImageFileNameProvider imageFileNameProvider) {
public Image(Device device, ImageDataProvider imageDataProvider) {
super(device);
this.imageDataProvider = imageDataProvider;
useDeviceZoom = true;
initialNativeZoom = DPIUtil.getNativeDeviceZoom();
ElementAtZoom<ImageData> data = DPIUtil.validateAndGetImageDataAtZoom(imageDataProvider, getZoom());
ImageData resizedData = DPIUtil.scaleImageData(device, data.element(), getZoom(), data.zoom());
Expand Down Expand Up @@ -788,6 +796,9 @@ private ImageData applyGrayImageData(ImageData data, int pHeight, int pWidth) {
* @noreference This method is not intended to be referenced by clients.
*/
public static Long win32_getHandle (Image image, int zoom) {
if(image.useDeviceZoom) {
zoom = DPIUtil.getZoomForAutoscaleProperty(zoom);
}
if(image.isDisposed()) {
return image.handle;
}
Expand All @@ -809,7 +820,7 @@ public static Long win32_getHandle (Image image, int zoom) {
image.init(newData, zoom);
}
} else if (image.imageDataProvider != null) {
ElementAtZoom<ImageData> imageCandidate = DPIUtil.validateAndGetImageDataAtZoom (image.imageDataProvider, zoom);
ElementAtZoom<ImageData> imageCandidate = DPIUtil.validateAndGetImageDataAtZoom (image.imageDataProvider, image.getZoom());
ImageData resizedData = DPIUtil.scaleImageData (image.device, imageCandidate.element(), zoom, imageCandidate.zoom());
ImageData newData = image.adaptImageDataIfDisabledOrGray(resizedData);
image.init(newData, zoom);
Expand Down Expand Up @@ -1008,6 +1019,9 @@ long initNative(String filename, int zoom) {
}

long [] createGdipImage(Integer zoom) {
if(useDeviceZoom) {
zoom = DPIUtil.getZoomForAutoscaleProperty(zoom);
}
long handle = Image.win32_getHandle(this, zoom);
switch (type) {
case SWT.BITMAP: {
Expand Down Expand Up @@ -1305,6 +1319,9 @@ public Rectangle getBounds() {
}

Rectangle getBounds(int zoom) {
if(useDeviceZoom) {
zoom = DPIUtil.getZoomForAutoscaleProperty(zoom);
}
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
// Read the bounds in pixels from native layer.
Rectangle bounds = getBoundsInPixelsFromNative();
Expand Down Expand Up @@ -1411,6 +1428,9 @@ public ImageData getImageData() {
* @since 3.106
*/
public ImageData getImageData (int zoom) {
if(useDeviceZoom) {
zoom = DPIUtil.getZoomForAutoscaleProperty(zoom);
}
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
int currentZoom = getZoom();
if (zoom == currentZoom) {
Expand Down Expand Up @@ -2333,6 +2353,9 @@ public void setBackground(Color color) {
}

private int getZoom() {
if(useDeviceZoom) {
return DPIUtil.getZoomForAutoscaleProperty(initialNativeZoom);
}
return initialNativeZoom;
}
/**
Expand Down
Loading

0 comments on commit cf6ade2

Please sign in to comment.