LibreOffice Module sw (master)  1
edtox.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 <com/sun/star/util/SearchAlgorithms2.hpp>
21 #include <com/sun/star/util/SearchFlags.hpp>
24 #include <i18nutil/searchopt.hxx>
25 #include <svl/fstathelper.hxx>
26 #include <osl/diagnose.h>
27 #include <osl/thread.h>
28 #include <unotools/syslocale.hxx>
29 
30 #include <sfx2/docfile.hxx>
31 
32 #include <swtypes.hxx>
33 #include <rootfrm.hxx>
34 #include <editsh.hxx>
35 #include <doc.hxx>
37 #include <IDocumentUndoRedo.hxx>
38 #include <pam.hxx>
39 #include <swundo.hxx>
40 #include <tox.hxx>
41 #include <doctxm.hxx>
42 #include <docary.hxx>
43 #include <mdiexp.hxx>
44 #include <strings.hrc>
45 
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::i18n;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::util;
50 
51 // Add/delete listing markers to a document
52 
53 void SwEditShell::Insert(const SwTOXMark& rMark)
54 {
55  bool bInsAtPos = rMark.IsAlternativeText();
57  for(SwPaM& rPaM : GetCursor()->GetRingContainer())
58  {
59  const SwPosition *pStt = rPaM.Start(),
60  *pEnd = rPaM.End();
61  if( bInsAtPos )
62  {
63  SwPaM aTmp( *pStt );
65  }
66  else if( *pEnd != *pStt )
67  {
69  rPaM, rMark, SetAttrMode::DONTEXPAND );
70  }
71 
72  }
73  EndAllAction();
74 }
75 
77 {
78  CurrShell aCurr( this );
80 
81  mxDoc->DeleteTOXMark( pMark );
82 
83  EndAllAction();
84 }
85 
88 {
89  SwDoc::GetCurTOXMark( *GetCursor()->Start(), rMarks );
90 }
91 
93 {
94  assert( dynamic_cast<const SwTOXBaseSection*>( &rTOXBase) && "no TOXBaseSection!" );
95  const SwTOXBaseSection& rTOXSect = static_cast<const SwTOXBaseSection&>(rTOXBase);
96  return rTOXSect.IsProtect();
97 }
98 
99 void SwEditShell::SetTOXBaseReadonly(const SwTOXBase& rTOXBase, bool bReadonly)
100 {
101  assert( dynamic_cast<const SwTOXBaseSection*>( &rTOXBase) && "no TOXBaseSection!" );
102  const SwTOXBaseSection& rTOXSect = static_cast<const SwTOXBaseSection&>(rTOXBase);
103  const_cast<SwTOXBase&>(rTOXBase).SetProtected(bReadonly);
104  OSL_ENSURE( rTOXSect.SwSection::GetType() == SectionType::ToxContent, "not a TOXContentSection" );
105 
106  SwSectionData aSectionData(rTOXSect);
107  aSectionData.SetProtectFlag(bReadonly);
108  UpdateSection( GetSectionFormatPos( *rTOXSect.GetFormat() ), aSectionData );
109 }
110 
112 {
113  return GetDoc()->GetDefaultTOXBase( eTyp, bCreate );
114 }
115 
117 {
118  GetDoc()->SetDefaultTOXBase(rBase);
119 }
120 
122 void SwEditShell::InsertTableOf( const SwTOXBase& rTOX, const SfxItemSet* pSet )
123 {
124  CurrShell aCurr( this );
125  StartAllAction();
126 
127  SwDocShell* pDocSh = GetDoc()->GetDocShell();
128  ::StartProgress( STR_STATSTR_TOX_INSERT, 0, 0, pDocSh );
129 
130  // Insert listing
131  const SwTOXBaseSection* pTOX = mxDoc->InsertTableOf(
132  *GetCursor()->GetPoint(), rTOX, pSet, true, GetLayout() );
133  OSL_ENSURE(pTOX, "No current TOX");
134 
135  // start formatting
136  CalcLayout();
137 
138  // insert page numbering
139  const_cast<SwTOXBaseSection*>(pTOX)->UpdatePageNum();
140 
141  pTOX->SetPosAtStartEnd( *GetCursor()->GetPoint() );
142 
143  // Fix for empty listing
145  ::EndProgress( pDocSh );
146  EndAllAction();
147 }
148 
150 void SwEditShell::UpdateTableOf(const SwTOXBase& rTOX, const SfxItemSet* pSet)
151 {
152  assert(dynamic_cast<const SwTOXBaseSection*>(&rTOX) && "no TOXBaseSection!");
153  SwTOXBaseSection& rTOXSect = static_cast<SwTOXBaseSection&>(const_cast<SwTOXBase&>(rTOX));
154  if (!rTOXSect.GetFormat()->GetSectionNode())
155  return;
156 
157  SwDoc* pMyDoc = GetDoc();
158  SwDocShell* pDocSh = pMyDoc->GetDocShell();
159 
160  bool bInIndex = &rTOX == GetCurTOX();
161  CurrShell aCurr( this );
162  StartAllAction();
163 
164  ::StartProgress( STR_STATSTR_TOX_UPDATE, 0, 0, pDocSh );
165 
167 
168  // create listing stub
169  rTOXSect.Update(pSet, GetLayout());
170 
171  // correct Cursor
172  if( bInIndex )
173  rTOXSect.SetPosAtStartEnd(*GetCursor()->GetPoint());
174 
175  // start formatting
176  // tdf#139426 ...but allow suppression of AssertFlyPages
178  CalcLayout();
180 
181  // insert page numbering
182  rTOXSect.UpdatePageNum();
183 
185 
186  ::EndProgress( pDocSh );
187  EndAllAction();
188 }
189 
192 {
193  return SwDoc::GetCurTOX( *GetCursor()->GetPoint() );
194 }
195 
196 bool SwEditShell::DeleteTOX( const SwTOXBase& rTOXBase, bool bDelNodes )
197 {
198  return GetDoc()->DeleteTOX( rTOXBase, bDelNodes );
199 }
200 
201 // manage types of listings
202 
203 const SwTOXType* SwEditShell::GetTOXType(TOXTypes eTyp, sal_uInt16 nId) const
204 {
205  return mxDoc->GetTOXType(eTyp, nId);
206 }
207 
208 // manage keys for the alphabetical index
209 
210 void SwEditShell::GetTOIKeys( SwTOIKeyType eTyp, std::vector<OUString>& rArr ) const
211 {
212  GetDoc()->GetTOIKeys( eTyp, rArr, *GetLayout() );
213 }
214 
215 sal_uInt16 SwEditShell::GetTOXCount() const
216 {
217  const SwSectionFormats& rFormats = GetDoc()->GetSections();
218  sal_uInt16 nRet = 0;
219  for( auto n = rFormats.size(); n; )
220  {
221  const SwSection* pSect = rFormats[ --n ]->GetSection();
222  if( SectionType::ToxContent == pSect->GetType() &&
223  pSect->GetFormat()->GetSectionNode() )
224  ++nRet;
225  }
226  return nRet;
227 }
228 
229 const SwTOXBase* SwEditShell::GetTOX( sal_uInt16 nPos ) const
230 {
231  const SwSectionFormats& rFormats = GetDoc()->GetSections();
232  sal_uInt16 nCnt {0};
233  for( const SwSectionFormat *pFormat : rFormats )
234  {
235  const SwSection* pSect = pFormat->GetSection();
236  if( SectionType::ToxContent == pSect->GetType() &&
237  pSect->GetFormat()->GetSectionNode() &&
238  nCnt++ == nPos )
239  {
240  assert( dynamic_cast<const SwTOXBaseSection*>( pSect) && "no TOXBaseSection!" );
241  return static_cast<const SwTOXBaseSection*>(pSect);
242  }
243  }
244  return nullptr;
245 }
246 
248 void SwEditShell::SetUpdateTOX( bool bFlag )
249 {
250  GetDoc()->SetUpdateTOX( bFlag );
251 }
252 
254 {
255  return GetDoc()->IsUpdateTOX();
256 }
257 
258 OUString const & SwEditShell::GetTOIAutoMarkURL() const
259 {
260  return GetDoc()->GetTOIAutoMarkURL();
261 }
262 
263 void SwEditShell::SetTOIAutoMarkURL(const OUString& rSet)
264 {
265  GetDoc()->SetTOIAutoMarkURL(rSet);
266 }
267 
269 {
270  StartAllAction();
271  bool bDoesUndo = DoesUndo();
272  DoUndo(false);
273  //1. remove all automatic generated index entries if AutoMarkURL has a
274  // length and the file exists
275  //2. load file
276  //3. select all occurrences of the searched words
277  //4. apply index entries
278 
279  OUString sAutoMarkURL(GetDoc()->GetTOIAutoMarkURL());
280  if( !sAutoMarkURL.isEmpty() && FStatHelper::IsDocument( sAutoMarkURL ))
281  {
282  //1.
283  const SwTOXType* pTOXType = GetTOXType(TOX_INDEX, 0);
284 
285  SwTOXMarks aMarks;
286  pTOXType->CollectTextMarks(aMarks);
287  for( SwTOXMark* pMark : aMarks )
288  {
289  if(pMark->IsAutoGenerated() && pMark->GetTextTOXMark())
290  // mba: test iteration; objects are deleted in iteration
291  DeleteTOXMark(pMark);
292  }
293 
294  //2.
295  SfxMedium aMedium( sAutoMarkURL, StreamMode::STD_READ );
296  SvStream& rStrm = *aMedium.GetInStream();
297  Push();
298  rtl_TextEncoding eChrSet = ::osl_getThreadTextEncoding();
299 
300  // SearchOptions to be used in loop below
301  sal_Int32 const nLEV_Other = 2; // -> changedChars;
302  sal_Int32 const nLEV_Longer = 3;
303  sal_Int32 const nLEV_Shorter = 1;
304 
305  i18nutil::SearchOptions2 aSearchOpt(
306  SearchAlgorithms_ABSOLUTE,
307  SearchFlags::LEV_RELAXED,
308  "", "",
309  SvtSysLocale().GetLanguageTag().getLocale(),
310  nLEV_Other, nLEV_Longer, nLEV_Shorter,
311  TransliterationFlags::NONE,
312  SearchAlgorithms2::ABSOLUTE,
313  '\\' );
314 
315  while (rStrm.good())
316  {
317  OString aRdLine;
318  rStrm.ReadLine( aRdLine );
319 
320  // # -> comment
321  // ; -> delimiter between entries ->
322  // Format: TextToSearchFor;AlternativeString;PrimaryKey;SecondaryKey;CaseSensitive;WordOnly
323  // Leading and trailing blanks are ignored
324  if( !aRdLine.isEmpty() && '#' != aRdLine[0] )
325  {
326  OUString sLine(OStringToOUString(aRdLine, eChrSet));
327 
328  sal_Int32 nTokenPos = 0;
329  OUString sToSelect( sLine.getToken(0, ';', nTokenPos ) );
330  if( !sToSelect.isEmpty() )
331  {
332  OUString sAlternative = sLine.getToken(0, ';', nTokenPos);
333  OUString sPrimary = sLine.getToken(0, ';', nTokenPos);
334  OUString sSecondary = sLine.getToken(0, ';', nTokenPos);
335  OUString sCase = sLine.getToken(0, ';', nTokenPos);
336  OUString sWordOnly = sLine.getToken(0, ';', nTokenPos);
337 
338  //3.
339  bool bCaseSensitive = !sCase.isEmpty() && sCase != "0";
340  bool bWordOnly = !sWordOnly.isEmpty() && sWordOnly != "0";
341 
342  if (!bCaseSensitive)
343  {
344  aSearchOpt.transliterateFlags |=
345  TransliterationFlags::IGNORE_CASE;
346  }
347  else
348  {
349  aSearchOpt.transliterateFlags &=
350  ~TransliterationFlags::IGNORE_CASE;
351  }
352  if ( bWordOnly)
353  aSearchOpt.searchFlag |= SearchFlags::NORM_WORD_ONLY;
354  else
355  aSearchOpt.searchFlag &= ~SearchFlags::NORM_WORD_ONLY;
356 
357  aSearchOpt.searchString = sToSelect;
358 
359  KillPams();
360  bool bCancel;
361 
362  // todo/mba: assuming that notes shouldn't be searched
363  sal_uLong nRet = Find_Text(aSearchOpt, false/*bSearchInNotes*/, SwDocPositions::Start, SwDocPositions::End, bCancel,
365 
366  if(nRet)
367  {
368  SwTOXMark* pTmpMark = new SwTOXMark(pTOXType);
369  if( !sPrimary.isEmpty() )
370  {
371  pTmpMark->SetPrimaryKey( sPrimary );
372  if( !sSecondary.isEmpty() )
373  pTmpMark->SetSecondaryKey( sSecondary );
374  }
375  if( !sAlternative.isEmpty() )
376  pTmpMark->SetAlternativeText(sAlternative);
377  pTmpMark->SetMainEntry(false);
378  pTmpMark->SetAutoGenerated(true);
379  //4.
380  SwEditShell::Insert(*pTmpMark);
381  }
382  }
383  }
384  }
385  KillPams();
387  }
388  DoUndo(bDoesUndo);
389  EndAllAction();
390 }
391 
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
TOXTypes
Definition: toxe.hxx:39
SVL_DLLPUBLIC bool IsDocument(const OUString &rURL)
void KillPams()
Definition: crsrsh.cxx:1022
bool IsAlternativeText() const
Definition: tox.hxx:591
sal_uLong Find_Text(const i18nutil::SearchOptions2 &rSearchOpt, bool bSearchInNotes, SwDocPositions eStart, SwDocPositions eEnd, bool &bCancel, FindRanges eRng, bool bReplace=false)
Definition: crsrsh.cxx:3481
size_t GetSectionFormatPos(const SwSectionFormat &) const
Definition: edsect.cxx:136
Marks a position in the document model.
Definition: pam.hxx:35
const OUString & GetTOIAutoMarkURL() const
Definition: doc.hxx:951
SwPaM * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:195
SwDocShell * GetDocShell()
Definition: doc.hxx:1352
void InsertTableOf(const SwTOXBase &rTOX, const SfxItemSet *pSet=nullptr)
Insert content table. Renew if required.
Definition: edtox.cxx:122
static SwTOXBase * GetCurTOX(const SwPosition &rPos)
Get current table of contents.
Definition: doctxm.cxx:447
void UpdateTableOf(const SwTOXBase &rTOX, const SfxItemSet *pSet=nullptr)
update tables of content
Definition: edtox.cxx:150
sal_uIntPtr sal_uLong
std::vector< SwTOXMark * > SwTOXMarks
Definition: tox.hxx:46
void CollectTextMarks(SwTOXMarks &rMarks) const
Definition: tox.hxx:187
sal_Int64 n
virtual SwUndoId EndUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Closes undo block.
Definition: doc.hxx:188
void SetTOIAutoMarkURL(const OUString &rSet)
Definition: edtox.cxx:263
bool IsUpdateTOX() const
Definition: doc.hxx:949
void EndProgress(SwDocShell const *pDocShell)
Definition: mainwn.cxx:92
SwSectionFormat * GetFormat()
Definition: section.hxx:336
OUString const & GetTOIAutoMarkURL() const
AutoMark file.
Definition: edtox.cxx:258
void DoUndo(bool bOn=true)
Undo.
Definition: edws.cxx:200
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:144
virtual bool InsertPoolItem(const SwPaM &rRg, const SfxPoolItem &, const SetAttrMode nFlags=SetAttrMode::DEFAULT, SwRootFrame const *pLayout=nullptr, SwTextAttr **ppNewTextAttr=nullptr)=0
Insert an attribute.
void StartProgress(TranslateId pMessResId, tools::Long nStartValue, tools::Long nEndValue, SwDocShell *pDocShell)
Definition: mainwn.cxx:52
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:315
void EndAllAction()
Definition: edws.cxx:97
static bool IsTOXBaseReadonly(const SwTOXBase &rTOXBase)
Definition: edtox.cxx:92
Array of Undo-history.
Definition: docary.hxx:192
void UpdateSection(size_t const nSect, SwSectionData &, SfxItemSet const *const =nullptr)
Definition: edsect.cxx:156
rtl::Reference< SwDoc > mxDoc
The document; never 0.
Definition: viewsh.hxx:171
void DeleteTOXMark(SwTOXMark const *pMark)
Definition: edtox.cxx:76
SwSectionNode * GetSectionNode()
Definition: section.cxx:952
bool ReadLine(OString &rStr, sal_Int32 nMaxBytesToRead=0xFFFE)
SwTOIKeyType
Definition: toxe.hxx:30
const SwTOXBase * GetTOX(sal_uInt16 nPos) const
Definition: edtox.cxx:229
void SetTOIAutoMarkURL(const OUString &rSet)
Definition: doc.hxx:952
SwRect maVisArea
The modern version of VisArea.
Definition: viewsh.hxx:169
bool DeleteTOX(const SwTOXBase &rTOXBase, bool bDelNodes)
Delete table of contents.
Definition: doctxm.cxx:523
SwDoc * GetDoc() const
Definition: viewsh.hxx:281
const SwTOXBase * GetCurTOX() const
Get current listing before or at the Cursor.
Definition: edtox.cxx:191
void SetAlternativeText(const OUString &rAlt)
Definition: tox.hxx:594
TransliterationFlags
bool IsUpdateTOX() const
Definition: edtox.cxx:253
void UpdatePageNum()
Calculate PageNumber and insert after formatting.
Definition: doctxm.cxx:1586
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
void GetTOIKeys(SwTOIKeyType eTyp, std::vector< OUString > &rArr) const
Key for managing index.
Definition: edtox.cxx:210
virtual SwUndoId StartUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Opens undo block.
const SwTOXBase * GetDefaultTOXBase(TOXTypes eTyp, bool bCreate=false)
Definition: edtox.cxx:111
void SetPrimaryKey(const OUString &rStr)
Definition: tox.hxx:614
bool IsProtect() const
Definition: section.cxx:343
void Push()
store a copy of the current cursor on the cursor stack
Definition: crsrsh.cxx:2240
TransliterationFlags transliterateFlags
bool SetPosAtStartEnd(SwPosition &rPos) const
Definition: doctxm.cxx:753
void SetUpdateTOX(bool bFlag)
Definition: doc.hxx:948
All (only in non-body and selections).
sal_uInt16 GetTOXCount() const
Definition: edtox.cxx:215
size_t size() const
Definition: docary.hxx:88
void GetTOIKeys(SwTOIKeyType eTyp, std::vector< OUString > &rArr, SwRootFrame const &rLayout) const
Definition: doctxm.cxx:85
void SetTableUpdateInProgress(bool bValue)
Definition: rootfrm.hxx:282
ring_container GetRingContainer()
void InvalidateWindows(const SwRect &rRect)
Definition: viewsh.cxx:551
void SetAutoGenerated(bool bSet)
Definition: tox.hxx:156
void SetDefaultTOXBase(const SwTOXBase &rBase)
Definition: edtox.cxx:116
void SetTOXBaseReadonly(const SwTOXBase &rTOXBase, bool bReadonly)
Definition: edtox.cxx:99
const LanguageTag & getLocale()
SvStream * GetInStream()
void Update(const SfxItemSet *pAttr=nullptr, SwRootFrame const *pLayout=nullptr, const bool _bNewTOX=false)
Collect table of contents content.
Definition: doctxm.cxx:768
static sal_uInt16 GetCurTOXMark(const SwPosition &rPos, SwTOXMarks &)
Get current table of contents Mark.
Definition: doctxm.cxx:116
void SetUpdateTOX(bool bFlag)
After reading file update all content tables.
Definition: edtox.cxx:248
void SetSecondaryKey(const OUString &rStr)
Definition: tox.hxx:620
const SwTOXType * GetTOXType(TOXTypes eTyp, sal_uInt16 nId) const
Definition: edtox.cxx:203
virtual void CalcLayout() override
To enable set up of StartActions and EndActions.
Definition: edws.cxx:108
void GetCurTOXMarks(SwTOXMarks &rMarks) const
Get all marks at current SPoint.
Definition: edtox.cxx:87
bool good() const
void Insert(sal_Unicode, bool bOnlyCurrCursor=false)
Edit (all selected ranges).
Definition: editsh.cxx:63
void ApplyAutoMark()
Definition: edtox.cxx:268
SectionType GetType() const
Definition: section.hxx:170
bool Pop(PopMode)
delete cursor
Definition: crsrsh.cxx:2262
bool DoesUndo() const
Definition: edws.cxx:203
void SetDefaultTOXBase(const SwTOXBase &rBase)
Definition: doctxm.cxx:502
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
void SvStream & rStrm
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2074
SwSectionFormats & GetSections()
Definition: doc.hxx:1338
void SetMainEntry(bool bSet)
Definition: tox.hxx:159
bool DeleteTOX(const SwTOXBase &rTOXBase, bool bDelNodes)
Definition: edtox.cxx:196
const SwTOXBase * GetDefaultTOXBase(TOXTypes eTyp, bool bCreate)
Definition: doctxm.cxx:476