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>
25#include <svl/fstathelper.hxx>
26#include <osl/diagnose.h>
27#include <osl/thread.h>
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#include <iodetect.hxx>
46
47using namespace ::com::sun::star;
48using namespace ::com::sun::star::i18n;
49using namespace ::com::sun::star::lang;
50using namespace ::com::sun::star::util;
51
52// Add/delete listing markers to a document
53
55{
56 bool bInsAtPos = rMark.IsAlternativeText();
58 for(SwPaM& rPaM : GetCursor()->GetRingContainer())
59 {
60 auto [pStt, pEnd] = rPaM.StartEnd(); // SwPosition*
61 if( bInsAtPos )
62 {
63 SwPaM aTmp( *pStt );
65 }
66 else if( *pEnd != *pStt )
67 {
69 rPaM, rMark, SetAttrMode::DONTEXPAND );
70 }
71
72 }
74}
75
77{
78 CurrShell aCurr( this );
80
81 mxDoc->DeleteTOXMark( pMark );
82
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
99void 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
122void SwEditShell::InsertTableOf( const SwTOXBase& rTOX, const SfxItemSet* pSet )
123{
124 CurrShell aCurr( this );
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
150void 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 );
163
164 ::StartProgress( STR_STATSTR_TOX_UPDATE, 0, 0, pDocSh );
165
166 pMyDoc->GetIDocumentUndoRedo().StartUndo(SwUndoId::TOXCHANGE, nullptr);
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
184 pMyDoc->GetIDocumentUndoRedo().EndUndo(SwUndoId::TOXCHANGE, nullptr);
185
186 ::EndProgress( pDocSh );
187 EndAllAction();
188}
189
192{
193 return SwDoc::GetCurTOX( *GetCursor()->GetPoint() );
194}
195
196bool SwEditShell::DeleteTOX( const SwTOXBase& rTOXBase, bool bDelNodes )
197{
198 return GetDoc()->DeleteTOX( rTOXBase, bDelNodes );
199}
200
201// manage types of listings
202
203const 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
210void SwEditShell::GetTOIKeys( SwTOIKeyType eTyp, std::vector<OUString>& rArr ) const
211{
212 GetDoc()->GetTOIKeys( eTyp, rArr, *GetLayout() );
213}
214
215sal_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
229const 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
249{
250 GetDoc()->SetUpdateTOX( bFlag );
251}
252
254{
255 return GetDoc()->IsUpdateTOX();
256}
257
258OUString const & SwEditShell::GetTOIAutoMarkURL() const
259{
260 return GetDoc()->GetTOIAutoMarkURL();
261}
262
263void SwEditShell::SetTOIAutoMarkURL(const OUString& rSet)
264{
266}
267
269{
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 // tdf#106899 - import tox concordance file using the appropriate character set
299 rtl_TextEncoding eChrSet = SwIoSystem::GetTextEncoding(rStrm);
300 if (eChrSet == RTL_TEXTENCODING_DONTKNOW)
301 eChrSet = ::osl_getThreadTextEncoding();
302
303 // SearchOptions to be used in loop below
304 sal_Int32 const nLEV_Other = 2; // -> changedChars;
305 sal_Int32 const nLEV_Longer = 3;
306 sal_Int32 const nLEV_Shorter = 1;
307
308 i18nutil::SearchOptions2 aSearchOpt(
309 SearchFlags::LEV_RELAXED,
310 "", "",
311 SvtSysLocale().GetLanguageTag().getLocale(),
312 nLEV_Other, nLEV_Longer, nLEV_Shorter,
313 TransliterationFlags::NONE,
314 SearchAlgorithms2::ABSOLUTE,
315 '\\' );
316
317 OStringBuffer aRdLine;
318 while (rStrm.good())
319 {
320 rStrm.ReadLine( aRdLine );
321
322 // # -> comment
323 // ; -> delimiter between entries ->
324 // Format: TextToSearchFor;AlternativeString;PrimaryKey;SecondaryKey;CaseSensitive;WordOnly
325 // Leading and trailing blanks are ignored
326 if( !aRdLine.isEmpty() && '#' != aRdLine[0] )
327 {
328 OUString sLine(OStringToOUString(aRdLine, eChrSet));
329
330 sal_Int32 nTokenPos = 0;
331 OUString sToSelect( sLine.getToken(0, ';', nTokenPos ) );
332 if( !sToSelect.isEmpty() )
333 {
334 OUString sAlternative = sLine.getToken(0, ';', nTokenPos);
335 OUString sPrimary = sLine.getToken(0, ';', nTokenPos);
336 OUString sSecondary = sLine.getToken(0, ';', nTokenPos);
337 OUString sCase = sLine.getToken(0, ';', nTokenPos);
338 OUString sWordOnly = sLine.getToken(0, ';', nTokenPos);
339
340 //3.
341 bool bCaseSensitive = !sCase.isEmpty() && sCase != "0";
342 bool bWordOnly = !sWordOnly.isEmpty() && sWordOnly != "0";
343
344 if (!bCaseSensitive)
345 {
346 aSearchOpt.transliterateFlags |=
347 TransliterationFlags::IGNORE_CASE;
348 }
349 else
350 {
351 aSearchOpt.transliterateFlags &=
352 ~TransliterationFlags::IGNORE_CASE;
353 }
354 if ( bWordOnly)
355 aSearchOpt.searchFlag |= SearchFlags::NORM_WORD_ONLY;
356 else
357 aSearchOpt.searchFlag &= ~SearchFlags::NORM_WORD_ONLY;
358
359 aSearchOpt.searchString = sToSelect;
360
361 KillPams();
362 bool bCancel;
363
364 // todo/mba: assuming that notes shouldn't be searched
365 sal_Int32 nRet = Find_Text(aSearchOpt, false/*bSearchInNotes*/, SwDocPositions::Start, SwDocPositions::End, bCancel,
367
368 if(nRet)
369 {
370 SwTOXMark* pTmpMark = new SwTOXMark(pTOXType);
371 if( !sPrimary.isEmpty() )
372 {
373 pTmpMark->SetPrimaryKey( sPrimary );
374 if( !sSecondary.isEmpty() )
375 pTmpMark->SetSecondaryKey( sSecondary );
376 }
377 if( !sAlternative.isEmpty() )
378 pTmpMark->SetAlternativeText(sAlternative);
379 pTmpMark->SetMainEntry(false);
380 pTmpMark->SetAutoGenerated(true);
381 //4.
382 SwEditShell::Insert(*pTmpMark);
383 }
384 }
385 }
386 }
387 KillPams();
389 }
390 DoUndo(bDoesUndo);
391 EndAllAction();
392}
393
394/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual bool InsertPoolItem(const SwPaM &rRg, const SfxPoolItem &, const SetAttrMode nFlags=SetAttrMode::DEFAULT, SwRootFrame const *pLayout=nullptr, SwTextAttr **ppNewTextAttr=nullptr)=0
Insert an attribute.
SvStream * GetInStream()
bool good() const
bool ReadLine(OStringBuffer &rStr, sal_Int32 nMaxBytesToRead=0xFFFE)
bool Pop(PopMode, ::std::optional< SwCallLink > &roLink)
void Push()
store a copy of the current cursor on the cursor stack
Definition: crsrsh.cxx:2550
SwCursor * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:194
sal_Int32 Find_Text(const i18nutil::SearchOptions2 &rSearchOpt, bool bSearchInNotes, SwDocPositions eStart, SwDocPositions eEnd, bool &bCancel, FindRanges eRng, bool bReplace=false)
Definition: crsrsh.cxx:3811
void KillPams()
Definition: crsrsh.cxx:1308
Definition: doc.hxx:197
void SetDefaultTOXBase(const SwTOXBase &rBase)
Definition: doctxm.cxx:504
SwSectionFormats & GetSections()
Definition: doc.hxx:1356
bool DeleteTOX(const SwTOXBase &rTOXBase, bool bDelNodes)
Delete table of contents.
Definition: doctxm.cxx:525
static sal_uInt16 GetCurTOXMark(const SwPosition &rPos, SwTOXMarks &)
Get current table of contents Mark.
Definition: doctxm.cxx:117
void SetUpdateTOX(bool bFlag)
Definition: doc.hxx:963
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:329
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:158
void GetTOIKeys(SwTOIKeyType eTyp, std::vector< OUString > &rArr, SwRootFrame const &rLayout) const
Definition: doctxm.cxx:86
static SwTOXBase * GetCurTOX(const SwPosition &rPos)
Get current table of contents.
Definition: doctxm.cxx:449
void SetTOIAutoMarkURL(const OUString &rSet)
Definition: doc.hxx:967
const OUString & GetTOIAutoMarkURL() const
Definition: doc.hxx:966
const SwTOXBase * GetDefaultTOXBase(TOXTypes eTyp, bool bCreate)
Definition: doctxm.cxx:478
bool IsUpdateTOX() const
Definition: doc.hxx:964
SwDocShell * GetDocShell()
Definition: doc.hxx:1370
virtual void CalcLayout() override
To enable set up of StartActions and EndActions.
Definition: edws.cxx:108
const SwTOXBase * GetCurTOX() const
Get current listing before or at the Cursor.
Definition: edtox.cxx:191
void StartAllAction()
For all views of this document.
Definition: edws.cxx:86
void SetUpdateTOX(bool bFlag)
After reading file update all content tables.
Definition: edtox.cxx:248
OUString const & GetTOIAutoMarkURL() const
AutoMark file.
Definition: edtox.cxx:258
sal_uInt16 GetTOXCount() const
Definition: edtox.cxx:215
bool IsUpdateTOX() const
Definition: edtox.cxx:253
const SwTOXBase * GetDefaultTOXBase(TOXTypes eTyp, bool bCreate=false)
Definition: edtox.cxx:111
void Insert(sal_Unicode, bool bOnlyCurrCursor=false)
Edit (all selected ranges).
Definition: editsh.cxx:62
void InsertTableOf(const SwTOXBase &rTOX, const SfxItemSet *pSet=nullptr)
Insert content table. Renew if required.
Definition: edtox.cxx:122
void SetDefaultTOXBase(const SwTOXBase &rBase)
Definition: edtox.cxx:116
const SwTOXBase * GetTOX(sal_uInt16 nPos) const
Definition: edtox.cxx:229
void UpdateSection(size_t const nSect, SwSectionData &, SfxItemSet const *const =nullptr)
Definition: edsect.cxx:156
void UpdateTableOf(const SwTOXBase &rTOX, const SfxItemSet *pSet=nullptr)
update tables of content
Definition: edtox.cxx:150
static bool IsTOXBaseReadonly(const SwTOXBase &rTOXBase)
Definition: edtox.cxx:92
void GetTOIKeys(SwTOIKeyType eTyp, std::vector< OUString > &rArr) const
Key for managing index.
Definition: edtox.cxx:210
bool DoesUndo() const
Definition: edws.cxx:203
void ApplyAutoMark()
Definition: edtox.cxx:268
size_t GetSectionFormatPos(const SwSectionFormat &) const
Definition: edsect.cxx:136
void DoUndo(bool bOn=true)
Undo.
Definition: edws.cxx:200
void DeleteTOXMark(SwTOXMark const *pMark)
Definition: edtox.cxx:76
bool DeleteTOX(const SwTOXBase &rTOXBase, bool bDelNodes)
Definition: edtox.cxx:196
void SetTOXBaseReadonly(const SwTOXBase &rTOXBase, bool bReadonly)
Definition: edtox.cxx:99
const SwTOXType * GetTOXType(TOXTypes eTyp, sal_uInt16 nId) const
Definition: edtox.cxx:203
void EndAllAction()
Definition: edws.cxx:97
void SetTOIAutoMarkURL(const OUString &rSet)
Definition: edtox.cxx:263
void GetCurTOXMarks(SwTOXMarks &rMarks) const
Get all marks at current SPoint.
Definition: edtox.cxx:87
static SW_DLLPUBLIC rtl_TextEncoding GetTextEncoding(SvStream &)
Definition: iodetect.cxx:241
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:188
const SwPosition * GetPoint() const
Definition: pam.hxx:253
void SetTableUpdateInProgress(bool bValue)
Definition: rootfrm.hxx:290
SAL_DLLPRIVATE void SetProtectFlag(bool const bFlag)
Definition: section.hxx:106
SwSectionNode * GetSectionNode()
Definition: section.cxx:923
Array of Undo-history.
Definition: docary.hxx:193
SwSectionFormat * GetFormat()
Definition: section.hxx:341
bool IsProtect() const
Definition: section.cxx:334
SectionType GetType() const
Definition: section.hxx:173
void UpdatePageNum()
Calculate PageNumber and insert after formatting.
Definition: doctxm.cxx:1668
void Update(const SfxItemSet *pAttr=nullptr, SwRootFrame const *pLayout=nullptr, const bool _bNewTOX=false)
Collect table of contents content.
Definition: doctxm.cxx:853
bool SetPosAtStartEnd(SwPosition &rPos) const
Definition: doctxm.cxx:839
void SetAlternativeText(const OUString &rAlt)
Definition: tox.hxx:582
void SetSecondaryKey(const OUString &rStr)
Definition: tox.hxx:604
bool IsAlternativeText() const
Definition: tox.hxx:579
void SetAutoGenerated(bool bSet)
Definition: tox.hxx:153
void SetMainEntry(bool bSet)
Definition: tox.hxx:156
void SetPrimaryKey(const OUString &rStr)
Definition: tox.hxx:598
void CollectTextMarks(SwTOXMarks &rMarks) const
Definition: tox.hxx:184
size_t size() const
Definition: docary.hxx:88
rtl::Reference< SwDoc > mxDoc
The document; never 0.
Definition: viewsh.hxx:199
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2163
SwRect maVisArea
The modern version of VisArea.
Definition: viewsh.hxx:197
void InvalidateWindows(const SwRect &rRect)
Definition: viewsh.cxx:568
SwDoc * GetDoc() const
Definition: viewsh.hxx:308
ring_container GetRingContainer()
Definition: ring.hxx:240
@ InSelAll
All (only in non-body and selections).
sal_Int64 n
sal_uInt16 nPos
void StartProgress(TranslateId pMessResId, tools::Long nStartValue, tools::Long nEndValue, SwDocShell *pDocShell)
Definition: mainwn.cxx:52
void EndProgress(SwDocShell const *pDocShell)
Definition: mainwn.cxx:92
SVL_DLLPUBLIC bool IsDocument(const OUString &rURL)
const LanguageTag & getLocale()
void SvStream & rStrm
sal_Int16 nId
static SfxItemSet & rSet
TransliterationFlags transliterateFlags
std::vector< SwTOXMark * > SwTOXMarks
Definition: tox.hxx:46
SwTOIKeyType
Definition: toxe.hxx:31
TOXTypes
Definition: toxe.hxx:40
@ TOX_INDEX
Definition: toxe.hxx:41
TransliterationFlags