@@ -1046,10 +1046,11 @@ void setDropDownItems (boolean set) {
10461046}
10471047
10481048void setDisabledImageList (ImageList imageList ) {
1049- long hImageList = OS . SendMessage ( handle , OS . TB_GETDISABLEDIMAGELIST , 0 , 0 ) ;
1049+ long hImageList = 0 ;
10501050 if ((disabledImageList = imageList ) != null ) {
1051+ hImageList = OS .SendMessage (handle , OS .TB_GETDISABLEDIMAGELIST , 0 , 0 );
10511052 long newImageList = disabledImageList .getHandle (getZoom ());
1052- if (hImageList == newImageList ) return ;
1053+ if (hImageList == newImageList ) return ;
10531054 hImageList = newImageList ;
10541055 }
10551056 setDropDownItems (false );
@@ -1084,10 +1085,11 @@ public void setFont (Font font) {
10841085}
10851086
10861087void setHotImageList (ImageList imageList ) {
1087- long hImageList = OS . SendMessage ( handle , OS . TB_GETHOTIMAGELIST , 0 , 0 ) ;
1088+ long hImageList = 0 ;
10881089 if ((hotImageList = imageList ) != null ) {
1090+ hImageList = OS .SendMessage (handle , OS .TB_GETHOTIMAGELIST , 0 , 0 );
10891091 long newImageList = hotImageList .getHandle (getZoom ());
1090- if (hImageList == newImageList ) return ;
1092+ if (hImageList == newImageList ) return ;
10911093 hImageList = newImageList ;
10921094 }
10931095 setDropDownItems (false );
@@ -1096,8 +1098,9 @@ void setHotImageList (ImageList imageList) {
10961098}
10971099
10981100void setImageList (ImageList imageList ) {
1099- long hImageList = OS . SendMessage ( handle , OS . TB_GETIMAGELIST , 0 , 0 ) ;
1101+ long hImageList = 0 ;
11001102 if ((this .imageList = imageList ) != null ) {
1103+ hImageList = OS .SendMessage (handle , OS .TB_GETIMAGELIST , 0 , 0 );
11011104 long newImageList = imageList .getHandle (getZoom ());
11021105 if (hImageList == newImageList ) return ;
11031106 hImageList = newImageList ;
@@ -1745,75 +1748,59 @@ private static void handleDPIChange(Widget widget, int newZoom, float scalingFac
17451748 if (!(widget instanceof ToolBar toolBar )) {
17461749 return ;
17471750 }
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 --) {
17541759 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
17571763 seperatorWidth [i ] = item .getWidth ();
17581764 }
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
17671765 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 );
17821766
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+ }
18001792 }
1801-
1802- toolItem .setEnabled (enabledState [i ]);
1803- toolItem .setSelection (selectedState [i ]);
18041793 }
1794+ OS .SendMessage (toolBar .handle , OS .TB_AUTOSIZE , 0 , 0 );
18051795
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+ }
18131803 }
1814- toolBar .setDropDownItems (true );
18151804 toolBar .layout (true );
1816- toolBar .sendResize ();
1817- toolBar .redraw ();
18181805}
18191806}
0 commit comments