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 <swwait.hxx>
29 #include <swundo.hxx>
30 #include <section.hxx>
31 #include <doctxm.hxx>
32 #include <edglbldc.hxx>
33 #include <osl/diagnose.h>
34 
36 {
38 }
39 
41 {
43  if( !GetDoc()->getIDocumentState().IsModified() ) // Bug 57028
44  {
46  }
48 }
49 
51 {
53 }
54 
56 {
57  rArr.clear();
58 
60  return;
61 
62  // then all linked areas on the topmost level
63  SwDoc* pMyDoc = GetDoc();
64  const SwSectionFormats& rSectFormats = pMyDoc->GetSections();
65 
66  for( auto n = rSectFormats.size(); n; )
67  {
68  const SwSection* pSect = rSectFormats[ --n ]->GetGlobalDocSection();
69  if( pSect )
70  {
71  std::unique_ptr<SwGlblDocContent> pNew;
72  switch( pSect->GetType() )
73  {
74  case TOX_HEADER_SECTION:
75  break; // ignore
77  OSL_ENSURE( dynamic_cast<const SwTOXBaseSection*>( pSect) != nullptr, "no TOXBaseSection!" );
78  pNew.reset(new SwGlblDocContent( static_cast<const SwTOXBaseSection*>(pSect) ));
79  break;
80 
81  default:
82  pNew.reset(new SwGlblDocContent( pSect ));
83  break;
84  }
85  rArr.insert( std::move(pNew) );
86  }
87  }
88 
89  // and finally add the dummies (other text)
90  SwNode* pNd;
91  sal_uLong nSttIdx = pMyDoc->GetNodes().GetEndOfExtras().GetIndex() + 2;
92  for( SwGlblDocContents::size_type n = 0; n < rArr.size(); ++n )
93  {
94  const SwGlblDocContent& rNew = *rArr[ n ];
95  // Search from StartPos until rNew.DocPos for a content node.
96  // If one exists then a dummy entry is needed.
97  for( ; nSttIdx < rNew.GetDocPos(); ++nSttIdx )
98  if( ( pNd = pMyDoc->GetNodes()[ nSttIdx ])->IsContentNode()
99  || pNd->IsSectionNode() || pNd->IsTableNode() )
100  {
101  std::unique_ptr<SwGlblDocContent> pNew(new SwGlblDocContent( nSttIdx ));
102  if( rArr.insert( std::move(pNew) ).second )
103  ++n; // to the next position
104  break;
105  }
106 
107  // set StartPosition to the end
108  nSttIdx = pMyDoc->GetNodes()[ rNew.GetDocPos() ]->EndOfSectionIndex();
109  ++nSttIdx;
110  }
111 
112  // Should the end also be set?
113  if( !rArr.empty() )
114  {
115  sal_uLong nNdEnd = pMyDoc->GetNodes().GetEndOfContent().GetIndex();
116  for( ; nSttIdx < nNdEnd; ++nSttIdx )
117  if( ( pNd = pMyDoc->GetNodes()[ nSttIdx ])->IsContentNode()
118  || pNd->IsSectionNode() || pNd->IsTableNode() )
119  {
120  rArr.insert( std::make_unique<SwGlblDocContent>( nSttIdx ) );
121  break;
122  }
123  }
124  else
125  {
126  std::unique_ptr<SwGlblDocContent> pNew(new SwGlblDocContent(
127  pMyDoc->GetNodes().GetEndOfExtras().GetIndex() + 2 ));
128  rArr.insert( std::move(pNew) );
129  }
130 }
131 
133  SwSectionData & rNew)
134 {
136  return;
137 
138  SET_CURR_SHELL( this );
139  StartAllAction();
140 
141  SwPaM* pCursor = GetCursor();
142  if( pCursor->GetNext() != pCursor || IsTableMode() )
143  ClearMark();
144 
145  SwPosition& rPos = *pCursor->GetPoint();
146  rPos.nNode = rInsPos.GetDocPos();
147 
148  bool bEndUndo = false;
149  SwDoc* pMyDoc = GetDoc();
150  SwTextNode *const pTextNd = rPos.nNode.GetNode().GetTextNode();
151  if( pTextNd )
152  rPos.nContent.Assign( pTextNd, 0 );
153  else
154  {
155  bEndUndo = true;
156  pMyDoc->GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
157  --rPos.nNode;
159  pCursor->SetMark();
160  }
161 
162  InsertSection( rNew );
163 
164  if( bEndUndo )
165  {
166  pMyDoc->GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
167  }
168  EndAllAction();
169 }
170 
172  const SwTOXBase& rTOX )
173 {
175  return false;
176 
177  SET_CURR_SHELL( this );
178  StartAllAction();
179 
180  SwPaM* pCursor = GetCursor();
181  if( pCursor->GetNext() != pCursor || IsTableMode() )
182  ClearMark();
183 
184  SwPosition& rPos = *pCursor->GetPoint();
185  rPos.nNode = rInsPos.GetDocPos();
186 
187  bool bEndUndo = false;
188  SwDoc* pMyDoc = GetDoc();
189  SwTextNode* pTextNd = rPos.nNode.GetNode().GetTextNode();
190  if (pTextNd && pTextNd->GetText().getLength() && rPos.nNode.GetIndex() + 1 !=
191  pMyDoc->GetNodes().GetEndOfContent().GetIndex() )
192  rPos.nContent.Assign( pTextNd, 0 );
193  else
194  {
195  bEndUndo = true;
196  pMyDoc->GetIDocumentUndoRedo().StartUndo( SwUndoId::START, nullptr );
197  --rPos.nNode;
199  }
200 
201  InsertTableOf( rTOX );
202 
203  if( bEndUndo )
204  {
205  pMyDoc->GetIDocumentUndoRedo().EndUndo( SwUndoId::END, nullptr );
206  }
207  EndAllAction();
208 
209  return true;
210 }
211 
213 {
215  return false;
216 
217  SET_CURR_SHELL( this );
218  StartAllAction();
219 
220  SwPaM* pCursor = GetCursor();
221  if( pCursor->GetNext() != pCursor || IsTableMode() )
222  ClearMark();
223 
224  SwPosition& rPos = *pCursor->GetPoint();
225  rPos.nNode = rInsPos.GetDocPos() - 1;
226  rPos.nContent.Assign( nullptr, 0 );
227 
228  SwDoc* pMyDoc = GetDoc();
230  EndAllAction();
231  return true;
232 }
233 
235  size_t nDelPos )
236 {
238  return;
239 
240  SET_CURR_SHELL( this );
241  StartAllAction();
243 
244  SwPaM* pCursor = GetCursor();
245  if( pCursor->GetNext() != pCursor || IsTableMode() )
246  ClearMark();
247 
248  SwPosition& rPos = *pCursor->GetPoint();
249 
250  SwDoc* pMyDoc = GetDoc();
251  const SwGlblDocContent& rDelPos = *rArr[ nDelPos ];
252  sal_uLong nDelIdx = rDelPos.GetDocPos();
253  if( 1 == rArr.size() )
254  {
255  // we need at least one node!
256  rPos.nNode = nDelIdx - 1;
257  rPos.nContent.Assign( nullptr, 0 );
258 
260  ++nDelIdx;
261  }
262 
263  switch( rDelPos.GetType() )
264  {
265  case GLBLDOC_UNKNOWN:
266  {
267  rPos.nNode = nDelIdx;
268  pCursor->SetMark();
269  if( ++nDelPos < rArr.size() )
270  rPos.nNode = rArr[ nDelPos ]->GetDocPos();
271  else
272  rPos.nNode = pMyDoc->GetNodes().GetEndOfContent();
273  --rPos.nNode;
274  if( !pMyDoc->getIDocumentContentOperations().DelFullPara( *pCursor ) )
275  Delete();
276  }
277  break;
278 
279  case GLBLDOC_TOXBASE:
280  {
281  const SwTOXBaseSection* pTOX = static_cast<const SwTOXBaseSection*>(rDelPos.GetTOX());
282  pMyDoc->DeleteTOX( *pTOX, true );
283  }
284  break;
285 
286  case GLBLDOC_SECTION:
287  {
288  SwSectionFormat* pSectFormat = const_cast<SwSectionFormat*>(rDelPos.GetSection()->GetFormat());
289  pMyDoc->DelSectionFormat( pSectFormat, true );
290  }
291  break;
292  }
293 
295  EndAllAction();
296 }
297 
299  size_t nFromPos, size_t nToPos,
300  size_t nInsPos )
301 {
303  nFromPos >= rArr.size() || nToPos > rArr.size() ||
304  nInsPos > rArr.size() || nFromPos >= nToPos ||
305  ( nFromPos <= nInsPos && nInsPos <= nToPos ) )
306  return false;
307 
308  SET_CURR_SHELL( this );
309  StartAllAction();
310 
311  SwPaM* pCursor = GetCursor();
312  if( pCursor->GetNext() != pCursor || IsTableMode() )
313  ClearMark();
314 
315  SwDoc* pMyDoc = GetDoc();
316  SwNodeRange aRg( pMyDoc->GetNodes(), rArr[ nFromPos ]->GetDocPos() );
317  if( nToPos < rArr.size() )
318  aRg.aEnd = rArr[ nToPos ]->GetDocPos();
319  else
320  aRg.aEnd = pMyDoc->GetNodes().GetEndOfContent();
321 
322  SwNodeIndex aInsPos( pMyDoc->GetNodes() );
323  if( nInsPos < rArr.size() )
324  aInsPos = rArr[ nInsPos ]->GetDocPos();
325  else
326  aInsPos = pMyDoc->GetNodes().GetEndOfContent();
327 
328  bool bRet = pMyDoc->getIDocumentContentOperations().MoveNodeRange( aRg, aInsPos,
330 
331  EndAllAction();
332  return bRet;
333 }
334 
336 {
338  return;
339 
340  SET_CURR_SHELL( this );
341  SttCursorMove();
342 
343  SwPaM* pCursor = GetCursor();
344  if( pCursor->GetNext() != pCursor || IsTableMode() )
345  ClearMark();
346 
347  SwPosition& rCursorPos = *pCursor->GetPoint();
348  rCursorPos.nNode = rPos.GetDocPos();
349 
350  SwDoc* pMyDoc = GetDoc();
351  SwContentNode * pCNd = rCursorPos.nNode.GetNode().GetContentNode();
352  if( !pCNd )
353  pCNd = pMyDoc->GetNodes().GoNext( &rCursorPos.nNode );
354 
355  rCursorPos.nContent.Assign( pCNd, 0 );
356 
357  EndCursorMove();
358 }
359 
361 {
363  PTR.pTOX = nullptr;
364  nDocPos = nPos;
365 }
366 
368 {
370  PTR.pTOX = pTOX;
371 
372  const SwSectionNode* pSectNd = pTOX->GetFormat()->GetSectionNode();
373  nDocPos = pSectNd ? pSectNd->GetIndex() : 0;
374 }
375 
377 {
379  PTR.pSect = pSect;
380 
381  const SwSectionNode* pSectNd = pSect->GetFormat()->GetSectionNode();
382  nDocPos = pSectNd ? pSectNd->GetIndex() : 0;
383 }
384 
385 /* 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:185
bool MoveGlobalDocContent(const SwGlblDocContents &rArr, size_t nFromPos, size_t nToPos, size_t nNewPos)
Definition: edglbldc.cxx:298
const OUString & GetText() const
Definition: ndtxt.hxx:211
void DelSectionFormat(SwSectionFormat *pFormat, bool bDelNodes=false)
Definition: ndsect.cxx:515
void InsertTableOf(const SwTOXBase &rTOX, const SfxItemSet *pSet=nullptr)
Insert content table. Renew if required.
Definition: edtox.cxx:129
SwNodeIndex nNode
Definition: pam.hxx:37
bool IsTableMode() const
Definition: crsrsh.hxx:643
SwGlblDocContent(sal_uLong nPos)
Definition: edglbldc.cxx:360
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
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:35
Definition: doc.hxx:185
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:2588
SwNode & GetNode() const
Definition: ndindex.hxx:118
SwSectionFormat * GetFormat()
Definition: section.hxx:337
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:176
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:347
const SwSection * pSect
Definition: edglbldc.hxx:40
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:291
SwIndex nContent
Definition: pam.hxx:38
SwSectionNode * GetSectionNode()
Definition: section.cxx:1007
bool IsGlblDocSaveLinks() const
Definition: edglbldc.cxx:50
sal_uLong GetIndex() const
Definition: ndindex.hxx:151
bool DeleteTOX(const SwTOXBase &rTOXBase, bool bDelNodes)
Delete table of contents.
Definition: doctxm.cxx:525
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:164
void EndCursorMove(const bool bIdleEnd=false)
Definition: crsrsh.cxx:300
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:234
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:426
#define SET_CURR_SHELL(shell)
Definition: swtypes.hxx:101
union SwGlblDocContent::@26 PTR
Marks a node in the document model.
Definition: ndindex.hxx:31
bool empty() const
GlobalDocContentType eType
Definition: edglbldc.hxx:36
const SwTOXBase * pTOX
Definition: edglbldc.hxx:39
virtual bool MoveNodeRange(SwNodeRange &, SwNodeIndex &, SwMoveFlags)=0
void GetGlobalDocContent(SwGlblDocContents &rArr) const
Definition: edglbldc.cxx:55
void GotoGlobalDocContent(const SwGlblDocContent &rPos)
Definition: edglbldc.cxx:335
bool Delete()
Delete content of all ranges.
Definition: eddel.cxx:117
const SwTOXBase * GetTOX() const
Definition: edglbldc.hxx:52
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
void InsertGlobalDocContent(const SwGlblDocContent &rPos, SwSectionData &rNew)
Definition: edglbldc.cxx:132
GlobalDocContentType GetType() const
Query contents.
Definition: edglbldc.hxx:49
virtual bool DelFullPara(SwPaM &)=0
Delete full paragraphs.
SwNodes & GetNodes()
Definition: doc.hxx:403
const SwSection * GetSection() const
Definition: edglbldc.hxx:50
SwNodeIndex aEnd
Definition: ndindex.hxx:132
virtual bool get(DocumentSettingId id) const =0
Return the specified document setting.
void ClearMark()
Definition: crsrsh.cxx:927
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:457
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:162
void StartAllAction()
For all views of this document.
Definition: edws.cxx:85
SwSectionFormats & GetSections()
Definition: doc.hxx:1326
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1273
void SetGlblDocSaveLinks(bool bFlag)
Definition: edglbldc.cxx:40
sal_uLong GetDocPos() const
Definition: edglbldc.hxx:54
sal_uLong nDocPos
Definition: edglbldc.hxx:37
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:843
bool IsGlobalDoc() const
Interfaces for GlobalDocument.
Definition: edglbldc.cxx:35
Base class of the Writer document model elements.
Definition: node.hxx:79