• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KTextEditor

  • interfaces
  • 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;
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Jul 23 2013 20:49:21 by doxygen 1.8.1.1 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

kdelibs-4.10.5 API Reference

Skip menu "kdelibs-4.10.5 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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