katecursor.h

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2002 Christian Couder <christian@kdevelop.org>
00003    Copyright (C) 2001, 2003 Christoph Cullmann <cullmann@kde.org>
00004    Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
00005    Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License version 2 as published by the Free Software Foundation.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019    Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #ifndef kate_cursor_h
00023 #define kate_cursor_h
00024 
00025 #include "../interfaces/document.h"
00026 
00027 class KateDocument;
00028 
00032 class KateTextCursor
00033 {
00034   public:
00035     KateTextCursor() : m_line(0), m_col(0) {};
00036     KateTextCursor(int line, int col) : m_line(line), m_col(col) {};
00037     virtual ~KateTextCursor () {};
00038 
00039     friend bool operator==(const KateTextCursor& c1, const KateTextCursor& c2)
00040       { return c1.m_line == c2.m_line && c1.m_col == c2.m_col; }
00041 
00042     friend bool operator!=(const KateTextCursor& c1, const KateTextCursor& c2)
00043       { return !(c1 == c2); }
00044 
00045     friend bool operator>(const KateTextCursor& c1, const KateTextCursor& c2)
00046       { return c1.m_line > c2.m_line || (c1.m_line == c2.m_line && c1.m_col > c2.m_col); }
00047 
00048     friend bool operator>=(const KateTextCursor& c1, const KateTextCursor& c2)
00049       { return c1.m_line > c2.m_line || (c1.m_line == c2.m_line && c1.m_col >= c2.m_col); }
00050 
00051     friend bool operator<(const KateTextCursor& c1, const KateTextCursor& c2)
00052       { return !(c1 >= c2); }
00053 
00054     friend bool operator<=(const KateTextCursor& c1, const KateTextCursor& c2)
00055       { return !(c1 > c2); }
00056       
00057 #ifndef Q_WS_WIN //not needed
00058     friend void qSwap(KateTextCursor & c1, KateTextCursor & c2) {
00059       KateTextCursor tmp = c1;
00060       c1 = c2;
00061       c2 = tmp;
00062     }
00063 #endif
00064 
00065     inline void pos(int *pline, int *pcol) const {
00066       if(pline) *pline = m_line;
00067       if(pcol) *pcol = m_col;
00068     }
00069 
00070     inline int line() const { return m_line; };
00071     inline int col() const { return m_col; };
00072 
00073     virtual void setLine(int line) { m_line = line; };
00074     virtual void setCol(int col) { m_col = col; };
00075     virtual void setPos(const KateTextCursor& pos) { m_line = pos.line(); m_col = pos.col(); };
00076     virtual void setPos(int line, int col) { m_line = line; m_col = col; };
00077 
00078   protected:
00079     int m_line;
00080     int m_col;
00081 };
00082 
00086 class KateDocCursor : public KateTextCursor
00087 {
00088   public:
00089     KateDocCursor(KateDocument *doc);
00090     KateDocCursor(int line, int col, KateDocument *doc);
00091     virtual ~KateDocCursor() {};
00092 
00093     bool validPosition(uint line, uint col);
00094     bool validPosition();
00095 
00096     bool gotoNextLine();
00097     bool gotoPreviousLine();
00098     bool gotoEndOfNextLine();
00099     bool gotoEndOfPreviousLine();
00100 
00101     int nbCharsOnLineAfter();
00102     bool moveForward(uint nbChar);
00103     bool moveBackward(uint nbChar);
00104 
00105     // KTextEditor::Cursor interface
00106     void position(uint *line, uint *col) const;
00107     bool setPosition(uint line, uint col);
00108     bool insertText(const QString& text);
00109     bool removeText(uint numberOfCharacters);
00110     QChar currentChar() const;
00111 
00112     uchar currentAttrib() const;
00113 
00122     bool nextNonSpaceChar();
00123 
00132     bool previousNonSpaceChar();
00133 
00134   protected:
00135     KateDocument *m_doc;
00136 };
00137 
00138 class KateRange
00139 {
00140   public:
00141     KateRange () {};
00142     virtual ~KateRange () {};
00143 
00144     virtual bool isValid() const = 0;
00145     virtual KateTextCursor& start() = 0;
00146     virtual KateTextCursor& end() = 0;
00147     virtual const KateTextCursor& start() const = 0;
00148     virtual const KateTextCursor& end() const = 0;
00149 };
00150 
00151 class KateTextRange : public KateRange
00152 {
00153   public:
00154     KateTextRange()
00155       : m_valid(false)
00156     {
00157     };
00158 
00159     KateTextRange(int startline, int startcol, int endline, int endcol)
00160       : m_start(startline, startcol)
00161       , m_end(endline, endcol)
00162       , m_valid(true)
00163     {
00164       normalize();
00165     };
00166 
00167     KateTextRange(const KateTextCursor& start, const KateTextCursor& end)
00168       : m_start(start)
00169       , m_end(end)
00170       , m_valid(true)
00171     {
00172       normalize();
00173     };
00174 
00175     virtual ~KateTextRange () {};
00176 
00177     virtual bool isValid() const { return m_valid; };
00178     void setValid(bool valid) { 
00179       m_valid = valid; 
00180       if( valid )
00181         normalize(); 
00182     };
00183 
00184     virtual KateTextCursor& start() { return m_start; };
00185     virtual KateTextCursor& end() { return m_end; };
00186     virtual const KateTextCursor& start() const { return m_start; };
00187     virtual const KateTextCursor& end() const { return m_end; };
00188     
00189     /* if range is not valid, the result is undefined
00190       if cursor is before start -1 is returned, if cursor is within range 0 is returned if cursor is after end 1 is returned*/
00191     inline int cursorInRange(const KateTextCursor &cursor) const {
00192       return ((cursor<m_start)?(-1):((cursor>m_end)?1:0));
00193     }
00194     
00195     inline void normalize() {
00196       if( m_start > m_end )
00197         qSwap(m_start, m_end);
00198     }
00199     
00200   protected:
00201     KateTextCursor m_start, m_end;
00202     bool m_valid;
00203 };
00204 
00205 
00206 class KateBracketRange : public KateTextRange
00207 {
00208   public:
00209     KateBracketRange()
00210       : KateTextRange()
00211       , m_minIndent(0)
00212     {
00213     };
00214     
00215     KateBracketRange(int startline, int startcol, int endline, int endcol, int minIndent)
00216       : KateTextRange(startline, startcol, endline, endcol)
00217       , m_minIndent(minIndent)
00218     {
00219     };
00220     
00221     KateBracketRange(const KateTextCursor& start, const KateTextCursor& end, int minIndent)
00222       : KateTextRange(start, end)
00223       , m_minIndent(minIndent)
00224     {
00225     };
00226     
00227     int getMinIndent() const
00228     {
00229       return m_minIndent;
00230     }
00231     
00232     void setIndentMin(int m)
00233     {
00234       m_minIndent = m;
00235     }
00236     
00237   protected:
00238     int m_minIndent;
00239 };
00240 
00241 
00242 #endif
00243 
00244 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE Home | KDE Accessibility Home | Description of Access Keys