LibreOffice Module sw (master)  1
edglbldc.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 <doc.hxx>
21 #include <IDocumentUndoRedo.hxx>
23 #include <IDocumentState.hxx>
24 #include <editsh.hxx>
25 #include <pam.hxx>
26 #include <ndtxt.hxx>
27 #include <docary.hxx>
28 #include <swundo.hxx>
29 #include <section.hxx>
30 #include <doctxm.hxx>
31 #include <edglbldc.hxx>
32 #include <osl/diagnose.h>
33 
35 {
37 }
38 
40 {
42  if( !GetDoc()->getIDocumentState().IsModified() ) // Bug 57028
43  {
45  }
47 }
48 
50 {
52 }
53 
55 {
56  rArr.clear();
57 
59  return;
60 
61  // then all linked areas on the topmost level
62  SwDoc* pMyDoc = GetDoc();
63  const SwSectionFormats& rSectFormats = pMyDoc->GetSections();
64 
65  for( auto n = rSectFormats.size(); n; )
66  {
67  const SwSection* pSect = rSectFormats[ --n ]->GetGlobalDocSection();
68  if( pSect )
69  {
70  std::unique_ptr<SwGlblDocContent> pNew;
71  switch( pSect->GetType() )
72  {
74  break; // ignore
76  OSL_ENSURE( dynamic_cast<const SwTOXBaseSection*>( pSect) != nullptr, "no TOXBaseSection!" );
77  pNew.reset(new SwGlblDocContent( static_cast<const SwTOXBaseSection*>(pSect) ));
78  break;
79 
80  default:
81  pNew.reset(new SwGlblDocContent( pSect ));
82  break;
83  }
84  rArr.insert( std::move(pNew) );
85  }
86  }
87 
88  // and finally add the dummies (other text)
89  SwNode* pNd;
90  sal_uLong nSttIdx = pMyDoc->GetNodes().GetEndOfExtras().GetIndex() + 2;
91  for( SwGlblDocContents::size_type n = 0; n < rArr.size(); ++n )
92  {
93  const SwGlblDocContent& rNew = *rArr[ n ];
94  // Search from StartPos until rNew.DocPos for a content node.
95  // If one exists then a dummy entry is needed.
96  for( ; nSttIdx < rNew.GetDocPos(); ++nSttIdx )
97  if( ( pNd = pMyDoc->GetNodes()[ nSttIdx ])->IsContentNode()
98  || pNd->IsSectionNode() || pNd->IsTableNode() )
99  {
100  std::unique_ptr<SwGlblDocContent> pNew(new SwGlblDocContent( nSttIdx ));
101  if( rArr.insert( std::move(pNew) ).second )
102  ++n; // to the next position
103  break;
104  }
105 
106  // set StartPosition to the end
107  nSttIdx = pMyDoc->GetNodes()[ rNew.GetDocPos() ]->EndOfSectionIndex();
108  ++nSttIdx;
109  }
110 
111  // Should the end also be set?
112  if( !rArr.empty() )
113  {
114  sal_uLong nNdEnd = pMyDoc->GetNodes().GetEndOfContent().GetIndex();
115  for( ; nSttIdx < nNdEnd; ++nSttIdx )
116  if( ( pNd = pMyDoc->GetNodes()[ nSttIdx ])->IsContentNode()
117  || pNd->IsSectionNode() || pNd->IsTableNode() )
118  {
119  rArr.insert( std::make_unique<SwGlblDocContent>( nSttIdx ) );
120  break;
121  }
122  }
123  else
124  {
125  std::unique_ptr<SwGlblDocContent> pNew(new SwGlblDocContent(
126  pMyDoc->GetNodes().GetEndOfExtras().GetIndex() + 2 ));
127  rArr.insert( std::move(pNew) );
128  }
129 }
130 
132  SwSectionData & rNew)
133 {
135  return;
136 
137  SET_CURR_SHELL( this );
138  StartAllAction();
139 
140  SwPaM* pCursor = GetCursor();
141  if( pCursor->GetNext() != pCursor || IsTableMode() )
142  ClearMark();
143 
144  SwPosition& rPos = *pCursor->GetPoint();
145  rPos.nNode = rInsPos.GetDocPos();
146 
147  bool bEndUndo = false;
148  SwDoc* pMyDoc = GetDoc();
149  SwTextNode *const pTextNd = rPos.nNode.GetNode().GetTextNode();
150  if( pTextNd )
151  rPos.nContent.Assign( pTextNd, 0 );
152  else
153  {
154  bEndUndo = true;
155  pMyDoc->GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
156  --rPos.nNode;
158  pCursor->SetMark();
159  }
160 
161  InsertSection( rNew );
162 
163  if( bEndUndo )
164  {
165  pMyDoc->GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
166  }
167  EndAllAction();
168 }
169 
171  const SwTOXBase& rTOX )
172 {
174  return false;
175 
176  SET_CURR_SHELL( this );
177  StartAllAction();
178 
179  SwPaM* pCursor = GetCursor();
180  if( pCursor->GetNext() != pCursor || IsTableMode() )
181  ClearMark();
182 
183  SwPosition& rPos = *pCursor->GetPoint();
184  rPos.nNode = rInsPos.GetDocPos();
185 
186  bool bEndUndo = false;
187  SwDoc* pMyDoc = GetDoc();
188  SwTextNode* pTextNd = rPos.nNode.GetNode().GetTextNode();
189  if (pTextNd && pTextNd->GetText().getLength() && rPos.nNode.GetIndex() + 1 !=
190  pMyDoc->GetNodes().GetEndOfContent().GetIndex() )
191  rPos.nContent.Assign( pTextNd, 0 );
192  else
193  {
194  bEndUndo = true;
195  pMyDoc->GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
196  --rPos.nNode;
198  }
199 
200  InsertTableOf( rTOX );
201 
202  if( bEndUndo )
203  {
204  pMyDoc->GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
205  }
206  EndAllAction();
207 
208  return true;
209 }
210 
212 {
214  return false;
215 
216  SET_CURR_SHELL( this );
217  StartAllAction();
218 
219  SwPaM* pCursor = GetCursor();
220  if( pCursor->GetNext() != pCursor || IsTableMode() )
221  ClearMark();
222 
223  SwPosition& rPos = *pCursor->GetPoint();
224  rPos.nNode = rInsPos.GetDocPos() - 1;
225  rPos.nContent.Assign( nullptr, 0 );
226 
227  SwDoc* pMyDoc = GetDoc();
229  EndAllAction();
230  return true;
231 }
232 
234  size_t nDelPos )
235 {
237  return;
238 
239  SET_CURR_SHELL( this );
240  StartAllAction();
242 
243  SwPaM* pCursor = GetCursor();
244  if( pCursor->GetNext() != pCursor || IsTableMode() )
245  ClearMark();
246 
247  SwPosition& rPos = *pCursor->GetPoint();
248 
249  SwDoc* pMyDoc = GetDoc();
250  const SwGlblDocContent& rDelPos = *rArr[ nDelPos ];
251  sal_uLong nDelIdx = rDelPos.GetDocPos();
252  if( 1 == rArr.size() )
253  {
254  // we need at least one node!
255  rPos.nNode = nDelIdx - 1;
256  rPos.nContent.Assign( nullptr, 0 );
257 
259  ++nDelIdx;
260  }
261 
262  switch( rDelPos.GetType() )
263  {
264  case GLBLDOC_UNKNOWN:
265  {
266  rPos.nNode = nDelIdx;
267  pCursor->SetMark();
268  if( ++nDelPos < rArr.size() )
269  rPos.nNode = rArr[ nDelPos ]->GetDocPos();
270  else
271  rPos.nNode = pMyDoc->GetNodes().GetEndOfContent();
272  --rPos.nNode;
273  if( !pMyDoc->getIDocumentContentOperations().DelFullPara( *pCursor ) )
274  Delete();
275  }
276  break;
277 
278  case GLBLDOC_TOXBASE:
279  {
280  const SwTOXBaseSection* pTOX = static_cast<const SwTOXBaseSection*>(rDelPos.GetTOX());
281  pMyDoc->DeleteTOX( *pTOX, true );
282  }
283  break;
284 
285  case GLBLDOC_SECTION:
286  {
287  SwSectionFormat* pSectFormat = const_cast<SwSectionFormat*>(rDelPos.GetSection()->GetFormat());
288  pMyDoc->DelSectionFormat( pSectFormat, true );
289  }
290  break;
291  }
292 
294  EndAllAction();
295 }
296 
298  size_t nFromPos, size_t nToPos,
299  size_t nInsPos )
300 {
302  nFromPos >= rArr.size() || nToPos > rArr.size() ||
303  nInsPos > rArr.size() || nFromPos >= nToPos ||
304  ( nFromPos <= nInsPos && nInsPos <= nToPos ) )
305  return false;
306 
307  SET_CURR_SHELL( this );
308  StartAllAction();
309 
310  SwPaM* pCursor = GetCursor();
311  if( pCursor->GetNext() != pCursor || IsTableMode() )
312  ClearMark();
313 
314  SwDoc* pMyDoc = GetDoc();
315  SwNodeRange aRg( pMyDoc->GetNodes(), rArr[ nFromPos ]->GetDocPos() );
316  if( nToPos < rArr.size() )
317  aRg.aEnd = rArr[ nToPos ]->GetDocPos();
318  else
319  aRg.aEnd = pMyDoc->GetNodes().GetEndOfContent();
320 
321  SwNodeIndex aInsPos( pMyDoc->GetNodes() );
322  if( nInsPos < rArr.size() )
323  aInsPos = rArr[ nInsPos ]->GetDocPos();
324  else
325  aInsPos = pMyDoc->GetNodes().GetEndOfContent();
326 
327  bool bRet = pMyDoc->getIDocumentContentOperations().MoveNodeRange( aRg, aInsPos,
329 
330  EndAllAction();
331  return bRet;
332 }
333 
335 {
337  return;
338 
339  SET_CURR_SHELL( this );
340  SttCursorMove();
341 
342  SwPaM* pCursor = GetCursor();
343  if( pCursor->GetNext() != pCursor || IsTableMode() )
344  ClearMark();
345 
346  SwPosition& rCursorPos = *pCursor->GetPoint();
347  rCursorPos.nNode = rPos.GetDocPos();
348 
349  SwDoc* pMyDoc = GetDoc();
350  SwContentNode * pCNd = rCursorPos.nNode.GetNode().GetContentNode();
351  if( !pCNd )
352  pCNd = pMyDoc->GetNodes().GoNext( &rCursorPos.nNode );
353 
354  rCursorPos.nContent.Assign( pCNd, 0 );
355 
356  EndCursorMove();
357 }
358 
360 {
362  PTR.pTOX = nullptr;
363  nDocPos = nPos;
364 }
365 
367 {
369  PTR.pTOX = pTOX;
370 
371  const SwSectionNode* pSectNd = pTOX->GetFormat()->GetSectionNode();
372  nDocPos = pSectNd ? pSectNd->GetIndex() : 0;
373 }
374 
376 {
378  PTR.pSect = pSect;
379 
380  const SwSectionNode* pSectNd = pSect->GetFormat()->GetSectionNode();
381  nDocPos = pSectNd ? pSectNd->GetIndex() : 0;
382 }
383 
384 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uLong GetIndex() const
Definition: node.hxx:282
Marks a position in the document model.
Definition: pam.hxx:35
bool IsSectionNode() const
Definition: node.hxx:644
SwPaM * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:188
bool MoveGlobalDocContent(const SwGlblDocContents &rArr, size_t nFromPos, size_t nToPos, size_t nNewPos)
Definition: edglbldc.cxx:297
const OUString & GetText() const
Definition: ndtxt.hxx:210
void DelSectionFormat(SwSectionFormat *pFormat, bool bDelNodes=false)
Definition: ndsect.cxx:512
void InsertTableOf(const SwTOXBase &rTOX, const SfxItemSet *pSet=nullptr)
Insert content table. Renew if required.
Definition: edtox.cxx:121
SwNodeIndex nNode
Definition: pam.hxx:37
bool IsTableMode() const
Definition: crsrsh.hxx:647
union SwGlblDocContent::@25 PTR
SwGlblDocContent(sal_uLong nPos)
Definition: edglbldc.cxx:359
virtual void SetModified()=0
Must be called manually at changes of format.
sal_uIntPtr sal_uLong
bool IsModified() const
Changes in document?
Definition: edws.cxx:64
sal_Int64 n
virtual SwUndoId EndUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Closes undo block.
SwSection const * InsertSection(SwSectionData &rNewData, SfxItemSet const *const =nullptr)
Definition: edsect.cxx:34
Definition: doc.hxx:186
SwUndoId EndUndo(SwUndoId eUndoId=SwUndoId::EMPTY, const SwRewriter *pRewriter=nullptr)
Closes parenthesis of nUndoId, not used by UI.
Definition: edws.cxx:233
const IDocumentSettingAccess & getIDocumentSettingAccess() const
Provides access to the document setting interface.
Definition: viewsh.cxx:2578
SwNode & GetNode() const
Definition: ndindex.hxx:119
SwSectionFormat * GetFormat()
Definition: section.hxx:337
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:143
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:314
const SwSection * pSect
Definition: edglbldc.hxx:41
void EndAllAction()
Definition: edws.cxx:96
Array of Undo-history.
Definition: docary.hxx:299
virtual void set(DocumentSettingId id, bool value)=0
Set the specified document setting.
void SttCursorMove()
Definition: crsrsh.cxx:294
SwIndex nContent
Definition: pam.hxx:38
SwSectionNode * GetSectionNode()
Definition: section.cxx:1003
bool IsGlblDocSaveLinks() const
Definition: edglbldc.cxx:49
sal_uLong GetIndex() const
Definition: ndindex.hxx:152
bool DeleteTOX(const SwTOXBase &rTOXBase, bool bDelNodes)
Delete table of contents.
Definition: doctxm.cxx:523
SwDoc * GetDoc() const
Definition: viewsh.hxx:284
size_type size() const
SwPaM * GetNext()
Definition: pam.hxx:264
SwNode & GetEndOfContent() const
Regular ContentSection (i.e. the BodyText).
Definition: ndarr.hxx:163
void EndCursorMove(const bool bIdleEnd=false)
Definition: crsrsh.cxx:303
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
virtual SwUndoId StartUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Opens undo block.
const SwPosition * GetPoint() const
Definition: pam.hxx:207
void DeleteGlobalDocContent(const SwGlblDocContents &rArr, size_t nPos)
Definition: edglbldc.cxx:233
virtual void SetUndoNoResetModified()=0
Disable (re)setting the document modified flag on Undo/Redo.
SwIndex & Assign(SwIndexReg *, sal_Int32)
Definition: index.cxx:198
SwContentNode * GetContentNode()
Definition: node.hxx:615
SwUndoId StartUndo(SwUndoId eUndoId=SwUndoId::EMPTY, const SwRewriter *pRewriter=nullptr)
Undo: set up Undo parenthesis, return nUndoId of this parenthesis.
Definition: edws.cxx:222
size_t size() const
Definition: docary.hxx:91
IDocumentState const & getIDocumentState() const
Definition: doc.cxx:393
#define SET_CURR_SHELL(shell)
Definition: swtypes.hxx:101
Marks a node in the document model.
Definition: ndindex.hxx:31
bool empty() const
GlobalDocContentType eType
Definition: edglbldc.hxx:37
const SwTOXBase * pTOX
Definition: edglbldc.hxx:40
virtual bool MoveNodeRange(SwNodeRange &, SwNodeIndex &, SwMoveFlags)=0
void GetGlobalDocContent(SwGlblDocContents &rArr) const
Definition: edglbldc.cxx:54
void GotoGlobalDocContent(const SwGlblDocContent &rPos)
Definition: edglbldc.cxx:334
bool Delete()
Delete content of all ranges.
Definition: eddel.cxx:110
const SwTOXBase * GetTOX() const
Definition: edglbldc.hxx:53
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
void InsertGlobalDocContent(const SwGlblDocContent &rPos, SwSectionData &rNew)
Definition: edglbldc.cxx:131
GlobalDocContentType GetType() const
Query contents.
Definition: edglbldc.hxx:50
virtual bool DelFullPara(SwPaM &)=0
Delete full paragraphs.
SwNodes & GetNodes()
Definition: doc.hxx:404
const SwSection * GetSection() const
Definition: edglbldc.hxx:51
SwNodeIndex aEnd
Definition: ndindex.hxx:133
virtual bool get(DocumentSettingId id) const =0
Return the specified document setting.
void ClearMark()
Definition: crsrsh.cxx:930
virtual bool AppendTextNode(SwPosition &rPos)=0
bool IsTableNode() const
Definition: node.hxx:640
virtual void SetMark()
Unless this is called, the getter method of Mark will return Point.
Definition: pam.cxx:470
SectionType GetType() const
Definition: section.hxx:171
std::pair< const_iterator, bool > insert(Value &&x)
SwNode & GetEndOfExtras() const
This is the last EndNode of a special section.
Definition: ndarr.hxx:161
void StartAllAction()
For all views of this document.
Definition: edws.cxx:85
SwSectionFormats & GetSections()
Definition: doc.hxx:1334
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1277
void SetGlblDocSaveLinks(bool bFlag)
Definition: edglbldc.cxx:39
sal_uLong GetDocPos() const
Definition: edglbldc.hxx:55
sal_uLong nDocPos
Definition: edglbldc.hxx:38
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:842
bool IsGlobalDoc() const
Interfaces for GlobalDocument.
Definition: edglbldc.cxx:34
Base class of the Writer document model elements.
Definition: node.hxx:79