75
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 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.ui.internal.forms.widgets.Paragraph;
|
|
14
|
|
15 import dwtx.ui.internal.forms.widgets.ParagraphSegment;
|
|
16 import dwtx.ui.internal.forms.widgets.Locator;
|
|
17 import dwtx.ui.internal.forms.widgets.IHyperlinkSegment;
|
|
18 import dwtx.ui.internal.forms.widgets.SelectionData;
|
|
19
|
|
20 import dwt.graphics.GC;
|
|
21 import dwt.graphics.Rectangle;
|
|
22 import dwtx.ui.forms.HyperlinkSettings;
|
|
23
|
|
24 import dwt.dwthelper.utils;
|
|
25 import tango.util.collection.ArraySeq;
|
|
26 import tango.util.collection.HashMap;
|
|
27
|
|
28 /**
|
|
29 * @version 1.0
|
|
30 * @author
|
|
31 */
|
|
32 public class Paragraph {
|
|
33 public static const String HTTP = "http://"; //$NON-NLS-1$
|
|
34
|
|
35 alias ArraySeq!(ParagraphSegment) TArraySeqParagraphSegment;
|
|
36 alias HashMap!(String,Object) Hashtable;
|
|
37
|
|
38 private TArraySeqParagraphSegment segments;
|
|
39
|
|
40 private bool addVerticalSpace = true;
|
|
41
|
|
42 public this(bool addVerticalSpace) {
|
|
43 this.addVerticalSpace = addVerticalSpace;
|
|
44 }
|
|
45
|
|
46 public int getIndent() {
|
|
47 return 0;
|
|
48 }
|
|
49
|
|
50 public bool getAddVerticalSpace() {
|
|
51 return addVerticalSpace;
|
|
52 }
|
|
53
|
|
54 /*
|
|
55 * @see IParagraph#getSegments()
|
|
56 */
|
|
57 public ParagraphSegment[] getSegments() {
|
|
58 if (segments is null)
|
|
59 return new TArraySeqParagraphSegment;
|
|
60 return segments
|
|
61 .toArray();
|
|
62 }
|
|
63
|
|
64 public void addSegment(ParagraphSegment segment) {
|
|
65 if (segments is null)
|
|
66 segments = new TArraySeqParagraphSegment;
|
|
67 segments.append(segment);
|
|
68 }
|
|
69
|
|
70 public void parseRegularText(String text, bool expandURLs, bool wrapAllowed,
|
|
71 HyperlinkSettings settings, String fontId) {
|
|
72 parseRegularText(text, expandURLs, wrapAllowed, settings, fontId, null);
|
|
73 }
|
|
74
|
|
75 public void parseRegularText(String text, bool expandURLs, bool wrapAllowed,
|
|
76 HyperlinkSettings settings, String fontId, String colorId) {
|
|
77 if (text.length() is 0)
|
|
78 return;
|
|
79 if (expandURLs) {
|
|
80 int loc = text.indexOf(HTTP);
|
|
81
|
|
82 if (loc is -1)
|
|
83 addSegment(new TextSegment(text, fontId, colorId, wrapAllowed));
|
|
84 else {
|
|
85 int textLoc = 0;
|
|
86 while (loc !is -1) {
|
|
87 addSegment(new TextSegment(text.substring(textLoc, loc),
|
|
88 fontId, colorId, wrapAllowed));
|
|
89 bool added = false;
|
|
90 for (textLoc = loc; textLoc < text.length(); textLoc++) {
|
|
91 char c = text.charAt(textLoc);
|
|
92 if (Character.isSpaceChar(c)) {
|
|
93 addHyperlinkSegment(text.substring(loc, textLoc),
|
|
94 settings, fontId);
|
|
95 added = true;
|
|
96 break;
|
|
97 }
|
|
98 }
|
|
99 if (!added) {
|
|
100 // there was no space - just end of text
|
|
101 addHyperlinkSegment(text.substring(loc), settings,
|
|
102 fontId);
|
|
103 break;
|
|
104 }
|
|
105 loc = text.indexOf(HTTP, textLoc);
|
|
106 }
|
|
107 if (textLoc < text.length()) {
|
|
108 addSegment(new TextSegment(text.substring(textLoc), fontId,
|
|
109 colorId, wrapAllowed));
|
|
110 }
|
|
111 }
|
|
112 } else {
|
|
113 addSegment(new TextSegment(text, fontId, colorId, wrapAllowed));
|
|
114 }
|
|
115 }
|
|
116
|
|
117 private void addHyperlinkSegment(String text, HyperlinkSettings settings,
|
|
118 String fontId) {
|
|
119 TextHyperlinkSegment hs = new TextHyperlinkSegment(text, settings,
|
|
120 fontId);
|
|
121 hs.setWordWrapAllowed(false);
|
|
122 hs.setHref(text);
|
|
123 addSegment(hs);
|
|
124 }
|
|
125
|
|
126 protected void computeRowHeights(GC gc, int width, Locator loc,
|
|
127 int lineHeight, Hashtable resourceTable) {
|
|
128 ParagraphSegment[] segments = getSegments();
|
|
129 // compute heights
|
|
130 Locator hloc = loc.create();
|
|
131 ArrayList heights = new ArrayList();
|
|
132 hloc.heights = heights;
|
|
133 hloc.rowCounter = 0;
|
|
134 int innerWidth = width - loc.marginWidth*2;
|
|
135 for (int j = 0; j < segments.length; j++) {
|
|
136 ParagraphSegment segment = segments[j];
|
|
137 segment.advanceLocator(gc, innerWidth, hloc, resourceTable, true);
|
|
138 }
|
|
139 hloc.collectHeights();
|
|
140 loc.heights = heights;
|
|
141 loc.rowCounter = 0;
|
|
142 }
|
|
143
|
|
144 public void layout(GC gc, int width, Locator loc, int lineHeight,
|
|
145 Hashtable resourceTable, IHyperlinkSegment selectedLink) {
|
|
146 ParagraphSegment[] segments = getSegments();
|
|
147 //int height;
|
|
148 if (segments.length > 0) {
|
|
149 /*
|
|
150 if (segments[0] instanceof TextSegment
|
|
151 && ((TextSegment) segments[0]).isSelectable())
|
|
152 loc.x += 1;
|
|
153 */
|
|
154 // compute heights
|
|
155 if (loc.heights is null)
|
|
156 computeRowHeights(gc, width, loc, lineHeight, resourceTable);
|
|
157 for (int j = 0; j < segments.length; j++) {
|
|
158 ParagraphSegment segment = segments[j];
|
|
159 bool doSelect = false;
|
|
160 if (selectedLink !is null && segment.equals(selectedLink))
|
|
161 doSelect = true;
|
|
162 segment.layout(gc, width, loc, resourceTable, doSelect);
|
|
163 }
|
|
164 loc.heights = null;
|
|
165 loc.y += loc.rowHeight;
|
|
166 } else {
|
|
167 loc.y += lineHeight;
|
|
168 }
|
|
169 }
|
|
170
|
|
171 public void paint(GC gc, Rectangle repaintRegion,
|
|
172 Hashtable resourceTable, IHyperlinkSegment selectedLink,
|
|
173 SelectionData selData) {
|
|
174 ParagraphSegment[] segments = getSegments();
|
|
175
|
|
176 for (int i = 0; i < segments.length; i++) {
|
|
177 ParagraphSegment segment = segments[i];
|
|
178 if (!segment.intersects(repaintRegion))
|
|
179 continue;
|
|
180 bool doSelect = false;
|
|
181 if (selectedLink !is null && segment.equals(selectedLink))
|
|
182 doSelect = true;
|
|
183 segment.paint(gc, false, resourceTable, doSelect, selData, repaintRegion);
|
|
184 }
|
|
185 }
|
|
186
|
|
187 public void computeSelection(GC gc, Hashtable resourceTable, IHyperlinkSegment selectedLink,
|
|
188 SelectionData selData) {
|
|
189 ParagraphSegment[] segments = getSegments();
|
|
190
|
|
191 for (int i = 0; i < segments.length; i++) {
|
|
192 ParagraphSegment segment = segments[i];
|
|
193 //bool doSelect = false;
|
|
194 //if (selectedLink !is null && segment.equals(selectedLink))
|
|
195 //doSelect = true;
|
|
196 segment.computeSelection(gc, resourceTable, selData);
|
|
197 }
|
|
198 }
|
|
199
|
|
200 public String getAccessibleText() {
|
|
201 ParagraphSegment[] segments = getSegments();
|
|
202 StringWriter swriter = new StringWriter();
|
|
203 PrintWriter writer = new PrintWriter(swriter);
|
|
204 for (int i = 0; i < segments.length; i++) {
|
|
205 ParagraphSegment segment = segments[i];
|
|
206 if ( auto ts = cast(TextSegment)segment ) {
|
|
207 String text = ts.getText();
|
|
208 writer.print(text);
|
|
209 }
|
|
210 }
|
|
211 writer.println();
|
|
212 swriter.flush();
|
|
213 return swriter.toString();
|
|
214 }
|
|
215
|
|
216 public ParagraphSegment findSegmentAt(int x, int y) {
|
|
217 if (segments !is null) {
|
|
218 for (int i = 0; i < segments.size(); i++) {
|
|
219 ParagraphSegment segment = cast(ParagraphSegment) segments.get(i);
|
|
220 if (segment.contains(x, y))
|
|
221 return segment;
|
|
222 }
|
|
223 }
|
|
224 return null;
|
|
225 }
|
|
226 public void clearCache(String fontId) {
|
|
227 if (segments !is null) {
|
|
228 for (int i = 0; i < segments.size(); i++) {
|
|
229 ParagraphSegment segment = cast(ParagraphSegment) segments.get(i);
|
|
230 segment.clearCache(fontId);
|
|
231 }
|
|
232 }
|
|
233 }
|
|
234 }
|