Skip to content

Commit

Permalink
[win32] Prevent exception on toolbar DPI change
Browse files Browse the repository at this point in the history
This commit prevents an ArrayIndexOutOfBounds exception, when the commandIds of toolbar buttons are not in line with button count, e.g. there is an id higher than the button count.

Contributes to #62 and #131
  • Loading branch information
akoch-yatta authored and HeikoKlare committed Aug 2, 2024
1 parent 58180a4 commit f68bc9b
Showing 1 changed file with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package org.eclipse.swt.widgets;


import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
Expand Down Expand Up @@ -1752,37 +1754,30 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac
var seperatorWidth = new int[toolItems.length];
int itemCount = toolItems.length;

record ToolItemData(ToolItem toolItem, TBBUTTON button) {
}

// Remove and re-add all button the let Windows resize the tool bar
ToolItem[] items = new ToolItem[itemCount];
TBBUTTON[] buttondata = new TBBUTTON[itemCount];
Stack<ToolItemData> buttondata = new Stack<>();
for (int i = itemCount - 1; i >= 0; i--) {
TBBUTTON lpButton = new TBBUTTON ();
OS.SendMessage (toolBar.handle, OS.TB_GETBUTTON, i, lpButton);
ToolItem item = toolItems[i];
if ((item.style & SWT.SEPARATOR) != 0 && item.getControl() != null) {
// Take note of widths of separators with control, so they can be resized
// at the end
seperatorWidth[i] = item.getWidth();
}
DPIZoomChangeRegistry.applyChange(item, newZoom, scalingFactor);

TBBUTTON lpButton = new TBBUTTON ();
OS.SendMessage (toolBar.handle, OS.TB_GETBUTTON, i, lpButton);
buttondata[lpButton.idCommand] = lpButton;
items[lpButton.idCommand] = item;
buttondata.push(new ToolItemData(item, lpButton));
OS.SendMessage(toolBar.handle, OS.TB_DELETEBUTTON, i, 0);
}

// Refresh the image lists so the image list for the correct zoom is used
toolBar.setImageList(toolBar.getImageList());
toolBar.setDisabledImageList(toolBar.getDisabledImageList());
toolBar.setHotImageList(toolBar.getHotImageList());

OS.SendMessage(toolBar.handle, OS.TB_BUTTONSTRUCTSIZE, TBBUTTON.sizeof, 0);
for (int i = 0; i < buttondata.length; i++) {
TBBUTTON button = buttondata[i];
if (button != null) {
OS.SendMessage(toolBar.handle, OS.TB_ADDBUTTONS, 1, button);
ToolItem item = items[i];

while (!buttondata.isEmpty()) {
ToolItemData itemData = buttondata.pop();
OS.SendMessage(toolBar.handle, OS.TB_ADDBUTTONS, 1, itemData.button);
ToolItem item = itemData.toolItem;
if (item != null) {
// The text is not retained correctly, so we need to reset it
String text = item.getText();
if (text != null) {
Expand All @@ -1791,8 +1786,6 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac
}
}
}
OS.SendMessage(toolBar.handle, OS.TB_AUTOSIZE, 0, 0);

for (int i = 0; i < itemCount; i++) {
ToolItem item = toolItems[i];
// If the separator is used with a control, we must reset the size to the cached value,
Expand All @@ -1801,6 +1794,12 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac
item.setWidth(seperatorWidth[i]);
}
}

// Refresh the image lists so the image list for the correct zoom is used
toolBar.setImageList(toolBar.getImageList());
toolBar.setDisabledImageList(toolBar.getDisabledImageList());
toolBar.setHotImageList(toolBar.getHotImageList());
OS.SendMessage(toolBar.handle, OS.TB_AUTOSIZE, 0, 0);
toolBar.layout(true);
}
}

0 comments on commit f68bc9b

Please sign in to comment.