tdf#35361 Add a Quick Look plugins for .od* files on macOS
[LibreOffice.git] / sw / source / core / frmedt / fedesc.cxx
blobc144201af6d2b9433e108f3dfef3118b138e66d7
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <fesh.hxx>
21 #include <doc.hxx>
22 #include <IDocumentUndoRedo.hxx>
23 #include <IDocumentContentOperations.hxx>
24 #include <IDocumentStylePoolAccess.hxx>
25 #include <pagefrm.hxx>
26 #include <rootfrm.hxx>
27 #include <cntfrm.hxx>
28 #include <txtfrm.hxx>
29 #include <notxtfrm.hxx>
30 #include <pam.hxx>
31 #include <fmtpdsc.hxx>
32 #include <pagedesc.hxx>
33 #include <tabfrm.hxx>
34 #include <SwStyleNameMapper.hxx>
35 #include <ndtxt.hxx>
36 #include <osl/diagnose.h>
38 size_t SwFEShell::GetPageDescCnt() const
40 return GetDoc()->GetPageDescCnt();
43 void SwFEShell::ChgCurPageDesc( const SwPageDesc& rDesc )
45 #if OSL_DEBUG_LEVEL > 0
46 // SS does not change PageDesc, but only sets the attribute.
47 // The Pagedesc should be available in the document
48 bool bFound = false;
49 for ( size_t nTst = 0; nTst < GetPageDescCnt(); ++nTst )
50 if ( &rDesc == &GetPageDesc( nTst ) )
51 bFound = true;
52 OSL_ENSURE( bFound, "ChgCurPageDesc with invalid descriptor." );
53 #endif
55 StartAllAction();
57 SwPageFrame *pPage = GetCurrFrame()->FindPageFrame();
58 const SwFrame *pFlow = nullptr;
59 ::std::optional<sal_uInt16> oPageNumOffset;
61 OSL_ENSURE( !GetCursor()->HasMark(), "ChgCurPageDesc only without selection!");
63 CurrShell aCurr( this );
64 while ( pPage )
66 pFlow = pPage->FindFirstBodyContent();
67 if ( pFlow )
69 if ( pFlow->IsInTab() )
70 pFlow = pFlow->FindTabFrame();
71 const SwFormatPageDesc& rPgDesc = pFlow->GetPageDescItem();
72 if( rPgDesc.GetPageDesc() )
74 // we found the culprit
75 oPageNumOffset = rPgDesc.GetNumOffset();
76 break;
79 pPage = static_cast<SwPageFrame*>( pPage->GetPrev() );
81 if ( !pPage )
83 pPage = static_cast<SwPageFrame*>(GetLayout()->Lower());
84 pFlow = pPage->FindFirstBodyContent();
85 if ( !pFlow )
87 pPage = static_cast<SwPageFrame*>(pPage->GetNext());
88 pFlow = pPage->FindFirstBodyContent();
89 assert(pFlow && "Document without content?!?");
93 // use pagenumber
94 SwFormatPageDesc aNew( &rDesc );
95 aNew.SetNumOffset( oPageNumOffset );
97 if ( pFlow->IsInTab() )
98 GetDoc()->SetAttr( aNew, *const_cast<SwFormat*>(static_cast<SwFormat const *>(pFlow->FindTabFrame()->GetFormat())) );
99 else
101 assert(pFlow->IsContentFrame());
102 SwPaM aPaM( pFlow->IsTextFrame()
103 ? *static_cast<SwTextFrame const*>(pFlow)->GetTextNodeFirst() // first, for PAGEDESC
104 : *static_cast<const SwNoTextFrame*>(pFlow)->GetNode() );
105 GetDoc()->getIDocumentContentOperations().InsertPoolItem(
106 aPaM, aNew, SetAttrMode::DEFAULT, GetLayout());
108 EndAllActionAndCall();
111 void SwFEShell::ChgPageDesc( size_t i, const SwPageDesc &rChged )
113 StartAllAction();
114 CurrShell aCurr( this );
115 //Fix i64842: because Undo has a very special way to handle header/footer content
116 // we have to copy the page descriptor before calling ChgPageDesc.
117 SwPageDesc aDesc( rChged );
119 ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
120 GetDoc()->CopyPageDesc(rChged, aDesc);
122 GetDoc()->ChgPageDesc( i, aDesc );
123 EndAllActionAndCall();
126 const SwPageDesc& SwFEShell::GetPageDesc( size_t i ) const
128 return GetDoc()->GetPageDesc( i );
131 SwPageDesc* SwFEShell::FindPageDescByName( const OUString& rName,
132 bool bGetFromPool,
133 size_t* pPos )
135 SwPageDesc* pDesc = GetDoc()->FindPageDesc(rName, pPos);
136 if( !pDesc && bGetFromPool )
138 sal_uInt16 nPoolId = SwStyleNameMapper::GetPoolIdFromUIName( rName, SwGetPoolIdFromName::PageDesc );
139 if( USHRT_MAX != nPoolId &&
140 nullptr != (pDesc = GetDoc()->getIDocumentStylePoolAccess().GetPageDescFromPool( nPoolId ))
141 && pPos )
142 // appended always
143 *pPos = GetDoc()->GetPageDescCnt() - 1 ;
145 return pDesc;
148 size_t SwFEShell::GetMousePageDesc( const Point &rPt ) const
150 if( GetLayout() )
152 const SwPageFrame* pPage =
153 static_cast<const SwPageFrame*>( GetLayout()->Lower() );
154 if( pPage )
156 while( pPage->GetNext() && rPt.Y() > pPage->getFrameArea().Bottom() )
157 pPage = static_cast<const SwPageFrame*>( pPage->GetNext() );
158 SwDoc *pMyDoc = GetDoc();
159 size_t nPos;
160 if (pMyDoc->ContainsPageDesc( pPage->GetPageDesc(), &nPos ) )
161 return nPos;
164 return 0;
167 size_t SwFEShell::GetCurPageDesc( const bool bCalcFrame ) const
169 const SwFrame *pFrame = GetCurrFrame( bCalcFrame );
170 if ( pFrame )
172 const SwPageFrame *pPage = pFrame->FindPageFrame();
173 if ( pPage )
175 size_t nPos;
176 if (GetDoc()->ContainsPageDesc( pPage->GetPageDesc(), &nPos ))
177 return nPos;
180 return 0;
183 // if inside all selection only one PageDesc, return this.
184 // Otherwise return 0 pointer
185 const SwPageDesc* SwFEShell::GetSelectedPageDescs() const
187 const SwContentNode* pCNd;
188 const SwFrame* pMkFrame, *pPtFrame;
189 const SwPageDesc* pFnd, *pRetDesc = reinterpret_cast<SwPageDesc*>(sal_IntPtr(-1));
190 const Point aNulPt;
191 std::pair<Point, bool> const tmp(aNulPt, false);
193 for(const SwPaM& rPaM : GetCursor()->GetRingContainer())
196 if( nullptr != (pCNd = rPaM.GetPointContentNode() ) &&
197 nullptr != (pPtFrame = pCNd->getLayoutFrame(GetLayout(), nullptr, &tmp)))
198 pPtFrame = pPtFrame->FindPageFrame();
199 else
200 pPtFrame = nullptr;
202 if( rPaM.HasMark() &&
203 nullptr != (pCNd = rPaM.GetMarkContentNode() ) &&
204 nullptr != (pMkFrame = pCNd->getLayoutFrame(GetLayout(), nullptr, &tmp)))
205 pMkFrame = pMkFrame->FindPageFrame();
206 else
207 pMkFrame = pPtFrame;
209 if( !pMkFrame || !pPtFrame )
210 pFnd = nullptr;
211 else if( pMkFrame == pPtFrame )
212 pFnd = static_cast<const SwPageFrame*>(pMkFrame)->GetPageDesc();
213 else
215 // swap pointer if PtFrame before MkFrame
216 if( static_cast<const SwPageFrame*>(pMkFrame)->GetPhyPageNum() >
217 static_cast<const SwPageFrame*>(pPtFrame)->GetPhyPageNum() )
219 const SwFrame* pTmp = pMkFrame; pMkFrame = pPtFrame; pPtFrame = pTmp;
222 // now check from MkFrame to PtFrame for equal PageDescs
223 pFnd = static_cast<const SwPageFrame*>(pMkFrame)->GetPageDesc();
224 while( pFnd && pMkFrame != pPtFrame )
226 pMkFrame = pMkFrame->GetNext();
227 if( !pMkFrame || pFnd != static_cast<const SwPageFrame*>(pMkFrame)->GetPageDesc() )
228 pFnd = nullptr;
232 if( reinterpret_cast<SwPageDesc*>(sal_IntPtr(-1)) == pRetDesc )
233 pRetDesc = pFnd;
234 else if( pFnd != pRetDesc )
236 pRetDesc = nullptr;
237 break;
242 return pRetDesc;
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */