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