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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/config.h>

#include <o3tl/safeint.hxx>
#include <svx/svdpage.hxx>
#include <drawdoc.hxx>
#include <fmtpdsc.hxx>
#include <swtable.hxx>
#include <rootfrm.hxx>
#include <pagefrm.hxx>
#include <dflyobj.hxx>
#include <frmtool.hxx>
#include "virtoutp.hxx"
#include <notxtfrm.hxx>
#include <pagedesc.hxx>
#include <viewimp.hxx>
#include <hints.hxx>
#include <viewopt.hxx>
#include <set>
#include <IDocumentDrawModelAccess.hxx>
#include <IDocumentSettingAccess.hxx>
#include <IDocumentFieldsAccess.hxx>
#include <DocumentLayoutManager.hxx>
#include <DocumentRedlineManager.hxx>
#include <ndindex.hxx>

SwLayVout     *SwRootFrame::s_pVout = nullptr;
bool           SwRootFrame::s_isInPaint = false;
bool           SwRootFrame::s_isNoVirDev = false;

SwCache *SwFrame::mpCache = nullptr;

static long FirstMinusSecond( long nFirst, long nSecond )
    { return nFirst - nSecond; }
static long SecondMinusFirst( long nFirst, long nSecond )
    { return nSecond - nFirst; }
static long SwIncrement( long nA, long nAdd )
    { return nA + nAdd; }
static long SwDecrement( long nA, long nSub )
    { return nA - nSub; }

static SwRectFnCollection aHorizontal = {
    /*.fnGetTop =*/&SwRect::Top_,
    /*.fnGetBottom =*/&SwRect::Bottom_,
    /*.fnGetLeft =*/&SwRect::Left_,
    /*.fnGetRight =*/&SwRect::Right_,
    /*.fnGetWidth =*/&SwRect::Width_,
    /*.fnGetHeight =*/&SwRect::Height_,
    /*.fnGetPos =*/&SwRect::TopLeft,
    /*.fnGetSize =*/&SwRect::Size_,

    /*.fnSetTop =*/&SwRect::Top_,
    /*.fnSetBottom =*/&SwRect::Bottom_,
    /*.fnSetLeft =*/&SwRect::Left_,
    /*.fnSetRight =*/&SwRect::Right_,
    /*.fnSetWidth =*/&SwRect::Width_,
    /*.fnSetHeight =*/&SwRect::Height_,

    /*.fnSubTop =*/&SwRect::SubTop,
    /*.fnAddBottom =*/&SwRect::AddBottom,
    /*.fnSubLeft =*/&SwRect::SubLeft,
    /*.fnAddRight =*/&SwRect::AddRight,
    /*.fnAddWidth =*/&SwRect::AddWidth,
    /*.fnAddHeight =*/&SwRect::AddHeight,

    /*.fnSetPosX =*/&SwRect::SetPosX,
    /*.fnSetPosY =*/&SwRect::SetPosY,

    /*.fnGetTopMargin =*/&SwFrame::GetTopMargin,
    /*.fnGetBottomMargin =*/&SwFrame::GetBottomMargin,
    /*.fnGetLeftMargin =*/&SwFrame::GetLeftMargin,
    /*.fnGetRightMargin =*/&SwFrame::GetRightMargin,
    /*.fnSetXMargins =*/&SwFrame::SetLeftRightMargins,
    /*.fnSetYMargins =*/&SwFrame::SetTopBottomMargins,
    /*.fnGetPrtTop =*/&SwFrame::GetPrtTop,
    /*.fnGetPrtBottom =*/&SwFrame::GetPrtBottom,
    /*.fnGetPrtLeft =*/&SwFrame::GetPrtLeft,
    /*.fnGetPrtRight =*/&SwFrame::GetPrtRight,
    /*.fnTopDist =*/&SwRect::GetTopDistance,
    /*.fnBottomDist =*/&SwRect::GetBottomDistance,
    /*.fnLeftDist =*/&SwRect::GetLeftDistance,
    /*.fnRightDist =*/&SwRect::GetRightDistance,
    /*.fnSetLimit =*/&SwFrame::SetMaxBottom,
    /*.fnOverStep =*/&SwRect::OverStepBottom,

    /*.fnSetPos =*/&SwRect::SetUpperLeftCorner,
    /*.fnMakePos =*/&SwFrame::MakeBelowPos,
    /*.fnXDiff =*/&FirstMinusSecond,
    /*.fnYDiff =*/&FirstMinusSecond,
    /*.fnXInc =*/&SwIncrement,
    /*.fnYInc =*/&o3tl::saturating_add<long>,

    /*.fnSetLeftAndWidth =*/&SwRect::SetLeftAndWidth,
    /*.fnSetTopAndHeight =*/&SwRect::SetTopAndHeight
};

static SwRectFnCollection aVertical = {
    /*.fnGetTop =*/&SwRect::Right_,
    /*.fnGetBottom =*/&SwRect::Left_,
    /*.fnGetLeft =*/&SwRect::Top_,
    /*.fnGetRight =*/&SwRect::Bottom_,
    /*.fnGetWidth =*/&SwRect::Height_,
    /*.fnGetHeight =*/&SwRect::Width_,
    /*.fnGetPos =*/&SwRect::TopRight,
    /*.fnGetSize =*/&SwRect::SwappedSize,

    /*.fnSetTop =*/&SwRect::Right_,
    /*.fnSetBottom =*/&SwRect::Left_,
    /*.fnSetLeft =*/&SwRect::Top_,
    /*.fnSetRight =*/&SwRect::Bottom_,
    /*.fnSetWidth =*/&SwRect::Height_,
    /*.fnSetHeight =*/&SwRect::Width_,

    /*.fnSubTop =*/&SwRect::AddRight,
    /*.fnAddBottom =*/&SwRect::SubLeft,
    /*.fnSubLeft =*/&SwRect::SubTop,
    /*.fnAddRight =*/&SwRect::AddBottom,
    /*.fnAddWidth =*/&SwRect::AddHeight,
    /*.fnAddHeight =*/&SwRect::AddWidth,

    /*.fnSetPosX =*/&SwRect::SetPosY,
    /*.fnSetPosY =*/&SwRect::SetPosX,

    /*.fnGetTopMargin =*/&SwFrame::GetRightMargin,
    /*.fnGetBottomMargin =*/&SwFrame::GetLeftMargin,
    /*.fnGetLeftMargin =*/&SwFrame::GetTopMargin,
    /*.fnGetRightMargin =*/&SwFrame::GetBottomMargin,
    /*.fnSetXMargins =*/&SwFrame::SetTopBottomMargins,
    /*.fnSetYMargins =*/&SwFrame::SetRightLeftMargins,
    /*.fnGetPrtTop =*/&SwFrame::GetPrtRight,
    /*.fnGetPrtBottom =*/&SwFrame::GetPrtLeft,
    /*.fnGetPrtLeft =*/&SwFrame::GetPrtTop,
    /*.fnGetPrtRight =*/&SwFrame::GetPrtBottom,
    /*.fnTopDist =*/&SwRect::GetRightDistance,
    /*.fnBottomDist =*/&SwRect::GetLeftDistance,
    /*.fnLeftDist =*/&SwRect::GetTopDistance,
    /*.fnRightDist =*/&SwRect::GetBottomDistance,
    /*.fnSetLimit =*/&SwFrame::SetMinLeft,
    /*.fnOverStep =*/&SwRect::OverStepLeft,

    /*.fnSetPos =*/&SwRect::SetUpperRightCorner,
    /*.fnMakePos =*/&SwFrame::MakeLeftPos,
    /*.fnXDiff =*/&FirstMinusSecond,
    /*.fnYDiff =*/&SecondMinusFirst,
    /*.fnXInc =*/&SwIncrement,
    /*.fnYInc =*/&SwDecrement,

    /*.fnSetLeftAndWidth =*/&SwRect::SetTopAndHeight,
    /*.fnSetTopAndHeight =*/&SwRect::SetRightAndWidth
};

