comparison org.eclipse.ui.forms/src/org/eclipse/ui/forms/widgets/ImageHyperlink.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 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 * Chriss Gross (schtoo@schtoo.com) - fix for 61670
11 * Port to the D programming language:
12 * Frank Benoit <benoit@tionex.de>
13 *******************************************************************************/
14 module org.eclipse.ui.forms.widgets.ImageHyperlink;
15
16 import org.eclipse.ui.forms.widgets.Hyperlink;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.GC;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.graphics.Point;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Event;
25
26 import java.lang.all;
27 import java.util.Set;
28
29 /**
30 * This class extends hyperlink widget by adding the capability to render an
31 * image relative to the text. If no text has been set, only image will be
32 * shown. Images for hover and active states can be set in addition to the
33 * normal state image.
34 * <p>
35 * When image is taller than the text, additional style can be provided to
36 * control vertical alignment (supported values are SWT.TOP, SWT.BOTTOM and
37 * SWT.CENTER).
38 * <p>
39 * The class does not need to be sublassed but it is allowed to do so if some
40 * aspect of the image hyperlink needs to be modified.
41 *
42 * @since 3.0
43 */
44 public class ImageHyperlink : Hyperlink {
45 /**
46 * Amount of pixels between the image and the text (default is 5).
47 */
48 public int textSpacing = 5;
49
50 private Image image;
51
52 private Image hoverImage;
53
54 private Image activeImage;
55
56 private int state;
57
58 private static const int HOVER = 1 << 1;
59
60 private static const int ACTIVE = 1 << 2;
61
62 private int verticalAlignment = SWT.CENTER;
63
64 private int horizontalAlignment = SWT.LEFT;
65
66 /**
67 * Creates the image hyperlink instance.
68 *
69 * @param parent
70 * the control parent
71 * @param style
72 * the control style (SWT.WRAP, BOTTOM, TOP, MIDDLE, LEFT, RIGHT)
73 */
74 public this(Composite parent, int style) {
75 super(parent, removeAlignment(style));
76 extractAlignment(style);
77 }
78
79 /*
80 * (non-Javadoc)
81 *
82 * @see org.eclipse.ui.forms.widgets.AbstractHyperlink#paintHyperlink(org.eclipse.swt.events.PaintEvent)
83 */
84 protected void paintHyperlink(GC gc) {
85 paintHyperlink(gc, getClientArea());
86 }
87
88 protected void paintHyperlink(GC gc, Rectangle bounds) {
89 Image image = null;
90 if ((state & ACTIVE) !is 0)
91 image = activeImage;
92 else if ((state & HOVER) !is 0)
93 image = hoverImage;
94 if (image is null)
95 image = this.image;
96 Rectangle ibounds = image !is null ? image.getBounds() : new Rectangle(0, 0, 0, 0);
97 Point maxsize = computeMaxImageSize();
98 int spacing = image !is null?textSpacing:0;
99 int textWidth = bounds.width - maxsize.x - spacing
100 - marginWidth - marginWidth;
101 int y = bounds.y+marginHeight + maxsize.y / 2 - ibounds.height / 2;
102
103 if (horizontalAlignment is SWT.LEFT) {
104 int x = bounds.x+marginWidth + maxsize.x / 2 - ibounds.width / 2;
105 int textX = bounds.x + marginWidth + maxsize.x + spacing;
106 if (image !is null)
107 gc.drawImage(image, x, y);
108 if (getText() !is null)
109 drawText(gc, bounds, textX, textWidth);
110 } else if (horizontalAlignment is SWT.RIGHT) {
111 int x = bounds.x+marginWidth;
112 if (getText() !is null) {
113 x += drawText(gc, bounds, x, textWidth);
114 }
115 x += maxsize.x / 2 - ibounds.width / 2 + spacing;
116 if (image !is null)
117 gc.drawImage(image, x, y);
118 }
119 }
120
121 private int drawText(GC gc, Rectangle clientArea, int textX, int textWidth) {
122 Point textSize = computeTextSize(textWidth, SWT.DEFAULT);
123 int slotHeight = clientArea.height - marginHeight - marginHeight;
124 int textY;
125 textWidth = textSize.x;
126 int textHeight = textSize.y;
127 if (verticalAlignment is SWT.BOTTOM) {
128 textY = marginHeight + slotHeight - textHeight;
129 } else if (verticalAlignment is SWT.CENTER) {
130 textY = marginHeight + slotHeight / 2 - textHeight / 2;
131 } else {
132 textY = marginHeight;
133 }
134 paintText(gc, new Rectangle(textX, textY, textWidth, textHeight));
135 return textWidth;
136 }
137
138 /**
139 * Computes the control size by reserving space for images in addition to
140 * text.
141 *
142 * @param wHint
143 * width hint
144 * @param hHint
145 * height hint
146 * @param changed
147 * if <code>true</code>, any cached layout data should be
148 * computed anew
149 */
150 public Point computeSize(int wHint, int hHint, bool changed) {
151 checkWidget();
152 Point isize = computeMaxImageSize();
153 int spacing = isize.x>0?textSpacing:0;
154 Point textSize = null;
155 if (getText() !is null) {
156 int innerWHint = wHint;
157 if (wHint !is SWT.DEFAULT) {
158 innerWHint = wHint - 2 * marginWidth - isize.x - spacing;
159 }
160 textSize = super.computeSize(innerWHint, hHint, changed);
161 }
162 int width = isize.x;
163 int height = isize.y;
164 if (textSize !is null) {
165 width += spacing;
166 width += textSize.x;
167 height = Math.max(height, textSize.y);
168 }
169 width += 2 * marginWidth;
170 height += 2 * marginHeight;
171 return new Point(width, height);
172 }
173
174 protected void handleEnter(Event e) {
175 state = HOVER;
176 super.handleEnter(e);
177 }
178
179 protected void handleExit(Event e) {
180 state = 0;
181 super.handleExit(e);
182 }
183
184 protected void handleActivate(Event e) {
185 state &= ACTIVE;
186 redraw();
187 super.handleActivate(e);
188 state &= ~ACTIVE;
189 if (!isDisposed())
190 redraw();
191 }
192
193 /**
194 * Returns active image.
195 *
196 * @return active image or <code>null</code> if not set.
197 */
198 public Image getActiveImage() {
199 return activeImage;
200 }
201
202 /**
203 * Sets the image to show when link is activated.
204 *
205 * @param activeImage
206 *
207 */
208 public void setActiveImage(Image activeImage) {
209 this.activeImage = activeImage;
210 }
211
212 /**
213 * Returns the hover image.
214 *
215 * @return hover image or <code>null</code> if not set.
216 */
217 public Image getHoverImage() {
218 return hoverImage;
219 }
220
221 /**
222 * Sets the image to show when link is hover state (on mouse over).
223 *
224 * @param hoverImage
225 */
226 public void setHoverImage(Image hoverImage) {
227 this.hoverImage = hoverImage;
228 }
229
230 /**
231 * Returns the image to show in the normal state.
232 *
233 * @return normal image or <code>null</code> if not set.
234 */
235 public Image getImage() {
236 return image;
237 }
238
239 /**
240 * Sets the image to show when link is in the normal state.
241 *
242 * @param image
243 */
244 public void setImage(Image image) {
245 this.image = image;
246 }
247
248 private Point computeMaxImageSize() {
249 int x = 0;
250 int y = 0;
251 if (image !is null) {
252 x = Math.max(image.getBounds().width, x);
253 y = Math.max(image.getBounds().height, y);
254 }
255 if (hoverImage !is null) {
256 x = Math.max(hoverImage.getBounds().width, x);
257 y = Math.max(hoverImage.getBounds().height, y);
258 }
259 if (activeImage !is null) {
260 x = Math.max(activeImage.getBounds().width, x);
261 y = Math.max(activeImage.getBounds().height, y);
262 }
263 return new Point(x, y);
264 }
265
266 private static int removeAlignment(int style) {
267 int resultStyle = style;
268 if ((style & SWT.CENTER) !is 0) {
269 resultStyle &= (~SWT.CENTER);
270 }
271 if ((style & SWT.TOP) !is 0) {
272 resultStyle &= (~SWT.TOP);
273 }
274 if ((style & SWT.BOTTOM) !is 0) {
275 resultStyle &= (~SWT.BOTTOM);
276 }
277 if ((style & SWT.LEFT) !is 0) {
278 resultStyle &= (~SWT.LEFT);
279 }
280 if ((style & SWT.RIGHT) !is 0) {
281 resultStyle &= (~SWT.RIGHT);
282 }
283 return resultStyle;
284 }
285
286 private void extractAlignment(int style) {
287 if ((style & SWT.CENTER) !is 0) {
288 verticalAlignment = SWT.CENTER;
289 } else if ((style & SWT.TOP) !is 0) {
290 verticalAlignment = SWT.TOP;
291 } else if ((style & SWT.BOTTOM) !is 0) {
292 verticalAlignment = SWT.BOTTOM;
293 }
294 if ((style & SWT.LEFT) !is 0) {
295 horizontalAlignment = SWT.LEFT;
296 } else if ((style & SWT.RIGHT) !is 0) {
297 horizontalAlignment = SWT.RIGHT;
298 }
299 }
300 }