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.
15 class SelectionPosition
{
16 Sci::Position position
;
17 Sci::Position virtualSpace
;
19 explicit SelectionPosition(Sci::Position position_
=INVALID_POSITION
, Sci::Position virtualSpace_
=0) : position(position_
), virtualSpace(virtualSpace_
) {
20 PLATFORM_ASSERT(virtualSpace
< 800000);
28 void MoveForInsertDelete(bool insertion
, Sci::Position startChange
, Sci::Position length
);
29 bool operator ==(const SelectionPosition
&other
) const {
30 return position
== other
.position
&& virtualSpace
== other
.virtualSpace
;
32 bool operator <(const SelectionPosition
&other
) const;
33 bool operator >(const SelectionPosition
&other
) const;
34 bool operator <=(const SelectionPosition
&other
) const;
35 bool operator >=(const SelectionPosition
&other
) const;
36 Sci::Position
Position() const {
39 void SetPosition(Sci::Position position_
) {
43 Sci::Position
VirtualSpace() const {
46 void SetVirtualSpace(Sci::Position virtualSpace_
) {
47 PLATFORM_ASSERT(virtualSpace_
< 800000);
48 if (virtualSpace_
>= 0)
49 virtualSpace
= virtualSpace_
;
51 void Add(Sci::Position increment
) {
52 position
= position
+ increment
;
54 bool IsValid() const {
59 // Ordered range to make drawing simpler
60 struct SelectionSegment
{
61 SelectionPosition start
;
62 SelectionPosition end
;
63 SelectionSegment() : start(), end() {
65 SelectionSegment(SelectionPosition a
, SelectionPosition b
) {
77 void Extend(SelectionPosition p
) {
85 struct SelectionRange
{
86 SelectionPosition caret
;
87 SelectionPosition anchor
;
89 SelectionRange() : caret(), anchor() {
91 explicit SelectionRange(SelectionPosition single
) : caret(single
), anchor(single
) {
93 explicit SelectionRange(Sci::Position single
) : caret(single
), anchor(single
) {
95 SelectionRange(SelectionPosition caret_
, SelectionPosition anchor_
) : caret(caret_
), anchor(anchor_
) {
97 SelectionRange(Sci::Position caret_
, Sci::Position anchor_
) : caret(caret_
), anchor(anchor_
) {
100 return anchor
== caret
;
102 Sci::Position
Length() const;
103 // Sci::Position Width() const; // Like Length but takes virtual space into account
104 bool operator ==(const SelectionRange
&other
) const {
105 return caret
== other
.caret
&& anchor
== other
.anchor
;
107 bool operator <(const SelectionRange
&other
) const {
108 return caret
< other
.caret
|| ((caret
== other
.caret
) && (anchor
< other
.anchor
));
114 void ClearVirtualSpace() {
115 anchor
.SetVirtualSpace(0);
116 caret
.SetVirtualSpace(0);
118 void MoveForInsertDelete(bool insertion
, Sci::Position startChange
, Sci::Position length
);
119 bool Contains(Sci::Position pos
) const;
120 bool Contains(SelectionPosition sp
) const;
121 bool ContainsCharacter(Sci::Position posCharacter
) const;
122 SelectionSegment
Intersect(SelectionSegment check
) const;
123 SelectionPosition
Start() const {
124 return (anchor
< caret
) ? anchor
: caret
;
126 SelectionPosition
End() const {
127 return (anchor
< caret
) ? caret
: anchor
;
130 bool Trim(SelectionRange range
);
131 // If range is all virtual collapse to start of virtual space
132 void MinimizeVirtualSpace();
136 std::vector
<SelectionRange
> ranges
;
137 std::vector
<SelectionRange
> rangesSaved
;
138 SelectionRange rangeRectangular
;
143 enum selTypes
{ noSel
, selStream
, selRectangle
, selLines
, selThin
};
148 bool IsRectangular() const;
149 Sci::Position
MainCaret() const;
150 Sci::Position
MainAnchor() const;
151 SelectionRange
&Rectangular();
152 SelectionSegment
Limits() const;
153 // This is for when you want to move the caret in response to a
154 // user direction command - for rectangular selections, use the range
155 // that covers all selected text otherwise return the main selection.
156 SelectionSegment
LimitsForRectangularElseMain() const;
157 size_t Count() const;
159 void SetMain(size_t r
);
160 SelectionRange
&Range(size_t r
);
161 const SelectionRange
&Range(size_t r
) const;
162 SelectionRange
&RangeMain();
163 const SelectionRange
&RangeMain() const;
164 SelectionPosition
Start() const;
165 bool MoveExtends() const;
166 void SetMoveExtends(bool moveExtends_
);
168 SelectionPosition
Last() const;
169 Sci::Position
Length() const;
170 void MovePositions(bool insertion
, Sci::Position startChange
, Sci::Position length
);
171 void TrimSelection(SelectionRange range
);
172 void TrimOtherSelections(size_t r
, SelectionRange range
);
173 void SetSelection(SelectionRange range
);
174 void AddSelection(SelectionRange range
);
175 void AddSelectionWithoutTrim(SelectionRange range
);
176 void DropSelection(size_t r
);
177 void DropAdditionalRanges();
178 void TentativeSelection(SelectionRange range
);
179 void CommitTentative();
180 int CharacterInSelection(Sci::Position posCharacter
) const;
181 int InSelectionForEOL(Sci::Position pos
) const;
182 Sci::Position
VirtualSpaceFor(Sci::Position pos
) const;
184 void RemoveDuplicates();
186 bool Tentative() const { return tentativeMain
; }
187 std::vector
<SelectionRange
> RangesCopy() const {