Mercurial > projects > dwt-addons
comparison dwtx/draw2d/text/FlowFigure.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.text.FlowFigure; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 import dwtx.dwtxhelper.Collection; | |
17 | |
18 import dwtx.draw2d.Figure; | |
19 import dwtx.draw2d.IFigure; | |
20 import dwtx.draw2d.geometry.Rectangle; | |
21 import dwtx.draw2d.text.BidiProcessor; | |
22 import dwtx.draw2d.text.FlowFigureLayout; | |
23 import dwtx.draw2d.text.BidiInfo; | |
24 import dwtx.draw2d.text.FlowContext; | |
25 | |
26 /** | |
27 * The base implementation for text flow figures. A flow figure is used to render a | |
28 * document in which elements are laid out horizontally within a "line" until that line is | |
29 * filled. Layout continues on the next line. | |
30 * | |
31 * <p>WARNING: This class is not intended to be subclassed by clients. Future versions may | |
32 * contain additional abstract methods. | |
33 * | |
34 * @author hudsonr | |
35 * @since 2.1 | |
36 */ | |
37 public abstract class FlowFigure | |
38 : Figure | |
39 { | |
40 alias Figure.add add; | |
41 | |
42 /** | |
43 * integer indicating whether selection should be displayed. | |
44 */ | |
45 protected int selectionStart = -1; | |
46 | |
47 /** | |
48 * Constructs a new FlowFigure. | |
49 */ | |
50 public this() { | |
51 setLayoutManager(createDefaultFlowLayout()); | |
52 } | |
53 | |
54 /** | |
55 * If the child is a <code>FlowFigure</code>, its FlowContext is passed to it. | |
56 * @see dwtx.draw2d.IFigure#add(IFigure, Object, int) | |
57 */ | |
58 public void add(IFigure child, Object constraint, int index) { | |
59 super.add(child, constraint, index); | |
60 //If this layout manager is a FlowContext, then the child *must* be a FlowFigure | |
61 if (null !is cast(FlowContext)getLayoutManager() ) | |
62 (cast(FlowFigure)child).setFlowContext(cast(FlowContext)getLayoutManager()); | |
63 revalidateBidi(this); | |
64 } | |
65 | |
66 /** | |
67 * Calculates the width of text before the next line-break is encountered. | |
68 * <p> | |
69 * Default implementation treats each FlowFigure as a line-break. It adds no width and | |
70 * returns <code>true</code>. Sub-classes should override as needed. | |
71 * | |
72 * @param width the width before the next line-break (if one's found; all the width, | |
73 * otherwise) will be added on to the first int in the given array | |
74 * @return bool indicating whether or not a line-break was found | |
75 * @since 3.1 | |
76 */ | |
77 public bool addLeadingWordRequirements(int[] width) { | |
78 return true; | |
79 } | |
80 | |
81 /** | |
82 * FlowFigures can contribute text for their block to the given {@link BidiProcessor}, | |
83 * which will process the contributions to determine Bidi levels and shaping requirements. | |
84 * <p> | |
85 * This method is invoked as part of validating Bidi. | |
86 * <p> | |
87 * Sub-classes that cache the BidiInfo and/or the bidi level in ContentBoxes should clear | |
88 * the cached values when this method is invoked. | |
89 * | |
90 * @param proc the BidiProcessor to which contributions should be made | |
91 * @see BidiProcessor#add(FlowFigure, String) | |
92 * @since 3.1 | |
93 */ | |
94 protected void contributeBidi(BidiProcessor proc) { | |
95 for (Iterator iter = getChildren().iterator(); iter.hasNext();) | |
96 (cast(FlowFigure)iter.next()).contributeBidi(proc); | |
97 } | |
98 | |
99 /** | |
100 * Creates the default layout manager | |
101 * @return The default layout | |
102 */ | |
103 protected abstract FlowFigureLayout createDefaultFlowLayout(); | |
104 | |
105 /** | |
106 * Called after validate has occurred. This is used to update the bounds of the FlowFigure | |
107 * to encompass its new flow boxed created during validate. | |
108 */ | |
109 public abstract void postValidate(); | |
110 | |
111 /** | |
112 * Overridden to revalidateBidi when fragments are removed. | |
113 * @see dwtx.draw2d.IFigure#remove(dwtx.draw2d.IFigure) | |
114 */ | |
115 public void remove(IFigure figure) { | |
116 super.remove(figure); | |
117 revalidateBidi(this); | |
118 } | |
119 | |
120 /** | |
121 * This method should be invoked whenever a change that can potentially affect the | |
122 * Bidi evaluation is made (eg., adding or removing children, changing text, etc.). | |
123 * <p> | |
124 * The default implementation delegates the revalidation task to the parent. Only | |
125 * {@link BlockFlow#revalidateBidi(IFigure) blocks} perform the actual revalidation. | |
126 * <p> | |
127 * The given IFigure is the one that triggered the revalidation. This can be used to | |
128 * optimize bidi evaluation. | |
129 * | |
130 * @param origin the figure that was revalidated | |
131 * @since 3.1 | |
132 */ | |
133 protected void revalidateBidi(IFigure origin) { | |
134 if (getParent() !is null) | |
135 (cast(FlowFigure)getParent()).revalidateBidi(origin); | |
136 } | |
137 | |
138 /** | |
139 * Sets the bidi information for this figure. A flow figure contributes bidi text in | |
140 * {@link #contributeBidi(BidiProcessor)}. If the figure contributes text associated with | |
141 * it, this method is called back to indicate the bidi properties for that text within its | |
142 * block. | |
143 * | |
144 * @param info the BidiInfo for this figure | |
145 * @since 3.1 | |
146 */ | |
147 public void setBidiInfo(BidiInfo info) { } | |
148 | |
149 /** | |
150 * FlowFigures override setBounds() to prevent translation of children. "bounds" is a | |
151 * derived property for FlowFigures, calculated from the fragments that make up the | |
152 * FlowFigure. | |
153 * @see Figure#setBounds(Rectangle) | |
154 */ | |
155 public void setBounds(Rectangle r) { | |
156 if (bounds.opEquals(r)) | |
157 return; | |
158 if (!r.contains(bounds)) | |
159 erase(); | |
160 bounds.x = r.x; | |
161 bounds.y = r.y; | |
162 bounds.width = r.width; | |
163 bounds.height = r.height; | |
164 fireFigureMoved(); | |
165 if (isCoordinateSystem()) | |
166 fireCoordinateSystemChanged(); | |
167 repaint(); | |
168 } | |
169 | |
170 /** | |
171 * Sets the flow context. | |
172 * @param flowContext the flow context for this flow figure | |
173 */ | |
174 public void setFlowContext(FlowContext flowContext) { | |
175 (cast(FlowFigureLayout)getLayoutManager()).setFlowContext(flowContext); | |
176 } | |
177 | |
178 /** | |
179 * Sets the selection or a range of selection. A start value of -1 is used to indicate no | |
180 * selection. A start value >=0 indicates show selection. A start and end value can be | |
181 * used to represent a range of offsets which should render selection. | |
182 * @param start the start offset | |
183 * @param end the end offset | |
184 * @since 3.1 | |
185 */ | |
186 public void setSelection(int start, int end) { | |
187 if (selectionStart is start) | |
188 return; | |
189 selectionStart = start; | |
190 repaint(); | |
191 } | |
192 | |
193 } |