"Fossies" - the Fresh Open Source Software Archive

Member "geany-1.36/scintilla/src/Selection.h" (28 Sep 2019, 5820 Bytes) of package /linux/misc/geany-1.36.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Selection.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.35_vs_1.36.

    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 namespace Scintilla {
   12 
   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;
   21     }
   22     void Reset() {
   23         position = 0;
   24         virtualSpace = 0;
   25     }
   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;
   29     }
   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;
   36     }
   37     void SetPosition(Sci::Position position_) {
   38         position = position_;
   39         virtualSpace = 0;
   40     }
   41     Sci::Position VirtualSpace() const {
   42         return virtualSpace;
   43     }
   44     void SetVirtualSpace(Sci::Position virtualSpace_) {
   45         PLATFORM_ASSERT(virtualSpace_ < 800000);
   46         if (virtualSpace_ >= 0)
   47             virtualSpace = virtualSpace_;
   48     }
   49     void Add(Sci::Position increment) {
   50         position = position + increment;
   51     }
   52     bool IsValid() const {
   53         return position >= 0;
   54     }
   55 };
   56 
   57 // Ordered range to make drawing simpler
   58 struct SelectionSegment {
   59     SelectionPosition start;
   60     SelectionPosition end;
   61     SelectionSegment() : start(), end() {
   62     }
   63     SelectionSegment(SelectionPosition a, SelectionPosition b) {
   64         if (a < b) {
   65             start = a;
   66             end = b;
   67         } else {
   68             start = b;
   69             end = a;
   70         }
   71     }
   72     bool Empty() const {
   73         return start == end;
   74     }
   75     void Extend(SelectionPosition p) {
   76         if (start > p)
   77             start = p;
   78         if (end < p)
   79             end = p;
   80     }
   81 };
   82 
   83 struct SelectionRange {
   84     SelectionPosition caret;
   85     SelectionPosition anchor;
   86 
   87     SelectionRange() noexcept : caret(), anchor() {
   88     }
   89     explicit SelectionRange(SelectionPosition single) noexcept : caret(single), anchor(single) {
   90     }
   91     explicit SelectionRange(Sci::Position single) noexcept : caret(single), anchor(single) {
   92     }
   93     SelectionRange(SelectionPosition caret_, SelectionPosition anchor_) noexcept : caret(caret_), anchor(anchor_) {
   94     }
   95     SelectionRange(Sci::Position caret_, Sci::Position anchor_) noexcept : caret(caret_), anchor(anchor_) {
   96     }
   97     bool Empty() const {
   98         return anchor == caret;
   99     }
  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;
  104     }
  105     bool operator <(const SelectionRange &other) const {
  106         return caret < other.caret || ((caret == other.caret) && (anchor < other.anchor));
  107     }
  108     void Reset() {
  109         anchor.Reset();
  110         caret.Reset();
  111     }
  112     void ClearVirtualSpace() {
  113         anchor.SetVirtualSpace(0);
  114         caret.SetVirtualSpace(0);
  115     }
  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;
  123     }
  124     SelectionPosition End() const {
  125         return (anchor < caret) ? caret : anchor;
  126     }
  127     void Swap();
  128     bool Trim(SelectionRange range);
  129     // If range is all virtual collapse to start of virtual space
  130     void MinimizeVirtualSpace();
  131 };
  132 
  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;
  143 
  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;
  187     }
  188 };
  189 
  190 }
  191 
  192 #endif