merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / edit / edglbldc.cxx
blob5e879462472487a91d4dacf4cb1b28fa92ecff8c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: edglbldc.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include <doc.hxx>
36 #include <editsh.hxx>
37 #include <pam.hxx>
38 #include <ndtxt.hxx>
39 #include <docary.hxx>
40 #include <swwait.hxx>
41 #include <swundo.hxx> // fuer die UndoIds
42 #include <section.hxx>
43 #include <doctxm.hxx>
44 #include <edglbldc.hxx>
47 SV_IMPL_OP_PTRARR_SORT( SwGlblDocContents, SwGlblDocContentPtr )
49 BOOL SwEditShell::IsGlobalDoc() const
51 return getIDocumentSettingAccess()->get(IDocumentSettingAccess::GLOBAL_DOCUMENT);
54 void SwEditShell::SetGlblDocSaveLinks( BOOL bFlag )
56 getIDocumentSettingAccess()->set(IDocumentSettingAccess::GLOBAL_DOCUMENT_SAVE_LINKS, bFlag);
57 if( !GetDoc()->IsModified() ) // Bug 57028
58 GetDoc()->SetUndoNoResetModified();
59 GetDoc()->SetModified();
62 BOOL SwEditShell::IsGlblDocSaveLinks() const
64 return getIDocumentSettingAccess()->get(IDocumentSettingAccess::GLOBAL_DOCUMENT_SAVE_LINKS);
67 USHORT SwEditShell::GetGlobalDocContent( SwGlblDocContents& rArr ) const
69 if( rArr.Count() )
70 rArr.DeleteAndDestroy( 0, rArr.Count() );
72 if( !getIDocumentSettingAccess()->get(IDocumentSettingAccess::GLOBAL_DOCUMENT) )
73 return 0;
75 // dann alle gelinkten Bereiche auf der obersten Ebene
76 SwDoc* pMyDoc = GetDoc();
77 const SwSectionFmts& rSectFmts = pMyDoc->GetSections();
78 USHORT n;
80 for( n = rSectFmts.Count(); n; )
82 const SwSection* pSect = rSectFmts[ --n ]->GetGlobalDocSection();
83 if( pSect )
85 SwGlblDocContentPtr pNew;
86 switch( pSect->GetType() )
88 case TOX_HEADER_SECTION: break; // ignore
89 case TOX_CONTENT_SECTION:
90 ASSERT( pSect->ISA( SwTOXBaseSection ), "keine TOXBaseSection!" );
91 pNew = new SwGlblDocContent( (SwTOXBaseSection*)pSect );
92 break;
94 default:
95 pNew = new SwGlblDocContent( pSect );
96 break;
98 if( !rArr.Insert( pNew ) )
99 delete pNew;
103 // und als letztes die Dummies (sonstiger Text) einfuegen
104 SwNode* pNd;
105 ULONG nSttIdx = pMyDoc->GetNodes().GetEndOfExtras().GetIndex() + 2;
106 for( n = 0; n < rArr.Count(); ++n )
108 const SwGlblDocContent& rNew = *rArr[ n ];
109 // suche von StartPos bis rNew.DocPos nach einem Content Node.
110 // Existiert dieser, so muss ein DummyEintrag eingefuegt werden.
111 for( ; nSttIdx < rNew.GetDocPos(); ++nSttIdx )
112 if( ( pNd = pMyDoc->GetNodes()[ nSttIdx ])->IsCntntNode()
113 || pNd->IsSectionNode() || pNd->IsTableNode() )
115 SwGlblDocContentPtr pNew = new SwGlblDocContent( nSttIdx );
116 if( !rArr.Insert( pNew ) )
117 delete pNew;
118 else
119 ++n; // auf die naechste Position
120 break;
123 // StartPosition aufs Ende setzen
124 nSttIdx = pMyDoc->GetNodes()[ rNew.GetDocPos() ]->EndOfSectionIndex();
125 ++nSttIdx;
128 // sollte man das Ende auch noch setzen??
129 if( rArr.Count() )
131 ULONG nNdEnd = pMyDoc->GetNodes().GetEndOfContent().GetIndex();
132 for( ; nSttIdx < nNdEnd; ++nSttIdx )
133 if( ( pNd = pMyDoc->GetNodes()[ nSttIdx ])->IsCntntNode()
134 || pNd->IsSectionNode() || pNd->IsTableNode() )
136 SwGlblDocContentPtr pNew = new SwGlblDocContent( nSttIdx );
137 if( !rArr.Insert( pNew ) )
138 delete pNew;
139 break;
142 else
144 SwGlblDocContentPtr pNew = new SwGlblDocContent(
145 pMyDoc->GetNodes().GetEndOfExtras().GetIndex() + 2 );
146 rArr.Insert( pNew );
148 return rArr.Count();
151 BOOL SwEditShell::InsertGlobalDocContent( const SwGlblDocContent& rInsPos,
152 const SwSection& rNew )
154 if( !getIDocumentSettingAccess()->get(IDocumentSettingAccess::GLOBAL_DOCUMENT) )
155 return FALSE;
157 SET_CURR_SHELL( this );
158 StartAllAction();
160 SwPaM* pCrsr = GetCrsr();
161 if( pCrsr->GetNext() != pCrsr || IsTableMode() )
162 ClearMark();
164 SwPosition& rPos = *pCrsr->GetPoint();
165 rPos.nNode = rInsPos.GetDocPos();
167 BOOL bEndUndo = FALSE;
168 SwDoc* pMyDoc = GetDoc();
169 SwTxtNode* pTxtNd = pMyDoc->GetNodes()[ rPos.nNode ]->GetTxtNode();
170 if( pTxtNd )
171 rPos.nContent.Assign( pTxtNd, 0 );
172 else
174 bEndUndo = TRUE;
175 pMyDoc->StartUndo( UNDO_START, NULL );
176 rPos.nNode--;
177 pMyDoc->AppendTxtNode( rPos );
178 pCrsr->SetMark();
181 InsertSection( rNew );
183 if( bEndUndo )
184 pMyDoc->EndUndo( UNDO_END, NULL );
185 EndAllAction();
187 return TRUE;
190 BOOL SwEditShell::InsertGlobalDocContent( const SwGlblDocContent& rInsPos,
191 const SwTOXBase& rTOX )
193 if( !getIDocumentSettingAccess()->get(IDocumentSettingAccess::GLOBAL_DOCUMENT) )
194 return FALSE;
196 SET_CURR_SHELL( this );
197 StartAllAction();
199 SwPaM* pCrsr = GetCrsr();
200 if( pCrsr->GetNext() != pCrsr || IsTableMode() )
201 ClearMark();
203 SwPosition& rPos = *pCrsr->GetPoint();
204 rPos.nNode = rInsPos.GetDocPos();
206 BOOL bEndUndo = FALSE;
207 SwDoc* pMyDoc = GetDoc();
208 SwTxtNode* pTxtNd = rPos.nNode.GetNode().GetTxtNode();
209 if( pTxtNd && pTxtNd->GetTxt().Len() && rPos.nNode.GetIndex() + 1 !=
210 pMyDoc->GetNodes().GetEndOfContent().GetIndex() )
211 rPos.nContent.Assign( pTxtNd, 0 );
212 else
214 bEndUndo = TRUE;
215 pMyDoc->StartUndo( UNDO_START, NULL );
216 rPos.nNode--;
217 pMyDoc->AppendTxtNode( rPos );
220 InsertTableOf( rTOX );
222 if( bEndUndo )
223 pMyDoc->EndUndo( UNDO_END, NULL );
224 EndAllAction();
226 return TRUE;
229 BOOL SwEditShell::InsertGlobalDocContent( const SwGlblDocContent& rInsPos )
231 if( !getIDocumentSettingAccess()->get(IDocumentSettingAccess::GLOBAL_DOCUMENT) )
232 return FALSE;
234 SET_CURR_SHELL( this );
235 StartAllAction();
237 SwPaM* pCrsr = GetCrsr();
238 if( pCrsr->GetNext() != pCrsr || IsTableMode() )
239 ClearMark();
241 SwPosition& rPos = *pCrsr->GetPoint();
242 rPos.nNode = rInsPos.GetDocPos() - 1;
243 rPos.nContent.Assign( 0, 0 );
245 SwDoc* pMyDoc = GetDoc();
246 pMyDoc->AppendTxtNode( rPos );
247 EndAllAction();
248 return TRUE;
251 BOOL SwEditShell::DeleteGlobalDocContent( const SwGlblDocContents& rArr ,
252 USHORT nDelPos )
254 if( !getIDocumentSettingAccess()->get(IDocumentSettingAccess::GLOBAL_DOCUMENT) )
255 return FALSE;
257 SET_CURR_SHELL( this );
258 StartAllAction();
259 StartUndo( UNDO_START );
261 SwPaM* pCrsr = GetCrsr();
262 if( pCrsr->GetNext() != pCrsr || IsTableMode() )
263 ClearMark();
265 SwPosition& rPos = *pCrsr->GetPoint();
267 SwDoc* pMyDoc = GetDoc();
268 const SwGlblDocContent& rDelPos = *rArr[ nDelPos ];
269 ULONG nDelIdx = rDelPos.GetDocPos();
270 if( 1 == rArr.Count() )
272 // ein Node muss aber da bleiben!
273 rPos.nNode = nDelIdx - 1;
274 rPos.nContent.Assign( 0, 0 );
276 pMyDoc->AppendTxtNode( rPos );
277 ++nDelIdx;
280 switch( rDelPos.GetType() )
282 case GLBLDOC_UNKNOWN:
284 rPos.nNode = nDelIdx;
285 pCrsr->SetMark();
286 if( ++nDelPos < rArr.Count() )
287 rPos.nNode = rArr[ nDelPos ]->GetDocPos();
288 else
289 rPos.nNode = pMyDoc->GetNodes().GetEndOfContent();
290 rPos.nNode--;
291 if( !pMyDoc->DelFullPara( *pCrsr ) )
292 Delete();
294 break;
296 case GLBLDOC_TOXBASE:
298 SwTOXBaseSection* pTOX = (SwTOXBaseSection*)rDelPos.GetTOX();
299 pMyDoc->DeleteTOX( *pTOX, TRUE );
301 break;
303 case GLBLDOC_SECTION:
305 SwSectionFmt* pSectFmt = (SwSectionFmt*)rDelPos.GetSection()->GetFmt();
306 pMyDoc->DelSectionFmt( pSectFmt, TRUE );
308 break;
311 EndUndo( UNDO_END );
312 EndAllAction();
313 return TRUE;
316 BOOL SwEditShell::MoveGlobalDocContent( const SwGlblDocContents& rArr ,
317 USHORT nFromPos, USHORT nToPos,
318 USHORT nInsPos )
320 if( !getIDocumentSettingAccess()->get(IDocumentSettingAccess::GLOBAL_DOCUMENT) ||
321 nFromPos >= rArr.Count() || nToPos > rArr.Count() ||
322 nInsPos > rArr.Count() || nFromPos >= nToPos ||
323 ( nFromPos <= nInsPos && nInsPos <= nToPos ) )
324 return FALSE;
326 SET_CURR_SHELL( this );
327 StartAllAction();
329 SwPaM* pCrsr = GetCrsr();
330 if( pCrsr->GetNext() != pCrsr || IsTableMode() )
331 ClearMark();
333 SwDoc* pMyDoc = GetDoc();
334 SwNodeRange aRg( pMyDoc->GetNodes(), rArr[ nFromPos ]->GetDocPos() );
335 if( nToPos < rArr.Count() )
336 aRg.aEnd = rArr[ nToPos ]->GetDocPos();
337 else
338 aRg.aEnd = pMyDoc->GetNodes().GetEndOfContent();
340 SwNodeIndex aInsPos( pMyDoc->GetNodes() );
341 if( nInsPos < rArr.Count() )
342 aInsPos = rArr[ nInsPos ]->GetDocPos();
343 else
344 aInsPos = pMyDoc->GetNodes().GetEndOfContent();
346 bool bRet = pMyDoc->MoveNodeRange( aRg, aInsPos,
347 static_cast<IDocumentContentOperations::SwMoveFlags>(
348 IDocumentContentOperations::DOC_MOVEALLFLYS
349 | IDocumentContentOperations::DOC_CREATEUNDOOBJ ));
351 EndAllAction();
352 return bRet;
355 BOOL SwEditShell::GotoGlobalDocContent( const SwGlblDocContent& rPos )
357 if( !getIDocumentSettingAccess()->get(IDocumentSettingAccess::GLOBAL_DOCUMENT) )
358 return FALSE;
360 SET_CURR_SHELL( this );
361 SttCrsrMove();
363 SwPaM* pCrsr = GetCrsr();
364 if( pCrsr->GetNext() != pCrsr || IsTableMode() )
365 ClearMark();
367 SwPosition& rCrsrPos = *pCrsr->GetPoint();
368 rCrsrPos.nNode = rPos.GetDocPos();
370 SwDoc* pMyDoc = GetDoc();
371 SwCntntNode* pCNd = pMyDoc->GetNodes()[ rCrsrPos.nNode ]->GetCntntNode();
372 if( !pCNd )
373 pCNd = pMyDoc->GetNodes().GoNext( &rCrsrPos.nNode );
375 rCrsrPos.nContent.Assign( pCNd, 0 );
377 EndCrsrMove();
378 return TRUE;
381 SwGlblDocContent::SwGlblDocContent( ULONG nPos )
383 eType = GLBLDOC_UNKNOWN;
384 PTR.pTOX = 0;
385 nDocPos = nPos;
388 SwGlblDocContent::SwGlblDocContent( const SwTOXBaseSection* pTOX )
390 eType = GLBLDOC_TOXBASE;
391 PTR.pTOX = pTOX;
393 const SwSectionNode* pSectNd = pTOX->GetFmt()->GetSectionNode();
394 nDocPos = pSectNd ? pSectNd->GetIndex() : 0;
397 SwGlblDocContent::SwGlblDocContent( const SwSection* pSect )
399 eType = GLBLDOC_SECTION;
400 PTR.pSect = pSect;
402 const SwSectionNode* pSectNd = pSect->GetFmt()->GetSectionNode();
403 nDocPos = pSectNd ? pSectNd->GetIndex() : 0;