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/endian.h>
24 #include <tools/urlobj.hxx>
25 #include <doc.hxx>
28 #include <pam.hxx>
29 #include <docary.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 
39 void SwEditShell::InsertGlossary( SwTextBlocks& rGlossary, const OUString& rStr )
40 {
42  GetDoc()->InsertGlossary( rGlossary, rStr, *GetCursor(), this );
43  EndAllAction();
44 }
45 
47 sal_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  {
55  INetURLObject aURL( rBlks.GetFileName() );
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()->nNode = *pNd;
108  if( pNd == pContentNd )
109  pCursor->GetPoint()->nContent.Assign( pContentNd, 0 );
110  pCursor->SetMark();
111 
112  // then until the end of the Node array
113  pCursor->GetPoint()->nNode = pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1;
114  pContentNd = pCursor->GetContentNode();
115  if( pContentNd )
116  pCursor->GetPoint()->nContent.Assign( pContentNd, pContentNd->Len() );
117 
118  OUString sBuf;
120  if( !sBuf.isEmpty() )
121  nRet = rBlock.PutText( rShortName, rName, sBuf );
122  }
123  else
124  {
125  rBlock.ClearDoc();
126  if( rBlock.BeginPutDoc( rShortName, rName ) )
127  {
128  SwNodeIndex aStt( pMyDoc->GetNodes().GetEndOfExtras(), 1 );
129  SwContentNode* pContentNd = pMyDoc->GetNodes().GoNext( &aStt );
130  const SwNode* pNd = pContentNd->FindTableNode();
131  if( !pNd ) pNd = pContentNd;
132  SwPaM aCpyPam( *pNd );
133  aCpyPam.SetMark();
134 
135  // then until the end of the nodes array
136  aCpyPam.GetPoint()->nNode = pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1;
137  pContentNd = aCpyPam.GetContentNode();
138  aCpyPam.GetPoint()->nContent.Assign(
139  pContentNd, pContentNd ? pContentNd->Len() : 0);
140 
141  aStt = pGDoc->GetNodes().GetEndOfExtras();
142  pContentNd = pGDoc->GetNodes().GoNext( &aStt );
143  SwPosition aInsPos( aStt, SwIndex( pContentNd ));
145 
146  nRet = rBlock.PutDoc();
147  }
148  }
149  EndAllAction();
150  return nRet;
151 }
152 
155 {
156  OSL_ENSURE( pInsDoc, "no Ins.Document" );
157 
158  SwNodes& rNds = pInsDoc->GetNodes();
159 
160  SwNodeIndex aIdx( rNds.GetEndOfContent(), -1 );
161  SwContentNode *const pContentNode = aIdx.GetNode().GetContentNode();
162  SwPosition aPos( aIdx,
163  SwIndex(pContentNode, pContentNode ? pContentNode->Len() : 0));
164 
165  bool bRet = false;
166  CurrShell aCurr( this );
167 
169 
170  if( IsTableMode() )
171  {
172  // Copy parts of a table: create a table with the width of the original one and copy the
173  // selected boxes. The sizes are corrected on a percentage basis.
174 
175  // search boxes using the layout
176  SwTableNode* pTableNd;
177  SwSelBoxes aBoxes;
178  GetTableSel( *this, aBoxes );
179  if( !aBoxes.empty() && nullptr != (pTableNd = const_cast<SwTableNode*>(aBoxes[0]
180  ->GetSttNd()->FindTableNode()) ))
181  {
182  // check if the table name can be copied
183  bool bCpyTableNm = aBoxes.size() == pTableNd->GetTable().GetTabSortBoxes().size();
184  if( bCpyTableNm )
185  {
186  const OUString rTableName = pTableNd->GetTable().GetFrameFormat()->GetName();
187  const SwFrameFormats& rTableFormats = *pInsDoc->GetTableFrameFormats();
188  for( auto n = rTableFormats.size(); n; )
189  if( rTableFormats[ --n ]->GetName() == rTableName )
190  {
191  bCpyTableNm = false;
192  break;
193  }
194  }
195  bRet = pInsDoc->InsCopyOfTable( aPos, aBoxes, nullptr, bCpyTableNm );
196  }
197  else
198  bRet = false;
199  }
200  else
201  {
202  bool bColSel = GetCursor_()->IsColumnSelection();
203  if( bColSel && pInsDoc->IsClipBoard() )
204  pInsDoc->SetColumnSelection( true );
205  bool bSelectAll = StartsWithTable() && ExtendedSelectedAll();
206  {
207  for(SwPaM& rPaM : GetCursor()->GetRingContainer())
208  {
209  if( !rPaM.HasMark() )
210  {
211  SwContentNode *const pNd = rPaM.GetContentNode();
212  if (nullptr != pNd &&
213  ( bColSel || !pNd->GetTextNode() ) )
214  {
215  rPaM.SetMark();
216  rPaM.Move( fnMoveForward, GoInContent );
218  || bRet;
219  rPaM.Exchange();
220  rPaM.DeleteMark();
221  }
222  }
223  else
224  {
225  // Make a copy, so that in case we need to adjust the selection
226  // for the purpose of copying, our shell cursor is not touched.
227  // (Otherwise we would have to restore it.)
228  SwPaM aPaM(*rPaM.GetMark(), *rPaM.GetPoint());
229  if (bSelectAll)
230  {
231  // Selection starts at the first para of the first cell,
232  // but we want to copy the table and the start node before
233  // the first cell as well.
234  // tdf#133982 tables can be nested
235  while (SwTableNode const* pTableNode =
236  aPaM.Start()->nNode.GetNode().StartOfSectionNode()->FindTableNode())
237  {
238  aPaM.Start()->nNode = *pTableNode;
239  }
240  while (SwSectionNode const* pSectionNode =
241  aPaM.Start()->nNode.GetNode().StartOfSectionNode()->FindSectionNode())
242  {
243  aPaM.Start()->nNode = *pSectionNode;
244  }
245  aPaM.Start()->nContent.Assign(nullptr, 0);
246  }
248  || bRet;
249  }
250  }
251  }
252  }
253 
255  if( !pInsDoc->getIDocumentFieldsAccess().IsExpFieldsLocked() )
256  pInsDoc->getIDocumentFieldsAccess().UpdateExpFields(nullptr, true);
257 
258  return bRet;
259 }
260 
263 void SwEditShell::GetSelectedText( OUString &rBuf, ParaBreakType nHndlParaBrk )
264 {
265  GetCursor(); // creates all cursors if needed
266  if( IsSelOnePara() )
267  {
268  rBuf = GetSelText();
269  if( ParaBreakType::ToBlank == nHndlParaBrk )
270  {
271  rBuf = rBuf.replaceAll("\x0a", " ");
272  }
273  else if( IsSelFullPara() &&
274  ParaBreakType::ToOnlyCR != nHndlParaBrk )
275  {
276 #ifdef _WIN32
277  rBuf += "\015\012";
278 #else
279  rBuf += "\012";
280 #endif
281  }
282  }
283  else if( IsSelection() )
284  {
285  SvMemoryStream aStream;
286 #ifdef OSL_BIGENDIAN
287  aStream.SetEndian( SvStreamEndian::BIG );
288 #else
289  aStream.SetEndian( SvStreamEndian::LITTLE );
290 #endif
291  WriterRef xWrt;
292  SwReaderWriter::GetWriter( FILTER_TEXT, OUString(), xWrt );
293  if( xWrt.is() )
294  {
295  // write selected areas into an ASCII document
296  SwWriter aWriter( aStream, *this);
297  xWrt->SetShowProgress(false);
298 
299  switch( nHndlParaBrk )
300  {
302  xWrt->m_bASCII_ParaAsBlank = true;
303  xWrt->m_bASCII_NoLastLineEnd = true;
304  break;
305 
307  xWrt->m_bASCII_ParaAsCR = true;
308  xWrt->m_bASCII_NoLastLineEnd = true;
309  break;
310  }
311 
312  //JP 09.05.00: write as UNICODE ! (and not as ANSI)
313  SwAsciiOptions aAsciiOpt( xWrt->GetAsciiOptions() );
314  aAsciiOpt.SetCharSet( RTL_TEXTENCODING_UCS2 );
315  xWrt->SetAsciiOptions( aAsciiOpt );
316  xWrt->m_bUCS2_WithStartChar = false;
317  xWrt->m_bHideDeleteRedlines = GetLayout()->IsHideRedlines();
318 
319  if ( ! aWriter.Write(xWrt).IsError() )
320  {
321  aStream.WriteUInt16( '\0' );
322 
323  const sal_Unicode *p = static_cast<sal_Unicode const *>(aStream.GetData());
324  if (p)
325  rBuf = OUString(p);
326  else
327  {
328  const sal_uInt64 nLen = aStream.GetSize();
329  OSL_ENSURE( nLen/sizeof( sal_Unicode )<o3tl::make_unsigned(SAL_MAX_INT32), "Stream can't fit in OUString" );
330  rtl_uString *pStr = rtl_uString_alloc(static_cast<sal_Int32>(nLen / sizeof( sal_Unicode )));
331  aStream.Seek( 0 );
332  aStream.ResetError();
333  //endian specific?, yipes!
334  aStream.ReadBytes(pStr->buffer, nLen);
335  rBuf = OUString(pStr, SAL_NO_ACQUIRE);
336  }
337  }
338  }
339  }
340 }
341 
342 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwSectionNode * FindSectionNode()
Search section node, in which it is.
Definition: ndsect.cxx:962
bool is() const
void InsertGlossary(SwTextBlocks &rGlossary, const OUString &)
Glossary from glossary document in current document.
Definition: edglss.cxx:39
bool IsColumnSelection() const
Definition: swcrsr.hxx:214
void KillPams()
Definition: crsrsh.cxx:1020
URL aURL
sal_uLong GetIndex() const
Definition: node.hxx:282
bool GoInContent(SwPaM &rPam, SwMoveFnCollection const &fnMove)
Definition: pam.cxx:963
bool CopySelToDoc(SwDoc *pInsDoc)
For copying via ClipBoard: If table is copied into table, move all cursors away from it...
Definition: edglss.cxx:154
Marks a position in the document model.
Definition: pam.hxx:35
SwPaM * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:189
SvStream & WriteUInt16(sal_uInt16 nUInt16)
virtual void ResetError() override
sal_uInt16 PutDoc()
Definition: swblocks.cxx:398
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
const SwFrameFormats * GetTableFrameFormats() const
Definition: doc.hxx:808
virtual void SetRedlineFlags_intern(RedlineFlags eMode)=0
Set a new redline mode.
SwNodeIndex nNode
Definition: pam.hxx:37
bool IsTableMode() const
Definition: crsrsh.hxx:643
void GetSelectedText(OUString &rBuf, ParaBreakType nHndlParaBreak=ParaBreakType::ToBlank)
Query text within selection.
Definition: edglss.cxx:263
sal_Int64 n
Definition: doc.hxx:184
sal_uInt64 Seek(sal_uInt64 nPos)
bool IsSelFullPara() const
Definition: crsrsh.cxx:3360
ErrCode Write(WriterRef const &rxWriter, const OUString *=nullptr)
Definition: shellio.cxx:730
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:315
void EndAllAction()
Definition: edws.cxx:97
SwTableFormat * GetFrameFormat()
Definition: swtable.hxx:201
virtual void LockExpFields()=0
SwContentNode * GetContentNode(bool bPoint=true) const
Definition: pam.hxx:229
virtual void UpdateExpFields(SwTextField *pField, bool bUpdateRefFields)=0
IDocumentFieldsAccess const & getIDocumentFieldsAccess() const
Definition: doc.cxx:357
sal_uInt16 sal_Unicode
SwIndex nContent
Definition: pam.hxx:38
check if target position is in fly anchored at source range
const OUString & GetName() const
Definition: format.hxx:111
virtual bool IsExpFieldsLocked() const =0
bool InsertGlossary(SwTextBlocks &rBlock, const OUString &rEntry, SwPaM &rPaM, SwCursorShell *pShell=nullptr)
inserts an AutoText block
Definition: docglos.cxx:131
const SwTable & GetTable() const
Definition: node.hxx:497
SwDoc * GetDoc() const
Definition: viewsh.hxx:281
size_type size() const
Specific frame formats (frames, DrawObjects).
SwNode & GetEndOfContent() const
Regular ContentSection (i.e. the BodyText).
Definition: ndarr.hxx:163
SwShellCursor * GetCursor_()
Definition: crsrsh.hxx:328
SwTableSortBoxes & GetTabSortBoxes()
Definition: swtable.hxx:259
void SetCharSet(rtl_TextEncoding nVal)
Definition: shellio.hxx:72
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
bool IsSelection() const
Definition: crsrsh.hxx:879
bool ExtendedSelectedAll()
If ExtendedSelectAll() was called and selection didn't change since then.
Definition: crsrsh.cxx:605
void SetBaseURL(const OUString &rURL)
Definition: swblocks.cxx:565
#define SAL_MAX_INT32
void GetTableSel(const SwCursorShell &rShell, SwSelBoxes &rBoxes, const SwTableSearchType eSearchType)
Definition: tblsel.cxx:149
sal_uInt16 PutText(const OUString &, const OUString &, const OUString &)
Definition: swblocks.cxx:427
const SwStartNode * StartOfSectionNode() const
Definition: node.hxx:131
const SwPosition * GetPoint() const
Definition: pam.hxx:207
virtual bool CopyRange(SwPaM &rPam, SwPosition &rPos, SwCopyFlags flags) const =0
Copy a selected content range to a position.
SwIndex & Assign(SwIndexReg *, sal_Int32)
Definition: index.cxx:206
sal_uInt64 GetSize()
SwContentNode * GetContentNode()
Definition: node.hxx:615
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
Marks a character position inside a document model node.
Definition: index.hxx:33
Marks a node in the document model.
Definition: ndindex.hxx:31
ring_container GetRingContainer()
bool empty() const
const SwPosition * Start() const
Definition: pam.hxx:212
SwDoc * GetDoc()
Definition: swblocks.cxx:470
std::size_t ReadBytes(void *pData, std::size_t nSize)
bool BeginPutDoc(const OUString &, const OUString &)
Definition: swblocks.cxx:374
bool IsError() const
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
IDocumentRedlineAccess const & getIDocumentRedlineAccess() const
Definition: doc.cxx:335
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:254
SwMoveFnCollection const & fnMoveForward
SwPam::Move()/Find() default argument.
Definition: paminit.cxx:59
OUString GetSelText() const
get selected text of a node at current cursor
Definition: crsrsh.cxx:2525
void SetEndian(SvStreamEndian SvStreamEndian)
void SetColumnSelection(bool bNew)
Definition: doc.hxx:965
bool IsClipBoard() const
Definition: doc.hxx:960
bool StartsWithTable()
If document body starts with a table.
Definition: crsrsh.cxx:624
SwNodes & GetNodes()
Definition: doc.hxx:403
ParaBreakType
Definition: editsh.hxx:111
void * p
bool IsHideRedlines() const
Replacement for sw::DocumentRedlineManager::GetRedlineFlags() (this is layout-level redline hiding)...
Definition: rootfrm.hxx:417
void ClearDoc()
Definition: swblocks.cxx:477
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:352
bool InsCopyOfTable(SwPosition &rInsPos, const SwSelBoxes &rBoxes, const SwTable *pCpyTable, bool bCpyName=false, bool bCorrPos=false)
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:4288
void GetWriter(const OUString &rFltName, const OUString &rBaseURL, WriterRef &xRet)
Return writer based on the name.
Definition: fltini.cxx:160
size_t size() const
virtual void SetMark()
Unless this is called, the getter method of Mark will return Point.
Definition: pam.cxx:475
virtual void UnlockExpFields()=0
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:86
bool IsSelOnePara() const
Definition: crsrsh.cxx:1074
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2070
#define FILTER_TEXT
text filter with default codeset
Definition: iodetect.hxx:31
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1288
no RedlineFlags
OUString const & GetFileName() const
Definition: swblocks.cxx:486
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:844
const void * GetData()
SwNodeIndex & Assign(SwNodes const &rNds, sal_uLong)
Definition: ndindex.hxx:272
Base class of the Writer document model elements.
Definition: node.hxx:79