merge the formfield patch from ooo-build
[ooovba.git] / sw / source / ui / index / toxmgr.cxx
blobe32529b6198a17e6db7a4a68211d3118a6947118
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: toxmgr.cxx,v $
10 * $Revision: 1.17 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
33 #include <wrtsh.hxx>
34 #include <shellres.hxx>
35 #include <swwait.hxx>
36 #include <view.hxx>
37 #include <toxmgr.hxx>
38 #include <crsskip.hxx>
39 #include <doc.hxx>
40 #include <swundo.hxx>
41 #ifndef _GLOBALS_HRC
42 #include <globals.hrc>
43 #endif
45 /*--------------------------------------------------------------------
46 Beschreibung: Handhabung der Verzeichnisse durch TOXMgr
47 --------------------------------------------------------------------*/
50 SwTOXMgr::SwTOXMgr(SwWrtShell* pShell):
51 pSh(pShell)
53 GetTOXMarks();
54 SetCurTOXMark(0);
57 /*--------------------------------------------------------------------
58 Beschreibung: Aktuelle TOXMarks behandeln
59 --------------------------------------------------------------------*/
62 USHORT SwTOXMgr::GetTOXMarks()
64 return pSh->GetCurTOXMarks(aCurMarks);
68 SwTOXMark* SwTOXMgr::GetTOXMark(USHORT nId)
70 if(aCurMarks.Count() > 0)
71 return aCurMarks[nId];
72 return 0;
76 void SwTOXMgr::DeleteTOXMark()
78 SwTOXMark* pNext = 0;
79 if( pCurTOXMark )
81 pNext = (SwTOXMark*)&pSh->GotoTOXMark( *pCurTOXMark, TOX_NXT );
82 if( pNext == pCurTOXMark )
83 pNext = 0;
85 pSh->DeleteTOXMark( pCurTOXMark );
86 pSh->SetModified();
88 // zur naechsten wandern
89 pCurTOXMark = pNext;
91 /* -----------------20.08.99 10:48-------------------
93 --------------------------------------------------*/
94 void SwTOXMgr::InsertTOXMark(const SwTOXMarkDescription& rDesc)
96 SwTOXMark* pMark = 0;
97 switch(rDesc.GetTOXType())
99 case TOX_CONTENT:
101 ASSERT(rDesc.GetLevel() > 0 && rDesc.GetLevel() <= MAXLEVEL,
102 ungueltiger Level InsertTOCMark);
103 pMark = new SwTOXMark(pSh->GetTOXType(TOX_CONTENT, 0));
104 pMark->SetLevel( static_cast< USHORT >(rDesc.GetLevel()) );
106 if(rDesc.GetAltStr())
107 pMark->SetAlternativeText(*rDesc.GetAltStr());
109 break;
110 case TOX_INDEX:
112 pMark = new SwTOXMark(pSh->GetTOXType(TOX_INDEX, 0));
114 if( rDesc.GetPrimKey() && rDesc.GetPrimKey()->Len() )
116 pMark->SetPrimaryKey( *rDesc.GetPrimKey() );
117 if(rDesc.GetPhoneticReadingOfPrimKey())
118 pMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
120 if( rDesc.GetSecKey() && rDesc.GetSecKey()->Len() )
122 pMark->SetSecondaryKey( *rDesc.GetSecKey() );
123 if(rDesc.GetPhoneticReadingOfSecKey())
124 pMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
127 if(rDesc.GetAltStr())
128 pMark->SetAlternativeText(*rDesc.GetAltStr());
129 if(rDesc.GetPhoneticReadingOfAltStr())
130 pMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
131 pMark->SetMainEntry(rDesc.IsMainEntry());
133 break;
134 case TOX_USER:
136 ASSERT(rDesc.GetLevel() > 0 && rDesc.GetLevel() <= MAXLEVEL,
137 ungueltiger Level InsertTOCMark);
138 USHORT nId = rDesc.GetTOUName() ?
139 GetUserTypeID(*rDesc.GetTOUName()) : 0;
140 pMark = new SwTOXMark(pSh->GetTOXType(TOX_USER, nId));
141 pMark->SetLevel( static_cast< USHORT >(rDesc.GetLevel()) );
143 if(rDesc.GetAltStr())
144 pMark->SetAlternativeText(*rDesc.GetAltStr());
146 break;
147 default:; //prevent warning
149 pSh->StartAllAction();
150 pSh->SwEditShell::Insert(*pMark);
151 pSh->EndAllAction();
153 /*--------------------------------------------------------------------
154 Beschreibung: Update eines TOXMarks
155 --------------------------------------------------------------------*/
158 void SwTOXMgr::UpdateTOXMark(const SwTOXMarkDescription& rDesc)
160 ASSERT(pCurTOXMark, "kein aktuelles TOXMark");
162 pSh->StartAllAction();
163 if(pCurTOXMark->GetTOXType()->GetType() == TOX_INDEX)
165 if(rDesc.GetPrimKey() && rDesc.GetPrimKey()->Len() )
167 pCurTOXMark->SetPrimaryKey( *rDesc.GetPrimKey() );
168 if(rDesc.GetPhoneticReadingOfPrimKey())
169 pCurTOXMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
170 else
171 pCurTOXMark->SetPrimaryKeyReading( aEmptyStr );
173 if( rDesc.GetSecKey() && rDesc.GetSecKey()->Len() )
175 pCurTOXMark->SetSecondaryKey( *rDesc.GetSecKey() );
176 if(rDesc.GetPhoneticReadingOfSecKey())
177 pCurTOXMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
178 else
179 pCurTOXMark->SetSecondaryKeyReading( aEmptyStr );
181 else
183 pCurTOXMark->SetSecondaryKey( aEmptyStr );
184 pCurTOXMark->SetSecondaryKeyReading( aEmptyStr );
187 else
189 pCurTOXMark->SetPrimaryKey( aEmptyStr );
190 pCurTOXMark->SetPrimaryKeyReading( aEmptyStr );
191 pCurTOXMark->SetSecondaryKey( aEmptyStr );
192 pCurTOXMark->SetSecondaryKeyReading( aEmptyStr );
194 if(rDesc.GetPhoneticReadingOfAltStr())
195 pCurTOXMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
196 else
197 pCurTOXMark->SetTextReading( aEmptyStr );
198 pCurTOXMark->SetMainEntry(rDesc.IsMainEntry());
200 else
201 pCurTOXMark->SetLevel( static_cast< USHORT >(rDesc.GetLevel()) );
203 if(rDesc.GetAltStr())
205 // JP 26.08.96: Bug 30344 - entweder der Text aus dem Doc oder
206 // ein Alternativ-Text, beides gibts nicht!
207 BOOL bReplace = pCurTOXMark->IsAlternativeText();
208 if( bReplace )
209 pCurTOXMark->SetAlternativeText( *rDesc.GetAltStr() );
210 else
212 SwTOXMark aCpy( *pCurTOXMark );
213 aCurMarks.Remove(0, aCurMarks.Count());
214 pSh->DeleteTOXMark(pCurTOXMark);
215 aCpy.SetAlternativeText( *rDesc.GetAltStr() );
216 pSh->SwEditShell::Insert( aCpy );
217 pCurTOXMark = 0;
220 pSh->SetModified();
221 pSh->EndAllAction();
222 // Bug 36207 pCurTOXMark zeigt hier in den Wald!
223 if(!pCurTOXMark)
225 pSh->Left(CRSR_SKIP_CHARS, FALSE, 1, FALSE );
226 pSh->GetCurTOXMarks(aCurMarks);
227 SetCurTOXMark(0);
232 /*--------------------------------------------------------------------
233 Beschreibung: UserTypeID ermitteln
234 --------------------------------------------------------------------*/
237 USHORT SwTOXMgr::GetUserTypeID(const String& rStr)
239 USHORT nSize = pSh->GetTOXTypeCount(TOX_USER);
240 for(USHORT i=0; i < nSize; ++i)
242 const SwTOXType* pTmp = pSh->GetTOXType(TOX_USER, i);
243 if(pTmp && pTmp->GetTypeName() == rStr)
244 return i;
246 SwTOXType aUserType(TOX_USER, rStr);
247 pSh->InsertTOXType(aUserType);
248 return nSize;
251 /*--------------------------------------------------------------------
252 Beschreibung: Traveling zwischen den TOXMarks
253 --------------------------------------------------------------------*/
256 void SwTOXMgr::NextTOXMark(BOOL bSame)
258 ASSERT(pCurTOXMark, "kein aktuelles TOXMark");
259 if( pCurTOXMark )
261 SwTOXSearch eDir = bSame ? TOX_SAME_NXT : TOX_NXT;
262 pCurTOXMark = (SwTOXMark*)&pSh->GotoTOXMark( *pCurTOXMark, eDir );
267 void SwTOXMgr::PrevTOXMark(BOOL bSame)
269 ASSERT(pCurTOXMark, "kein aktuelles TOXMark");
270 if( pCurTOXMark )
272 SwTOXSearch eDir = bSame ? TOX_SAME_PRV : TOX_PRV;
273 pCurTOXMark = (SwTOXMark*)&pSh->GotoTOXMark(*pCurTOXMark, eDir );
277 /*--------------------------------------------------------------------
278 Beschreibung: Stichwortverzeichnis einfuegen
279 --------------------------------------------------------------------*/
280 const SwTOXBase* SwTOXMgr::GetCurTOX()
282 return pSh->GetCurTOX();
285 const SwTOXType* SwTOXMgr::GetTOXType(TOXTypes eTyp, USHORT nId) const
287 return pSh->GetTOXType(eTyp, nId);
290 void SwTOXMgr::SetCurTOXMark(USHORT nId)
292 pCurTOXMark = (nId < aCurMarks.Count()) ? aCurMarks[nId] : 0;
295 /* -----------------01.07.99 16:23-------------------
297 --------------------------------------------------*/
299 BOOL SwTOXMgr::UpdateOrInsertTOX(const SwTOXDescription& rDesc,
300 SwTOXBase** ppBase,
301 const SfxItemSet* pSet)
303 SwWait aWait( *pSh->GetView().GetDocShell(), TRUE );
304 BOOL bRet = TRUE;
305 const SwTOXBase* pCurTOX = ppBase && *ppBase ? *ppBase : GetCurTOX();
306 SwTOXBase* pTOX = (SwTOXBase*)pCurTOX;
308 SwTOXBase * pNewTOX = NULL;
310 if (pTOX)
311 pNewTOX = new SwTOXBase(*pTOX);
313 TOXTypes eCurTOXType = rDesc.GetTOXType();
314 if(pCurTOX && !ppBase && pSh->HasSelection())
315 pSh->EnterStdMode();
317 switch(eCurTOXType)
319 case TOX_INDEX :
321 if(!pCurTOX || (ppBase && !(*ppBase)))
323 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
324 SwForm aForm(eCurTOXType);
325 pNewTOX = new SwTOXBase(pType, aForm, nsSwTOXElement::TOX_MARK, pType->GetTypeName());
327 pNewTOX->SetOptions(rDesc.GetIndexOptions());
328 pNewTOX->SetMainEntryCharStyle(rDesc.GetMainEntryCharStyle());
329 pSh->SetTOIAutoMarkURL(rDesc.GetAutoMarkURL());
330 pSh->ApplyAutoMark();
332 break;
333 case TOX_CONTENT :
335 if(!pCurTOX || (ppBase && !(*ppBase)))
337 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
338 SwForm aForm(eCurTOXType);
339 pNewTOX = new SwTOXBase(pType, aForm, rDesc.GetContentOptions(), pType->GetTypeName());
341 pNewTOX->SetCreate(rDesc.GetContentOptions());
342 pNewTOX->SetLevel(rDesc.GetLevel());
344 break;
345 case TOX_USER :
347 if(!pCurTOX || (ppBase && !(*ppBase)))
349 USHORT nPos = 0;
350 USHORT nSize = pSh->GetTOXTypeCount(eCurTOXType);
351 for(USHORT i=0; rDesc.GetTOUName() && i < nSize; ++i)
352 { const SwTOXType* pType = pSh->GetTOXType(TOX_USER, i);
353 if(pType->GetTypeName() == *rDesc.GetTOUName())
354 { nPos = i;
355 break;
358 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, nPos);
360 SwForm aForm(eCurTOXType);
361 pNewTOX = new SwTOXBase(pType, aForm, rDesc.GetContentOptions(), pType->GetTypeName());
364 else
366 const_cast<SwTOXBase*>( pCurTOX )->SetCreate(rDesc.GetContentOptions());
368 pNewTOX->SetLevelFromChapter(rDesc.IsLevelFromChapter());
370 break;
371 case TOX_OBJECTS:
372 case TOX_TABLES:
373 case TOX_AUTHORITIES:
374 case TOX_ILLUSTRATIONS:
376 //Special handling for TOX_AUTHORITY
377 if(TOX_AUTHORITIES == eCurTOXType)
379 SwAuthorityFieldType* pFType = (SwAuthorityFieldType*)
380 pSh->GetFldType(RES_AUTHORITY, aEmptyStr);
381 if(!pFType)
382 pFType = (SwAuthorityFieldType*)pSh->InsertFldType(
383 SwAuthorityFieldType(pSh->GetDoc()));
384 pFType->SetPreSuffix(rDesc.GetAuthBrackets().GetChar(0),
385 rDesc.GetAuthBrackets().GetChar(1));
386 pFType->SetSequence(rDesc.IsAuthSequence());
387 SwTOXSortKey rArr[3];
388 rArr[0] = rDesc.GetSortKey1();
389 rArr[1] = rDesc.GetSortKey2();
390 rArr[2] = rDesc.GetSortKey3();
391 pFType->SetSortKeys(3, rArr);
392 pFType->SetSortByDocument(rDesc.IsSortByDocument());
393 pFType->SetLanguage(rDesc.GetLanguage());
394 pFType->SetSortAlgorithm(rDesc.GetSortAlgorithm());
396 pFType->UpdateFlds();
398 // TODO: consider properties of the current TOXType
399 if(!pCurTOX || (ppBase && !(*ppBase)))
401 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
402 SwForm aForm(eCurTOXType);
403 pNewTOX = new SwTOXBase(
404 pType, aForm,
405 TOX_AUTHORITIES == eCurTOXType ? nsSwTOXElement::TOX_MARK : 0, pType->GetTypeName());
407 else
409 if((!ppBase || !(*ppBase)) && pSh->HasSelection())
410 pSh->DelRight();
411 pNewTOX = (SwTOXBase*)pCurTOX;
413 // pTOX->SetOptions(rDesc.GetIndexOptions());
414 pNewTOX->SetFromObjectNames(rDesc.IsCreateFromObjectNames());
415 pNewTOX->SetOLEOptions(rDesc.GetOLEOptions());
417 break;
421 DBG_ASSERT(pNewTOX, "no TOXBase created!" );
422 if(!pNewTOX)
423 return FALSE;
425 pNewTOX->SetFromChapter(rDesc.IsFromChapter());
426 pNewTOX->SetSequenceName(rDesc.GetSequenceName());
427 pNewTOX->SetCaptionDisplay(rDesc.GetCaptionDisplay());
428 pNewTOX->SetProtected(rDesc.IsReadonly());
430 for(USHORT nLevel = 0; nLevel < MAXLEVEL; nLevel++)
431 pNewTOX->SetStyleNames(rDesc.GetStyleNames(nLevel), nLevel);
433 if(rDesc.GetTitle())
434 pNewTOX->SetTitle(*rDesc.GetTitle());
435 if(rDesc.GetForm())
436 pNewTOX->SetTOXForm(*rDesc.GetForm());
437 pNewTOX->SetLanguage(rDesc.GetLanguage());
438 pNewTOX->SetSortAlgorithm(rDesc.GetSortAlgorithm());
440 if(!pCurTOX || (ppBase && !(*ppBase)) )
442 // wird ppBase uebergeben, dann wird das TOXBase hier nur erzeugt
443 // und dann ueber den Dialog in ein Globaldokument eingefuegt
444 if(ppBase)
445 (*ppBase) = pNewTOX;
446 else
448 pSh->InsertTableOf(*pNewTOX, pSet);
449 delete pNewTOX;
452 else
454 SwDoc * pDoc = pSh->GetDoc();
456 if (pDoc->DoesUndo())
458 if (pNewTOX != NULL)
459 pDoc->DelAllUndoObj();
461 pDoc->StartUndo(UNDO_TOXCHANGE, NULL);
464 if (pNewTOX != NULL) // => pTOX != NULL
465 pDoc->ChgTOX(*pTOX, *pNewTOX);
467 bRet = pSh->UpdateTableOf(*pTOX, pSet);
469 if (pDoc->DoesUndo())
471 pDoc->EndUndo(UNDO_TOXCHANGE, NULL);
473 if (pNewTOX == NULL)
474 pDoc->DelAllUndoObj();
478 return bRet;
480 /* -----------------20.10.99 14:11-------------------
482 --------------------------------------------------*/
483 void SwTOXDescription::SetSortKeys(SwTOXSortKey eKey1,
484 SwTOXSortKey eKey2,
485 SwTOXSortKey eKey3)
487 SwTOXSortKey aArr[3];
488 USHORT nPos = 0;
489 if(AUTH_FIELD_END > eKey1.eField)
490 aArr[nPos++] = eKey1;
491 if(AUTH_FIELD_END > eKey2.eField)
492 aArr[nPos++] = eKey2;
493 if(AUTH_FIELD_END > eKey3.eField)
494 aArr[nPos++] = eKey3;
496 eSortKey1 = aArr[0];
497 eSortKey2 = aArr[1];
498 eSortKey3 = aArr[2];
501 /* -----------------10.09.99 10:02-------------------
503 --------------------------------------------------*/
504 void SwTOXDescription::ApplyTo(SwTOXBase& rTOXBase)
506 for(USHORT i = 0; i < MAXLEVEL; i++)
507 rTOXBase.SetStyleNames(GetStyleNames(i), i);
508 rTOXBase.SetTitle(GetTitle() ? *GetTitle() : aEmptyStr);
509 rTOXBase.SetCreate(GetContentOptions());
511 if(GetTOXType() == TOX_INDEX)
512 rTOXBase.SetOptions(GetIndexOptions());
513 if(GetTOXType() != TOX_INDEX)
514 rTOXBase.SetLevel(GetLevel());
515 rTOXBase.SetFromObjectNames(IsCreateFromObjectNames());
516 rTOXBase.SetSequenceName(GetSequenceName());
517 rTOXBase.SetCaptionDisplay(GetCaptionDisplay());
518 rTOXBase.SetFromChapter(IsFromChapter());
519 rTOXBase.SetProtected(IsReadonly());
520 rTOXBase.SetOLEOptions(GetOLEOptions());
521 rTOXBase.SetLevelFromChapter(IsLevelFromChapter());
522 rTOXBase.SetLanguage(eLanguage);
523 rTOXBase.SetSortAlgorithm(sSortAlgorithm);