LibreOffice Module sw (master) 1
edglss.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 <sal/config.h>
21
22#include <o3tl/safeint.hxx>
23#include <osl/diagnose.h>
24#include <osl/endian.h>
25#include <tools/urlobj.hxx>
26#include <doc.hxx>
29#include <pam.hxx>
30#include <editsh.hxx>
31#include <frmfmt.hxx>
32#include <rootfrm.hxx>
33#include <ndtxt.hxx>
34#include <swtable.hxx>
35#include <shellio.hxx>
36#include <iodetect.hxx>
37#include <frameformats.hxx>
38
39void SwEditShell::InsertGlossary( SwTextBlocks& rGlossary, const OUString& rStr )
40{
42 GetDoc()->InsertGlossary( rGlossary, rStr, *GetCursor(), this );
44}
45
47sal_uInt16 SwEditShell::MakeGlossary( SwTextBlocks& rBlks, const OUString& rName, const OUString& rShortName,
48 bool bSaveRelFile, const OUString* pOnlyText )
49{
50 SwDoc* pGDoc = rBlks.GetDoc();
51
52 OUString sBase;
53 if(bSaveRelFile)
54 {
56 sBase = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
57 }
58 rBlks.SetBaseURL( sBase );
59
60 if( pOnlyText )
61 return rBlks.PutText( rShortName, rName, *pOnlyText );
62
63 rBlks.ClearDoc();
64 if( rBlks.BeginPutDoc( rShortName, rName ) )
65 {
67 CopySelToDoc(*pGDoc);
69 return rBlks.PutDoc();
70 }
71
72 return USHRT_MAX;
73}
74
76 const OUString& rName,
77 const OUString& rShortName,
78 bool bSaveRelFile,
79 bool bOnlyText )
80{
82
83 SwDoc* pGDoc = rBlock.GetDoc();
84 SwDoc* pMyDoc = GetDoc();
85
86 OUString sBase;
87 if(bSaveRelFile)
88 {
89 INetURLObject aURL( rBlock.GetFileName() );
90 sBase = aURL.GetMainURL( INetURLObject::DecodeMechanism::NONE );
91 }
92 rBlock.SetBaseURL( sBase );
93 sal_uInt16 nRet = USHRT_MAX;
94
95 if( bOnlyText )
96 {
97 KillPams();
98
99 SwPaM* pCursor = GetCursor();
100
101 SwNodeIndex aStt( pMyDoc->GetNodes().GetEndOfExtras(), 1 );
102 SwContentNode* pContentNd = pMyDoc->GetNodes().GoNext( &aStt );
103 const SwNode* pNd = pContentNd->FindTableNode();
104 if( !pNd )
105 pNd = pContentNd;
106
107 pCursor->GetPoint()->Assign(*pNd);
108 pCursor->SetMark();
109
110 // then until the end of the Node array
111 pCursor->GetPoint()->Assign(pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1);
112 pContentNd = pCursor->GetPointContentNode();
113 if( pContentNd )
114 pCursor->GetPoint()->SetContent( pContentNd->Len() );
115
116 OUString sBuf;
118 if( !sBuf.isEmpty() )
119 nRet = rBlock.PutText( rShortName, rName, sBuf );
120 }
121 else
122 {
123 rBlock.ClearDoc();
124 if( rBlock.BeginPutDoc( rShortName, rName ) )
125 {
126 SwNodeIndex aStt( pMyDoc->GetNodes().GetEndOfExtras(), 1 );
127 SwContentNode* pContentNd = pMyDoc->GetNodes().GoNext( &aStt );
128 const SwNode* pNd = pContentNd->FindTableNode();
129 if( !pNd ) pNd = pContentNd;
130 SwPaM aCpyPam( *pNd );
131 aCpyPam.SetMark();
132
133 // then until the end of the nodes array
134 aCpyPam.GetPoint()->Assign(pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1);
135 pContentNd = aCpyPam.GetPointContentNode();
136 if(pContentNd)
137 aCpyPam.GetPoint()->SetContent( pContentNd->Len() );
138
139 aStt = pGDoc->GetNodes().GetEndOfExtras();
140 pContentNd = pGDoc->GetNodes().GoNext( &aStt );
141 SwPosition aInsPos( aStt );
143
144 nRet = rBlock.PutDoc();
145 }
146 }
147 EndAllAction();
148 return nRet;
149}
150
153{
154 SwNodes& rNds = rInsDoc.GetNodes();
155
156 SwNodeIndex aIdx( rNds.GetEndOfContent(), -1 );
157 SwContentNode *const pContentNode = aIdx.GetNode().GetContentNode();
158 SwPosition aPos( aIdx, pContentNode, pContentNode ? pContentNode->Len() : 0);
159
160 bool bRet = false;
161 CurrShell aCurr( this );
162
164
165 if( IsTableMode() )
166 {
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.
169
170 // search boxes using the layout
171 SwTableNode* pTableNd;
172 SwSelBoxes aBoxes;
173 GetTableSel( *this, aBoxes );
174 if( !aBoxes.empty() && nullptr != (pTableNd = const_cast<SwTableNode*>(aBoxes[0]
175 ->GetSttNd()->FindTableNode()) ))
176 {
177 // check if the table name can be copied
178 bool bCpyTableNm = aBoxes.size() == pTableNd->GetTable().GetTabSortBoxes().size();
179 if( bCpyTableNm )
180 {
181 const OUString rTableName = pTableNd->GetTable().GetFrameFormat()->GetName();
182 const SwFrameFormats& rTableFormats = *rInsDoc.GetTableFrameFormats();
183 for( auto n = rTableFormats.size(); n; )
184 if( rTableFormats[ --n ]->GetName() == rTableName )
185 {
186 bCpyTableNm = false;
187 break;
188 }
189 }
190 bRet = rInsDoc.InsCopyOfTable( aPos, aBoxes, nullptr, bCpyTableNm, false, pTableNd->GetTable().GetTableStyleName() );
191 }
192 else
193 bRet = false;
194 }
195 else
196 {
197 bool bColSel = GetCursor_()->IsColumnSelection();
198 if( bColSel && rInsDoc.IsClipBoard() )
199 rInsDoc.SetColumnSelection( true );
201 {
202 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
203 {
204 if( !rPaM.HasMark() )
205 {
206 SwContentNode *const pNd = rPaM.GetPointContentNode();
207 if (nullptr != pNd &&
208 ( bColSel || !pNd->GetTextNode() ) )
209 {
210 rPaM.SetMark();
211 rPaM.Move( fnMoveForward, GoInContent );
213 || bRet;
214 rPaM.Exchange();
215 rPaM.DeleteMark();
216 }
217 }
218 else
219 {
220 // Make a copy, so that in case we need to adjust the selection
221 // for the purpose of copying, our shell cursor is not touched.
222 // (Otherwise we would have to restore it.)
223 SwPaM aPaM(*rPaM.GetMark(), *rPaM.GetPoint());
224 if (bSelectAll)
225 {
226 // Selection starts at the first para of the first cell,
227 // but we want to copy the table and the start node before
228 // the first cell as well.
229 // tdf#133982 tables can be nested
230 const SwNode* pNode = &aPaM.Start()->GetNode();
231 while (SwTableNode const* pTableNode =
233 {
234 pNode = pTableNode;
235 }
236 while (SwSectionNode const* pSectionNode =
238 {
239 pNode = pSectionNode;
240 }
241 aPaM.Start()->Assign(*pNode);
242 }
244 || bRet;
245 }
246 }
247 }
248 }
249
252 rInsDoc.getIDocumentFieldsAccess().UpdateExpFields(nullptr, true);
253
254 return bRet;
255}
256
259void SwEditShell::GetSelectedText( OUString &rBuf, ParaBreakType nHndlParaBrk )
260{
261 GetCursor(); // creates all cursors if needed
262 if( IsSelOnePara() )
263 {
264 rBuf = GetSelText();
265 if( ParaBreakType::ToBlank == nHndlParaBrk )
266 {
267 rBuf = rBuf.replaceAll("\x0a", " ");
268 }
269 else if( IsSelFullPara() &&
270 ParaBreakType::ToOnlyCR != nHndlParaBrk )
271 {
272 rBuf += SAL_NEWLINE_STRING;
273 }
274 }
275 else if( IsSelection() )
276 {
277 SvMemoryStream aStream;
278#ifdef OSL_BIGENDIAN
279 aStream.SetEndian( SvStreamEndian::BIG );
280#else
281 aStream.SetEndian( SvStreamEndian::LITTLE );
282#endif
283 WriterRef xWrt;
284 SwReaderWriter::GetWriter( FILTER_TEXT, OUString(), xWrt );
285 if( xWrt.is() )
286 {
287 // write selected areas into an ASCII document
288 SwWriter aWriter( aStream, *this);
289 xWrt->SetShowProgress(false);
290
291 switch( nHndlParaBrk )
292 {
294 xWrt->m_bASCII_ParaAsBlank = true;
295 xWrt->m_bASCII_NoLastLineEnd = true;
296 break;
297
299 xWrt->m_bASCII_ParaAsCR = true;
300 xWrt->m_bASCII_NoLastLineEnd = true;
301 break;
302 }
303
304 //JP 09.05.00: write as UNICODE ! (and not as ANSI)
305 SwAsciiOptions aAsciiOpt( xWrt->GetAsciiOptions() );
306 aAsciiOpt.SetCharSet( RTL_TEXTENCODING_UCS2 );
307 xWrt->SetAsciiOptions( aAsciiOpt );
308 xWrt->m_bUCS2_WithStartChar = false;
309 xWrt->m_bHideDeleteRedlines = GetLayout()->IsHideRedlines();
310
311 if ( ! aWriter.Write(xWrt).IsError() )
312 {
313 aStream.WriteUInt16( '\0' );
314
315 const sal_Unicode *p = static_cast<sal_Unicode const *>(aStream.GetData());
316 if (p)
317 rBuf = OUString(p);
318 else
319 {
320 const sal_uInt64 nLen = aStream.GetSize();
321 OSL_ENSURE( nLen/sizeof( sal_Unicode )<o3tl::make_unsigned(SAL_MAX_INT32), "Stream can't fit in OUString" );
322 rtl_uString *pStr = rtl_uString_alloc(static_cast<sal_Int32>(nLen / sizeof( sal_Unicode )));
323 aStream.Seek( 0 );
324 aStream.ResetError();
325 //endian specific?, yipes!
326 aStream.ReadBytes(pStr->buffer, nLen);
327 rBuf = OUString(pStr, SAL_NO_ACQUIRE);
328 }
329 }
330 }
331 }
332}
333
334/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
@ CheckPosInFly
check if target position is in fly anchored at source range
@ NONE
no RedlineFlags
bool IsError() const
virtual bool CopyRange(SwPaM &rPam, SwPosition &rPos, SwCopyFlags flags) const =0
Copy a selected content range to a position.
virtual void LockExpFields()=0
virtual bool IsExpFieldsLocked() const =0
virtual void UpdateExpFields(SwTextField *pField, bool bUpdateRefFields)=0
virtual void UnlockExpFields()=0
virtual void SetRedlineFlags_intern(RedlineFlags eMode)=0
Set a new redline mode.
const void * GetData()
sal_uInt64 GetSize()
virtual void ResetError() override
void SetEndian(SvStreamEndian SvStreamEndian)
SvStream & WriteUInt16(sal_uInt16 nUInt16)
sal_uInt64 Seek(sal_uInt64 nPos)
std::size_t ReadBytes(void *pData, std::size_t nSize)
void SetCharSet(rtl_TextEncoding nVal)
Definition: shellio.hxx:77
virtual sal_Int32 Len() const
Definition: node.cxx:1258
bool IsSelOnePara() const
Definition: crsrsh.cxx:1090
SwShellCursor * GetCursor_()
Definition: crsrsh.hxx:341
StartsWith StartsWith_()
If document body starts with a table or starts/ends with hidden paragraph.
Definition: crsrsh.cxx:626
SwCursor * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:194
OUString GetSelText() const
get selected text of a node at current cursor
Definition: crsrsh.cxx:2588
bool IsSelection() const
Definition: crsrsh.hxx:904
bool IsSelFullPara() const
Definition: crsrsh.cxx:3424
bool ExtendedSelectedAll()
If ExtendedSelectAll() was called and selection didn't change since then.
Definition: crsrsh.cxx:607
void KillPams()
Definition: crsrsh.cxx:1036
bool IsTableMode() const
Definition: crsrsh.hxx:662
bool IsColumnSelection() const
Definition: swcrsr.hxx:214
Definition: doc.hxx:195
bool InsertGlossary(SwTextBlocks &rBlock, const OUString &rEntry, SwPaM &rPaM, SwCursorShell *pShell=nullptr)
inserts an AutoText block
Definition: docglos.cxx:131
bool IsClipBoard() const
Definition: doc.hxx:974
bool InsCopyOfTable(SwPosition &rInsPos, const SwSelBoxes &rBoxes, const SwTable *pCpyTable, bool bCpyName=false, bool bCorrPos=false, const OUString &rStyleName="")
Copies a Table from the same or another Doc into itself We create a new Table or an existing one is f...
Definition: ndtbl.cxx:4255
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:323
SwNodes & GetNodes()
Definition: doc.hxx:420
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:365
IDocumentRedlineAccess const & getIDocumentRedlineAccess() const
Definition: doc.cxx:343
const SwFrameFormats * GetTableFrameFormats() const
Definition: doc.hxx:822
void SetColumnSelection(bool bNew)
Definition: doc.hxx:979
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
bool CopySelToDoc(SwDoc &rInsDoc)
For copying via ClipBoard: If table is copied into table, move all cursors away from it.
Definition: edglss.cxx:152
sal_uInt16 MakeGlossary(SwTextBlocks &rToFill, const OUString &rName, const OUString &rShortName, bool bSaveRelFile, const OUString *pOnlyText)
Make current selection glossary and insert into glossary document including styles.
Definition: edglss.cxx:47
sal_uInt16 SaveGlossaryDoc(SwTextBlocks &rGlossary, const OUString &rName, const OUString &rShortName, bool bSaveRelFile, bool bOnlyText)
Save complete content of doc as glossary.
Definition: edglss.cxx:75
void GetSelectedText(OUString &rBuf, ParaBreakType nHndlParaBreak=ParaBreakType::ToBlank)
Query text within selection.
Definition: edglss.cxx:259
void EndAllAction()
Definition: edws.cxx:97
void InsertGlossary(SwTextBlocks &rGlossary, const OUString &)
Glossary from glossary document in current document.
Definition: edglss.cxx:39
const OUString & GetName() const
Definition: format.hxx:131
Specific frame formats (frames, DrawObjects).
size_t size() const
Marks a node in the document model.
Definition: ndindex.hxx:31
SwNode & GetNode() const
Definition: ndindex.hxx:123
Base class of the Writer document model elements.
Definition: node.hxx:98
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:903
SwNodeOffset GetIndex() const
Definition: node.hxx:312
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:380
SwSectionNode * FindSectionNode()
Search section node, in which it is.
Definition: ndsect.cxx:974
const SwStartNode * StartOfSectionNode() const
Definition: node.hxx:153
SwContentNode * GetContentNode()
Definition: node.hxx:666
SwNode & GetEndOfExtras() const
This is the last EndNode of a special section.
Definition: ndarr.hxx:163
SwNode & GetEndOfContent() const
Regular ContentSection (i.e. the BodyText).
Definition: ndarr.hxx:165
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1299
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:187
virtual void SetMark()
Unless this is called, the getter method of Mark will return Point.
Definition: pam.cxx:642
SwContentNode * GetPointContentNode() const
Definition: pam.hxx:287
const SwPosition * GetPoint() const
Definition: pam.hxx:261
const SwPosition * Start() const
Definition: pam.hxx:266
bool IsHideRedlines() const
Replacement for sw::DocumentRedlineManager::GetRedlineFlags() (this is layout-level redline hiding).
Definition: rootfrm.hxx:425
A section node represents the start of a section on the UI, i.e.
Definition: node.hxx:575
const SwTable & GetTable() const
Definition: node.hxx:542
const OUString & GetTableStyleName() const
Return the table style name of this table.
Definition: swtable.hxx:196
SwTableFormat * GetFrameFormat()
Definition: swtable.hxx:209
SwTableSortBoxes & GetTabSortBoxes()
Definition: swtable.hxx:267
bool BeginPutDoc(const OUString &, const OUString &)
Definition: swblocks.cxx:385
SwDoc * GetDoc()
Definition: swblocks.cxx:481
OUString const & GetFileName() const
Definition: swblocks.cxx:497
sal_uInt16 PutText(const OUString &, const OUString &, const OUString &)
Definition: swblocks.cxx:438
void ClearDoc()
Definition: swblocks.cxx:488
sal_uInt16 PutDoc()
Definition: swblocks.cxx:409
void SetBaseURL(const OUString &rURL)
Definition: swblocks.cxx:576
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2159
SwDoc * GetDoc() const
Definition: viewsh.hxx:290
ErrCode Write(WriterRef const &rxWriter, const OUString *=nullptr)
Definition: shellio.cxx:739
bool empty() const
size_type size() const
ring_container GetRingContainer()
Definition: ring.hxx:240
bool is() const
#define SAL_NEWLINE_STRING
URL aURL
virtual OUString GetName() const override
ParaBreakType
Definition: editsh.hxx:112
constexpr OUStringLiteral FILTER_TEXT
text filter with default codeset
Definition: iodetect.hxx:34
void * p
sal_Int64 n
void GetWriter(std::u16string_view rFltName, const OUString &rBaseURL, WriterRef &xRet)
Return writer based on the name.
Definition: fltini.cxx:159
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
bool GoInContent(SwPaM &rPam, SwMoveFnCollection const &fnMove)
Definition: pam.cxx:1180
SwMoveFnCollection const & fnMoveForward
SwPam::Move()/Find() default argument.
Definition: paminit.cxx:61
Marks a position in the document model.
Definition: pam.hxx:37
SwNode & GetNode() const
Definition: pam.hxx:80
void Assign(const SwNode &rNd, SwNodeOffset nDelta, sal_Int32 nContentOffset=0)
These all set both nNode and nContent.
Definition: pam.cxx:230
void SetContent(sal_Int32 nContentIndex)
Set content index, only valid to call this if the position points to a SwContentNode subclass.
Definition: pam.cxx:266
void GetTableSel(const SwCursorShell &rShell, SwSelBoxes &rBoxes, const SwTableSearchType eSearchType)
Definition: tblsel.cxx:149
#define SAL_MAX_INT32
sal_uInt16 sal_Unicode