Skip to content

Commit

Permalink
Scaling of Text Icon for multi zoom level
Browse files Browse the repository at this point in the history
This commit provides the ability to scaling the special icons of Text in
win 32, i.e., Search and Cancel icon. The scaled icon is obtained at the
draw time in the method Text.WM_DRAWITEM. We get rid of the fields
hIconSearch and hIconCancel and we maintain a hashmap of size to icon
for both the icons.

contributes to #62 and #127
  • Loading branch information
amartya4256 authored and fedejeanne committed Jul 4, 2024
1 parent 332dee4 commit 69af8e1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ public class Display extends Device implements Executor {
boolean textUseDarkthemeIcons = false;

/* Custom icons */
long hIconSearch;
long hIconCancel;
private HashMap<Integer, Long> sizeToSearchIconHandle = new HashMap<>();
private HashMap<Integer, Long> sizeToCancelIconHandle = new HashMap<>();

/* Focus */
int focusEvent;
Expand Down Expand Up @@ -1188,6 +1188,26 @@ static Image createIcon (Image image) {
return Image.win32_new (device, SWT.ICON, hIcon);
}

long getTextSearchIcon(int size) {
if (!sizeToSearchIconHandle.containsKey(size)) {
int searchIconResource = textUseDarkthemeIcons ? Text.IDI_SEARCH_DARKTHEME : Text.IDI_SEARCH;
long iconHandle = OS.LoadImage (OS.GetLibraryHandle (), searchIconResource, OS.IMAGE_ICON, size, size, 0);
if (iconHandle == 0) error(SWT.ERROR_NO_HANDLES);
sizeToSearchIconHandle.put(size, iconHandle);
}
return sizeToSearchIconHandle.get(size);
}

long getTextCancelIcon(int size) {
if (!sizeToCancelIconHandle.containsKey(size)) {
int searchIconResource = textUseDarkthemeIcons ? Text.IDI_CANCEL_DARKTHEME : Text.IDI_CANCEL;
long iconHandle = OS.LoadImage (OS.GetLibraryHandle (), searchIconResource, OS.IMAGE_ICON, size, size, 0);
if (iconHandle == 0) error(SWT.ERROR_NO_HANDLES);
sizeToCancelIconHandle.put(size, iconHandle);
}
return sizeToCancelIconHandle.get(size);
}

static void deregister (Display display) {
synchronized (Device.class) {
for (int i=0; i<Displays.length; i++) {
Expand Down Expand Up @@ -3795,8 +3815,10 @@ void releaseDisplay () {
}

/* Free custom icons */
if (hIconSearch != 0) OS.DestroyIcon (hIconSearch);
if (hIconCancel != 0) OS.DestroyIcon (hIconCancel);
sizeToSearchIconHandle.values().forEach(handle -> OS.DestroyIcon(handle));
sizeToCancelIconHandle.values().forEach(handle -> OS.DestroyIcon(handle));
sizeToSearchIconHandle.clear();
sizeToCancelIconHandle.clear();

/* Release XP Themes */
resetThemes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,24 +337,7 @@ void createHandle () {
state |= THEME_BACKGROUND;
}
}
addIcons();
}

private void addIcons() {
if ((style & SWT.SEARCH) != 0) {
if (display.hIconSearch == 0) {
long [] phicon = new long [1];

int searchIconResource = display.textUseDarkthemeIcons ? IDI_SEARCH_DARKTHEME : IDI_SEARCH;
int hresult = OS.LoadIconMetric (OS.GetLibraryHandle (), searchIconResource, OS.LIM_SMALL, phicon);
if (hresult != OS.S_OK) error (SWT.ERROR_NO_HANDLES);
display.hIconSearch = phicon [0];

int cancelIconResource = display.textUseDarkthemeIcons ? IDI_CANCEL_DARKTHEME : IDI_CANCEL;
hresult = OS.LoadIconMetric (OS.GetLibraryHandle (), cancelIconResource, OS.LIM_SMALL, phicon);
if (hresult != OS.S_OK) error (SWT.ERROR_NO_HANDLES);
display.hIconCancel = phicon [0];
}
if ((style & SWT.ICON_SEARCH) != 0) {
long hwndSearch = OS.CreateWindowEx (
0,
Expand Down Expand Up @@ -2770,7 +2753,10 @@ LRESULT WM_DRAWITEM (long wParam, long lParam) {
int state = OS.GetKeyState (OS.VK_LBUTTON) < 0 ? OS.PBS_PRESSED : OS.PBS_HOT;
OS.DrawThemeBackground (display.hButtonThemeAuto (), struct.hDC, OS.BP_PUSHBUTTON, state, rect, null);
}
long hIcon = (struct.CtlID == SWT.ICON_SEARCH) ? display.hIconSearch : display.hIconCancel;
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
int size = Math.min(width, height);
long hIcon = (struct.CtlID == SWT.ICON_SEARCH) ? display.getTextSearchIcon(size) : display.getTextCancelIcon(size);
int y = (rect.bottom - rect.right) / 2;
OS.DrawIconEx (struct.hDC, 0, y, hIcon, 0, 0, 0, 0, OS.DI_NORMAL);
return LRESULT.ONE;
Expand Down Expand Up @@ -3167,7 +3153,6 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac
if (!(widget instanceof Text text)) {
return;
}
text.addIcons();
text.setMargins();
}
}

0 comments on commit 69af8e1

Please sign in to comment.