Skip to content

Commit 06f1598

Browse files
amartya4256fedejeanne
authored andcommitted
[win32] Support scaling of ImageList per zoom level
This commit adds a method to receive handles for scaled variants of an ImageList for the specified zoom. All calls to the handle of an ImageList is replaced by the added method. Contributes to #62 and #131.
1 parent 29a1795 commit 06f1598

File tree

7 files changed

+73
-44
lines changed

7 files changed

+73
-44
lines changed

bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/DragSource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,8 @@ private void drag(Event dragEvent) {
491491
hwndDrag = 0;
492492
topControl = null;
493493
if (image != null) {
494-
imagelist = new ImageList(SWT.NONE, DPIUtil.getZoomForAutoscaleProperty(nativeZoom));
494+
int zoom = DPIUtil.getZoomForAutoscaleProperty(nativeZoom);
495+
imagelist = new ImageList(SWT.NONE, zoom);
495496
imagelist.add(image);
496497
topControl = control.getShell();
497498
/*
@@ -519,7 +520,7 @@ private void drag(Event dragEvent) {
519520
null);
520521
OS.ShowWindow (hwndDrag, OS.SW_SHOW);
521522
}
522-
OS.ImageList_BeginDrag(imagelist.getHandle(), 0, offsetX, event.offsetY);
523+
OS.ImageList_BeginDrag(imagelist.getHandle(zoom), 0, offsetX, event.offsetY);
523524
/*
524525
* Feature in Windows. When ImageList_DragEnter() is called,
525526
* it takes a snapshot of the screen If a drag is started
@@ -530,7 +531,6 @@ private void drag(Event dragEvent) {
530531
*/
531532
int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
532533
OS.RedrawWindow (topControl.handle, null, 0, flags);
533-
int zoom = DPIUtil.getZoomForAutoscaleProperty(nativeZoom);
534534
POINT pt = new POINT ();
535535
pt.x = DPIUtil.scaleUp(dragEvent.x, zoom);// To Pixels
536536
pt.y = DPIUtil.scaleUp(dragEvent.y, zoom);// To Pixels

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/ImageList.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package org.eclipse.swt.internal;
1515

1616

17+
import java.util.*;
18+
1719
import org.eclipse.swt.*;
1820
import org.eclipse.swt.graphics.*;
1921
import org.eclipse.swt.internal.win32.*;
@@ -23,16 +25,23 @@ public class ImageList {
2325
int style, refCount;
2426
Image [] images;
2527
private final int zoom;
28+
private int width, height;
29+
private int flags;
30+
31+
private HashMap<Integer, Long> zoomToHandle = new HashMap<>();
2632

2733
public ImageList (int style, int zoom) {
2834
this (style, 32, 32, zoom);
2935
}
3036

3137
public ImageList (int style, int width, int height, int zoom) {
3238
this.style = style;
33-
int flags = OS.ILC_MASK | OS.ILC_COLOR32;
34-
if ((style & SWT.RIGHT_TO_LEFT) != 0) flags |= OS.ILC_MIRROR;
35-
handle = OS.ImageList_Create (width, height, flags, 16, 16);
39+
this.flags = OS.ILC_MASK | OS.ILC_COLOR32;
40+
if ((style & SWT.RIGHT_TO_LEFT) != 0) this.flags |= OS.ILC_MIRROR;
41+
this.height = height;
42+
this.width = width;
43+
handle = OS.ImageList_Create (width, height, this.flags, 16, 16);
44+
zoomToHandle.put(zoom, handle);
3645
images = new Image [4];
3746
this.zoom = zoom;
3847
}
@@ -51,7 +60,7 @@ public int add (Image image) {
5160
Rectangle rect = DPIUtil.scaleBounds(image.getBounds(), zoom, 100);
5261
OS.ImageList_SetIconSize (handle, rect.width, rect.height);
5362
}
54-
set (index, image, count);
63+
setForAllHandles(index, image, count);
5564
if (index == images.length) {
5665
Image [] newImages = new Image [images.length + 4];
5766
System.arraycopy (images, 0, newImages, 0, images.length);
@@ -320,8 +329,22 @@ public int getStyle () {
320329
return style;
321330
}
322331

323-
public long getHandle () {
324-
return handle;
332+
public long getHandle(int targetZoom) {
333+
if (!zoomToHandle.containsKey(targetZoom)) {
334+
int scaledWidth = DPIUtil.scaleUp(DPIUtil.scaleDown(width, this.zoom), targetZoom);
335+
int scaledHeight = DPIUtil.scaleUp(DPIUtil.scaleDown(height, this.zoom), targetZoom);
336+
long handle = OS.ImageList_Create(scaledWidth, scaledHeight, flags, 16, 16);
337+
int count = OS.ImageList_GetImageCount(handle);
338+
for (int i = 0; i < images.length; i++) {
339+
Image image = images[i];
340+
if (image != null) {
341+
set(i, image, count, handle, targetZoom);
342+
count++;
343+
}
344+
}
345+
zoomToHandle.put(targetZoom, handle);
346+
}
347+
return zoomToHandle.get(targetZoom);
325348
}
326349

327350
public Point getImageSize() {
@@ -345,14 +368,14 @@ public void put (int index, Image image) {
345368
if ((0 <= index && index < images.length) && (images [index] == image)) return;
346369
int count = OS.ImageList_GetImageCount (handle);
347370
if (!(0 <= index && index < count)) return;
348-
if (image != null) set(index, image, count);
371+
if (image != null) setForAllHandles(index, image, count);
349372
images [index] = image;
350373
}
351374

352375
public void remove (int index) {
353376
int count = OS.ImageList_GetImageCount (handle);
354377
if (!(0 <= index && index < count)) return;
355-
OS.ImageList_Remove (handle, index);
378+
zoomToHandle.values().forEach(handle -> OS.ImageList_Remove (handle, index));
356379
System.arraycopy (images, index + 1, images, index, --count - index);
357380
images [index] = null;
358381
}
@@ -361,7 +384,11 @@ public int removeRef() {
361384
return --refCount;
362385
}
363386

364-
void set (int index, Image image, int count) {
387+
private void setForAllHandles(int index, Image image, int count) {
388+
zoomToHandle.forEach((zoom, handle) -> set(index, image, count, handle, zoom));
389+
}
390+
391+
void set (int index, Image image, int count, long handle, int zoom) {
365392
long hImage = Image.win32_getHandle(image, zoom);
366393
int [] cx = new int [1], cy = new int [1];
367394
OS.ImageList_GetIconSize (handle, cx, cy);
@@ -444,5 +471,4 @@ public int size () {
444471
}
445472
return result;
446473
}
447-
448474
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Button.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void _setImage (Image image) {
148148
imageList.add (disabledImage);
149149
}
150150
BUTTON_IMAGELIST buttonImageList = new BUTTON_IMAGELIST ();
151-
buttonImageList.himl = imageList.getHandle ();
151+
buttonImageList.himl = imageList.getHandle(getZoom());
152152
int oldBits = OS.GetWindowLong (handle, OS.GWL_STYLE), newBits = oldBits;
153153
newBits &= ~(OS.BS_LEFT | OS.BS_CENTER | OS.BS_RIGHT);
154154
if ((style & SWT.LEFT) != 0) newBits |= OS.BS_LEFT;
@@ -189,7 +189,7 @@ void _setText (String text) {
189189
if ((style & SWT.RIGHT) != 0) newBits |= OS.BS_RIGHT;
190190
if (imageList != null) {
191191
BUTTON_IMAGELIST buttonImageList = new BUTTON_IMAGELIST ();
192-
buttonImageList.himl = imageList.getHandle ();
192+
buttonImageList.himl = imageList.getHandle(getZoom());
193193
if (text.length () == 0) {
194194
if ((style & SWT.LEFT) != 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_LEFT;
195195
if ((style & SWT.CENTER) != 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_CENTER;
@@ -779,7 +779,7 @@ public void setAlignment (int alignment) {
779779
if ((style & SWT.RIGHT) != 0) newBits |= OS.BS_RIGHT;
780780
if (imageList != null) {
781781
BUTTON_IMAGELIST buttonImageList = new BUTTON_IMAGELIST ();
782-
buttonImageList.himl = imageList.getHandle ();
782+
buttonImageList.himl = imageList.getHandle(getZoom());
783783
if (text.length () == 0) {
784784
if ((style & SWT.LEFT) != 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_LEFT;
785785
if ((style & SWT.CENTER) != 0) buttonImageList.uAlign = OS.BUTTON_IMAGELIST_ALIGN_CENTER;
@@ -1042,7 +1042,7 @@ void updateImageList () {
10421042
disabledImage = new Image (display, image, SWT.IMAGE_DISABLE);
10431043
imageList.add (disabledImage);
10441044
}
1045-
buttonImageList.himl = imageList.getHandle ();
1045+
buttonImageList.himl = imageList.getHandle(getZoom());
10461046
OS.SendMessage (handle, OS.BCM_SETIMAGELIST, 0, buttonImageList);
10471047
/*
10481048
* Bug in Windows. Under certain cirumstances yet to be

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/TabFolder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ int imageIndex (Image image) {
458458
Rectangle bounds = DPIUtil.scaleBounds(image.getBounds(), this.getZoom(), 100);
459459
imageList = display.getImageList (style & SWT.RIGHT_TO_LEFT, bounds.width, bounds.height, this.getZoom());
460460
int index = imageList.add (image);
461-
long hImageList = imageList.getHandle ();
461+
long hImageList = imageList.getHandle(getZoom());
462462
OS.SendMessage (handle, OS.TCM_SETIMAGELIST, 0, hImageList);
463463
return index;
464464
}
@@ -840,7 +840,7 @@ void updateOrientation () {
840840
Point size = imageList.getImageSize ();
841841
display.releaseImageList (imageList);
842842
imageList = display.getImageList (style & SWT.RIGHT_TO_LEFT, size.x, size.y, this.getZoom());
843-
long hImageList = imageList.getHandle ();
843+
long hImageList = imageList.getHandle(getZoom());
844844
OS.SendMessage (handle, OS.TCM_SETIMAGELIST, 0, hImageList);
845845
TCITEM tcItem = new TCITEM ();
846846
tcItem.mask = OS.TCIF_IMAGE;

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Table.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,7 +2853,7 @@ int imageIndex (Image image, int column) {
28532853
imageList = display.getImageList (style & SWT.RIGHT_TO_LEFT, bounds.width, bounds.height, getZoom());
28542854
int index = imageList.indexOf (image);
28552855
if (index == -1) index = imageList.add (image);
2856-
long hImageList = imageList.getHandle ();
2856+
long hImageList = imageList.getHandle(getZoom());
28572857
/*
28582858
* Bug in Windows. Making any change to an item that
28592859
* changes the item height of a table while the table
@@ -2870,7 +2870,7 @@ int imageIndex (Image image, int column) {
28702870
}
28712871
OS.SendMessage (handle, OS.LVM_SETIMAGELIST, OS.LVSIL_SMALL, hImageList);
28722872
if (headerImageList != null) {
2873-
long hHeaderImageList = headerImageList.getHandle ();
2873+
long hHeaderImageList = headerImageList.getHandle(getZoom());
28742874
OS.SendMessage (hwndHeader, OS.HDM_SETIMAGELIST, 0, hHeaderImageList);
28752875
}
28762876
fixCheckboxImageList (false);
@@ -2893,7 +2893,7 @@ int imageIndexHeader (Image image) {
28932893
headerImageList = display.getImageList (style & SWT.RIGHT_TO_LEFT, bounds.width, bounds.height, getZoom());
28942894
int index = headerImageList.indexOf (image);
28952895
if (index == -1) index = headerImageList.add (image);
2896-
long hImageList = headerImageList.getHandle ();
2896+
long hImageList = headerImageList.getHandle(getZoom());
28972897
OS.SendMessage (hwndHeader, OS.HDM_SETIMAGELIST, 0, hImageList);
28982898
return index;
28992899
}
@@ -5126,7 +5126,7 @@ void setTableEmpty () {
51265126
OS.SendMessage (handle, OS.LVM_SETIMAGELIST, OS.LVSIL_SMALL, hImageList);
51275127
OS.SendMessage (handle, OS.LVM_SETIMAGELIST, OS.LVSIL_SMALL, 0);
51285128
if (headerImageList != null) {
5129-
long hHeaderImageList = headerImageList.getHandle ();
5129+
long hHeaderImageList = headerImageList.getHandle(getZoom());
51305130
long hwndHeader = OS.SendMessage (handle, OS.LVM_GETHEADER, 0, 0);
51315131
OS.SendMessage (hwndHeader, OS.HDM_SETIMAGELIST, 0, hHeaderImageList);
51325132
}
@@ -5590,7 +5590,7 @@ void updateOrientation () {
55905590
}
55915591
}
55925592
}
5593-
long hImageList = imageList.getHandle ();
5593+
long hImageList = imageList.getHandle(getZoom());
55945594
OS.SendMessage (handle, OS.LVM_SETIMAGELIST, OS.LVSIL_SMALL, hImageList);
55955595
}
55965596
if (hwndHeader != 0) {
@@ -5618,7 +5618,7 @@ void updateOrientation () {
56185618
}
56195619
}
56205620
}
5621-
long hHeaderImageList = headerImageList.getHandle ();
5621+
long hHeaderImageList = headerImageList.getHandle(getZoom());
56225622
OS.SendMessage (hwndHeader, OS.HDM_SETIMAGELIST, 0, hHeaderImageList);
56235623
}
56245624
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/ToolBar.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,10 +1046,11 @@ void setDropDownItems (boolean set) {
10461046
}
10471047

10481048
void setDisabledImageList (ImageList imageList) {
1049-
if (disabledImageList == imageList) return;
1050-
long hImageList = 0;
1049+
long hImageList = OS.SendMessage (handle, OS.TB_GETDISABLEDIMAGELIST, 0, 0);
10511050
if ((disabledImageList = imageList) != null) {
1052-
hImageList = disabledImageList.getHandle ();
1051+
long newImageList = disabledImageList.getHandle(getZoom());
1052+
if(hImageList == newImageList) return;
1053+
hImageList = newImageList;
10531054
}
10541055
setDropDownItems (false);
10551056
OS.SendMessage (handle, OS.TB_SETDISABLEDIMAGELIST, 0, hImageList);
@@ -1083,21 +1084,23 @@ public void setFont (Font font) {
10831084
}
10841085

10851086
void setHotImageList (ImageList imageList) {
1086-
if (hotImageList == imageList) return;
1087-
long hImageList = 0;
1087+
long hImageList = OS.SendMessage (handle, OS.TB_GETHOTIMAGELIST, 0, 0);
10881088
if ((hotImageList = imageList) != null) {
1089-
hImageList = hotImageList.getHandle ();
1089+
long newImageList = hotImageList.getHandle(getZoom());
1090+
if(hImageList == newImageList) return;
1091+
hImageList = newImageList;
10901092
}
10911093
setDropDownItems (false);
10921094
OS.SendMessage (handle, OS.TB_SETHOTIMAGELIST, 0, hImageList);
10931095
setDropDownItems (true);
10941096
}
10951097

10961098
void setImageList (ImageList imageList) {
1097-
if (this.imageList == imageList) return;
1098-
long hImageList = 0;
1099+
long hImageList = OS.SendMessage (handle, OS.TB_GETIMAGELIST, 0, 0);
10991100
if ((this.imageList = imageList) != null) {
1100-
hImageList = imageList.getHandle ();
1101+
long newImageList = imageList.getHandle(getZoom());
1102+
if (hImageList == newImageList) return;
1103+
hImageList = newImageList;
11011104
}
11021105
setDropDownItems (false);
11031106
OS.SendMessage (handle, OS.TB_SETIMAGELIST, 0, hImageList);
@@ -1293,9 +1296,9 @@ void updateOrientation () {
12931296
display.releaseToolImageList (imageList);
12941297
display.releaseToolHotImageList (hotImageList);
12951298
display.releaseToolDisabledImageList (disabledImageList);
1296-
OS.SendMessage (handle, OS.TB_SETIMAGELIST, 0, newImageList.getHandle ());
1297-
OS.SendMessage (handle, OS.TB_SETHOTIMAGELIST, 0, newHotImageList.getHandle ());
1298-
OS.SendMessage (handle, OS.TB_SETDISABLEDIMAGELIST, 0, newDisabledImageList.getHandle ());
1299+
OS.SendMessage (handle, OS.TB_SETIMAGELIST, 0, newImageList.getHandle(getZoom()));
1300+
OS.SendMessage (handle, OS.TB_SETHOTIMAGELIST, 0, newHotImageList.getHandle(getZoom()));
1301+
OS.SendMessage (handle, OS.TB_SETDISABLEDIMAGELIST, 0, newDisabledImageList.getHandle(getZoom()));
12991302
imageList = newImageList;
13001303
hotImageList = newHotImageList;
13011304
disabledImageList = newDisabledImageList;
@@ -1759,15 +1762,15 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac
17591762

17601763
}
17611764
for (ToolItem item : toolItems) {
1762-
toolBar.destroyItem(item);
1765+
// toolBar.destroyItem(item);
17631766
// Resize after, as zoom update changes references to imageLists
17641767
DPIZoomChangeRegistry.applyChange(item, newZoom, scalingFactor);
17651768
}
17661769

17671770
for (int i = 0; i < toolItems.length; i++) {
17681771
ToolItem toolItem = toolItems[i];
17691772

1770-
toolBar.createItem(toolItem, i);
1773+
// toolBar.createItem(toolItem, i);
17711774
String currentText = toolItem.getText();
17721775
toolItem.setText(" ");
17731776
toolItem.setText(currentText);

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3751,7 +3751,7 @@ int imageIndex (Image image, int index) {
37513751
* times, Windows does work making this operation slow. The fix
37523752
* is to test for the same image list before setting the new one.
37533753
*/
3754-
long hImageList = imageList.getHandle ();
3754+
long hImageList = imageList.getHandle(getZoom());
37553755
long hOldImageList = OS.SendMessage (handle, OS.TVM_GETIMAGELIST, OS.TVSIL_NORMAL, 0);
37563756
if (hOldImageList != hImageList) {
37573757
OS.SendMessage (handle, OS.TVM_SETIMAGELIST, OS.TVSIL_NORMAL, hImageList);
@@ -3768,7 +3768,7 @@ int imageIndexHeader (Image image) {
37683768
headerImageList = display.getImageList (style & SWT.RIGHT_TO_LEFT, bounds.width, bounds.height, getZoom());
37693769
int index = headerImageList.indexOf (image);
37703770
if (index == -1) index = headerImageList.add (image);
3771-
long hImageList = headerImageList.getHandle ();
3771+
long hImageList = headerImageList.getHandle(getZoom());
37723772
if (hwndHeader != 0) {
37733773
OS.SendMessage (hwndHeader, OS.HDM_SETIMAGELIST, 0, hImageList);
37743774
}
@@ -5729,7 +5729,7 @@ void updateImageList () {
57295729
* times, Windows does work making this operation slow. The fix
57305730
* is to test for the same image list before setting the new one.
57315731
*/
5732-
long hImageList = i == items.length ? 0 : imageList.getHandle ();
5732+
long hImageList = i == items.length ? 0 : imageList.getHandle(getZoom());
57335733
long hOldImageList = OS.SendMessage (handle, OS.TVM_GETIMAGELIST, OS.TVSIL_NORMAL, 0);
57345734
if (hImageList != hOldImageList) {
57355735
OS.SendMessage (handle, OS.TVM_SETIMAGELIST, OS.TVSIL_NORMAL, hImageList);
@@ -5803,7 +5803,7 @@ void updateOrientation () {
58035803
}
58045804
}
58055805
}
5806-
long hImageList = imageList.getHandle ();
5806+
long hImageList = imageList.getHandle(getZoom());
58075807
OS.SendMessage (handle, OS.TVM_SETIMAGELIST, OS.TVSIL_NORMAL, hImageList);
58085808
}
58095809
if (hwndHeader != 0) {
@@ -5831,7 +5831,7 @@ void updateOrientation () {
58315831
}
58325832
}
58335833
}
5834-
long hImageListHeader = headerImageList.getHandle ();
5834+
long hImageListHeader = headerImageList.getHandle(getZoom());
58355835
OS.SendMessage (hwndHeader, OS.HDM_SETIMAGELIST, 0, hImageListHeader);
58365836
}
58375837
}

0 commit comments

Comments
 (0)