Mercurial > projects > dwt-addons
annotate dwtx/ui/internal/forms/widgets/Paragraph.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 1088ca33d3e0 |
children |
rev | line source |
---|---|
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; | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
19 import dwtx.ui.internal.forms.widgets.TextSegment; |
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
20 import dwtx.ui.internal.forms.widgets.TextHyperlinkSegment; |
75 | 21 |
22 import dwt.graphics.GC; | |
23 import dwt.graphics.Rectangle; | |
24 import dwtx.ui.forms.HyperlinkSettings; | |
25 | |
26 import dwt.dwthelper.utils; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
95
diff
changeset
|
27 import dwtx.dwtxhelper.Collection; |
95
1088ca33d3e0
Removed version=TANGOSVN for release 0.99.7
Frank Benoit <benoit@tionex.de>
parents:
94
diff
changeset
|
28 import tango.io.model.IFile; |
94 | 29 |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
30 import tango.text.Text; |
75 | 31 |
32 /** | |
33 * @version 1.0 | |
34 * @author | |
35 */ | |
36 public class Paragraph { | |
37 public static const String HTTP = "http://"; //$NON-NLS-1$ | |
38 | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
95
diff
changeset
|
39 private Vector segments; |
75 | 40 |
41 private bool addVerticalSpace = true; | |
42 | |
43 public this(bool addVerticalSpace) { | |
44 this.addVerticalSpace = addVerticalSpace; | |
45 } | |
46 | |
47 public int getIndent() { | |
48 return 0; | |
49 } | |
50 | |
51 public bool getAddVerticalSpace() { | |
52 return addVerticalSpace; | |
53 } | |
54 | |
55 /* | |
56 * @see IParagraph#getSegments() | |
57 */ | |
58 public ParagraphSegment[] getSegments() { | |
59 if (segments is null) | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
60 return null; |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
95
diff
changeset
|
61 return arraycast!(ParagraphSegment)(segments |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
95
diff
changeset
|
62 .toArray()); |
75 | 63 } |
64 | |
65 public void addSegment(ParagraphSegment segment) { | |
66 if (segments is null) | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
95
diff
changeset
|
67 segments = new Vector; |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
95
diff
changeset
|
68 segments.add(segment); |
75 | 69 } |
70 | |
71 public void parseRegularText(String text, bool expandURLs, bool wrapAllowed, | |
72 HyperlinkSettings settings, String fontId) { | |
73 parseRegularText(text, expandURLs, wrapAllowed, settings, fontId, null); | |
74 } | |
75 | |
76 public void parseRegularText(String text, bool expandURLs, bool wrapAllowed, | |
77 HyperlinkSettings settings, String fontId, String colorId) { | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
78 if (text.length is 0) |
75 | 79 return; |
80 if (expandURLs) { | |
81 int loc = text.indexOf(HTTP); | |
82 | |
83 if (loc is -1) | |
84 addSegment(new TextSegment(text, fontId, colorId, wrapAllowed)); | |
85 else { | |
86 int textLoc = 0; | |
87 while (loc !is -1) { | |
88 addSegment(new TextSegment(text.substring(textLoc, loc), | |
89 fontId, colorId, wrapAllowed)); | |
90 bool added = false; | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
91 for (textLoc = loc; textLoc < text.length; textLoc++) { |
75 | 92 char c = text.charAt(textLoc); |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
93 if (CharacterIsWhitespace(c)) { |
75 | 94 addHyperlinkSegment(text.substring(loc, textLoc), |
95 settings, fontId); | |
96 added = true; | |
97 break; | |
98 } | |
99 } | |
100 if (!added) { | |
101 // there was no space - just end of text | |
102 addHyperlinkSegment(text.substring(loc), settings, | |
103 fontId); | |
104 break; | |
105 } | |
106 loc = text.indexOf(HTTP, textLoc); | |
107 } | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
108 if (textLoc < text.length) { |
75 | 109 addSegment(new TextSegment(text.substring(textLoc), fontId, |
110 colorId, wrapAllowed)); | |
111 } | |
112 } | |
113 } else { | |
114 addSegment(new TextSegment(text, fontId, colorId, wrapAllowed)); | |
115 } | |
116 } | |
117 | |
118 private void addHyperlinkSegment(String text, HyperlinkSettings settings, | |
119 String fontId) { | |
120 TextHyperlinkSegment hs = new TextHyperlinkSegment(text, settings, | |
121 fontId); | |
122 hs.setWordWrapAllowed(false); | |
123 hs.setHref(text); | |
124 addSegment(hs); | |
125 } | |
126 | |
127 protected void computeRowHeights(GC gc, int width, Locator loc, | |
128 int lineHeight, Hashtable resourceTable) { | |
129 ParagraphSegment[] segments = getSegments(); | |
130 // compute heights | |
131 Locator hloc = loc.create(); | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
95
diff
changeset
|
132 ArrayList heights = new ArrayList(); |
75 | 133 hloc.heights = heights; |
134 hloc.rowCounter = 0; | |
135 int innerWidth = width - loc.marginWidth*2; | |
136 for (int j = 0; j < segments.length; j++) { | |
137 ParagraphSegment segment = segments[j]; | |
138 segment.advanceLocator(gc, innerWidth, hloc, resourceTable, true); | |
139 } | |
140 hloc.collectHeights(); | |
141 loc.heights = heights; | |
142 loc.rowCounter = 0; | |
143 } | |
144 | |
145 public void layout(GC gc, int width, Locator loc, int lineHeight, | |
146 Hashtable resourceTable, IHyperlinkSegment selectedLink) { | |
147 ParagraphSegment[] segments = getSegments(); | |
148 //int height; | |
149 if (segments.length > 0) { | |
150 /* | |
151 if (segments[0] instanceof TextSegment | |
152 && ((TextSegment) segments[0]).isSelectable()) | |
153 loc.x += 1; | |
154 */ | |
155 // compute heights | |
156 if (loc.heights is null) | |
157 computeRowHeights(gc, width, loc, lineHeight, resourceTable); | |
158 for (int j = 0; j < segments.length; j++) { | |
159 ParagraphSegment segment = segments[j]; | |
160 bool doSelect = false; | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
161 if (selectedLink !is null && segment.opEquals(cast(Object)selectedLink)) |
75 | 162 doSelect = true; |
163 segment.layout(gc, width, loc, resourceTable, doSelect); | |
164 } | |
165 loc.heights = null; | |
166 loc.y += loc.rowHeight; | |
167 } else { | |
168 loc.y += lineHeight; | |
169 } | |
170 } | |
171 | |
172 public void paint(GC gc, Rectangle repaintRegion, | |
173 Hashtable resourceTable, IHyperlinkSegment selectedLink, | |
174 SelectionData selData) { | |
175 ParagraphSegment[] segments = getSegments(); | |
176 | |
177 for (int i = 0; i < segments.length; i++) { | |
178 ParagraphSegment segment = segments[i]; | |
179 if (!segment.intersects(repaintRegion)) | |
180 continue; | |
181 bool doSelect = false; | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
182 if (selectedLink !is null && segment.opEquals(cast(Object)selectedLink)) |
75 | 183 doSelect = true; |
184 segment.paint(gc, false, resourceTable, doSelect, selData, repaintRegion); | |
185 } | |
186 } | |
187 | |
188 public void computeSelection(GC gc, Hashtable resourceTable, IHyperlinkSegment selectedLink, | |
189 SelectionData selData) { | |
190 ParagraphSegment[] segments = getSegments(); | |
191 | |
192 for (int i = 0; i < segments.length; i++) { | |
193 ParagraphSegment segment = segments[i]; | |
194 //bool doSelect = false; | |
195 //if (selectedLink !is null && segment.equals(selectedLink)) | |
196 //doSelect = true; | |
197 segment.computeSelection(gc, resourceTable, selData); | |
198 } | |
199 } | |
200 | |
201 public String getAccessibleText() { | |
202 ParagraphSegment[] segments = getSegments(); | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
203 auto txt = new tango.text.Text.Text!(char); |
75 | 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(); | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
208 txt.append(text); |
75 | 209 } |
210 } | |
77
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
211 txt.append( FileConst.NewlineString ); |
26c6c9dfd13c
ui.forms compile, just FormTextModel with xml reimpl left todo
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
212 return txt.toString(); |
75 | 213 } |
214 | |
215 public ParagraphSegment findSegmentAt(int x, int y) { | |
216 if (segments !is null) { | |
217 for (int i = 0; i < segments.size(); i++) { | |
218 ParagraphSegment segment = cast(ParagraphSegment) segments.get(i); | |
219 if (segment.contains(x, y)) | |
220 return segment; | |
221 } | |
222 } | |
223 return null; | |
224 } | |
225 public void clearCache(String fontId) { | |
226 if (segments !is null) { | |
227 for (int i = 0; i < segments.size(); i++) { | |
228 ParagraphSegment segment = cast(ParagraphSegment) segments.get(i); | |
229 segment.clearCache(fontId); | |
230 } | |
231 } | |
232 } | |
233 } |