1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text;
18
19/**
20 * This is the interface for text that has markup objects attached to
21 * ranges of it.  Not all text classes have mutable markup or text;
22 * see {@link Spannable} for mutable markup and {@link Editable} for
23 * mutable text.
24 */
25public interface Spanned
26extends CharSequence
27{
28    /**
29     * Bitmask of bits that are relevent for controlling point/mark behavior
30     * of spans.
31     *
32     * MARK and POINT are conceptually located <i>between</i> two adjacent characters.
33     * A MARK is "attached" to the character before, while a POINT will stick to the character
34     * after. The insertion cursor is conceptually located between the MARK and the POINT.
35     *
36     * As a result, inserting a new character between a MARK and a POINT will leave the MARK
37     * unchanged, while the POINT will be shifted, now located after the inserted character and
38     * still glued to the same character after it.
39     *
40     * Depending on whether the insertion happens at the beginning or the end of a span, the span
41     * will hence be expanded to <i>include</i> the new character (when the span is using a MARK at
42     * its beginning or a POINT at its end) or it will be <i>excluded</i>.
43     *
44     * Note that <i>before</i> and <i>after</i> here refer to offsets in the String, which are
45     * independent from the visual representation of the text (left-to-right or right-to-left).
46     */
47    public static final int SPAN_POINT_MARK_MASK = 0x33;
48
49    /**
50     * 0-length spans with type SPAN_MARK_MARK behave like text marks:
51     * they remain at their original offset when text is inserted
52     * at that offset. Conceptually, the text is added after the mark.
53     */
54    public static final int SPAN_MARK_MARK =   0x11;
55    /**
56     * SPAN_MARK_POINT is a synonym for {@link #SPAN_INCLUSIVE_INCLUSIVE}.
57     */
58    public static final int SPAN_MARK_POINT =  0x12;
59    /**
60     * SPAN_POINT_MARK is a synonym for {@link #SPAN_EXCLUSIVE_EXCLUSIVE}.
61     */
62    public static final int SPAN_POINT_MARK =  0x21;
63
64    /**
65     * 0-length spans with type SPAN_POINT_POINT behave like cursors:
66     * they are pushed forward by the length of the insertion when text
67     * is inserted at their offset.
68     * The text is conceptually inserted before the point.
69     */
70    public static final int SPAN_POINT_POINT = 0x22;
71
72    /**
73     * SPAN_PARAGRAPH behaves like SPAN_INCLUSIVE_EXCLUSIVE
74     * (SPAN_MARK_MARK), except that if either end of the span is
75     * at the end of the buffer, that end behaves like _POINT
76     * instead (so SPAN_INCLUSIVE_INCLUSIVE if it starts in the
77     * middle and ends at the end, or SPAN_EXCLUSIVE_INCLUSIVE
78     * if it both starts and ends at the end).
79     * <p>
80     * Its endpoints must be the start or end of the buffer or
81     * immediately after a \n character, and if the \n
82     * that anchors it is deleted, the endpoint is pulled to the
83     * next \n that follows in the buffer (or to the end of
84     * the buffer).
85     */
86    public static final int SPAN_PARAGRAPH =   0x33;
87
88    /**
89     * Non-0-length spans of type SPAN_INCLUSIVE_EXCLUSIVE expand
90     * to include text inserted at their starting point but not at their
91     * ending point.  When 0-length, they behave like marks.
92     */
93    public static final int SPAN_INCLUSIVE_EXCLUSIVE = SPAN_MARK_MARK;
94
95    /**
96     * Spans of type SPAN_INCLUSIVE_INCLUSIVE expand
97     * to include text inserted at either their starting or ending point.
98     */
99    public static final int SPAN_INCLUSIVE_INCLUSIVE = SPAN_MARK_POINT;
100
101    /**
102     * Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand
103     * to include text inserted at either their starting or ending point.
104     * They can never have a length of 0 and are automatically removed
105     * from the buffer if all the text they cover is removed.
106     */
107    public static final int SPAN_EXCLUSIVE_EXCLUSIVE = SPAN_POINT_MARK;
108
109    /**
110     * Non-0-length spans of type SPAN_EXCLUSIVE_INCLUSIVE expand
111     * to include text inserted at their ending point but not at their
112     * starting point.  When 0-length, they behave like points.
113     */
114    public static final int SPAN_EXCLUSIVE_INCLUSIVE = SPAN_POINT_POINT;
115
116    /**
117     * This flag is set on spans that are being used to apply temporary
118     * styling information on the composing text of an input method, so that
119     * they can be found and removed when the composing text is being
120     * replaced.
121     */
122    public static final int SPAN_COMPOSING = 0x100;
123
124    /**
125     * This flag will be set for intermediate span changes, meaning there
126     * is guaranteed to be another change following it.  Typically it is
127     * used for {@link Selection} which automatically uses this with the first
128     * offset it sets when updating the selection.
129     */
130    public static final int SPAN_INTERMEDIATE = 0x200;
131
132    /**
133     * The bits numbered SPAN_USER_SHIFT and above are available
134     * for callers to use to store scalar data associated with their
135     * span object.
136     */
137    public static final int SPAN_USER_SHIFT = 24;
138    /**
139     * The bits specified by the SPAN_USER bitfield are available
140     * for callers to use to store scalar data associated with their
141     * span object.
142     */
143    public static final int SPAN_USER = 0xFFFFFFFF << SPAN_USER_SHIFT;
144
145    /**
146     * The bits numbered just above SPAN_PRIORITY_SHIFT determine the order
147     * of change notifications -- higher numbers go first.  You probably
148     * don't need to set this; it is used so that when text changes, the
149     * text layout gets the chance to update itself before any other
150     * callbacks can inquire about the layout of the text.
151     */
152    public static final int SPAN_PRIORITY_SHIFT = 16;
153    /**
154     * The bits specified by the SPAN_PRIORITY bitmap determine the order
155     * of change notifications -- higher numbers go first.  You probably
156     * don't need to set this; it is used so that when text changes, the
157     * text layout gets the chance to update itself before any other
158     * callbacks can inquire about the layout of the text.
159     */
160    public static final int SPAN_PRIORITY = 0xFF << SPAN_PRIORITY_SHIFT;
161
162    /**
163     * Return an array of the markup objects attached to the specified
164     * slice of this CharSequence and whose type is the specified type
165     * or a subclass of it.  Specify Object.class for the type if you
166     * want all the objects regardless of type.
167     */
168    public <T> T[] getSpans(int start, int end, Class<T> type);
169
170    /**
171     * Return the beginning of the range of text to which the specified
172     * markup object is attached, or -1 if the object is not attached.
173     */
174    public int getSpanStart(Object tag);
175
176    /**
177     * Return the end of the range of text to which the specified
178     * markup object is attached, or -1 if the object is not attached.
179     */
180    public int getSpanEnd(Object tag);
181
182    /**
183     * Return the flags that were specified when {@link Spannable#setSpan} was
184     * used to attach the specified markup object, or 0 if the specified
185     * object has not been attached.
186     */
187    public int getSpanFlags(Object tag);
188
189    /**
190     * Return the first offset greater than or equal to <code>start</code>
191     * where a markup object of class <code>type</code> begins or ends,
192     * or <code>limit</code> if there are no starts or ends greater than or
193     * equal to <code>start</code> but less than <code>limit</code>.  Specify
194     * <code>null</code> or Object.class for the type if you want every
195     * transition regardless of type.
196     */
197    public int nextSpanTransition(int start, int limit, Class type);
198}
199