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