LibreOffice Module starmath (master) 1
cursor.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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
10#pragma once
11
12#include "caret.hxx"
13
14#include <list>
15
19#define HORIZONTICAL_DISTANCE_FACTOR 10
20
23{
28};
29
32{
42};
43
45enum class SmBracketType
46{
48 Round,
50 Square,
52 Curly,
53};
54
56typedef std::list<SmNode*> SmNodeList;
57
58typedef std::list<std::unique_ptr<SmNode>> SmClipboard;
59
60class SmDocShell;
61
69{
70public:
71 SmCursor(SmNode* tree, SmDocShell* pShell)
72 : mpAnchor(nullptr)
73 , mpPosition(nullptr)
74 , mpTree(tree)
75 , mpDocShell(pShell)
78 {
79 //Build graph
80 BuildGraph();
81 }
82
84 const SmCaretPos& GetPosition() const { return mpPosition->CaretPos; }
85
87 bool HasSelection() const { return mpAnchor != mpPosition; }
88
90 void Move(OutputDevice* pDev, SmMovementDirection direction, bool bMoveAnchor = true);
91
93 void MoveTo(OutputDevice* pDev, const Point& pos, bool bMoveAnchor);
94
96 void Delete();
97
102 void DeletePrev(OutputDevice* pDev);
103
105 void InsertText(const OUString& aString);
106
108 void InsertElement(SmFormulaElement element);
109
118 void InsertCommandText(const OUString& aCommandText);
119
129 void InsertSpecial(std::u16string_view aString);
130
140 void InsertSubSup(SmSubSup eSubSup);
141
152 bool InsertRow();
153
155 void InsertFraction();
156
158 void InsertBrackets(SmBracketType eBracketType);
159
161 void Copy();
163 void Cut()
164 {
165 Copy();
166 Delete();
167 }
169 void Paste();
170
177 bool HasComplexSelection();
178
184 static SmNode* FindTopMostNodeInLine(SmNode* pSNode, bool MoveUpIfSelected = false);
185
187 void Draw(OutputDevice& pDev, Point Offset, bool isCaretVisible);
188
191
192 bool IsAtTailOfBracket(SmBracketType eBracketType) const;
193
194private:
195 friend class SmDocShell;
196
203 std::unique_ptr<SmCaretPosGraph> mpGraph;
206
209
214 static bool IsLineCompositionNode(SmNode const* pNode);
215
223 int CountSelectedNodes(SmNode* pNode);
224
231 static void LineToList(SmStructureNode* pLine, SmNodeList& rList);
232
238 static void NodeToList(SmNode*& rpNode, SmNodeList& rList)
239 {
240 //Remove from parent and NULL rpNode
241 SmNode* pNode = rpNode;
242 if (rpNode && rpNode->GetParent())
243 { //Don't remove this, correctness relies on it
244 int index = rpNode->GetParent()->IndexOfSubNode(rpNode);
245 assert(index >= 0);
246 rpNode->GetParent()->SetSubNode(index, nullptr);
247 }
248 rpNode = nullptr;
249 //Create line from node
250 if (pNode && IsLineCompositionNode(pNode))
251 {
252 LineToList(static_cast<SmStructureNode*>(pNode), rList);
253 return;
254 }
255 if (pNode)
256 rList.push_front(pNode);
257 }
258
264 static void CloneLineToClipboard(SmStructureNode* pLine, SmClipboard* pClipboard);
265
267 void BuildGraph();
268
270 void InsertNodes(std::unique_ptr<SmNodeList> pNewNodes);
271
277
279 void AnnotateSelection() const;
280
282 static std::unique_ptr<SmNodeList> CloneList(SmClipboard& rClipboard);
283
294 static SmNodeList::iterator FindPositionInLineList(SmNodeList* pLineList,
295 const SmCaretPos& rCaretPos);
296
309 static SmCaretPos PatchLineList(SmNodeList* pLineList, SmNodeList::iterator aIter);
310
321 static SmNodeList::iterator TakeSelectedNodesFromList(SmNodeList* pLineList,
322 SmNodeList* pSelectedNodes = nullptr);
323
325 static SmNode* CreateBracket(SmBracketType eBracketType, bool bIsLeft);
326
334 void BeginEdit();
336 void EndEdit();
352 void FinishEdit(std::unique_ptr<SmNodeList> pLineList, SmStructureNode* pParent,
353 int nParentIndex, SmCaretPos PosAfterEdit, SmNode* pStartLine = nullptr);
355 void RequestRepaint();
356};
357
377{
378public:
380 SmNodeListParser() { pList = nullptr; }
385 SmNode* Parse(SmNodeList* list);
387 static bool IsOperator(const SmToken& token);
389 static bool IsRelationOperator(const SmToken& token);
391 static bool IsSumOperator(const SmToken& token);
393 static bool IsProductOperator(const SmToken& token);
395 static bool IsUnaryOperator(const SmToken& token);
397 static bool IsPostfixOperator(const SmToken& token);
398
399private:
403 {
404 if (!pList->empty())
405 return pList->front();
406 return nullptr;
407 }
410 {
411 pList->pop_front();
412 return Terminal();
413 }
416 {
417 SmNode* pRetVal = Terminal();
418 Next();
419 return pRetVal;
420 }
422 SmNode* Relation();
423 SmNode* Sum();
424 SmNode* Product();
425 SmNode* Factor();
426 SmNode* Postfix();
427 static SmNode* Error();
428};
429
430/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Formula cursor.
Definition: cursor.hxx:69
void Delete()
Delete the current selection or do nothing.
Definition: cursor.cxx:266
void InsertElement(SmFormulaElement element)
Insert an element into the formula.
Definition: cursor.cxx:898
static SmNodeList::iterator TakeSelectedNodesFromList(SmNodeList *pLineList, SmNodeList *pSelectedNodes=nullptr)
Take selected nodes from a list.
Definition: cursor.cxx:441
void InsertFraction()
Insert a fraction, use selection as numerator.
Definition: cursor.cxx:818
SmCaretPosGraphEntry * mpPosition
Definition: cursor.hxx:197
const SmCaretPos & GetPosition() const
Get position.
Definition: cursor.hxx:84
void InsertBrackets(SmBracketType eBracketType)
Create brackets around current selection, or new SmPlaceNode.
Definition: cursor.cxx:601
void Copy()
Copy the current selection.
Definition: cursor.cxx:1050
static SmNode * CreateBracket(SmBracketType eBracketType, bool bIsLeft)
Create an instance of SmMathSymbolNode usable for brackets.
Definition: cursor.cxx:665
void DeletePrev(OutputDevice *pDev)
Delete selection, previous element or merge lines.
Definition: cursor.cxx:188
void Draw(OutputDevice &pDev, Point Offset, bool isCaretVisible)
Draw the caret.
Definition: cursor.cxx:173
SmNode * mpTree
Formula tree.
Definition: cursor.hxx:199
static SmNode * FindTopMostNodeInLine(SmNode *pSNode, bool MoveUpIfSelected=false)
Finds the topmost node in a visual line.
Definition: cursor.cxx:1109
SmCursor(SmNode *tree, SmDocShell *pShell)
Definition: cursor.hxx:71
SmCaretPosGraphEntry * mpAnchor
Definition: cursor.hxx:197
bool HasSelection() const
True, if the cursor has a selection.
Definition: cursor.hxx:87
bool IsAtTailOfBracket(SmBracketType eBracketType) const
Definition: cursor.cxx:1348
static std::unique_ptr< SmNodeList > CloneList(SmClipboard &rClipboard)
Clone list of nodes in a clipboard (creates a deep clone)
Definition: cursor.cxx:1097
void InsertNodes(std::unique_ptr< SmNodeList > pNewNodes)
Insert new nodes in the tree after position.
Definition: cursor.cxx:307
int CountSelectedNodes(SmNode *pNode)
Count number of selected nodes, excluding line composition nodes.
Definition: cursor.cxx:1207
void FinishEdit(std::unique_ptr< SmNodeList > pLineList, SmStructureNode *pParent, int nParentIndex, SmCaretPos PosAfterEdit, SmNode *pStartLine=nullptr)
Finish editing.
Definition: cursor.cxx:1230
void EndEdit()
End edit section where the tree will be modified.
Definition: cursor.cxx:1301
void MoveTo(OutputDevice *pDev, const Point &pos, bool bMoveAnchor)
Move to the caret position closest to a given point.
Definition: cursor.cxx:83
static void NodeToList(SmNode *&rpNode, SmNodeList &rList)
Auxiliary function for calling LineToList on a node.
Definition: cursor.hxx:238
SmClipboard maClipboard
Clipboard holder.
Definition: cursor.hxx:205
void Cut()
Cut the current selection.
Definition: cursor.hxx:163
void Paste()
Paste the clipboard.
Definition: cursor.cxx:1087
SmNode * FindSelectedNode(SmNode *pNode)
Returns a node that is selected, if any could be found.
Definition: cursor.cxx:1128
static void CloneLineToClipboard(SmStructureNode *pLine, SmClipboard *pClipboard)
Clone a visual line to a clipboard.
Definition: cursor.cxx:1169
void BuildGraph()
Build pGraph over caret positions.
Definition: cursor.cxx:113
SmDocShell * mpDocShell
Owner of the formula tree.
Definition: cursor.hxx:201
void RequestRepaint()
Request the formula is repainted.
Definition: cursor.cxx:1333
bool SetCaretPosition(SmCaretPos pos)
tries to set position to a specific SmCaretPos
Definition: cursor.cxx:155
int mnEditSections
The number of times BeginEdit have been called Used to allow nesting of BeginEdit() and EndEdit() sec...
Definition: cursor.hxx:330
bool HasComplexSelection()
Returns true if more than one node is selected.
Definition: cursor.cxx:1222
static bool IsLineCompositionNode(SmNode const *pNode)
Is this one of the nodes used to compose a line.
Definition: cursor.cxx:1193
std::unique_ptr< SmCaretPosGraph > mpGraph
Graph over caret position in the current tree.
Definition: cursor.hxx:203
tools::Rectangle GetCaretRectangle(OutputDevice &rOutDev) const
Definition: cursor.cxx:177
void InsertSubSup(SmSubSup eSubSup)
Create sub-/super script.
Definition: cursor.cxx:498
void InsertCommandText(const OUString &aCommandText)
Insert command text translated into line entries at position.
Definition: cursor.cxx:1027
static SmNodeList::iterator FindPositionInLineList(SmNodeList *pLineList, const SmCaretPos &rCaretPos)
Find an iterator pointing to the node in pLineList following rCaretPos.
Definition: cursor.cxx:354
bool InsertRow()
Insert a new row or newline.
Definition: cursor.cxx:697
void BeginEdit()
Begin edit section where the tree will be modified.
Definition: cursor.cxx:1293
bool mbIsEnabledSetModifiedSmDocShell
Holds data for BeginEdit() and EndEdit()
Definition: cursor.hxx:332
static void LineToList(SmStructureNode *pLine, SmNodeList &rList)
Convert a visual line to a list.
Definition: cursor.cxx:1144
static SmCaretPos PatchLineList(SmNodeList *pLineList, SmNodeList::iterator aIter)
Patch a line list after modification, merge SmTextNode, remove SmPlaceNode etc.
Definition: cursor.cxx:388
void AnnotateSelection() const
Set selected on nodes of the tree.
Definition: cursor.cxx:168
void InsertText(const OUString &aString)
Insert text at the current position.
Definition: cursor.cxx:873
void Move(OutputDevice *pDev, SmMovementDirection direction, bool bMoveAnchor=true)
Move the position of this cursor.
Definition: cursor.cxx:20
tools::Rectangle GetSelectionRectangle(OutputDevice &rOutDev) const
Definition: cursor.cxx:182
void InsertSpecial(std::u16string_view aString)
Insert a special node created from aString.
Definition: cursor.cxx:1000
Minimalistic recursive decent SmNodeList parser.
Definition: cursor.hxx:377
SmNode * Postfix()
Definition: cursor.cxx:1520
SmNode * Parse(SmNodeList *list)
Parse a list of nodes to an expression.
Definition: cursor.cxx:1418
SmNode * Product()
Definition: cursor.cxx:1481
SmNode * Expression()
Definition: cursor.cxx:1435
static bool IsOperator(const SmToken &token)
True, if the token is an operator.
Definition: cursor.cxx:1543
static bool IsRelationOperator(const SmToken &token)
True, if the token is a relation operator.
Definition: cursor.cxx:1551
SmNode * Sum()
Definition: cursor.cxx:1464
SmNode * Next()
Move to next terminal.
Definition: cursor.hxx:409
SmNode * Relation()
Definition: cursor.cxx:1447
static SmNode * Error()
Definition: cursor.cxx:1539
SmNode * Take()
Take the current terminal.
Definition: cursor.hxx:415
static bool IsProductOperator(const SmToken &token)
True, if the token is a product operator.
Definition: cursor.cxx:1559
static bool IsUnaryOperator(const SmToken &token)
True, if the token is a unary operator.
Definition: cursor.cxx:1568
SmNodeListParser()
Create an instance of SmNodeListParser.
Definition: cursor.hxx:380
SmNodeList * pList
Definition: cursor.hxx:400
static bool IsSumOperator(const SmToken &token)
True, if the token is a sum operator.
Definition: cursor.cxx:1555
static bool IsPostfixOperator(const SmToken &token)
True, if the token is a postfix operator.
Definition: cursor.cxx:1578
SmNode * Factor()
Definition: cursor.cxx:1498
SmNode * Terminal()
Get the current terminal.
Definition: cursor.hxx:402
Definition: node.hxx:125
const SmStructureNode * GetParent() const
Gets the parent node of this node.
Definition: node.hxx:443
Abstract baseclass for all composite node.
Definition: node.hxx:471
int IndexOfSubNode(SmNode const *pSubNode)
Get the index of the child node pSubNode.
Definition: node.cxx:468
void SetSubNode(size_t nIndex, SmNode *pNode)
Sets the subnode pNode at nIndex.
Definition: node.cxx:477
SmFormulaElement
Enum of elements that can inserted into a formula.
Definition: cursor.hxx:32
@ FactorialElement
Definition: cursor.hxx:34
@ CDotElement
Definition: cursor.hxx:37
@ MinusElement
Definition: cursor.hxx:36
@ BlankElement
Definition: cursor.hxx:33
@ GreaterThanElement
Definition: cursor.hxx:40
@ EqualElement
Definition: cursor.hxx:38
@ PlusElement
Definition: cursor.hxx:35
@ LessThanElement
Definition: cursor.hxx:39
@ PercentElement
Definition: cursor.hxx:41
std::list< SmNode * > SmNodeList
A list of nodes.
Definition: cursor.hxx:56
std::list< std::unique_ptr< SmNode > > SmClipboard
Definition: cursor.hxx:58
SmBracketType
Bracket types that can be inserted.
Definition: cursor.hxx:46
@ Curly
Curly brackets, left command "lbrace".
SmMovementDirection
Enum of direction for movement.
Definition: cursor.hxx:23
@ MoveDown
Definition: cursor.hxx:25
@ MoveRight
Definition: cursor.hxx:27
@ MoveUp
Definition: cursor.hxx:24
@ MoveLeft
Definition: cursor.hxx:26
index
SmSubSup
Enum used to index sub-/supscripts in the 'maSubNodes' array in 'SmSubSupNode'.
Definition: node.hxx:1536
const char Round[]
An entry in SmCaretPosGraph.
Definition: caret.hxx:111
const SmCaretPos CaretPos
Caret position.
Definition: caret.hxx:119
Representation of caret position with an equation.
Definition: caret.hxx:17