summaryrefslogtreecommitdiff
path: root/starmath/inc/visitors.hxx
blob: bb4ee155fc3adb6856b43456b582b8ee5803c374 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#ifndef SMVISITORS_H
#define SMVISITORS_H

#include "node.hxx"
#include "caret.hxx"

/** Base class for visitors that visits a tree of SmNodes
 * @remarks all methods have been left abstract to ensure that implementers
 * don't forget to implement one.
 */
class SmVisitor
{
public:
    virtual void Visit( SmTableNode* pNode ) = 0;
    virtual void Visit( SmBraceNode* pNode ) = 0;
    virtual void Visit( SmBracebodyNode* pNode ) = 0;
    virtual void Visit( SmOperNode* pNode ) = 0;
    virtual void Visit( SmAlignNode* pNode ) = 0;
    virtual void Visit( SmAttributNode* pNode ) = 0;
    virtual void Visit( SmFontNode* pNode ) = 0;
    virtual void Visit( SmUnHorNode* pNode ) = 0;
    virtual void Visit( SmBinHorNode* pNode ) = 0;
    virtual void Visit( SmBinVerNode* pNode ) = 0;
    virtual void Visit( SmBinDiagonalNode* pNode ) = 0;
    virtual void Visit( SmSubSupNode* pNode ) = 0;
    virtual void Visit( SmMatrixNode* pNode ) = 0;
    virtual void Visit( SmPlaceNode* pNode ) = 0;
    virtual void Visit( SmTextNode* pNode ) = 0;
    virtual void Visit( SmSpecialNode* pNode ) = 0;
    virtual void Visit( SmGlyphSpecialNode* pNode ) = 0;
    virtual void Visit( SmMathSymbolNode* pNode ) = 0;
    virtual void Visit( SmBlankNode* pNode ) = 0;
    virtual void Visit( SmErrorNode* pNode ) = 0;
    virtual void Visit( SmLineNode* pNode ) = 0;
    virtual void Visit( SmExpressionNode* pNode ) = 0;
    virtual void Visit( SmPolyLineNode* pNode ) = 0;
    virtual void Visit( SmRootNode* pNode ) = 0;
    virtual void Visit( SmRootSymbolNode* pNode ) = 0;
    virtual void Visit( SmRectangleNode* pNode ) = 0;
    virtual void Visit( SmVerticalBraceNode* pNode ) = 0;
};

/** Simple visitor for testing SmVisitor */
class SmVisitorTest : public SmVisitor
{
public:
    void Visit( SmTableNode* pNode );
    void Visit( SmBraceNode* pNode );
    void Visit( SmBracebodyNode* pNode );
    void Visit( SmOperNode* pNode );
    void Visit( SmAlignNode* pNode );
    void Visit( SmAttributNode* pNode );
    void Visit( SmFontNode* pNode );
    void Visit( SmUnHorNode* pNode );
    void Visit( SmBinHorNode* pNode );
    void Visit( SmBinVerNode* pNode );
    void Visit( SmBinDiagonalNode* pNode );
    void Visit( SmSubSupNode* pNode );
    void Visit( SmMatrixNode* pNode );
    void Visit( SmPlaceNode* pNode );
    void Visit( SmTextNode* pNode );
    void Visit( SmSpecialNode* pNode );
    void Visit( SmGlyphSpecialNode* pNode );
    void Visit( SmMathSymbolNode* pNode );
    void Visit( SmBlankNode* pNode );
    void Visit( SmErrorNode* pNode );
    void Visit( SmLineNode* pNode );
    void Visit( SmExpressionNode* pNode );
    void Visit( SmPolyLineNode* pNode );
    void Visit( SmRootNode* pNode );
    void Visit( SmRootSymbolNode* pNode );
    void Visit( SmRectangleNode* pNode );
    void Visit( SmVerticalBraceNode* pNode );
private:
    /** Auxiliary method for visiting the children of a pNode */
    void VisitChildren( SmNode* pNode );
};

/////////////////////////////// SmDefaultingVisitor ////////////////////////////////


/** Visitor that uses DefaultVisit for handling visits by default
 *
 * This abstract baseclass is useful for visitors where many methods share the same
 * implementation.
 */
class SmDefaultingVisitor : public SmVisitor
{
public:
    void Visit( SmTableNode* pNode );
    void Visit( SmBraceNode* pNode );
    void Visit( SmBracebodyNode* pNode );
    void Visit( SmOperNode* pNode );
    void Visit( SmAlignNode* pNode );
    void Visit( SmAttributNode* pNode );
    void Visit( SmFontNode* pNode );
    void Visit( SmUnHorNode* pNode );
    void Visit( SmBinHorNode* pNode );
    void Visit( SmBinVerNode* pNode );
    void Visit( SmBinDiagonalNode* pNode );
    void Visit( SmSubSupNode* pNode );
    void Visit( SmMatrixNode* pNode );
    void Visit( SmPlaceNode* pNode );
    void Visit( SmTextNode* pNode );
    void Visit( SmSpecialNode* pNode );
    void Visit( SmGlyphSpecialNode* pNode );
    void Visit( SmMathSymbolNode* pNode );
    void Visit( SmBlankNode* pNode );
    void Visit( SmErrorNode* pNode );
    void Visit( SmLineNode* pNode );
    void Visit( SmExpressionNode* pNode );
    void Visit( SmPolyLineNode* pNode );
    void Visit( SmRootNode* pNode );
    void Visit( SmRootSymbolNode* pNode );
    void Visit( SmRectangleNode* pNode );
    void Visit( SmVerticalBraceNode* pNode );
protected:
    /** Method invoked by Visit methods by default */
    virtual void DefaultVisit( SmNode* pNode ) = 0;
};

/////////////////////////////// SmCaretDrawingVisitor ////////////////////////////////

/** Visitor for drawing a caret position */
class SmCaretDrawingVisitor : public SmDefaultingVisitor
{
public:
    /** Given position and device this constructor will draw the caret */
    SmCaretDrawingVisitor( OutputDevice& rDevice, SmCaretPos position, Point offset );
    void Visit( SmTextNode* pNode );
private:
    OutputDevice &rDev;
    SmCaretPos pos;
    /** Offset to draw from */
    Point Offset;
protected:
    /** Default method for drawing pNodes */
    void DefaultVisit( SmNode* pNode );
};

/////////////////////////////// SmCaretPos2LineVisitor ////////////////////////////////

/** Visitor getting a line from a caret position */
class SmCaretPos2LineVisitor : public SmDefaultingVisitor
{
public:
    /** Given position and device this constructor will compute a line for the caret */
    SmCaretPos2LineVisitor( OutputDevice *pDevice, SmCaretPos position ) {
        pDev = pDevice;
        pos = position;
        j_assert( position.IsValid( ), "Cannot draw invalid position!" );

        pos.pSelectedNode->Accept( this );
    }
    void Visit( SmTextNode* pNode );
    SmCaretLine GetResult( ){
        return line;
    }
private:
    SmCaretLine line;
    OutputDevice *pDev;
    SmCaretPos pos;
protected:
    /** Default method for computing lines for pNodes */
    void DefaultVisit( SmNode* pNode );
};

/////////////////////////////// SmDrawingVisitor ////////////////////////////////

/** Visitor for drawing SmNodes to OutputDevice */
class SmDrawingVisitor : public SmVisitor
{
public:
    /** Create an instance of SmDrawingVisitor, and use it to draw a formula
     * @param rDevice   Device to draw on
     * @param position  Offset on device to draw the formula
     * @param pTree     Formula tree to draw
     * @remarks This constructor will do the drawing, no need to anything more.
     */
    SmDrawingVisitor( OutputDevice &rDevice, Point position, SmNode* pTree )
        : rDev( rDevice ) {
        this->Position = position;
        pTree->Accept( this );
    }
    void Visit( SmTableNode* pNode );
    void Visit( SmBraceNode* pNode );
    void Visit( SmBracebodyNode* pNode );
    void Visit( SmOperNode* pNode );
    void Visit( SmAlignNode* pNode );
    void Visit( SmAttributNode* pNode );
    void Visit( SmFontNode* pNode );
    void Visit( SmUnHorNode* pNode );
    void Visit( SmBinHorNode* pNode );
    void Visit( SmBinVerNode* pNode );
    void Visit( SmBinDiagonalNode* pNode );
    void Visit( SmSubSupNode* pNode );
    void Visit( SmMatrixNode* pNode );
    void Visit( SmPlaceNode* pNode );
    void Visit( SmTextNode* pNode );
    void Visit( SmSpecialNode* pNode );
    void Visit( SmGlyphSpecialNode* pNode );
    void Visit( SmMathSymbolNode* pNode );
    void Visit( SmBlankNode* pNode );
    void Visit( SmErrorNode* pNode );
    void Visit( SmLineNode* pNode );
    void Visit( SmExpressionNode* pNode );
    void Visit( SmPolyLineNode* pNode );
    void Visit( SmRootNode* pNode );
    void Visit( SmRootSymbolNode* pNode );
    void Visit( SmRectangleNode* pNode );
    void Visit( SmVerticalBraceNode* pNode );
private:
    /** Draw the children of a pNode
     * This the default method, use by most pNodes
     */
    void DrawChildren( SmNode* pNode );

    /** Draw an SmTextNode or a subclass of this */
    void DrawTextNode( SmTextNode* pNode );
    /** Draw an SmSpecialNode or a subclass of this  */
    void DrawSpecialNode( SmSpecialNode* pNode );
    /** OutputDevice to draw on */
    OutputDevice& rDev;
    /** Position to draw on the rDev
     * @remarks This variable is used to pass parameters in DrawChildren( ), this means
                that after a call to DrawChildren( ) the contents of this method is undefined
                so if needed cache it locally on the stack.
     */
    Point Position;
};

/////////////////////////////// SmSetSelectionVisitor ////////////////////////////////

/** Set Selection Visitor
 * Sets the IsSelected( ) property on all SmNodes of the tree
 */
class SmSetSelectionVisitor : public SmDefaultingVisitor
{
public:
    SmSetSelectionVisitor( SmCaretPos startPos,
                        SmCaretPos endPos ){
        StartPos    = startPos;
        EndPos      = endPos;
        IsSelecting = false;
    }
    void Visit( SmBinHorNode* pNode );
    void Visit( SmUnHorNode* pNode );
    void Visit( SmFontNode* pNode );
    void Visit( SmTextNode* pNode );
    void Visit( SmExpressionNode* pNode );
    void Visit( SmAlignNode* pNode );
    /** Set IsSelected on all pNodes of pSubTree */
    static void SetSelectedOnAll( SmNode* pSubTree, bool IsSelected = true );
private:
    /** Visit a selectable pNode
     * Can be used to handle pNodes that can be selected, that doesn't have more SmCaretPos'
     * than 0 and 1 inside them. SmTextNode should be handle seperately!
     * Also note that pNodes such as SmBinVerNode cannot be selected, don't this method for
     * it.
     */
    void DefaultVisit( SmNode* pNode );
    void VisitCompositionNode( SmNode* pNode );
    /** Caret position where the selection starts */
    SmCaretPos  StartPos;
    /** Caret position where the selection ends */
    SmCaretPos  EndPos;
    /** The current state of this visitor
     * This property changes when the visitor meets either StartPos
     * or EndPos. This means that anything visited in between will be
     * selected.
     */
    BOOL IsSelecting;
};


/////////////////////////////// SmCaretPosGraphBuildingVisitor ////////////////////////////////


/** A visitor for building a SmCaretPosGraph
 *
 * Visit invariant:
 * Each pNode, except SmExpressionNode, SmBinHorNode and a few others, constitues an entry
 * in a line. Consider the line entry "H", this entry creates one carat position, here
 * denoted by | in "H|".
 *
 * Parameter variables:
 *  The following variables are used to transfer parameters in to calls and results out
 *  of calls.
 *      pRightMost : SmCaretPosGraphEntry*
 *
 * Prior to a Visit call:
 *  pRightMost: A pointer to right most position infront of the current line entry.
 *
 * After a Visit call:
 *  pRightMost: A pointer to the right most position in the called line entry, if no there's
 *              no caret positions in called line entry don't change this variable.
 */
class SmCaretPosGraphBuildingVisitor : public SmVisitor
{
public:
    /** Builds a caret position graph for pRootNode */
    SmCaretPosGraphBuildingVisitor( SmNode* pRootNode );
    void Visit( SmTableNode* pNode );
    void Visit( SmBraceNode* pNode );
    void Visit( SmBracebodyNode* pNode );
    void Visit( SmOperNode* pNode );
    void Visit( SmAlignNode* pNode );
    void Visit( SmAttributNode* pNode );
    void Visit( SmFontNode* pNode );
    void Visit( SmUnHorNode* pNode );
    void Visit( SmBinHorNode* pNode );
    void Visit( SmBinVerNode* pNode );
    void Visit( SmBinDiagonalNode* pNode );
    void Visit( SmSubSupNode* pNode );
    void Visit( SmMatrixNode* pNode );
    void Visit( SmPlaceNode* pNode );
    void Visit( SmTextNode* pNode );
    void Visit( SmSpecialNode* pNode );
    void Visit( SmGlyphSpecialNode* pNode );
    void Visit( SmMathSymbolNode* pNode );
    void Visit( SmBlankNode* pNode );
    void Visit( SmErrorNode* pNode );
    void Visit( SmLineNode* pNode );
    void Visit( SmExpressionNode* pNode );
    void Visit( SmPolyLineNode* pNode );
    void Visit( SmRootNode* pNode );
    void Visit( SmRootSymbolNode* pNode );
    void Visit( SmRectangleNode* pNode );
    void Visit( SmVerticalBraceNode* pNode );
    SmCaretPosGraph* Graph( ){
        return pGraph;
    }
private:
    SmCaretPosGraphEntry* pRightMost;
    SmCaretPosGraph*      pGraph;
};

/////////////////////////////// SmCloningVisitor ///////////////////////////////

/** Visitor for cloning a pNode
 *
 * This visitor creates deep clones.
 */
class SmCloningVisitor : public SmVisitor
{
public:
    SmCloningVisitor( ){ pResult = NULL; }
    void Visit( SmTableNode* pNode );
    void Visit( SmBraceNode* pNode );
    void Visit( SmBracebodyNode* pNode );
    void Visit( SmOperNode* pNode );
    void Visit( SmAlignNode* pNode );
    void Visit( SmAttributNode* pNode );
    void Visit( SmFontNode* pNode );
    void Visit( SmUnHorNode* pNode );
    void Visit( SmBinHorNode* pNode );
    void Visit( SmBinVerNode* pNode );
    void Visit( SmBinDiagonalNode* pNode );
    void Visit( SmSubSupNode* pNode );
    void Visit( SmMatrixNode* pNode );
    void Visit( SmPlaceNode* pNode );
    void Visit( SmTextNode* pNode );
    void Visit( SmSpecialNode* pNode );
    void Visit( SmGlyphSpecialNode* pNode );
    void Visit( SmMathSymbolNode* pNode );
    void Visit( SmBlankNode* pNode );
    void Visit( SmErrorNode* pNode );
    void Visit( SmLineNode* pNode );
    void Visit( SmExpressionNode* pNode );
    void Visit( SmPolyLineNode* pNode );
    void Visit( SmRootNode* pNode );
    void Visit( SmRootSymbolNode* pNode );
    void Visit( SmRectangleNode* pNode );
    void Visit( SmVerticalBraceNode* pNode );
    /** Clone a pNode */
    SmNode* Clone( SmNode* pNode );
private:
    SmNode* pResult;
    /** Clone children of pSource and give them to pTarget */
    void CloneKids( SmStructureNode* pSource, SmStructureNode* pTarget );
    /** Clone attributes on a pNode */
    void CloneNodeAttr( SmNode* pSource, SmNode* pTarget );
};


/////////////////////////////// SmSelectionDrawingVisitor ///////////////////////////////

class SmSelectionDrawingVisitor : public SmDefaultingVisitor
{
public:
    /** Draws a selection on rDevice for the selection on pTree */
    SmSelectionDrawingVisitor( OutputDevice& rDevice, SmNode* pTree, Point Offset );
    void Visit( SmTextNode* pNode );
private:
    /** Reference to drawing device */
    OutputDevice& rDev;
    /** True if  aSelectionArea have been initialized */
    BOOL bHasSelectionArea;
    /** The current area that is selected */
    Rectangle aSelectionArea;
    /** Extend the area that must be selected  */
    void ExtendSelectionArea( Rectangle aArea );
    /** Default visiting method */
    void DefaultVisit( SmNode* pNode );
    /** Visit the children of a given pNode */
    void VisitChildren( SmNode* pNode );
};

/////////////////////////////// SmNodeToTextVisitor ///////////////////////////////

/** Extract command text from pNodes */
class SmNodeToTextVisitor : public SmVisitor
{
public:
    SmNodeToTextVisitor( SmNode* pNode, String &rText )
        : rCmdText( rText ) {
        pNode->Accept( this );
    }
    void Visit( SmTableNode* pNode );
    void Visit( SmBraceNode* pNode );
    void Visit( SmBracebodyNode* pNode );
    void Visit( SmOperNode* pNode );
    void Visit( SmAlignNode* pNode );
    void Visit( SmAttributNode* pNode );
    void Visit( SmFontNode* pNode );
    void Visit( SmUnHorNode* pNode );
    void Visit( SmBinHorNode* pNode );
    void Visit( SmBinVerNode* pNode );
    void Visit( SmBinDiagonalNode* pNode );
    void Visit( SmSubSupNode* pNode );
    void Visit( SmMatrixNode* pNode );
    void Visit( SmPlaceNode* pNode );
    void Visit( SmTextNode* pNode );
    void Visit( SmSpecialNode* pNode );
    void Visit( SmGlyphSpecialNode* pNode );
    void Visit( SmMathSymbolNode* pNode );
    void Visit( SmBlankNode* pNode );
    void Visit( SmErrorNode* pNode );
    void Visit( SmLineNode* pNode );
    void Visit( SmExpressionNode* pNode );
    void Visit( SmPolyLineNode* pNode );
    void Visit( SmRootNode* pNode );
    void Visit( SmRootSymbolNode* pNode );
    void Visit( SmRectangleNode* pNode );
    void Visit( SmVerticalBraceNode* pNode );
private:
    /** Extract text from a pNode that constitues a line */
    void LineToText( SmNode* pNode ) {
        Separate( );
        if( pNode )
            pNode->Accept( this );
        Separate( );
    }
    inline void Append( const sal_Char* pCharStr ) {
        rCmdText.AppendAscii( pCharStr );
    }
    inline void Append( const String &rText ) {
        rCmdText.Append( rText );
    }
    /** Append a blank for separation, if needed */
    inline void Separate( ){
        if( rCmdText.GetChar( rCmdText.Len( ) - 1 ) != ' ' )
            rCmdText.AppendAscii( RTL_CONSTASCII_STRINGPARAM( " " ) );
    }
    /** Output text generated from the pNodes */
    String &rCmdText;
};

#endif /* SMVISITORS_H */