comparison dwtx/draw2d/text/FlowAdapter.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) 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.FlowAdapter;
14
15 import dwt.dwthelper.utils;
16 import dwtx.dwtxhelper.Collection;
17
18 import dwtx.draw2d.IFigure;
19 import dwtx.draw2d.geometry.Dimension;
20 import dwtx.draw2d.geometry.Rectangle;
21 import dwtx.draw2d.text.FlowFigure;
22 import dwtx.draw2d.text.BidiProcessor;
23 import dwtx.draw2d.text.FlowFigureLayout;
24 import dwtx.draw2d.text.FlowContext;
25 import dwtx.draw2d.text.ContentBox;
26 import dwtx.draw2d.text.BidiInfo;
27 import dwtx.draw2d.text.BidiChars;
28
29
30 /**
31 * Adapts non-flow figures for use within a parent hierarchy requiring flow figures.
32 * Normal draw2d figures can be added as children. If a normal LayoutManager is set, the
33 * children will be positioned by that layout manager. The size of this figure within
34 * the flow will be determined by its preferred size.
35 * <p>
36 * WARNING: This class is not intended to be subclassed by clients.
37 *
38 * @author Pratik Shah
39 * @since 3.1
40 */
41 public class FlowAdapter
42 : FlowFigure
43 {
44
45 private FlowContext context;
46 private FigureBox box;
47
48 public this(){
49 box = new FigureBox();
50 }
51 /**
52 * This FlowFigure contributes an Object Replacement Character.
53 * @see FlowFigure#contributeBidi(BidiProcessor)
54 */
55 protected void contributeBidi(BidiProcessor proc) {
56 box.setBidiLevel(-1);
57 // contributes a single object replacement char
58 proc.add(this, BidiChars.OBJ);
59 }
60
61 /**
62 * @return <code>null</code>
63 * @see dwtx.draw2d.text.FlowFigure#createDefaultFlowLayout()
64 */
65 protected FlowFigureLayout createDefaultFlowLayout() {
66 return null;
67 }
68
69 /**
70 * Sizes the content box to be big enough to display all figures. Wraps to the next line
71 * if there is not enough room on the current one.
72 * @see dwtx.draw2d.Figure#layout()
73 */
74 protected void layout() {
75 int wHint = context.getRemainingLineWidth();
76 if (wHint is Integer.MAX_VALUE)
77 wHint = -1;
78 Dimension prefSize = getPreferredSize(wHint, -1);
79 if (context.isCurrentLineOccupied()
80 && prefSize.width > context.getRemainingLineWidth()) {
81 context.endLine();
82 prefSize = getPreferredSize(context.getRemainingLineWidth(), -1);
83 }
84 box.setSize(prefSize);
85 context.addToCurrentLine(box);
86 }
87
88 /**
89 * Updates the bounds of this figure to match that of its content box, and lays out this
90 * figure's children.
91 * @see FlowFigure#postValidate()
92 */
93 public void postValidate() {
94 setBounds(new Rectangle(box.getX(), box.getBaseline() - box.ascent,
95 box.width, box.ascent));
96 super.layout();
97 for (Iterator itr = getChildren().iterator(); itr.hasNext();)
98 (cast(IFigure)itr.next()).validate();
99 }
100
101 /**
102 * Sets the bidi level of the content box associated with this Figure
103 * @see FlowFigure#setBidiInfo(BidiInfo)
104 */
105 public void setBidiInfo(BidiInfo info) {
106 box.setBidiLevel(info.levelInfo[0]);
107 }
108
109 /**
110 * @see dwtx.draw2d.IFigure#setBounds(dwtx.draw2d.geometry.Rectangle)
111 */
112 public void setBounds(Rectangle rect) {
113 int x = bounds.x,
114 y = bounds.y;
115
116 bool resize = (rect.width !is bounds.width) || (rect.height !is bounds.height),
117 translate = (rect.x !is x) || (rect.y !is y);
118
119 if ((resize || translate) && isVisible())
120 erase();
121 if (translate) {
122 int dx = rect.x - x;
123 int dy = rect.y - y;
124 primTranslate(dx, dy);
125 }
126
127 bounds.width = rect.width;
128 bounds.height = rect.height;
129
130 if (translate || resize) {
131 fireFigureMoved();
132 repaint();
133 }
134 }
135
136 /**
137 * @see FlowFigure#setFlowContext(FlowContext)
138 */
139 public void setFlowContext(FlowContext flowContext) {
140 context = flowContext;
141 }
142
143 /**
144 * Do not validate children.
145 * @see dwtx.draw2d.IFigure#validate()
146 */
147 public void validate() {
148 if (isValid())
149 return;
150 setValid(true);
151 layout();
152 }
153
154 private class FigureBox : ContentBox {
155 private int ascent;
156 public bool containsPoint(int x, int y) {
157 return this.outer.containsPoint(x, y);
158 }
159 public int getAscent() {
160 return ascent;
161 }
162 public int getDescent() {
163 return 0;
164 }
165 public void setSize(Dimension size) {
166 ascent = size.height;
167 width = size.width;
168 }
169 }
170
171 }