1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
25 #include <IDocumentUndoRedo.hxx>
28 // handle indexes with TOXMgr
29 SwTOXMgr::SwTOXMgr(SwWrtShell
* pShell
):
32 pSh
->GetCurTOXMarks(aCurMarks
);
36 SwTOXMark
* SwTOXMgr::GetTOXMark(sal_uInt16 nId
)
38 if(!aCurMarks
.empty())
39 return aCurMarks
[nId
];
43 void SwTOXMgr::DeleteTOXMark()
45 SwTOXMark
* pNext
= nullptr;
48 pNext
= const_cast<SwTOXMark
*>(&pSh
->GotoTOXMark( *pCurTOXMark
, TOX_NXT
));
49 if( pNext
== pCurTOXMark
)
52 pSh
->DeleteTOXMark( pCurTOXMark
);
59 void SwTOXMgr::InsertTOXMark(const SwTOXMarkDescription
& rDesc
)
61 SwTOXMark
* pMark
= nullptr;
62 switch(rDesc
.GetTOXType())
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()) );
72 pMark
->SetAlternativeText(*rDesc
.GetAltStr());
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() );
93 pMark
->SetAlternativeText(*rDesc
.GetAltStr());
94 if(rDesc
.GetPhoneticReadingOfAltStr())
95 pMark
->SetTextReading( *rDesc
.GetPhoneticReadingOfAltStr() );
96 pMark
->SetMainEntry(rDesc
.IsMainEntry());
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());
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());
136 default:; //prevent warning
142 pSh
->StartAllAction();
143 pSh
->SwEditShell::Insert(*pMark
);
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() );
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() );
168 pCurTOXMark
->SetSecondaryKeyReading(OUString());
172 pCurTOXMark
->SetSecondaryKey(OUString());
173 pCurTOXMark
->SetSecondaryKeyReading(OUString());
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() );
186 pCurTOXMark
->SetTextReading(OUString());
187 pCurTOXMark
->SetMainEntry(rDesc
.IsMainEntry());
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,
196 bool bReplace
= pCurTOXMark
->IsAlternativeText();
198 pCurTOXMark
->SetAlternativeText( *rDesc
.GetAltStr() );
201 SwTOXMark
aCpy( *pCurTOXMark
);
203 pSh
->DeleteTOXMark(pCurTOXMark
);
204 aCpy
.SetAlternativeText( *rDesc
.GetAltStr() );
205 pSh
->SwEditShell::Insert( aCpy
);
206 pCurTOXMark
= nullptr;
211 // Bug 36207 pCurTOXMark points nowhere here!
214 pSh
->Left(CRSR_SKIP_CHARS
, false, 1, false );
215 pSh
->GetCurTOXMarks(aCurMarks
);
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
)
230 SwTOXType
aUserType(*pSh
->GetDoc(), TOX_USER
, rStr
);
231 pSh
->InsertTOXType(aUserType
);
235 // traveling between TOXMarks
236 void SwTOXMgr::NextTOXMark(bool bSame
)
238 OSL_ENSURE(pCurTOXMark
, "no current TOXMark");
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");
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
,
268 const SfxItemSet
* pSet
)
270 SwWait
aWait( *pSh
->GetView().GetDocShell(), 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())
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();
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());
310 if(!pCurTOX
|| (ppBase
&& !(*ppBase
)))
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())
321 const SwTOXType
* pType
= pSh
->GetTOXType(eCurTOXType
, nPos
);
323 SwForm
aForm(eCurTOXType
);
324 pNewTOX
= new SwTOXBase(pType
, aForm
, rDesc
.GetContentOptions(), pType
->GetTypeName());
329 pNewTOX
->SetCreate(rDesc
.GetContentOptions());
331 pNewTOX
->SetLevelFromChapter(rDesc
.IsLevelFromChapter());
334 case TOX_CITATION
: /** TODO */break;
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()));
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');
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(
381 TOX_AUTHORITIES
== eCurTOXType
? SwTOXElement::Mark
: SwTOXElement::NONE
,
382 pType
->GetTypeName());
384 pNewTOX
->SetFromObjectNames(rDesc
.IsCreateFromObjectNames());
385 pNewTOX
->SetOLEOptions(rDesc
.GetOLEOptions());
390 OSL_ENSURE(pNewTOX
, "no TOXBase created!" );
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
);
403 pNewTOX
->SetTitle(*rDesc
.GetTitle());
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
417 pSh
->InsertTableOf(*pNewTOX
, pSet
);
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
);
436 pTOX
->EnableKeepExpression();
438 if (pDoc
->GetIDocumentUndoRedo().DoesUndo())
440 pDoc
->GetIDocumentUndoRedo().EndUndo(SwUndoId::TOXCHANGE
, nullptr);
447 void SwTOXDescription::SetSortKeys(SwTOXSortKey eKey1
,
451 SwTOXSortKey aArr
[3];
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: */