1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
9 #ifndef INCLUDED_STARMATH_INC_CARET_HXX
10 #define INCLUDED_STARMATH_INC_CARET_HXX
12 #include <sal/config.h>
20 /** Representation of caret position with an equation */
23 SmCaretPos(SmNode
* selectedNode
= nullptr, int iIndex
= 0)
24 : pSelectedNode(selectedNode
)
31 SmNode
* pSelectedNode
;
33 /** Index (invariant: non-negative) within the selected node
35 * 0: Position in front of a node
36 * 1: Position after a node or after first char in SmTextNode
37 * n: Position after n char in SmTextNode
39 * Notice how there's special cases for SmTextNode.
41 //TODO: Special cases for SmBlankNode is needed
42 //TODO: Consider forgetting about the todo above... As it's really unpleasant.
45 /** True, if this is a valid caret position */
46 bool IsValid() const { return pSelectedNode
!= nullptr; }
47 bool operator==(const SmCaretPos
& pos
) const
49 return pos
.pSelectedNode
== pSelectedNode
&& nIndex
== pos
.nIndex
;
51 /** Get the caret position after pNode, regardless of pNode
53 * Gets the caret position following pNode, this is SmCaretPos(pNode, 1).
54 * Unless pNode is an instance of SmTextNode, then the index is the text length.
56 static SmCaretPos
GetPosAfter(SmNode
* pNode
)
58 if (pNode
&& pNode
->GetType() == SmNodeType::Text
)
59 return SmCaretPos(pNode
, static_cast<SmTextNode
*>(pNode
)->GetText().getLength());
60 return SmCaretPos(pNode
, 1);
64 /** A line that represents a caret */
68 SmCaretLine(tools::Long left
= 0, tools::Long top
= 0, tools::Long height
= 0)
74 tools::Long
GetTop() const { return _top
; }
75 tools::Long
GetLeft() const { return _left
; }
76 tools::Long
GetHeight() const { return _height
; }
77 tools::Long
SquaredDistanceX(const SmCaretLine
& line
) const
79 return (GetLeft() - line
.GetLeft()) * (GetLeft() - line
.GetLeft());
81 tools::Long
SquaredDistanceX(const Point
& pos
) const
83 return (GetLeft() - pos
.X()) * (GetLeft() - pos
.X());
85 tools::Long
SquaredDistanceY(const SmCaretLine
& line
) const
87 tools::Long d
= GetTop() - line
.GetTop();
89 d
= (d
* -1) - GetHeight();
91 d
= d
- line
.GetHeight();
96 tools::Long
SquaredDistanceY(const Point
& pos
) const
98 tools::Long d
= GetTop() - pos
.Y();
100 d
= (d
* -1) - GetHeight();
114 /** An entry in SmCaretPosGraph */
115 struct SmCaretPosGraphEntry
117 SmCaretPosGraphEntry(SmCaretPos pos
, SmCaretPosGraphEntry
* left
, SmCaretPosGraphEntry
* right
)
123 /** Caret position */
124 const SmCaretPos CaretPos
;
125 /** Entry to the left visually */
126 SmCaretPosGraphEntry
* Left
;
127 /** Entry to the right visually */
128 SmCaretPosGraphEntry
* Right
;
129 void SetRight(SmCaretPosGraphEntry
* right
) { Right
= right
; }
130 void SetLeft(SmCaretPosGraphEntry
* left
) { Left
= left
; }
133 /** A graph over all caret positions
134 * @remarks Graphs can only grow, entries cannot be removed!
136 class SmCaretPosGraph
143 /** Add a caret position
144 * @remarks If left is NULL, they will point back to the entry.
146 SmCaretPosGraphEntry
* Add(SmCaretPos pos
, SmCaretPosGraphEntry
* left
= nullptr);
148 std::vector
<std::unique_ptr
<SmCaretPosGraphEntry
>>::iterator
begin()
150 return mvEntries
.begin();
153 std::vector
<std::unique_ptr
<SmCaretPosGraphEntry
>>::iterator
end() { return mvEntries
.end(); }
156 std::vector
<std::unique_ptr
<SmCaretPosGraphEntry
>> mvEntries
;
159 /** \page visual_formula_editing Visual Formula Editing
160 * A visual formula editor allows users to easily edit formulas without having to learn and
161 * use complicated commands. A visual formula editor is a WYSIWYG editor. For OpenOffice Math
162 * this essentially means that you can click on the formula image, to get a caret, which you
163 * can move with arrow keys, and use to modify the formula by entering text, clicking buttons
164 * or using shortcuts.
166 * \subsection formula_trees Formula Trees
167 * A formula in OpenOffice Math is a tree of nodes, take for instance the formula
168 * "A + {B cdot C} over D", it looks like this
169 * \f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$. The tree for this formula
175 * label= "Equation: \"A + {B cdot C} over D\"";
177 * n0 [label="SmTableNode (1)"];
178 * n0 -> n1 [label="0"];
179 * n1 [label="SmLineNode (2)"];
180 * n1 -> n2 [label="0"];
181 * n2 [label="SmExpressionNode (3)"];
182 * n2 -> n3 [label="0"];
183 * n3 [label="SmBinHorNode (4)"];
184 * n3 -> n4 [label="0"];
185 * n4 [label="SmTextNode: A (5)"];
186 * n3 -> n5 [label="1"];
187 * n5 [label="SmMathSymbolNode: + (6)"];
188 * n3 -> n6 [label="2"];
189 * n6 [label="SmBinVerNode (7)"];
190 * n6 -> n7 [label="0"];
191 * n7 [label="SmExpressionNode (8)"];
192 * n7 -> n8 [label="0"];
193 * n8 [label="SmBinHorNode (9)"];
194 * n8 -> n9 [label="0"];
195 * n9 [label="SmTextNode: B (10)"];
196 * n8 -> n10 [label="1"];
197 * n10 [label="SmMathSymbolNode: · (11)"];
198 * n8 -> n11 [label="2"];
199 * n11 [label="SmTextNode: C (12)"];
200 * n6 -> n12 [label="1"];
201 * n12 [label="SmRectangleNode (13)"];
202 * n6 -> n13 [label="2"];
203 * n13 [label="SmTextNode: D (14)"];
207 * The vertices are nodes, their label says what kind of node and the number in parentheses is
208 * the identifier of the node (In practices a pointer is used instead of the id). The direction
209 * of the edges tells which node is parent and which is child. The label of the edges are the
210 * child node index number, given to SmNode::GetSubNode() of the parent to get the child node.
213 * \subsection visual_lines Visual Lines
215 * Inorder to do caret movement in visual lines, we need a definition of caret position and
216 * visual line. In a tree such as the above there are three visual lines. There's the outer most
217 * line, with entries such as
218 * \f$\mbox{A}\f$, \f$ + \f$ and \f$ \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$. Then there's
219 * the numerator line of the fraction it has entries \f$ \mbox{B} \f$, \f$ \cdot \f$ and \f$ \mbox{C} \f$.
220 * And last by not least there's the denominator line of the fraction it's only entry is \f$ \mbox{D} \f$.
222 * For visual editing it should be possible to place a caret on both sides of any line entry,
223 * consider a line entry a character or construction that in a line is treated as a character.
224 * Imagine the caret is placed to the right of the plus sign (id: 6), now if user presses
225 * backspace this should delete the plus sign (id: 6), and if the user presses delete this
226 * should delete the entire fraction (id: 7). This is because the caret is in the outer most
227 * line where the fraction is considered a line entry.
229 * However, inorder to prevent users from accidentally deleting large subtrees, just because
230 * they logically placed there caret a in the wrong line, require that complex constructions
231 * such as a fraction is selected before it is deleted. Thus in this case it wouldn't be
232 * deleted, but only selected and then deleted if the user hit delete again. Anyway, this is
233 * slightly off topic for now.
235 * Important about visual lines is that they don't always have an SmExpressionNode as root
236 * and the entries in a visual line is all the nodes of a subtree ordered left to right that
237 * isn't either an SmExpressionNode, SmBinHorNode or SmUnHorNode.
240 * \subsection caret_positions Caret Positions
242 * A caret position in OpenOffice Math is represented by an instance of SmCaretPos.
243 * That is a caret position is a node and an index related to this node. For most nodes the
244 * index 0, means caret is in front of this node, the index 1 means caret is after this node.
245 * For SmTextNode the index is the caret position after the specified number of characters,
246 * imagine an SmTextNode with the number 1337. The index 3 in such SmTextNode would mean a
247 * caret placed right before 7, e.g. "133|7".
249 * For SmExpressionNode, SmBinHorNode and SmUnHorNode the only legal index is 0, which means
250 * in front of the node. Actually the index 0 may only because for the first caret position
251 * in a visual line. From the example above, consider the following subtree that constitutes
257 * label= "Subtree that constitutes a visual line";
259 * n7 [label="SmExpressionNode (8)"];
260 * n7 -> n8 [label="0"];
261 * n8 [label="SmBinHorNode (9)"];
262 * n8 -> n9 [label="0"];
263 * n9 [label="SmTextNode: B (10)"];
264 * n8 -> n10 [label="1"];
265 * n10 [label="SmMathSymbolNode: · (11)"];
266 * n8 -> n11 [label="2"];
267 * n11 [label="SmTextNode: C (12)"];
270 * Here the caret positions are:
273 * <TR><TD><B>Caret position:</B></TD><TD><B>Example:</B></TD>
275 * <TD>{id: 8, index: 0}</TD>
276 * <TD>\f$ \mid \mbox{C} \cdot \mbox{C} \f$</TD>
278 * <TD>{id: 10, index: 1}</TD>
279 * <TD>\f$ \mbox{C} \mid \cdot \mbox{C} \f$</TD>
281 * <TD>{id: 11, index: 1}</TD>
282 * <TD>\f$ \mbox{C} \cdot \mid \mbox{C} \f$</TD>
284 * <TD>{id: 12, index: 1}</TD>
285 * <TD>\f$ \mbox{C} \cdot \mbox{C} \mid \f$</TD>
289 * Where \f$ \mid \f$ is used to denote caret position.
291 * With these exceptions included in the definition the id and index: {id: 11, index: 0} does
292 * \b not constitute a caret position in the given context. Note the method
293 * SmCaretPos::IsValid() does not check if this invariant holds true, but code in SmCaret,
294 * SmSetSelectionVisitor and other places depends on this invariant to hold.
297 * \subsection caret_movement Caret Movement
299 * As the placement of caret positions depends very much on the context within which a node
300 * appears it is not trivial to find all caret positions and determine which follows which.
301 * In OpenOffice Math this is done by the SmCaretPosGraphBuildingVisitor. This visitor builds
302 * graph (an instance of SmCaretPosGraph) over the caret positions. For details on how this
303 * graph is build, and how new methods should be implemented see SmCaretPosGraphBuildingVisitor.
305 * The result of the SmCaretPosGraphBuildingVisitor is a graph over the caret positions in a
306 * formula, represented by an instance of SmCaretPosGraph. Each entry (instances of SmCaretPosGraphEntry)
307 * has a pointer to the entry to the left and right of itself. This way we can easily find
308 * the caret position to a right or left of a given caret position. Note each caret position
309 * only appears once in this graph.
311 * When searching for a caret position after a left click on the formula this map is also used.
312 * We simply iterate over all entries, uses the SmCaretPos2LineVisitor to find a line for each
313 * caret position. Then the distance from the click to the line is computed and we choose the
314 * caret position closest to the click.
316 * For up and down movement, we also iterator over all caret positions and use SmCaretPos2LineVisitor
317 * to find a line for each caret position. Then we compute the distance from the current
318 * caret position to every other caret position and chooses the one closest that is either
319 * above or below the current caret position, depending on whether we're doing up or down movement.
321 * This result of this approach to caret movement is that we have logically predictable
322 * movement for left and right, whilst leftclick, up and down movement depends on the sizes
323 * and placement of all node and may be less logically predictable. This solution also means
324 * that we only have one complex visitor generating the graph, imagine the nightmare if we
325 * had a visitor for movement in each direction.
327 * Making up and down movement independent of node sizes and placement wouldn't necessarily
328 * be a good thing either. Consider the formula \f$ \frac{1+2+3+4+5}{6} \f$, if the caret is
329 * placed as displayed here: \f$ \frac{1+2+3+4+5}{6 \mid} \f$, up movement should move to right
330 * after "3": \f$ \frac{1+2+3|+4+5}{6} \f$. However, such a move depends on the sizes and placement
331 * of all nodes in the fraction.
334 * \subsubsection caretpos_graph_example Example of Caret Position Graph
336 * If we consider the formula
337 * \f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$ from \ref formula_trees.
338 * It has the following caret positions:
342 * <TD><B>Caret position:</B></TD>
343 * <TD><B>Example:</B></TD>
345 * <TD>{id: 3, index: 0}</TD>
346 * <TD>\f$ \mid\mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
348 * <TD>{id: 5, index: 1}</TD>
349 * <TD>\f$ \mbox{A}\mid + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
351 * <TD>{id: 6, index: 1}</TD>
352 * <TD>\f$ \mbox{A} + \mid \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
354 * <TD>{id: 8, index: 0}</TD>
355 * <TD>\f$ \mbox{A} + \frac{ \mid \mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
357 * <TD>{id: 10, index: 1}</TD>
358 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \mid \cdot \mbox{C}}{\mbox{D}} \f$</TD>
360 * <TD>{id: 11, index: 1}</TD>
361 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mid \mbox{C}}{\mbox{D}} \f$</TD>
363 * <TD>{id: 12, index: 1}</TD>
364 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C} \mid}{\mbox{D}} \f$</TD>
366 * <TD>{id: 14, index: 0}</TD>
367 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mid \mbox{D}} \f$</TD>
369 * <TD>{id: 14, index: 1}</TD>
370 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D} \mid} \f$</TD>
372 * <TD>{id: 7, index: 1}</TD>
373 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \mid \f$</TD>
377 * Below is a directed graph over the caret positions and how you can move between them.
381 * label= "Caret Position Graph";
383 * p0 [label = "{id: 3, index: 0}"];
384 * p0 -> p1 [fontsize = 10.0, label = "right"];
385 * p1 [label = "{id: 5, index: 1}"];
386 * p1 -> p0 [fontsize = 10.0, label = "left"];
387 * p1 -> p2 [fontsize = 10.0, label = "right"];
388 * p2 [label = "{id: 6, index: 1}"];
389 * p2 -> p1 [fontsize = 10.0, label = "left"];
390 * p2 -> p3 [fontsize = 10.0, label = "right"];
391 * p3 [label = "{id: 8, index: 0}"];
392 * p3 -> p2 [fontsize = 10.0, label = "left"];
393 * p3 -> p4 [fontsize = 10.0, label = "right"];
394 * p4 [label = "{id: 10, index: 1}"];
395 * p4 -> p3 [fontsize = 10.0, label = "left"];
396 * p4 -> p5 [fontsize = 10.0, label = "right"];
397 * p5 [label = "{id: 11, index: 1}"];
398 * p5 -> p4 [fontsize = 10.0, label = "left"];
399 * p5 -> p6 [fontsize = 10.0, label = "right"];
400 * p6 [label = "{id: 12, index: 1}"];
401 * p6 -> p5 [fontsize = 10.0, label = "left"];
402 * p6 -> p9 [fontsize = 10.0, label = "right"];
403 * p7 [label = "{id: 14, index: 0}"];
404 * p7 -> p2 [fontsize = 10.0, label = "left"];
405 * p7 -> p8 [fontsize = 10.0, label = "right"];
406 * p8 [label = "{id: 14, index: 1}"];
407 * p8 -> p7 [fontsize = 10.0, label = "left"];
408 * p8 -> p9 [fontsize = 10.0, label = "right"];
409 * p9 [label = "{id: 7, index: 1}"];
410 * p9 -> p6 [fontsize = 10.0, label = "left"];
415 /* TODO: Write documentation about the following keywords:
419 * - Talk about how the visitor does this
421 * Modifying a Visual Line:
422 * - Find top most non-compo of the line (e.g. The subtree that constitutes a line)
423 * - Make the line into a list
424 * - Edit the list, add/remove/modify nodes
425 * - Parse the list back into a subtree
426 * - Insert the new subtree where the old was taken
429 #endif // INCLUDED_STARMATH_INC_CARET_HXX
431 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */