Skip to content

Commit b945716

Browse files
committed
[Win32] Encapsulate ToolItem's access to tool bar image lists
ToolItem manipulated ToolBar's three normal/hot/disabled ImageLists directly: reading them via ToolBar's getters, creating them inline when absent, and calling add()/put() on each of them itself. This spread the bookkeeping for a single item's images across both classes and made ToolItem responsible for details that are really ToolBar's to own, such as lazily creating the image lists sized to the first image added. With this change, ToolBar exposes addImage/putImage/clearImage instead, and ToolItem goes through these instead of touching ImageList directly. ToolBar's internal representation (three separate ImageList fields, and their existing setImageList/setHotImageList/setDisabledImageList synchronization methods) is otherwise unchanged. This is a behavior-preserving refactoring: the image lists are still created, filled and synchronized with the same values as before. The disabledImageList != null guard in ToolItem.updateImages is dropped rather than moved: in that branch the item already has an image index, so all three image lists necessarily exist, as they are only ever created together in addImage and cleared together in destroyItem and releaseWidget. Related to #3466
1 parent bb1b092 commit b945716

2 files changed

Lines changed: 57 additions & 44 deletions

File tree

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class ToolBar extends Composite {
5555
ToolItem [] items;
5656
ToolItem [] tabItemList;
5757
boolean ignoreResize, ignoreMouse;
58-
ImageList imageList, disabledImageList, hotImageList;
58+
private ImageList imageList, disabledImageList, hotImageList;
5959
static final long ToolBarProc;
6060
static final TCHAR ToolBarClass = new TCHAR (OS.TOOLBARCLASSNAME, true);
6161
static {
@@ -143,6 +143,34 @@ public ToolBar (Composite parent, int style) {
143143
}
144144
}
145145

146+
/*
147+
* The given image bounds are the bounds of the tool item's image and determine which shared image
148+
* lists are used. They are intentionally not derived from the images actually added: for a disabled
149+
* item with CHECK or RADIO style, those are the disabled images, which may have different bounds.
150+
* Note that the icon size of an image list is defined by the first image added to it.
151+
*/
152+
int addImage(Rectangle imageBounds, Image image, Image hotImage, Image disabledImage) {
153+
int listStyle = style & SWT.RIGHT_TO_LEFT;
154+
if (imageList == null) {
155+
imageList = display.getImageListToolBar(listStyle, imageBounds.width, imageBounds.height, getAutoscalingZoom());
156+
}
157+
if (hotImageList == null) {
158+
hotImageList = display.getImageListToolBarHot(listStyle, imageBounds.width, imageBounds.height,
159+
getAutoscalingZoom());
160+
}
161+
if (disabledImageList == null) {
162+
disabledImageList = display.getImageListToolBarDisabled(listStyle, imageBounds.width, imageBounds.height,
163+
getAutoscalingZoom());
164+
}
165+
int index = imageList.add(image);
166+
hotImageList.add(hotImage);
167+
disabledImageList.add(disabledImage);
168+
setImageList(imageList);
169+
setHotImageList(hotImageList);
170+
setDisabledImageList(disabledImageList);
171+
return index;
172+
}
173+
146174
@Override
147175
long callWindowProc (long hwnd, int msg, long wParam, long lParam) {
148176
if (handle == 0) return 0;
@@ -199,6 +227,10 @@ public void layout (boolean changed) {
199227
super.layout(changed);
200228
}
201229

230+
void clearImage(int index) {
231+
putImage(index, null, null, null);
232+
}
233+
202234
void clearSizeCache(boolean changed) {
203235
// If changed, discard the cached layout information
204236
if (changed) {
@@ -869,6 +901,18 @@ boolean mnemonicMatch (char ch) {
869901
return findMnemonic (items [id [0]].text) != '\0';
870902
}
871903

904+
void putImage(int index, Image image, Image hotImage, Image disabledImage) {
905+
if (imageList != null) {
906+
imageList.put(index, image);
907+
}
908+
if (hotImageList != null) {
909+
hotImageList.put(index, hotImage);
910+
}
911+
if (disabledImageList != null) {
912+
disabledImageList.put(index, disabledImage);
913+
}
914+
}
915+
872916
@Override
873917
void releaseChildren (boolean destroy) {
874918
if (items != null) {

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

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,7 @@ void releaseImages () {
535535
* an image and one is never assigned, this is not a problem.
536536
*/
537537
if ((info.fsStyle & OS.BTNS_SEP) == 0 && info.iImage != OS.I_IMAGENONE) {
538-
ImageList imageList = parent.getImageList ();
539-
ImageList hotImageList = parent.getHotImageList ();
540-
ImageList disabledImageList = parent.getDisabledImageList();
541-
if (imageList != null) imageList.put (info.iImage, null);
542-
if (hotImageList != null) hotImageList.put (info.iImage, null);
543-
if (disabledImageList != null) disabledImageList.put (info.iImage, null);
538+
parent.clearImage(info.iImage);
544539
}
545540
}
546541

@@ -1099,21 +1094,7 @@ void updateImages (boolean enabled) {
10991094
info.dwMask = OS.TBIF_IMAGE;
11001095
OS.SendMessage (hwnd, OS.TB_GETBUTTONINFO, id, info);
11011096
if (info.iImage == OS.I_IMAGENONE && image == null) return;
1102-
ImageList imageList = parent.getImageList ();
1103-
ImageList hotImageList = parent.getHotImageList ();
1104-
ImageList disabledImageList = parent.getDisabledImageList();
11051097
if (info.iImage == OS.I_IMAGENONE) {
1106-
Rectangle boundsInPoints = image.getBounds();
1107-
int listStyle = parent.style & SWT.RIGHT_TO_LEFT;
1108-
if (imageList == null) {
1109-
imageList = display.getImageListToolBar (listStyle, boundsInPoints.width, boundsInPoints.height, getAutoscalingZoom());
1110-
}
1111-
if (disabledImageList == null) {
1112-
disabledImageList = display.getImageListToolBarDisabled (listStyle, boundsInPoints.width, boundsInPoints.height, getAutoscalingZoom());
1113-
}
1114-
if (hotImageList == null) {
1115-
hotImageList = display.getImageListToolBarHot (listStyle, boundsInPoints.width, boundsInPoints.height, getAutoscalingZoom());
1116-
}
11171098
Image disabled = disabledImage;
11181099
if (disabledImage == null) {
11191100
if (disabledImage2 != null) disabledImage2.dispose ();
@@ -1134,27 +1115,19 @@ void updateImages (boolean enabled) {
11341115
if ((style & (SWT.CHECK | SWT.RADIO)) != 0) {
11351116
if (!enabled) image2 = hot = disabled;
11361117
}
1137-
info.iImage = imageList.add (image2);
1138-
disabledImageList.add (disabled);
1139-
hotImageList.add (hot != null ? hot : image2);
1140-
parent.setImageList (imageList);
1141-
parent.setDisabledImageList (disabledImageList);
1142-
parent.setHotImageList (hotImageList);
1118+
info.iImage = parent.addImage(image.getBounds(), image2, hot != null ? hot : image2, disabled);
11431119
} else {
11441120
Image disabled = null;
1145-
if (disabledImageList != null) {
1146-
if (image != null) {
1147-
if (disabledImage2 != null) disabledImage2.dispose ();
1148-
disabledImage2 = null;
1149-
disabled = disabledImage;
1150-
if (disabledImage == null) {
1151-
disabled = image;
1152-
if (!enabled) {
1153-
disabled = disabledImage2 = new Image (display, image, SWT.IMAGE_DISABLE);
1154-
}
1121+
if (image != null) {
1122+
if (disabledImage2 != null) disabledImage2.dispose ();
1123+
disabledImage2 = null;
1124+
disabled = disabledImage;
1125+
if (disabledImage == null) {
1126+
disabled = image;
1127+
if (!enabled) {
1128+
disabled = disabledImage2 = new Image (display, image, SWT.IMAGE_DISABLE);
11551129
}
11561130
}
1157-
disabledImageList.put (info.iImage, disabled);
11581131
}
11591132
/*
11601133
* Bug in Windows. When a tool item with the style
@@ -1167,12 +1140,8 @@ void updateImages (boolean enabled) {
11671140
if ((style & (SWT.CHECK | SWT.RADIO)) != 0) {
11681141
if (!enabled) image2 = hot = disabled;
11691142
}
1170-
if (imageList != null) {
1171-
imageList.put (info.iImage, image2);
1172-
}
1173-
if (hotImageList != null) {
1174-
hotImageList.put (info.iImage, hot != null ? hot : image2);
1175-
}
1143+
1144+
parent.putImage(info.iImage, image2, hot != null ? hot : image2, disabled);
11761145
if (image == null) info.iImage = OS.I_IMAGENONE;
11771146
}
11781147

0 commit comments

Comments
 (0)