From 130731d7c7bb43e82d5b1d9eb1441b1e07124b05 Mon Sep 17 00:00:00 2001 From: nyamatongwe Date: Wed, 15 Jul 2009 03:05:04 +0000 Subject: No explicit count of ranges in selection as can use vector::size(). Removed EmptyRanges method so that there is always at least one selection. Added SetSelection method to set a simple single selection. Removed 3 argument form of AddSelection since callers do know which argument is the caret. Simplified rectangular selection code. --- src/Editor.cxx | 58 +++++++++++++++------------------------- src/Selection.cxx | 79 +++++++++++++++++-------------------------------------- src/Selection.h | 5 +--- 3 files changed, 46 insertions(+), 96 deletions(-) (limited to 'src') diff --git a/src/Editor.cxx b/src/Editor.cxx index 5effff931..71c8965fb 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -687,34 +687,20 @@ void Editor::SetRectangularRange() { if (sel.IsRectangular()) { int xAnchor = XFromPosition(sel.Rectangular().anchor); int xCaret = XFromPosition(sel.Rectangular().caret); - bool anchorTop = sel.Rectangular().anchor < sel.Rectangular().caret; - bool anchorLeft = xAnchor < xCaret; if (sel.selType == Selection::selThin) { xCaret = xAnchor; } - SelectionPosition selStart = sel.Rectangular().Start(); - SelectionPosition selEnd = sel.Rectangular().End(); - int lineStart = pdoc->LineFromPosition(selStart.Position()); - int lineEnd = pdoc->LineFromPosition(selEnd.Position()); - // Left of rectangle - int minX = Platform::Minimum(xAnchor, xCaret); - // Right of rectangle - int maxX = Platform::Maximum(xAnchor, xCaret); - sel.EmptyRanges(); - int increment = 1; - if (!anchorTop) { - increment = -1; - lineEnd = lineStart; - lineStart = pdoc->LineFromPosition(selEnd.Position()); - } - for (int line=lineStart; line != lineEnd+increment; line += increment) { - SelectionPosition spMin(SPositionFromLineX(line, minX)); - SelectionPosition spMax(SPositionFromLineX(line, maxX)); - if ((virtualSpaceOptions & SCVS_RECTANGULARSELECTION) == 0) { - spMin.SetVirtualSpace(0); - spMax.SetVirtualSpace(0); - } - sel.AddSelection(spMin, spMax, anchorLeft); + int lineAnchor = pdoc->LineFromPosition(sel.Rectangular().anchor.Position()); + int lineCaret = pdoc->LineFromPosition(sel.Rectangular().caret.Position()); + int increment = (lineCaret > lineAnchor) ? 1 : -1; + for (int line=lineAnchor; line != lineCaret+increment; line += increment) { + SelectionRange range(SPositionFromLineX(line, xCaret), SPositionFromLineX(line, xAnchor)); + if ((virtualSpaceOptions & SCVS_RECTANGULARSELECTION) == 0) + range.ClearVirtualSpace(); + if (line == lineAnchor) + sel.SetSelection(range); + else + sel.AddSelection(range); } } } @@ -3669,8 +3655,7 @@ void Editor::FilterSelections() { if (!additionalSelectionTyping && (sel.Count() > 1)) { SelectionRange rangeOnly = sel.RangeMain(); InvalidateSelection(rangeOnly, true); - sel.EmptyRanges(); - sel.AddSelection(rangeOnly); + sel.SetSelection(rangeOnly); } } @@ -3686,7 +3671,7 @@ void Editor::AddCharUTF(char *s, unsigned int len, bool treatAsDBCS) { if (!sel.Range(r).Empty()) { if (sel.Range(r).Length()) { pdoc->DeleteChars(positionInsert, sel.Range(r).Length()); - sel.ClearVirtualSpace(r); + sel.Range(r).ClearVirtualSpace(); } else { // Range is all virtual so collapse to start of virtual space sel.Range(r).MinimizeVirtualSpace(); @@ -3695,7 +3680,7 @@ void Editor::AddCharUTF(char *s, unsigned int len, bool treatAsDBCS) { if (positionInsert < pdoc->Length()) { if (!IsEOLChar(pdoc->CharAt(positionInsert))) { pdoc->DelChar(positionInsert); - sel.ClearVirtualSpace(r); + sel.Range(r).ClearVirtualSpace(); } } } @@ -3704,7 +3689,7 @@ void Editor::AddCharUTF(char *s, unsigned int len, bool treatAsDBCS) { sel.Range(r).caret.SetPosition(positionInsert + len); sel.Range(r).anchor.SetPosition(positionInsert + len); } - sel.ClearVirtualSpace(r); + sel.Range(r).ClearVirtualSpace(); // If in wrap mode rewrap current line so EnsureCaretVisible has accurate information if (wrapState != eWrapNone) { AutoSurface surface(this); @@ -3777,7 +3762,7 @@ void Editor::ClearSelection() { sel.Range(r).End().Position())) { pdoc->DeleteChars(sel.Range(r).Start().Position(), sel.Range(r).Length()); - sel.ClearVirtualSpace(r); + sel.Range(r).ClearVirtualSpace(); } } } @@ -3884,10 +3869,10 @@ void Editor::Clear() { if (!RangeContainsProtected(sel.Range(r).caret.Position(), sel.Range(r).caret.Position() + 1)) { if ((sel.Count() == 1) || !IsEOLChar(pdoc->CharAt(sel.Range(r).caret.Position()))) { pdoc->DelChar(sel.Range(r).caret.Position()); - sel.ClearVirtualSpace(r); + sel.Range(r).ClearVirtualSpace(); } // else multiple selection so don't eat line ends } else { - sel.ClearVirtualSpace(r); + sel.Range(r).ClearVirtualSpace(); } } } else { @@ -3960,7 +3945,7 @@ void Editor::DelCharBack(bool allowLineStartDeletion) { } } } else { - sel.ClearVirtualSpace(r); + sel.Range(r).ClearVirtualSpace(); } } } else { @@ -8171,13 +8156,12 @@ sptr_t Editor::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) { break; case SCI_SETSELECTION: - sel.Clear(); - sel.RangeMain() = SelectionRange(wParam, lParam); + sel.SetSelection(SelectionRange(wParam, lParam)); Redraw(); break; case SCI_ADDSELECTION: - sel.AddSelection(SelectionPosition(wParam), SelectionPosition(lParam), false); + sel.AddSelection(SelectionRange(wParam, lParam)); Redraw(); break; diff --git a/src/Selection.cxx b/src/Selection.cxx index eada57d5e..43607b244 100644 --- a/src/Selection.cxx +++ b/src/Selection.cxx @@ -72,26 +72,6 @@ int SelectionRange::Length() const { } } -#ifdef NEEDED -// Like Length but takes virtual space into account -int SelectionRange::Width() const { - SelectionPosition first; - SelectionPosition last; - if (anchor > caret) { - first = caret; - last = anchor; - } else { - first = anchor; - last = caret; - } - if (first.Position() == last.Position()) { - return last.VirtualSpace() - first.VirtualSpace(); - } else { - return last.Position() - first.Position() + last.VirtualSpace(); - } -} -#endif - bool SelectionRange::Contains(int pos) const { if (anchor > caret) return (pos >= caret.Position()) && (pos <= anchor.Position()); @@ -176,7 +156,7 @@ void SelectionRange::MinimizeVirtualSpace() { } } -Selection::Selection() : ranges(0), nRanges(0), mainRange(0), moveExtends(false), selType(selStream) { +Selection::Selection() : mainRange(0), moveExtends(false), selType(selStream) { AddSelection(SelectionPosition(0)); } @@ -200,7 +180,7 @@ SelectionRange &Selection::Rectangular() { } size_t Selection::Count() const { - return nRanges; + return ranges.size(); } size_t Selection::Main() const { @@ -208,7 +188,7 @@ size_t Selection::Main() const { } void Selection::SetMain(size_t r) { - PLATFORM_ASSERT(r < nRanges); + PLATFORM_ASSERT(r < ranges.size()); mainRange = r; } @@ -220,10 +200,6 @@ SelectionRange &Selection::RangeMain() { return ranges[mainRange]; } -void Selection::ClearVirtualSpace(size_t r) { - ranges[r].ClearVirtualSpace(); -} - bool Selection::MoveExtends() const { return moveExtends; } @@ -233,7 +209,7 @@ void Selection::SetMoveExtends(bool moveExtends_) { } bool Selection::Empty() const { - for (size_t i=0; i ranges[i].Start().Position()) && (pos <= ranges[i].End().Position())) return i == mainRange ? 1 : 2; } @@ -319,7 +292,7 @@ int Selection::InSelectionForEOL(int pos) const { int Selection::VirtualSpaceFor(int pos) const { int virtualSpace = 0; - for (size_t i=0; i ranges; SelectionRange rangeRectangular; - size_t nRanges; size_t mainRange; bool moveExtends; public: @@ -142,7 +141,6 @@ public: void SetMain(size_t r); SelectionRange &Range(size_t r); SelectionRange &RangeMain(); - void ClearVirtualSpace(size_t r); bool MoveExtends() const; void SetMoveExtends(bool moveExtends_); bool Empty() const; @@ -150,14 +148,13 @@ public: int Length() const; void MovePositions(bool insertion, int startChange, int length); void TrimSelection(SelectionRange range); + void SetSelection(SelectionRange range); void AddSelection(SelectionRange range); void AddSelection(SelectionPosition spPos); - void AddSelection(SelectionPosition spStartPos, SelectionPosition spEndPos, bool anchorLeft); int CharacterInSelection(int posCharacter) const; int InSelectionForEOL(int pos) const; int VirtualSpaceFor(int pos) const; void Clear(); - void EmptyRanges(); }; #ifdef SCI_NAMESPACE -- cgit v1.2.3