KTextEditor
smartcursor.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "smartcursor.h" 00020 00021 #include "document.h" 00022 #include "smartrange.h" 00023 00024 using namespace KTextEditor; 00025 00026 SmartCursor::SmartCursor( const Cursor & position, Document * doc, InsertBehavior insertBehavior ) 00027 : Cursor(position) 00028 , m_doc(doc) 00029 , m_moveOnInsert(insertBehavior == MoveOnInsert) 00030 { 00031 Q_ASSERT(m_doc); 00032 } 00033 00034 SmartCursor::~ SmartCursor( ) 00035 { 00036 } 00037 00038 bool SmartCursor::isValid( ) const 00039 { 00040 return m_doc->cursorInText(*this); 00041 } 00042 00043 bool SmartCursor::atEndOfDocument( ) const 00044 { 00045 return *this >= m_doc->documentEnd(); 00046 } 00047 00048 bool SmartCursor::isSmartCursor( ) const 00049 { 00050 return true; 00051 } 00052 00053 bool SmartCursor::insertText( const QStringList & text, bool block ) 00054 { 00055 return document()->insertText(*this, text, block); 00056 } 00057 00058 QChar SmartCursor::character( ) const 00059 { 00060 return document()->character(*this); 00061 } 00062 00063 SmartRange * SmartCursor::smartRange( ) const 00064 { 00065 return static_cast<SmartRange*>(m_range); 00066 } 00067 00068 Document* SmartCursor::document() const 00069 { 00070 return m_doc; 00071 } 00072 00073 bool SmartCursor::atEndOfLine( ) const 00074 { 00075 return column() == m_doc->lineLength(line()); 00076 } 00077 00078 SmartCursor::InsertBehavior SmartCursor::insertBehavior( ) const 00079 { 00080 return m_moveOnInsert ? MoveOnInsert : StayOnInsert; 00081 } 00082 00083 void SmartCursor::setInsertBehavior( InsertBehavior insertBehavior ) 00084 { 00085 m_moveOnInsert = insertBehavior == MoveOnInsert; 00086 } 00087 00088 SmartCursor * SmartCursor::toSmartCursor( ) const 00089 { 00090 return const_cast<SmartCursor*>(this); 00091 } 00092 00093 bool SmartCursor::advance(int distance, AdvanceMode mode) 00094 { 00095 Cursor c = *this; 00096 if (mode == ByCharacter) { 00097 while (distance) { 00098 int lineLength = document()->lineLength(c.line()); 00099 00100 if (distance > 0) { 00101 int advance = qMin(lineLength - c.column(), distance); 00102 00103 if (distance > advance) { 00104 if (c.line() + 1 >= document()->lines()) 00105 return false; 00106 00107 c.setPosition(c.line() + 1, 0); 00108 // Account for end of line advancement 00109 distance -= advance + 1; 00110 00111 } else { 00112 c.setColumn(c.column() + distance); 00113 distance = 0; 00114 } 00115 00116 } else { 00117 int back = qMin(c.column(), -distance); 00118 if (-distance > back) { 00119 if (c.line() - 1 < 0) 00120 return false; 00121 00122 c.setPosition(c.line() - 1, document()->lineLength(c.line() - 1)); 00123 // Account for end of line advancement 00124 distance += back + 1; 00125 00126 } else { 00127 c.setColumn(c.column() + distance); 00128 distance = 0; 00129 } 00130 } 00131 } 00132 00133 } else { 00134 // Not supported by the interface alone 00135 return false; 00136 } 00137 00138 setPosition(c); 00139 return true; 00140 } 00141 00142 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference