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