155
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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 dwt.custom.Bullet;
|
|
14
|
|
15 import dwt.DWT;
|
|
16 import dwt.custom.StyleRange;
|
|
17 import dwt.custom.ST;
|
|
18 import dwt.dwthelper.System;
|
|
19
|
|
20 /**
|
|
21 * Instances of this class represent bullets in the <code>StyledText</code>.
|
|
22 * <p>
|
|
23 * The hashCode() method in this class uses the values of the public
|
|
24 * fields to compute the hash value. When storing instances of the
|
|
25 * class in hashed collections, do not modify these fields after the
|
|
26 * object has been inserted.
|
|
27 * </p>
|
|
28 * <p>
|
|
29 * Application code does <em>not</em> need to explicitly release the
|
|
30 * resources managed by each instance when those instances are no longer
|
|
31 * required, and thus no <code>dispose()</code> method is provided.
|
|
32 * </p>
|
|
33 *
|
|
34 * @see StyledText#setLineBullet(int, int, Bullet)
|
|
35 *
|
|
36 * @since 3.2
|
|
37 */
|
|
38 public class Bullet {
|
|
39 public int type;
|
|
40 public StyleRange style;
|
|
41 public char[] text;
|
|
42 int[] linesIndices;
|
|
43 int count;
|
|
44
|
|
45 /**
|
|
46 * Create a new bullet the specified style, the type is set to ST.BULLET_DOT.
|
|
47 * The style must have a glyph metrics set.
|
|
48 *
|
|
49 * @param style the style
|
|
50 *
|
|
51 * @exception IllegalArgumentException <ul>
|
|
52 * <li>ERROR_NULL_ARGUMENT when the style or the glyph metrics are null</li>
|
|
53 * </ul>
|
|
54 */
|
|
55 public this(StyleRange style) {
|
|
56 this(ST.BULLET_DOT, style);
|
|
57 }
|
|
58 /**
|
|
59 * Create a new bullet the specified style and type.
|
|
60 * The style must have a glyph metrics set.
|
|
61 *
|
|
62 * @param style the style
|
|
63 *
|
|
64 * @exception IllegalArgumentException <ul>
|
|
65 * <li>ERROR_NULL_ARGUMENT when the style or the glyph metrics are null</li>
|
|
66 * </ul>
|
|
67 */
|
|
68 public this(int type, StyleRange style) {
|
|
69 if (style is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
|
|
70 if (style.metrics is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
|
|
71 this.type = type;
|
|
72 this.style = style;
|
|
73 }
|
|
74 void addIndices (int startLine, int lineCount) {
|
|
75 if (linesIndices is null) {
|
|
76 linesIndices = new int[lineCount];
|
|
77 count = lineCount;
|
|
78 for (int i = 0; i < lineCount; i++) linesIndices[i] = startLine + i;
|
|
79 } else {
|
|
80 int modifyStart = 0;
|
|
81 while (modifyStart < count) {
|
|
82 if (startLine <= linesIndices[modifyStart]) break;
|
|
83 modifyStart++;
|
|
84 }
|
|
85 int modifyEnd = modifyStart;
|
|
86 while (modifyEnd < count) {
|
|
87 if (startLine + lineCount <= linesIndices[modifyEnd]) break;
|
|
88 modifyEnd++;
|
|
89 }
|
|
90 int newSize = modifyStart + lineCount + count - modifyEnd;
|
|
91 if (newSize > linesIndices.length) {
|
|
92 int[] newLinesIndices = new int[newSize];
|
|
93 System.arraycopy(linesIndices, 0, newLinesIndices, 0, count);
|
|
94 linesIndices = newLinesIndices;
|
|
95 }
|
|
96 System.arraycopy(linesIndices, modifyEnd, linesIndices, modifyStart + lineCount, count - modifyEnd);
|
|
97 for (int i = 0; i < lineCount; i++) linesIndices[modifyStart + i] = startLine + i;
|
|
98 count = newSize;
|
|
99 }
|
|
100 }
|
|
101 int indexOf (int lineIndex) {
|
|
102 for (int i = 0; i < count; i++) {
|
|
103 if (linesIndices[i] is lineIndex) return i;
|
|
104 }
|
|
105 return -1;
|
|
106 }
|
|
107 public override hash_t toHash() {
|
|
108 return style.toHash() ^ type;
|
|
109 }
|
|
110 int[] removeIndices (int startLine, int replaceLineCount, int newLineCount, bool update) {
|
|
111 if (count is 0) return null;
|
|
112 if (startLine > linesIndices[count - 1]) return null;
|
|
113 int endLine = startLine + replaceLineCount;
|
|
114 int delta = newLineCount - replaceLineCount;
|
|
115 for (int i = 0; i < count; i++) {
|
|
116 int index = linesIndices[i];
|
|
117 if (startLine <= index) {
|
|
118 int j = i;
|
|
119 while (j < count) {
|
|
120 if (linesIndices[j] >= endLine) break;
|
|
121 j++;
|
|
122 }
|
|
123 if (update) {
|
|
124 for (int k = j; k < count; k++) linesIndices[k] += delta;
|
|
125 }
|
|
126 int[] redrawLines = new int[count - j];
|
|
127 System.arraycopy(linesIndices, j, redrawLines, 0, count - j);
|
|
128 System.arraycopy(linesIndices, j, linesIndices, i, count - j);
|
|
129 count -= (j - i);
|
|
130 return redrawLines;
|
|
131 }
|
|
132 }
|
|
133 for (int i = 0; i < count; i++) linesIndices[i] += delta;
|
|
134 return null;
|
|
135 }
|
|
136 int size() {
|
|
137 return count;
|
|
138 }
|
|
139 }
|