diff --git a/src/selection.cpp b/src/selection.cpp index 68d72e7..80ab288 100644 --- a/src/selection.cpp +++ b/src/selection.cpp @@ -1,97 +1,107 @@ +/*************************************************************************** + * Copyright (C) 2003-2011 by Joachim Eibl * + * joachim.eibl at gmx.de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + #include "gnudiff_diff.h" #include "selection.h" #include int Selection::firstPosInLine(LineRef l) { Q_ASSERT(firstLine != invalidRef); LineRef l1 = firstLine; LineRef l2 = lastLine; int p1 = firstPos; int p2 = lastPos; if(l1 > l2) { std::swap(l1, l2); std::swap(p1, p2); } if(l1 == l2 && p1 > p2) { std::swap(p1, p2); } if(l == l1) return p1; return 0; } int Selection::lastPosInLine(LineRef l) { Q_ASSERT(firstLine != invalidRef); LineRef l1 = firstLine; LineRef l2 = lastLine; int p1 = firstPos; int p2 = lastPos; if(l1 > l2) { std::swap(l1, l2); std::swap(p1, p2); } if(l1 == l2 && p1 > p2) { std::swap(p1, p2); } if(l == l2) return p2; return INT_MAX; } bool Selection::within(LineRef l, LineRef p) { if(firstLine == invalidRef) return false; LineRef l1 = firstLine; LineRef l2 = lastLine; int p1 = firstPos; int p2 = lastPos; if(l1 > l2) { std::swap(l1, l2); std::swap(p1, p2); } if(l1 == l2 && p1 > p2) { std::swap(p1, p2); } if(l1 <= l && l <= l2) { if(l1 == l2) return p >= p1 && p < p2; if(l == l1) return p >= p1; if(l == l2) return p < p2; return true; } return false; } bool Selection::lineWithin(LineRef l) { if(firstLine == invalidRef) return false; LineRef l1 = firstLine; LineRef l2 = lastLine; if(l1 > l2) { std::swap(l1, l2); } return (l1 <= l && l <= l2); } diff --git a/src/selection.h b/src/selection.h index f9b13a5..bcce88a 100644 --- a/src/selection.h +++ b/src/selection.h @@ -1,68 +1,78 @@ +/*************************************************************************** + * Copyright (C) 2003-2011 by Joachim Eibl * + * joachim.eibl at gmx.de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + #include "gnudiff_diff.h" #include "common.h" class Selection { public: Selection(){} private: const LineRef invalidRef = -1; LineRef firstLine = invalidRef; LineRef lastLine = invalidRef; int firstPos = -1; int lastPos = -1; LineRef oldFirstLine = invalidRef; LineRef oldLastLine = invalidRef; public: //private: bool bSelectionContainsData = false; public: inline LineRef getFirstLine() { return firstLine; }; inline LineRef getLastLine() { return lastLine; }; inline int getFirstPos() { return firstPos; }; inline int getLastPos() { return lastPos; }; inline bool isValidFirstLine() { return firstLine != invalidRef; } inline void clearOldSelection() { oldLastLine = invalidRef, oldFirstLine = invalidRef; }; inline LineRef getOldLastLine() { return oldLastLine; }; inline LineRef getOldFirstLine() { return oldFirstLine; }; inline bool selectionContainsData(void) { return bSelectionContainsData; }; bool isEmpty() { return firstLine == invalidRef || (firstLine == lastLine && firstPos == lastPos) || bSelectionContainsData == false; } void reset() { oldLastLine = lastLine; oldFirstLine = firstLine; firstLine = invalidRef; lastLine = invalidRef; bSelectionContainsData = false; } void start( LineRef l, int p ) { firstLine = l; firstPos = p; } void end( LineRef l, int p ) { if ( oldLastLine == invalidRef ) oldLastLine = lastLine; lastLine = l; lastPos = p; //bSelectionContainsData = (firstLine == lastLine && firstPos == lastPos); } bool within( LineRef l, LineRef p ); bool lineWithin( LineRef l ); int firstPosInLine(LineRef l); int lastPosInLine(LineRef l); LineRef beginLine(){ if (firstLine<0 && lastLine<0) return invalidRef; return max2((LineRef)0,min2(firstLine,lastLine)); } LineRef endLine(){ if (firstLine<0 && lastLine<0) return invalidRef; return max2(firstLine,lastLine); } int beginPos() { return firstLine==lastLine ? min2(firstPos,lastPos) : firstLine