comparison dwt/custom/Bullet.d @ 0:380af2bdd8e5

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