~brian-sidebotham/wxwidgets-cmake/wxpython-2.9.4

« back to all changes in this revision

Viewing changes to src/stc/scintilla/src/Selection.h

  • Committer: Brian Sidebotham
  • Date: 2013-08-03 14:30:08 UTC
  • Revision ID: brian.sidebotham@gmail.com-20130803143008-c7806tkych1tp6fc
Initial import into Bazaar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
7
 
 
8
#ifndef SELECTION_H
 
9
#define SELECTION_H
 
10
 
 
11
#ifdef SCI_NAMESPACE
 
12
namespace Scintilla {
 
13
#endif
 
14
 
 
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;
 
23
        }
 
24
        void Reset() {
 
25
                position = 0;
 
26
                virtualSpace = 0;
 
27
        }
 
28
        void MoveForInsertDelete(bool insertion, int startChange, int length);
 
29
        bool operator ==(const SelectionPosition &other) const {
 
30
                return position == other.position && virtualSpace == other.virtualSpace;
 
31
        }
 
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;
 
38
        }
 
39
        void SetPosition(int position_) {
 
40
                position = position_;
 
41
                virtualSpace = 0;
 
42
        }
 
43
        int VirtualSpace() const {
 
44
                return virtualSpace;
 
45
        }
 
46
        void SetVirtualSpace(int virtualSpace_) {
 
47
                PLATFORM_ASSERT(virtualSpace_ < 800000);
 
48
                if (virtualSpace_ >= 0)
 
49
                        virtualSpace = virtualSpace_;
 
50
        }
 
51
        void Add(int increment) {
 
52
                position = position + increment;
 
53
        }
 
54
        bool IsValid() const {
 
55
                return position >= 0;
 
56
        }
 
57
};
 
58
 
 
59
// Ordered range to make drawing simpler
 
60
struct SelectionSegment {       
 
61
        SelectionPosition start;
 
62
        SelectionPosition end;
 
63
        SelectionSegment() {
 
64
        }
 
65
        SelectionSegment(SelectionPosition a, SelectionPosition b) {
 
66
                if (a < b) {
 
67
                        start = a;
 
68
                        end = b;
 
69
                } else {
 
70
                        start = b;
 
71
                        end = a;
 
72
                }
 
73
        }
 
74
        bool Empty() const {
 
75
                return start == end;
 
76
        }
 
77
        void Extend(SelectionPosition p) {
 
78
                if (start > p)
 
79
                        start = p;
 
80
                if (end < p)
 
81
                        end = p;
 
82
        }
 
83
};
 
84
 
 
85
struct SelectionRange {
 
86
        SelectionPosition caret;
 
87
        SelectionPosition anchor;
 
88
 
 
89
        SelectionRange() {
 
90
        }
 
91
        SelectionRange(SelectionPosition single) : caret(single), anchor(single) {
 
92
        }
 
93
        SelectionRange(int single) : caret(single), anchor(single) {
 
94
        }
 
95
        SelectionRange(SelectionPosition caret_, SelectionPosition anchor_) : caret(caret_), anchor(anchor_) {
 
96
        }
 
97
        SelectionRange(int caret_, int anchor_) : caret(caret_), anchor(anchor_) {
 
98
        }
 
99
        bool Empty() const {
 
100
                return anchor == caret;
 
101
        }
 
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;
 
106
        }
 
107
        bool operator <(const SelectionRange &other) const {
 
108
                return caret < other.caret || ((caret == other.caret) && (anchor < other.anchor));
 
109
        }
 
110
        void Reset() {
 
111
                anchor.Reset();
 
112
                caret.Reset();
 
113
        }
 
114
        void ClearVirtualSpace() {
 
115
                anchor.SetVirtualSpace(0);
 
116
                caret.SetVirtualSpace(0);
 
117
        }
 
118
        bool Contains(int pos) const;
 
119
        bool Contains(SelectionPosition sp) const;
 
120
        bool ContainsCharacter(int posCharacter) const;
 
121
        SelectionSegment Intersect(SelectionSegment check) const;
 
122
        SelectionPosition Start() const {
 
123
                return (anchor < caret) ? anchor : caret;
 
124
        }
 
125
        SelectionPosition End() const {
 
126
                return (anchor < caret) ? caret : anchor;
 
127
        }
 
128
        bool Trim(SelectionRange range);
 
129
        // If range is all virtual collapse to start of virtual space
 
130
        void MinimizeVirtualSpace();
 
131
};
 
132
 
 
133
    
 
134
#include "wx/vector.h"
 
135
    
 
136
class Selection {
 
137
        wxVector<SelectionRange> ranges;
 
138
        wxVector<SelectionRange> rangesSaved;
 
139
        SelectionRange rangeRectangular;
 
140
        size_t mainRange;
 
141
        bool moveExtends;
 
142
        bool tentativeMain;
 
143
public:
 
144
        enum selTypes { noSel, selStream, selRectangle, selLines, selThin };
 
145
        selTypes selType;
 
146
 
 
147
        Selection();
 
148
        ~Selection();
 
149
        bool IsRectangular() const;
 
150
        int MainCaret() const;
 
151
        int MainAnchor() const;
 
152
        SelectionRange &Rectangular();
 
153
        SelectionSegment Limits() const;
 
154
        // This is for when you want to move the caret in response to a 
 
155
        // user direction command - for rectangular selections, use the range
 
156
        // that covers all selected text otherwise return the main selection.
 
157
        SelectionSegment LimitsForRectangularElseMain() const;
 
158
        size_t Count() const;
 
159
        size_t Main() const;
 
160
        void SetMain(size_t r);
 
161
        SelectionRange &Range(size_t r);
 
162
        SelectionRange &RangeMain();
 
163
        bool MoveExtends() const;
 
164
        void SetMoveExtends(bool moveExtends_);
 
165
        bool Empty() const;
 
166
        SelectionPosition Last() const;
 
167
        int Length() const;
 
168
        void MovePositions(bool insertion, int startChange, int length);
 
169
        void TrimSelection(SelectionRange range);
 
170
        void SetSelection(SelectionRange range);
 
171
        void AddSelection(SelectionRange range);
 
172
        void TentativeSelection(SelectionRange range);
 
173
        void CommitTentative();
 
174
        int CharacterInSelection(int posCharacter) const;
 
175
        int InSelectionForEOL(int pos) const;
 
176
        int VirtualSpaceFor(int pos) const;
 
177
        void Clear();
 
178
        void RemoveDuplicates();
 
179
        void RotateMain();
 
180
        bool Tentative() const { return tentativeMain; }
 
181
        wxVector<SelectionRange> RangesCopy() const {
 
182
                return ranges;
 
183
        }
 
184
};
 
185
 
 
186
#ifdef SCI_NAMESPACE
 
187
}
 
188
#endif
 
189
 
 
190
#endif