static SwRectFnCollection aVerticalLeftToRight = {
    /*.fnGetTop =*/&SwRect::Left_,
    /*.fnGetBottom =*/&SwRect::Right_,
    /*.fnGetLeft =*/&SwRect::Top_,
    /*.fnGetRight =*/&SwRect::Bottom_,
    /*.fnGetWidth =*/&SwRect::Height_,
    /*.fnGetHeight =*/&SwRect::Width_,
    /*.fnGetPos =*/&SwRect::TopLeft,
    /*.fnGetSize =*/&SwRect::SwappedSize,

    /*.fnSetTop =*/&SwRect::Left_,
    /*.fnSetBottom =*/&SwRect::Right_,
    /*.fnSetLeft =*/&SwRect::Top_,
    /*.fnSetRight =*/&SwRect::Bottom_,
    /*.fnSetWidth =*/&SwRect::Height_,
    /*.fnSetHeight =*/&SwRect::Width_,

    /*.fnSubTop =*/&SwRect::SubLeft,
    /*.fnAddBottom =*/&SwRect::AddRight,
    /*.fnSubLeft =*/&SwRect::SubTop,
    /*.fnAddRight =*/&SwRect::AddBottom,
    /*.fnAddWidth =*/&SwRect::AddHeight,
    /*.fnAddHeight =*/&SwRect::AddWidth,

    /*.fnSetPosX =*/&SwRect::SetPosY,
    /*.fnSetPosY =*/&SwRect::SetPosX,

    /*.fnGetTopMargin =*/&SwFrame::GetLeftMargin,
    /*.fnGetBottomMargin =*/&SwFrame::GetRightMargin,
    /*.fnGetLeftMargin =*/&SwFrame::GetTopMargin,
    /*.fnGetRightMargin =*/&SwFrame::GetBottomMargin,
    /*.fnSetXMargins =*/&SwFrame::SetTopBottomMargins,
    /*.fnSetYMargins =*/&SwFrame::SetLeftRightMargins,
    /*.fnGetPrtTop =*/&SwFrame::GetPrtLeft,
    /*.fnGetPrtBottom =*/&SwFrame::GetPrtRight,
    /*.fnGetPrtLeft =*/&SwFrame::GetPrtTop,
    /*.fnGetPrtRight =*/&SwFrame::GetPrtBottom,
    /*.fnTopDist =*/&SwRect::GetLeftDistance,
    /*.fnBottomDist =*/&SwRect::GetRightDistance,
    /*.fnLeftDist =*/&SwRect::GetTopDistance,
    /*.fnRightDist =*/&SwRect::GetBottomDistance,
    /*.fnSetLimit =*/&SwFrame::SetMaxRight,
    /*.fnOverStep =*/&SwRect::OverStepRight,

    /*.fnSetPos =*/&SwRect::SetUpperLeftCorner,
    /*.fnMakePos =*/&SwFrame::MakeRightPos,
    /*.fnXDiff =*/&FirstMinusSecond,
    /*.fnYDiff =*/&FirstMinusSecond,
    /*.fnXInc =*/&SwIncrement,
    /*.fnYInc =*/&SwIncrement,

    /*.fnSetLeftAndWidth =*/&SwRect::SetTopAndHeight,
    /*.fnSetTopAndHeight =*/&SwRect::SetLeftAndWidth
};

/**
 * This is the same as horizontal, but rotated counter-clockwise by 90 degrees.
 * This means logical top is physical left, bottom is right, left is bottom,
 * finally right is top. Values map from logical to physical.
 */
static SwRectFnCollection aVerticalLeftToRightBottomToTop = {
    /*.fnGetTop =*/&SwRect::Left_,
    /*.fnGetBottom =*/&SwRect::Right_,
    /*.fnGetLeft =*/&SwRect::Bottom_,
    /*.fnGetRight =*/&SwRect::Top_,
    /*.fnGetWidth =*/&SwRect::Height_,
    /*.fnGetHeight =*/&SwRect::Width_,
    /*.fnGetPos =*/&SwRect::BottomLeft,
    /*.fnGetSize =*/&SwRect::SwappedSize,

    /*.fnSetTop =*/&SwRect::Left_,
    /*.fnSetBottom =*/&SwRect::Right_,
    /*.fnSetLeft =*/&SwRect::Bottom_,
    /*.fnSetRight =*/&SwRect::Top_,
    /*.fnSetWidth =*/&SwRect::Height_,
    /*.fnSetHeight =*/&SwRect::Width_,

    /*.fnSubTop =*/&SwRect::SubLeft,
    /*.fnAddBottom =*/&SwRect::AddRight,
    /*.fnSubLeft =*/&SwRect::AddBottom,
    /*.fnAddRight =*/&SwRect::SubTop,
    /*.fnAddWidth =*/&SwRect::AddHeight,
    /*.fnAddHeight =*/&SwRect::AddWidth,

    /*.fnSetPosX =*/&SwRect::SetPosY,
    /*.fnSetPosY =*/&SwRect::SetPosX,

    /*.fnGetTopMargin =*/&SwFrame::GetLeftMargin,
    /*.fnGetBottomMargin =*/&SwFrame::GetRightMargin,
    /*.fnGetLeftMargin =*/&SwFrame::GetBottomMargin,
    /*.fnGetRightMargin =*/&SwFrame::GetTopMargin,
    /*.fnSetXMargins =*/&SwFrame::SetTopBottomMargins,
    /*.fnSetYMargins =*/&SwFrame::SetLeftRightMargins,
    /*.fnGetPrtTop =*/&SwFrame::GetPrtLeft,
    /*.fnGetPrtBottom =*/&SwFrame::GetPrtRight,
    /*.fnGetPrtLeft =*/&SwFrame::GetPrtBottom,
    /*.fnGetPrtRight =*/&SwFrame::GetPrtTop,
    /*.fnTopDist =*/&SwRect::GetLeftDistance,
    /*.fnBottomDist =*/&SwRect::GetRightDistance,
    /*.fnLeftDist =*/&SwRect::GetBottomDistance,
    /*.fnRightDist =*/&SwRect::GetTopDistance,
    /*.fnSetLimit =*/&SwFrame::SetMaxRight,
    /*.fnOverStep =*/&SwRect::OverStepRight,

    /*.fnSetPos =*/&SwRect::SetLowerLeftCorner,
    /*.fnMakePos =*/&SwFrame::MakeRightPos,
    /*.fnXDiff =*/&SecondMinusFirst,
    /*.fnYDiff =*/&FirstMinusSecond,
    /*.fnXInc =*/&SwDecrement,
    /*.fnYInc =*/&SwIncrement,

    /*.fnSetLeftAndWidth =*/&SwRect::SetBottomAndHeight,
    /*.fnSetTopAndHeight =*/&SwRect::SetLeftAndWidth
};

SwRectFn fnRectHori = &aHorizontal;
SwRectFn fnRectVert = &aVertical;
SwRectFn fnRectVertL2R = &aVerticalLeftToRight;
SwRectFn fnRectVertL2RB2T = &aVerticalLeftToRightBottomToTop;

// #i65250#
sal_uInt32 SwFrameAreaDefinition::mnLastFrameId=0;


void FrameInit()
{
    SwRootFrame::s_pVout = new SwLayVout();
    SwCache *pNew = new SwCache( 100
#ifdef DBG_UTIL
    , "static SwBorderAttrs::pCache"
#endif
    );
    SwFrame::SetCache( pNew );
}

void FrameFinit()
{
#if OSL_DEBUG_LEVEL > 0
    // The cache may only contain null pointers at this time.
    for( size_t n = SwFrame::GetCachePtr()->size(); n; )
        if( (*SwFrame::GetCachePtr())[ --n ] )
        {
            SwCacheObj* pObj = (*SwFrame::GetCachePtr())[ n ];
            OSL_ENSURE( !pObj, "Who didn't deregister?");
        }
#endif
    delete SwRootFrame::s_pVout;
    delete SwFrame::GetCachePtr();
}

// RootFrame::Everything that belongs to CurrShell

CurrShell::CurrShell( SwViewShell *pNew )
{
    OSL_ENSURE( pNew, "insert 0-Shell?" );
    pRoot = pNew->GetLayout();
    if ( pRoot )
    {
        pPrev = pRoot->mpCurrShell;
        pRoot->mpCurrShell = pNew;
        pRoot->mpCurrShells->insert( this );
    }
    else
        pPrev = nullptr;
}

CurrShell::~CurrShell()
{
    if ( pRoot )
    {
        pRoot->mpCurrShells->erase( this );
        if ( pPrev )
            pRoot->mpCurrShell = pPrev;
        if ( pRoot->mpCurrShells->empty() && pRoot->mpWaitingCurrShell )
        {
            pRoot->mpCurrShell = pRoot->mpWaitingCurrShell;
            pRoot->mpWaitingCurrShell = nullptr;
        }
    }
}

void SetShell( SwViewShell *pSh )
{
    SwRootFrame *pRoot = pSh->GetLayout();
    if ( pRoot->mpCurrShells->empty() )
        pRoot->mpCurrShell = pSh;
    else
        pRoot->mpWaitingCurrShell = pSh;
}

void SwRootFrame::DeRegisterShell( SwViewShell *pSh )
{
    // Activate some shell if possible
    if ( mpCurrShell == pSh )
    {
        mpCurrShell = nullptr;
        for(SwViewShell& rShell : pSh->GetRingContainer())
        {
            if(&rShell != pSh)
            {<--- Consider using std::find_if algorithm instead of a raw loop.
                mpCurrShell = &rShell;
                break;
            }
        }
    }

    // Doesn't matter anymore
    if ( mpWaitingCurrShell == pSh )
        mpWaitingCurrShell = nullptr;

    // Remove references
    for ( CurrShell *pC : *mpCurrShells )
    {
        if (pC->pPrev == pSh)
            pC->pPrev = nullptr;
    }
}

void InitCurrShells( SwRootFrame *pRoot )
{
    pRoot->mpCurrShells.reset( new SwCurrShells );
}

/*
|*      The RootFrame requests an own FrameFormat from the document, which it is
|*      going to delete again in the dtor. The own FrameFormat is derived from
|*      the passed FrameFormat.
|*/
SwRootFrame::SwRootFrame( SwFrameFormat *pFormat, SwViewShell * pSh ) :
    SwLayoutFrame( pFormat->GetDoc()->MakeFrameFormat(
        "Root", pFormat ), nullptr ),
    maPagesArea(),
    mnViewWidth( -1 ),
    mnColumns( 0 ),
    mbBookMode( false ),
    mbSidebarChanged( false ),
    mbNeedGrammarCheck( false ),
    mbCheckSuperfluous( false ),
    mbIdleFormat( true ),
    mbBrowseWidthValid( false ),
    mbTurboAllowed( true ),
    mbAssertFlyPages( true ),
    mbIsVirtPageNum( false ),
    mbIsNewLayout( true ),
    mbCallbackActionEnabled ( false ),
    mbLayoutFreezed ( false ),
    mbHideRedlines(pFormat->GetDoc()->GetDocumentRedlineManager().IsHideRedlines()),
    mnBrowseWidth(MIN_BROWSE_WIDTH),
    mpTurbo( nullptr ),
    mpLastPage( nullptr ),
    mpCurrShell( pSh ),
    mpWaitingCurrShell( nullptr ),
    mpDrawPage( nullptr ),
    mnPhyPageNums( 0 ),
    mnAccessibleShells( 0 )
{
    mnFrameType = SwFrameType::Root;
    setRootFrame( this );
}

void SwRootFrame::Init( SwFrameFormat* pFormat )
{
    InitCurrShells( this );

    IDocumentTimerAccess& rTimerAccess = pFormat->getIDocumentTimerAccess();
    IDocumentLayoutAccess& rLayoutAccess = pFormat->getIDocumentLayoutAccess();
    IDocumentFieldsAccess& rFieldsAccess = pFormat->getIDocumentFieldsAccess();
    const IDocumentSettingAccess& rSettingAccess = pFormat->getIDocumentSettingAccess();
    rTimerAccess.StopIdling();
    // For creating the Flys by MakeFrames()
    rLayoutAccess.SetCurrentViewShell( GetCurrShell() );
    mbCallbackActionEnabled = false; // needs to be set to true before leaving!

    SwDrawModel* pMd = pFormat->getIDocumentDrawModelAccess().GetDrawModel();
    if ( pMd )
    {
        // Disable "multiple layout"
        mpDrawPage = pMd->GetPage(0);

        mpDrawPage->SetSize( getFrameArea().SSize() );
    }

    // Initialize the layout: create pages, link content with Content etc.
    // First, initialize some stuff, then get hold of the first
    // node (which will be needed for the PageDesc).

    SwDoc* pDoc = pFormat->GetDoc();
    SwNodeIndex aIndex( *pDoc->GetNodes().GetEndOfContent().StartOfSectionNode() );
    SwContentNode *pNode = pDoc->GetNodes().GoNextSection( &aIndex, true, false );
    // #123067# pNode = 0 can really happen
    SwTableNode *pTableNd= pNode ? pNode->FindTableNode() : nullptr;

    // Get hold of PageDesc (either via FrameFormat of the first node or the initial one).
    SwPageDesc *pDesc = nullptr;
    ::std::optional<sal_uInt16> oPgNum;

    if ( pTableNd )
    {
        const SwFormatPageDesc &rDesc = pTableNd->GetTable().GetFrameFormat()->GetPageDesc();
        pDesc = const_cast<SwPageDesc*>(rDesc.GetPageDesc());
        //#19104# respect the page number offset!!
        oPgNum = rDesc.GetNumOffset();
        if (oPgNum)
            mbIsVirtPageNum = true;
    }
    else if ( pNode )
    {
        const SwFormatPageDesc &rDesc = pNode->GetSwAttrSet().GetPageDesc();
        pDesc = const_cast<SwPageDesc*>(rDesc.GetPageDesc());
        //#19104# respect the page number offset!!
        oPgNum = rDesc.GetNumOffset();
        if (oPgNum)
            mbIsVirtPageNum = true;
    }
    else
        mbIsVirtPageNum = false;
    if ( !pDesc )
        pDesc = &pDoc->GetPageDesc( 0 );

    // Create a page and put it in the layout
    // The first page is always a right-page and always a first-page
    SwPageFrame *pPage = ::InsertNewPage(*pDesc, this, true, true, false, false, nullptr);

    // Find the first page in the Bodytext section.
    SwLayoutFrame *pLay = pPage->FindBodyCont();
    while( pLay->Lower() )
        pLay = static_cast<SwLayoutFrame*>(pLay->Lower());

    SwNodeIndex aTmp( *pDoc->GetNodes().GetEndOfContent().StartOfSectionNode(), 1 );
    ::InsertCnt_( pLay, pDoc, aTmp.GetIndex(), true );
    //Remove masters that haven't been replaced yet from the list.
    RemoveMasterObjs( mpDrawPage );
    if( rSettingAccess.get(DocumentSettingId::GLOBAL_DOCUMENT) )
        rFieldsAccess.UpdateRefFields();
    //b6433357: Update page fields after loading
    if ( !mpCurrShell || !mpCurrShell->Imp()->IsUpdateExpFields() )
    {
        SwDocPosUpdate aMsgHint( pPage->getFrameArea().Top() );
        rFieldsAccess.UpdatePageFields( &aMsgHint );
    }

    rTimerAccess.StartIdling();
    mbCallbackActionEnabled = true;

    SwViewShell *pViewSh  = GetCurrShell();
    if (pViewSh)
        mbNeedGrammarCheck = pViewSh->GetViewOptions()->IsOnlineSpell();
}

void SwRootFrame::DestroyImpl()
{
    mbTurboAllowed = false;
    mpTurbo = nullptr;

    SwFrameFormat *pRegisteredInNonConst = static_cast<SwFrameFormat*>(GetDep());
    if ( pRegisteredInNonConst )
    {
        SwDoc *pDoc = pRegisteredInNonConst->GetDoc();
        pDoc->DelFrameFormat( pRegisteredInNonConst );
        // do this before calling RemoveFootnotes() because footnotes
        // can contain anchored objects
        pDoc->GetDocumentLayoutManager().ClearSwLayouterEntries();
    }

    mpDestroy.reset();

    // Remove references
    for ( auto& rpCurrShell : *mpCurrShells )
        rpCurrShell->pRoot = nullptr;

    mpCurrShells.reset();

    // Some accessible shells are left => problems on second SwFrame::Destroy call
    assert(0 == mnAccessibleShells);

    // fdo#39510 crash on document close with footnotes
    // Object ownership in writer and esp. in layout are a mess: Before the
    // document/layout split SwDoc and SwRootFrame were essentially one object
    // and magically/uncleanly worked around their common destruction by call
    // to SwDoc::IsInDtor() -- even from the layout. As of now destruction of
    // the layout proceeds forward through the frames. Since SwTextFootnote::DelFrames
    // also searches backwards to find the master of footnotes, they must be
    // considered to be owned by the SwRootFrame and also be destroyed here,
    // before tearing down the (now footnote free) rest of the layout.
    RemoveFootnotes(nullptr, false, true);

    SwLayoutFrame::DestroyImpl();
}

SwRootFrame::~SwRootFrame()
{
}

void SwRootFrame::RemoveMasterObjs( SdrPage *pPg )
{
    // Remove all master objects from the Page. But don't delete!
    for( size_t i = pPg ? pPg->GetObjCount() : 0; i; )
    {
        SdrObject* pObj = pPg->GetObj( --i );
        if( dynamic_cast< const SwFlyDrawObj *>( pObj ) !=  nullptr )
            pPg->RemoveObject( i );
    }
}

void SwRootFrame::AllCheckPageDescs() const
{
    if ( !IsLayoutFreezed() )
        CheckPageDescs( const_cast<SwPageFrame*>(static_cast<const SwPageFrame*>(Lower())) );
}

void SwRootFrame::AllInvalidateAutoCompleteWords() const
{
    SwPageFrame *pPage = const_cast<SwPageFrame*>(static_cast<const SwPageFrame*>(Lower()));
    while ( pPage )
    {
        pPage->InvalidateAutoCompleteWords();
        pPage = static_cast<SwPageFrame*>(pPage->GetNext());
    }
}

void SwRootFrame::AllAddPaintRect() const
{
    GetCurrShell()->AddPaintRect( getFrameArea() );
}

void SwRootFrame::AllRemoveFootnotes()
{
    RemoveFootnotes();
}

void SwRootFrame::AllInvalidateSmartTagsOrSpelling(bool bSmartTags) const
{
    SwPageFrame *pPage = const_cast<SwPageFrame*>(static_cast<const SwPageFrame*>(Lower()));
    while ( pPage )
    {
        if ( bSmartTags )
            pPage->InvalidateSmartTags();

        pPage->InvalidateSpelling();
        pPage = static_cast<SwPageFrame*>(pPage->GetNext());
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */