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
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
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 SearchAlgorithms_ABSOLUTE,
310 SearchFlags::LEV_RELAXED,
311 "", "",
312 SvtSysLocale().GetLanguageTag().getLocale(),
313 nLEV_Other, nLEV_Longer, nLEV_Shorter,
314 TransliterationFlags::NONE,
315 SearchAlgorithms2::ABSOLUTE,
316 '\\' );
317
318 OStringBuffer aRdLine;
319 while (rStrm.good())
320 {
321 rStrm.ReadLine( aRdLine );
322
323 // # -> comment
324 // ; -> delimiter between entries ->
325 // Format: TextToSearchFor;AlternativeString;PrimaryKey;SecondaryKey;CaseSensitive;WordOnly
326 // Leading and trailing blanks are ignored
327 if( !aRdLine.isEmpty() && '#' != aRdLine[0] )
328 {
329 OUString sLine(OStringToOUString(aRdLine, eChrSet));
330
331 sal_Int32 nTokenPos = 0;
332 OUString sToSelect( sLine.getToken(0, ';', nTokenPos ) );
333 if( !sToSelect.isEmpty() )
334 {
335 OUString sAlternative = sLine.getToken(0, ';', nTokenPos);
336 OUString sPrimary = sLine.getToken(0, ';', nTokenPos);
337 OUString sSecondary = sLine.getToken(0, ';', nTokenPos);
338 OUString sCase = sLine.getToken(0, ';', nTokenPos);
339 OUString sWordOnly = sLine.getToken(0, ';', nTokenPos);
340
341 //3.
342 bool bCaseSensitive = !sCase.isEmpty() && sCase != "0";
343 bool bWordOnly = !sWordOnly.isEmpty() && sWordOnly != "0";
344
345 if (!bCaseSensitive)
346 {
347 aSearchOpt.transliterateFlags |=
348 TransliterationFlags::IGNORE_CASE;
349 }
350 else
351 {
352 aSearchOpt.transliterateFlags &=
353 ~TransliterationFlags::IGNORE_CASE;
354 }
355 if ( bWordOnly)
356 aSearchOpt.searchFlag |= SearchFlags::NORM_WORD_ONLY;
357 else
358 aSearchOpt.searchFlag &= ~SearchFlags::NORM_WORD_ONLY;
359
360 aSearchOpt.searchString = sToSelect;
361
362 KillPams();
363 bool bCancel;
364
365 // todo/mba: assuming that notes shouldn't be searched
366 sal_Int32 nRet = Find_Text(aSearchOpt, false/*bSearchInNotes*/, SwDocPositions::Start, SwDocPositions::End, bCancel,
368
369 if(nRet)
370 {
371 SwTOXMark* pTmpMark = new SwTOXMark(pTOXType);
372 if( !sPrimary.isEmpty() )
373 {
374 pTmpMark->SetPrimaryKey( sPrimary );
375 if( !sSecondary.isEmpty() )
376 pTmpMark->SetSecondaryKey( sSecondary );
377 }
378 if( !sAlternative.isEmpty() )
379 pTmpMark->SetAlternativeText(sAlternative);
380 pTmpMark->SetMainEntry(false);
381 pTmpMark->SetAutoGenerated(true);
382 //4.
383 SwEditShell::Insert(*pTmpMark);
384 }
385 }
386 }
387 }
388 KillPams();
390 }
391 DoUndo(bDoesUndo);
392 EndAllAction();
393}
394
395/* 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.
virtual SwUndoId EndUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Closes undo block.
virtual SwUndoId StartUndo(SwUndoId const eUndoId, SwRewriter const *const pRewriter)=0
Opens undo block.
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:2246
SwCursor * GetCursor(bool bMakeTableCursor=true) const
Return pointer to the current shell cursor.
Definition: crsrsh.cxx:193
sal_Int32 Find_Text(const i18nutil::SearchOptions2 &rSearchOpt, bool bSearchInNotes, SwDocPositions eStart, SwDocPositions eEnd, bool &bCancel, FindRanges eRng, bool bReplace=false)
Definition: crsrsh.cxx:3488
void KillPams()
Definition: crsrsh.cxx:1021
Definition: doc.hxx:192
void SetDefaultTOXBase(const SwTOXBase &rBase)
Definition: doctxm.cxx:503
SwSectionFormats & GetSections()
Definition: doc.hxx:1341
bool DeleteTOX(const SwTOXBase &rTOXBase, bool bDelNodes)
Delete table of contents.
Definition: doctxm.cxx:524
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:952
IDocumentContentOperations const & getIDocumentContentOperations() const
Definition: doc.cxx:316
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:145
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:448
void SetTOIAutoMarkURL(const OUString &rSet)
Definition: doc.hxx:956
const OUString & GetTOIAutoMarkURL() const
Definition: doc.hxx:955
const SwTOXBase * GetDefaultTOXBase(TOXTypes eTyp, bool bCreate)
Definition: doctxm.cxx:477
bool IsUpdateTOX() const
Definition: doc.hxx:953
SwDocShell * GetDocShell()
Definition: doc.hxx:1355
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:187
const SwPosition * GetPoint() const
Definition: pam.hxx:261
void SetTableUpdateInProgress(bool bValue)
Definition: rootfrm.hxx:281
SAL_DLLPRIVATE void SetProtectFlag(bool const bFlag)
Definition: section.hxx:106
SwSectionNode * GetSectionNode()
Definition: section.cxx:928
Array of Undo-history.
Definition: docary.hxx:192
SwSectionFormat * GetFormat()
Definition: section.hxx:337
bool IsProtect() const
Definition: section.cxx:326
SectionType GetType() const
Definition: section.hxx:171
void UpdatePageNum()
Calculate PageNumber and insert after formatting.
Definition: doctxm.cxx:1665
void Update(const SfxItemSet *pAttr=nullptr, SwRootFrame const *pLayout=nullptr, const bool _bNewTOX=false)
Collect table of contents content.
Definition: doctxm.cxx:850
bool SetPosAtStartEnd(SwPosition &rPos) const
Definition: doctxm.cxx:836
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:87
rtl::Reference< SwDoc > mxDoc
The document; never 0.
Definition: viewsh.hxx:179
SwRootFrame * GetLayout() const
Definition: viewsh.cxx:2172
SwRect maVisArea
The modern version of VisArea.
Definition: viewsh.hxx:177
void InvalidateWindows(const SwRect &rRect)
Definition: viewsh.cxx:556
SwDoc * GetDoc() const
Definition: viewsh.hxx:290
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