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