Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / index / toxmgr.cxx
blob5fc78fc18053da78dea348bd6b80e01fb1074bb6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <wrtsh.hxx>
21 #include <swwait.hxx>
22 #include <view.hxx>
23 #include <toxmgr.hxx>
24 #include <doc.hxx>
25 #include <IDocumentUndoRedo.hxx>
26 #include <swundo.hxx>
27 #include <osl/diagnose.h>
29 // handle indexes with TOXMgr
30 SwTOXMgr::SwTOXMgr(SwWrtShell* pShell):
31 m_pSh(pShell)
33 m_pSh->GetCurTOXMarks(m_aCurMarks);
34 SetCurTOXMark(0);
37 SwTOXMark* SwTOXMgr::GetTOXMark(sal_uInt16 nId)
39 if(!m_aCurMarks.empty())
40 return m_aCurMarks[nId];
41 return nullptr;
44 void SwTOXMgr::DeleteTOXMark()
46 SwTOXMark* pNext = nullptr;
47 if( m_pCurTOXMark )
49 pNext = const_cast<SwTOXMark*>(&m_pSh->GotoTOXMark( *m_pCurTOXMark, TOX_NXT ));
50 if( pNext == m_pCurTOXMark )
51 pNext = nullptr;
53 m_pSh->DeleteTOXMark( m_pCurTOXMark );
54 m_pSh->SetModified();
56 // go to next one
57 m_pCurTOXMark = pNext;
60 void SwTOXMgr::InsertTOXMark(const SwTOXMarkDescription& rDesc)
62 SwTOXMark* pMark = nullptr;
63 switch(rDesc.GetTOXType())
65 case TOX_CONTENT:
67 OSL_ENSURE(rDesc.GetLevel() > 0 && rDesc.GetLevel() <= MAXLEVEL,
68 "invalid InsertTOCMark level");
69 pMark = new SwTOXMark(m_pSh->GetTOXType(TOX_CONTENT, 0));
70 pMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
72 if(rDesc.GetAltStr())
73 pMark->SetAlternativeText(*rDesc.GetAltStr());
75 break;
76 case TOX_INDEX:
78 pMark = new SwTOXMark(m_pSh->GetTOXType(TOX_INDEX, 0));
80 if( rDesc.GetPrimKey() && !rDesc.GetPrimKey()->isEmpty() )
82 pMark->SetPrimaryKey( *rDesc.GetPrimKey() );
83 if(rDesc.GetPhoneticReadingOfPrimKey())
84 pMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
86 if( rDesc.GetSecKey() && !rDesc.GetSecKey()->isEmpty() )
88 pMark->SetSecondaryKey( *rDesc.GetSecKey() );
89 if(rDesc.GetPhoneticReadingOfSecKey())
90 pMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
93 if(rDesc.GetAltStr())
94 pMark->SetAlternativeText(*rDesc.GetAltStr());
95 if(rDesc.GetPhoneticReadingOfAltStr())
96 pMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
97 pMark->SetMainEntry(rDesc.IsMainEntry());
99 break;
100 case TOX_USER:
102 OSL_ENSURE(rDesc.GetLevel() > 0 && rDesc.GetLevel() <= MAXLEVEL,
103 "invalid InsertTOCMark level");
104 sal_uInt16 nId = rDesc.GetTOUName() ?
105 GetUserTypeID(*rDesc.GetTOUName()) : 0;
106 pMark = new SwTOXMark(m_pSh->GetTOXType(TOX_USER, nId));
107 pMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
109 if(rDesc.GetAltStr())
110 pMark->SetAlternativeText(*rDesc.GetAltStr());
112 break;
113 case TOX_BIBLIOGRAPHY:
115 pMark = new SwTOXMark(m_pSh->GetTOXType(TOX_BIBLIOGRAPHY, 0));
117 if( rDesc.GetPrimKey() && !rDesc.GetPrimKey()->isEmpty() )
119 pMark->SetPrimaryKey( *rDesc.GetPrimKey() );
120 if(rDesc.GetPhoneticReadingOfPrimKey())
121 pMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
123 if( rDesc.GetSecKey() && !rDesc.GetSecKey()->isEmpty() )
125 pMark->SetSecondaryKey( *rDesc.GetSecKey() );
126 if(rDesc.GetPhoneticReadingOfSecKey())
127 pMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
130 if(rDesc.GetAltStr())
131 pMark->SetAlternativeText(*rDesc.GetAltStr());
132 if(rDesc.GetPhoneticReadingOfAltStr())
133 pMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
134 pMark->SetMainEntry(rDesc.IsMainEntry());
136 break;
137 default:; //prevent warning
140 if (!pMark)
141 return;
143 m_pSh->StartAllAction();
144 m_pSh->SwEditShell::Insert(*pMark);
145 m_pSh->EndAllAction();
148 // Update of TOXMarks
149 void SwTOXMgr::UpdateTOXMark(const SwTOXMarkDescription& rDesc)
151 assert(m_pCurTOXMark && "no current TOXMark");
152 m_pSh->StartAllAction();
153 if(m_pCurTOXMark->GetTOXType()->GetType() == TOX_INDEX)
155 if(rDesc.GetPrimKey() && !rDesc.GetPrimKey()->isEmpty() )
157 m_pCurTOXMark->SetPrimaryKey( *rDesc.GetPrimKey() );
158 if(rDesc.GetPhoneticReadingOfPrimKey())
159 m_pCurTOXMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
160 else
161 m_pCurTOXMark->SetPrimaryKeyReading(OUString());
163 if( rDesc.GetSecKey() && !rDesc.GetSecKey()->isEmpty() )
165 m_pCurTOXMark->SetSecondaryKey( *rDesc.GetSecKey() );
166 if(rDesc.GetPhoneticReadingOfSecKey())
167 m_pCurTOXMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
168 else
169 m_pCurTOXMark->SetSecondaryKeyReading(OUString());
171 else
173 m_pCurTOXMark->SetSecondaryKey(OUString());
174 m_pCurTOXMark->SetSecondaryKeyReading(OUString());
177 else
179 m_pCurTOXMark->SetPrimaryKey(OUString());
180 m_pCurTOXMark->SetPrimaryKeyReading(OUString());
181 m_pCurTOXMark->SetSecondaryKey(OUString());
182 m_pCurTOXMark->SetSecondaryKeyReading(OUString());
184 if(rDesc.GetPhoneticReadingOfAltStr())
185 m_pCurTOXMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
186 else
187 m_pCurTOXMark->SetTextReading(OUString());
188 m_pCurTOXMark->SetMainEntry(rDesc.IsMainEntry());
190 else
191 m_pCurTOXMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
193 if(rDesc.GetAltStr())
195 // JP 26.08.96: Bug 30344 - either the text of a Doc or an alternative test,
196 // not both!
197 bool bReplace = m_pCurTOXMark->IsAlternativeText();
198 if( bReplace )
199 m_pCurTOXMark->SetAlternativeText( *rDesc.GetAltStr() );
200 else
202 SwTOXMark aCpy( *m_pCurTOXMark );
203 m_aCurMarks.clear();
204 m_pSh->DeleteTOXMark(m_pCurTOXMark);
205 aCpy.SetAlternativeText( *rDesc.GetAltStr() );
206 m_pSh->SwEditShell::Insert( aCpy );
207 m_pCurTOXMark = nullptr;
210 m_pSh->SetModified();
211 m_pSh->EndAllAction();
212 // Bug 36207 pCurTOXMark points nowhere here!
213 if(!m_pCurTOXMark)
215 m_pSh->Left(SwCursorSkipMode::Chars, false, 1, false );
216 m_pSh->GetCurTOXMarks(m_aCurMarks);
217 SetCurTOXMark(0);
221 // determine UserTypeID
222 sal_uInt16 SwTOXMgr::GetUserTypeID(const OUString& rStr)
224 sal_uInt16 nSize = m_pSh->GetTOXTypeCount(TOX_USER);
225 for(sal_uInt16 i=0; i < nSize; ++i)
227 const SwTOXType* pTmp = m_pSh->GetTOXType(TOX_USER, i);
228 if(pTmp && pTmp->GetTypeName() == rStr)
229 return i;
231 SwTOXType aUserType(*m_pSh->GetDoc(), TOX_USER, rStr);
232 m_pSh->InsertTOXType(aUserType);
233 return nSize;
236 // traveling between TOXMarks
237 void SwTOXMgr::NextTOXMark(bool bSame)
239 OSL_ENSURE(m_pCurTOXMark, "no current TOXMark");
240 if( m_pCurTOXMark )
242 SwTOXSearch eDir = bSame ? TOX_SAME_NXT : TOX_NXT;
243 m_pCurTOXMark = const_cast<SwTOXMark*>(&m_pSh->GotoTOXMark( *m_pCurTOXMark, eDir ));
247 void SwTOXMgr::PrevTOXMark(bool bSame)
249 OSL_ENSURE(m_pCurTOXMark, "no current TOXMark");
250 if( m_pCurTOXMark )
252 SwTOXSearch eDir = bSame ? TOX_SAME_PRV : TOX_PRV;
253 m_pCurTOXMark = const_cast<SwTOXMark*>(&m_pSh->GotoTOXMark(*m_pCurTOXMark, eDir ));
257 const SwTOXType* SwTOXMgr::GetTOXType(TOXTypes eTyp) const
259 return m_pSh->GetTOXType(eTyp, 0);
262 void SwTOXMgr::SetCurTOXMark(sal_uInt16 nId)
264 m_pCurTOXMark = (nId < m_aCurMarks.size()) ? m_aCurMarks[nId] : nullptr;
267 bool SwTOXMgr::UpdateOrInsertTOX(const SwTOXDescription& rDesc,
268 SwTOXBase** ppBase,
269 const SfxItemSet* pSet)
271 SwWait aWait( *m_pSh->GetView().GetDocShell(), true );
272 bool bRet = true;
273 const SwTOXBase *const pCurTOX = ppBase && *ppBase ? *ppBase : m_pSh->GetCurTOX();
275 SwTOXBase * pNewTOX = pCurTOX ? new SwTOXBase(*pCurTOX) : nullptr;
277 TOXTypes eCurTOXType = rDesc.GetTOXType();
278 if(pCurTOX && !ppBase && m_pSh->HasSelection())
279 m_pSh->EnterStdMode();
281 switch(eCurTOXType)
283 case TOX_INDEX :
285 if(!pCurTOX || (ppBase && !(*ppBase)))
287 const SwTOXType* pType = m_pSh->GetTOXType(eCurTOXType, 0);
288 SwForm aForm(eCurTOXType);
289 pNewTOX = new SwTOXBase(pType, aForm, SwTOXElement::Mark, pType->GetTypeName());
291 pNewTOX->SetOptions(rDesc.GetIndexOptions());
292 pNewTOX->SetMainEntryCharStyle(rDesc.GetMainEntryCharStyle());
293 m_pSh->SetTOIAutoMarkURL(rDesc.GetAutoMarkURL());
294 m_pSh->ApplyAutoMark();
296 break;
297 case TOX_CONTENT :
299 if(!pCurTOX || (ppBase && !(*ppBase)))
301 const SwTOXType* pType = m_pSh->GetTOXType(eCurTOXType, 0);
302 SwForm aForm(eCurTOXType);
303 pNewTOX = new SwTOXBase(pType, aForm, rDesc.GetContentOptions(), pType->GetTypeName());
305 pNewTOX->SetCreate(rDesc.GetContentOptions());
306 pNewTOX->SetLevel(rDesc.GetLevel());
308 break;
309 case TOX_USER :
311 if(!pCurTOX || (ppBase && !(*ppBase)))
313 sal_uInt16 nPos = 0;
314 sal_uInt16 nSize = m_pSh->GetTOXTypeCount(eCurTOXType);
315 for(sal_uInt16 i=0; rDesc.GetTOUName() && i < nSize; ++i)
316 { const SwTOXType* pType = m_pSh->GetTOXType(TOX_USER, i);
317 if(pType->GetTypeName() == *rDesc.GetTOUName())
318 { nPos = i;
319 break;
322 const SwTOXType* pType = m_pSh->GetTOXType(eCurTOXType, nPos);
324 SwForm aForm(eCurTOXType);
325 pNewTOX = new SwTOXBase(pType, aForm, rDesc.GetContentOptions(), pType->GetTypeName());
328 else
330 pNewTOX->SetCreate(rDesc.GetContentOptions());
332 pNewTOX->SetLevelFromChapter(rDesc.IsLevelFromChapter());
334 break;
335 case TOX_CITATION: /** TODO */break;
336 case TOX_OBJECTS:
337 case TOX_TABLES:
338 case TOX_AUTHORITIES:
339 case TOX_BIBLIOGRAPHY:
340 case TOX_ILLUSTRATIONS:
342 //Special handling for TOX_AUTHORITY
343 if(TOX_AUTHORITIES == eCurTOXType)
345 SwAuthorityFieldType* pFType = static_cast<SwAuthorityFieldType*>(
346 m_pSh->GetFieldType(SwFieldIds::TableOfAuthorities, OUString()));
347 if (!pFType)
349 SwAuthorityFieldType const type(m_pSh->GetDoc());
350 pFType = static_cast<SwAuthorityFieldType*>(
351 m_pSh->InsertFieldType(type));
353 OUString const& rBrackets(rDesc.GetAuthBrackets());
354 if (rBrackets.isEmpty())
356 pFType->SetPreSuffix('\0', '\0');
358 else
360 assert(rBrackets.getLength() == 2);
361 pFType->SetPreSuffix(rBrackets[0], rBrackets[1]);
363 pFType->SetSequence(rDesc.IsAuthSequence());
364 SwTOXSortKey rArr[3];
365 rArr[0] = rDesc.GetSortKey1();
366 rArr[1] = rDesc.GetSortKey2();
367 rArr[2] = rDesc.GetSortKey3();
368 pFType->SetSortKeys(3, rArr);
369 pFType->SetSortByDocument(rDesc.IsSortByDocument());
370 pFType->SetLanguage(rDesc.GetLanguage());
371 pFType->SetSortAlgorithm(rDesc.GetSortAlgorithm());
373 pFType->UpdateFields();
375 // TODO: consider properties of the current TOXType
376 if(!pCurTOX || (ppBase && !(*ppBase)))
378 const SwTOXType* pType = m_pSh->GetTOXType(eCurTOXType, 0);
379 SwForm aForm(eCurTOXType);
380 pNewTOX = new SwTOXBase(
381 pType, aForm,
382 TOX_AUTHORITIES == eCurTOXType ? SwTOXElement::Mark : SwTOXElement::NONE,
383 pType->GetTypeName());
385 pNewTOX->SetFromObjectNames(rDesc.IsCreateFromObjectNames());
386 pNewTOX->SetOLEOptions(rDesc.GetOLEOptions());
387 if (eCurTOXType == TOX_ILLUSTRATIONS
388 || eCurTOXType == TOX_TABLES
389 || eCurTOXType == TOX_OBJECTS)
391 pNewTOX->SetCreate(rDesc.GetContentOptions());
394 break;
397 OSL_ENSURE(pNewTOX, "no TOXBase created!" );
398 if(!pNewTOX)
399 return false;
401 pNewTOX->SetFromChapter(rDesc.IsFromChapter());
402 pNewTOX->SetSequenceName(rDesc.GetSequenceName());
403 pNewTOX->SetCaptionDisplay(rDesc.GetCaptionDisplay());
404 pNewTOX->SetProtected(rDesc.IsReadonly());
406 for(sal_uInt16 nLevel = 0; nLevel < MAXLEVEL; nLevel++)
407 pNewTOX->SetStyleNames(rDesc.GetStyleNames(nLevel), nLevel);
409 if(rDesc.GetTitle())
410 pNewTOX->SetTitle(*rDesc.GetTitle());
411 if(rDesc.GetForm())
412 pNewTOX->SetTOXForm(*rDesc.GetForm());
413 pNewTOX->SetLanguage(rDesc.GetLanguage());
414 pNewTOX->SetSortAlgorithm(rDesc.GetSortAlgorithm());
416 if(!pCurTOX || (ppBase && !(*ppBase)) )
418 // when ppBase is passed over, TOXBase is only created here
419 // and then inserted in a global document by the dialog
420 if(ppBase)
421 (*ppBase) = pNewTOX;
422 else
424 m_pSh->InsertTableOf(*pNewTOX, pSet);
425 delete pNewTOX;
428 else
430 SwDoc * pDoc = m_pSh->GetDoc();
432 if (pDoc->GetIDocumentUndoRedo().DoesUndo())
434 pDoc->GetIDocumentUndoRedo().StartUndo(SwUndoId::TOXCHANGE, nullptr);
437 SwTOXBase *const pTOX = const_cast<SwTOXBase*>(pCurTOX);
438 pDoc->ChangeTOX(*pTOX, *pNewTOX);
440 pTOX->DisableKeepExpression();
441 m_pSh->UpdateTableOf(*pTOX, pSet);
442 bRet = false;
443 pTOX->EnableKeepExpression();
445 if (pDoc->GetIDocumentUndoRedo().DoesUndo())
447 pDoc->GetIDocumentUndoRedo().EndUndo(SwUndoId::TOXCHANGE, nullptr);
451 return bRet;
454 void SwTOXDescription::SetSortKeys(SwTOXSortKey eKey1,
455 SwTOXSortKey eKey2,
456 SwTOXSortKey eKey3)
458 SwTOXSortKey aArr[3];
459 sal_uInt16 nPos = 0;
460 if(AUTH_FIELD_END > eKey1.eField)
461 aArr[nPos++] = eKey1;
462 if(AUTH_FIELD_END > eKey2.eField)
463 aArr[nPos++] = eKey2;
464 if(AUTH_FIELD_END > eKey3.eField)
465 aArr[nPos++] = eKey3;
467 m_eSortKey1 = aArr[0];
468 m_eSortKey2 = aArr[1];
469 m_eSortKey3 = aArr[2];
472 void SwTOXDescription::ApplyTo(SwTOXBase& rTOXBase)
474 for(sal_uInt16 i = 0; i < MAXLEVEL; i++)
475 rTOXBase.SetStyleNames(GetStyleNames(i), i);
476 rTOXBase.SetTitle(GetTitle() ? *GetTitle() : OUString());
477 rTOXBase.SetCreate(GetContentOptions());
479 if(GetTOXType() == TOX_INDEX)
480 rTOXBase.SetOptions(GetIndexOptions());
481 if(GetTOXType() != TOX_INDEX)
482 rTOXBase.SetLevel(GetLevel());
483 rTOXBase.SetFromObjectNames(IsCreateFromObjectNames());
484 rTOXBase.SetSequenceName(GetSequenceName());
485 rTOXBase.SetCaptionDisplay(GetCaptionDisplay());
486 rTOXBase.SetFromChapter(IsFromChapter());
487 rTOXBase.SetProtected(IsReadonly());
488 rTOXBase.SetOLEOptions(GetOLEOptions());
489 rTOXBase.SetLevelFromChapter(IsLevelFromChapter());
490 rTOXBase.SetLanguage(m_eLanguage);
491 rTOXBase.SetSortAlgorithm(m_sSortAlgorithm);
495 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */