• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

KTextEditor

  • kde-4.14
  • applications
  • kate
  • ktexteditor
movingcursor.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  *
3  * Copyright (C) 2010 Christoph Cullmann <cullmann@kde.org>
4  * Copyright (C) 2010 Dominik Haumann <dhaumann kde org>
5  *
6  * Based on code of the SmartCursor/Range by:
7  * Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this library; see the file COPYING.LIB. If not, write to
21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 
25 #include "movingcursor.h"
26 #include "document.h"
27 
28 using namespace KTextEditor;
29 
30 MovingCursor::MovingCursor ()
31 {
32 }
33 
34 MovingCursor::~MovingCursor ()
35 {
36 }
37 
38 void MovingCursor::setPosition(int line, int column)
39 {
40  // just use setPosition
41  setPosition(Cursor(line, column));
42 }
43 
44 void MovingCursor::setLine (int line)
45 {
46  // just use setPosition
47  setPosition (line, column());
48 }
49 
50 void MovingCursor::setColumn (int column)
51 {
52  // just use setPosition
53  setPosition (line(), column);
54 }
55 
56 bool MovingCursor::atStartOfLine() const {
57  return isValidTextPosition() && column() == 0;
58 }
59 
60 bool MovingCursor::atEndOfLine() const {
61  return isValidTextPosition() && column() == document()->lineLength(line());
62 }
63 
64 bool MovingCursor::atEndOfDocument() const {
65  return *this == document()->documentEnd();
66 }
67 
68 bool MovingCursor::atStartOfDocument() const {
69  return line() == 0 && column() == 0;
70 }
71 
72 bool MovingCursor::gotoNextLine()
73 {
74  // only touch valid cursors
75  const bool ok = isValid() && (line() + 1 < document()->lines());
76 
77  if (ok) {
78  setPosition(Cursor(line() + 1, 0));
79  }
80 
81  return ok;
82 }
83 
84 bool MovingCursor::gotoPreviousLine()
85 {
86  // only touch valid cursors
87  bool ok = (line() > 0) && (column() >= 0);
88 
89  if (ok) {
90  setPosition(Cursor(line() - 1, 0));
91  }
92 
93  return ok;
94 }
95 
96 bool MovingCursor::move(int chars, WrapBehavior wrapBehavior)
97 {
98  if (!isValid()) {
99  return false;
100  }
101 
102  Cursor c(toCursor());
103 
104  // special case: cursor position is not in valid text, then the algo does
105  // not work for Wrap mode. Hence, catch this special case by setting
106  // c.column() to the lineLength()
107  if (chars > 0 && wrapBehavior == Wrap && c.column() > document()->lineLength(c.line())) {
108  c.setColumn(document()->lineLength(c.line()));
109  }
110 
111  while (chars != 0) {
112  if (chars > 0) {
113  if (wrapBehavior == Wrap) {
114  int advance = qMin(document()->lineLength(c.line()) - c.column(), chars);
115 
116  if (chars > advance) {
117  if (c.line() + 1 >= document()->lines()) {
118  return false;
119  }
120 
121  c.setPosition(c.line() + 1, 0);
122  chars -= advance + 1; // +1 because of end-of-line wrap
123  } else {
124  c.setColumn(c.column() + chars);
125  chars = 0;
126  }
127  } else { // NoWrap
128  c.setColumn(c.column() + chars);
129  chars = 0;
130  }
131  } else {
132  int back = qMin(c.column(), -chars);
133  if (-chars > back) {
134  if (c.line() == 0)
135  return false;
136 
137  c.setPosition(c.line() - 1, document()->lineLength(c.line() - 1));
138  chars += back + 1; // +1 because of wrap-around at start-of-line
139  } else {
140  c.setColumn(c.column() + chars);
141  chars = 0;
142  }
143  }
144  }
145 
146  if (c != *this) {
147  setPosition(c);
148  }
149  return true;
150 }
151 
152 // kate: space-indent on; indent-width 2; replace-tabs on;
KTextEditor::MovingCursor::column
virtual int column() const =0
Retrieve the column on which this cursor is situated.
KTextEditor::MovingCursor::atEndOfLine
bool atEndOfLine() const
Determine if this cursor is located at the end of the current line.
Definition: movingcursor.cpp:60
KTextEditor::Document::documentEnd
virtual Cursor documentEnd() const =0
End position of the document.
KTextEditor::Cursor
An object which represents a position in a Document.
Definition: cursor.h:55
KTextEditor::MovingCursor::~MovingCursor
virtual ~MovingCursor()
Destruct the moving cursor.
Definition: movingcursor.cpp:34
KTextEditor::MovingCursor::setColumn
void setColumn(int column)
Set the cursor column to column.
Definition: movingcursor.cpp:50
KTextEditor::MovingCursor::gotoPreviousLine
bool gotoPreviousLine()
Moves the cursor to the previous line and sets the column to 0.
Definition: movingcursor.cpp:84
KTextEditor::MovingCursor::atEndOfDocument
bool atEndOfDocument() const
Determine if this cursor is located at the end of the last line in the document.
Definition: movingcursor.cpp:64
KTextEditor::MovingCursor::Wrap
wrap at end of line
Definition: movingcursor.h:85
KTextEditor::MovingCursor::isValidTextPosition
bool isValidTextPosition() const
Check whether the current position of this cursor is a valid text position.
Definition: movingcursor.h:181
KTextEditor::MovingCursor::atStartOfDocument
bool atStartOfDocument() const
Determine if this cursor is located at line 0 and column 0.
Definition: movingcursor.cpp:68
document.h
KTextEditor::MovingCursor::MovingCursor
MovingCursor()
For inherited class only.
Definition: movingcursor.cpp:30
KTextEditor::MovingCursor::toCursor
const Cursor toCursor() const
Convert this clever cursor into a dumb one.
Definition: movingcursor.h:272
KTextEditor::MovingCursor::WrapBehavior
WrapBehavior
Wrap behavior for end of line treatement used in move().
Definition: movingcursor.h:84
KTextEditor::MovingCursor::isValid
bool isValid() const
Returns whether the current position of this cursor is a valid position, i.e.
Definition: movingcursor.h:171
KTextEditor::Cursor::line
virtual int line() const
Retrieve the line on which this cursor is situated.
Definition: cursor.cpp:62
KTextEditor::MovingCursor::line
virtual int line() const =0
Retrieve the line on which this cursor is situated.
KTextEditor::MovingCursor::atStartOfLine
bool atStartOfLine() const
Determine if this cursor is located at column 0 of a valid text line.
Definition: movingcursor.cpp:56
KTextEditor::Cursor::setPosition
virtual void setPosition(const Cursor &position)
Set the current cursor position to position.
Definition: cursor.cpp:96
KTextEditor::MovingCursor::gotoNextLine
bool gotoNextLine()
Moves the cursor to the next line and sets the column to 0.
Definition: movingcursor.cpp:72
movingcursor.h
KTextEditor::Document::lineLength
virtual int lineLength(int line) const =0
Get the length of a given line in characters.
KTextEditor::MovingCursor::move
bool move(int chars, WrapBehavior wrapBehavior=Wrap)
Moves the cursor chars character forward or backwards.
Definition: movingcursor.cpp:96
KTextEditor::MovingCursor::setLine
void setLine(int line)
Set the cursor line to line.
Definition: movingcursor.cpp:44
KTextEditor::Cursor::setColumn
virtual void setColumn(int column)
Set the cursor column to column.
Definition: cursor.cpp:84
KTextEditor::Cursor::column
int column() const
Retrieve the column on which this cursor is situated.
Definition: cursor.cpp:79
KTextEditor::MovingCursor::setPosition
virtual void setPosition(const KTextEditor::Cursor &position)=0
Set the current cursor position to position.
KTextEditor::MovingCursor::document
virtual Document * document() const =0
Gets the document to which this cursor is bound.
KTextEditor::Document::lines
virtual int lines() const =0
Get the count of lines of the document.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:48 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KTextEditor

Skip menu "KTextEditor"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal