Skip to main content
summaryrefslogtreecommitdiffstats
blob: d44e28e1c5939cce3c90af54283cc08d7892cad3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package org.eclipse.swt.widgets;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved
 */

import org.eclipse.swt.internal.win32.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;

class ImageList {
	int handle, background;
	Image [] images;
	static final int CREATE_FLAGS;
	static {
		int flags = OS.ILC_MASK;
		int hDC = OS.GetDC (0);
		int bits = OS.GetDeviceCaps (hDC, OS.BITSPIXEL);
		int planes = OS.GetDeviceCaps (hDC, OS.PLANES);
		OS.ReleaseDC (0, hDC);
		int depth = bits * planes;
		switch (depth) {
			case 4:
				flags |= OS.ILC_COLOR4;
				break;
			case 8:
				flags |= OS.ILC_COLOR8;
				break;
			case 16:
				flags |= OS.ILC_COLOR16;
				break;
			case 24:
				flags |= OS.ILC_COLOR24;
				break;
			case 32:
				flags |= OS.ILC_COLOR32;
				break;
			default:
				flags |= OS.ILC_COLOR;
		}
		CREATE_FLAGS = flags;
	}
	
public ImageList () {
	handle = OS.ImageList_Create (32, 32, CREATE_FLAGS, 16, 16);
	images = new Image [4];
	background = 0x00FFFFFF;
}

public int add (Image image) {
	int count = OS.ImageList_GetImageCount (handle);
	int index = 0;
	while (index < count) {
		if (images [index] != null) {
			if (images [index].isDisposed ()) images [index] = null;
		}
		if (images [index] == null) break;
		index++;
	}
	int hImage = image.handle;
	int [] cx = new int [1], cy = new int [1];
	OS.ImageList_GetIconSize (handle, cx, cy);
	switch (image.type) {
		case SWT.BITMAP: {
			if (count == 0) {
				BITMAP bm = new BITMAP ();
				OS.GetObject (hImage, BITMAP.sizeof, bm);
				cx [0] = bm.bmWidth; cy [0] = bm.bmHeight;
				OS.ImageList_SetIconSize (handle, cx [0], cy [0]);
			}
			int hBitmap = copyBitmap (hImage, cx [0], cy [0]);
			if (index == count) {
				OS.ImageList_AddMasked (handle, hBitmap, background);
			} else {
				int hMask = createMask (hBitmap, cx [0], cy [0]);
				OS.ImageList_Replace (handle, index, hBitmap, hMask);
				OS.DeleteObject (hMask);
			}
			OS.DeleteObject (hBitmap);
			break;
		}
		case SWT.ICON: {
			if (count == 0) {
				BITMAP bm = new BITMAP ();
				ICONINFO info = new ICONINFO ();
				OS.GetIconInfo (hImage, info);
				int hBitmap = info.hbmColor;
				if (hBitmap == 0) hBitmap = info.hbmMask;
				OS.GetObject (hBitmap, BITMAP.sizeof, bm);
				if (hBitmap == info.hbmMask) bm.bmHeight /= 2;
				if (info.hbmColor != 0) OS.DeleteObject (info.hbmColor);
				if (info.hbmMask != 0) OS.DeleteObject (info.hbmMask);
				cx [0] = bm.bmWidth;  cy [0] = bm.bmHeight;
				OS.ImageList_SetIconSize (handle, cx [0], cy [0]);
			}
			int hIcon = copyIcon (hImage, cx [0], cy [0]);
			OS.ImageList_ReplaceIcon (handle, index == count ? -1 : index, hIcon);
			OS.DestroyIcon (hIcon);
			break;
		}
	}
	if (index == images.length) {
		Image [] newImages = new Image [images.length + 4];
		System.arraycopy (images, 0, newImages, 0, images.length);
		images = newImages;
	}
	images [index] = image;
	return index;
}

int copyBitmap (int hImage, int width, int height) {
	BITMAP bm = new BITMAP ();
	OS.GetObject (hImage, BITMAP.sizeof, bm);
	int hDC = OS.GetDC (0);
	int hdc1 = OS.CreateCompatibleDC (hDC);
	OS.SelectObject (hdc1, hImage);
	int hdc2 = OS.CreateCompatibleDC (hDC);
	int hBitmap = OS.CreateCompatibleBitmap (hDC, width, height);
	OS.SelectObject (hdc2, hBitmap);
	OS.SetStretchBltMode(hdc2, OS.COLORONCOLOR);
	OS.StretchBlt (hdc2, 0, 0, width, height, hdc1, 0, 0, bm.bmWidth, bm.bmHeight, OS.SRCCOPY);
	OS.DeleteDC (hdc1);
	OS.DeleteDC (hdc2);
	OS.ReleaseDC (0, hDC);
	return hBitmap;
}

int copyIcon (int hImage, int width, int height) {
	int hIcon = OS.CopyImage (hImage, OS.IMAGE_ICON, width, height, OS.LR_DEFAULTCOLOR);
	return hIcon;
}

int createMask (int hBitmap, int width, int height) {
	int hDC = OS.GetDC (0);
	int hdc1 = OS.CreateCompatibleDC (hDC);
	OS.SelectObject (hdc1, hBitmap);
	int hdc2 = OS.CreateCompatibleDC (hDC);
	int hMask = OS.CreateBitmap (width, height, 1, 1, null);
	OS.SelectObject (hdc2, hMask);
	OS.SetBkColor (hdc1, background);
	OS.BitBlt (hdc2, 0, 0, width, height, hdc1, 0, 0, OS.SRCCOPY);
	OS.ReleaseDC (0, hDC);
	OS.DeleteDC (hdc1);
	OS.DeleteDC (hdc2);
	return hMask;
}

public void dispose () {
	if (handle != 0) OS.ImageList_Destroy (handle);
	handle = 0;
	images = null;
}

public Image get (int index) {
	return images [index];
}

public int getBackground () {
	return background;
}

public int getHandle () {
	return handle;
}

public int indexOf (Image image) {
	int count = OS.ImageList_GetImageCount (handle);
	for (int i=0; i<count; i++) {
		if (images [i] != null) {
			if (images [i].isDisposed ()) images [i] = null;
			if (images [i] != null && images [i].equals (image)) return i;
		}
	}
	return -1;
}

public void put (int index, Image image) {
	int count = OS.ImageList_GetImageCount (handle);
	if (!(0 <= index && index < count)) return;
	if (image != null) {
		int [] cx = new int [1], cy = new int [1];
		OS.ImageList_GetIconSize (handle, cx, cy);
		int hImage = image.handle;
		switch (image.type) {
			case SWT.BITMAP:
				int hBitmap = copyBitmap (hImage, cx [0], cy [0]);
				int hMask = createMask (hBitmap, cx [0], cy [0]);
				OS.ImageList_Replace (handle, index, hBitmap, hMask);
				OS.DeleteObject (hBitmap);
				OS.DeleteObject (hMask);
				break;
			case SWT.ICON:
				int hIcon = copyIcon (hImage, cx [0], cy [0]);
				OS.ImageList_ReplaceIcon (handle, index, hIcon);
				OS.DestroyIcon (hIcon);
				break;
		}
	}
	images [index] = image;
}

public void remove (int index) {
	int count = OS.ImageList_GetImageCount (handle);
	if (!(0 <= index && index < count)) return;
	OS.ImageList_Remove (handle, index);
	System.arraycopy (images, index + 1, images, index, --count - index);
	images [index] = null;
}

public void setBackground (int color) {
	if (background == color) return;
	background = color;
	/*
	* This code is intentionally commented.  When the background
	* color of the control changes, it is necessary to recompute
	* the masks for all bitmaps in the image list.  This can look
	* really bad when the application program assumes the original
	* color of the control and makes bitmaps accordingly.  Typically
	* this happens when the colors are change using the control panel.
	* Therefore, this code remains commented even though it is correct.
	*/
//	int length = OS.ImageList_GetImageCount (handle);
//	if (length == 0) return;
//	int [] cx = new int [1], cy = new int [1];
//	OS.ImageList_GetIconSize (handle, cx, cy);
//	int width = cx [0], height = cy [0];
//	OS.ImageList_Destroy (handle);
//	handle = OS.ImageList_Create (width, height, CREATE_FLAGS, length, 16);
//	for (int i=0; i<length; i++) {
//		Image image = images [i];
//		if (image == null || image.isDisposed ()) {
//			images [i] = null;
//			int hBitmap = OS.CreateBitmap (width, height, 1, 1, null);
//			OS.ImageList_AddMasked (handle, hBitmap, background);
//			OS.DeleteObject (hBitmap);
//		} else {
//			int hImage = image.handle;
//			switch (image.type) {
//				case SWT.BITMAP:
//					int hBitmap = copyBitmap (hImage, width, height);
//					OS.ImageList_AddMasked (handle, hBitmap, background);
//					OS.DeleteObject (hBitmap);
//					break;
//				case SWT.ICON:
//					int hIcon = copyIcon (hImage, width, height);
//					OS.ImageList_ReplaceIcon (handle, -1, hIcon);
//					OS.DestroyIcon (hIcon);
//					break;
//				default:
//					SWT.error (SWT.ERROR_NOT_IMPLEMENTED);
//					break;
//			}
//		}
//	}
}

public int size () {
	int result = 0;
	int count = OS.ImageList_GetImageCount (handle);
	for (int i=0; i<count; i++) {
		if (images [i] != null) {
			if (images [i].isDisposed ()) images [i] = null;
			if (images [i] == null) result++;
		}
	}
	return result;
}

}

Back to the top