@@ -274,26 +274,6 @@ void closeWidget () {
274274 if (event .doit && !isDisposed ()) dispose ();
275275}
276276
277- int compare (ImageData data1 , ImageData data2 , int width , int height , int depth ) {
278- int value1 = Math .abs (data1 .width - width ), value2 = Math .abs (data2 .width - width );
279- if (value1 == value2 ) {
280- int transparent1 = data1 .getTransparencyType ();
281- int transparent2 = data2 .getTransparencyType ();
282- if (transparent1 == transparent2 ) {
283- if (data1 .depth == data2 .depth ) return 0 ;
284- return data1 .depth > data2 .depth && data1 .depth <= depth ? -1 : 1 ;
285- }
286- if (transparent1 == SWT .TRANSPARENCY_ALPHA ) return -1 ;
287- if (transparent2 == SWT .TRANSPARENCY_ALPHA ) return 1 ;
288- if (transparent1 == SWT .TRANSPARENCY_MASK ) return -1 ;
289- if (transparent2 == SWT .TRANSPARENCY_MASK ) return 1 ;
290- if (transparent1 == SWT .TRANSPARENCY_PIXEL ) return -1 ;
291- if (transparent2 == SWT .TRANSPARENCY_PIXEL ) return 1 ;
292- return 0 ;
293- }
294- return value1 < value2 ? -1 : 1 ;
295- }
296-
297277@ Override
298278Widget computeTabGroup () {
299279 return this ;
@@ -882,7 +862,7 @@ public void setImage (Image image) {
882862 setImages (image , null );
883863}
884864
885- void setImages (Image image , Image [] images ) {
865+ private void setImages (Image image , Image [] images ) {
886866 if (smallImage != null ) smallImage .dispose ();
887867 if (largeImage != null ) largeImage .dispose ();
888868 smallImage = largeImage = null ;
@@ -893,22 +873,14 @@ void setImages (Image image, Image [] images) {
893873 } else {
894874 if (images != null && images .length > 0 ) {
895875 int depth = display .getIconDepth ();
896- ImageData [] datas = null ;
897- if (images .length > 1 ) {
898- Image [] bestImages = new Image [images .length ];
899- System .arraycopy (images , 0 , bestImages , 0 , images .length );
900- datas = new ImageData [images .length ];
901- for (int i =0 ; i <datas .length ; i ++) {
902- datas [i ] = images [i ].getImageData ();
903- }
904- images = bestImages ;
905- sort (images , datas , getSystemMetrics (OS .SM_CXSMICON ), getSystemMetrics (OS .SM_CYSMICON ), depth );
906- }
907- smallIcon = images [0 ];
908- if (images .length > 1 ) {
909- sort (images , datas , getSystemMetrics (OS .SM_CXICON ), getSystemMetrics (OS .SM_CYICON ), depth );
910- }
911- largeIcon = images [0 ];
876+
877+ ImageData [] imageData = getImageData (images );
878+
879+ int smallIconIndex = findIndexOfClosest (imageData , getSystemMetrics (OS .SM_CXSMICON ),getSystemMetrics (OS .SM_CYSMICON ), depth );
880+ smallIcon = images [smallIconIndex ];
881+
882+ int largeIconIndex = findIndexOfClosest (imageData , getSystemMetrics (OS .SM_CXICON ),getSystemMetrics (OS .SM_CYICON ), depth );
883+ largeIcon = images [largeIconIndex ];
912884 }
913885 }
914886 if (smallIcon != null ) {
@@ -949,6 +921,53 @@ void setImages (Image image, Image [] images) {
949921 }
950922}
951923
924+ private ImageData [] getImageData (Image [] images ) {
925+ ImageData [] datas = new ImageData [images .length ];
926+ for (int i = 0 ; i < images .length ; i ++) {
927+ datas [i ] = images [i ].getImageData ();
928+ }
929+ return datas ;
930+ }
931+
932+ private int findIndexOfClosest (ImageData [] imageData , int width , int height , int depth ) {
933+ int closestIndex = 0 ;
934+ ImageData closestData = imageData [0 ];
935+ for (int i =1 ; i <imageData .length ;i ++) {
936+ if (isCloserThan (imageData [i ],closestData , width , height , depth )) {
937+ closestIndex = i ;
938+ closestData = imageData [i ];
939+ }
940+ }
941+
942+ return closestIndex ;
943+ }
944+
945+ /**
946+ * @return <code>true</code> if <code>data1</code> is closer to the desired width, height and depth than <code>data2</code>.
947+ */
948+ private boolean isCloserThan (ImageData data1 , ImageData data2 , int width , int height , int depth ) {
949+ int diffWidth1 = Math .abs (data1 .width - width );
950+ int diffWidth2 = Math .abs (data2 .width - width );
951+
952+ if (diffWidth1 == diffWidth2 ) {
953+ int transparent1 = data1 .getTransparencyType ();
954+ int transparent2 = data2 .getTransparencyType ();
955+ if (transparent1 == transparent2 ) {
956+ if (data1 .depth == data2 .depth ) return false ;
957+ // TODO: analyze this, it seems broken.
958+ return data1 .depth > data2 .depth && data1 .depth <= depth ;
959+ }
960+ if (transparent1 == SWT .TRANSPARENCY_ALPHA ) return true ;
961+ if (transparent2 == SWT .TRANSPARENCY_ALPHA ) return false ;
962+ if (transparent1 == SWT .TRANSPARENCY_MASK ) return true ;
963+ if (transparent2 == SWT .TRANSPARENCY_MASK ) return false ;
964+ if (transparent1 == SWT .TRANSPARENCY_PIXEL ) return true ;
965+ if (transparent2 == SWT .TRANSPARENCY_PIXEL ) return false ;
966+ return false ;
967+ }
968+ return diffWidth1 < diffWidth2 ;
969+ }
970+
952971/**
953972 * Sets the receiver's images to the argument, which may
954973 * be an empty array. Images are typically displayed by the
@@ -1313,26 +1332,6 @@ public void setVisible (boolean visible) {
13131332 }
13141333}
13151334
1316- void sort (Image [] images , ImageData [] datas , int width , int height , int depth ) {
1317- /* Shell Sort from K&R, pg 108 */
1318- int length = images .length ;
1319- if (length <= 1 ) return ;
1320- for (int gap =length /2 ; gap >0 ; gap /=2 ) {
1321- for (int i =gap ; i <length ; i ++) {
1322- for (int j =i -gap ; j >=0 ; j -=gap ) {
1323- if (compare (datas [j ], datas [j + gap ], width , height , depth ) >= 0 ) {
1324- Image swap = images [j ];
1325- images [j ] = images [j + gap ];
1326- images [j + gap ] = swap ;
1327- ImageData swapData = datas [j ];
1328- datas [j ] = datas [j + gap ];
1329- datas [j + gap ] = swapData ;
1330- }
1331- }
1332- }
1333- }
1334- }
1335-
13361335@ Override
13371336boolean translateAccelerator (MSG msg ) {
13381337 if (!isEnabled () || !isActive ()) return false ;
0 commit comments