KTextEditor

movingcursor.cpp
1 /* SPDX-License-Identifier: LGPL-2.0-or-later
2 
3  Copyright (C) 2010 Christoph Cullmann <[email protected]>
4  Copyright (C) 2010-2018 Dominik Haumann <[email protected]>
5 
6  Based on code of the SmartCursor/Range by:
7  Copyright (C) 2003-2005 Hamish Rodda <[email protected]>
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 #include "documentcursor.h"
28 
29 using namespace KTextEditor;
30 
32 {
33 }
34 
36 {
37 }
38 
40 {
41  // just use setPosition
42  setPosition(Cursor(line, column));
43 }
44 
46 {
47  // just use setPosition
48  setPosition(line, column());
49 }
50 
52 {
53  // just use setPosition
54  setPosition(line(), column);
55 }
56 
58 {
59  return isValidTextPosition() && column() == 0;
60 }
61 
63 {
64  return isValidTextPosition() && column() == document()->lineLength(line());
65 }
66 
68 {
69  return *this == document()->documentEnd();
70 }
71 
73 {
74  return line() == 0 && column() == 0;
75 }
76 
78 {
79  // only touch valid cursors
80  const bool ok = isValid() && (line() + 1 < document()->lines());
81 
82  if (ok) {
83  setPosition(Cursor(line() + 1, 0));
84  }
85 
86  return ok;
87 }
88 
90 {
91  // only touch valid cursors
92  bool ok = (line() > 0) && (column() >= 0);
93 
94  if (ok) {
95  setPosition(Cursor(line() - 1, 0));
96  }
97 
98  return ok;
99 }
100 
101 bool MovingCursor::move(int chars, WrapBehavior wrapBehavior)
102 {
104 
105  const bool success = dc.move(chars, static_cast<DocumentCursor::WrapBehavior>(wrapBehavior));
106 
107  if (success && dc.toCursor() != toCursor()) {
108  setPosition(dc.toCursor());
109  }
110 
111  return success;
112 }
virtual Document * document() const =0
Gets the document to which this cursor is bound.
bool atEndOfLine() const
Determine if this cursor is located at the end of the current line.
virtual int lines() const =0
Get the count of lines of the document.
A Cursor which is bound to a specific Document.
virtual int lineLength(int line) const =0
Get the length of a given line in characters.
The Cursor represents a position in a Document.
Definition: cursor.h:85
bool move(int chars, WrapBehavior wrapBehavior=Wrap)
Moves the cursor chars character forward or backwards.
virtual ~MovingCursor()
Destruct the moving cursor.
void setColumn(int column)
Set the cursor column to column.
bool gotoPreviousLine()
Moves the cursor to the previous line and sets the column to 0.
bool atEndOfDocument() const
Determine if this cursor is located at the end of the last line in the document.
bool isValidTextPosition() const
Check whether this MovingCursor is located at a valid text position.
Definition: movingcursor.h:191
virtual int column() const =0
Retrieve the column on which this cursor is situated.
bool atStartOfDocument() const
Determine if this cursor is located at line 0 and column 0.
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
MovingCursor()
For inherited class only.
virtual Cursor documentEnd() const =0
End position of the document.
const Cursor toCursor() const
Convert this clever cursor into a dumb one.
Definition: movingcursor.h:283
WrapBehavior
Wrap behavior for end of line treatement used in move().
Definition: movingcursor.h:86
bool isValid() const
Returns whether the current position of this cursor is a valid position, i.e.
Definition: movingcursor.h:173
bool atStartOfLine() const
Determine if this cursor is located at column 0 of a valid text line.
bool gotoNextLine()
Moves the cursor to the next line and sets the column to 0.
Cursor toCursor() const
Convert this clever cursor into a dumb one.
bool move(int chars, WrapBehavior wrapBehavior=Wrap)
Moves the cursor chars character forward or backwards.
void setLine(int line)
Set the cursor line to line.
virtual void setPosition(const KTextEditor::Cursor &position)=0
Set the current cursor position to position.
virtual int line() const =0
Retrieve the line on which this cursor is situated.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 22:55:05 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.