LibreOffice Module sw (master)  1
fedesc.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <fesh.hxx>
21 #include <doc.hxx>
22 #include <IDocumentUndoRedo.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>
37 
39 {
40  return GetDoc()->GetPageDescCnt();
41 }
42 
44 {
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
54 
56 
58  const SwFrame *pFlow = nullptr;
59  ::std::optional<sal_uInt16> oPageNumOffset;
60 
61  OSL_ENSURE( !GetCursor()->HasMark(), "ChgCurPageDesc only without selection!");
62 
63  CurrShell aCurr( this );
64  while ( pPage )
65  {
66  pFlow = pPage->FindFirstBodyContent();
67  if ( pFlow )
68  {
69  if ( pFlow->IsInTab() )
70  pFlow = pFlow->FindTabFrame();
71  const SwFormatPageDesc& rPgDesc = pFlow->GetPageDescItem();
72  if( rPgDesc.GetPageDesc() )
73  {
74  // we found the culprit
75  oPageNumOffset = rPgDesc.GetNumOffset();
76  break;
77  }
78  }
79  pPage = static_cast<SwPageFrame*>( pPage->GetPrev() );
80  }
81  if ( !pPage )
82  {
83  pPage = static_cast<SwPageFrame*>(GetLayout()->Lower());
84  pFlow = pPage->FindFirstBodyContent();
85  if ( !pFlow )
86  {
87  pPage = static_cast<SwPageFrame*>(pPage->GetNext());
88  pFlow = pPage->FindFirstBodyContent();
89  OSL_ENSURE( pFlow, "Document without content?!?" );
90  }
91  }
92 
93  // use pagenumber
94  SwFormatPageDesc aNew( &rDesc );
95  aNew.SetNumOffset( oPageNumOffset );
96 
97  if ( pFlow->IsInTab() )
98  GetDoc()->SetAttr( aNew, *const_cast<SwFormat*>(static_cast<SwFormat const *>(pFlow->FindTabFrame()->GetFormat())) );
99  else
100  {
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() );
106  aPaM, aNew, SetAttrMode::DEFAULT, GetLayout());
107  }
109 }
110 
111 void SwFEShell::ChgPageDesc( size_t i, const SwPageDesc &rChged )
112 {
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 );
118  {
119  ::sw::UndoGuard const undoGuard(GetDoc()->GetIDocumentUndoRedo());
120  GetDoc()->CopyPageDesc(rChged, aDesc);
121  }
122  GetDoc()->ChgPageDesc( i, aDesc );
124 }
125 
126 const SwPageDesc& SwFEShell::GetPageDesc( size_t i ) const
127 {
128  return GetDoc()->GetPageDesc( i );
129 }
130 
132  bool bGetFromPool,
133  size_t* pPos )
134 {
135  SwPageDesc* pDesc = GetDoc()->FindPageDesc(rName, pPos);
136  if( !pDesc && bGetFromPool )
137  {
139  if( USHRT_MAX != nPoolId &&
140  nullptr != (pDesc = GetDoc()->getIDocumentStylePoolAccess().GetPageDescFromPool( nPoolId ))
141  && pPos )
142  // appended always
143  *pPos = GetDoc()->GetPageDescCnt() - 1 ;
144  }
145  return pDesc;
146 }
147 
148 size_t SwFEShell::GetMousePageDesc( const Point &rPt ) const
149 {
150  if( GetLayout() )
151  {
152  const SwPageFrame* pPage =
153  static_cast<const SwPageFrame*>( GetLayout()->Lower() );
154  if( pPage )
155  {
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;
162  }
163  }
164  return 0;
165 }
166 
167 size_t SwFEShell::GetCurPageDesc( const bool bCalcFrame ) const
168 {
169  const SwFrame *pFrame = GetCurrFrame( bCalcFrame );
170  if ( pFrame )
171  {
172  const SwPageFrame *pPage = pFrame->FindPageFrame();
173  if ( pPage )
174  {
175  size_t nPos;
176  if (GetDoc()->ContainsPageDesc( pPage->GetPageDesc(), &nPos ))
177  return nPos;
178  }
179  }
180  return 0;
181 }
182 
183 // if inside all selection only one PageDesc, return this.
184 // Otherwise return 0 pointer
186 {
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);
192 
193  for(const SwPaM& rPaM : GetCursor()->GetRingContainer())
194  {
195 
196  if( nullptr != (pCNd = rPaM.GetContentNode() ) &&
197  nullptr != (pPtFrame = pCNd->getLayoutFrame(GetLayout(), nullptr, &tmp)))
198  pPtFrame = pPtFrame->FindPageFrame();
199  else
200  pPtFrame = nullptr;
201 
202  if( rPaM.HasMark() &&
203  nullptr != (pCNd = rPaM.GetContentNode( false ) ) &&
204  nullptr != (pMkFrame = pCNd->getLayoutFrame(GetLayout(), nullptr, &tmp)))
205  pMkFrame = pMkFrame->FindPageFrame();
206  else
207  pMkFrame = pPtFrame;
208 
209  if( !pMkFrame || !pPtFrame )
210  pFnd = nullptr;
211  else if( pMkFrame == pPtFrame )
212  pFnd = static_cast<const SwPageFrame*>(pMkFrame)->GetPageDesc();
213  else
214  {
215  // swap pointer if PtFrame before MkFrame
216  if( static_cast<const SwPageFrame*>(pMkFrame)->GetPhyPageNum() >
217  static_cast<const SwPageFrame*>(pPtFrame)->GetPhyPageNum() )
218  {
219  const SwFrame* pTmp = pMkFrame; pMkFrame = pPtFrame; pPtFrame = pTmp;
220  }
221 
222  // now check from MkFrame to PtFrame for equal PageDescs
223  pFnd = static_cast<const SwPageFrame*>(pMkFrame)->GetPageDesc();
224  while( pFnd && pMkFrame != pPtFrame )
225  {
226  pMkFrame = pMkFrame->GetNext();
227  if( !pMkFrame || pFnd != static_cast<const SwPageFrame*>(pMkFrame)->GetPageDesc() )
228  pFnd = nullptr;
229  }
230  }
231 
232  if( reinterpret_cast<SwPageDesc*>(sal_IntPtr(-1)) == pRetDesc )
233  pRetDesc = pFnd;
234  else if( pFnd != pRetDesc )
235  {
236  pRetDesc = nullptr;
237  break;
238  }
239 
240  }
241 
242  return pRetDesc;
243 }
244 
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPRIVATE void EndAllActionAndCall()
Terminate actions for all shells and call ChangeLink.
Definition: fews.cxx:69
Base class of the Writer layout elements.
Definition: frame.hxx:313
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:157
const ::std::optional< sal_uInt16 > & GetNumOffset() const
Definition: fmtpdsc.hxx:64
bool ContainsPageDesc(const SwPageDesc *pDesc, size_t *pPos) const
Definition: docdesc.cxx:949
SwContentNode * GetNode(SwPaM &rPam, bool &rbFirst, SwMoveFnCollection const &fnMove, bool const bInReadOnly, SwRootFrame const *const i_pLayout)
This function returns the next node in direction of search.
Definition: pam.cxx:823
const SwPageDesc * GetSelectedPageDescs() const
if inside all selection only one PageDesc,
Definition: fedesc.cxx:185
Pagedescriptor Client of SwPageDesc that is "described" by the attribute.
Definition: fmtpdsc.hxx:35
SwPaM * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:194
size_t GetPageDescCnt() const
Definition: fedesc.cxx:38
SwPageDesc * GetPageDesc()
Definition: fmtpdsc.hxx:61
sal_uInt16 char char * pDesc
SwContentFrame * getLayoutFrame(const SwRootFrame *, const SwPosition *pPos=nullptr, std::pair< Point, bool > const *pViewPosAndCalcFrame=nullptr) const
Definition: node.cxx:1212
Definition: doc.hxx:187
SwContentFrame * FindFirstBodyContent()
Definition: pagefrm.hxx:350
virtual const SwFormatPageDesc & GetPageDescItem() const
Definition: findfrm.cxx:660
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:315
size_t GetMousePageDesc(const Point &rPt) const
Definition: fedesc.cxx:148
const SwRect & getFrameArea() const
Definition: frame.hxx:178
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
bool IsInTab() const
Definition: frame.hxx:951
void SetAttr(const SfxPoolItem &, SwFormat &)
Set attribute in given format.1y If Undo is enabled, the old values is added to the Undo history...
Definition: docfmt.cxx:451
bool IsTextFrame() const
Definition: frame.hxx:1230
static SW_DLLPUBLIC sal_uInt16 GetPoolIdFromUIName(const OUString &rName, SwGetPoolIdFromName)
IDocumentStylePoolAccess & getIDocumentStylePoolAccess()
Provides access to the document style pool interface.
Definition: viewsh.cxx:2674
SwDoc * GetDoc() const
Definition: viewsh.hxx:281
SwPageDesc * FindPageDesc(const OUString &rName, size_t *pPos=nullptr) const
Definition: docdesc.cxx:944
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
bool IsContentFrame() const
Definition: frame.hxx:1226
const SwPageDesc & GetPageDesc(const size_t i) const
Definition: doc.hxx:882
SwPageFrame * FindPageFrame()
Definition: frame.hxx:678
sal_uInt16 GetPhyPageNum() const
Phy: real page count.
Definition: fews.cxx:335
const SwFrame * Lower() const
Definition: layfrm.hxx:101
bool HasMark() const
Definition: crsrsh.hxx:877
SwPageDesc * GetPageDesc()
Definition: pagefrm.hxx:143
SwContentNode * GetContentNode()
Definition: node.hxx:619
const SwPageDesc & GetPageDesc(size_t i) const
Definition: fedesc.cxx:126
size_t GetPageDescCnt() const
Definition: doc.hxx:881
void ChgPageDesc(const OUString &rName, const SwPageDesc &)
Definition: docdesc.cxx:975
SwFrame * GetPrev()
Definition: frame.hxx:675
ring_container GetRingContainer()
A page of the document layout.
Definition: pagefrm.hxx:57
IDocumentUndoRedo const & GetIDocumentUndoRedo() const
Provides access to the document undo/redo interface.
Definition: viewsh.cxx:2679
void Bottom(const tools::Long nBottom)
Definition: swrect.hxx:209
void CopyPageDesc(const SwPageDesc &rSrcDesc, SwPageDesc &rDstDesc, bool bCopyPoolIds=true)
Copy the complete PageDesc - beyond document and "deep"! Optionally copying of PoolFormatId, -HlpId can be prevented.
Definition: docfmt.cxx:1416
SwContentFrame * GetCurrFrame(const bool bCalcFrame=true) const
Get current frame in which the cursor is positioned.
Definition: crsrsh.cxx:2444
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:254
virtual bool InsertPoolItem(const SwPaM &rRg, const SfxPoolItem &, const SetAttrMode nFlags=SetAttrMode::DEFAULT, SwRootFrame const *pLayout=nullptr, bool bExpandCharToPara=false, SwTextAttr **ppNewTextAttr=nullptr)=0
Insert an attribute.
void ChgPageDesc(size_t i, const SwPageDesc &)
Definition: fedesc.cxx:111
SwPageDesc * GetPageDescFromPool(sal_uInt16 nId)
Definition: edfmt.cxx:134
SwPageDesc * FindPageDescByName(const OUString &rName, bool bGetFromPool=false, size_t *pPos=nullptr)
Definition: fedesc.cxx:131
void SetNumOffset(const ::std::optional< sal_uInt16 > &oNum)
Definition: fmtpdsc.hxx:65
void ChgCurPageDesc(const SwPageDesc &)
PageDescriptor-interface.
Definition: fedesc.cxx:43
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2067
size_t GetCurPageDesc(const bool bCalcFrame=true) const
Definition: fedesc.cxx:167
sal_uInt16 nPos
virtual const SwFrameFormat * GetFormat() const
Definition: ssfrm.cxx:395
SwTabFrame * FindTabFrame()
Definition: frame.hxx:1095
SwFrame * GetNext()
Definition: frame.hxx:674