1 /****************************************************************************
2 **
3 ** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
4 **
5 ** This file is part of the Edyuk project <http://edyuk.org>
6 **
7 ** This file may be used under the terms of the GNU General Public License
8 ** version 3 as published by the Free Software Foundation and appearing in the
9 ** file GPL.txt included in the packaging of this file.
10 **
11 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 **
14 ****************************************************************************/
15 
16 #ifndef Header_QDocument_Cursor
17 #define Header_QDocument_Cursor
18 
19 #include "qce-config.h"
20 
21 #include "modifiedQObject.h"
22 #include <QMetaType>
23 #include <QPoint>
24 
25 /*!
26 	\file qdocumentcursor.h
27 	\brief Definition of the QDocumentCursor class
28 */
29 
30 class QChar;
31 class QPoint;
32 class QString;
33 class QPolygon;
34 
35 class QString;
36 class QChar;
37 
38 class QDocument;
39 class QDocumentLine;
40 struct QDocumentSelection;
41 class QDocumentCursorHandle;
42 
43 class QCE_EXPORT QDocumentCursor : public QObject
44 {
45         Q_OBJECT
46 		Q_ENUMS(MoveOperation)
47 		Q_ENUMS(MoveMode MoveFlag)
48 		Q_ENUMS(SelectionType)
49 	public:
50 		enum MoveFlag
51 		{
52 			MoveAnchor	= 0,
53 			KeepAnchor	= 1,
54 			ThroughWrap	= 2,
55 			ThroughFolding  = 4
56 		};
57 
58         Q_DECLARE_FLAGS(MoveMode, MoveFlag)
59 
60 		enum MoveOperation
61 		{
62 			NoMove,
63 			Up,
64 			Down,
65 			Left,
66 			PreviousCharacter,
67 			Right,
68 			NextCharacter,
69 			Start,
70 			StartOfLine,
71 			StartOfBlock = StartOfLine,
72 			StartOfWord,
73 			StartOfWordOrCommand,
74 			StartOfParenthesis,
75 			PreviousBlock,
76 			PreviousLine = PreviousBlock,
77 			PreviousWord,
78 			WordLeft,
79 			WordRight,
80 			End,
81 			EndOfLine,
82 			EndOfBlock = EndOfLine,
83 			EndOfWord,
84 			EndOfWordOrCommand,
85 			NextWord,
86 			NextBlock,
87 			NextLine = NextBlock
88 		};
89 
90 		enum SelectionType
91 		{
92 			WordUnderCursor,
93 			WordOrCommandUnderCursor,
94 			LineUnderCursor,
95 			ParenthesesInner,
96 			ParenthesesOuter
97 		};
98 
99         Q_INVOKABLE explicit QDocumentCursor(QDocument *doc);
100         Q_INVOKABLE QDocumentCursor(const QDocumentCursor& cursor);
101 		QDocumentCursor(const QDocumentCursor& cursor, const bool cloneAutoUpdateFlag);
102         Q_INVOKABLE QDocumentCursor(QDocument *doc, int line, int column = 0, int lineTo = -1, int columnTo = -1);
103 		//QDocumentCursor(const QDocumentLine& line, int column = 0);
104         QDocumentCursor(QDocumentCursorHandle* handle = nullptr);
105 		QDocumentCursor(const QDocumentCursor& from, const QDocumentCursor& to);
106 
107 		~QDocumentCursor();
108 
109 		QDocumentCursor clone(bool cloneAutoUpdatedFlag) const;
110 
111 		QDocumentCursor& operator = (const QDocumentCursor& c);
112 
113 		bool operator == (const QDocumentCursor& c) const;
114 		bool operator != (const QDocumentCursor& c) const;
115 
116 		bool operator < (const QDocumentCursor& c) const;
117 		bool operator > (const QDocumentCursor& c) const;
118 
119 		bool operator <= (const QDocumentCursor& c) const;
120 		bool operator >= (const QDocumentCursor& c) const;
121 
122 		Q_INVOKABLE bool beginBoundaryLarger (const QDocumentCursor& c) const;
123 		Q_INVOKABLE bool endBoundaryLarger (const QDocumentCursor& c) const;
124 		Q_INVOKABLE bool equalBoundaries(const QDocumentCursor& c) const;
125 		Q_INVOKABLE bool equal(const QDocumentCursor& c) const;
126 
127 		void beginBoundary(int& begline, int& begcol) const;
128 		void endBoundary(int& endline, int& endcol) const;
129 		void boundaries(int& begline, int& begcol, int& endline, int& endcol) const;
130 
131 		Q_INVOKABLE bool isNull() const;
132 		Q_INVOKABLE bool isValid() const;
133 
134 		Q_INVOKABLE bool atEnd() const;
135 		Q_INVOKABLE bool atStart() const;
136 
137 		Q_INVOKABLE bool atBlockEnd() const;
138 		Q_INVOKABLE bool atBlockStart() const;
139 
140 		Q_INVOKABLE bool atLineEnd() const;
141 		Q_INVOKABLE bool atLineStart() const;
142 
143 		Q_INVOKABLE bool hasSelection() const;
144 		Q_INVOKABLE bool isForwardSelection() const;
145 
146 		Q_INVOKABLE bool isSilent() const;
147 		void setSilent(bool y);
148 
149 		Q_INVOKABLE bool isAutoUpdated() const;
150 		void setAutoUpdated(bool y);
151 
152 		Q_INVOKABLE bool isAutoErasable() const;
153 		void setAutoErasable(bool y);
154 
155 		int position() const;
156 
157 		Q_INVOKABLE int lineNumber() const;
158 		Q_INVOKABLE int columnNumber() const;
159 
160 		Q_INVOKABLE int anchorLineNumber() const;
161 		Q_INVOKABLE int anchorColumnNumber() const;
162 
163 		Q_INVOKABLE int startLineNumber() const;
164 		Q_INVOKABLE int startColumnNumber() const;
165 
166 		Q_INVOKABLE int endLineNumber() const;
167 		Q_INVOKABLE int endColumnNumber() const;
168 
169 		Q_INVOKABLE int visualColumnNumber() const;
170 
171 		Q_INVOKABLE void setLineNumber(int c, MoveMode m = MoveAnchor);
172 		Q_INVOKABLE void setColumnNumber(int c, MoveMode m = MoveAnchor);
173 		Q_INVOKABLE void setAnchorLineNumber(int c) const;
174 		Q_INVOKABLE void setAnchorColumnNumber(int c) const;
175 
176 		Q_INVOKABLE int wrappedLineOffset() const;
177 		Q_INVOKABLE int anchorWrappedLineOffset() const;
178 
179         Q_INVOKABLE QPointF documentPosition() const;
180         Q_INVOKABLE QPointF anchorDocumentPosition() const;
181 
182 		QPolygonF documentRegion() const;
183 
184 		QDocumentLine line() const;
185 		QDocumentLine anchorLine() const;
186 
187 		Q_INVOKABLE void shift(int offset);
188 		Q_INVOKABLE void setPosition(int pos, MoveMode m = MoveAnchor);
189 		Q_INVOKABLE bool movePosition(int offset, MoveOperation op = NextCharacter, MoveMode m = MoveAnchor);
190 
191 		Q_INVOKABLE void moveTo(int line, int column, MoveMode m = MoveAnchor);
192 		void moveTo(const QDocumentCursor &c, MoveMode m = MoveAnchor);
193 		//void moveTo(const QDocumentLine &l, int column, MoveMode m = MoveAnchor);
194 
195 		Q_INVOKABLE void eraseLine();
196 		Q_INVOKABLE void insertLine(bool keepAnchor = false);
197 		Q_INVOKABLE void insertText(const QString& s, bool keepAnchor = false);
198 
199 		QDocumentCursor selectionStart() const;
200 		QDocumentCursor selectionEnd() const;
201 		QDocumentCursor anchorCursor() const;
202 
203 		Q_INVOKABLE QString selectedText() const;
204 
205 		Q_INVOKABLE void clearSelection();
206 		Q_INVOKABLE void flipSelection();
207 		Q_INVOKABLE void removeSelectedText();
208 		Q_INVOKABLE void replaceSelectedText(const QString& text);
209 
210 		Q_INVOKABLE void select(SelectionType t);
211 		Q_INVOKABLE void expandSelect(SelectionType t);
212 		Q_INVOKABLE void setSelectionBoundary(const QDocumentCursor& c);
213 		Q_INVOKABLE void select(int line, int column, int lineTo = -1, int columnTo = -1);
214 		Q_INVOKABLE void selectColumns(int column, int columnTo = -1);
215 
216 		Q_INVOKABLE bool isWithinSelection(const QDocumentCursor& c) const;
217 		Q_INVOKABLE QDocumentCursor intersect(const QDocumentCursor& c) const;
218 		void intersectBoundaries(int& lbeg, int& cbeg, int& lend, int& cend) const;
219 
220 		void getMatchingPair(QDocumentCursor& from, QDocumentCursor& to, bool maximal = true) const;
221 
222 		Q_INVOKABLE QChar nextChar() const;
223 		Q_INVOKABLE QChar previousChar() const;
224 
225 		Q_INVOKABLE void deleteChar();
226 		Q_INVOKABLE void deletePreviousChar();
227 
228 		Q_INVOKABLE void beginEditBlock();
229 		Q_INVOKABLE void endEditBlock();
230 
231 		void refreshColumnMemory();
232 		bool hasColumnMemory() const;
233 		void setColumnMemory(bool y);
234 
235 		QDocumentSelection selection() const;
236 
237 		bool isRTL() const;
238 
239 		QDocument* document() const;
240 
handle()241 		inline QDocumentCursorHandle* handle() const
242 		{ return m_handle; }
243 
244 		static void sort(QDocumentCursor& from, QDocumentCursor& to);
245 
246 	private:
247 		QDocumentCursorHandle *m_handle;
248 };
249 
250 Q_DECLARE_METATYPE(QDocumentCursor)
251 
252 #endif
253