1
// Scintilla source code edit control
3
** Classes maintaining the selection.
5
// Copyright 2009 by Neil Hodgson <neilh@scintilla.org>
6
// The License.txt file describes the conditions under which this software may be distributed.
19
class SelectionPosition {
23
explicit SelectionPosition(int position_=INVALID_POSITION, int virtualSpace_=0) : position(position_), virtualSpace(virtualSpace_) {
24
PLATFORM_ASSERT(virtualSpace < 800000);
32
void MoveForInsertDelete(bool insertion, int startChange, int length);
33
bool operator ==(const SelectionPosition &other) const {
34
return position == other.position && virtualSpace == other.virtualSpace;
36
bool operator <(const SelectionPosition &other) const;
37
bool operator >(const SelectionPosition &other) const;
38
bool operator <=(const SelectionPosition &other) const;
39
bool operator >=(const SelectionPosition &other) const;
40
int Position() const {
43
void SetPosition(int position_) {
47
int VirtualSpace() const {
50
void SetVirtualSpace(int virtualSpace_) {
51
PLATFORM_ASSERT(virtualSpace_ < 800000);
52
if (virtualSpace_ >= 0)
53
virtualSpace = virtualSpace_;
55
void Add(int increment) {
56
position = position + increment;
58
bool IsValid() const {
63
// Ordered range to make drawing simpler
64
struct SelectionSegment {
65
SelectionPosition start;
66
SelectionPosition end;
69
SelectionSegment(SelectionPosition a, SelectionPosition b) {
81
void Extend(SelectionPosition p) {
89
struct SelectionRange {
90
SelectionPosition caret;
91
SelectionPosition anchor;
95
SelectionRange(SelectionPosition single) : caret(single), anchor(single) {
97
SelectionRange(int single) : caret(single), anchor(single) {
99
SelectionRange(SelectionPosition caret_, SelectionPosition anchor_) : caret(caret_), anchor(anchor_) {
101
SelectionRange(int caret_, int anchor_) : caret(caret_), anchor(anchor_) {
104
return anchor == caret;
107
// int Width() const; // Like Length but takes virtual space into account
108
bool operator ==(const SelectionRange &other) const {
109
return caret == other.caret && anchor == other.anchor;
111
bool operator <(const SelectionRange &other) const {
112
return caret < other.caret || ((caret == other.caret) && (anchor < other.anchor));
118
void ClearVirtualSpace() {
119
anchor.SetVirtualSpace(0);
120
caret.SetVirtualSpace(0);
122
bool Contains(int pos) const;
123
bool Contains(SelectionPosition sp) const;
124
bool ContainsCharacter(int posCharacter) const;
125
SelectionSegment Intersect(SelectionSegment check) const;
126
SelectionPosition Start() const {
127
return (anchor < caret) ? anchor : caret;
129
SelectionPosition End() const {
130
return (anchor < caret) ? caret : anchor;
132
bool Trim(SelectionRange range);
133
// If range is all virtual collapse to start of virtual space
134
void MinimizeVirtualSpace();
138
std::vector<SelectionRange> ranges;
139
std::vector<SelectionRange> rangesSaved;
140
SelectionRange rangeRectangular;
145
enum selTypes { noSel, selStream, selRectangle, selLines, selThin };
150
bool IsRectangular() const;
151
int MainCaret() const;
152
int MainAnchor() const;
153
SelectionRange &Rectangular();
154
SelectionSegment Limits() const;
155
// This is for when you want to move the caret in response to a
156
// user direction command - for rectangular selections, use the range
157
// that covers all selected text otherwise return the main selection.
158
SelectionSegment LimitsForRectangularElseMain() const;
159
size_t Count() const;
161
void SetMain(size_t r);
162
SelectionRange &Range(size_t r);
163
SelectionRange &RangeMain();
164
bool MoveExtends() const;
165
void SetMoveExtends(bool moveExtends_);
167
SelectionPosition Last() const;
169
void MovePositions(bool insertion, int startChange, int length);
170
void TrimSelection(SelectionRange range);
171
void SetSelection(SelectionRange range);
172
void AddSelection(SelectionRange range);
173
void TentativeSelection(SelectionRange range);
174
void CommitTentative();
175
int CharacterInSelection(int posCharacter) const;
176
int InSelectionForEOL(int pos) const;
177
int VirtualSpaceFor(int pos) const;
179
void RemoveDuplicates();
181
bool Tentative() const { return tentativeMain; }
182
std::vector<SelectionRange> RangesCopy() const {