Mercurial > projects > dwt2
comparison org.eclipse.draw2d/src/org/eclipse/draw2d/text/InlineFlow.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, 2008 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.InlineFlow; | |
14 | |
15 import java.lang.all; | |
16 | |
17 import org.eclipse.swt.SWT; | |
18 import org.eclipse.draw2d.Border; | |
19 import org.eclipse.draw2d.ColorConstants; | |
20 import org.eclipse.draw2d.Graphics; | |
21 import org.eclipse.draw2d.geometry.Rectangle; | |
22 import org.eclipse.draw2d.text.FlowFigure; | |
23 import org.eclipse.draw2d.text.FlowFigureLayout; | |
24 import org.eclipse.draw2d.text.FlowBox; | |
25 import org.eclipse.draw2d.text.InlineFlowLayout; | |
26 import org.eclipse.draw2d.text.FlowBorder; | |
27 | |
28 /** | |
29 * A <code>FlowFigure</code> represented by multiple <code>LineBox</code> fragments. An | |
30 * <code>InlineFlow</code>'s parent must be either a {@link BlockFlow} or another | |
31 * InlineFlow. | |
32 * | |
33 * <P>An InlineFlow may contain other InlineFlow figures. | |
34 * | |
35 * <P>WARNING: This class is not intended to be subclassed by clients. | |
36 * @author Randy Hudson | |
37 * @since 2.0 | |
38 */ | |
39 public class InlineFlow : FlowFigure { | |
40 | |
41 List fragments; | |
42 | |
43 public this(){ | |
44 fragments = new ArrayList(1); | |
45 } | |
46 | |
47 /** | |
48 * Iterates over the children to find the width before a line-break is encountered. | |
49 * @see org.eclipse.draw2d.text.FlowFigure#addLeadingWordRequirements(int[]) | |
50 */ | |
51 public bool addLeadingWordRequirements(int[] width) { | |
52 Iterator iter = getChildren().iterator(); | |
53 while (iter.hasNext()) { | |
54 if ((cast(FlowFigure)iter.next()).addLeadingWordRequirements(width)) | |
55 return true; | |
56 } | |
57 return false; | |
58 } | |
59 | |
60 /** | |
61 * Extended to return false if the point is not also contained by at least one fragment. | |
62 * @return <code>true</code> if a fragment contains the given point | |
63 * @param x the relative x coordinate | |
64 * @param y the relative y coordinate | |
65 */ | |
66 public bool containsPoint(int x, int y) { | |
67 if (super.containsPoint(x, y)) { | |
68 List frags = getFragments(); | |
69 for (int i = 0; i < frags.size(); i++) | |
70 if ((cast(FlowBox)frags.get(i)).containsPoint(x, y)) | |
71 return true; | |
72 } | |
73 | |
74 return false; | |
75 } | |
76 | |
77 /** | |
78 * @see FlowFigure#createDefaultFlowLayout() | |
79 */ | |
80 protected FlowFigureLayout createDefaultFlowLayout() { | |
81 return new InlineFlowLayout(this); | |
82 } | |
83 | |
84 /** | |
85 * Returns the <code>FlowBox</code> fragments contained in this InlineFlow. The returned | |
86 * list should not be modified. | |
87 * @return The fragments | |
88 */ | |
89 public List getFragments() { | |
90 return fragments; | |
91 } | |
92 | |
93 /** | |
94 * Overridden to paint a {@link FlowBorder} if present, and draw selection. The border is | |
95 * painted first, followed by selection which is generally done in XOR, which still allows | |
96 * the border to be seen. | |
97 * @param graphics the graphics | |
98 */ | |
99 protected void paintBorder(Graphics graphics) { | |
100 if (getBorder() !is null) { | |
101 FlowBorder fb = cast(FlowBorder)getBorder(); | |
102 List frags = getFragments(); | |
103 Rectangle where = new Rectangle(); | |
104 int sides; | |
105 for (int i = 0; i < frags.size(); i++) { | |
106 FlowBox box = cast(FlowBox)frags.get(i); | |
107 | |
108 where.x = box.getX(); | |
109 where.width = box.getWidth(); | |
110 where.y = -box.getAscentWithBorder(); | |
111 where.height = box.getDescentWithBorder() - where.y; | |
112 where.y += box.getBaseline(); | |
113 sides = 0; | |
114 if (i is 0) | |
115 sides = SWT.LEAD; | |
116 if (i is frags.size() - 1) | |
117 sides |= SWT.TRAIL; | |
118 fb.paint(this, graphics, where, sides); | |
119 } | |
120 graphics.restoreState(); | |
121 } | |
122 if (selectionStart !is -1) | |
123 paintSelection(graphics); | |
124 } | |
125 | |
126 /** | |
127 * Renders the XOR selection rectangles to the graphics. | |
128 * @param graphics the graphics to paint on | |
129 * @since 3.1 | |
130 */ | |
131 protected void paintSelection(Graphics graphics) { | |
132 graphics.restoreState(); | |
133 graphics.setXORMode(true); | |
134 graphics.setBackgroundColor(ColorConstants.white); | |
135 List list = getFragments(); | |
136 FlowBox box; | |
137 for (int i = 0; i < list.size(); i++) { | |
138 box = cast(FlowBox)list.get(i); | |
139 int top = box.getLineRoot().getVisibleTop(); | |
140 int bottom = box.getLineRoot().getVisibleBottom(); | |
141 graphics.fillRectangle(box.getX(), top, box.getWidth(), bottom - top); | |
142 } | |
143 } | |
144 | |
145 /** | |
146 * @see FlowFigure#postValidate() | |
147 */ | |
148 public void postValidate() { | |
149 List list = getFragments(); | |
150 FlowBox box; | |
151 int left = Integer.MAX_VALUE, top = left; | |
152 int right = Integer.MIN_VALUE, bottom = right; | |
153 | |
154 for (int i = 0; i < list.size(); i++) { | |
155 box = cast(FlowBox)list.get(i); | |
156 left = Math.min(left, box.getX()); | |
157 right = Math.max(right, box.getX() + box.getWidth()); | |
158 top = Math.min(top, box.getLineRoot().getVisibleTop()); | |
159 bottom = Math.max(bottom, box.getLineRoot().getVisibleBottom()); | |
160 } | |
161 | |
162 setBounds(new Rectangle(left, top, right - left, bottom - top)); | |
163 repaint(); | |
164 list = getChildren(); | |
165 for (int i = 0; i < list.size(); i++) | |
166 (cast(FlowFigure)list.get(i)).postValidate(); | |
167 } | |
168 | |
169 /** | |
170 * Overridden to assert that only {@link FlowBorder} is used. <code>null</code> is still a | |
171 * valid value as well. | |
172 * @param border <code>null</code> or a FlowBorder | |
173 */ | |
174 public void setBorder(Border border) { | |
175 if (border is null || null !is cast(FlowBorder)border ) | |
176 super.setBorder(border); | |
177 else | |
178 throw new RuntimeException("Border must be an instance of FlowBorder"); //$NON-NLS-1$ | |
179 } | |
180 | |
181 } |