bump product version to 7.2.5.1
[LibreOffice.git] / starmath / inc / visitors.hxx
blobeac047133ebf89abf997f5f93748b3afc44fceff
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 */
10 /** Visitors are an easy way to automating operations with nodes.
12 * The available visitors are:
13 * SmVisitor base class
14 * SmDefaultingVisitor default visitor
15 * SmDrawingVisitor draws formula
16 * SmCaretPosGraphBuildingVisitor position of the node inside starmath code
17 * SmCloningVisitor duplicate nodes
18 * SmNodeToTextVisitor create code from nodes
22 #pragma once
24 #include <sal/config.h>
25 #include <sal/log.hxx>
27 #include "node.hxx"
28 #include "caret.hxx"
30 /** Base class for visitors that visits a tree of SmNodes
31 * @remarks all methods have been left abstract to ensure that implementers
32 * don't forget to implement one.
34 class SmVisitor
36 public:
37 virtual void Visit( SmTableNode* pNode ) = 0;
38 virtual void Visit( SmBraceNode* pNode ) = 0;
39 virtual void Visit( SmBracebodyNode* pNode ) = 0;
40 virtual void Visit( SmOperNode* pNode ) = 0;
41 virtual void Visit( SmAlignNode* pNode ) = 0;
42 virtual void Visit( SmAttributeNode* pNode ) = 0;
43 virtual void Visit( SmFontNode* pNode ) = 0;
44 virtual void Visit( SmUnHorNode* pNode ) = 0;
45 virtual void Visit( SmBinHorNode* pNode ) = 0;
46 virtual void Visit( SmBinVerNode* pNode ) = 0;
47 virtual void Visit( SmBinDiagonalNode* pNode ) = 0;
48 virtual void Visit( SmSubSupNode* pNode ) = 0;
49 virtual void Visit( SmMatrixNode* pNode ) = 0;
50 virtual void Visit( SmPlaceNode* pNode ) = 0;
51 virtual void Visit( SmTextNode* pNode ) = 0;
52 virtual void Visit( SmSpecialNode* pNode ) = 0;
53 virtual void Visit( SmGlyphSpecialNode* pNode ) = 0;
54 virtual void Visit( SmMathSymbolNode* pNode ) = 0;
55 virtual void Visit( SmBlankNode* pNode ) = 0;
56 virtual void Visit( SmErrorNode* pNode ) = 0;
57 virtual void Visit( SmLineNode* pNode ) = 0;
58 virtual void Visit( SmExpressionNode* pNode ) = 0;
59 virtual void Visit( SmPolyLineNode* pNode ) = 0;
60 virtual void Visit( SmRootNode* pNode ) = 0;
61 virtual void Visit( SmRootSymbolNode* pNode ) = 0;
62 virtual void Visit( SmRectangleNode* pNode ) = 0;
63 virtual void Visit( SmVerticalBraceNode* pNode ) = 0;
65 protected:
66 ~SmVisitor() {}
69 // SmDefaultingVisitor
72 /** Visitor that uses DefaultVisit for handling visits by default
74 * This abstract baseclass is useful for visitors where many methods share the same
75 * implementation.
77 class SmDefaultingVisitor : public SmVisitor
79 public:
80 void Visit( SmTableNode* pNode ) override;
81 void Visit( SmBraceNode* pNode ) override;
82 void Visit( SmBracebodyNode* pNode ) override;
83 void Visit( SmOperNode* pNode ) override;
84 void Visit( SmAlignNode* pNode ) override;
85 void Visit( SmAttributeNode* pNode ) override;
86 void Visit( SmFontNode* pNode ) override;
87 void Visit( SmUnHorNode* pNode ) override;
88 void Visit( SmBinHorNode* pNode ) override;
89 void Visit( SmBinVerNode* pNode ) override;
90 void Visit( SmBinDiagonalNode* pNode ) override;
91 void Visit( SmSubSupNode* pNode ) override;
92 void Visit( SmMatrixNode* pNode ) override;
93 void Visit( SmPlaceNode* pNode ) override;
94 void Visit( SmTextNode* pNode ) override;
95 void Visit( SmSpecialNode* pNode ) override;
96 void Visit( SmGlyphSpecialNode* pNode ) override;
97 void Visit( SmMathSymbolNode* pNode ) override;
98 void Visit( SmBlankNode* pNode ) override;
99 void Visit( SmErrorNode* pNode ) override;
100 void Visit( SmLineNode* pNode ) override;
101 void Visit( SmExpressionNode* pNode ) override;
102 void Visit( SmPolyLineNode* pNode ) override;
103 void Visit( SmRootNode* pNode ) override;
104 void Visit( SmRootSymbolNode* pNode ) override;
105 void Visit( SmRectangleNode* pNode ) override;
106 void Visit( SmVerticalBraceNode* pNode ) override;
107 protected:
108 ~SmDefaultingVisitor() {}
110 /** Method invoked by Visit methods by default */
111 virtual void DefaultVisit( SmNode* pNode ) = 0;
114 // SmCaretDrawingVisitor
116 /** Visitor for drawing a caret position */
117 class SmCaretDrawingVisitor final : public SmDefaultingVisitor
119 public:
120 /** Given position and device this constructor will draw the caret */
121 SmCaretDrawingVisitor( OutputDevice& rDevice, SmCaretPos position, Point offset, bool caretVisible );
122 virtual ~SmCaretDrawingVisitor() {}
123 void Visit( SmTextNode* pNode ) override;
124 using SmDefaultingVisitor::Visit;
125 private:
126 OutputDevice &mrDev;
127 SmCaretPos maPos;
128 /** Offset to draw from */
129 Point maOffset;
130 bool mbCaretVisible;
132 /** Default method for drawing pNodes */
133 void DefaultVisit( SmNode* pNode ) override;
136 // SmCaretPos2LineVisitor
138 /** Visitor getting a line from a caret position */
139 class SmCaretPos2LineVisitor final : public SmDefaultingVisitor
141 public:
142 /** Given position and device this constructor will compute a line for the caret */
143 SmCaretPos2LineVisitor( OutputDevice *pDevice, SmCaretPos position )
144 : mpDev( pDevice )
145 , maPos( position )
147 SAL_WARN_IF( !position.IsValid(), "starmath", "Cannot draw invalid position!" );
149 maPos.pSelectedNode->Accept( this );
151 virtual ~SmCaretPos2LineVisitor() {}
152 void Visit( SmTextNode* pNode ) override;
153 using SmDefaultingVisitor::Visit;
154 const SmCaretLine& GetResult( ) const {
155 return maLine;
157 private:
158 SmCaretLine maLine;
159 VclPtr<OutputDevice> mpDev;
160 SmCaretPos maPos;
162 /** Default method for computing lines for pNodes */
163 void DefaultVisit( SmNode* pNode ) override;
166 // SmDrawingVisitor
168 /** Visitor for drawing SmNodes to OutputDevice */
169 class SmDrawingVisitor final : public SmVisitor
171 public:
172 /** Create an instance of SmDrawingVisitor, and use it to draw a formula
173 * @param rDevice Device to draw on
174 * @param position Offset on device to draw the formula
175 * @param pTree Formula tree to draw
176 * @remarks This constructor will do the drawing, no need to anything more.
178 SmDrawingVisitor( OutputDevice &rDevice, Point position, SmNode* pTree )
179 : mrDev( rDevice )
180 , maPosition( position )
182 pTree->Accept( this );
184 virtual ~SmDrawingVisitor() {}
185 void Visit( SmTableNode* pNode ) override;
186 void Visit( SmBraceNode* pNode ) override;
187 void Visit( SmBracebodyNode* pNode ) override;
188 void Visit( SmOperNode* pNode ) override;
189 void Visit( SmAlignNode* pNode ) override;
190 void Visit( SmAttributeNode* pNode ) override;
191 void Visit( SmFontNode* pNode ) override;
192 void Visit( SmUnHorNode* pNode ) override;
193 void Visit( SmBinHorNode* pNode ) override;
194 void Visit( SmBinVerNode* pNode ) override;
195 void Visit( SmBinDiagonalNode* pNode ) override;
196 void Visit( SmSubSupNode* pNode ) override;
197 void Visit( SmMatrixNode* pNode ) override;
198 void Visit( SmPlaceNode* pNode ) override;
199 void Visit( SmTextNode* pNode ) override;
200 void Visit( SmSpecialNode* pNode ) override;
201 void Visit( SmGlyphSpecialNode* pNode ) override;
202 void Visit( SmMathSymbolNode* pNode ) override;
203 void Visit( SmBlankNode* pNode ) override;
204 void Visit( SmErrorNode* pNode ) override;
205 void Visit( SmLineNode* pNode ) override;
206 void Visit( SmExpressionNode* pNode ) override;
207 void Visit( SmPolyLineNode* pNode ) override;
208 void Visit( SmRootNode* pNode ) override;
209 void Visit( SmRootSymbolNode* pNode ) override;
210 void Visit( SmRectangleNode* pNode ) override;
211 void Visit( SmVerticalBraceNode* pNode ) override;
212 private:
213 /** Draw the children of a pNode
214 * This the default method, use by most pNodes
216 void DrawChildren( SmStructureNode* pNode );
218 /** Draw an SmTextNode or a subclass of this */
219 void DrawTextNode( SmTextNode* pNode );
220 /** Draw an SmSpecialNode or a subclass of this */
221 void DrawSpecialNode( SmSpecialNode* pNode );
222 /** OutputDevice to draw on */
223 OutputDevice& mrDev;
224 /** Position to draw on the mrDev
225 * @remarks This variable is used to pass parameters in DrawChildren( ), this means
226 that after a call to DrawChildren( ) the contents of this method is undefined
227 so if needed cache it locally on the stack.
229 Point maPosition;
232 // SmSetSelectionVisitor
234 /** Set Selection Visitor
235 * Sets the IsSelected( ) property on all SmNodes of the tree
237 class SmSetSelectionVisitor final : public SmDefaultingVisitor
239 public:
240 SmSetSelectionVisitor( SmCaretPos startPos, SmCaretPos endPos, SmNode* pNode);
241 virtual ~SmSetSelectionVisitor() {}
242 void Visit( SmBinHorNode* pNode ) override;
243 void Visit( SmUnHorNode* pNode ) override;
244 void Visit( SmFontNode* pNode ) override;
245 void Visit( SmTextNode* pNode ) override;
246 void Visit( SmExpressionNode* pNode ) override;
247 void Visit( SmLineNode* pNode ) override;
248 void Visit( SmAlignNode* pNode ) override;
249 using SmDefaultingVisitor::Visit;
250 /** Set IsSelected on all pNodes of pSubTree */
251 static void SetSelectedOnAll( SmNode* pSubTree, bool IsSelected = true );
252 private:
253 /** Visit a selectable pNode
254 * Can be used to handle pNodes that can be selected, that doesn't have more SmCaretPos'
255 * than 0 and 1 inside them. SmTextNode should be handle separately!
256 * Also note that pNodes such as SmBinVerNode cannot be selected, don't this method for
257 * it.
259 void DefaultVisit( SmNode* pNode ) override;
260 void VisitCompositionNode( SmStructureNode* pNode );
261 /** Caret position where the selection starts */
262 SmCaretPos maStartPos;
263 /** Caret position where the selection ends */
264 SmCaretPos maEndPos;
265 /** The current state of this visitor
266 * This property changes when the visitor meets either maStartPos
267 * or maEndPos. This means that anything visited in between will be
268 * selected.
270 bool mbSelecting;
274 // SmCaretPosGraphBuildingVisitor
277 /** A visitor for building a SmCaretPosGraph
279 * Visit invariant:
280 * Each pNode, except SmExpressionNode, SmBinHorNode and a few others, constitutes an entry
281 * in a line. Consider the line entry "H", this entry creates one carat position, here
282 * denoted by | in "H|".
284 * Parameter variables:
285 * The following variables are used to transfer parameters into calls and results out
286 * of calls.
287 * pRightMost : SmCaretPosGraphEntry*
289 * Prior to a Visit call:
290 * pRightMost: A pointer to right most position in front of the current line entry.
292 * After a Visit call:
293 * pRightMost: A pointer to the right most position in the called line entry, if no there's
294 * no caret positions in called line entry don't change this variable.
296 class SmCaretPosGraphBuildingVisitor final : public SmVisitor
298 public:
299 /** Builds a caret position graph for pRootNode */
300 explicit SmCaretPosGraphBuildingVisitor( SmNode* pRootNode );
301 virtual ~SmCaretPosGraphBuildingVisitor();
302 void Visit( SmTableNode* pNode ) override;
303 void Visit( SmBraceNode* pNode ) override;
304 void Visit( SmBracebodyNode* pNode ) override;
305 void Visit( SmOperNode* pNode ) override;
306 void Visit( SmAlignNode* pNode ) override;
307 void Visit( SmAttributeNode* pNode ) override;
308 void Visit( SmFontNode* pNode ) override;
309 void Visit( SmUnHorNode* pNode ) override;
310 void Visit( SmBinHorNode* pNode ) override;
311 void Visit( SmBinVerNode* pNode ) override;
312 void Visit( SmBinDiagonalNode* pNode ) override;
313 void Visit( SmSubSupNode* pNode ) override;
314 void Visit( SmMatrixNode* pNode ) override;
315 void Visit( SmPlaceNode* pNode ) override;
316 void Visit( SmTextNode* pNode ) override;
317 void Visit( SmSpecialNode* pNode ) override;
318 void Visit( SmGlyphSpecialNode* pNode ) override;
319 void Visit( SmMathSymbolNode* pNode ) override;
320 void Visit( SmBlankNode* pNode ) override;
321 void Visit( SmErrorNode* pNode ) override;
322 void Visit( SmLineNode* pNode ) override;
323 void Visit( SmExpressionNode* pNode ) override;
324 void Visit( SmPolyLineNode* pNode ) override;
325 void Visit( SmRootNode* pNode ) override;
326 void Visit( SmRootSymbolNode* pNode ) override;
327 void Visit( SmRectangleNode* pNode ) override;
328 void Visit( SmVerticalBraceNode* pNode ) override;
329 SmCaretPosGraph* takeGraph()
331 return mpGraph.release();
333 private:
334 SmCaretPosGraphEntry* mpRightMost;
335 std::unique_ptr<SmCaretPosGraph> mpGraph;
338 // SmCloningVisitor
340 /** Visitor for cloning a pNode
342 * This visitor creates deep clones.
344 class SmCloningVisitor final : public SmVisitor
346 public:
347 SmCloningVisitor()
348 : mpResult(nullptr)
350 virtual ~SmCloningVisitor() {}
351 void Visit( SmTableNode* pNode ) override;
352 void Visit( SmBraceNode* pNode ) override;
353 void Visit( SmBracebodyNode* pNode ) override;
354 void Visit( SmOperNode* pNode ) override;
355 void Visit( SmAlignNode* pNode ) override;
356 void Visit( SmAttributeNode* pNode ) override;
357 void Visit( SmFontNode* pNode ) override;
358 void Visit( SmUnHorNode* pNode ) override;
359 void Visit( SmBinHorNode* pNode ) override;
360 void Visit( SmBinVerNode* pNode ) override;
361 void Visit( SmBinDiagonalNode* pNode ) override;
362 void Visit( SmSubSupNode* pNode ) override;
363 void Visit( SmMatrixNode* pNode ) override;
364 void Visit( SmPlaceNode* pNode ) override;
365 void Visit( SmTextNode* pNode ) override;
366 void Visit( SmSpecialNode* pNode ) override;
367 void Visit( SmGlyphSpecialNode* pNode ) override;
368 void Visit( SmMathSymbolNode* pNode ) override;
369 void Visit( SmBlankNode* pNode ) override;
370 void Visit( SmErrorNode* pNode ) override;
371 void Visit( SmLineNode* pNode ) override;
372 void Visit( SmExpressionNode* pNode ) override;
373 void Visit( SmPolyLineNode* pNode ) override;
374 void Visit( SmRootNode* pNode ) override;
375 void Visit( SmRootSymbolNode* pNode ) override;
376 void Visit( SmRectangleNode* pNode ) override;
377 void Visit( SmVerticalBraceNode* pNode ) override;
378 /** Clone a pNode */
379 SmNode* Clone( SmNode* pNode );
380 private:
381 SmNode* mpResult;
382 /** Clone children of pSource and give them to pTarget */
383 void CloneKids( SmStructureNode* pSource, SmStructureNode* pTarget );
384 /** Clone attributes on a pNode */
385 static void CloneNodeAttr( SmNode const * pSource, SmNode* pTarget );
389 // SmSelectionDrawingVisitor
391 class SmSelectionDrawingVisitor final : public SmDefaultingVisitor
393 public:
394 /** Draws a selection on rDevice for the selection on pTree */
395 SmSelectionDrawingVisitor( OutputDevice& rDevice, SmNode* pTree, const Point& rOffset );
396 virtual ~SmSelectionDrawingVisitor() {}
397 void Visit( SmTextNode* pNode ) override;
398 using SmDefaultingVisitor::Visit;
399 private:
400 /** Reference to drawing device */
401 OutputDevice& mrDev;
402 /** True if aSelectionArea have been initialized */
403 bool mbHasSelectionArea;
404 /** The current area that is selected */
405 tools::Rectangle maSelectionArea;
406 /** Extend the area that must be selected */
407 void ExtendSelectionArea(const tools::Rectangle& rArea);
408 /** Default visiting method */
409 void DefaultVisit( SmNode* pNode ) override;
410 /** Visit the children of a given pNode */
411 void VisitChildren( SmNode* pNode );
414 // SmNodeToTextVisitor
416 /** Extract command text from pNodes */
417 class SmNodeToTextVisitor final : public SmVisitor
419 public:
420 SmNodeToTextVisitor( SmNode* pNode, OUString &rText );
421 virtual ~SmNodeToTextVisitor() {}
423 void Visit( SmTableNode* pNode ) override;
424 void Visit( SmBraceNode* pNode ) override;
425 void Visit( SmBracebodyNode* pNode ) override;
426 void Visit( SmOperNode* pNode ) override;
427 void Visit( SmAlignNode* pNode ) override;
428 void Visit( SmAttributeNode* pNode ) override;
429 void Visit( SmFontNode* pNode ) override;
430 void Visit( SmUnHorNode* pNode ) override;
431 void Visit( SmBinHorNode* pNode ) override;
432 void Visit( SmBinVerNode* pNode ) override;
433 void Visit( SmBinDiagonalNode* pNode ) override;
434 void Visit( SmSubSupNode* pNode ) override;
435 void Visit( SmMatrixNode* pNode ) override;
436 void Visit( SmPlaceNode* pNode ) override;
437 void Visit( SmTextNode* pNode ) override;
438 void Visit( SmSpecialNode* pNode ) override;
439 void Visit( SmGlyphSpecialNode* pNode ) override;
440 void Visit( SmMathSymbolNode* pNode ) override;
441 void Visit( SmBlankNode* pNode ) override;
442 void Visit( SmErrorNode* pNode ) override;
443 void Visit( SmLineNode* pNode ) override;
444 void Visit( SmExpressionNode* pNode ) override;
445 void Visit( SmPolyLineNode* pNode ) override;
446 void Visit( SmRootNode* pNode ) override;
447 void Visit( SmRootSymbolNode* pNode ) override;
448 void Visit( SmRectangleNode* pNode ) override;
449 void Visit( SmVerticalBraceNode* pNode ) override;
450 private:
453 * Extract text from a pNode that constitutes a line.
454 * @param pNode
455 * @return
457 void LineToText( SmNode* pNode ) {
458 Separate( );
459 if( pNode ) pNode->Accept( this );
460 Separate( );
464 * Appends rText to the OUStringBuffer ( maCmdText ).
465 * @param rText
466 * @return
468 void Append( std::u16string_view rText ) {
469 maCmdText.append( rText );
473 * Appends rText to the OUStringBuffer ( maCmdText ).
474 * @param rText
475 * @return
477 void Append( const char* rText ) {
478 maCmdText.append( OUString::createFromAscii(rText) );
482 * Append a blank for separation, if needed.
483 * It is needed if last char is not ' '.
484 * @return
486 void Separate( ){
487 if( !maCmdText.isEmpty() && maCmdText[ maCmdText.getLength() - 1 ] != ' ' )
488 maCmdText.append(' ');
491 /** Output text generated from the pNodes */
492 OUStringBuffer maCmdText;
495 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */