comparison dwtx/draw2d/ImageFigure.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children
comparison
equal deleted inserted replaced
96:b492ba44e44d 98:95307ad235d9
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 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.draw2d.ImageFigure;
14
15 import dwt.dwthelper.utils;
16
17
18
19 import dwt.graphics.Image;
20 import dwtx.draw2d.geometry.Dimension;
21 import dwtx.draw2d.geometry.Insets;
22 import dwtx.draw2d.geometry.Rectangle;
23 import dwtx.draw2d.IFigure;
24 import dwtx.draw2d.Figure;
25 import dwtx.draw2d.Graphics;
26 import dwtx.draw2d.PositionConstants;
27
28 /**
29 * A Figure that simply contains an Image. Use this Figure, instead of a Label, when
30 * displaying Images without any accompanying text. This figure is not intended to have a
31 * layout mananger or children.
32 * <P>
33 * Note that it is the client's responsibility to dispose the given image. There is no
34 * "free" resource management in draw2d.
35 *
36 * @author Pratik Shah
37 */
38 public class ImageFigure
39 : Figure
40 {
41
42 private Image img;
43 private Dimension size;
44 private int alignment;
45
46 /**
47 * Constructor<br>
48 * The default alignment is <code>PositionConstants.CENTER</code>.
49 */
50 public this() {
51 this(null, PositionConstants.CENTER);
52 }
53
54 /**
55 * Constructor<br>
56 * The default alignment is <code>PositionConstants.CENTER</code>.
57 *
58 * @param image The Image to be displayed
59 */
60 public this(Image image) {
61 this(image, PositionConstants.CENTER);
62 }
63
64 /**
65 * Constructor
66 *
67 * @param image The Image to be displayed
68 * @param alignment A PositionConstant indicating the alignment
69 *
70 * @see ImageFigure#setImage(Image)
71 * @see ImageFigure#setAlignment(int)
72 */
73 public this(Image image, int alignment) {
74 size = new Dimension();
75 setImage(image);
76 setAlignment(alignment);
77 }
78
79 /**
80 * @return The Image that this Figure displays
81 */
82 public Image getImage() {
83 return img;
84 }
85
86 /**
87 * Calculates the necessary size to display the Image within the figure's client area.
88 *
89 * @see dwtx.draw2d.Figure#getPreferredSize(int, int)
90 */
91 public Dimension getPreferredSize(int wHint, int hHint) {
92 if (getInsets() is IFigure_NO_INSETS)
93 return size;
94 Insets i = getInsets();
95 return size.getExpanded(i.getWidth(), i.getHeight());
96 }
97
98 /**
99 * @see dwtx.draw2d.Figure#paintFigure(Graphics)
100 */
101 protected void paintFigure(Graphics graphics) {
102 super.paintFigure(graphics);
103
104 if (getImage() is null)
105 return;
106
107 int x, y;
108 Rectangle area = getClientArea();
109 switch (alignment & PositionConstants.NORTH_SOUTH) {
110 case PositionConstants.NORTH:
111 y = area.y;
112 break;
113 case PositionConstants.SOUTH:
114 y = area.y + area.height - size.height;
115 break;
116 default:
117 y = (area.height - size.height) / 2 + area.y;
118 break;
119 }
120 switch (alignment & PositionConstants.EAST_WEST) {
121 case PositionConstants.EAST:
122 x = area.x + area.width - size.width;
123 break;
124 case PositionConstants.WEST:
125 x = area.x;
126 break;
127 default:
128 x = (area.width - size.width) / 2 + area.x;
129 break;
130 }
131 graphics.drawImage(getImage(), x, y);
132 }
133
134 /**
135 * Sets the alignment of the Image within this Figure. The alignment comes into play
136 * when the ImageFigure is larger than the Image. The alignment could be any valid
137 * combination of the following:
138 *
139 * <UL>
140 * <LI>PositionConstants.NORTH</LI>
141 * <LI>PositionConstants.SOUTH</LI>
142 * <LI>PositionConstants.EAST</LI>
143 * <LI>PositionConstants.WEST</LI>
144 * <LI>PositionConstants.CENTER or PositionConstants.NONE</LI>
145 * </UL>
146 *
147 * @param flag A constant indicating the alignment
148 */
149 public void setAlignment(int flag) {
150 alignment = flag;
151 }
152
153 /**
154 * Sets the Image that this ImageFigure displays.
155 * <p>
156 * IMPORTANT: Note that it is the client's responsibility to dispose the given image.
157 *
158 * @param image The Image to be displayed. It can be <code>null</code>.
159 */
160 public void setImage(Image image) {
161 if (img is image)
162 return;
163 img = image;
164 if (img !is null)
165 size = (new Rectangle(image.getBounds())).getSize();
166 else
167 size = new Dimension();
168 revalidate();
169 repaint();
170 }
171
172 }