1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
22 #include <o3tl/safeint.hxx>
23 #include <osl/diagnose.h>
24 #include <osl/endian.h>
25 #include <tools/urlobj.hxx>
27 #include <IDocumentRedlineAccess.hxx>
28 #include <IDocumentFieldsAccess.hxx>
32 #include <rootfrm.hxx>
34 #include <swtable.hxx>
35 #include <shellio.hxx>
36 #include <iodetect.hxx>
37 #include <frameformats.hxx>
39 void SwEditShell::InsertGlossary( SwTextBlocks
& rGlossary
, const OUString
& rStr
)
42 GetDoc()->InsertGlossary( rGlossary
, rStr
, *GetCursor(), this );
46 /// convert current selection into text block and add to the text block document, incl. templates
47 sal_uInt16
SwEditShell::MakeGlossary( SwTextBlocks
& rBlks
, const OUString
& rName
, const OUString
& rShortName
,
48 bool bSaveRelFile
, const OUString
* pOnlyText
)
50 SwDoc
* pGDoc
= rBlks
.GetDoc();
55 INetURLObject
aURL( rBlks
.GetFileName() );
56 sBase
= aURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
);
58 rBlks
.SetBaseURL( sBase
);
61 return rBlks
.PutText( rShortName
, rName
, *pOnlyText
);
64 if( rBlks
.BeginPutDoc( rShortName
, rName
) )
66 rBlks
.GetDoc()->getIDocumentRedlineAccess().SetRedlineFlags_intern( RedlineFlags::DeleteRedlines
);
68 rBlks
.GetDoc()->getIDocumentRedlineAccess().SetRedlineFlags_intern( RedlineFlags::NONE
);
69 return rBlks
.PutDoc();
75 sal_uInt16
SwEditShell::SaveGlossaryDoc( SwTextBlocks
& rBlock
,
76 const OUString
& rName
,
77 const OUString
& rShortName
,
83 SwDoc
* pGDoc
= rBlock
.GetDoc();
84 SwDoc
* pMyDoc
= GetDoc();
89 INetURLObject
aURL( rBlock
.GetFileName() );
90 sBase
= aURL
.GetMainURL( INetURLObject::DecodeMechanism::NONE
);
92 rBlock
.SetBaseURL( sBase
);
93 sal_uInt16 nRet
= USHRT_MAX
;
99 SwPaM
* pCursor
= GetCursor();
101 SwNodeIndex
aStt( pMyDoc
->GetNodes().GetEndOfExtras(), 1 );
102 SwContentNode
* pContentNd
= SwNodes::GoNext(&aStt
);
103 const SwNode
* pNd
= pContentNd
->FindTableNode();
107 pCursor
->GetPoint()->Assign(*pNd
);
110 // then until the end of the Node array
111 pCursor
->GetPoint()->Assign(pMyDoc
->GetNodes().GetEndOfContent().GetIndex()-1);
112 pContentNd
= pCursor
->GetPointContentNode();
114 pCursor
->GetPoint()->SetContent( pContentNd
->Len() );
117 GetSelectedText( sBuf
, ParaBreakType::ToOnlyCR
);
118 if( !sBuf
.isEmpty() )
119 nRet
= rBlock
.PutText( rShortName
, rName
, sBuf
);
124 if( rBlock
.BeginPutDoc( rShortName
, rName
) )
126 SwNodeIndex
aStt( pMyDoc
->GetNodes().GetEndOfExtras(), 1 );
127 SwContentNode
* pContentNd
= SwNodes::GoNext(&aStt
);
128 const SwNode
* pNd
= pContentNd
->FindTableNode();
129 if( !pNd
) pNd
= pContentNd
;
130 SwPaM
aCpyPam( *pNd
);
133 // then until the end of the nodes array
134 aCpyPam
.GetPoint()->Assign(pMyDoc
->GetNodes().GetEndOfContent().GetIndex()-1);
135 pContentNd
= aCpyPam
.GetPointContentNode();
137 aCpyPam
.GetPoint()->SetContent( pContentNd
->Len() );
139 aStt
= pGDoc
->GetNodes().GetEndOfExtras();
140 pContentNd
= SwNodes::GoNext(&aStt
);
141 SwPosition
aInsPos( aStt
);
142 pMyDoc
->getIDocumentContentOperations().CopyRange(aCpyPam
, aInsPos
, SwCopyFlags::CheckPosInFly
);
144 nRet
= rBlock
.PutDoc();
151 /// copy all selections to the doc
152 bool SwEditShell::CopySelToDoc( SwDoc
& rInsDoc
)
154 SwNodes
& rNds
= rInsDoc
.GetNodes();
156 SwNodeIndex
aIdx( rNds
.GetEndOfContent(), -1 );
157 SwContentNode
*const pContentNode
= aIdx
.GetNode().GetContentNode();
158 SwPosition
aPos( aIdx
, pContentNode
, pContentNode
? pContentNode
->Len() : 0);
161 CurrShell
aCurr( this );
163 rInsDoc
.getIDocumentFieldsAccess().LockExpFields();
167 // Copy parts of a table: create a table with the width of the original one and copy the
168 // selected boxes. The sizes are corrected on a percentage basis.
170 // search boxes using the layout
171 SwTableNode
* pTableNd
;
173 GetTableSel( *this, aBoxes
);
174 if( !aBoxes
.empty() && nullptr != (pTableNd
= const_cast<SwTableNode
*>(aBoxes
[0]
175 ->GetSttNd()->FindTableNode()) ))
177 // check if the table name can be copied
178 bool bCpyTableNm
= aBoxes
.size() == pTableNd
->GetTable().GetTabSortBoxes().size();
181 const OUString rTableName
= pTableNd
->GetTable().GetFrameFormat()->GetName();
182 const sw::TableFrameFormats
& rTableFormats
= *rInsDoc
.GetTableFrameFormats();
183 for( auto n
= rTableFormats
.size(); n
; )
184 if( rTableFormats
[ --n
]->GetName() == rTableName
)
190 bRet
= rInsDoc
.InsCopyOfTable( aPos
, aBoxes
, nullptr, bCpyTableNm
, false, pTableNd
->GetTable().GetTableStyleName() );
197 bool bColSel
= GetCursor_()->IsColumnSelection();
198 if( bColSel
&& rInsDoc
.IsClipBoard() )
199 rInsDoc
.SetColumnSelection( true );
200 const ExtendedSelection
oSelectAll(StartsWith_() != SwCursorShell::StartsWith::None
201 ? ExtendedSelectedAll()
202 : ExtendedSelection
{});
204 for(SwPaM
& rPaM
: GetCursor()->GetRingContainer())
206 if( !rPaM
.HasMark() )
208 SwContentNode
*const pNd
= rPaM
.GetPointContentNode();
209 if (nullptr != pNd
&&
210 ( bColSel
|| !pNd
->GetTextNode() ) )
213 rPaM
.Move( fnMoveForward
, GoInContent
);
214 bRet
= GetDoc()->getIDocumentContentOperations().CopyRange(rPaM
, aPos
, SwCopyFlags::CheckPosInFly
)
222 // Make a copy, so that in case we need to adjust the selection
223 // for the purpose of copying, our shell cursor is not touched.
224 // (Otherwise we would have to restore it.)
225 SwPaM
aPaM(*rPaM
.GetMark(), *rPaM
.GetPoint());
228 // Selection starts at the first para of the first cell,
229 // but we want to copy the table and the start node before
230 // the first cell as well.
231 aPaM
.Start()->Assign(*oSelectAll
->first
);
232 if (SwSectionNode
const* pSection
= oSelectAll
->first
->GetSectionNode())
234 if (aPaM
.End()->GetNodeIndex() < pSection
->EndOfSectionIndex())
236 // include section end so that section is copied
237 aPaM
.End()->Assign(*oSelectAll
->first
->GetNodes()[pSection
->EndOfSectionIndex() + 1]);
241 bRet
= GetDoc()->getIDocumentContentOperations().CopyRange( aPaM
, aPos
, SwCopyFlags::CheckPosInFly
)
248 rInsDoc
.getIDocumentFieldsAccess().UnlockExpFields();
249 if( !rInsDoc
.getIDocumentFieldsAccess().IsExpFieldsLocked() )
250 rInsDoc
.getIDocumentFieldsAccess().UpdateExpFields(nullptr, true);
255 /** Get text in a Selection
257 void SwEditShell::GetSelectedText( OUString
&rBuf
, ParaBreakType nHndlParaBrk
)
259 GetCursor(); // creates all cursors if needed
263 if( ParaBreakType::ToBlank
== nHndlParaBrk
)
265 rBuf
= rBuf
.replaceAll("\x0a", " ");
267 else if( IsSelFullPara() &&
268 ParaBreakType::ToOnlyCR
!= nHndlParaBrk
)
270 rBuf
+= SAL_NEWLINE_STRING
;
273 else if( IsSelection() )
275 SvMemoryStream aStream
;
277 aStream
.SetEndian( SvStreamEndian::BIG
);
279 aStream
.SetEndian( SvStreamEndian::LITTLE
);
282 SwReaderWriter::GetWriter( FILTER_TEXT
, OUString(), xWrt
);
285 // write selected areas into an ASCII document
286 SwWriter
aWriter( aStream
, *this);
287 xWrt
->SetShowProgress(false);
289 switch( nHndlParaBrk
)
291 case ParaBreakType::ToBlank
:
292 xWrt
->m_bASCII_ParaAsBlank
= true;
293 xWrt
->m_bASCII_NoLastLineEnd
= true;
296 case ParaBreakType::ToOnlyCR
:
297 xWrt
->m_bASCII_ParaAsCR
= true;
298 xWrt
->m_bASCII_NoLastLineEnd
= true;
302 //JP 09.05.00: write as UNICODE ! (and not as ANSI)
303 SwAsciiOptions
aAsciiOpt( xWrt
->GetAsciiOptions() );
304 aAsciiOpt
.SetCharSet( RTL_TEXTENCODING_UCS2
);
305 xWrt
->SetAsciiOptions( aAsciiOpt
);
306 xWrt
->m_bUCS2_WithStartChar
= false;
307 xWrt
->m_bHideDeleteRedlines
= GetLayout()->IsHideRedlines();
309 if ( ! aWriter
.Write(xWrt
).IsError() )
311 aStream
.WriteUInt16( '\0' );
313 const sal_Unicode
*p
= static_cast<sal_Unicode
const *>(aStream
.GetData());
318 const sal_uInt64 nLen
= aStream
.GetSize();
319 OSL_ENSURE( nLen
/sizeof( sal_Unicode
)<o3tl::make_unsigned(SAL_MAX_INT32
), "Stream can't fit in OUString" );
320 rtl_uString
*pStr
= rtl_uString_alloc(static_cast<sal_Int32
>(nLen
/ sizeof( sal_Unicode
)));
322 aStream
.ResetError();
323 //endian specific?, yipes!
324 aStream
.ReadBytes(pStr
->buffer
, nLen
);
325 rBuf
= OUString(pStr
, SAL_NO_ACQUIRE
);
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */