Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / starmath / inc / caret.hxx
blob90ba2f899becada87d4cdf1a26eb8ae515e99634
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9 #ifndef INCLUDED_STARMATH_INC_CARET_HXX
10 #define INCLUDED_STARMATH_INC_CARET_HXX
12 #include <sal/config.h>
14 #include "node.hxx"
16 #include <cassert>
17 #include <memory>
18 #include <vector>
20 /** Representation of caret position with an equation */
21 struct SmCaretPos{
22 SmCaretPos(SmNode* selectedNode = nullptr, int iIndex = 0)
23 : pSelectedNode(selectedNode)
24 , nIndex(iIndex)
26 assert(nIndex >= 0);
29 /** Selected node */
30 SmNode* pSelectedNode;
32 /** Index (invariant: non-negative) within the selected node
34 * 0: Position in front of a node
35 * 1: Position after a node or after first char in SmTextNode
36 * n: Position after n char in SmTextNode
38 * Notice how there's special cases for SmTextNode.
40 //TODO: Special cases for SmBlankNode is needed
41 //TODO: Consider forgetting about the todo above... As it's really unpleasant.
42 int nIndex;
44 /** True, if this is a valid caret position */
45 bool IsValid() const { return pSelectedNode != nullptr; }
46 bool operator==(const SmCaretPos &pos) const {
47 return pos.pSelectedNode == pSelectedNode && nIndex == pos.nIndex;
49 /** Get the caret position after pNode, regardless of pNode
51 * Gets the caret position following pNode, this is SmCaretPos(pNode, 1).
52 * Unless pNode is an instance of SmTextNode, then the index is the text length.
54 static SmCaretPos GetPosAfter(SmNode* pNode) {
55 if(pNode && pNode->GetType() == SmNodeType::Text)
56 return SmCaretPos(pNode, static_cast<SmTextNode*>(pNode)->GetText().getLength());
57 return SmCaretPos(pNode, 1);
61 /** A line that represents a caret */
62 class SmCaretLine{
63 public:
64 SmCaretLine(long left = 0, long top = 0, long height = 0) {
65 _top = top;
66 _left = left;
67 _height = height;
69 long GetTop() const {return _top;}
70 long GetLeft() const {return _left;}
71 long GetHeight() const {return _height;}
72 long SquaredDistanceX(const SmCaretLine& line) const{
73 return (GetLeft() - line.GetLeft()) * (GetLeft() - line.GetLeft());
75 long SquaredDistanceX(const Point &pos) const{
76 return (GetLeft() - pos.X()) * (GetLeft() - pos.X());
78 long SquaredDistanceY(const SmCaretLine& line) const{
79 long d = GetTop() - line.GetTop();
80 if(d < 0)
81 d = (d * -1) - GetHeight();
82 else
83 d = d - line.GetHeight();
84 if(d < 0)
85 return 0;
86 return d * d;
88 long SquaredDistanceY(const Point &pos) const{
89 long d = GetTop() - pos.Y();
90 if(d < 0)
91 d = (d * -1) - GetHeight();
92 if(d < 0)
93 return 0;
94 return d * d;
96 private:
97 long _top;
98 long _left;
99 long _height;
102 // SmCaretPosGraph
104 /** An entry in SmCaretPosGraph */
105 struct SmCaretPosGraphEntry{
106 SmCaretPosGraphEntry(SmCaretPos pos,
107 SmCaretPosGraphEntry* left,
108 SmCaretPosGraphEntry* right) {
109 CaretPos = pos;
110 Left = left;
111 Right = right;
113 /** Caret position */
114 SmCaretPos CaretPos;
115 /** Entry to the left visually */
116 SmCaretPosGraphEntry* Left;
117 /** Entry to the right visually */
118 SmCaretPosGraphEntry* Right;
119 void SetRight(SmCaretPosGraphEntry* right){
120 Right = right;
122 void SetLeft(SmCaretPosGraphEntry* left){
123 Left = left;
127 /** A graph over all caret positions
128 * @remarks Graphs can only grow, entries cannot be removed!
130 class SmCaretPosGraph{
131 public:
132 SmCaretPosGraph();
134 ~SmCaretPosGraph();
136 /** Add a caret position
137 * @remarks If left is NULL, they will point back to the entry.
139 SmCaretPosGraphEntry* Add(SmCaretPos pos,
140 SmCaretPosGraphEntry* left = nullptr);
142 std::vector<std::unique_ptr<SmCaretPosGraphEntry>>::iterator begin()
144 return mvEntries.begin();
147 std::vector<std::unique_ptr<SmCaretPosGraphEntry>>::iterator end()
149 return mvEntries.end();
152 private:
153 std::vector<std::unique_ptr<SmCaretPosGraphEntry>> mvEntries;
156 /** \page visual_formula_editing Visual Formula Editing
157 * A visual formula editor allows users to easily edit formulas without having to learn and
158 * use complicated commands. A visual formula editor is a WYSIWYG editor. For OpenOffice Math
159 * this essentially means that you can click on the formula image, to get a caret, which you
160 * can move with arrow keys, and use to modify the formula by entering text, clicking buttons
161 * or using shortcuts.
163 * \subsection formula_trees Formula Trees
164 * A formula in OpenOffice Math is a tree of nodes, take for instance the formula
165 * "A + {B cdot C} over D", it looks like this
166 * \f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$. The tree for this formula
167 * looks like this:
169 * \dot
170 * digraph {
171 * labelloc = "t";
172 * label= "Equation: \"A + {B cdot C} over D\"";
173 * size = "9,9";
174 * n0 [label="SmTableNode (1)"];
175 * n0 -> n1 [label="0"];
176 * n1 [label="SmLineNode (2)"];
177 * n1 -> n2 [label="0"];
178 * n2 [label="SmExpressionNode (3)"];
179 * n2 -> n3 [label="0"];
180 * n3 [label="SmBinHorNode (4)"];
181 * n3 -> n4 [label="0"];
182 * n4 [label="SmTextNode: A (5)"];
183 * n3 -> n5 [label="1"];
184 * n5 [label="SmMathSymbolNode: + (6)"];
185 * n3 -> n6 [label="2"];
186 * n6 [label="SmBinVerNode (7)"];
187 * n6 -> n7 [label="0"];
188 * n7 [label="SmExpressionNode (8)"];
189 * n7 -> n8 [label="0"];
190 * n8 [label="SmBinHorNode (9)"];
191 * n8 -> n9 [label="0"];
192 * n9 [label="SmTextNode: B (10)"];
193 * n8 -> n10 [label="1"];
194 * n10 [label="SmMathSymbolNode: &#183; (11)"];
195 * n8 -> n11 [label="2"];
196 * n11 [label="SmTextNode: C (12)"];
197 * n6 -> n12 [label="1"];
198 * n12 [label="SmRectangleNode (13)"];
199 * n6 -> n13 [label="2"];
200 * n13 [label="SmTextNode: D (14)"];
202 * \enddot
204 * The vertices are nodes, their label says what kind of node and the number in parentheses is
205 * the identifier of the node (In practices a pointer is used instead of the id). The direction
206 * of the edges tells which node is parent and which is child. The label of the edges are the
207 * child node index number, given to SmNode::GetSubNode() of the parent to get the child node.
210 * \subsection visual_lines Visual Lines
212 * Inorder to do caret movement in visual lines, we need a definition of caret position and
213 * visual line. In a tree such as the above there are three visual lines. There's the outer most
214 * line, with entries such as
215 * \f$\mbox{A}\f$, \f$ + \f$ and \f$ \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$. Then there's
216 * the numerator line of the fraction it has entries \f$ \mbox{B} \f$, \f$ \cdot \f$ and \f$ \mbox{C} \f$.
217 * And last by not least there's the denominator line of the fraction it's only entry is \f$ \mbox{D} \f$.
219 * For visual editing it should be possible to place a caret on both sides of any line entry,
220 * consider a line entry a character or construction that in a line is treated as a character.
221 * Imagine the caret is placed to the right of the plus sign (id: 6), now if user presses
222 * backspace this should delete the plus sign (id: 6), and if the user presses delete this
223 * should delete the entire fraction (id: 7). This is because the caret is in the outer most
224 * line where the fraction is considered a line entry.
226 * However, inorder to prevent users from accidentally deleting large subtrees, just because
227 * they logically placed there caret a in the wrong line, require that complex constructions
228 * such as a fraction is selected before it is deleted. Thus in this case it wouldn't be
229 * deleted, but only selected and then deleted if the user hit delete again. Anyway, this is
230 * slightly off topic for now.
232 * Important about visual lines is that they don't always have an SmExpressionNode as root
233 * and the entries in a visual line is all the nodes of a subtree ordered left to right that
234 * isn't either an SmExpressionNode, SmBinHorNode or SmUnHorNode.
237 * \subsection caret_positions Caret Positions
239 * A caret position in OpenOffice Math is representated by an instance of SmCaretPos.
240 * That is a caret position is a node and an index related to this node. For most nodes the
241 * index 0, means caret is in front of this node, the index 1 means caret is after this node.
242 * For SmTextNode the index is the caret position after the specified number of characters,
243 * imagine an SmTextNode with the number 1337. The index 3 in such SmTextNode would mean a
244 * caret placed right before 7, e.g. "133|7".
246 * For SmExpressionNode, SmBinHorNode and SmUnHorNode the only legal index is 0, which means
247 * in front of the node. Actually the index 0 may only because for the first caret position
248 * in a visual line. From the example above, consider the following subtree that constitutes
249 * a visual line:
251 * \dot
252 * digraph {
253 * labelloc = "t";
254 * label= "Subtree that constitutes a visual line";
255 * size = "7,5";
256 * n7 [label="SmExpressionNode (8)"];
257 * n7 -> n8 [label="0"];
258 * n8 [label="SmBinHorNode (9)"];
259 * n8 -> n9 [label="0"];
260 * n9 [label="SmTextNode: B (10)"];
261 * n8 -> n10 [label="1"];
262 * n10 [label="SmMathSymbolNode: &#183; (11)"];
263 * n8 -> n11 [label="2"];
264 * n11 [label="SmTextNode: C (12)"];
266 * \enddot
267 * Here the caret positions are:
269 * <TABLE>
270 * <TR><TD><B>Caret position:</B></TD><TD><B>Example:</B></TD>
271 * </TR><TR>
272 * <TD>{id: 8, index: 0}</TD>
273 * <TD>\f$ \mid \mbox{C} \cdot \mbox{C} \f$</TD>
274 * </TR><TR>
275 * <TD>{id: 10, index: 1}</TD>
276 * <TD>\f$ \mbox{C} \mid \cdot \mbox{C} \f$</TD>
277 * </TR><TR>
278 * <TD>{id: 11, index: 1}</TD>
279 * <TD>\f$ \mbox{C} \cdot \mid \mbox{C} \f$</TD>
280 * </TR><TR>
281 * <TD>{id: 12, index: 1}</TD>
282 * <TD>\f$ \mbox{C} \cdot \mbox{C} \mid \f$</TD>
283 * </TR><TR>
284 * </TABLE>
286 * Where \f$ \mid \f$ is used to denote caret position.
288 * With these exceptions included in the definition the id and index: {id: 11, index: 0} does
289 * \b not constitute a caret position in the given context. Note the method
290 * SmCaretPos::IsValid() does not check if this invariant holds true, but code in SmCaret,
291 * SmSetSelectionVisitor and other places depends on this invariant to hold.
294 * \subsection caret_movement Caret Movement
296 * As the placement of caret positions depends very much on the context within which a node
297 * appears it is not trivial to find all caret positions and determine which follows which.
298 * In OpenOffice Math this is done by the SmCaretPosGraphBuildingVisitor. This visitor builds
299 * graph (an instance of SmCaretPosGraph) over the caret positions. For details on how this
300 * graph is build, and how new methods should be implemented see SmCaretPosGraphBuildingVisitor.
302 * The result of the SmCaretPosGraphBuildingVisitor is a graph over the caret positions in a
303 * formula, representated by an instance of SmCaretPosGraph. Each entry (instances of SmCaretPosGraphEntry)
304 * has a pointer to the entry to the left and right of itself. This way we can easily find
305 * the caret position to a right or left of a given caret position. Note each caret position
306 * only appears once in this graph.
308 * When searching for a caret position after a left click on the formula this map is also used.
309 * We simply iterate over all entries, uses the SmCaretPos2LineVisitor to find a line for each
310 * caret position. Then the distance from the click to the line is computed and we choose the
311 * caret position closest to the click.
313 * For up and down movement, we also iterator over all caret positions and use SmCaretPos2LineVisitor
314 * to find a line for each caret position. Then we compute the distance from the current
315 * caret position to every other caret position and chooses the one closest that is either
316 * above or below the current caret position, depending on whether we're doing up or down movement.
318 * This result of this approach to caret movement is that we have logically predictable
319 * movement for left and right, whilst leftclick, up and down movement depends on the sizes
320 * and placement of all node and may be less logically predictable. This solution also means
321 * that we only have one complex visitor generating the graph, imagine the nightmare if we
322 * had a visitor for movement in each direction.
324 * Making up and down movement independent of node sizes and placement wouldn't necessarily
325 * be a good thing either. Consider the formula \f$ \frac{1+2+3+4+5}{6} \f$, if the caret is
326 * placed as displayed here: \f$ \frac{1+2+3+4+5}{6 \mid} \f$, up movement should move to right
327 * after "3": \f$ \frac{1+2+3|+4+5}{6} \f$. However, such a move depends on the sizes and placement
328 * of all nodes in the fraction.
331 * \subsubsection caretpos_graph_example Example of Caret Position Graph
333 * If we consider the formula
334 * \f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$ from \ref formula_trees.
335 * It has the following caret positions:
337 * <TABLE>
338 * <TR>
339 * <TD><B>Caret position:</B></TD>
340 * <TD><B>Example:</B></TD>
341 * </TR><TR>
342 * <TD>{id: 3, index: 0}</TD>
343 * <TD>\f$ \mid\mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
344 * </TR><TR>
345 * <TD>{id: 5, index: 1}</TD>
346 * <TD>\f$ \mbox{A}\mid + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
347 * </TR><TR>
348 * <TD>{id: 6, index: 1}</TD>
349 * <TD>\f$ \mbox{A} + \mid \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
350 * </TR><TR>
351 * <TD>{id: 8, index: 0}</TD>
352 * <TD>\f$ \mbox{A} + \frac{ \mid \mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
353 * </TR><TR>
354 * <TD>{id: 10, index: 1}</TD>
355 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \mid \cdot \mbox{C}}{\mbox{D}} \f$</TD>
356 * </TR><TR>
357 * <TD>{id: 11, index: 1}</TD>
358 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mid \mbox{C}}{\mbox{D}} \f$</TD>
359 * </TR><TR>
360 * <TD>{id: 12, index: 1}</TD>
361 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C} \mid}{\mbox{D}} \f$</TD>
362 * </TR><TR>
363 * <TD>{id: 14, index: 0}</TD>
364 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mid \mbox{D}} \f$</TD>
365 * </TR><TR>
366 * <TD>{id: 14, index: 1}</TD>
367 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D} \mid} \f$</TD>
368 * </TR><TR>
369 * <TD>{id: 7, index: 1}</TD>
370 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \mid \f$</TD>
371 * </TR>
372 * </TABLE>
374 * Below is a directed graph over the caret positions and how you can move between them.
375 * \dot
376 * digraph {
377 * labelloc = "t";
378 * label= "Caret Position Graph";
379 * size = "4,6";
380 * p0 [label = "{id: 3, index: 0}"];
381 * p0 -> p1 [fontsize = 10.0, label = "right"];
382 * p1 [label = "{id: 5, index: 1}"];
383 * p1 -> p0 [fontsize = 10.0, label = "left"];
384 * p1 -> p2 [fontsize = 10.0, label = "right"];
385 * p2 [label = "{id: 6, index: 1}"];
386 * p2 -> p1 [fontsize = 10.0, label = "left"];
387 * p2 -> p3 [fontsize = 10.0, label = "right"];
388 * p3 [label = "{id: 8, index: 0}"];
389 * p3 -> p2 [fontsize = 10.0, label = "left"];
390 * p3 -> p4 [fontsize = 10.0, label = "right"];
391 * p4 [label = "{id: 10, index: 1}"];
392 * p4 -> p3 [fontsize = 10.0, label = "left"];
393 * p4 -> p5 [fontsize = 10.0, label = "right"];
394 * p5 [label = "{id: 11, index: 1}"];
395 * p5 -> p4 [fontsize = 10.0, label = "left"];
396 * p5 -> p6 [fontsize = 10.0, label = "right"];
397 * p6 [label = "{id: 12, index: 1}"];
398 * p6 -> p5 [fontsize = 10.0, label = "left"];
399 * p6 -> p9 [fontsize = 10.0, label = "right"];
400 * p7 [label = "{id: 14, index: 0}"];
401 * p7 -> p2 [fontsize = 10.0, label = "left"];
402 * p7 -> p8 [fontsize = 10.0, label = "right"];
403 * p8 [label = "{id: 14, index: 1}"];
404 * p8 -> p7 [fontsize = 10.0, label = "left"];
405 * p8 -> p9 [fontsize = 10.0, label = "right"];
406 * p9 [label = "{id: 7, index: 1}"];
407 * p9 -> p6 [fontsize = 10.0, label = "left"];
409 * \enddot
412 /* TODO: Write documentation about the following keywords:
414 * Visual Selections:
415 * - Show images
416 * - Talk about how the visitor does this
418 * Modifying a Visual Line:
419 * - Find top most non-compo of the line (e.g. The subtree that constitutes a line)
420 * - Make the line into a list
421 * - Edit the list, add/remove/modify nodes
422 * - Parse the list back into a subtree
423 * - Insert the new subtree where the old was taken
426 #endif // INCLUDED_STARMATH_INC_CARET_HXX
428 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */