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
  
     | 
    
      /* -*- 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 <DocumentLayoutManager.hxx>
#include <doc.hxx>
#include <IDocumentState.hxx>
#include <IDocumentUndoRedo.hxx>
#include <DocumentContentOperationsManager.hxx>
#include <IDocumentStylePoolAccess.hxx>
#include <undobj.hxx>
#include <viewsh.hxx>
#include <layouter.hxx>
#include <poolfmt.hxx>
#include <frmfmt.hxx>
#include <fmtcntnt.hxx>
#include <fmtcnct.hxx>
#include <ndole.hxx>
#include <fmtanchr.hxx>
#include <txtflcnt.hxx>
#include <fmtflcnt.hxx>
#include <ndtxt.hxx>
#include <unoframe.hxx>
#include <textboxhelper.hxx>
#include <ndindex.hxx>
#include <pam.hxx>
#include <frameformats.hxx>
#include <com/sun/star/embed/EmbedStates.hpp>
#include <svx/svdobj.hxx>
#include <svx/svdpage.hxx>
#include <osl/diagnose.h>
using namespace ::com::sun::star;
namespace sw
{
DocumentLayoutManager::DocumentLayoutManager( SwDoc& i_rSwdoc ) :
    m_rDoc( i_rSwdoc ),
    mpCurrentView( nullptr )
{
}
const SwViewShell *DocumentLayoutManager::GetCurrentViewShell() const
{
    return mpCurrentView;
}
SwViewShell *DocumentLayoutManager::GetCurrentViewShell()
{
    return mpCurrentView;
}
void DocumentLayoutManager::SetCurrentViewShell( SwViewShell* pNew )
{
    mpCurrentView = pNew;
}
// It must be able to communicate to a SwViewShell. This is going to be removed later.
const SwRootFrame *DocumentLayoutManager::GetCurrentLayout() const
{
    if(GetCurrentViewShell())
        return GetCurrentViewShell()->GetLayout();
    return nullptr;
}
SwRootFrame *DocumentLayoutManager::GetCurrentLayout()
{
    if(GetCurrentViewShell())
        return GetCurrentViewShell()->GetLayout();
    return nullptr;
}
bool DocumentLayoutManager::HasLayout() const
{
    // if there is a view, there is always a layout
    return (mpCurrentView != nullptr);
}
SwLayouter* DocumentLayoutManager::GetLayouter()
{
    return mpLayouter.get();
}
const SwLayouter* DocumentLayoutManager::GetLayouter() const
{
    return mpLayouter.get();
}
void DocumentLayoutManager::SetLayouter( SwLayouter* pNew )
{
    mpLayouter.reset( pNew );
}
/** Create a new format whose settings fit to the Request by default.
    The format is put into the respective format array.
    If there already is a fitting format, it is returned instead. */
SwFrameFormat *DocumentLayoutManager::MakeLayoutFormat( RndStdIds eRequest, const SfxItemSet* pSet )
{
    SwFrameFormat *pFormat = nullptr;
    const bool bMod = m_rDoc.getIDocumentState().IsModified();
    bool bHeader = false;
    switch ( eRequest )
    {
    case RndStdIds::HEADER:
    case RndStdIds::HEADERL:
    case RndStdIds::HEADERR:
        {
            bHeader = true;
            [[fallthrough]];
        }
    case RndStdIds::FOOTER:
        {
            pFormat = new SwFrameFormat( m_rDoc.GetAttrPool(),
                                 (bHeader ? "Right header" : "Right footer"),
                                 m_rDoc.GetDfltFrameFormat() );
            SwNodeIndex aTmpIdx( m_rDoc.GetNodes().GetEndOfAutotext() );
            SwStartNode* pSttNd =
                m_rDoc.GetNodes().MakeTextSection
                ( aTmpIdx,
                  bHeader ? SwHeaderStartNode : SwFooterStartNode,
                  m_rDoc.getIDocumentStylePoolAccess().GetTextCollFromPool(o3tl::narrowing<sal_uInt16>( bHeader
                                     ? ( eRequest == RndStdIds::HEADERL
                                         ? RES_POOLCOLL_HEADERL
                                         : eRequest == RndStdIds::HEADERR
                                         ? RES_POOLCOLL_HEADERR
                                         : RES_POOLCOLL_HEADER )
                                     : RES_POOLCOLL_FOOTER
                                     ) ) );
            pFormat->SetFormatAttr( SwFormatContent( pSttNd ));
            if( pSet )      // Set a few more attributes
                pFormat->SetFormatAttr( *pSet );
            // Why set it back?  Doc has changed, or not?
            // In any case, wrong for the FlyFrames!
            if ( !bMod )
                m_rDoc.getIDocumentState().ResetModified();
        }
        break;
    case RndStdIds::DRAW_OBJECT:
        {
            pFormat = m_rDoc.MakeDrawFrameFormat( OUString(), m_rDoc.GetDfltFrameFormat() );
            if( pSet )      // Set a few more attributes
                pFormat->SetFormatAttr( *pSet );
            if (m_rDoc.GetIDocumentUndoRedo().DoesUndo())
            {
                m_rDoc.GetIDocumentUndoRedo().AppendUndo(
                    std::make_unique<SwUndoInsLayFormat>(pFormat, SwNodeOffset(0), 0));
            }
        }
        break;
#if OSL_DEBUG_LEVEL > 0
    case RndStdIds::FLY_AT_PAGE:
    case RndStdIds::FLY_AT_CHAR:
    case RndStdIds::FLY_AT_FLY:
    case RndStdIds::FLY_AT_PARA:
    case RndStdIds::FLY_AS_CHAR:
        OSL_FAIL( "use new interface instead: SwDoc::MakeFlySection!" );
        break;
#endif
    default:
        OSL_ENSURE( false,
                "LayoutFormat was requested with an invalid Request." );
    }
    return pFormat;
}
/// Deletes the denoted format and its content.
void DocumentLayoutManager::DelLayoutFormat( SwFrameFormat *pFormat )
{
    // A chain of frames needs to be merged, if necessary,
    // so that the Frame's contents are adjusted accordingly before we destroy the Frames.
    const SwFormatChain &rChain = pFormat->GetChain();
    if ( rChain.GetPrev() )
    {
        SwFormatChain aChain( rChain.GetPrev()->GetChain() );
        aChain.SetNext( rChain.GetNext() );
        m_rDoc.SetAttr( aChain, *rChain.GetPrev() );
    }
    if ( rChain.GetNext() )
    {
        SwFormatChain aChain( rChain.GetNext()->GetChain() );
        aChain.SetPrev( rChain.GetPrev() );
        m_rDoc.SetAttr( aChain, *rChain.GetNext() );
    }
    const SwNodeIndex* pCntIdx = nullptr;
    // The draw format doesn't own its content, it just has a pointer to it.
    if (pFormat->Which() != RES_DRAWFRMFMT)
        pCntIdx = pFormat->GetContent().GetContentIdx();
    if (pCntIdx && !m_rDoc.GetIDocumentUndoRedo().DoesUndo())
    {
        // Disconnect if it's an OLE object
        SwOLENode* pOLENd = m_rDoc.GetNodes()[ pCntIdx->GetIndex()+1 ]->GetOLENode();
        if( pOLENd && pOLENd->GetOLEObj().IsOleRef() )
        {
            try
            {
                pOLENd->GetOLEObj().GetOleRef()->changeState( embed::EmbedStates::LOADED );
            }
            catch ( uno::Exception& )
            {
            }
        }
    }
    // Destroy Frames
    pFormat->DelFrames();
    // Only FlyFrames are undoable at first
    const sal_uInt16 nWh = pFormat->Which();
    if (m_rDoc.GetIDocumentUndoRedo().DoesUndo() &&
        (RES_FLYFRMFMT == nWh || RES_DRAWFRMFMT == nWh))
    {
        m_rDoc.GetIDocumentUndoRedo().AppendUndo( std::make_unique<SwUndoDelLayFormat>( pFormat ));
    }
    else
    {
        // #i32089# - delete at-frame anchored objects
        if ( nWh == RES_FLYFRMFMT )
        {
            // determine frame formats of at-frame anchored objects
            const SwNodeIndex* pContentIdx = nullptr;
            if (pFormat->Which() != RES_DRAWFRMFMT)
                pContentIdx = pFormat->GetContent().GetContentIdx();
            if (pContentIdx)
            {
                const SwFrameFormats* pTable = pFormat->GetDoc()->GetSpzFrameFormats();
                if ( pTable )
                {
                    std::vector<SwFrameFormat*> aToDeleteFrameFormats;
                    const SwNodeOffset nNodeIdxOfFlyFormat( pContentIdx->GetIndex() );
                    for ( size_t i = 0; i < pTable->size(); ++i )
                    {
                        SwFrameFormat* pTmpFormat = (*pTable)[i];
                        const SwFormatAnchor &rAnch = pTmpFormat->GetAnchor();
                        if ( rAnch.GetAnchorId() == RndStdIds::FLY_AT_FLY &&
                             rAnch.GetContentAnchor()->nNode.GetIndex() == nNodeIdxOfFlyFormat )
                        {
                            aToDeleteFrameFormats.push_back( pTmpFormat );
                        }
                    }
                    // delete found frame formats
                    while ( !aToDeleteFrameFormats.empty() )
                    {
                        SwFrameFormat* pTmpFormat = aToDeleteFrameFormats.back();
                        pFormat->GetDoc()->getIDocumentLayoutAccess().DelLayoutFormat( pTmpFormat );
                        aToDeleteFrameFormats.pop_back();
                    }
                }
            }
        }
        // Delete content
        if( pCntIdx )
        {
            SwNode *pNode = &pCntIdx->GetNode();
            const_cast<SwFormatContent&>(pFormat->GetFormatAttr( RES_CNTNT )).SetNewContentIdx( nullptr );
            m_rDoc.getIDocumentContentOperations().DeleteSection( pNode );
        }
        // Delete the character for FlyFrames anchored as char (if necessary)
        const SwFormatAnchor& rAnchor = pFormat->GetAnchor();
        if ((RndStdIds::FLY_AS_CHAR == rAnchor.GetAnchorId()) && rAnchor.GetContentAnchor())
        {
            const SwPosition* pPos = rAnchor.GetContentAnchor();
            SwTextNode *pTextNd = pPos->nNode.GetNode().GetTextNode();
            // attribute is still in text node, delete it
            if ( pTextNd )
            {
                SwTextFlyCnt* const pAttr = static_cast<SwTextFlyCnt*>(
                    pTextNd->GetTextAttrForCharAt( pPos->nContent.GetIndex(),
                        RES_TXTATR_FLYCNT ));
                if ( pAttr && (pAttr->GetFlyCnt().GetFrameFormat() == pFormat) )
                {
                    // don't delete, set pointer to 0
                    const_cast<SwFormatFlyCnt&>(pAttr->GetFlyCnt()).SetFlyFormat();
                    SwIndex aIdx( pPos->nContent );
                    pTextNd->EraseText( aIdx, 1 );
                }
            }
        }
        m_rDoc.DelFrameFormat( pFormat );
    }
    m_rDoc.getIDocumentState().SetModified();
}
/** Copies the stated format (pSrc) to pDest and returns pDest.
    If there's no pDest, it is created.
    If the source format is located in another document, also copy correctly
    in this case.
    The Anchor attribute's position is always set to 0! */
SwFrameFormat *DocumentLayoutManager::CopyLayoutFormat(
    const SwFrameFormat& rSource,
    const SwFormatAnchor& rNewAnchor,
    bool bSetTextFlyAtt,
    bool bMakeFrames )
{
    const bool bFly = RES_FLYFRMFMT == rSource.Which();
    const bool bDraw = RES_DRAWFRMFMT == rSource.Which();
    OSL_ENSURE( bFly || bDraw, "this method only works for fly or draw" );
    SwDoc* pSrcDoc = const_cast<SwDoc*>(rSource.GetDoc());
    // May we copy this object?
    // We may, unless it's 1) it's a control (and therefore a draw)
    //                     2) anchored in a header/footer
    //                     3) anchored (to paragraph?)
    bool bMayNotCopy = false;
    const auto pCAnchor = rNewAnchor.GetContentAnchor();
    bool bInHeaderFooter = pCAnchor && m_rDoc.IsInHeaderFooter(pCAnchor->nNode);
    if(bDraw)
    {
        bool bCheckControlLayer = false;
        rSource.CallSwClientNotify(sw::CheckDrawFrameFormatLayerHint(&bCheckControlLayer));
        bMayNotCopy =
            bCheckControlLayer &&
            ((RndStdIds::FLY_AT_PARA == rNewAnchor.GetAnchorId()) || (RndStdIds::FLY_AT_FLY  == rNewAnchor.GetAnchorId()) || (RndStdIds::FLY_AT_CHAR == rNewAnchor.GetAnchorId())) &&
            bInHeaderFooter;
    }
    // just return if we can't copy this
    if( bMayNotCopy )
        return nullptr;
    SwFrameFormat* pDest = m_rDoc.GetDfltFrameFormat();
    if( rSource.GetRegisteredIn() != pSrcDoc->GetDfltFrameFormat() )
        pDest = m_rDoc.CopyFrameFormat( *static_cast<const SwFrameFormat*>(rSource.GetRegisteredIn()) );
    if( bFly )
    {
        // #i11176#
        // To do a correct cloning concerning the ZOrder for all objects
        // it is necessary to actually create a draw object for fly frames, too.
        // These are then added to the DrawingLayer (which needs to exist).
        // Together with correct sorting of all drawinglayer based objects
        // before cloning ZOrder transfer works correctly then.
        SwFlyFrameFormat *pFormat = m_rDoc.MakeFlyFrameFormat( rSource.GetName(), pDest );
        pDest = pFormat;
        SwXFrame::GetOrCreateSdrObject(*pFormat);
    }
    else
        pDest = m_rDoc.MakeDrawFrameFormat( OUString(), pDest );
    // Copy all other or new attributes
    pDest->CopyAttrs( rSource );
    // Do not copy chains
    pDest->ResetFormatAttr( RES_CHAIN );
    if( bFly )
    {
        // Duplicate the content.
        const SwNode& rCSttNd = rSource.GetContent().GetContentIdx()->GetNode();
        SwNodeRange aRg( rCSttNd, SwNodeOffset(1), *rCSttNd.EndOfSectionNode() );
        SwNodeIndex aIdx( m_rDoc.GetNodes().GetEndOfAutotext() );
        SwStartNode* pSttNd = SwNodes::MakeEmptySection( aIdx, SwFlyStartNode );
        // Set the Anchor/ContentIndex first.
        // Within the copying part, we can access the values (DrawFormat in Headers and Footers)
        aIdx = *pSttNd;
        SwFormatContent aAttr( rSource.GetContent() );
        aAttr.SetNewContentIdx( &aIdx );
        pDest->SetFormatAttr( aAttr );
        pDest->SetFormatAttr( rNewAnchor );
        if( !m_rDoc.IsCopyIsMove() || &m_rDoc != pSrcDoc )
        {
            if( (m_rDoc.IsInReading() && !bInHeaderFooter) || m_rDoc.IsInMailMerge() )
                pDest->SetName( OUString() );
            else
            {
                // Test first if the name is already taken, if so generate a new one.
                SwNodeType nNdTyp = aRg.aStart.GetNode().GetNodeType();
                OUString sOld( pDest->GetName() );
                pDest->SetName( OUString() );
                if( m_rDoc.FindFlyByName( sOld, nNdTyp ) )     // found one
                    switch( nNdTyp )
                    {
                    case SwNodeType::Grf:    sOld = m_rDoc.GetUniqueGrfName(sOld);  break;
                    case SwNodeType::Ole:    sOld = m_rDoc.GetUniqueOLEName();      break;
                    default:                 sOld = m_rDoc.GetUniqueFrameName();    break;
                    }
                pDest->SetName( sOld );
            }
        }
        if (m_rDoc.GetIDocumentUndoRedo().DoesUndo())
        {
            m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::make_unique<SwUndoInsLayFormat>(pDest,SwNodeOffset(0),0));
        }
        // Make sure that FlyFrames in FlyFrames are copied
        aIdx = *pSttNd->EndOfSectionNode();
        //fdo#36631 disable (scoped) any undo operations associated with the
        //contact object itself. They should be managed by SwUndoInsLayFormat.
        const ::sw::DrawUndoGuard drawUndoGuard(m_rDoc.GetIDocumentUndoRedo());
        pSrcDoc->GetDocumentContentOperationsManager().CopyWithFlyInFly(aRg, aIdx, nullptr, false, true, true);
    }
    else
    {
        OSL_ENSURE( RES_DRAWFRMFMT == rSource.Which(), "Neither Fly nor Draw." );
        // #i52780# - Note: moving object to visible layer not needed.
        rSource.CallSwClientNotify(sw::DrawFormatLayoutCopyHint(static_cast<SwDrawFrameFormat&>(*pDest), m_rDoc));
        if(pDest->GetAnchor() == rNewAnchor)
        {
            // Do *not* connect to layout, if a <MakeFrames> will not be called.
            if(bMakeFrames)
                pDest->CallSwClientNotify(sw::DrawFrameFormatHint(sw::DrawFrameFormatHintId::MAKE_FRAMES));
        }
        else
            pDest->SetFormatAttr( rNewAnchor );
        if (m_rDoc.GetIDocumentUndoRedo().DoesUndo())
        {
            m_rDoc.GetIDocumentUndoRedo().AppendUndo(std::make_unique<SwUndoInsLayFormat>(pDest,SwNodeOffset(0),0));
        }
    }
    if (bSetTextFlyAtt && (RndStdIds::FLY_AS_CHAR == rNewAnchor.GetAnchorId()))
    {
        const SwPosition* pPos = rNewAnchor.GetContentAnchor();
        SwFormatFlyCnt aFormat( pDest );
        pPos->nNode.GetNode().GetTextNode()->InsertItem(
            aFormat, pPos->nContent.GetIndex(), 0 );
    }
    if( bMakeFrames )
        pDest->MakeFrames();
    if (pDest->GetName().isEmpty())
    {
        // Format name should have unique name. Let's use object name as a fallback
        SdrObject *pObj = pDest->FindSdrObject();
        if (pObj)
            pDest->SetName(pObj->GetName());
    }
    // If the draw format has a TextBox, then copy its fly format as well.
    if (const auto& pTextBoxes = rSource.GetOtherTextBoxFormats())
        pTextBoxes->Clone(&m_rDoc, rNewAnchor, pDest, bSetTextFlyAtt, bMakeFrames);
    return pDest;
}
//Load document from fdo#42534 under valgrind, drag the scrollbar down so full
//document layout is triggered. Close document before layout has completed, and
//SwAnchoredObject objects deleted by the deletion of layout remain referenced
//by the SwLayouter
void DocumentLayoutManager::ClearSwLayouterEntries()
{
    SwLayouter::ClearMovedFwdFrames( m_rDoc );
    SwLayouter::ClearObjsTmpConsiderWrapInfluence( m_rDoc );
    // #i65250#
    SwLayouter::ClearMoveBwdLayoutInfo( m_rDoc );
}
DocumentLayoutManager::~DocumentLayoutManager()
{
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 
     |