Kate
katetextcursor.cpp
Go to the documentation of this file.
00001 /* This file is part of the Kate project. 00002 * 00003 * Copyright (C) 2010 Christoph Cullmann <cullmann@kde.org> 00004 * 00005 * Based on code of the SmartCursor/Range by: 00006 * Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org> 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Library General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Library General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Library General Public License 00019 * along with this library; see the file COPYING.LIB. If not, write to 00020 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00021 * Boston, MA 02110-1301, USA. 00022 */ 00023 00024 #include "katetextcursor.h" 00025 #include "katetextbuffer.h" 00026 00027 namespace Kate { 00028 00029 TextCursor::TextCursor (TextBuffer &buffer, const KTextEditor::Cursor &position, InsertBehavior insertBehavior) 00030 : m_buffer (buffer) 00031 , m_range (0) 00032 , m_block (0) 00033 , m_line (-1) 00034 , m_column (-1) 00035 , m_moveOnInsert (insertBehavior == MoveOnInsert) 00036 { 00037 // init position 00038 setPosition (position, true); 00039 } 00040 00041 TextCursor::TextCursor (TextBuffer &buffer, TextRange *range, const KTextEditor::Cursor &position, InsertBehavior insertBehavior) 00042 : m_buffer (buffer) 00043 , m_range (range) 00044 , m_block (0) 00045 , m_line (-1) 00046 , m_column (-1) 00047 , m_moveOnInsert (insertBehavior == MoveOnInsert) 00048 { 00049 // init position 00050 setPosition (position, true); 00051 } 00052 00053 TextCursor::~TextCursor () 00054 { 00055 // remove cursor from block or buffer 00056 if (m_block) 00057 m_block->m_cursors.remove (this); 00058 00059 // only cursors without range are here! 00060 else if (!m_range) 00061 m_buffer.m_invalidCursors.remove (this); 00062 } 00063 00064 void TextCursor::setPosition( const TextCursor& position ) 00065 { 00066 if (m_block && m_block != position.m_block) 00067 m_block->m_cursors.remove(this); 00068 00069 m_line = position.m_line; 00070 m_column = position.m_column; 00071 00072 m_block = position.m_block; 00073 if (m_block) 00074 m_block->m_cursors.insert(this); 00075 } 00076 00077 void TextCursor::setPosition(const KTextEditor::Cursor& position, bool init) 00078 { 00079 // any change or init? else do nothing 00080 if (!init && position.line() == line() && position.column() == m_column) 00081 return; 00082 00083 // remove cursor from old block in any case 00084 if (m_block) 00085 m_block->m_cursors.remove (this); 00086 00087 // first: validate the line and column, else invalid 00088 if (position.column() < 0 || position.line () < 0 || position.line () >= m_buffer.lines ()) { 00089 if (!m_range) 00090 m_buffer.m_invalidCursors.insert (this); 00091 m_block = 0; 00092 m_line = m_column = -1; 00093 return; 00094 } 00095 00096 // else, find block 00097 TextBlock *block = m_buffer.blockForIndex (m_buffer.blockForLine (position.line())); 00098 00099 // get line 00100 TextLine textLine = block->line (position.line()); 00101 00102 #if 0 // this is no good idea, smart cursors don't do that, too, for non-wrapping cursors 00103 // now, validate column, else stay invalid 00104 if (position.column() > textLine->text().size()) { 00105 if (!m_range) 00106 m_buffer.m_invalidCursors.insert (this); 00107 m_block = 0; 00108 m_line = m_column = -1; 00109 return; 00110 } 00111 #endif 00112 00113 // else: valid cursor 00114 m_block = block; 00115 m_line = position.line () - m_block->startLine (); 00116 m_column = position.column (); 00117 m_block->m_cursors.insert (this); 00118 } 00119 00120 void TextCursor::setPosition(const KTextEditor::Cursor& position) 00121 { 00122 setPosition(position, false); 00123 } 00124 00125 int TextCursor::line() const 00126 { 00127 // invalid cursor have no block 00128 if (!m_block) 00129 return -1; 00130 00131 // else, calculate real line 00132 return m_block->startLine () + m_line; 00133 } 00134 00135 KTextEditor::Document *Kate::TextCursor::document () const 00136 { 00137 return m_buffer.document(); 00138 } 00139 00140 KTextEditor::MovingRange *Kate::TextCursor::range () const 00141 { 00142 return m_range; 00143 } 00144 00145 }
KDE 4.6 API Reference