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