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 <tools/urlobj.hxx>
22 #include <doc.hxx>
25 #include <pam.hxx>
26 #include <docary.hxx>
27 #include <editsh.hxx>
28 #include <frmfmt.hxx>
29 #include <rootfrm.hxx>
30 #include <ndtxt.hxx>
31 #include <swtable.hxx>
32 #include <shellio.hxx>
33 #include <iodetect.hxx>
34 
35 void SwEditShell::InsertGlossary( SwTextBlocks& rGlossary, const OUString& rStr )
36 {
38  GetDoc()->InsertGlossary( rGlossary, rStr, *GetCursor(), this );
39  EndAllAction();
40 }
41 
43 sal_uInt16 SwEditShell::MakeGlossary( SwTextBlocks& rBlks, const OUString& rName, const OUString& rShortName,
44  bool bSaveRelFile, const OUString* pOnlyText )
45 {
46  SwDoc* pGDoc = rBlks.GetDoc();
47 
48  OUString sBase;
49  if(bSaveRelFile)
50  {
51  INetURLObject aURL( rBlks.GetFileName() );
53  }
54  rBlks.SetBaseURL( sBase );
55 
56  if( pOnlyText )
57  return rBlks.PutText( rShortName, rName, *pOnlyText );
58 
59  rBlks.ClearDoc();
60  if( rBlks.BeginPutDoc( rShortName, rName ) )
61  {
63  CopySelToDoc( pGDoc );
65  return rBlks.PutDoc();
66  }
67 
68  return USHRT_MAX;
69 }
70 
72  const OUString& rName,
73  const OUString& rShortName,
74  bool bSaveRelFile,
75  bool bOnlyText )
76 {
78 
79  SwDoc* pGDoc = rBlock.GetDoc();
80  SwDoc* pMyDoc = GetDoc();
81 
82  OUString sBase;
83  if(bSaveRelFile)
84  {
85  INetURLObject aURL( rBlock.GetFileName() );
87  }
88  rBlock.SetBaseURL( sBase );
89  sal_uInt16 nRet = USHRT_MAX;
90 
91  if( bOnlyText )
92  {
93  KillPams();
94 
95  SwPaM* pCursor = GetCursor();
96 
97  SwNodeIndex aStt( pMyDoc->GetNodes().GetEndOfExtras(), 1 );
98  SwContentNode* pContentNd = pMyDoc->GetNodes().GoNext( &aStt );
99  const SwNode* pNd = pContentNd->FindTableNode();
100  if( !pNd )
101  pNd = pContentNd;
102 
103  pCursor->GetPoint()->nNode = *pNd;
104  if( pNd == pContentNd )
105  pCursor->GetPoint()->nContent.Assign( pContentNd, 0 );
106  pCursor->SetMark();
107 
108  // then until the end of the Node array
109  pCursor->GetPoint()->nNode = pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1;
110  pContentNd = pCursor->GetContentNode();
111  if( pContentNd )
112  pCursor->GetPoint()->nContent.Assign( pContentNd, pContentNd->Len() );
113 
114  OUString sBuf;
116  if( !sBuf.isEmpty() )
117  nRet = rBlock.PutText( rShortName, rName, sBuf );
118  }
119  else
120  {
121  rBlock.ClearDoc();
122  if( rBlock.BeginPutDoc( rShortName, rName ) )
123  {
124  SwNodeIndex aStt( pMyDoc->GetNodes().GetEndOfExtras(), 1 );
125  SwContentNode* pContentNd = pMyDoc->GetNodes().GoNext( &aStt );
126  const SwNode* pNd = pContentNd->FindTableNode();
127  if( !pNd ) pNd = pContentNd;
128  SwPaM aCpyPam( *pNd );
129  aCpyPam.SetMark();
130 
131  // then until the end of the nodes array
132  aCpyPam.GetPoint()->nNode = pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1;
133  pContentNd = aCpyPam.GetContentNode();
134  aCpyPam.GetPoint()->nContent.Assign(
135  pContentNd, pContentNd ? pContentNd->Len() : 0);
136 
137  aStt = pGDoc->GetNodes().GetEndOfExtras();
138  pContentNd = pGDoc->GetNodes().GoNext( &aStt );
139  SwPosition aInsPos( aStt, SwIndex( pContentNd ));
140  pMyDoc->getIDocumentContentOperations().CopyRange( aCpyPam, aInsPos, /*bCopyAll=*/false, /*bCheckPos=*/true );
141 
142  nRet = rBlock.PutDoc();
143  }
144  }
145  EndAllAction();
146  return nRet;
147 }
148 
151 {
152  OSL_ENSURE( pInsDoc, "no Ins.Document" );
153 
154  SwNodes& rNds = pInsDoc->GetNodes();
155 
156  SwNodeIndex aIdx( rNds.GetEndOfContent(), -1 );
157  SwContentNode *const pContentNode = aIdx.GetNode().GetContentNode();
158  SwPosition aPos( aIdx,
159  SwIndex(pContentNode, pContentNode ? pContentNode->Len() : 0));
160 
161  bool bRet = false;
162  SET_CURR_SHELL( this );
163 
165 
166  if( IsTableMode() )
167  {
168  // Copy parts of a table: create a table with the width of the original one and copy the
169  // selected boxes. The sizes are corrected on a percentage basis.
170 
171  // search boxes using the layout
172  SwTableNode* pTableNd;
173  SwSelBoxes aBoxes;
174  GetTableSel( *this, aBoxes );
175  if( !aBoxes.empty() && nullptr != (pTableNd = const_cast<SwTableNode*>(aBoxes[0]
176  ->GetSttNd()->FindTableNode()) ))
177  {
178  // check if the table name can be copied
179  bool bCpyTableNm = aBoxes.size() == pTableNd->GetTable().GetTabSortBoxes().size();
180  if( bCpyTableNm )
181  {
182  const OUString rTableName = pTableNd->GetTable().GetFrameFormat()->GetName();
183  const SwFrameFormats& rTableFormats = *pInsDoc->GetTableFrameFormats();
184  for( auto n = rTableFormats.size(); n; )
185  if( rTableFormats[ --n ]->GetName() == rTableName )
186  {
187  bCpyTableNm = false;
188  break;
189  }
190  }
191  bRet = pInsDoc->InsCopyOfTable( aPos, aBoxes, nullptr, bCpyTableNm );
192  }
193  else
194  bRet = false;
195  }
196  else
197  {
198  bool bColSel = GetCursor_()->IsColumnSelection();
199  if( bColSel && pInsDoc->IsClipBoard() )
200  pInsDoc->SetColumnSelection( true );
201  bool bSelectAll = StartsWithTable() && ExtendedSelectedAll();
202  {
203  for(SwPaM& rPaM : GetCursor()->GetRingContainer())
204  {
205  if( !rPaM.HasMark() )
206  {
207  SwContentNode *const pNd = rPaM.GetContentNode();
208  if (nullptr != pNd &&
209  ( bColSel || !pNd->GetTextNode() ) )
210  {
211  rPaM.SetMark();
212  rPaM.Move( fnMoveForward, GoInContent );
213  bRet = GetDoc()->getIDocumentContentOperations().CopyRange( rPaM, aPos, /*bCopyAll=*/false, /*bCheckPos=*/true )
214  || bRet;
215  rPaM.Exchange();
216  rPaM.DeleteMark();
217  }
218  }
219  else
220  {
221  // Make a copy, so that in case we need to adjust the selection
222  // for the purpose of copying, our shell cursor is not touched.
223  // (Otherwise we would have to restore it.)
224  SwPaM aPaM(*rPaM.GetMark(), *rPaM.GetPoint());
225  if (bSelectAll)
226  {
227  // Selection starts at the first para of the first cell,
228  // but we want to copy the table and the start node before
229  // the first cell as well.
230  aPaM.Start()->nNode = aPaM.Start()->nNode.GetNode().FindTableNode()->GetIndex();
231  aPaM.Start()->nContent.Assign(nullptr, 0);
232  }
233  bRet = GetDoc()->getIDocumentContentOperations().CopyRange( aPaM, aPos, /*bCopyAll=*/false, /*bCheckPos=*/true ) || bRet;
234  }
235  }
236  }
237  }
238 
240  if( !pInsDoc->getIDocumentFieldsAccess().IsExpFieldsLocked() )
241  pInsDoc->getIDocumentFieldsAccess().UpdateExpFields(nullptr, true);
242 
243  return bRet;
244 }
245 
248 void SwEditShell::GetSelectedText( OUString &rBuf, ParaBreakType nHndlParaBrk )
249 {
250  GetCursor(); // creates all cursors if needed
251  if( IsSelOnePara() )
252  {
253  rBuf = GetSelText();
254  if( ParaBreakType::ToBlank == nHndlParaBrk )
255  {
256  rBuf = rBuf.replaceAll("\x0a", " ");
257  }
258  else if( IsSelFullPara() &&
259  ParaBreakType::ToOnlyCR != nHndlParaBrk )
260  {
261 #ifdef _WIN32
262  rBuf += "\015\012";
263 #else
264  rBuf += "\012";
265 #endif
266  }
267  }
268  else if( IsSelection() )
269  {
270  SvMemoryStream aStream;
271 #ifdef OSL_BIGENDIAN
272  aStream.SetEndian( SvStreamEndian::BIG );
273 #else
274  aStream.SetEndian( SvStreamEndian::LITTLE );
275 #endif
276  WriterRef xWrt;
277  SwReaderWriter::GetWriter( FILTER_TEXT, OUString(), xWrt );
278  if( xWrt.is() )
279  {
280  // write selected areas into an ASCII document
281  SwWriter aWriter( aStream, *this);
282  xWrt->SetShowProgress(false);
283 
284  switch( nHndlParaBrk )
285  {
287  xWrt->m_bASCII_ParaAsBlank = true;
288  xWrt->m_bASCII_NoLastLineEnd = true;
289  break;
290 
292  xWrt->m_bASCII_ParaAsCR = true;
293  xWrt->m_bASCII_NoLastLineEnd = true;
294  break;
295  }
296 
297  //JP 09.05.00: write as UNICODE ! (and not as ANSI)
298  SwAsciiOptions aAsciiOpt( xWrt->GetAsciiOptions() );
299  aAsciiOpt.SetCharSet( RTL_TEXTENCODING_UCS2 );
300  xWrt->SetAsciiOptions( aAsciiOpt );
301  xWrt->m_bUCS2_WithStartChar = false;
302  xWrt->m_bHideDeleteRedlines = GetLayout()->IsHideRedlines();
303 
304  if ( ! aWriter.Write(xWrt).IsError() )
305  {
306  aStream.WriteUInt16( '\0' );
307 
308  const sal_Unicode *p = static_cast<sal_Unicode const *>(aStream.GetData());
309  if (p)
310  rBuf = OUString(p);
311  else
312  {
313  const sal_uInt64 nLen = aStream.GetSize();
314  OSL_ENSURE( nLen/sizeof( sal_Unicode )<static_cast<sal_uInt64>(SAL_MAX_INT32), "Stream can't fit in OUString" );
315  rtl_uString *pStr = rtl_uString_alloc(static_cast<sal_Int32>(nLen / sizeof( sal_Unicode )));
316  aStream.Seek( 0 );
317  aStream.ResetError();
318  //endian specific?, yipes!
319  aStream.ReadBytes(pStr->buffer, nLen);
320  rBuf = OUString(pStr, SAL_NO_ACQUIRE);
321  }
322  }
323  }
324  }
325 }
326 
327 /* 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:35
bool IsColumnSelection() const
Definition: swcrsr.hxx:212
void KillPams()
Definition: crsrsh.cxx:1009
sal_uLong GetIndex() const
Definition: node.hxx:282
bool GoInContent(SwPaM &rPam, SwMoveFnCollection const &fnMove)
Definition: pam.cxx:905
bool CopySelToDoc(SwDoc *pInsDoc)
For copying via ClipBoard: If table is copied into table, move all cursors away from it...
Definition: edglss.cxx:150
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:184
SvStream & WriteUInt16(sal_uInt16 nUInt16)
virtual void ResetError() override
sal_uInt16 PutDoc()
Definition: swblocks.cxx:404
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:43
const SwFrameFormats * GetTableFrameFormats() const
Definition: doc.hxx:807
virtual void SetRedlineFlags_intern(RedlineFlags eMode)=0
Set a new redline mode.
SwNodeIndex nNode
Definition: pam.hxx:37
bool IsTableMode() const
Definition: crsrsh.hxx:647
void GetSelectedText(OUString &rBuf, ParaBreakType nHndlParaBreak=ParaBreakType::ToBlank)
Query text within selection.
Definition: edglss.cxx:248
Definition: doc.hxx:185
sal_uInt64 Seek(sal_uInt64 nPos)
SwNode & GetNode() const
Definition: ndindex.hxx:119
bool IsSelFullPara() const
Definition: crsrsh.cxx:3340
ErrCode Write(WriterRef const &rxWriter, const OUString *=nullptr)
Definition: shellio.cxx:726
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:314
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:356
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:332
SwTableSortBoxes & GetTabSortBoxes()
Definition: swtable.hxx:259
void SetCharSet(rtl_TextEncoding nVal)
Definition: shellio.hxx:78
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
bool IsSelection() const
Definition: crsrsh.hxx:883
bool ExtendedSelectedAll()
If ExtendedSelectAll() was called and selection didn't change since then.
Definition: crsrsh.cxx:595
void SetBaseURL(const OUString &rURL)
Definition: swblocks.cxx:571
#define SAL_MAX_INT32
void GetTableSel(const SwCursorShell &rShell, SwSelBoxes &rBoxes, const SwTableSearchType eSearchType)
Definition: tblsel.cxx:141
sal_uInt16 PutText(const OUString &, const OUString &, const OUString &)
Definition: swblocks.cxx:433
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:476
std::size_t ReadBytes(void *pData, std::size_t nSize)
bool BeginPutDoc(const OUString &, const OUString &)
Definition: swblocks.cxx:380
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:71
IDocumentRedlineAccess const & getIDocumentRedlineAccess() const
Definition: doc.cxx:334
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:261
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:2507
void SetEndian(SvStreamEndian SvStreamEndian)
void SetColumnSelection(bool bNew)
Definition: doc.hxx:961
bool IsClipBoard() const
Definition: doc.hxx:956
bool StartsWithTable()
If document body starts with a table.
Definition: crsrsh.cxx:613
SwNodes & GetNodes()
Definition: doc.hxx:402
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:483
SwTableNode * FindTableNode()
Search table node, in which it is.
Definition: node.cxx:349
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:4236
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:459
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:1063
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2075
#define FILTER_TEXT
text filter with default codeset
Definition: iodetect.hxx:31
SwContentNode * GoNext(SwNodeIndex *) const
Definition: nodes.cxx:1271
no RedlineFlags
OUString const & GetFileName() const
Definition: swblocks.cxx:492
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.