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.TextSegment;
|
|
14
|
|
15 import dwtx.ui.internal.forms.widgets.ParagraphSegment;
|
|
16 import dwtx.ui.internal.forms.widgets.Locator;
|
|
17 import dwtx.ui.internal.forms.widgets.SelectionData;
|
|
18
|
|
19 import dwt.DWT;
|
|
20 import dwt.graphics.Color;
|
|
21 import dwt.graphics.Font;
|
|
22 import dwt.graphics.FontMetrics;
|
|
23 import dwt.graphics.GC;
|
|
24 import dwt.graphics.Point;
|
|
25 import dwt.graphics.Rectangle;
|
|
26
|
|
27 import dwt.dwthelper.utils;
|
|
28 import tango.util.collection.ArraySeq;
|
|
29
|
|
30 /**
|
|
31 * @version 1.0
|
|
32 * @author
|
|
33 */
|
|
34 public class TextSegment : ParagraphSegment {
|
|
35
|
|
36 //DWT_TODO temp type
|
|
37 static class BreakIterator{
|
|
38
|
|
39 public static const int DONE = 0;
|
|
40
|
|
41 public static BreakIterator getLineInstance() {
|
|
42 // TODO Auto-generated method stub
|
|
43 return null;
|
|
44 }
|
|
45
|
|
46 public void setText(String text) {
|
|
47 // TODO Auto-generated method stub
|
|
48
|
|
49 }
|
|
50
|
|
51 public int first() {
|
|
52 // TODO Auto-generated method stub
|
|
53 return 0;
|
|
54 }
|
|
55
|
|
56 public int next() {
|
|
57 // TODO Auto-generated method stub
|
|
58 return 0;
|
|
59 }
|
|
60
|
|
61 }
|
|
62 private String colorId;
|
|
63
|
|
64 private String fontId;
|
|
65
|
|
66 private String text;
|
|
67
|
|
68 protected bool underline;
|
|
69
|
|
70 private bool wrapAllowed = true;
|
|
71
|
|
72 protected ArraySeq!(Object) areaRectangles;
|
|
73
|
|
74 private TextFragment[] textFragments;
|
|
75
|
|
76 class AreaRectangle {
|
|
77 Rectangle rect;
|
|
78
|
|
79 int from, to;
|
|
80
|
|
81 public this(Rectangle rect, int from, int to) {
|
|
82 this.rect = rect;
|
|
83 this.from = from;
|
|
84 this.to = to;
|
|
85 }
|
|
86
|
|
87 public bool contains(int x, int y) {
|
|
88 return rect.contains(x, y);
|
|
89 }
|
|
90
|
|
91 public bool intersects(Rectangle region) {
|
|
92 return rect.intersects(region);
|
|
93 }
|
|
94
|
|
95 public String getText() {
|
|
96 if (from is 0 && to is -1)
|
|
97 return this.outer.getText();
|
|
98 if (from > 0 && to is -1)
|
|
99 return this.outer.getText().substring(from);
|
|
100 return this.outer.getText().substring(from, to);
|
|
101 }
|
|
102 }
|
|
103
|
|
104 static class SelectionRange {
|
|
105 public int start;
|
|
106
|
|
107 public int stop;
|
|
108
|
|
109 public this() {
|
|
110 reset();
|
|
111 }
|
|
112
|
|
113 public void reset() {
|
|
114 start = -1;
|
|
115 stop = -1;
|
|
116 }
|
|
117 }
|
|
118
|
|
119 static class TextFragment {
|
|
120 short index;
|
|
121
|
|
122 short length;
|
|
123
|
|
124 public this(short index, short length) {
|
|
125 this.index = index;
|
|
126 this.length = length;
|
|
127 }
|
|
128 }
|
|
129
|
|
130 public this(String text, String fontId) {
|
|
131 this(text, fontId, null, true);
|
|
132 }
|
|
133
|
|
134 public this(String text, String fontId, String colorId) {
|
|
135 this(text, fontId, colorId, true);
|
|
136 }
|
|
137
|
|
138 public this(String text, String fontId, String colorId, bool wrapAllowed) {
|
|
139 areaRectangles = new ArraySeq!(Object);
|
|
140 this.text = cleanup(text);
|
|
141 this.fontId = fontId;
|
|
142 this.colorId = colorId;
|
|
143 this.wrapAllowed = wrapAllowed;
|
|
144 }
|
|
145
|
|
146 private String cleanup(String text) {
|
|
147 StringBuffer buf = new StringBuffer();
|
|
148 for (int i = 0; i < text.length(); i++) {
|
|
149 char c = text.charAt(i);
|
|
150 if (c is '\n' || c is '\r' || c is '\f') {
|
|
151 if (i > 0)
|
|
152 buf.append(' ');
|
|
153 } else
|
|
154 buf.append(c);
|
|
155 }
|
|
156 return buf.toString();
|
|
157 }
|
|
158
|
|
159 public void setWordWrapAllowed(bool value) {
|
|
160 wrapAllowed = value;
|
|
161 }
|
|
162
|
|
163 public bool isWordWrapAllowed() {
|
|
164 return wrapAllowed;
|
|
165 }
|
|
166
|
|
167 public bool isSelectable() {
|
|
168 return false;
|
|
169 }
|
|
170
|
|
171 public String getColorId() {
|
|
172 return colorId;
|
|
173 }
|
|
174
|
|
175 public String getText() {
|
|
176 return text;
|
|
177 }
|
|
178
|
|
179 void setText(String text) {
|
|
180 this.text = cleanup(text);
|
|
181 textFragments = null;
|
|
182 }
|
|
183
|
|
184 void setColorId(String colorId) {
|
|
185 this.colorId = colorId;
|
|
186 }
|
|
187
|
|
188 void setFontId(String fontId) {
|
|
189 this.fontId = fontId;
|
|
190 textFragments = null;
|
|
191 }
|
|
192
|
|
193 public bool contains(int x, int y) {
|
|
194 for (int i = 0; i < areaRectangles.size(); i++) {
|
|
195 AreaRectangle ar = cast(AreaRectangle) areaRectangles.get(i);
|
|
196 if (ar.contains(x, y))
|
|
197 return true;
|
|
198 if (i<areaRectangles.size()-1) {
|
|
199 // test the gap
|
|
200 Rectangle top = ar.rect;
|
|
201 Rectangle bot = (cast(AreaRectangle)areaRectangles.get(i+1)).rect;
|
|
202 if (y >= top.y+top.height && y < bot.y) {
|
|
203 // in the gap
|
|
204 int left = Math.max(top.x, bot.x);
|
|
205 int right = Math.min(top.x+top.width, bot.x+bot.width);
|
|
206 if (x>=left && x<=right) {
|
|
207 return true;
|
|
208 }
|
|
209 }
|
|
210 }
|
|
211 }
|
|
212 return false;
|
|
213 }
|
|
214
|
|
215 public bool intersects(Rectangle rect) {
|
|
216 for (int i = 0; i < areaRectangles.size(); i++) {
|
|
217 AreaRectangle ar = cast(AreaRectangle) areaRectangles.get(i);
|
|
218 if (ar.intersects(rect))
|
|
219 return true;
|
|
220 if (i<areaRectangles.size()-1) {
|
|
221 // test the gap
|
|
222 Rectangle top = ar.rect;
|
|
223 Rectangle bot = (cast(AreaRectangle)areaRectangles.get(i+1)).rect;
|
|
224 if (top.y+top.height < bot.y) {
|
|
225 int y = top.y+top.height;
|
|
226 int height = bot.y-y;
|
|
227 int left = Math.max(top.x, bot.x);
|
|
228 int right = Math.min(top.x+top.width, bot.x+bot.width);
|
|
229 Rectangle gap = new Rectangle(left, y, right-left, height);
|
|
230 if (gap.intersects(rect))
|
|
231 return true;
|
|
232 }
|
|
233 }
|
|
234 }
|
|
235 return false;
|
|
236 }
|
|
237
|
|
238 public Rectangle getBounds() {
|
|
239 int x = 0, y = 0;
|
|
240 int width = 0, height = 0;
|
|
241
|
|
242 for (int i = 0; i < areaRectangles.size(); i++) {
|
|
243 AreaRectangle ar = cast(AreaRectangle) areaRectangles.get(i);
|
|
244 if (i is 0) {
|
|
245 x = ar.rect.x;
|
|
246 y = ar.rect.y;
|
|
247 } else
|
|
248 x = Math.min(ar.rect.x, x);
|
|
249 width = Math.max(ar.rect.width, width);
|
|
250 height += ar.rect.height;
|
|
251 }
|
|
252 return new Rectangle(x, y, width, height);
|
|
253 }
|
|
254
|
|
255 public bool advanceLocator(GC gc, int wHint, Locator locator,
|
|
256 Hashtable objectTable, bool computeHeightOnly) {
|
|
257 Font oldFont = null;
|
|
258 if (fontId !is null) {
|
|
259 oldFont = gc.getFont();
|
|
260 Font newFont = cast(Font) objectTable.get(fontId);
|
|
261 if (newFont !is null)
|
|
262 gc.setFont(newFont);
|
|
263 }
|
|
264 FontMetrics fm = gc.getFontMetrics();
|
|
265 int lineHeight = fm.getHeight();
|
|
266 bool newLine = false;
|
|
267
|
|
268 if (wHint is DWT.DEFAULT || !wrapAllowed) {
|
|
269 Point extent = gc.textExtent(text);
|
|
270 int totalExtent = locator.x+extent.x;
|
|
271 if (isSelectable())
|
|
272 totalExtent+=1;
|
|
273
|
|
274 if (wHint !is DWT.DEFAULT && totalExtent > wHint) {
|
|
275 // new line
|
|
276 locator.x = locator.indent;
|
|
277 locator.y += locator.rowHeight;
|
|
278 if (computeHeightOnly)
|
|
279 locator.collectHeights();
|
|
280 locator.rowHeight = 0;
|
|
281 locator.leading = 0;
|
|
282 newLine = true;
|
|
283 }
|
|
284 int width = extent.x;
|
|
285 if (isSelectable())
|
|
286 width += 1;
|
|
287 locator.x += width;
|
|
288 locator.width = locator.indent + width;
|
|
289 locator.rowHeight = Math.max(locator.rowHeight, extent.y);
|
|
290 locator.leading = Math.max(locator.leading, fm.getLeading());
|
|
291 return newLine;
|
|
292 }
|
|
293
|
|
294 computeTextFragments(gc);
|
|
295
|
|
296 int width = 0;
|
|
297 Point lineExtent = new Point(0, 0);
|
|
298
|
|
299 for (int i = 0; i < textFragments.length; i++) {
|
|
300 TextFragment textFragment = textFragments[i];
|
|
301 int currentExtent = locator.x + lineExtent.x;
|
|
302
|
|
303 if (isSelectable())
|
|
304 currentExtent += 1;
|
|
305
|
|
306 if (i !is 0 && currentExtent + textFragment.length > wHint) {
|
|
307 // overflow
|
|
308 int lineWidth = currentExtent;
|
|
309 locator.rowHeight = Math.max(locator.rowHeight, lineExtent.y);
|
|
310 locator.leading = Math.max(locator.leading, fm.getLeading());
|
|
311 if (computeHeightOnly)
|
|
312 locator.collectHeights();
|
|
313 locator.x = locator.indent;
|
|
314 locator.y += locator.rowHeight;
|
|
315 locator.rowHeight = 0;
|
|
316 locator.leading = 0;
|
|
317 lineExtent.x = 0;
|
|
318 lineExtent.y = 0;
|
|
319 width = Math.max(width, lineWidth);
|
|
320 newLine = true;
|
|
321 }
|
|
322 lineExtent.x += textFragment.length;
|
|
323 lineExtent.y = Math.max(lineHeight, lineExtent.y);
|
|
324 }
|
|
325 int lineWidth = lineExtent.x;
|
|
326 if (isSelectable())
|
|
327 lineWidth += 1;
|
|
328 locator.x += lineWidth;
|
|
329 locator.width = width;
|
|
330 locator.rowHeight = Math.max(locator.rowHeight, lineExtent.y);
|
|
331 locator.leading = Math.max(locator.leading, fm.getLeading());
|
|
332 if (oldFont !is null) {
|
|
333 gc.setFont(oldFont);
|
|
334 }
|
|
335 return newLine;
|
|
336 }
|
|
337
|
|
338 /**
|
|
339 * @param gc
|
|
340 * @param width
|
|
341 * @param locator
|
|
342 * @param selected
|
|
343 * @param selData
|
|
344 * @param color
|
|
345 * @param fm
|
|
346 * @param lineHeight
|
|
347 * @param descent
|
|
348 */
|
|
349 private void layoutWithoutWrapping(GC gc, int width, Locator locator,
|
|
350 bool selected, FontMetrics fm, int lineHeight, int descent) {
|
|
351 Point extent = gc.textExtent(text);
|
|
352 int ewidth = extent.x;
|
|
353 if (isSelectable())
|
|
354 ewidth += 1;
|
|
355 if (locator.x + ewidth > width-locator.marginWidth) {
|
|
356 // new line
|
|
357 locator.resetCaret();
|
|
358 locator.y += locator.rowHeight;
|
|
359 locator.rowHeight = 0;
|
|
360 locator.rowCounter++;
|
|
361 }
|
|
362 int ly = locator.getBaseline(fm.getHeight() - fm.getLeading());
|
|
363 //int lineY = ly + lineHeight - descent + 1;
|
|
364 Rectangle br = new Rectangle(locator.x, ly, ewidth,
|
|
365 lineHeight - descent + 3);
|
|
366 areaRectangles.add(new AreaRectangle(br, 0, -1));
|
|
367 locator.x += ewidth;
|
|
368 locator.width = ewidth;
|
|
369 locator.rowHeight = Math.max(locator.rowHeight, extent.y);
|
|
370 }
|
|
371
|
|
372 protected int convertOffsetToStringIndex(GC gc, String s, int x,
|
|
373 int swidth, int selOffset) {
|
|
374 int index = s.length();
|
|
375 while (index > 0 && x + swidth > selOffset) {
|
|
376 index--;
|
|
377 String ss = s.substring(0, index);
|
|
378 swidth = gc.textExtent(ss).x;
|
|
379 }
|
|
380 return index;
|
|
381 }
|
|
382
|
|
383 public void paintFocus(GC gc, Color bg, Color fg, bool selected,
|
|
384 Rectangle repaintRegion) {
|
|
385 if (areaRectangles is null)
|
|
386 return;
|
|
387 for (int i = 0; i < areaRectangles.size(); i++) {
|
|
388 AreaRectangle areaRectangle = cast(AreaRectangle) areaRectangles.get(i);
|
|
389 Rectangle br = areaRectangle.rect;
|
|
390 int bx = br.x;
|
|
391 int by = br.y;
|
|
392 if (repaintRegion !is null) {
|
|
393 bx -= repaintRegion.x;
|
|
394 by -= repaintRegion.y;
|
|
395 }
|
|
396 if (selected) {
|
|
397 gc.setBackground(bg);
|
|
398 gc.setForeground(fg);
|
|
399 gc.drawFocus(bx, by, br.width, br.height);
|
|
400 } else {
|
|
401 gc.setForeground(bg);
|
|
402 gc.drawRectangle(bx, by, br.width - 1, br.height - 1);
|
|
403 }
|
|
404 }
|
|
405 }
|
|
406
|
|
407 public void paint(GC gc, bool hover, Hashtable resourceTable,
|
|
408 bool selected, SelectionData selData, Rectangle repaintRegion) {
|
|
409 this.paint(gc, hover, resourceTable, selected, false, selData,
|
|
410 repaintRegion);
|
|
411 }
|
|
412
|
|
413 protected void paint(GC gc, bool hover, Hashtable resourceTable,
|
|
414 bool selected, bool rollover, SelectionData selData,
|
|
415 Rectangle repaintRegion) {
|
|
416 Font oldFont = null;
|
|
417 Color oldColor = null;
|
|
418 Color oldBg = null;
|
|
419
|
|
420 // apply segment-specific font, color and background
|
|
421 if (fontId !is null) {
|
|
422 oldFont = gc.getFont();
|
|
423 Font newFont = cast(Font) resourceTable.get(fontId);
|
|
424 if (newFont !is null)
|
|
425 gc.setFont(newFont);
|
|
426 }
|
|
427 if (!hover && colorId !is null) {
|
|
428 oldColor = gc.getForeground();
|
|
429 Color newColor = cast(Color) resourceTable.get(colorId);
|
|
430 if (newColor !is null)
|
|
431 gc.setForeground(newColor);
|
|
432 }
|
|
433 oldBg = gc.getBackground();
|
|
434
|
|
435 FontMetrics fm = gc.getFontMetrics();
|
|
436 int lineHeight = fm.getHeight();
|
|
437 int descent = fm.getDescent();
|
|
438
|
|
439 // paint area rectangles of the segment
|
|
440 for (int i = 0; i < areaRectangles.size(); i++) {
|
|
441 AreaRectangle areaRectangle = cast(AreaRectangle) areaRectangles.get(i);
|
|
442 Rectangle rect = areaRectangle.rect;
|
|
443 String text = areaRectangle.getText();
|
|
444 Point extent = gc.textExtent(text);
|
|
445 int textX = rect.x + (isSelectable()?1:0);
|
|
446 int lineY = rect.y + lineHeight - descent + 1;
|
|
447 paintString(gc, text, extent.x, textX, rect.y, lineY, selData,
|
|
448 rect, hover, rollover, repaintRegion);
|
|
449 if (selected) {
|
|
450 int fx = rect.x;
|
|
451 int fy = rect.y;
|
|
452 if (repaintRegion !is null) {
|
|
453 fx -= repaintRegion.x;
|
|
454 fy -= repaintRegion.y;
|
|
455 }
|
|
456 //To avoid partially cancelling the focus by painting over
|
|
457 //X-ORed pixels, first cancel it yourself
|
|
458 Color fg = gc.getForeground();
|
|
459 gc.setForeground(oldBg);
|
|
460 gc.drawRectangle(fx, fy, rect.width - 1, rect.height - 1);
|
|
461 gc.setForeground(fg);
|
|
462 gc.drawFocus(fx, fy, rect.width, rect.height);
|
|
463 }
|
|
464 }
|
|
465 // restore GC resources
|
|
466 if (oldFont !is null) {
|
|
467 gc.setFont(oldFont);
|
|
468 }
|
|
469 if (oldColor !is null) {
|
|
470 gc.setForeground(oldColor);
|
|
471 }
|
|
472 if (oldBg !is null) {
|
|
473 gc.setBackground(oldBg);
|
|
474 }
|
|
475 }
|
|
476
|
|
477 public void computeSelection(GC gc, Hashtable resourceTable, SelectionData selData) {
|
|
478 Font oldFont = null;
|
|
479
|
|
480 if (fontId !is null) {
|
|
481 oldFont = gc.getFont();
|
|
482 Font newFont = cast(Font) resourceTable.get(fontId);
|
|
483 if (newFont !is null)
|
|
484 gc.setFont(newFont);
|
|
485 }
|
|
486
|
|
487 for (int i = 0; i < areaRectangles.size(); i++) {
|
|
488 AreaRectangle areaRectangle = cast(AreaRectangle) areaRectangles.get(i);
|
|
489 Rectangle rect = areaRectangle.rect;
|
|
490 String text = areaRectangle.getText();
|
|
491 Point extent = gc.textExtent(text);
|
|
492 computeSelection(gc, text, extent.x, selData,
|
|
493 rect);
|
|
494 }
|
|
495 // restore GC resources
|
|
496 if (oldFont !is null) {
|
|
497 gc.setFont(oldFont);
|
|
498 }
|
|
499 }
|
|
500
|
|
501 private void paintString(GC gc, String s, int swidth, int x, int y,
|
|
502 int lineY, SelectionData selData, Rectangle bounds, bool hover,
|
|
503 bool rolloverMode, Rectangle repaintRegion) {
|
|
504 // repaints one area rectangle
|
|
505 if (selData !is null && selData.isEnclosed()) {
|
|
506 Color savedBg = gc.getBackground();
|
|
507 Color savedFg = gc.getForeground();
|
|
508 int leftOffset = selData.getLeftOffset(bounds.height);
|
|
509 int rightOffset = selData.getRightOffset(bounds.height);
|
|
510 bool firstRow = selData.isFirstSelectionRow(bounds.y,
|
|
511 bounds.height);
|
|
512 bool lastRow = selData.isLastSelectionRow(bounds.y,
|
|
513 bounds.height);
|
|
514 bool selectedRow = selData
|
|
515 .isSelectedRow(bounds.y, bounds.height);
|
|
516
|
|
517 int sstart = -1;
|
|
518 int sstop = -1;
|
|
519
|
|
520 if ((firstRow && x + swidth < leftOffset)
|
|
521 || (lastRow && x > rightOffset)) {
|
|
522 paintStringSegment(gc, s, gc.textExtent(s).x, x, y, lineY,
|
|
523 hover, rolloverMode, repaintRegion);
|
|
524 return;
|
|
525 }
|
|
526
|
|
527 if (firstRow && bounds.x + swidth > leftOffset) {
|
|
528 sstart = convertOffsetToStringIndex(gc, s, bounds.x, swidth,
|
|
529 leftOffset);
|
|
530 }
|
|
531 if (lastRow && bounds.x + swidth > rightOffset) {
|
|
532 sstop = convertOffsetToStringIndex(gc, s, bounds.x, swidth,
|
|
533 rightOffset);
|
|
534 }
|
|
535
|
|
536 if (firstRow && sstart !is -1) {
|
|
537 String left = s.substring(0, sstart);
|
|
538 int width = gc.textExtent(left).x;
|
|
539 paintStringSegment(gc, left, width, x, y, lineY, hover,
|
|
540 rolloverMode, repaintRegion);
|
|
541 x += width;
|
|
542 }
|
|
543 if (selectedRow) {
|
|
544 int lindex = sstart !is -1 ? sstart : 0;
|
|
545 int rindex = sstop !is -1 ? sstop : s.length();
|
|
546 String mid = s.substring(lindex, rindex);
|
|
547 Point extent = gc.textExtent(mid);
|
|
548 gc.setForeground(selData.fg);
|
|
549 gc.setBackground(selData.bg);
|
|
550 gc.fillRectangle(x, y, extent.x, extent.y);
|
|
551 paintStringSegment(gc, mid, extent.x, x, y, lineY, hover,
|
|
552 rolloverMode, repaintRegion);
|
|
553 x += extent.x;
|
|
554 gc.setForeground(savedFg);
|
|
555 gc.setBackground(savedBg);
|
|
556 } else {
|
|
557 paintStringSegment(gc, s, gc.textExtent(s).x, x, y, lineY,
|
|
558 hover, rolloverMode, repaintRegion);
|
|
559 }
|
|
560 if (lastRow && sstop !is -1) {
|
|
561 String right = s.substring(sstop);
|
|
562 paintStringSegment(gc, right, gc.textExtent(right).x, x, y,
|
|
563 lineY, hover, rolloverMode, repaintRegion);
|
|
564 }
|
|
565 } else {
|
|
566 paintStringSegment(gc, s, gc.textExtent(s).x, x, y, lineY, hover,
|
|
567 rolloverMode, repaintRegion);
|
|
568 }
|
|
569 }
|
|
570
|
|
571 private void computeSelection(GC gc, String s, int swidth, SelectionData selData, Rectangle bounds) {
|
|
572 int leftOffset = selData.getLeftOffset(bounds.height);
|
|
573 int rightOffset = selData.getRightOffset(bounds.height);
|
|
574 bool firstRow = selData.isFirstSelectionRow(bounds.y, bounds.height);
|
|
575 bool lastRow = selData.isLastSelectionRow(bounds.y, bounds.height);
|
|
576 bool selectedRow = selData.isSelectedRow(bounds.y, bounds.height);
|
|
577
|
|
578 int sstart = -1;
|
|
579 int sstop = -1;
|
|
580
|
|
581 if (firstRow && bounds.x + swidth > leftOffset) {
|
|
582 sstart = convertOffsetToStringIndex(gc, s, bounds.x, swidth,
|
|
583 leftOffset);
|
|
584 }
|
|
585 if (lastRow && bounds.x + swidth > rightOffset) {
|
|
586 sstop = convertOffsetToStringIndex(gc, s, bounds.x, swidth,
|
|
587 rightOffset);
|
|
588 }
|
|
589
|
|
590 if (selectedRow) {
|
|
591 int lindex = sstart !is -1 ? sstart : 0;
|
|
592 int rindex = sstop !is -1 ? sstop : s.length();
|
|
593 String mid = s.substring(lindex, rindex);
|
|
594 selData.addSegment(mid);
|
|
595 }
|
|
596 }
|
|
597
|
|
598 /**
|
|
599 * @param gc
|
|
600 * @param s
|
|
601 * @param x
|
|
602 * @param y
|
|
603 * @param lineY
|
|
604 * @param hover
|
|
605 * @param rolloverMode
|
|
606 */
|
|
607 private void paintStringSegment(GC gc, String s, int swidth, int x, int y,
|
|
608 int lineY, bool hover, bool rolloverMode,
|
|
609 Rectangle repaintRegion) {
|
|
610 bool reverse = false;
|
|
611 int clipX = x;
|
|
612 int clipY = y;
|
|
613 int clipLineY = lineY;
|
|
614 if (repaintRegion !is null) {
|
|
615 clipX -= repaintRegion.x;
|
|
616 clipY -= repaintRegion.y;
|
|
617 clipLineY -= repaintRegion.y;
|
|
618 }
|
|
619 if (underline || hover || rolloverMode) {
|
|
620 if (rolloverMode && !hover)
|
|
621 reverse = true;
|
|
622 }
|
|
623 if (reverse) {
|
|
624 drawUnderline(gc, swidth, clipX, clipLineY, hover, rolloverMode);
|
|
625 gc.drawString(s, clipX, clipY, false);
|
|
626 } else {
|
|
627 gc.drawString(s, clipX, clipY, false);
|
|
628 drawUnderline(gc, swidth, clipX, clipLineY, hover, rolloverMode);
|
|
629 }
|
|
630 }
|
|
631
|
|
632 private void drawUnderline(GC gc, int swidth, int x, int y, bool hover,
|
|
633 bool rolloverMode) {
|
|
634 if (underline || hover || rolloverMode) {
|
|
635 Color saved = null;
|
|
636 if (rolloverMode && !hover) {
|
|
637 saved = gc.getForeground();
|
|
638 gc.setForeground(gc.getBackground());
|
|
639 }
|
|
640 gc.drawLine(x, y, x + swidth-1, y);
|
|
641 if (saved !is null)
|
|
642 gc.setForeground(saved);
|
|
643 }
|
|
644 }
|
|
645
|
|
646 /*
|
|
647 * (non-Javadoc)
|
|
648 *
|
|
649 * @see dwtx.ui.internal.forms.widgets.ParagraphSegment#layout(dwt.graphics.GC,
|
|
650 * int, dwtx.ui.internal.forms.widgets.Locator,
|
|
651 * java.util.Hashtable, bool,
|
|
652 * dwtx.ui.internal.forms.widgets.SelectionData)
|
|
653 */
|
|
654 public void layout(GC gc, int width, Locator locator,
|
|
655 Hashtable resourceTable, bool selected) {
|
|
656 Font oldFont = null;
|
|
657
|
|
658 areaRectangles.clear();
|
|
659
|
|
660 if (fontId !is null) {
|
|
661 oldFont = gc.getFont();
|
|
662 Font newFont = cast(Font) resourceTable.get(fontId);
|
|
663 if (newFont !is null)
|
|
664 gc.setFont(newFont);
|
|
665 }
|
|
666 FontMetrics fm = gc.getFontMetrics();
|
|
667 int lineHeight = fm.getHeight();
|
|
668 int descent = fm.getDescent();
|
|
669
|
|
670 if (!wrapAllowed) {
|
|
671 layoutWithoutWrapping(gc, width, locator, selected, fm, lineHeight,
|
|
672 descent);
|
|
673 } else {
|
|
674 int lineStart = 0;
|
|
675 int lastLoc = 0;
|
|
676 Point lineExtent = new Point(0, 0);
|
|
677 computeTextFragments(gc);
|
|
678 int rightEdge = width-locator.marginWidth;
|
|
679 for (int i = 0; i < textFragments.length; i++) {
|
|
680 TextFragment fragment = textFragments[i];
|
|
681 int breakLoc = fragment.index;
|
|
682 if (breakLoc is 0)
|
|
683 continue;
|
|
684 if (i !is 0 && locator.x + lineExtent.x + fragment.length > rightEdge) {
|
|
685 // overflow
|
|
686 int lineWidth = locator.x + lineExtent.x;
|
|
687 if (isSelectable())
|
|
688 lineWidth += 1;
|
|
689 int ly = locator.getBaseline(lineHeight - fm.getLeading());
|
|
690 Rectangle br = new Rectangle(isSelectable()?
|
|
691 locator.x - 1:locator.x, ly,
|
|
692 isSelectable()?lineExtent.x + 1:lineExtent.x, lineHeight - descent + 3);
|
|
693 areaRectangles
|
|
694 .add(new AreaRectangle(br, lineStart, lastLoc));
|
|
695
|
|
696 locator.rowHeight = Math.max(locator.rowHeight,
|
|
697 lineExtent.y);
|
|
698 locator.resetCaret();
|
|
699 if (isSelectable())
|
|
700 locator.x += 1;
|
|
701 locator.y += locator.rowHeight;
|
|
702 locator.rowCounter++;
|
|
703 locator.rowHeight = 0;
|
|
704 lineStart = lastLoc;
|
|
705 lineExtent.x = 0;
|
|
706 lineExtent.y = 0;
|
|
707 }
|
|
708 lastLoc = breakLoc;
|
|
709 lineExtent.x += fragment.length;
|
|
710 lineExtent.y = Math.max(lineHeight, lineExtent.y);
|
|
711 }
|
|
712 //String lastLine = text.substring(lineStart, lastLoc);
|
|
713 int ly = locator.getBaseline(lineHeight - fm.getLeading());
|
|
714 int lastWidth = lineExtent.x;
|
|
715 if (isSelectable())
|
|
716 lastWidth += 1;
|
|
717 Rectangle br = new Rectangle(isSelectable()?locator.x - 1:locator.x, ly,
|
|
718 isSelectable()?lineExtent.x + 1:lineExtent.x,
|
|
719 lineHeight - descent + 3);
|
|
720 //int lineY = ly + lineHeight - descent + 1;
|
|
721 areaRectangles.add(new AreaRectangle(br, lineStart, lastLoc));
|
|
722 locator.x += lastWidth;
|
|
723 locator.rowHeight = Math.max(locator.rowHeight, lineExtent.y);
|
|
724 }
|
|
725 if (oldFont !is null) {
|
|
726 gc.setFont(oldFont);
|
|
727 }
|
|
728 }
|
|
729
|
|
730 private void computeTextFragments(GC gc) {
|
|
731 if (textFragments !is null)
|
|
732 return;
|
|
733 ArrayList list = new ArrayList();
|
|
734 BreakIterator wb = BreakIterator.getLineInstance();
|
|
735 wb.setText(getText());
|
|
736 int cursor = 0;
|
|
737 for (int loc = wb.first(); loc !is BreakIterator.DONE; loc = wb.next()) {
|
|
738 if (loc is 0)
|
|
739 continue;
|
|
740 String word = text.substring(cursor, loc);
|
|
741 Point extent = gc.textExtent(word);
|
|
742 list.add(new TextFragment(cast(short) loc, cast(short) extent.x));
|
|
743 cursor = loc;
|
|
744 }
|
|
745 textFragments = cast(TextFragment[]) list.toArray(new TextFragment[list
|
|
746 .size()]);
|
|
747 }
|
|
748
|
|
749 public void clearCache(String fontId) {
|
|
750 if (fontIdisnull && (this.fontId is null||this.fontId.equals(FormTextModel.BOLD_FONT_ID)))
|
|
751 textFragments = null;
|
|
752 else if (fontId !is null && this.fontId !is null && fontId.equals(this.fontId))
|
|
753 textFragments = null;
|
|
754 }
|
|
755 }
|