@@ -1046,10 +1046,11 @@ void setDropDownItems (boolean set) {
1046
1046
}
1047
1047
1048
1048
void setDisabledImageList (ImageList imageList ) {
1049
- long hImageList = OS . SendMessage ( handle , OS . TB_GETDISABLEDIMAGELIST , 0 , 0 ) ;
1049
+ long hImageList = 0 ;
1050
1050
if ((disabledImageList = imageList ) != null ) {
1051
+ hImageList = OS .SendMessage (handle , OS .TB_GETDISABLEDIMAGELIST , 0 , 0 );
1051
1052
long newImageList = disabledImageList .getHandle (getZoom ());
1052
- if (hImageList == newImageList ) return ;
1053
+ if (hImageList == newImageList ) return ;
1053
1054
hImageList = newImageList ;
1054
1055
}
1055
1056
setDropDownItems (false );
@@ -1084,10 +1085,11 @@ public void setFont (Font font) {
1084
1085
}
1085
1086
1086
1087
void setHotImageList (ImageList imageList ) {
1087
- long hImageList = OS . SendMessage ( handle , OS . TB_GETHOTIMAGELIST , 0 , 0 ) ;
1088
+ long hImageList = 0 ;
1088
1089
if ((hotImageList = imageList ) != null ) {
1090
+ hImageList = OS .SendMessage (handle , OS .TB_GETHOTIMAGELIST , 0 , 0 );
1089
1091
long newImageList = hotImageList .getHandle (getZoom ());
1090
- if (hImageList == newImageList ) return ;
1092
+ if (hImageList == newImageList ) return ;
1091
1093
hImageList = newImageList ;
1092
1094
}
1093
1095
setDropDownItems (false );
@@ -1096,8 +1098,9 @@ void setHotImageList (ImageList imageList) {
1096
1098
}
1097
1099
1098
1100
void setImageList (ImageList imageList ) {
1099
- long hImageList = OS . SendMessage ( handle , OS . TB_GETIMAGELIST , 0 , 0 ) ;
1101
+ long hImageList = 0 ;
1100
1102
if ((this .imageList = imageList ) != null ) {
1103
+ hImageList = OS .SendMessage (handle , OS .TB_GETIMAGELIST , 0 , 0 );
1101
1104
long newImageList = imageList .getHandle (getZoom ());
1102
1105
if (hImageList == newImageList ) return ;
1103
1106
hImageList = newImageList ;
@@ -1745,75 +1748,59 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac
1745
1748
if (!(widget instanceof ToolBar toolBar )) {
1746
1749
return ;
1747
1750
}
1748
- ToolItem [] toolItems = toolBar ._getItems ();
1749
- // Only Items with SWT.Sepreator Style have an own width assigned to them
1750
- var seperatorWidth = new int [toolItems .length ];
1751
- var enabledState = new boolean [toolItems .length ];
1752
- var selectedState = new boolean [toolItems .length ];
1753
- for (int i = 0 ; i < toolItems .length ; i ++) {
1751
+ ToolItem [] toolItems = toolBar ._getItems ();
1752
+ var seperatorWidth = new int [toolItems .length ];
1753
+ int itemCount = toolItems .length ;
1754
+
1755
+ // 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 ];
1758
+ for (int i = itemCount - 1 ; i >= 0 ; i --) {
1754
1759
ToolItem item = toolItems [i ];
1755
- if ((item .style & SWT .SEPARATOR ) != 0 ) {
1756
- // Take note of widths, so we can re-apply them later
1760
+ if ((item .style & SWT .SEPARATOR ) != 0 && item .getControl () != null ) {
1761
+ // Take note of widths of separators with control, so they can be resized
1762
+ // at the end
1757
1763
seperatorWidth [i ] = item .getWidth ();
1758
1764
}
1759
- // Remember states of ToolItem to apply them later
1760
- enabledState [i ] = item .getEnabled ();
1761
- selectedState [i ] = item .getSelection ();
1762
-
1763
- }
1764
- for (ToolItem item : toolItems ) {
1765
- // toolBar.destroyItem(item);
1766
- // Resize after, as zoom update changes references to imageLists
1767
1765
DPIZoomChangeRegistry .applyChange (item , newZoom , scalingFactor );
1768
- }
1769
-
1770
- for (int i = 0 ; i < toolItems .length ; i ++) {
1771
- ToolItem toolItem = toolItems [i ];
1772
-
1773
- // toolBar.createItem(toolItem, i);
1774
- String currentText = toolItem .getText ();
1775
- toolItem .setText (" " );
1776
- toolItem .setText (currentText );
1777
-
1778
- // Refresh images (upscaling already performed by toolItem)
1779
- Image image = toolItem .getImage ();
1780
- toolItem .setImage (null );
1781
- toolItem .setImage (image );
1782
1766
1783
- Image hotImage = toolItem .getHotImage ();
1784
- toolItem .setHotImage (null );
1785
- toolItem .setHotImage (hotImage );
1786
-
1787
- Image disabledImage = toolItem .getDisabledImage ();
1788
- toolItem .setDisabledImage (null );
1789
- toolItem .setDisabledImage (disabledImage );
1790
-
1791
- var content = toolItem .getControl ();
1792
- toolItem .setControl (null );
1793
- toolItem .setControl (content );
1794
-
1795
- // In SWT, Width can only be set for Separators
1796
- if ((toolItem .style & SWT .SEPARATOR ) != 0 ) {
1797
- var width = (int )((seperatorWidth [i ]) * scalingFactor );
1798
- toolItem .setWidth (width );
1799
- toolItem .resizeControl ();
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 ;
1771
+ OS .SendMessage (toolBar .handle , OS .TB_DELETEBUTTON , i , 0 );
1772
+ }
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
+
1779
+ 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
+
1786
+ // The text is not retained correctly, so we need to reset it
1787
+ String text = item .getText ();
1788
+ if (text != null ) {
1789
+ item .setText ("" );
1790
+ item .setText (text );
1791
+ }
1800
1792
}
1801
-
1802
- toolItem .setEnabled (enabledState [i ]);
1803
- toolItem .setSelection (selectedState [i ]);
1804
1793
}
1794
+ OS .SendMessage (toolBar .handle , OS .TB_AUTOSIZE , 0 , 0 );
1805
1795
1806
- // Force a refresh of the toolbar by resetting the Font
1807
- toolBar . setDropDownItems ( false ) ;
1808
- long hFont = OS . SendMessage ( toolBar . handle , OS . WM_GETFONT , 0 , 0 );
1809
- OS . SendMessage ( toolBar . handle , OS . WM_SETFONT , hFont , 0 );
1810
- if (( toolBar .style & SWT .VERTICAL ) != 0 ) {
1811
- // Reset row count to prevent wrapping of buttons
1812
- toolBar . setRowCount (( int ) OS . SendMessage ( toolBar . handle , OS . TB_BUTTONCOUNT , 0 , 0 ));
1796
+ for ( int i = 0 ; i < itemCount ; i ++) {
1797
+ ToolItem item = toolItems [ i ] ;
1798
+ // If the separator is used with a control, we must reset the size to the cached value,
1799
+ // cause windows will treat the separator as normal separator and shrinks it accordingly
1800
+ if (( item .style & SWT .SEPARATOR ) != 0 && item . getControl () != null ) {
1801
+ item . setWidth ( seperatorWidth [ i ]);
1802
+ }
1813
1803
}
1814
- toolBar .setDropDownItems (true );
1815
1804
toolBar .layout (true );
1816
- toolBar .sendResize ();
1817
- toolBar .redraw ();
1818
1805
}
1819
1806
}
0 commit comments