Skip to content

Commit f68bc9b

Browse files
akoch-yattaHeikoKlare
authored andcommitted
[win32] Prevent exception on toolbar DPI change
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
1 parent 58180a4 commit f68bc9b

File tree

1 file changed

+20
-21
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets

1 file changed

+20
-21
lines changed

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

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

1616

17+
import java.util.*;
18+
1719
import org.eclipse.swt.*;
1820
import org.eclipse.swt.graphics.*;
1921
import org.eclipse.swt.internal.*;
@@ -1752,37 +1754,30 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac
17521754
var seperatorWidth = new int[toolItems.length];
17531755
int itemCount = toolItems.length;
17541756

1757+
record ToolItemData(ToolItem toolItem, TBBUTTON button) {
1758+
}
1759+
17551760
// Remove and re-add all button the let Windows resize the tool bar
1756-
ToolItem[] items = new ToolItem[itemCount];
1757-
TBBUTTON[] buttondata = new TBBUTTON[itemCount];
1761+
Stack<ToolItemData> buttondata = new Stack<>();
17581762
for (int i = itemCount - 1; i >= 0; i--) {
1763+
TBBUTTON lpButton = new TBBUTTON ();
1764+
OS.SendMessage (toolBar.handle, OS.TB_GETBUTTON, i, lpButton);
17591765
ToolItem item = toolItems[i];
17601766
if ((item.style & SWT.SEPARATOR) != 0 && item.getControl() != null) {
17611767
// Take note of widths of separators with control, so they can be resized
17621768
// at the end
17631769
seperatorWidth[i] = item.getWidth();
17641770
}
17651771
DPIZoomChangeRegistry.applyChange(item, newZoom, scalingFactor);
1766-
1767-
TBBUTTON lpButton = new TBBUTTON ();
1768-
OS.SendMessage (toolBar.handle, OS.TB_GETBUTTON, i, lpButton);
1769-
buttondata[lpButton.idCommand] = lpButton;
1770-
items[lpButton.idCommand] = item;
1772+
buttondata.push(new ToolItemData(item, lpButton));
17711773
OS.SendMessage(toolBar.handle, OS.TB_DELETEBUTTON, i, 0);
17721774
}
1773-
1774-
// Refresh the image lists so the image list for the correct zoom is used
1775-
toolBar.setImageList(toolBar.getImageList());
1776-
toolBar.setDisabledImageList(toolBar.getDisabledImageList());
1777-
toolBar.setHotImageList(toolBar.getHotImageList());
1778-
17791775
OS.SendMessage(toolBar.handle, OS.TB_BUTTONSTRUCTSIZE, TBBUTTON.sizeof, 0);
1780-
for (int i = 0; i < buttondata.length; i++) {
1781-
TBBUTTON button = buttondata[i];
1782-
if (button != null) {
1783-
OS.SendMessage(toolBar.handle, OS.TB_ADDBUTTONS, 1, button);
1784-
ToolItem item = items[i];
1785-
1776+
while (!buttondata.isEmpty()) {
1777+
ToolItemData itemData = buttondata.pop();
1778+
OS.SendMessage(toolBar.handle, OS.TB_ADDBUTTONS, 1, itemData.button);
1779+
ToolItem item = itemData.toolItem;
1780+
if (item != null) {
17861781
// The text is not retained correctly, so we need to reset it
17871782
String text = item.getText();
17881783
if (text != null) {
@@ -1791,8 +1786,6 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac
17911786
}
17921787
}
17931788
}
1794-
OS.SendMessage(toolBar.handle, OS.TB_AUTOSIZE, 0, 0);
1795-
17961789
for (int i = 0; i < itemCount; i++) {
17971790
ToolItem item = toolItems[i];
17981791
// If the separator is used with a control, we must reset the size to the cached value,
@@ -1801,6 +1794,12 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac
18011794
item.setWidth(seperatorWidth[i]);
18021795
}
18031796
}
1797+
1798+
// Refresh the image lists so the image list for the correct zoom is used
1799+
toolBar.setImageList(toolBar.getImageList());
1800+
toolBar.setDisabledImageList(toolBar.getDisabledImageList());
1801+
toolBar.setHotImageList(toolBar.getHotImageList());
1802+
OS.SendMessage(toolBar.handle, OS.TB_AUTOSIZE, 0, 0);
18041803
toolBar.layout(true);
18051804
}
18061805
}

0 commit comments

Comments
 (0)