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>
14 #include <sal/log.hxx>
21 /** Representation of caret position with an equation */
23 SmCaretPos(SmNode
* selectedNode
= nullptr, int iIndex
= 0) {
24 pSelectedNode
= selectedNode
;
28 SmNode
* pSelectedNode
;
29 /** Index within the selected node
31 * 0: Position in front of a node
32 * 1: Position after a node or after first char in SmTextNode
33 * n: Position after n char in SmTextNode
35 * Notice how there's special cases for SmTextNode.
37 //TODO: Special cases for SmBlankNode is needed
38 //TODO: Consider forgetting about the todo above... As it's really unpleasant.
40 /** True, if this is a valid caret position */
41 bool IsValid() const { return pSelectedNode
!= nullptr; }
42 bool operator==(const SmCaretPos
&pos
) const {
43 return pos
.pSelectedNode
== pSelectedNode
&& Index
== pos
.Index
;
45 /** Get the caret position after pNode, regardless of pNode
47 * Gets the caret position following pNode, this is SmCaretPos(pNode, 1).
48 * Unless pNode is an instance of SmTextNode, then the index is the text length.
50 static SmCaretPos
GetPosAfter(SmNode
* pNode
) {
51 if(pNode
&& pNode
->GetType() == NTEXT
)
52 return SmCaretPos(pNode
, static_cast<SmTextNode
*>(pNode
)->GetText().getLength());
53 return SmCaretPos(pNode
, 1);
57 /** A line that represents a caret */
60 SmCaretLine(long left
= 0, long top
= 0, long height
= 0) {
65 long GetTop() const {return _top
;}
66 long GetLeft() const {return _left
;}
67 long GetHeight() const {return _height
;}
68 long SquaredDistanceX(const SmCaretLine
& line
) const{
69 return (GetLeft() - line
.GetLeft()) * (GetLeft() - line
.GetLeft());
71 long SquaredDistanceX(const Point
&pos
) const{
72 return (GetLeft() - pos
.X()) * (GetLeft() - pos
.X());
74 long SquaredDistanceY(const SmCaretLine
& line
) const{
75 long d
= GetTop() - line
.GetTop();
77 d
= (d
* -1) - GetHeight();
79 d
= d
- line
.GetHeight();
84 long SquaredDistanceY(const Point
&pos
) const{
85 long d
= GetTop() - pos
.Y();
87 d
= (d
* -1) - GetHeight();
100 /** An entry in SmCaretPosGraph */
101 struct SmCaretPosGraphEntry
{
102 SmCaretPosGraphEntry(SmCaretPos pos
,
103 SmCaretPosGraphEntry
* left
,
104 SmCaretPosGraphEntry
* right
) {
109 /** Caret position */
111 /** Entry to the left visually */
112 SmCaretPosGraphEntry
* Left
;
113 /** Entry to the right visually */
114 SmCaretPosGraphEntry
* Right
;
115 void SetRight(SmCaretPosGraphEntry
* right
){
118 void SetLeft(SmCaretPosGraphEntry
* left
){
123 /** A graph over all caret positions
124 * @remarks Graphs can only grow, entries cannot be removed!
126 class SmCaretPosGraph
{
132 /** Add a caret position
133 * @remarks If left is NULL, they will point back to the entry.
135 SmCaretPosGraphEntry
* Add(SmCaretPos pos
,
136 SmCaretPosGraphEntry
* left
= nullptr);
138 std::vector
<std::unique_ptr
<SmCaretPosGraphEntry
>>::iterator
begin()
140 return mvEntries
.begin();
143 std::vector
<std::unique_ptr
<SmCaretPosGraphEntry
>>::iterator
end()
145 return mvEntries
.end();
149 std::vector
<std::unique_ptr
<SmCaretPosGraphEntry
>> mvEntries
;
152 /** \page visual_formula_editing Visual Formula Editing
153 * A visual formula editor allows users to easily edit formulas without having to learn and
154 * use complicated commands. A visual formula editor is a WYSIWYG editor. For OpenOffice Math
155 * this essentially means that you can click on the formula image, to get a caret, which you
156 * can move with arrow keys, and use to modify the formula by entering text, clicking buttons
157 * or using shortcuts.
159 * \subsection formula_trees Formula Trees
160 * A formula in OpenOffice Math is a tree of nodes, take for instance the formula
161 * "A + {B cdot C} over D", it looks like this
162 * \f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$. The tree for this formula
168 * label= "Equation: \"A + {B cdot C} over D\"";
170 * n0 [label="SmTableNode (1)"];
171 * n0 -> n1 [label="0"];
172 * n1 [label="SmLineNode (2)"];
173 * n1 -> n2 [label="0"];
174 * n2 [label="SmExpressionNode (3)"];
175 * n2 -> n3 [label="0"];
176 * n3 [label="SmBinHorNode (4)"];
177 * n3 -> n4 [label="0"];
178 * n4 [label="SmTextNode: A (5)"];
179 * n3 -> n5 [label="1"];
180 * n5 [label="SmMathSymbolNode: + (6)"];
181 * n3 -> n6 [label="2"];
182 * n6 [label="SmBinVerNode (7)"];
183 * n6 -> n7 [label="0"];
184 * n7 [label="SmExpressionNode (8)"];
185 * n7 -> n8 [label="0"];
186 * n8 [label="SmBinHorNode (9)"];
187 * n8 -> n9 [label="0"];
188 * n9 [label="SmTextNode: B (10)"];
189 * n8 -> n10 [label="1"];
190 * n10 [label="SmMathSymbolNode: · (11)"];
191 * n8 -> n11 [label="2"];
192 * n11 [label="SmTextNode: C (12)"];
193 * n6 -> n12 [label="1"];
194 * n12 [label="SmRectangleNode (13)"];
195 * n6 -> n13 [label="2"];
196 * n13 [label="SmTextNode: D (14)"];
200 * The vertices are nodes, their label says what kind of node and the number in parentheses is
201 * the identifier of the node (In practices a pointer is used instead of the id). The direction
202 * of the edges tells which node is parent and which is child. The label of the edges are the
203 * child node index number, given to SmNode::GetSubNode() of the parent to get the child node.
206 * \subsection visual_lines Visual Lines
208 * Inorder to do caret movement in visual lines, we need a definition of caret position and
209 * visual line. In a tree such as the above there are three visual lines. There's the outer most
210 * line, with entries such as
211 * \f$\mbox{A}\f$, \f$ + \f$ and \f$ \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$. Then there's
212 * the numerator line of the fraction it has entries \f$ \mbox{B} \f$, \f$ \cdot \f$ and \f$ \mbox{C} \f$.
213 * And last by not least there's the denominator line of the fraction it's only entry is \f$ \mbox{D} \f$.
215 * For visual editing it should be possible to place a caret on both sides of any line entry,
216 * consider a line entry a character or construction that in a line is treated as a character.
217 * Imagine the caret is placed to the right of the plus sign (id: 6), now if user presses
218 * backspace this should delete the plus sign (id: 6), and if the user presses delete this
219 * should delete the entire fraction (id: 7). This is because the caret is in the outer most
220 * line where the fraction is considered a line entry.
222 * However, inorder to prevent users from accidentally deleting large subtrees, just because
223 * they logically placed there caret a in the wrong line, require that complex constructions
224 * such as a fraction is selected before it is deleted. Thus in this case it wouldn't be
225 * deleted, but only selected and then deleted if the user hit delete again. Anyway, this is
226 * slightly off topic for now.
228 * Important about visual lines is that they don't always have an SmExpressionNode as root
229 * and the entries in a visual line is all the nodes of a subtree ordered left to right that
230 * isn't either an SmExpressionNode, SmBinHorNode or SmUnHorNode.
233 * \subsection caret_positions Caret Positions
235 * A caret position in OpenOffice Math is representated by an instance of SmCaretPos.
236 * That is a caret position is a node and an index related to this node. For most nodes the
237 * index 0, means caret is in front of this node, the index 1 means caret is after this node.
238 * For SmTextNode the index is the caret position after the specified number of characters,
239 * imagine an SmTextNode with the number 1337. The index 3 in such SmTextNode would mean a
240 * caret placed right before 7, e.g. "133|7".
242 * For SmExpressionNode, SmBinHorNode and SmUnHorNode the only legal index is 0, which means
243 * in front of the node. Actually the index 0 may only because for the first caret position
244 * in a visual line. From the example above, consider the following subtree that constitutes
250 * label= "Subtree that constitutes a visual line";
252 * n7 [label="SmExpressionNode (8)"];
253 * n7 -> n8 [label="0"];
254 * n8 [label="SmBinHorNode (9)"];
255 * n8 -> n9 [label="0"];
256 * n9 [label="SmTextNode: B (10)"];
257 * n8 -> n10 [label="1"];
258 * n10 [label="SmMathSymbolNode: · (11)"];
259 * n8 -> n11 [label="2"];
260 * n11 [label="SmTextNode: C (12)"];
263 * Here the caret positions are:
266 * <TR><TD><B>Caret position:</B></TD><TD><B>Example:</B></TD>
268 * <TD>{id: 8, index: 0}</TD>
269 * <TD>\f$ \mid \mbox{C} \cdot \mbox{C} \f$</TD>
271 * <TD>{id: 10, index: 1}</TD>
272 * <TD>\f$ \mbox{C} \mid \cdot \mbox{C} \f$</TD>
274 * <TD>{id: 11, index: 1}</TD>
275 * <TD>\f$ \mbox{C} \cdot \mid \mbox{C} \f$</TD>
277 * <TD>{id: 12, index: 1}</TD>
278 * <TD>\f$ \mbox{C} \cdot \mbox{C} \mid \f$</TD>
282 * Where \f$ \mid \f$ is used to denote caret position.
284 * With these exceptions included in the definition the id and index: {id: 11, index: 0} does
285 * \b not constitute a caret position in the given context. Note the method
286 * SmCaretPos::IsValid() does not check if this invariant holds true, but code in SmCaret,
287 * SmSetSelectionVisitor and other places depends on this invariant to hold.
290 * \subsection caret_movement Caret Movement
292 * As the placement of caret positions depends very much on the context within which a node
293 * appears it is not trivial to find all caret positions and determine which follows which.
294 * In OpenOffice Math this is done by the SmCaretPosGraphBuildingVisitor. This visitor builds
295 * graph (an instance of SmCaretPosGraph) over the caret positions. For details on how this
296 * graph is build, and how new methods should be implemented see SmCaretPosGraphBuildingVisitor.
298 * The result of the SmCaretPosGraphBuildingVisitor is a graph over the caret positions in a
299 * formula, representated by an instance of SmCaretPosGraph. Each entry (instances of SmCaretPosGraphEntry)
300 * has a pointer to the entry to the left and right of itself. This way we can easily find
301 * the caret position to a right or left of a given caret position. Note each caret position
302 * only appears once in this graph.
304 * When searching for a caret position after a left click on the formula this map is also used.
305 * We simply iterate over all entries, uses the SmCaretPos2LineVisitor to find a line for each
306 * caret position. Then the distance from the click to the line is computed and we choose the
307 * caret position closest to the click.
309 * For up and down movement, we also iterator over all caret positions and use SmCaretPos2LineVisitor
310 * to find a line for each caret position. Then we compute the distance from the current
311 * caret position to every other caret position and chooses the one closest that is either
312 * above or below the current caret position, depending on whether we're doing up or down movement.
314 * This result of this approach to caret movement is that we have logically predictable
315 * movement for left and right, whilst leftclick, up and down movement depends on the sizes
316 * and placement of all node and may be less logically predictable. This solution also means
317 * that we only have one complex visitor generating the graph, imagine the nightmare if we
318 * had a visitor for movement in each direction.
320 * Making up and down movement independent of node sizes and placement wouldn't necessarily
321 * be a good thing either. Consider the formula \f$ \frac{1+2+3+4+5}{6} \f$, if the caret is
322 * placed as displayed here: \f$ \frac{1+2+3+4+5}{6 \mid} \f$, up movement should move to right
323 * after "3": \f$ \frac{1+2+3|+4+5}{6} \f$. However, such a move depends on the sizes and placement
324 * of all nodes in the fraction.
327 * \subsubsection caretpos_graph_example Example of Caret Position Graph
329 * If we consider the formula
330 * \f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$ from \ref formula_trees.
331 * It has the following caret positions:
335 * <TD><B>Caret position:</B></TD>
336 * <TD><B>Example:</B></TD>
338 * <TD>{id: 3, index: 0}</TD>
339 * <TD>\f$ \mid\mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
341 * <TD>{id: 5, index: 1}</TD>
342 * <TD>\f$ \mbox{A}\mid + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
344 * <TD>{id: 6, index: 1}</TD>
345 * <TD>\f$ \mbox{A} + \mid \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
347 * <TD>{id: 8, index: 0}</TD>
348 * <TD>\f$ \mbox{A} + \frac{ \mid \mbox{B} \cdot \mbox{C}}{\mbox{D}} \f$</TD>
350 * <TD>{id: 10, index: 1}</TD>
351 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \mid \cdot \mbox{C}}{\mbox{D}} \f$</TD>
353 * <TD>{id: 11, index: 1}</TD>
354 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mid \mbox{C}}{\mbox{D}} \f$</TD>
356 * <TD>{id: 12, index: 1}</TD>
357 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C} \mid}{\mbox{D}} \f$</TD>
359 * <TD>{id: 14, index: 0}</TD>
360 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mid \mbox{D}} \f$</TD>
362 * <TD>{id: 14, index: 1}</TD>
363 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D} \mid} \f$</TD>
365 * <TD>{id: 7, index: 1}</TD>
366 * <TD>\f$ \mbox{A} + \frac{\mbox{B} \cdot \mbox{C}}{\mbox{D}} \mid \f$</TD>
370 * Below is a directed graph over the caret positions and how you can move between them.
374 * label= "Caret Position Graph";
376 * p0 [label = "{id: 3, index: 0}"];
377 * p0 -> p1 [fontsize = 10.0, label = "right"];
378 * p1 [label = "{id: 5, index: 1}"];
379 * p1 -> p0 [fontsize = 10.0, label = "left"];
380 * p1 -> p2 [fontsize = 10.0, label = "right"];
381 * p2 [label = "{id: 6, index: 1}"];
382 * p2 -> p1 [fontsize = 10.0, label = "left"];
383 * p2 -> p3 [fontsize = 10.0, label = "right"];
384 * p3 [label = "{id: 8, index: 0}"];
385 * p3 -> p2 [fontsize = 10.0, label = "left"];
386 * p3 -> p4 [fontsize = 10.0, label = "right"];
387 * p4 [label = "{id: 10, index: 1}"];
388 * p4 -> p3 [fontsize = 10.0, label = "left"];
389 * p4 -> p5 [fontsize = 10.0, label = "right"];
390 * p5 [label = "{id: 11, index: 1}"];
391 * p5 -> p4 [fontsize = 10.0, label = "left"];
392 * p5 -> p6 [fontsize = 10.0, label = "right"];
393 * p6 [label = "{id: 12, index: 1}"];
394 * p6 -> p5 [fontsize = 10.0, label = "left"];
395 * p6 -> p9 [fontsize = 10.0, label = "right"];
396 * p7 [label = "{id: 14, index: 0}"];
397 * p7 -> p2 [fontsize = 10.0, label = "left"];
398 * p7 -> p8 [fontsize = 10.0, label = "right"];
399 * p8 [label = "{id: 14, index: 1}"];
400 * p8 -> p7 [fontsize = 10.0, label = "left"];
401 * p8 -> p9 [fontsize = 10.0, label = "right"];
402 * p9 [label = "{id: 7, index: 1}"];
403 * p9 -> p6 [fontsize = 10.0, label = "left"];
408 /* TODO: Write documentation about the following keywords:
412 * - Talk about how the visitor does this
414 * Modifying a Visual Line:
415 * - Find top most non-compo of the line (e.g. The subtree that constitutes a line)
416 * - Make the line into a list
417 * - Edit the list, add/remove/modify nodes
418 * - Parse the list back into a subtree
419 * - Insert the new subtree where the old was taken
422 #endif // INCLUDED_STARMATH_INC_CARET_HXX
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */