bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / index / toxmgr.cxx
blob60e62208e55ed15e54089a7f93fbf754b6e50f77
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 .
21 #include <wrtsh.hxx>
22 #include <shellres.hxx>
23 #include <swwait.hxx>
24 #include <view.hxx>
25 #include <toxmgr.hxx>
26 #include <crsskip.hxx>
27 #include <doc.hxx>
28 #include <IDocumentUndoRedo.hxx>
29 #include <swundo.hxx>
30 #include <globals.hrc>
32 /*--------------------------------------------------------------------
33 Description: handle indexes with TOXMgr
34 --------------------------------------------------------------------*/
37 SwTOXMgr::SwTOXMgr(SwWrtShell* pShell):
38 pSh(pShell)
40 GetTOXMarks();
41 SetCurTOXMark(0);
44 /*--------------------------------------------------------------------
45 Description: handle current TOXMarks
46 --------------------------------------------------------------------*/
49 sal_uInt16 SwTOXMgr::GetTOXMarks()
51 return pSh->GetCurTOXMarks(aCurMarks);
55 SwTOXMark* SwTOXMgr::GetTOXMark(sal_uInt16 nId)
57 if(!aCurMarks.empty())
58 return aCurMarks[nId];
59 return 0;
63 void SwTOXMgr::DeleteTOXMark()
65 SwTOXMark* pNext = 0;
66 if( pCurTOXMark )
68 pNext = (SwTOXMark*)&pSh->GotoTOXMark( *pCurTOXMark, TOX_NXT );
69 if( pNext == pCurTOXMark )
70 pNext = 0;
72 pSh->DeleteTOXMark( pCurTOXMark );
73 pSh->SetModified();
75 // go to next one
76 pCurTOXMark = pNext;
79 void SwTOXMgr::InsertTOXMark(const SwTOXMarkDescription& rDesc)
81 SwTOXMark* pMark = 0;
82 switch(rDesc.GetTOXType())
84 case TOX_CONTENT:
86 OSL_ENSURE(rDesc.GetLevel() > 0 && rDesc.GetLevel() <= MAXLEVEL,
87 "invalid InsertTOCMark level");
88 pMark = new SwTOXMark(pSh->GetTOXType(TOX_CONTENT, 0));
89 pMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
91 if(rDesc.GetAltStr())
92 pMark->SetAlternativeText(*rDesc.GetAltStr());
94 break;
95 case TOX_INDEX:
97 pMark = new SwTOXMark(pSh->GetTOXType(TOX_INDEX, 0));
99 if( rDesc.GetPrimKey() && rDesc.GetPrimKey()->Len() )
101 pMark->SetPrimaryKey( *rDesc.GetPrimKey() );
102 if(rDesc.GetPhoneticReadingOfPrimKey())
103 pMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
105 if( rDesc.GetSecKey() && rDesc.GetSecKey()->Len() )
107 pMark->SetSecondaryKey( *rDesc.GetSecKey() );
108 if(rDesc.GetPhoneticReadingOfSecKey())
109 pMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
112 if(rDesc.GetAltStr())
113 pMark->SetAlternativeText(*rDesc.GetAltStr());
114 if(rDesc.GetPhoneticReadingOfAltStr())
115 pMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
116 pMark->SetMainEntry(rDesc.IsMainEntry());
118 break;
119 case TOX_USER:
121 OSL_ENSURE(rDesc.GetLevel() > 0 && rDesc.GetLevel() <= MAXLEVEL,
122 "invalid InsertTOCMark level");
123 sal_uInt16 nId = rDesc.GetTOUName() ?
124 GetUserTypeID(*rDesc.GetTOUName()) : 0;
125 pMark = new SwTOXMark(pSh->GetTOXType(TOX_USER, nId));
126 pMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
128 if(rDesc.GetAltStr())
129 pMark->SetAlternativeText(*rDesc.GetAltStr());
131 break;
132 default:; //prevent warning
134 pSh->StartAllAction();
135 pSh->SwEditShell::Insert(*pMark);
136 pSh->EndAllAction();
138 /*--------------------------------------------------------------------
139 Description: Update of TOXMarks
140 --------------------------------------------------------------------*/
143 void SwTOXMgr::UpdateTOXMark(const SwTOXMarkDescription& rDesc)
145 OSL_ENSURE(pCurTOXMark, "no current TOXMark");
147 pSh->StartAllAction();
148 if(pCurTOXMark->GetTOXType()->GetType() == TOX_INDEX)
150 if(rDesc.GetPrimKey() && rDesc.GetPrimKey()->Len() )
152 pCurTOXMark->SetPrimaryKey( *rDesc.GetPrimKey() );
153 if(rDesc.GetPhoneticReadingOfPrimKey())
154 pCurTOXMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
155 else
156 pCurTOXMark->SetPrimaryKeyReading( aEmptyStr );
158 if( rDesc.GetSecKey() && rDesc.GetSecKey()->Len() )
160 pCurTOXMark->SetSecondaryKey( *rDesc.GetSecKey() );
161 if(rDesc.GetPhoneticReadingOfSecKey())
162 pCurTOXMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
163 else
164 pCurTOXMark->SetSecondaryKeyReading( aEmptyStr );
166 else
168 pCurTOXMark->SetSecondaryKey( aEmptyStr );
169 pCurTOXMark->SetSecondaryKeyReading( aEmptyStr );
172 else
174 pCurTOXMark->SetPrimaryKey( aEmptyStr );
175 pCurTOXMark->SetPrimaryKeyReading( aEmptyStr );
176 pCurTOXMark->SetSecondaryKey( aEmptyStr );
177 pCurTOXMark->SetSecondaryKeyReading( aEmptyStr );
179 if(rDesc.GetPhoneticReadingOfAltStr())
180 pCurTOXMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
181 else
182 pCurTOXMark->SetTextReading( aEmptyStr );
183 pCurTOXMark->SetMainEntry(rDesc.IsMainEntry());
185 else
186 pCurTOXMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
188 if(rDesc.GetAltStr())
190 // JP 26.08.96: Bug 30344 - either the text of a Doc or an alternative test,
191 // not both!
192 sal_Bool bReplace = pCurTOXMark->IsAlternativeText();
193 if( bReplace )
194 pCurTOXMark->SetAlternativeText( *rDesc.GetAltStr() );
195 else
197 SwTOXMark aCpy( *pCurTOXMark );
198 aCurMarks.clear();
199 pSh->DeleteTOXMark(pCurTOXMark);
200 aCpy.SetAlternativeText( *rDesc.GetAltStr() );
201 pSh->SwEditShell::Insert( aCpy );
202 pCurTOXMark = 0;
205 pSh->SetModified();
206 pSh->EndAllAction();
207 // Bug 36207 pCurTOXMark points nowhere here!
208 if(!pCurTOXMark)
210 pSh->Left(CRSR_SKIP_CHARS, sal_False, 1, sal_False );
211 pSh->GetCurTOXMarks(aCurMarks);
212 SetCurTOXMark(0);
217 /*--------------------------------------------------------------------
218 Description: determine UserTypeID
219 --------------------------------------------------------------------*/
222 sal_uInt16 SwTOXMgr::GetUserTypeID(const String& rStr)
224 sal_uInt16 nSize = pSh->GetTOXTypeCount(TOX_USER);
225 for(sal_uInt16 i=0; i < nSize; ++i)
227 const SwTOXType* pTmp = pSh->GetTOXType(TOX_USER, i);
228 if(pTmp && pTmp->GetTypeName() == rStr)
229 return i;
231 SwTOXType aUserType(TOX_USER, rStr);
232 pSh->InsertTOXType(aUserType);
233 return nSize;
236 /*--------------------------------------------------------------------
237 Description: traveling between TOXMarks
238 --------------------------------------------------------------------*/
241 void SwTOXMgr::NextTOXMark(sal_Bool bSame)
243 OSL_ENSURE(pCurTOXMark, "no current TOXMark");
244 if( pCurTOXMark )
246 SwTOXSearch eDir = bSame ? TOX_SAME_NXT : TOX_NXT;
247 pCurTOXMark = (SwTOXMark*)&pSh->GotoTOXMark( *pCurTOXMark, eDir );
252 void SwTOXMgr::PrevTOXMark(sal_Bool bSame)
254 OSL_ENSURE(pCurTOXMark, "no current TOXMark");
255 if( pCurTOXMark )
257 SwTOXSearch eDir = bSame ? TOX_SAME_PRV : TOX_PRV;
258 pCurTOXMark = (SwTOXMark*)&pSh->GotoTOXMark(*pCurTOXMark, eDir );
262 /*--------------------------------------------------------------------
263 Description: insert keyword index
264 --------------------------------------------------------------------*/
265 const SwTOXBase* SwTOXMgr::GetCurTOX()
267 return pSh->GetCurTOX();
270 const SwTOXType* SwTOXMgr::GetTOXType(TOXTypes eTyp, sal_uInt16 nId) const
272 return pSh->GetTOXType(eTyp, nId);
275 void SwTOXMgr::SetCurTOXMark(sal_uInt16 nId)
277 pCurTOXMark = (nId < aCurMarks.size()) ? aCurMarks[nId] : 0;
280 sal_Bool SwTOXMgr::UpdateOrInsertTOX(const SwTOXDescription& rDesc,
281 SwTOXBase** ppBase,
282 const SfxItemSet* pSet)
284 SwWait aWait( *pSh->GetView().GetDocShell(), sal_True );
285 sal_Bool bRet = sal_True;
286 const SwTOXBase* pCurTOX = ppBase && *ppBase ? *ppBase : GetCurTOX();
287 SwTOXBase* pTOX = (SwTOXBase*)pCurTOX;
289 SwTOXBase * pNewTOX = NULL;
291 if (pTOX)
292 pNewTOX = new SwTOXBase(*pTOX);
294 TOXTypes eCurTOXType = rDesc.GetTOXType();
295 if(pCurTOX && !ppBase && pSh->HasSelection())
296 pSh->EnterStdMode();
298 switch(eCurTOXType)
300 case TOX_INDEX :
302 if(!pCurTOX || (ppBase && !(*ppBase)))
304 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
305 SwForm aForm(eCurTOXType);
306 pNewTOX = new SwTOXBase(pType, aForm, nsSwTOXElement::TOX_MARK, pType->GetTypeName());
308 pNewTOX->SetOptions(rDesc.GetIndexOptions());
309 pNewTOX->SetMainEntryCharStyle(rDesc.GetMainEntryCharStyle());
310 pSh->SetTOIAutoMarkURL(rDesc.GetAutoMarkURL());
311 pSh->ApplyAutoMark();
313 break;
314 case TOX_CONTENT :
316 if(!pCurTOX || (ppBase && !(*ppBase)))
318 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
319 SwForm aForm(eCurTOXType);
320 pNewTOX = new SwTOXBase(pType, aForm, rDesc.GetContentOptions(), pType->GetTypeName());
322 pNewTOX->SetCreate(rDesc.GetContentOptions());
323 pNewTOX->SetLevel(rDesc.GetLevel());
325 break;
326 case TOX_USER :
328 if(!pCurTOX || (ppBase && !(*ppBase)))
330 sal_uInt16 nPos = 0;
331 sal_uInt16 nSize = pSh->GetTOXTypeCount(eCurTOXType);
332 for(sal_uInt16 i=0; rDesc.GetTOUName() && i < nSize; ++i)
333 { const SwTOXType* pType = pSh->GetTOXType(TOX_USER, i);
334 if(pType->GetTypeName() == *rDesc.GetTOUName())
335 { nPos = i;
336 break;
339 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, nPos);
341 SwForm aForm(eCurTOXType);
342 pNewTOX = new SwTOXBase(pType, aForm, rDesc.GetContentOptions(), pType->GetTypeName());
345 else
347 const_cast<SwTOXBase*>( pCurTOX )->SetCreate(rDesc.GetContentOptions());
349 pNewTOX->SetLevelFromChapter(rDesc.IsLevelFromChapter());
351 break;
352 case TOX_OBJECTS:
353 case TOX_TABLES:
354 case TOX_AUTHORITIES:
355 case TOX_ILLUSTRATIONS:
357 //Special handling for TOX_AUTHORITY
358 if(TOX_AUTHORITIES == eCurTOXType)
360 SwAuthorityFieldType* pFType = (SwAuthorityFieldType*)
361 pSh->GetFldType(RES_AUTHORITY, aEmptyStr);
362 if (!pFType)
364 SwAuthorityFieldType const type(pSh->GetDoc());
365 pFType = static_cast<SwAuthorityFieldType*>(
366 pSh->InsertFldType(type));
368 pFType->SetPreSuffix(rDesc.GetAuthBrackets().GetChar(0),
369 rDesc.GetAuthBrackets().GetChar(1));
370 pFType->SetSequence(rDesc.IsAuthSequence());
371 SwTOXSortKey rArr[3];
372 rArr[0] = rDesc.GetSortKey1();
373 rArr[1] = rDesc.GetSortKey2();
374 rArr[2] = rDesc.GetSortKey3();
375 pFType->SetSortKeys(3, rArr);
376 pFType->SetSortByDocument(rDesc.IsSortByDocument());
377 pFType->SetLanguage(rDesc.GetLanguage());
378 pFType->SetSortAlgorithm(rDesc.GetSortAlgorithm());
380 pFType->UpdateFlds();
382 // TODO: consider properties of the current TOXType
383 if(!pCurTOX || (ppBase && !(*ppBase)))
385 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
386 SwForm aForm(eCurTOXType);
387 pNewTOX = new SwTOXBase(
388 pType, aForm,
389 TOX_AUTHORITIES == eCurTOXType ? nsSwTOXElement::TOX_MARK : 0, pType->GetTypeName());
391 else
393 if((!ppBase || !(*ppBase)) && pSh->HasSelection())
394 pSh->DelRight();
395 pNewTOX = (SwTOXBase*)pCurTOX;
397 pNewTOX->SetFromObjectNames(rDesc.IsCreateFromObjectNames());
398 pNewTOX->SetOLEOptions(rDesc.GetOLEOptions());
400 break;
404 OSL_ENSURE(pNewTOX, "no TOXBase created!" );
405 if(!pNewTOX)
406 return sal_False;
408 pNewTOX->SetFromChapter(rDesc.IsFromChapter());
409 pNewTOX->SetSequenceName(rDesc.GetSequenceName());
410 pNewTOX->SetCaptionDisplay(rDesc.GetCaptionDisplay());
411 pNewTOX->SetProtected(rDesc.IsReadonly());
413 for(sal_uInt16 nLevel = 0; nLevel < MAXLEVEL; nLevel++)
414 pNewTOX->SetStyleNames(rDesc.GetStyleNames(nLevel), nLevel);
416 if(rDesc.GetTitle())
417 pNewTOX->SetTitle(*rDesc.GetTitle());
418 if(rDesc.GetForm())
419 pNewTOX->SetTOXForm(*rDesc.GetForm());
420 pNewTOX->SetLanguage(rDesc.GetLanguage());
421 pNewTOX->SetSortAlgorithm(rDesc.GetSortAlgorithm());
423 if(!pCurTOX || (ppBase && !(*ppBase)) )
425 // when ppBase is passed over, TOXBase is only created here
426 // and then inserted in a global document by the dialog
427 if(ppBase)
428 (*ppBase) = pNewTOX;
429 else
431 pSh->InsertTableOf(*pNewTOX, pSet);
432 delete pNewTOX;
435 else
437 SwDoc * pDoc = pSh->GetDoc();
439 if (pDoc->GetIDocumentUndoRedo().DoesUndo())
441 if (pNewTOX != NULL)
443 pDoc->GetIDocumentUndoRedo().DelAllUndoObj();
446 pDoc->GetIDocumentUndoRedo().StartUndo(UNDO_TOXCHANGE, NULL);
449 if (pNewTOX != NULL) // => pTOX != NULL
450 pDoc->ChgTOX(*pTOX, *pNewTOX);
452 bRet = pSh->UpdateTableOf(*pTOX, pSet);
454 if (pDoc->GetIDocumentUndoRedo().DoesUndo())
456 pDoc->GetIDocumentUndoRedo().EndUndo(UNDO_TOXCHANGE, NULL);
458 if (pNewTOX == NULL)
460 pDoc->GetIDocumentUndoRedo().DelAllUndoObj();
465 return bRet;
468 void SwTOXDescription::SetSortKeys(SwTOXSortKey eKey1,
469 SwTOXSortKey eKey2,
470 SwTOXSortKey eKey3)
472 SwTOXSortKey aArr[3];
473 sal_uInt16 nPos = 0;
474 if(AUTH_FIELD_END > eKey1.eField)
475 aArr[nPos++] = eKey1;
476 if(AUTH_FIELD_END > eKey2.eField)
477 aArr[nPos++] = eKey2;
478 if(AUTH_FIELD_END > eKey3.eField)
479 aArr[nPos++] = eKey3;
481 eSortKey1 = aArr[0];
482 eSortKey2 = aArr[1];
483 eSortKey3 = aArr[2];
486 void SwTOXDescription::ApplyTo(SwTOXBase& rTOXBase)
488 for(sal_uInt16 i = 0; i < MAXLEVEL; i++)
489 rTOXBase.SetStyleNames(GetStyleNames(i), i);
490 rTOXBase.SetTitle(GetTitle() ? *GetTitle() : aEmptyStr);
491 rTOXBase.SetCreate(GetContentOptions());
493 if(GetTOXType() == TOX_INDEX)
494 rTOXBase.SetOptions(GetIndexOptions());
495 if(GetTOXType() != TOX_INDEX)
496 rTOXBase.SetLevel(GetLevel());
497 rTOXBase.SetFromObjectNames(IsCreateFromObjectNames());
498 rTOXBase.SetSequenceName(GetSequenceName());
499 rTOXBase.SetCaptionDisplay(GetCaptionDisplay());
500 rTOXBase.SetFromChapter(IsFromChapter());
501 rTOXBase.SetProtected(IsReadonly());
502 rTOXBase.SetOLEOptions(GetOLEOptions());
503 rTOXBase.SetLevelFromChapter(IsLevelFromChapter());
504 rTOXBase.SetLanguage(eLanguage);
505 rTOXBase.SetSortAlgorithm(sSortAlgorithm);
511 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */