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