Kate
katecursor.h
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 #ifndef kate_cursor_h
00023 #define kate_cursor_h
00024 
00025 #include "../interfaces/document.h"
00026 #include <kdebug.h>
00027 
00028 class KateDocument;
00029 
00033 class KateTextCursor
00034 {
00035   public:
00036     KateTextCursor() : m_line(0), m_col(0) {};
00037     KateTextCursor(int line, int col) : m_line(line), m_col(col) {};
00038     virtual ~KateTextCursor () {};
00039 
00040     friend bool operator==(const KateTextCursor& c1, const KateTextCursor& c2)
00041       { return c1.m_line == c2.m_line && c1.m_col == c2.m_col; }
00042 
00043     friend bool operator!=(const KateTextCursor& c1, const KateTextCursor& c2)
00044       { return !(c1 == c2); }
00045 
00046     friend bool operator>(const KateTextCursor& c1, const KateTextCursor& c2)
00047       { return c1.m_line > c2.m_line || (c1.m_line == c2.m_line && c1.m_col > c2.m_col); }
00048 
00049     friend bool operator>=(const KateTextCursor& c1, const KateTextCursor& c2)
00050       { return c1.m_line > c2.m_line || (c1.m_line == c2.m_line && c1.m_col >= c2.m_col); }
00051 
00052     friend bool operator<(const KateTextCursor& c1, const KateTextCursor& c2)
00053       { return !(c1 >= c2); }
00054 
00055     friend bool operator<=(const KateTextCursor& c1, const KateTextCursor& c2)
00056       { return !(c1 > c2); }
00057 
00058     friend kdbgstream& operator<<(kdbgstream& stream, const KateTextCursor& c) {
00059       stream << c.m_line << "," << c.m_col;
00060       return stream;
00061     }
00062 
00063 #ifndef Q_WS_WIN //not needed
00064     friend void qSwap(KateTextCursor & c1, KateTextCursor & c2) {
00065       KateTextCursor tmp = c1;
00066       c1 = c2;
00067       c2 = tmp;
00068     }
00069 #endif
00070 
00071     inline void pos(int *pline, int *pcol) const {
00072       if(pline) *pline = m_line;
00073       if(pcol) *pcol = m_col;
00074     }
00075 
00076     inline int line() const { return m_line; };
00077     inline int col() const { return m_col; };
00078 
00079     virtual void setLine(int line) { m_line = line; };
00080     virtual void setCol(int col) { m_col = col; };
00081     virtual void setPos(const KateTextCursor& pos) { m_line = pos.line(); m_col = pos.col(); };
00082     virtual void setPos(int line, int col) { m_line = line; m_col = col; };
00083 
00084   protected:
00085     int m_line;
00086     int m_col;
00087 };
00088 
00092 class KateDocCursor : public KateTextCursor
00093 {
00094   public:
00095     KateDocCursor(KateDocument *doc);
00096     KateDocCursor(int line, int col, KateDocument *doc);
00097     virtual ~KateDocCursor() {};
00098 
00099     bool validPosition(uint line, uint col);
00100     bool validPosition();
00101 
00102     bool gotoNextLine();
00103     bool gotoPreviousLine();
00104     bool gotoEndOfNextLine();
00105     bool gotoEndOfPreviousLine();
00106 
00107     int nbCharsOnLineAfter();
00108     bool moveForward(uint nbChar);
00109     bool moveBackward(uint nbChar);
00110 
00111     
00112     void position(uint *line, uint *col) const;
00113     bool setPosition(uint line, uint col);
00114     bool insertText(const QString& text);
00115     bool removeText(uint numberOfCharacters);
00116     QChar currentChar() const;
00117 
00118     uchar currentAttrib() const;
00119 
00128     bool nextNonSpaceChar();
00129 
00138     bool previousNonSpaceChar();
00139 
00140   protected:
00141     KateDocument *m_doc;
00142 };
00143 
00144 class KateRange
00145 {
00146   public:
00147     KateRange () {};
00148     virtual ~KateRange () {};
00149 
00150     virtual bool isValid() const = 0;
00151     virtual KateTextCursor& start() = 0;
00152     virtual KateTextCursor& end() = 0;
00153     virtual const KateTextCursor& start() const = 0;
00154     virtual const KateTextCursor& end() const = 0;
00155 };
00156 
00157 class KateTextRange : public KateRange
00158 {
00159   public:
00160     KateTextRange()
00161       : m_valid(false)
00162     {
00163     };
00164 
00165     KateTextRange(int startline, int startcol, int endline, int endcol)
00166       : m_start(startline, startcol)
00167       , m_end(endline, endcol)
00168       , m_valid(true)
00169     {
00170       normalize();
00171     };
00172 
00173     KateTextRange(const KateTextCursor& start, const KateTextCursor& end)
00174       : m_start(start)
00175       , m_end(end)
00176       , m_valid(true)
00177     {
00178       normalize();
00179     };
00180 
00181     virtual ~KateTextRange () {};
00182 
00183     virtual bool isValid() const { return m_valid; };
00184     void setValid(bool valid) { 
00185       m_valid = valid; 
00186       if( valid )
00187         normalize(); 
00188     };
00189 
00190     virtual KateTextCursor& start() { return m_start; };
00191     virtual KateTextCursor& end() { return m_end; };
00192     virtual const KateTextCursor& start() const { return m_start; };
00193     virtual const KateTextCursor& end() const { return m_end; };
00194     
00195     
00196 
00197     inline int cursorInRange(const KateTextCursor &cursor) const {
00198       return ((cursor<m_start)?(-1):((cursor>m_end)?1:0));
00199     }
00200     
00201     inline void normalize() {
00202       if( m_start > m_end )
00203         qSwap(m_start, m_end);
00204     }
00205     
00206   protected:
00207     KateTextCursor m_start, m_end;
00208     bool m_valid;
00209 };
00210 
00211 
00212 class KateBracketRange : public KateTextRange
00213 {
00214   public:
00215     KateBracketRange()
00216       : KateTextRange()
00217       , m_minIndent(0)
00218     {
00219     };
00220     
00221     KateBracketRange(int startline, int startcol, int endline, int endcol, int minIndent)
00222       : KateTextRange(startline, startcol, endline, endcol)
00223       , m_minIndent(minIndent)
00224     {
00225     };
00226     
00227     KateBracketRange(const KateTextCursor& start, const KateTextCursor& end, int minIndent)
00228       : KateTextRange(start, end)
00229       , m_minIndent(minIndent)
00230     {
00231     };
00232     
00233     int getMinIndent() const
00234     {
00235       return m_minIndent;
00236     }
00237     
00238     void setIndentMin(int m)
00239     {
00240       m_minIndent = m;
00241     }
00242     
00243   protected:
00244     int m_minIndent;
00245 };
00246 
00247 
00248 #endif
00249 
00250