comparison dwtx/jface/viewers/DecorationOverlayIcon.d @ 10:b6c35faf97c8

Viewers
author Frank Benoit <benoit@tionex.de>
date Mon, 31 Mar 2008 00:47:19 +0200
parents
children ea8ff534f622
comparison
equal deleted inserted replaced
9:6c14e54dfc11 10:b6c35faf97c8
1 /*******************************************************************************
2 * Copyright (c) 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.jface.viewers.DecorationOverlayIcon;
14
15 import dwtx.jface.viewers.IDecoration;
16 import dwtx.jface.util.Util;
17
18 // import tango.util.Arrays;
19
20 import dwt.graphics.Image;
21 import dwt.graphics.ImageData;
22 import dwt.graphics.Point;
23 import dwtx.jface.resource.CompositeImageDescriptor;
24 import dwtx.jface.resource.ImageDescriptor;
25
26 import dwt.dwthelper.utils;
27
28 /**
29 * A <code>DecorationOverlayIcon</code> is an image descriptor that can be used
30 * to overlay decoration images on to the 4 corner quadrants of a base image.
31 * The four quadrants are {@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
32 * {@link IDecoration#BOTTOM_LEFT} and {@link IDecoration#BOTTOM_RIGHT}. Additionally,
33 * the overlay can be used to provide an underlay corresponding to {@link IDecoration#UNDERLAY}.
34 *
35 * @since 3.3
36 * @see IDecoration
37 */
38 public class DecorationOverlayIcon : CompositeImageDescriptor {
39
40 // the base image
41 private Image base;
42
43 // the overlay images
44 private ImageDescriptor[] overlays;
45
46 // the size
47 private Point size;
48
49 /**
50 * Create the decoration overlay for the base image using the array of
51 * provided overlays. The indices of the array correspond to the values
52 * of the 5 overlay constants defined on {@link IDecoration}
53 * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
54 * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
55 * and{@link IDecoration#UNDERLAY}).
56 *
57 * @param baseImage the base image
58 * @param overlaysArray the overlay images
59 * @param sizeValue the size of the resulting image
60 */
61 public this(Image baseImage,
62 ImageDescriptor[] overlaysArray, Point sizeValue) {
63 this.base = baseImage;
64 this.overlays = overlaysArray;
65 this.size = sizeValue;
66 }
67
68 /**
69 * Create the decoration overlay for the base image using the array of
70 * provided overlays. The indices of the array correspond to the values
71 * of the 5 overlay constants defined on {@link IDecoration}
72 * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
73 * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
74 * and {@link IDecoration#UNDERLAY}).
75 *
76 * @param baseImage the base image
77 * @param overlaysArray the overlay images
78 */
79 public this(Image baseImage, ImageDescriptor[] overlaysArray) {
80 this(baseImage, overlaysArray, new Point(baseImage.getBounds().width, baseImage.getBounds().height));
81 }
82
83 /**
84 * Create a decoration overlay icon that will place the given overlay icon in
85 * the given quadrant of the base image.
86 * @param baseImage the base image
87 * @param overlayImage the overlay image
88 * @param quadrant the quadrant (one of {@link IDecoration}
89 * ({@link IDecoration#TOP_LEFT}, {@link IDecoration#TOP_RIGHT},
90 * {@link IDecoration#BOTTOM_LEFT}, {@link IDecoration#BOTTOM_RIGHT}
91 * or {@link IDecoration#UNDERLAY})
92 */
93 public this(Image baseImage, ImageDescriptor overlayImage, int quadrant) {
94 this(baseImage, createArrayFrom(overlayImage, quadrant));
95 }
96
97 /**
98 * Convert the given image and quadrant into the proper input array.
99 * @param overlayImage the overlay image
100 * @param quadrant the quadrant
101 * @return an array with the given image in the proper quadrant
102 */
103 private static ImageDescriptor[] createArrayFrom(
104 ImageDescriptor overlayImage, int quadrant) {
105 ImageDescriptor[] descs = [ cast(ImageDescriptor) null, null, null, null, null ];
106 descs[quadrant] = overlayImage;
107 return descs;
108 }
109
110 /**
111 * Draw the overlays for the receiver.
112 * @param overlaysArray
113 */
114 private void drawOverlays(ImageDescriptor[] overlaysArray) {
115
116 for (int i = 0; i < overlays.length; i++) {
117 ImageDescriptor overlay = overlaysArray[i];
118 if (overlay is null) {
119 continue;
120 }
121 ImageData overlayData = overlay.getImageData();
122 //Use the missing descriptor if it is not there.
123 if (overlayData is null) {
124 overlayData = ImageDescriptor.getMissingImageDescriptor()
125 .getImageData();
126 }
127 switch (i) {
128 case IDecoration.TOP_LEFT:
129 drawImage(overlayData, 0, 0);
130 break;
131 case IDecoration.TOP_RIGHT:
132 drawImage(overlayData, size.x - overlayData.width, 0);
133 break;
134 case IDecoration.BOTTOM_LEFT:
135 drawImage(overlayData, 0, size.y - overlayData.height);
136 break;
137 case IDecoration.BOTTOM_RIGHT:
138 drawImage(overlayData, size.x - overlayData.width, size.y
139 - overlayData.height);
140 break;
141 }
142 }
143 }
144
145 /* (non-Javadoc)
146 * @see java.lang.Object#equals(java.lang.Object)
147 */
148 public override int opEquals(Object o) {
149 if (!( cast(DecorationOverlayIcon)o )) {
150 return false;
151 }
152 DecorationOverlayIcon other = cast(DecorationOverlayIcon) o;
153 return base.opEquals(other.base)
154 && Util.opEquals(overlays, other.overlays);
155 }
156
157 /* (non-Javadoc)
158 * @see java.lang.Object#hashCode()
159 */
160 public override hash_t toHash() {
161 int code = base.toHash();
162 for (int i = 0; i < overlays.length; i++) {
163 if (overlays[i] !is null) {
164 code ^= overlays[i].toHash();
165 }
166 }
167 return code;
168 }
169
170 /* (non-Javadoc)
171 * @see dwtx.jface.resource.CompositeImageDescriptor#drawCompositeImage(int, int)
172 */
173 protected void drawCompositeImage(int width, int height) {
174 if (overlays.length > IDecoration.UNDERLAY) {
175 ImageDescriptor underlay = overlays[IDecoration.UNDERLAY];
176 if (underlay !is null) {
177 drawImage(underlay.getImageData(), 0, 0);
178 }
179 }
180 drawImage(base.getImageData(), 0, 0);
181 drawOverlays(overlays);
182 }
183
184 /* (non-Javadoc)
185 * @see dwtx.jface.resource.CompositeImageDescriptor#getSize()
186 */
187 protected Point getSize() {
188 return size;
189 }
190
191 /* (non-Javadoc)
192 * @see dwtx.jface.resource.CompositeImageDescriptor#getTransparentPixel()
193 */
194 protected int getTransparentPixel() {
195 return base.getImageData().transparentPixel;
196 }
197
198 }