comparison dwtx/draw2d/Shape.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.Shape;
14
15 import dwt.dwthelper.utils;
16 import dwtx.draw2d.Figure;
17 import dwtx.draw2d.Graphics;
18 import dwtx.draw2d.ColorConstants;
19
20 /**
21 * Provides abstract support for a variety of shapes.
22 * <p>
23 * When customizing shapes, you shouldn't override paintFigure(). Override fillShape()
24 * and outlineShape() methods instead.
25 */
26 public abstract class Shape
27 : Figure
28 {
29
30 /** The width of this shape's outline. */
31 protected int lineWidth = 1;
32 /** The line style to be used for this shape's outline. */
33 protected int lineStyle;
34
35 private bool
36 fill = true,
37 outline = true,
38 xorFill,
39 xorOutline;
40
41 /**
42 * Default constructor.
43 *
44 * @since 2.0
45 */
46 public this() {
47 lineStyle = Graphics.LINE_SOLID;
48 }
49
50 /**
51 * Fills the interior of the shape with the background color.
52 * @param graphics the graphics object
53 */
54 protected abstract void fillShape(Graphics graphics);
55
56 /**
57 * Returns the line style used to outline this shape.
58 * @return the line style
59 */
60 public int getLineStyle() {
61 return lineStyle;
62 }
63
64 /**
65 * Returns the line width of this shape's outline.
66 * @return the line width
67 */
68 public int getLineWidth() {
69 return lineWidth;
70 }
71
72 /**
73 * Returns <code>false</code> as shapes only draw themselves onto other figures, and
74 * generally dont have rectangular dimensions.
75 *
76 * @see Figure#isOpaque()
77 * @since 2.0
78 */
79 public bool isOpaque() {
80 return false;
81 }
82
83 /**
84 * Outlines this shape using the foreground color.
85 * @param graphics the graphics object
86 */
87 protected abstract void outlineShape(Graphics graphics);
88
89 /**
90 * Paints the shape. Each shape has an outline to draw, and a region to fill within that
91 * outline. Disabled shapes must visually depict the disabled state.
92 *
93 * @see Figure#paintFigure(Graphics)
94 */
95 public void paintFigure(Graphics graphics) {
96 if (!isEnabled()) {
97 graphics.translate(1, 1);
98 graphics.setBackgroundColor(ColorConstants.buttonLightest);
99 graphics.setForegroundColor(ColorConstants.buttonLightest);
100 if (fill) {
101 graphics.setXORMode(xorFill);
102 fillShape(graphics);
103 }
104 if (outline) {
105 graphics.setXORMode(xorOutline);
106 graphics.setLineStyle(lineStyle);
107 graphics.setLineWidth(lineWidth);
108 outlineShape(graphics);
109 }
110 graphics.setBackgroundColor(ColorConstants.buttonDarker);
111 graphics.setForegroundColor(ColorConstants.buttonDarker);
112 graphics.translate(-1, -1);
113 }
114 if (fill) {
115 graphics.setXORMode(xorFill);
116 fillShape(graphics);
117 }
118 if (outline) {
119 graphics.setXORMode(xorOutline);
120 graphics.setLineStyle(lineStyle);
121 graphics.setLineWidth(lineWidth);
122 outlineShape(graphics);
123 }
124 }
125
126 /**
127 * Sets whether this shape should fill its region or not. It repaints this figure.
128 *
129 * @param b fill state
130 * @since 2.0
131 */
132 public void setFill(bool b) {
133 if (fill is b)
134 return;
135 fill = b;
136 repaint();
137 }
138
139 /**
140 * Sets whether XOR based fill should be used by the shape. It repaints this figure.
141 *
142 * @param b XOR fill state
143 * @since 2.0
144 */
145 public void setFillXOR(bool b) {
146 if (xorFill is b)
147 return;
148 xorFill = b;
149 repaint();
150 }
151
152 /**
153 * Sets the line width to be used to outline the shape.
154 *
155 * @param w the new width
156 * @since 2.0
157 */
158 public void setLineWidth(int w) {
159 if (lineWidth is w)
160 return;
161 lineWidth = w;
162 repaint();
163 }
164
165 /**
166 * Sets the style of line to be used by this shape.
167 *
168 * @param s the new line style
169 * @since 2.0
170 */
171 public void setLineStyle(int s) {
172 if (lineStyle is s)
173 return;
174 lineStyle = s;
175 repaint();
176 }
177
178 /**
179 * Sets whether the outline should be drawn for this shape.
180 *
181 * @param b <code>true</code> if the shape should be outlined
182 * @since 2.0
183 */
184 public void setOutline(bool b) {
185 if (outline is b)
186 return;
187 outline = b;
188 repaint();
189 }
190
191 /**
192 * Sets whether XOR based outline should be used for this shape.
193 *
194 * @param b <code>true</code> if the outline should be XOR'ed
195 * @since 2.0
196 */
197 public void setOutlineXOR(bool b) {
198 if (xorOutline is b)
199 return;
200 xorOutline = b;
201 repaint();
202 }
203
204 /**
205 * Sets whether XOR based fill and XOR based outline should be used for this shape.
206 *
207 * @param b <code>true</code> if the outline and fill should be XOR'ed
208 * @since 2.0
209 */
210 public void setXOR(bool b) {
211 xorOutline = xorFill = b;
212 repaint();
213 }
214
215 }