1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.text;
/**
* Utility class for manipulating cursors and selections in CharSequences.
* A cursor is a selection where the start and end are at the same offset.
*/
public class Selection {
private Selection() { /* cannot be instantiated */ }
/*
* Retrieving the selection
*/
/**
* Return the offset of the selection anchor or cursor, or -1 if
* there is no selection or cursor.
*/
public static final int getSelectionStart(CharSequence text) {
if (text instanceof Spanned)
return ((Spanned) text).getSpanStart(SELECTION_START);
else
return -1;
}
/**
* Return the offset of the selection edge or cursor, or -1 if
* there is no selection or cursor.
*/
public static final int getSelectionEnd(CharSequence text) {
if (text instanceof Spanned)
return ((Spanned) text).getSpanStart(SELECTION_END);
else
return -1;
}
/*
* Setting the selection
*/
// private static int pin(int value, int min, int max) {
// return value < min ? 0 : (value > max ? max : value);
// }
/**
* Set the selection anchor to <code>start</code> and the selection edge
* to <code>stop</code>.
*/
public static void setSelection(Spannable text, int start, int stop) {
// int len = text.length();
// start = pin(start, 0, len); XXX remove unless we really need it
// stop = pin(stop, 0, len);
int ostart = getSelectionStart(text);
int oend = getSelectionEnd(text);
if (ostart != start || oend != stop) {
text.setSpan(SELECTION_START, start, start,
Spanned.SPAN_POINT_POINT|Spanned.SPAN_INTERMEDIATE);
text.setSpan(SELECTION_END, stop, stop,
Spanned.SPAN_POINT_POINT);
}
}
/**
* Move the cursor to offset <code>index</code>.
*/
public static final void setSelection(Spannable text, int index) {
setSelection(text, index, index);
}
/**
* Select the entire text.
*/
public static final void selectAll(Spannable text) {
setSelection(text, 0, text.length());
}
/**
* Move the selection edge to offset <code>index</code>.
*/
public static final void extendSelection(Spannable text, int index) {
if (text.getSpanStart(SELECTION_END) != index)
text.setSpan(SELECTION_END, index, index, Spanned.SPAN_POINT_POINT);
}
/**
* Remove the selection or cursor, if any, from the text.
*/
public static final void removeSelection(Spannable text) {
text.removeSpan(SELECTION_START);
text.removeSpan(SELECTION_END);
}
/*
* Moving the selection within the layout
*/
/**
* Move the cursor to the buffer offset physically above the current
* offset, or return false if the cursor is already on the top line.
*/
public static boolean moveUp(Spannable text, Layout layout) {
int start = getSelectionStart(text);
int end = getSelectionEnd(text);
if (start != end) {
int min = Math.min(start, end);
int max = Math.max(start, end);
setSelection(text, min);
if (min == 0 && max == text.length()) {
return false;
}
return true;
} else {
int line = layout.getLineForOffset(end);
if (line > 0) {
int move;
if (layout.getParagraphDirection(line) ==
layout.getParagraphDirection(line - 1)) {
float h = layout.getPrimaryHorizontal(end);
move = layout.getOffsetForHorizontal(line - 1, h);
} else {
move = layout.getLineStart(line - 1);
}
setSelection(text, move);
return true;
}
}
return false;
}
/**
* Move the cursor to the buffer offset physically below the current
* offset, or return false if the cursor is already on the bottom line.
*/
public static boolean moveDown(Spannable text, Layout layout) {
int start = getSelectionStart(text);
int end = getSelectionEnd(text);
if (start != end) {
int min = Math.min(start, end);
int max = Math.max(start, end);
setSelection(text, max);
if (min == 0 && max == text.length()) {
return false;
}
return true;
} else {
int line = layout.getLineForOffset(end);
if (line < layout.getLineCount() - 1) {
int move;
if (layout.getParagraphDirection(line) ==
layout.getParagraphDirection(line + 1)) {
float h = layout.getPrimaryHorizontal(end);
move = layout.getOffsetForHorizontal(line + 1, h);
} else {
move = layout.getLineStart(line + 1);
}
setSelection(text, move);
return true;
}
}
return false;
}
/**
* Move the cursor to the buffer offset physically to the left of
* the current offset, or return false if the cursor is already
* at the left edge of the line and there is not another line to move it to.
*/
public static boolean moveLeft(Spannable text, Layout layout) {
int start = getSelectionStart(text);
int end = getSelectionEnd(text);
if (start != end) {
setSelection(text, chooseHorizontal(layout, -1, start, end));
return true;
} else {
int to = layout.getOffsetToLeftOf(end);
if (to != end) {
setSelection(text, to);
return true;
}
}
return false;
}
/**
* Move the cursor to the buffer offset physically to the right of
* the current offset, or return false if the cursor is already at
* at the right edge of the line and there is not another line
* to move it to.
*/
public static boolean moveRight(Spannable text, Layout layout) {
int start = getSelectionStart(text);
int end = getSelectionEnd(text);
if (start != end) {
setSelection(text, chooseHorizontal(layout, 1, start, end));
return true;
} else {
int to = layout.getOffsetToRightOf(end);
if (to != end) {
setSelection(text, to);
return true;
}
}
return false;
}
/**
* Move the selection end to the buffer offset physically above
* the current selection end.
*/
public static boolean extendUp(Spannable text, Layout layout) {
int end = getSelectionEnd(text);
int line = layout.getLineForOffset(end);
if (line > 0) {
int move;
if (layout.getParagraphDirection(line) ==
layout.getParagraphDirection(line - 1)) {
float h = layout.getPrimaryHorizontal(end);
move = layout.getOffsetForHorizontal(line - 1, h);
} else {
move = layout.getLineStart(line - 1);
}
extendSelection(text, move);
return true;
} else if (end != 0) {
extendSelection(text, 0);
return true;
}
return true;
}
/**
* Move the selection end to the buffer offset physically below
* the current selection end.
*/
public static boolean extendDown(Spannable text, Layout layout) {
int end = getSelectionEnd(text);
int line = layout.getLineForOffset(end);
if (line < layout.getLineCount() - 1) {
int move;
if (layout.getParagraphDirection(line) ==
layout.getParagraphDirection(line + 1)) {
float h = layout.getPrimaryHorizontal(end);
move = layout.getOffsetForHorizontal(line + 1, h);
} else {
move = layout.getLineStart(line + 1);
}
extendSelection(text, move);
return true;
} else if (end != text.length()) {
extendSelection(text, text.length());
return true;
}
return true;
}
/**
* Move the selection end to the buffer offset physically to the left of
* the current selection end.
*/
public static boolean extendLeft(Spannable text, Layout layout) {
int end = getSelectionEnd(text);
int to = layout.getOffsetToLeftOf(end);
if (to != end) {
extendSelection(text, to);
return true;
}
return true;
}
/**
* Move the selection end to the buffer offset physically to the right of
* the current selection end.
*/
public static boolean extendRight(Spannable text, Layout layout) {
int end = getSelectionEnd(text);
int to = layout.getOffsetToRightOf(end);
if (to != end) {
extendSelection(text, to);
return true;
}
return true;
}
public static boolean extendToLeftEdge(Spannable text, Layout layout) {
int where = findEdge(text, layout, -1);
extendSelection(text, where);
return true;
}
public static boolean extendToRightEdge(Spannable text, Layout layout) {
int where = findEdge(text, layout, 1);
extendSelection(text, where);
return true;
}
public static boolean moveToLeftEdge(Spannable text, Layout layout) {
int where = findEdge(text, layout, -1);
setSelection(text, where);
return true;
}
public static boolean moveToRightEdge(Spannable text, Layout layout) {
int where = findEdge(text, layout, 1);
setSelection(text, where);
return true;
}
private static int findEdge(Spannable text, Layout layout, int dir) {
int pt = getSelectionEnd(text);
int line = layout.getLineForOffset(pt);
int pdir = layout.getParagraphDirection(line);
if (dir * pdir < 0) {
return layout.getLineStart(line);
} else {
int end = layout.getLineEnd(line);
if (line == layout.getLineCount() - 1)
return end;
else
return end - 1;
}
}
private static int chooseHorizontal(Layout layout, int direction,
int off1, int off2) {
int line1 = layout.getLineForOffset(off1);
int line2 = layout.getLineForOffset(off2);
if (line1 == line2) {
// same line, so it goes by pure physical direction
float h1 = layout.getPrimaryHorizontal(off1);
float h2 = layout.getPrimaryHorizontal(off2);
if (direction < 0) {
// to left
if (h1 < h2)
return off1;
else
return off2;
} else {
// to right
if (h1 > h2)
return off1;
else
return off2;
}
} else {
// different line, so which line is "left" and which is "right"
// depends upon the directionality of the text
// This only checks at one end, but it's not clear what the
// right thing to do is if the ends don't agree. Even if it
// is wrong it should still not be too bad.
int line = layout.getLineForOffset(off1);
int textdir = layout.getParagraphDirection(line);
if (textdir == direction)
return Math.max(off1, off2);
else
return Math.min(off1, off2);
}
}
private static final class START implements NoCopySpan { }
private static final class END implements NoCopySpan { }
/*
* Public constants
*/
public static final Object SELECTION_START = new START();
public static final Object SELECTION_END = new END();
}
|