Merge branch 'master' of https://github.com/konsolebox/geany into konsolebox-master
[geany-mirror.git] / scintilla / src / Selection.h
blob22c01beffdb34f65974428ee04ed5cb4e27d58c3
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 #ifdef SCI_NAMESPACE
12 namespace Scintilla {
13 #endif
15 class SelectionPosition {
16 int position;
17 int virtualSpace;
18 public:
19 explicit SelectionPosition(int position_=INVALID_POSITION, int virtualSpace_=0) : position(position_), virtualSpace(virtualSpace_) {
20 PLATFORM_ASSERT(virtualSpace < 800000);
21 if (virtualSpace < 0)
22 virtualSpace = 0;
24 void Reset() {
25 position = 0;
26 virtualSpace = 0;
28 void MoveForInsertDelete(bool insertion, int startChange, int 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 int Position() const {
37 return position;
39 void SetPosition(int position_) {
40 position = position_;
41 virtualSpace = 0;
43 int VirtualSpace() const {
44 return virtualSpace;
46 void SetVirtualSpace(int virtualSpace_) {
47 PLATFORM_ASSERT(virtualSpace_ < 800000);
48 if (virtualSpace_ >= 0)
49 virtualSpace = virtualSpace_;
51 void Add(int increment) {
52 position = position + increment;
54 bool IsValid() const {
55 return position >= 0;
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) {
66 if (a < b) {
67 start = a;
68 end = b;
69 } else {
70 start = b;
71 end = a;
74 bool Empty() const {
75 return start == end;
77 void Extend(SelectionPosition p) {
78 if (start > p)
79 start = p;
80 if (end < p)
81 end = 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(int single) : caret(single), anchor(single) {
95 SelectionRange(SelectionPosition caret_, SelectionPosition anchor_) : caret(caret_), anchor(anchor_) {
97 SelectionRange(int caret_, int anchor_) : caret(caret_), anchor(anchor_) {
99 bool Empty() const {
100 return anchor == caret;
102 int Length() const;
103 // int 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));
110 void Reset() {
111 anchor.Reset();
112 caret.Reset();
114 void ClearVirtualSpace() {
115 anchor.SetVirtualSpace(0);
116 caret.SetVirtualSpace(0);
118 void MoveForInsertDelete(bool insertion, int startChange, int length);
119 bool Contains(int pos) const;
120 bool Contains(SelectionPosition sp) const;
121 bool ContainsCharacter(int 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;
129 bool Trim(SelectionRange range);
130 // If range is all virtual collapse to start of virtual space
131 void MinimizeVirtualSpace();
134 class Selection {
135 std::vector<SelectionRange> ranges;
136 std::vector<SelectionRange> rangesSaved;
137 SelectionRange rangeRectangular;
138 size_t mainRange;
139 bool moveExtends;
140 bool tentativeMain;
141 public:
142 enum selTypes { noSel, selStream, selRectangle, selLines, selThin };
143 selTypes selType;
145 Selection();
146 ~Selection();
147 bool IsRectangular() const;
148 int MainCaret() const;
149 int MainAnchor() const;
150 SelectionRange &Rectangular();
151 SelectionSegment Limits() const;
152 // This is for when you want to move the caret in response to a
153 // user direction command - for rectangular selections, use the range
154 // that covers all selected text otherwise return the main selection.
155 SelectionSegment LimitsForRectangularElseMain() const;
156 size_t Count() const;
157 size_t Main() const;
158 void SetMain(size_t r);
159 SelectionRange &Range(size_t r);
160 const SelectionRange &Range(size_t r) const;
161 SelectionRange &RangeMain();
162 const SelectionRange &RangeMain() const;
163 SelectionPosition Start() const;
164 bool MoveExtends() const;
165 void SetMoveExtends(bool moveExtends_);
166 bool Empty() const;
167 SelectionPosition Last() const;
168 int Length() const;
169 void MovePositions(bool insertion, int startChange, int length);
170 void TrimSelection(SelectionRange range);
171 void TrimOtherSelections(size_t r, SelectionRange range);
172 void SetSelection(SelectionRange range);
173 void AddSelection(SelectionRange range);
174 void AddSelectionWithoutTrim(SelectionRange range);
175 void DropSelection(size_t r);
176 void DropAdditionalRanges();
177 void TentativeSelection(SelectionRange range);
178 void CommitTentative();
179 int CharacterInSelection(int posCharacter) const;
180 int InSelectionForEOL(int pos) const;
181 int VirtualSpaceFor(int pos) const;
182 void Clear();
183 void RemoveDuplicates();
184 void RotateMain();
185 bool Tentative() const { return tentativeMain; }
186 std::vector<SelectionRange> RangesCopy() const {
187 return ranges;
191 #ifdef SCI_NAMESPACE
193 #endif
195 #endif