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 <osl/endian.h>
21 #include <hintids.hxx>
22 #include <svl/urihelper.hxx>
23 #include <doc.hxx>
26 #include <pam.hxx>
27 #include <docary.hxx>
28 #include <editsh.hxx>
29 #include <edimp.hxx>
30 #include <frmfmt.hxx>
31 #include <rootfrm.hxx>
32 #include <swundo.hxx>
33 #include <ndtxt.hxx>
34 #include <swtable.hxx>
35 #include <shellio.hxx>
36 #include <acorrect.hxx>
37 #include <swerror.h>
38 #include <iodetect.hxx>
39 
40 void SwEditShell::InsertGlossary( SwTextBlocks& rGlossary, const OUString& rStr )
41 {
43  GetDoc()->InsertGlossary( rGlossary, rStr, *GetCursor(), this );
44  EndAllAction();
45 }
46 
48 sal_uInt16 SwEditShell::MakeGlossary( SwTextBlocks& rBlks, const OUString& rName, const OUString& rShortName,
49  bool bSaveRelFile, const OUString* pOnlyText )
50 {
51  SwDoc* pGDoc = rBlks.GetDoc();
52 
53  OUString sBase;
54  if(bSaveRelFile)
55  {
56  INetURLObject aURL( rBlks.GetFileName() );
58  }
59  rBlks.SetBaseURL( sBase );
60 
61  if( pOnlyText )
62  return rBlks.PutText( rShortName, rName, *pOnlyText );
63 
64  rBlks.ClearDoc();
65  if( rBlks.BeginPutDoc( rShortName, rName ) )
66  {
68  CopySelToDoc( pGDoc );
70  return rBlks.PutDoc();
71  }
72 
73  return USHRT_MAX;
74 }
75 
77  const OUString& rName,
78  const OUString& rShortName,
79  bool bSaveRelFile,
80  bool bOnlyText )
81 {
83 
84  SwDoc* pGDoc = rBlock.GetDoc();
85  SwDoc* pMyDoc = GetDoc();
86 
87  OUString sBase;
88  if(bSaveRelFile)
89  {
90  INetURLObject aURL( rBlock.GetFileName() );
92  }
93  rBlock.SetBaseURL( sBase );
94  sal_uInt16 nRet = USHRT_MAX;
95 
96  if( bOnlyText )
97  {
98  KillPams();
99 
100  SwPaM* pCursor = GetCursor();
101 
102  SwNodeIndex aStt( pMyDoc->GetNodes().GetEndOfExtras(), 1 );
103  SwContentNode* pContentNd = pMyDoc->GetNodes().GoNext( &aStt );
104  const SwNode* pNd = pContentNd->FindTableNode();
105  if( !pNd )
106  pNd = pContentNd;
107 
108  pCursor->GetPoint()->nNode = *pNd;
109  if( pNd == pContentNd )
110  pCursor->GetPoint()->nContent.Assign( pContentNd, 0 );
111  pCursor->SetMark();
112 
113  // then until the end of the Node array
114  pCursor->GetPoint()->nNode = pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1;
115  pContentNd = pCursor->GetContentNode();
116  if( pContentNd )
117  pCursor->GetPoint()->nContent.Assign( pContentNd, pContentNd->Len() );
118 
119  OUString sBuf;
121  if( !sBuf.isEmpty() )
122  nRet = rBlock.PutText( rShortName, rName, sBuf );
123  }
124  else
125  {
126  rBlock.ClearDoc();
127  if( rBlock.BeginPutDoc( rShortName, rName ) )
128  {
129  SwNodeIndex aStt( pMyDoc->GetNodes().GetEndOfExtras(), 1 );
130  SwContentNode* pContentNd = pMyDoc->GetNodes().GoNext( &aStt );
131  const SwNode* pNd = pContentNd->FindTableNode();
132  if( !pNd ) pNd = pContentNd;
133  SwPaM aCpyPam( *pNd );
134  aCpyPam.SetMark();
135 
136  // then until the end of the nodes array
137  aCpyPam.GetPoint()->nNode = pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1;
138  pContentNd = aCpyPam.GetContentNode();
139  aCpyPam.GetPoint()->nContent.Assign(
140  pContentNd, pContentNd ? pContentNd->Len() : 0);
141 
142  aStt = pGDoc->GetNodes().GetEndOfExtras();
143  pContentNd = pGDoc->GetNodes().GoNext( &aStt );
144  SwPosition aInsPos( aStt, SwIndex( pContentNd ));
145  pMyDoc->getIDocumentContentOperations().CopyRange( aCpyPam, aInsPos, /*bCopyAll=*/false, /*bCheckPos=*/true );
146 
147  nRet = rBlock.PutDoc();
148  }
149  }
150  EndAllAction();
151  return nRet;
152 }
153 
156 {
157  OSL_ENSURE( pInsDoc, "no Ins.Document" );
158 
159  SwNodes& rNds = pInsDoc->GetNodes();
160 
161  SwNodeIndex aIdx( rNds.GetEndOfContent(), -1 );
162  SwContentNode *const pContentNode = aIdx.GetNode().GetContentNode();
163  SwPosition aPos( aIdx,
164  SwIndex(pContentNode, pContentNode ? pContentNode->Len() : 0));
165 
166  bool bRet = false;
167  SET_CURR_SHELL( this );
168 
170 
171  if( IsTableMode() )
172  {
173  // Copy parts of a table: create a table with the width of the original one and copy the
174  // selected boxes. The sizes are corrected on a percentage basis.
175 
176  // search boxes using the layout
177  SwTableNode* pTableNd;
178  SwSelBoxes aBoxes;
179  GetTableSel( *this, aBoxes );
180  if( !aBoxes.empty() && nullptr != (pTableNd = const_cast<SwTableNode*>(aBoxes[0]
181  ->GetSttNd()->FindTableNode()) ))
182  {
183  // check if the table name can be copied
184  bool bCpyTableNm = aBoxes.size() == pTableNd->GetTable().GetTabSortBoxes().size();
185  if( bCpyTableNm )
186  {
187  const OUString rTableName = pTableNd->GetTable().GetFrameFormat()->GetName();
188  const SwFrameFormats& rTableFormats = *pInsDoc->GetTableFrameFormats();
189  for( auto n = rTableFormats.size(); n; )
190  if( rTableFormats[ --n ]->GetName() == rTableName )
191  {
192  bCpyTableNm = false;
193  break;
194  }
195  }
196  bRet = pInsDoc->InsCopyOfTable( aPos, aBoxes, nullptr, bCpyTableNm );
197  }
198  else
199  bRet = false;
200  }
201  else
202  {
203  bool bColSel = GetCursor_()->IsColumnSelection();
204  if( bColSel && pInsDoc->IsClipBoard() )
205  pInsDoc->SetColumnSelection( true );
206  bool bSelectAll = StartsWithTable() && ExtendedSelectedAll();
207  {
208  for(SwPaM& rPaM : GetCursor()->GetRingContainer())
209  {
210  if( !rPaM.HasMark() )
211  {
212  SwContentNode *const pNd = rPaM.GetContentNode();
213  if (nullptr != pNd &&
214  ( bColSel || !pNd->GetTextNode() ) )
215  {
216  rPaM.SetMark();
217  rPaM.Move( fnMoveForward, GoInContent );
218  bRet = GetDoc()->getIDocumentContentOperations().CopyRange( rPaM, aPos, /*bCopyAll=*/false, /*bCheckPos=*/true )
219  || bRet;
220  rPaM.Exchange();
221  rPaM.DeleteMark();
222  }
223  }
224  else
225  {
226  // Make a copy, so that in case we need to adjust the selection
227  // for the purpose of copying, our shell cursor is not touched.
228  // (Otherwise we would have to restore it.)
229  SwPaM aPaM(*rPaM.GetMark(), *rPaM.GetPoint());
230  if (bSelectAll)
231  {
232  // Selection starts at the first para of the first cell,
233  // but we want to copy the table and the start node before
234  // the first cell as well.
235  aPaM.Start()->nNode = aPaM.Start()->nNode.GetNode().FindTableNode()->GetIndex();
236  aPaM.Start()->nContent.Assign(nullptr, 0);
237  }
238  bRet = GetDoc()->getIDocumentContentOperations().CopyRange( aPaM, aPos, /*bCopyAll=*/false, /*bCheckPos=*/true ) || bRet;
239  }
240  }
241  }
242  }
243 
245  if( !pInsDoc->getIDocumentFieldsAccess().IsExpFieldsLocked() )
246  pInsDoc->getIDocumentFieldsAccess().UpdateExpFields(nullptr, true);
247 
248  return bRet;
249 }
250 
253 void SwEditShell::GetSelectedText( OUString &rBuf, ParaBreakType nHndlParaBrk )
254 {
255  GetCursor(); // creates all cursors if needed
256  if( IsSelOnePara() )
257  {
258  rBuf = GetSelText();
259  if( ParaBreakType::ToBlank == nHndlParaBrk )
260  {
261  rBuf = rBuf.replaceAll("\x0a", " ");
262  }
263  else if( IsSelFullPara() &&
264  ParaBreakType::ToOnlyCR != nHndlParaBrk )
265  {
266 #ifdef _WIN32
267  rBuf += "\015\012";
268 #else
269  rBuf += "\012";
270 #endif
271  }
272  }
273  else if( IsSelection() )
274  {
275  SvMemoryStream aStream;
276 #ifdef OSL_BIGENDIAN
277  aStream.SetEndian( SvStreamEndian::BIG );
278 #else
279  aStream.SetEndian( SvStreamEndian::LITTLE );
280 #endif
281  WriterRef xWrt;
282  SwReaderWriter::GetWriter( FILTER_TEXT, OUString(), xWrt );
283  if( xWrt.is() )
284  {
285  // write selected areas into a ASCII document
286  SwWriter aWriter( aStream, *this);
287  xWrt->SetShowProgress(false);
288 
289  switch( nHndlParaBrk )
290  {
292  xWrt->m_bASCII_ParaAsBlank = true;
293  xWrt->m_bASCII_NoLastLineEnd = true;
294  break;
295 
297  xWrt->m_bASCII_ParaAsCR = true;
298  xWrt->m_bASCII_NoLastLineEnd = true;
299  break;
300  }
301 
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();
308 
309  if ( ! aWriter.Write(xWrt).IsError() )
310  {
311  aStream.WriteUInt16( '\0' );
312 
313  const sal_Unicode *p = static_cast<sal_Unicode const *>(aStream.GetData());
314  if (p)
315  rBuf = OUString(p);
316  else
317  {
318  const sal_uInt64 nLen = aStream.GetSize();
319  OSL_ENSURE( nLen/sizeof( sal_Unicode )<static_cast<sal_uInt64>(SAL_MAX_INT32), "Stream can't fit in OUString" );
320  rtl_uString *pStr = rtl_uString_alloc(static_cast<sal_Int32>(nLen / sizeof( sal_Unicode )));
321  aStream.Seek( 0 );
322  aStream.ResetError();
323  //endian specific?, yipes!
324  aStream.ReadBytes(pStr->buffer, nLen);
325  rBuf = OUString(pStr, SAL_NO_ACQUIRE);
326  }
327  }
328  }
329  }
330 }
331 
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool is() const
void InsertGlossary(SwTextBlocks &rGlossary, const OUString &)
Glossary from glossary document in current document.
Definition: edglss.cxx:40
bool IsColumnSelection() const
Definition: swcrsr.hxx:212
void KillPams()
Definition: crsrsh.cxx:1010
sal_uLong GetIndex() const
Definition: node.hxx:282
bool GoInContent(SwPaM &rPam, SwMoveFnCollection const &fnMove)
Definition: pam.cxx:903
bool CopySelToDoc(SwDoc *pInsDoc)
For copying via ClipBoard: If table is copied into table, move all cursors away from it...
Definition: edglss.cxx:155
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:185
SvStream & WriteUInt16(sal_uInt16 nUInt16)
virtual void ResetError() override
sal_uInt16 PutDoc()
Definition: swblocks.cxx:436
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:48
const SwFrameFormats * GetTableFrameFormats() const
Definition: doc.hxx:806
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:253
Definition: doc.hxx:185
sal_uInt64 Seek(sal_uInt64 nPos)
SwNode & GetNode() const
Definition: ndindex.hxx:118
bool IsSelFullPara() const
Definition: crsrsh.cxx:3255
ErrCode Write(WriterRef const &rxWriter, const OUString *=nullptr)
Definition: shellio.cxx:743
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:347
void EndAllAction()
Definition: edws.cxx:96
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:389
sal_uInt16 sal_Unicode
SwIndex nContent
Definition: pam.hxx:38
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:284
size_type size() const
Specific frame formats (frames, DrawObjects).
Definition: docary.hxx:201
SwNode & GetEndOfContent() const
Regular ContentSection (i.e. the BodyText).
Definition: ndarr.hxx:164
SwShellCursor * GetCursor_()
Definition: crsrsh.hxx:330
SwTableSortBoxes & GetTabSortBoxes()
Definition: swtable.hxx:259
void SetCharSet(rtl_TextEncoding nVal)
Definition: shellio.hxx:77
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
bool IsSelection() const
Definition: crsrsh.hxx:875
bool ExtendedSelectedAll()
If ExtendedSelectAll() was called and selection didn't change since then.
Definition: crsrsh.cxx:596
void SetBaseURL(const OUString &rURL)
Definition: swblocks.cxx:603
#define SAL_MAX_INT32
void GetTableSel(const SwCursorShell &rShell, SwSelBoxes &rBoxes, const SwTableSearchType eSearchType)
Definition: tblsel.cxx:144
sal_uInt16 PutText(const OUString &, const OUString &, const OUString &)
Definition: swblocks.cxx:465
const SwPosition * GetPoint() const
Definition: pam.hxx:207
SwIndex & Assign(SwIndexReg *, sal_Int32)
Definition: index.cxx:198
sal_uInt64 GetSize()
SwContentNode * GetContentNode()
Definition: node.hxx:615
Marks a character position inside a document model node.
Definition: index.hxx:37
#define SET_CURR_SHELL(shell)
Definition: swtypes.hxx:101
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:508
std::size_t ReadBytes(void *pData, std::size_t nSize)
bool BeginPutDoc(const OUString &, const OUString &)
Definition: swblocks.cxx:412
OUString GetMainURL(DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
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:76
IDocumentRedlineAccess const & getIDocumentRedlineAccess() const
Definition: doc.cxx:367
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:259
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:2429
void SetEndian(SvStreamEndian SvStreamEndian)
void SetColumnSelection(bool bNew)
Definition: doc.hxx:959
bool IsClipBoard() const
Definition: doc.hxx:954
bool StartsWithTable()
If document body starts with a table.
Definition: crsrsh.cxx:614
SwNodes & GetNodes()
Definition: doc.hxx:403
ParaBreakType
Definition: editsh.hxx:113
bool IsHideRedlines() const
Replacement for sw::DocumentRedlineManager::GetRedlineFlags() (this is layout-level redline hiding)...
Definition: rootfrm.hxx:416
void ClearDoc()
Definition: swblocks.cxx:515
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:351
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:4243
void GetWriter(const OUString &rFltName, const OUString &rBaseURL, WriterRef &xRet)
Return writer based on the name.
Definition: fltini.cxx:177
size_t size() const
Definition: docary.hxx:225
virtual void SetMark()
Unless this is called, the getter method of Mark will return Point.
Definition: pam.cxx:457
virtual void UnlockExpFields()=0
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
bool IsSelOnePara() const
Definition: crsrsh.cxx:1064
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2072
#define FILTER_TEXT
text filter with default codeset
Definition: iodetect.hxx:31
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1273
no RedlineFlags
OUString const & GetFileName() const
Definition: swblocks.cxx:524
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:842
const void * GetData()
Base class of the Writer document model elements.
Definition: node.hxx:79
virtual bool CopyRange(SwPaM &rPam, SwPosition &rPos, const bool bCopyAll, bool bCheckPos) const =0
Copy a selected content range to a position.