KTextEditor
movingcursor.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE project 00002 * 00003 * Copyright (C) 2010 Christoph Cullmann <cullmann@kde.org> 00004 * Copyright (C) 2010 Dominik Haumann <dhaumann kde org> 00005 * 00006 * Based on code of the SmartCursor/Range by: 00007 * Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org> 00008 * 00009 * This library is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Library General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2 of the License, or (at your option) any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Library General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Library General Public License 00020 * along with this library; see the file COPYING.LIB. If not, write to 00021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00022 * Boston, MA 02110-1301, USA. 00023 */ 00024 00025 #include "movingcursor.h" 00026 #include "document.h" 00027 00028 using namespace KTextEditor; 00029 00030 MovingCursor::MovingCursor () 00031 { 00032 } 00033 00034 MovingCursor::~MovingCursor () 00035 { 00036 } 00037 00038 void MovingCursor::setPosition(int line, int column) 00039 { 00040 // just use setPosition 00041 setPosition(Cursor(line, column)); 00042 } 00043 00044 void MovingCursor::setLine (int line) 00045 { 00046 // just use setPosition 00047 setPosition (line, column()); 00048 } 00049 00050 void MovingCursor::setColumn (int column) 00051 { 00052 // just use setPosition 00053 setPosition (line(), column); 00054 } 00055 00056 bool MovingCursor::atStartOfLine() const { 00057 return isValidTextPosition() && column() == 0; 00058 } 00059 00060 bool MovingCursor::atEndOfLine() const { 00061 return isValidTextPosition() && column() == document()->lineLength(line()); 00062 } 00063 00064 bool MovingCursor::atEndOfDocument() const { 00065 return *this == document()->documentEnd(); 00066 } 00067 00068 bool MovingCursor::atStartOfDocument() const { 00069 return line() == 0 && column() == 0; 00070 } 00071 00072 bool MovingCursor::gotoNextLine() 00073 { 00074 // only touch valid cursors 00075 const bool ok = isValid() && (line() + 1 < document()->lines()); 00076 00077 if (ok) { 00078 setPosition(Cursor(line() + 1, 0)); 00079 } 00080 00081 return ok; 00082 } 00083 00084 bool MovingCursor::gotoPreviousLine() 00085 { 00086 // only touch valid cursors 00087 bool ok = (line() > 0) && (column() >= 0); 00088 00089 if (ok) { 00090 setPosition(Cursor(line() - 1, 0)); 00091 } 00092 00093 return ok; 00094 } 00095 00096 bool MovingCursor::move(int chars, WrapBehavior wrapBehavior) 00097 { 00098 if (!isValid()) { 00099 return false; 00100 } 00101 00102 Cursor c(toCursor()); 00103 00104 // special case: cursor position is not in valid text, then the algo does 00105 // not work for Wrap mode. Hence, catch this special case by setting 00106 // c.column() to the lineLength() 00107 if (chars > 0 && wrapBehavior == Wrap && c.column() > document()->lineLength(c.line())) { 00108 c.setColumn(document()->lineLength(c.line())); 00109 } 00110 00111 while (chars != 0) { 00112 if (chars > 0) { 00113 if (wrapBehavior == Wrap) { 00114 int advance = qMin(document()->lineLength(c.line()) - c.column(), chars); 00115 00116 if (chars > advance) { 00117 if (c.line() + 1 >= document()->lines()) { 00118 return false; 00119 } 00120 00121 c.setPosition(c.line() + 1, 0); 00122 chars -= advance + 1; // +1 because of end-of-line wrap 00123 } else { 00124 c.setColumn(c.column() + chars); 00125 chars = 0; 00126 } 00127 } else { // NoWrap 00128 c.setColumn(c.column() + chars); 00129 chars = 0; 00130 } 00131 } else { 00132 int back = qMin(c.column(), -chars); 00133 if (-chars > back) { 00134 if (c.line() == 0) 00135 return false; 00136 00137 c.setPosition(c.line() - 1, document()->lineLength(c.line() - 1)); 00138 chars += back + 1; // +1 because of wrap-around at start-of-line 00139 } else { 00140 c.setColumn(c.column() + chars); 00141 chars = 0; 00142 } 00143 } 00144 } 00145 00146 if (c != *this) { 00147 setPosition(c); 00148 } 00149 return true; 00150 } 00151 00152 // kate: space-indent on; indent-width 2; replace-tabs on;
KDE 4.6 API Reference