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

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • buffer
katetextcursor.cpp
Go to the documentation of this file.
1 /* This file is part of the Kate project.
2  *
3  * Copyright (C) 2010 Christoph Cullmann <cullmann@kde.org>
4  *
5  * Based on code of the SmartCursor/Range by:
6  * Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB. If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "katetextcursor.h"
25 #include "katetextbuffer.h"
26 
27 namespace Kate {
28 
29 TextCursor::TextCursor (TextBuffer &buffer, const KTextEditor::Cursor &position, InsertBehavior insertBehavior)
30  : m_buffer (buffer)
31  , m_range (0)
32  , m_block (0)
33  , m_line (-1)
34  , m_column (-1)
35  , m_moveOnInsert (insertBehavior == MoveOnInsert)
36 {
37  // init position
38  setPosition (position, true);
39 }
40 
41 TextCursor::TextCursor (TextBuffer &buffer, TextRange *range, const KTextEditor::Cursor &position, InsertBehavior insertBehavior)
42  : m_buffer (buffer)
43  , m_range (range)
44  , m_block (0)
45  , m_line (-1)
46  , m_column (-1)
47  , m_moveOnInsert (insertBehavior == MoveOnInsert)
48 {
49  // init position
50  setPosition (position, true);
51 }
52 
53 TextCursor::~TextCursor ()
54 {
55  // remove cursor from block or buffer
56  if (m_block)
57  m_block->removeCursor (this);
58 
59  // only cursors without range are here!
60  else if (!m_range)
61  m_buffer.m_invalidCursors.remove (this);
62 }
63 
64 void TextCursor::setPosition( const TextCursor& position )
65 {
66  if (m_block && m_block != position.m_block)
67  m_block->removeCursor (this);
68 
69  m_line = position.m_line;
70  m_column = position.m_column;
71 
72  m_block = position.m_block;
73  if (m_block)
74  m_block->insertCursor (this);
75 }
76 
77 void TextCursor::setPosition(const KTextEditor::Cursor& position, bool init)
78 {
79  // any change or init? else do nothing
80  if (!init && position.line() == line() && position.column() == m_column)
81  return;
82 
83  // remove cursor from old block in any case
84  if (m_block)
85  m_block->removeCursor (this);
86 
87  // first: validate the line and column, else invalid
88  if (position.column() < 0 || position.line () < 0 || position.line () >= m_buffer.lines ()) {
89  if (!m_range)
90  m_buffer.m_invalidCursors.insert (this);
91  m_block = 0;
92  m_line = m_column = -1;
93  return;
94  }
95 
96  // else, find block
97  TextBlock *block = m_buffer.blockForIndex (m_buffer.blockForLine (position.line()));
98  Q_ASSERT(block);
99 
100  // get line
101  TextLine textLine = block->line (position.line());
102 
103 #if 0 // this is no good idea, smart cursors don't do that, too, for non-wrapping cursors
104  // now, validate column, else stay invalid
105  if (position.column() > textLine->text().size()) {
106  if (!m_range)
107  m_buffer.m_invalidCursors.insert (this);
108  m_block = 0;
109  m_line = m_column = -1;
110  return;
111  }
112 #endif
113 
114  // if cursor was invalid before, remove it from invalid cursor list
115  if (!m_range && !m_block && !init) {
116  Q_ASSERT(m_buffer.m_invalidCursors.contains (this));
117  m_buffer.m_invalidCursors.remove (this);
118  }
119 
120  // else: valid cursor
121  m_block = block;
122  m_line = position.line () - m_block->startLine ();
123  m_column = position.column ();
124  m_block->insertCursor (this);
125 }
126 
127 void TextCursor::setPosition(const KTextEditor::Cursor& position)
128 {
129  setPosition(position, false);
130 }
131 
132 int TextCursor::line() const
133 {
134  // invalid cursor have no block
135  if (!m_block)
136  return -1;
137 
138  // else, calculate real line
139  return m_block->startLine () + m_line;
140 }
141 
142 KTextEditor::Document *Kate::TextCursor::document () const
143 {
144  return m_buffer.document();
145 }
146 
147 KTextEditor::MovingRange *Kate::TextCursor::range () const
148 {
149  return m_range;
150 }
151 
152 }
Kate::TextBlock::removeCursor
void removeCursor(Kate::TextCursor *cursor)
Remove cursor from this block.
Definition: katetextblock.h:194
katetextbuffer.h
Kate::TextCursor::block
TextBlock * block() const
Get block this cursor belongs to, if any.
Definition: katetextcursor.h:165
Kate::TextCursor::TextBlock
friend class TextBlock
Definition: katetextcursor.h:48
Kate::TextBlock::startLine
int startLine() const
Start line of this block.
Definition: katetextblock.h:60
Kate::TextCursor::setPosition
void setPosition(const TextCursor &position)
Fast way to set the current cursor position to position.
Definition: katetextcursor.cpp:64
Kate::TextCursor::~TextCursor
~TextCursor()
Destruct the text cursor.
Definition: katetextcursor.cpp:53
Kate::TextBlock::insertCursor
void insertCursor(Kate::TextCursor *cursor)
Insert cursor into this block.
Definition: katetextblock.h:188
Kate::TextCursor::line
int line() const
Retrieve the line on which this cursor is situated.
Definition: katetextcursor.cpp:132
Kate::TextBuffer::lines
int lines() const
Lines currently stored in this buffer.
Definition: katetextbuffer.h:189
Kate::TextCursor::range
KTextEditor::MovingRange * range() const
Get range this cursor belongs to, if any.
Definition: katetextcursor.cpp:147
Kate::TextCursor
Class representing a 'clever' text cursor.
Definition: katetextcursor.h:43
Kate::TextLine
QSharedPointer< TextLineData > TextLine
The normal world only accesses the text lines with shared pointers.
Definition: katetextline.h:443
Kate::TextBuffer
Class representing a text buffer.
Definition: katetextbuffer.h:48
Kate::TextCursor::document
KTextEditor::Document * document() const
Gets the document to which this cursor is bound.
Definition: katetextcursor.cpp:142
katetextcursor.h
Kate::TextRange
Class representing a 'clever' text range.
Definition: katetextrange.h:46
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • 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