comparison dwtx/draw2d/text/InlineFlowLayout.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.InlineFlowLayout;
14
15 import dwt.dwthelper.utils;
16 import dwtx.dwtxhelper.Collection;
17 import dwtx.draw2d.text.FlowContainerLayout;
18 import dwtx.draw2d.text.FlowFigure;
19 import dwtx.draw2d.text.LineBox;
20 import dwtx.draw2d.text.CompositeBox;
21 import dwtx.draw2d.text.NestedLine;
22 import dwtx.draw2d.text.InlineFlow;
23
24 /**
25 * The layout manager for {@link InlineFlow} figures.
26 *
27 * <P>WARNING: This class is not intended to be subclassed by clients.
28 * @author hudsonr
29 * @since 2.1
30 */
31 public class InlineFlowLayout
32 : FlowContainerLayout
33 {
34
35 /**
36 * Creates a new InlineFlowLayout with the given FlowFigure.
37 * @param flow The FlowFigure
38 */
39 public this(FlowFigure flow) {
40 super(flow);
41 }
42
43 /**
44 * Adds the given box as a line below the current line.
45 * @param box the box to add
46 */
47 public void addLine(CompositeBox box) {
48 endLine();
49 getContext().addLine(box);
50 }
51
52 /**
53 * @see FlowContainerLayout#createNewLine()
54 */
55 protected void createNewLine() {
56 currentLine = new NestedLine(cast(InlineFlow)getFlowFigure());
57 setupLine(currentLine);
58 }
59
60 /**
61 * @see FlowContext#endLine()
62 */
63 public void endLine() {
64 flush();
65 getContext().endLine();
66 }
67
68 /**
69 * @see FlowContainerLayout#flush()
70 */
71 protected void flush() {
72 if (currentLine !is null && currentLine.isOccupied()) {
73 // We want to preserve the state when a linebox is being added
74 bool sameLine = getContext().getContinueOnSameLine();
75 getContext().addToCurrentLine(currentLine);
76 (cast(InlineFlow)getFlowFigure()).getFragments().add(currentLine);
77 currentLine = null;
78 getContext().setContinueOnSameLine(sameLine);
79 }
80 }
81
82 /**
83 * InlineFlowLayout gets this information from its context.
84 * @see FlowContext#getContinueOnSameLine()
85 */
86 public bool getContinueOnSameLine() {
87 return getContext().getContinueOnSameLine();
88 }
89
90 /**
91 * @see FlowContext#getWidthLookahead(FlowFigure, int[])
92 */
93 public void getWidthLookahead(FlowFigure child, int result[]) {
94 List children = getFlowFigure().getChildren();
95 int index = -1;
96 if (child !is null)
97 index = children.indexOf(child);
98
99 for (int i = index + 1; i < children.size(); i++)
100 if ((cast(FlowFigure)children.get(i)).addLeadingWordRequirements(result))
101 return;
102
103 getContext().getWidthLookahead(getFlowFigure(), result);
104 }
105
106 /**
107 * @see FlowContainerLayout#isCurrentLineOccupied()
108 */
109 public bool isCurrentLineOccupied() {
110 return (currentLine !is null && !currentLine.getFragments().isEmpty())
111 || getContext().isCurrentLineOccupied();
112 }
113
114 /**
115 * Clears out all fragments prior to the call to layoutChildren().
116 */
117 public void preLayout() {
118 (cast(InlineFlow)getFlowFigure()).getFragments().clear();
119 }
120
121 /**
122 * InlineFlow passes this information to its context.
123 * @see FlowContext#setContinueOnSameLine(bool)
124 */
125 public void setContinueOnSameLine(bool value) {
126 getContext().setContinueOnSameLine(value);
127 }
128
129 /**
130 * Initializes the given LineBox. Called by createNewLine().
131 * @param line The LineBox to initialize.
132 */
133 protected void setupLine(LineBox line) {
134 line.setX(0);
135 line.setRecommendedWidth(getContext().getRemainingLineWidth());
136 }
137
138 }