Merge pull request #2212 from TwlyY29/bibtex-parser
[geany-mirror.git] / scintilla / src / Selection.h
blob82fc4aa487790d510cf72a58d08c36c260700558
1 // Scintilla source code edit control
2 /** @file Selection.h
3 ** Classes maintaining the selection.
4 **/
5 // Copyright 2009 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #ifndef SELECTION_H
9 #define SELECTION_H
11 namespace Scintilla {
13 class SelectionPosition {
14 Sci::Position position;
15 Sci::Position virtualSpace;
16 public:
17 explicit SelectionPosition(Sci::Position position_=INVALID_POSITION, Sci::Position virtualSpace_=0) noexcept : position(position_), virtualSpace(virtualSpace_) {
18 PLATFORM_ASSERT(virtualSpace < 800000);
19 if (virtualSpace < 0)
20 virtualSpace = 0;
22 void Reset() {
23 position = 0;
24 virtualSpace = 0;
26 void MoveForInsertDelete(bool insertion, Sci::Position startChange, Sci::Position length);
27 bool operator ==(const SelectionPosition &other) const {
28 return position == other.position && virtualSpace == other.virtualSpace;
30 bool operator <(const SelectionPosition &other) const;
31 bool operator >(const SelectionPosition &other) const;
32 bool operator <=(const SelectionPosition &other) const;
33 bool operator >=(const SelectionPosition &other) const;
34 Sci::Position Position() const {
35 return position;
37 void SetPosition(Sci::Position position_) {
38 position = position_;
39 virtualSpace = 0;
41 Sci::Position VirtualSpace() const {
42 return virtualSpace;
44 void SetVirtualSpace(Sci::Position virtualSpace_) {
45 PLATFORM_ASSERT(virtualSpace_ < 800000);
46 if (virtualSpace_ >= 0)
47 virtualSpace = virtualSpace_;
49 void Add(Sci::Position increment) {
50 position = position + increment;
52 bool IsValid() const {
53 return position >= 0;
57 // Ordered range to make drawing simpler
58 struct SelectionSegment {
59 SelectionPosition start;
60 SelectionPosition end;
61 SelectionSegment() : start(), end() {
63 SelectionSegment(SelectionPosition a, SelectionPosition b) {
64 if (a < b) {
65 start = a;
66 end = b;
67 } else {
68 start = b;
69 end = a;
72 bool Empty() const {
73 return start == end;
75 void Extend(SelectionPosition p) {
76 if (start > p)
77 start = p;
78 if (end < p)
79 end = p;
83 struct SelectionRange {
84 SelectionPosition caret;
85 SelectionPosition anchor;
87 SelectionRange() noexcept : caret(), anchor() {
89 explicit SelectionRange(SelectionPosition single) noexcept : caret(single), anchor(single) {
91 explicit SelectionRange(Sci::Position single) noexcept : caret(single), anchor(single) {
93 SelectionRange(SelectionPosition caret_, SelectionPosition anchor_) noexcept : caret(caret_), anchor(anchor_) {
95 SelectionRange(Sci::Position caret_, Sci::Position anchor_) noexcept : caret(caret_), anchor(anchor_) {
97 bool Empty() const {
98 return anchor == caret;
100 Sci::Position Length() const;
101 // Sci::Position Width() const; // Like Length but takes virtual space into account
102 bool operator ==(const SelectionRange &other) const {
103 return caret == other.caret && anchor == other.anchor;
105 bool operator <(const SelectionRange &other) const {
106 return caret < other.caret || ((caret == other.caret) && (anchor < other.anchor));
108 void Reset() {
109 anchor.Reset();
110 caret.Reset();
112 void ClearVirtualSpace() {
113 anchor.SetVirtualSpace(0);
114 caret.SetVirtualSpace(0);
116 void MoveForInsertDelete(bool insertion, Sci::Position startChange, Sci::Position length);
117 bool Contains(Sci::Position pos) const;
118 bool Contains(SelectionPosition sp) const;
119 bool ContainsCharacter(Sci::Position posCharacter) const;
120 SelectionSegment Intersect(SelectionSegment check) const;
121 SelectionPosition Start() const {
122 return (anchor < caret) ? anchor : caret;
124 SelectionPosition End() const {
125 return (anchor < caret) ? caret : anchor;
127 void Swap();
128 bool Trim(SelectionRange range);
129 // If range is all virtual collapse to start of virtual space
130 void MinimizeVirtualSpace();
133 class Selection {
134 std::vector<SelectionRange> ranges;
135 std::vector<SelectionRange> rangesSaved;
136 SelectionRange rangeRectangular;
137 size_t mainRange;
138 bool moveExtends;
139 bool tentativeMain;
140 public:
141 enum selTypes { noSel, selStream, selRectangle, selLines, selThin };
142 selTypes selType;
144 Selection() noexcept;
145 ~Selection();
146 bool IsRectangular() const;
147 Sci::Position MainCaret() const;
148 Sci::Position MainAnchor() const;
149 SelectionRange &Rectangular();
150 SelectionSegment Limits() const;
151 // This is for when you want to move the caret in response to a
152 // user direction command - for rectangular selections, use the range
153 // that covers all selected text otherwise return the main selection.
154 SelectionSegment LimitsForRectangularElseMain() const;
155 size_t Count() const;
156 size_t Main() const;
157 void SetMain(size_t r);
158 SelectionRange &Range(size_t r);
159 const SelectionRange &Range(size_t r) const;
160 SelectionRange &RangeMain();
161 const SelectionRange &RangeMain() const;
162 SelectionPosition Start() const;
163 bool MoveExtends() const;
164 void SetMoveExtends(bool moveExtends_);
165 bool Empty() const;
166 SelectionPosition Last() const;
167 Sci::Position Length() const;
168 void MovePositions(bool insertion, Sci::Position startChange, Sci::Position length);
169 void TrimSelection(SelectionRange range);
170 void TrimOtherSelections(size_t r, SelectionRange range);
171 void SetSelection(SelectionRange range);
172 void AddSelection(SelectionRange range);
173 void AddSelectionWithoutTrim(SelectionRange range);
174 void DropSelection(size_t r);
175 void DropAdditionalRanges();
176 void TentativeSelection(SelectionRange range);
177 void CommitTentative();
178 int CharacterInSelection(Sci::Position posCharacter) const;
179 int InSelectionForEOL(Sci::Position pos) const;
180 Sci::Position VirtualSpaceFor(Sci::Position pos) const;
181 void Clear();
182 void RemoveDuplicates();
183 void RotateMain();
184 bool Tentative() const { return tentativeMain; }
185 std::vector<SelectionRange> RangesCopy() const {
186 return ranges;
192 #endif