nss: upgrade to release 3.73
[LibreOffice.git] / sw / source / uibase / index / toxmgr.cxx
blobfe4885830ffa4db7689f919360130d975c43957a
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>
28 // handle indexes with TOXMgr
29 SwTOXMgr::SwTOXMgr(SwWrtShell* pShell):
30 pSh(pShell)
32 pSh->GetCurTOXMarks(aCurMarks);
33 SetCurTOXMark(0);
36 SwTOXMark* SwTOXMgr::GetTOXMark(sal_uInt16 nId)
38 if(!aCurMarks.empty())
39 return aCurMarks[nId];
40 return nullptr;
43 void SwTOXMgr::DeleteTOXMark()
45 SwTOXMark* pNext = nullptr;
46 if( pCurTOXMark )
48 pNext = const_cast<SwTOXMark*>(&pSh->GotoTOXMark( *pCurTOXMark, TOX_NXT ));
49 if( pNext == pCurTOXMark )
50 pNext = nullptr;
52 pSh->DeleteTOXMark( pCurTOXMark );
53 pSh->SetModified();
55 // go to next one
56 pCurTOXMark = pNext;
59 void SwTOXMgr::InsertTOXMark(const SwTOXMarkDescription& rDesc)
61 SwTOXMark* pMark = nullptr;
62 switch(rDesc.GetTOXType())
64 case TOX_CONTENT:
66 OSL_ENSURE(rDesc.GetLevel() > 0 && rDesc.GetLevel() <= MAXLEVEL,
67 "invalid InsertTOCMark level");
68 pMark = new SwTOXMark(pSh->GetTOXType(TOX_CONTENT, 0));
69 pMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
71 if(rDesc.GetAltStr())
72 pMark->SetAlternativeText(*rDesc.GetAltStr());
74 break;
75 case TOX_INDEX:
77 pMark = new SwTOXMark(pSh->GetTOXType(TOX_INDEX, 0));
79 if( rDesc.GetPrimKey() && !rDesc.GetPrimKey()->isEmpty() )
81 pMark->SetPrimaryKey( *rDesc.GetPrimKey() );
82 if(rDesc.GetPhoneticReadingOfPrimKey())
83 pMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
85 if( rDesc.GetSecKey() && !rDesc.GetSecKey()->isEmpty() )
87 pMark->SetSecondaryKey( *rDesc.GetSecKey() );
88 if(rDesc.GetPhoneticReadingOfSecKey())
89 pMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
92 if(rDesc.GetAltStr())
93 pMark->SetAlternativeText(*rDesc.GetAltStr());
94 if(rDesc.GetPhoneticReadingOfAltStr())
95 pMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
96 pMark->SetMainEntry(rDesc.IsMainEntry());
98 break;
99 case TOX_USER:
101 OSL_ENSURE(rDesc.GetLevel() > 0 && rDesc.GetLevel() <= MAXLEVEL,
102 "invalid InsertTOCMark level");
103 sal_uInt16 nId = rDesc.GetTOUName() ?
104 GetUserTypeID(*rDesc.GetTOUName()) : 0;
105 pMark = new SwTOXMark(pSh->GetTOXType(TOX_USER, nId));
106 pMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
108 if(rDesc.GetAltStr())
109 pMark->SetAlternativeText(*rDesc.GetAltStr());
111 break;
112 case TOX_BIBLIOGRAPHY:
114 pMark = new SwTOXMark(pSh->GetTOXType(TOX_BIBLIOGRAPHY, 0));
116 if( rDesc.GetPrimKey() && !rDesc.GetPrimKey()->isEmpty() )
118 pMark->SetPrimaryKey( *rDesc.GetPrimKey() );
119 if(rDesc.GetPhoneticReadingOfPrimKey())
120 pMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
122 if( rDesc.GetSecKey() && !rDesc.GetSecKey()->isEmpty() )
124 pMark->SetSecondaryKey( *rDesc.GetSecKey() );
125 if(rDesc.GetPhoneticReadingOfSecKey())
126 pMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
129 if(rDesc.GetAltStr())
130 pMark->SetAlternativeText(*rDesc.GetAltStr());
131 if(rDesc.GetPhoneticReadingOfAltStr())
132 pMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
133 pMark->SetMainEntry(rDesc.IsMainEntry());
135 break;
136 default:; //prevent warning
139 if (!pMark)
140 return;
142 pSh->StartAllAction();
143 pSh->SwEditShell::Insert(*pMark);
144 pSh->EndAllAction();
147 // Update of TOXMarks
148 void SwTOXMgr::UpdateTOXMark(const SwTOXMarkDescription& rDesc)
150 assert(pCurTOXMark && "no current TOXMark");
151 pSh->StartAllAction();
152 if(pCurTOXMark->GetTOXType()->GetType() == TOX_INDEX)
154 if(rDesc.GetPrimKey() && !rDesc.GetPrimKey()->isEmpty() )
156 pCurTOXMark->SetPrimaryKey( *rDesc.GetPrimKey() );
157 if(rDesc.GetPhoneticReadingOfPrimKey())
158 pCurTOXMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
159 else
160 pCurTOXMark->SetPrimaryKeyReading(OUString());
162 if( rDesc.GetSecKey() && !rDesc.GetSecKey()->isEmpty() )
164 pCurTOXMark->SetSecondaryKey( *rDesc.GetSecKey() );
165 if(rDesc.GetPhoneticReadingOfSecKey())
166 pCurTOXMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
167 else
168 pCurTOXMark->SetSecondaryKeyReading(OUString());
170 else
172 pCurTOXMark->SetSecondaryKey(OUString());
173 pCurTOXMark->SetSecondaryKeyReading(OUString());
176 else
178 pCurTOXMark->SetPrimaryKey(OUString());
179 pCurTOXMark->SetPrimaryKeyReading(OUString());
180 pCurTOXMark->SetSecondaryKey(OUString());
181 pCurTOXMark->SetSecondaryKeyReading(OUString());
183 if(rDesc.GetPhoneticReadingOfAltStr())
184 pCurTOXMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
185 else
186 pCurTOXMark->SetTextReading(OUString());
187 pCurTOXMark->SetMainEntry(rDesc.IsMainEntry());
189 else
190 pCurTOXMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
192 if(rDesc.GetAltStr())
194 // JP 26.08.96: Bug 30344 - either the text of a Doc or an alternative test,
195 // not both!
196 bool bReplace = pCurTOXMark->IsAlternativeText();
197 if( bReplace )
198 pCurTOXMark->SetAlternativeText( *rDesc.GetAltStr() );
199 else
201 SwTOXMark aCpy( *pCurTOXMark );
202 aCurMarks.clear();
203 pSh->DeleteTOXMark(pCurTOXMark);
204 aCpy.SetAlternativeText( *rDesc.GetAltStr() );
205 pSh->SwEditShell::Insert( aCpy );
206 pCurTOXMark = nullptr;
209 pSh->SetModified();
210 pSh->EndAllAction();
211 // Bug 36207 pCurTOXMark points nowhere here!
212 if(!pCurTOXMark)
214 pSh->Left(CRSR_SKIP_CHARS, false, 1, false );
215 pSh->GetCurTOXMarks(aCurMarks);
216 SetCurTOXMark(0);
220 // determine UserTypeID
221 sal_uInt16 SwTOXMgr::GetUserTypeID(const OUString& rStr)
223 sal_uInt16 nSize = pSh->GetTOXTypeCount(TOX_USER);
224 for(sal_uInt16 i=0; i < nSize; ++i)
226 const SwTOXType* pTmp = pSh->GetTOXType(TOX_USER, i);
227 if(pTmp && pTmp->GetTypeName() == rStr)
228 return i;
230 SwTOXType aUserType(*pSh->GetDoc(), TOX_USER, rStr);
231 pSh->InsertTOXType(aUserType);
232 return nSize;
235 // traveling between TOXMarks
236 void SwTOXMgr::NextTOXMark(bool bSame)
238 OSL_ENSURE(pCurTOXMark, "no current TOXMark");
239 if( pCurTOXMark )
241 SwTOXSearch eDir = bSame ? TOX_SAME_NXT : TOX_NXT;
242 pCurTOXMark = const_cast<SwTOXMark*>(&pSh->GotoTOXMark( *pCurTOXMark, eDir ));
246 void SwTOXMgr::PrevTOXMark(bool bSame)
248 OSL_ENSURE(pCurTOXMark, "no current TOXMark");
249 if( pCurTOXMark )
251 SwTOXSearch eDir = bSame ? TOX_SAME_PRV : TOX_PRV;
252 pCurTOXMark = const_cast<SwTOXMark*>(&pSh->GotoTOXMark(*pCurTOXMark, eDir ));
256 const SwTOXType* SwTOXMgr::GetTOXType(TOXTypes eTyp) const
258 return pSh->GetTOXType(eTyp, 0);
261 void SwTOXMgr::SetCurTOXMark(sal_uInt16 nId)
263 pCurTOXMark = (nId < aCurMarks.size()) ? aCurMarks[nId] : nullptr;
266 bool SwTOXMgr::UpdateOrInsertTOX(const SwTOXDescription& rDesc,
267 SwTOXBase** ppBase,
268 const SfxItemSet* pSet)
270 SwWait aWait( *pSh->GetView().GetDocShell(), true );
271 bool bRet = true;
272 const SwTOXBase *const pCurTOX = ppBase && *ppBase ? *ppBase : pSh->GetCurTOX();
274 SwTOXBase * pNewTOX = pCurTOX ? new SwTOXBase(*pCurTOX) : nullptr;
276 TOXTypes eCurTOXType = rDesc.GetTOXType();
277 if(pCurTOX && !ppBase && pSh->HasSelection())
278 pSh->EnterStdMode();
280 switch(eCurTOXType)
282 case TOX_INDEX :
284 if(!pCurTOX || (ppBase && !(*ppBase)))
286 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
287 SwForm aForm(eCurTOXType);
288 pNewTOX = new SwTOXBase(pType, aForm, SwTOXElement::Mark, pType->GetTypeName());
290 pNewTOX->SetOptions(rDesc.GetIndexOptions());
291 pNewTOX->SetMainEntryCharStyle(rDesc.GetMainEntryCharStyle());
292 pSh->SetTOIAutoMarkURL(rDesc.GetAutoMarkURL());
293 pSh->ApplyAutoMark();
295 break;
296 case TOX_CONTENT :
298 if(!pCurTOX || (ppBase && !(*ppBase)))
300 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
301 SwForm aForm(eCurTOXType);
302 pNewTOX = new SwTOXBase(pType, aForm, rDesc.GetContentOptions(), pType->GetTypeName());
304 pNewTOX->SetCreate(rDesc.GetContentOptions());
305 pNewTOX->SetLevel(rDesc.GetLevel());
307 break;
308 case TOX_USER :
310 if(!pCurTOX || (ppBase && !(*ppBase)))
312 sal_uInt16 nPos = 0;
313 sal_uInt16 nSize = pSh->GetTOXTypeCount(eCurTOXType);
314 for(sal_uInt16 i=0; rDesc.GetTOUName() && i < nSize; ++i)
315 { const SwTOXType* pType = pSh->GetTOXType(TOX_USER, i);
316 if(pType->GetTypeName() == *rDesc.GetTOUName())
317 { nPos = i;
318 break;
321 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, nPos);
323 SwForm aForm(eCurTOXType);
324 pNewTOX = new SwTOXBase(pType, aForm, rDesc.GetContentOptions(), pType->GetTypeName());
327 else
329 pNewTOX->SetCreate(rDesc.GetContentOptions());
331 pNewTOX->SetLevelFromChapter(rDesc.IsLevelFromChapter());
333 break;
334 case TOX_CITATION: /** TODO */break;
335 case TOX_OBJECTS:
336 case TOX_TABLES:
337 case TOX_AUTHORITIES:
338 case TOX_BIBLIOGRAPHY:
339 case TOX_ILLUSTRATIONS:
341 //Special handling for TOX_AUTHORITY
342 if(TOX_AUTHORITIES == eCurTOXType)
344 SwAuthorityFieldType* pFType = static_cast<SwAuthorityFieldType*>(
345 pSh->GetFieldType(SwFieldIds::TableOfAuthorities, OUString()));
346 if (!pFType)
348 SwAuthorityFieldType const type(pSh->GetDoc());
349 pFType = static_cast<SwAuthorityFieldType*>(
350 pSh->InsertFieldType(type));
352 OUString const& rBrackets(rDesc.GetAuthBrackets());
353 if (rBrackets.isEmpty())
355 pFType->SetPreSuffix('\0', '\0');
357 else
359 assert(rBrackets.getLength() == 2);
360 pFType->SetPreSuffix(rBrackets[0], rBrackets[1]);
362 pFType->SetSequence(rDesc.IsAuthSequence());
363 SwTOXSortKey rArr[3];
364 rArr[0] = rDesc.GetSortKey1();
365 rArr[1] = rDesc.GetSortKey2();
366 rArr[2] = rDesc.GetSortKey3();
367 pFType->SetSortKeys(3, rArr);
368 pFType->SetSortByDocument(rDesc.IsSortByDocument());
369 pFType->SetLanguage(rDesc.GetLanguage());
370 pFType->SetSortAlgorithm(rDesc.GetSortAlgorithm());
372 pFType->UpdateFields();
374 // TODO: consider properties of the current TOXType
375 if(!pCurTOX || (ppBase && !(*ppBase)))
377 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
378 SwForm aForm(eCurTOXType);
379 pNewTOX = new SwTOXBase(
380 pType, aForm,
381 TOX_AUTHORITIES == eCurTOXType ? SwTOXElement::Mark : SwTOXElement::NONE,
382 pType->GetTypeName());
384 pNewTOX->SetFromObjectNames(rDesc.IsCreateFromObjectNames());
385 pNewTOX->SetOLEOptions(rDesc.GetOLEOptions());
387 break;
390 OSL_ENSURE(pNewTOX, "no TOXBase created!" );
391 if(!pNewTOX)
392 return false;
394 pNewTOX->SetFromChapter(rDesc.IsFromChapter());
395 pNewTOX->SetSequenceName(rDesc.GetSequenceName());
396 pNewTOX->SetCaptionDisplay(rDesc.GetCaptionDisplay());
397 pNewTOX->SetProtected(rDesc.IsReadonly());
399 for(sal_uInt16 nLevel = 0; nLevel < MAXLEVEL; nLevel++)
400 pNewTOX->SetStyleNames(rDesc.GetStyleNames(nLevel), nLevel);
402 if(rDesc.GetTitle())
403 pNewTOX->SetTitle(*rDesc.GetTitle());
404 if(rDesc.GetForm())
405 pNewTOX->SetTOXForm(*rDesc.GetForm());
406 pNewTOX->SetLanguage(rDesc.GetLanguage());
407 pNewTOX->SetSortAlgorithm(rDesc.GetSortAlgorithm());
409 if(!pCurTOX || (ppBase && !(*ppBase)) )
411 // when ppBase is passed over, TOXBase is only created here
412 // and then inserted in a global document by the dialog
413 if(ppBase)
414 (*ppBase) = pNewTOX;
415 else
417 pSh->InsertTableOf(*pNewTOX, pSet);
418 delete pNewTOX;
421 else
423 SwDoc * pDoc = pSh->GetDoc();
425 if (pDoc->GetIDocumentUndoRedo().DoesUndo())
427 pDoc->GetIDocumentUndoRedo().StartUndo(SwUndoId::TOXCHANGE, nullptr);
430 SwTOXBase *const pTOX = const_cast<SwTOXBase*>(pCurTOX);
431 pDoc->ChangeTOX(*pTOX, *pNewTOX);
433 pTOX->DisableKeepExpression();
434 pSh->UpdateTableOf(*pTOX, pSet);
435 bRet = false;
436 pTOX->EnableKeepExpression();
438 if (pDoc->GetIDocumentUndoRedo().DoesUndo())
440 pDoc->GetIDocumentUndoRedo().EndUndo(SwUndoId::TOXCHANGE, nullptr);
444 return bRet;
447 void SwTOXDescription::SetSortKeys(SwTOXSortKey eKey1,
448 SwTOXSortKey eKey2,
449 SwTOXSortKey eKey3)
451 SwTOXSortKey aArr[3];
452 sal_uInt16 nPos = 0;
453 if(AUTH_FIELD_END > eKey1.eField)
454 aArr[nPos++] = eKey1;
455 if(AUTH_FIELD_END > eKey2.eField)
456 aArr[nPos++] = eKey2;
457 if(AUTH_FIELD_END > eKey3.eField)
458 aArr[nPos++] = eKey3;
460 m_eSortKey1 = aArr[0];
461 m_eSortKey2 = aArr[1];
462 m_eSortKey3 = aArr[2];
465 void SwTOXDescription::ApplyTo(SwTOXBase& rTOXBase)
467 for(sal_uInt16 i = 0; i < MAXLEVEL; i++)
468 rTOXBase.SetStyleNames(GetStyleNames(i), i);
469 rTOXBase.SetTitle(GetTitle() ? *GetTitle() : OUString());
470 rTOXBase.SetCreate(GetContentOptions());
472 if(GetTOXType() == TOX_INDEX)
473 rTOXBase.SetOptions(GetIndexOptions());
474 if(GetTOXType() != TOX_INDEX)
475 rTOXBase.SetLevel(GetLevel());
476 rTOXBase.SetFromObjectNames(IsCreateFromObjectNames());
477 rTOXBase.SetSequenceName(GetSequenceName());
478 rTOXBase.SetCaptionDisplay(GetCaptionDisplay());
479 rTOXBase.SetFromChapter(IsFromChapter());
480 rTOXBase.SetProtected(IsReadonly());
481 rTOXBase.SetOLEOptions(GetOLEOptions());
482 rTOXBase.SetLevelFromChapter(IsLevelFromChapter());
483 rTOXBase.SetLanguage(m_eLanguage);
484 rTOXBase.SetSortAlgorithm(m_sSortAlgorithm);
488 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */