comparison org.eclipse.draw2d/src/org/eclipse/draw2d/text/BlockFlow.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 dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 org.eclipse.draw2d.text.BlockFlow;
14
15 import java.lang.all;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.draw2d.ColorConstants;
19 import org.eclipse.draw2d.Graphics;
20 import org.eclipse.draw2d.IFigure;
21 import org.eclipse.draw2d.PositionConstants;
22 import org.eclipse.draw2d.geometry.Insets;
23 import org.eclipse.draw2d.geometry.Rectangle;
24 import org.eclipse.draw2d.text.FlowFigure;
25 import org.eclipse.draw2d.text.BlockBox;
26 import org.eclipse.draw2d.text.BidiProcessor;
27 import org.eclipse.draw2d.text.FlowFigureLayout;
28 import org.eclipse.draw2d.text.BidiChars;
29 import org.eclipse.draw2d.text.BlockFlowLayout;
30 import org.eclipse.draw2d.text.FlowBorder;
31
32 /**
33 * A <code>FlowFigure</code> represented by a single {@link BlockBox} containing one or
34 * more lines. A BlockFlow is a creator of LineBoxes, which its children require during
35 * layout. A BlockFlow can be thought of as a foundation for a paragraph.
36 * <P>
37 * BlockFlows must be parented by a <code>FlowFigure</code>. {@link FlowPage} can be
38 * used as a "root" block and can be parented by normal Figures.
39 * <P>
40 * Only {@link FlowFigure}s can be added to a BlockFlow.
41 * <P>
42 * WARNING: This class is not intended to be subclassed by clients.
43 * @author hudsonr
44 * @since 2.1
45 */
46 public class BlockFlow
47 : FlowFigure
48 {
49
50 private const BlockBox blockBox;
51 private int alignment = PositionConstants.NONE;
52 private int orientation = SWT.NONE;
53 private bool bidiValid;
54
55 /**
56 * Constructs a new BlockFlow.
57 */
58 public this() {
59 blockBox = createBlockBox();
60 }
61
62 /**
63 * BlockFlows contribute a paragraph separator so as to keep the Bidi state of the text
64 * on either side of this block from affecting each other. Since each block is like a
65 * different paragraph, it does not contribute any actual text to its containing block.
66 *
67 * @see org.eclipse.draw2d.text.FlowFigure#contributeBidi(org.eclipse.draw2d.text.BidiProcessor)
68 */
69 protected void contributeBidi(BidiProcessor proc) {
70 proc.addControlChar(BidiChars.P_SEP);
71 }
72
73 BlockBox createBlockBox() {
74 return new BlockBox(this);
75 }
76
77 /**
78 * @see org.eclipse.draw2d.text.FlowFigure#createDefaultFlowLayout()
79 */
80 protected FlowFigureLayout createDefaultFlowLayout() {
81 return new BlockFlowLayout(this);
82 }
83
84 /**
85 * Returns the BlockBox associated with this.
86 * @return This BlockFlow's BlockBox
87 */
88 protected BlockBox getBlockBox() {
89 return blockBox;
90 }
91 package BlockBox getBlockBox_package() {
92 return getBlockBox();
93 }
94
95 int getBottomMargin() {
96 int margin = 0;
97 if (auto border = cast(FlowBorder)getBorder() ) {
98 return border.getBottomMargin();
99 }
100 List children = getChildren();
101 int childIndex = children.size() - 1;
102 if (childIndex >= 0 && null !is cast(BlockFlow)children.get(childIndex) ) {
103 margin = Math.max(margin,
104 (cast(BlockFlow)children.get(childIndex)).getBottomMargin());
105 }
106 return margin;
107 }
108
109 /**
110 * Returns the effective horizontal alignment. This method will never return {@link
111 * PositionConstants#NONE}. If the value is none, it will return the inherited alignment.
112 * If no alignment was inherited, it will return the default alignment ({@link
113 * PositionConstants#LEFT}).
114 * @return the effective alignment
115 */
116 public int getHorizontalAligment() {
117 if (alignment !is PositionConstants.NONE)
118 return alignment;
119 IFigure parent = getParent();
120 while (parent !is null && !( null !is cast(BlockFlow)parent ))
121 parent = parent.getParent();
122 if (parent !is null)
123 return (cast(BlockFlow)parent).getHorizontalAligment();
124 return PositionConstants.LEFT;
125 }
126
127 int getLeftMargin() {
128 if ( auto b = cast(FlowBorder)getBorder() )
129 return b.getLeftMargin();
130 return 0;
131 }
132
133 /**
134 * Returns the orientation set on this block.
135 * @return LTR, RTL or NONE
136 * @see #setOrientation(int)
137 * @since 3.1
138 */
139 public int getLocalOrientation() {
140 return orientation;
141 }
142
143 /**
144 * Returns the horizontal alignment set on this block.
145 * @return LEFT, RIGHT, ALWAYS_LEFT, ALWAYS_RIGHT, NONE
146 * @see #setHorizontalAligment(int)
147 * @since 3.1
148 */
149 public int getLocalHorizontalAlignment() {
150 return alignment;
151 }
152
153 /**
154 * Returns this block's Bidi orientation. If none was set on this block, it
155 * will inherit the one from its containing block. If there is no containing block, it
156 * will return the default orientation (SWT.RIGHT_TO_LEFT if mirrored; SWT.LEFT_TO_RIGHT
157 * otherwise).
158 *
159 * @return SWT.RIGHT_TO_LEFT or SWT.LEFT_TO_RIGHT
160 * @see #setOrientation(int)
161 * @since 3.1
162 */
163 public int getOrientation() {
164 if (orientation !is SWT.NONE)
165 return orientation;
166 IFigure parent = getParent();
167 while (parent !is null && !(null !is cast(BlockFlow)parent ))
168 parent = parent.getParent();
169 if (parent !is null)
170 return (cast(BlockFlow)parent).getOrientation();
171 return isMirrored() ? SWT.RIGHT_TO_LEFT : SWT.LEFT_TO_RIGHT;
172 }
173
174 int getRightMargin() {
175 if (auto b = cast(FlowBorder)getBorder() )
176 return b.getRightMargin();
177 return 0;
178 }
179
180 int getTopMargin() {
181 int margin = 0;
182 if (auto border = cast(FlowBorder)getBorder() ) {
183 return border.getTopMargin();
184 }
185 List children = getChildren();
186 if (children.size() > 0 && null !is cast(BlockFlow)children.get(0) ) {
187 margin = Math.max(margin,
188 (cast(BlockFlow)children.get(0)).getTopMargin());
189 }
190 return margin;
191 }
192
193 /**
194 * @see org.eclipse.draw2d.Figure#paintBorder(org.eclipse.draw2d.Graphics)
195 */
196 public void paintBorder(Graphics graphics) {
197 if ( auto b = cast(FlowBorder)getBorder() ) {
198 Rectangle where = getBlockBox().toRectangle();
199 where.crop(new Insets(getTopMargin(), getLeftMargin(),
200 getBottomMargin(), getRightMargin()));
201 (cast(FlowBorder)getBorder()).paint(this, graphics, where, SWT.LEAD | SWT.TRAIL);
202 } else
203 super.paintBorder(graphics);
204 if (selectionStart !is -1) {
205 graphics.restoreState();
206 graphics.setXORMode(true);
207 graphics.setBackgroundColor(ColorConstants.white);
208 graphics.fillRectangle(getBounds());
209 }
210 }
211
212 /**
213 * @see org.eclipse.draw2d.text.FlowFigure#postValidate()
214 */
215 public void postValidate() {
216 Rectangle newBounds = getBlockBox().toRectangle();
217 newBounds.crop(new Insets(getTopMargin(), getLeftMargin(),
218 getBottomMargin(), getRightMargin()));
219 setBounds(newBounds);
220 }
221
222 /**
223 * @see FlowFigure#revalidate()
224 */
225 public void revalidate() {
226 BlockFlowLayout layout = cast(BlockFlowLayout)getLayoutManager();
227 layout.blockContentsChanged();
228 super.revalidate();
229 }
230
231 /**
232 * A Block will invalidate the Bidi state of all its children, so that it is
233 * re-evaluated when this block is next validated.
234 * @see org.eclipse.draw2d.text.FlowFigure#revalidateBidi(org.eclipse.draw2d.IFigure)
235 */
236 protected void revalidateBidi(IFigure origin) {
237 if (bidiValid) {
238 bidiValid = false;
239 revalidate();
240 }
241 }
242
243 /**
244 * Sets the horitontal aligment of the block. Valid values are:
245 * <UL>
246 * <LI>{@link PositionConstants#NONE NONE} - (default) Alignment is inherited from
247 * parent. If a parent is not found then LEFT is used.</LI>
248 * <LI>{@link PositionConstants#LEFT} - Alignment is with leading edge</LI>
249 * <LI>{@link PositionConstants#RIGHT} - Alignment is with trailing edge</LI>
250 * <LI>{@link PositionConstants#CENTER}</LI>
251 * <LI>{@link PositionConstants#ALWAYS_LEFT} - Left, irrespective of orientation</LI>
252 * <LI>{@link PositionConstants#ALWAYS_RIGHT} - Right, irrespective of orientation</LI>
253 * </UL>
254 * @param value the aligment
255 * @see #getHorizontalAligment()
256 */
257 public void setHorizontalAligment(int value) {
258 value &= PositionConstants.LEFT | PositionConstants.CENTER | PositionConstants.RIGHT
259 | PositionConstants.ALWAYS_LEFT | PositionConstants.ALWAYS_RIGHT;
260 if (value is alignment)
261 return;
262 alignment = value;
263 revalidate();
264 }
265
266 /**
267 * Sets the orientation for this block. Orientation can be one of:
268 * <UL>
269 * <LI>{@link SWT#LEFT_TO_RIGHT}
270 * <LI>{@link SWT#RIGHT_TO_LEFT}
271 * <LI>{@link SWT#NONE} (default)
272 * </UL>
273 * <code>NONE</code> is used to indicate that orientation should be inherited from the
274 * encompassing block.
275 *
276 * @param orientation LTR, RTL or NONE
277 * @see #getOrientation()
278 * @since 3.1
279 */
280 public void setOrientation(int orientation) {
281 orientation &= SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT;
282 if (this.orientation is orientation)
283 return;
284 this.orientation = orientation;
285 revalidateBidi(this);
286 }
287
288 /**
289 * @see org.eclipse.draw2d.Figure#useLocalCoordinates()
290 */
291 protected bool useLocalCoordinates() {
292 return true;
293 }
294
295 /**
296 * Re-evaluate the Bidi state of all the fragments if it has been
297 * invalidated.
298 * @see org.eclipse.draw2d.IFigure#validate()
299 */
300 public void validate() {
301 if (!bidiValid) {
302 BidiProcessor.INSTANCE.setOrientation(getOrientation());
303 if (getOrientation() is SWT.LEFT_TO_RIGHT && isMirrored())
304 BidiProcessor.INSTANCE.addControlChar(BidiChars.LRE);
305 super.contributeBidi(BidiProcessor.INSTANCE);
306 BidiProcessor.INSTANCE.process();
307 bidiValid = true;
308 }
309 super.validate();
310 }
311
312 }