xref: /trunk/main/sw/source/ui/index/toxmgr.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 #include <wrtsh.hxx>
32 #include <shellres.hxx>
33 #include <swwait.hxx>
34 #include <view.hxx>
35 #include <toxmgr.hxx>
36 #include <crsskip.hxx>
37 #include <doc.hxx>
38 #include <IDocumentUndoRedo.hxx>
39 #include <swundo.hxx>
40 #include <globals.hrc>
41 
42 /*--------------------------------------------------------------------
43     Beschreibung: Handhabung der Verzeichnisse durch TOXMgr
44  --------------------------------------------------------------------*/
45 
46 
47 SwTOXMgr::SwTOXMgr(SwWrtShell* pShell):
48     pSh(pShell)
49 {
50     GetTOXMarks();
51     SetCurTOXMark(0);
52 }
53 
54 /*--------------------------------------------------------------------
55     Beschreibung: Aktuelle TOXMarks behandeln
56  --------------------------------------------------------------------*/
57 
58 
59 sal_uInt16 SwTOXMgr::GetTOXMarks()
60 {
61     return pSh->GetCurTOXMarks(aCurMarks);
62 }
63 
64 
65 SwTOXMark* SwTOXMgr::GetTOXMark(sal_uInt16 nId)
66 {
67     if(aCurMarks.Count() > 0)
68         return aCurMarks[nId];
69     return 0;
70 }
71 
72 
73 void SwTOXMgr::DeleteTOXMark()
74 {
75     SwTOXMark* pNext = 0;
76     if( pCurTOXMark )
77     {
78         pNext = (SwTOXMark*)&pSh->GotoTOXMark( *pCurTOXMark, TOX_NXT );
79         if( pNext == pCurTOXMark )
80             pNext = 0;
81 
82         pSh->DeleteTOXMark( pCurTOXMark );
83         pSh->SetModified();
84     }
85     // zur naechsten wandern
86     pCurTOXMark = pNext;
87 }
88 /* -----------------20.08.99 10:48-------------------
89 
90  --------------------------------------------------*/
91 void    SwTOXMgr::InsertTOXMark(const SwTOXMarkDescription& rDesc)
92 {
93     SwTOXMark* pMark = 0;
94     switch(rDesc.GetTOXType())
95     {
96         case  TOX_CONTENT:
97         {
98             ASSERT(rDesc.GetLevel() > 0 && rDesc.GetLevel() <= MAXLEVEL,
99                                             ungueltiger Level InsertTOCMark);
100             pMark = new SwTOXMark(pSh->GetTOXType(TOX_CONTENT, 0));
101             pMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
102 
103             if(rDesc.GetAltStr())
104                 pMark->SetAlternativeText(*rDesc.GetAltStr());
105         }
106         break;
107         case  TOX_INDEX:
108         {
109             pMark = new SwTOXMark(pSh->GetTOXType(TOX_INDEX, 0));
110 
111             if( rDesc.GetPrimKey() && rDesc.GetPrimKey()->Len() )
112             {
113                 pMark->SetPrimaryKey( *rDesc.GetPrimKey() );
114                 if(rDesc.GetPhoneticReadingOfPrimKey())
115                     pMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
116 
117                 if( rDesc.GetSecKey() && rDesc.GetSecKey()->Len() )
118                 {
119                     pMark->SetSecondaryKey( *rDesc.GetSecKey() );
120                     if(rDesc.GetPhoneticReadingOfSecKey())
121                         pMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
122                 }
123             }
124             if(rDesc.GetAltStr())
125                 pMark->SetAlternativeText(*rDesc.GetAltStr());
126             if(rDesc.GetPhoneticReadingOfAltStr())
127                 pMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
128             pMark->SetMainEntry(rDesc.IsMainEntry());
129         }
130         break;
131         case  TOX_USER:
132         {
133             ASSERT(rDesc.GetLevel() > 0 && rDesc.GetLevel() <= MAXLEVEL,
134                                             ungueltiger Level InsertTOCMark);
135             sal_uInt16 nId = rDesc.GetTOUName() ?
136                 GetUserTypeID(*rDesc.GetTOUName()) : 0;
137             pMark = new SwTOXMark(pSh->GetTOXType(TOX_USER, nId));
138             pMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
139 
140             if(rDesc.GetAltStr())
141                 pMark->SetAlternativeText(*rDesc.GetAltStr());
142         }
143         break;
144         default:; //prevent warning
145     }
146     pSh->StartAllAction();
147     pSh->SwEditShell::Insert(*pMark);
148     pSh->EndAllAction();
149 }
150 /*--------------------------------------------------------------------
151     Beschreibung: Update eines TOXMarks
152  --------------------------------------------------------------------*/
153 
154 
155 void SwTOXMgr::UpdateTOXMark(const SwTOXMarkDescription& rDesc)
156 {
157     ASSERT(pCurTOXMark, "kein aktuelles TOXMark");
158 
159     pSh->StartAllAction();
160     if(pCurTOXMark->GetTOXType()->GetType() == TOX_INDEX)
161     {
162         if(rDesc.GetPrimKey() && rDesc.GetPrimKey()->Len() )
163         {
164             pCurTOXMark->SetPrimaryKey( *rDesc.GetPrimKey() );
165             if(rDesc.GetPhoneticReadingOfPrimKey())
166                 pCurTOXMark->SetPrimaryKeyReading( *rDesc.GetPhoneticReadingOfPrimKey() );
167             else
168                 pCurTOXMark->SetPrimaryKeyReading( aEmptyStr );
169 
170             if( rDesc.GetSecKey() && rDesc.GetSecKey()->Len() )
171             {
172                 pCurTOXMark->SetSecondaryKey( *rDesc.GetSecKey() );
173                 if(rDesc.GetPhoneticReadingOfSecKey())
174                     pCurTOXMark->SetSecondaryKeyReading( *rDesc.GetPhoneticReadingOfSecKey() );
175                 else
176                     pCurTOXMark->SetSecondaryKeyReading( aEmptyStr );
177             }
178             else
179             {
180                 pCurTOXMark->SetSecondaryKey( aEmptyStr );
181                 pCurTOXMark->SetSecondaryKeyReading( aEmptyStr );
182             }
183         }
184         else
185         {
186             pCurTOXMark->SetPrimaryKey( aEmptyStr );
187             pCurTOXMark->SetPrimaryKeyReading( aEmptyStr );
188             pCurTOXMark->SetSecondaryKey( aEmptyStr );
189             pCurTOXMark->SetSecondaryKeyReading( aEmptyStr );
190         }
191         if(rDesc.GetPhoneticReadingOfAltStr())
192             pCurTOXMark->SetTextReading( *rDesc.GetPhoneticReadingOfAltStr() );
193         else
194             pCurTOXMark->SetTextReading( aEmptyStr );
195         pCurTOXMark->SetMainEntry(rDesc.IsMainEntry());
196     }
197     else
198         pCurTOXMark->SetLevel( static_cast< sal_uInt16 >(rDesc.GetLevel()) );
199 
200     if(rDesc.GetAltStr())
201     {
202         // JP 26.08.96: Bug 30344 - entweder der Text aus dem Doc oder
203         //                          ein Alternativ-Text, beides gibts nicht!
204         sal_Bool bReplace = pCurTOXMark->IsAlternativeText();
205         if( bReplace )
206             pCurTOXMark->SetAlternativeText( *rDesc.GetAltStr() );
207         else
208         {
209             SwTOXMark aCpy( *pCurTOXMark );
210             aCurMarks.Remove(0, aCurMarks.Count());
211             pSh->DeleteTOXMark(pCurTOXMark);
212             aCpy.SetAlternativeText( *rDesc.GetAltStr() );
213             pSh->SwEditShell::Insert( aCpy );
214             pCurTOXMark = 0;
215         }
216     }
217     pSh->SetModified();
218     pSh->EndAllAction();
219     // Bug 36207 pCurTOXMark zeigt hier in den Wald!
220     if(!pCurTOXMark)
221     {
222         pSh->Left(CRSR_SKIP_CHARS, sal_False, 1, sal_False );
223         pSh->GetCurTOXMarks(aCurMarks);
224         SetCurTOXMark(0);
225     }
226 }
227 
228 
229 /*--------------------------------------------------------------------
230     Beschreibung:   UserTypeID ermitteln
231  --------------------------------------------------------------------*/
232 
233 
234 sal_uInt16 SwTOXMgr::GetUserTypeID(const String& rStr)
235 {
236     sal_uInt16 nSize = pSh->GetTOXTypeCount(TOX_USER);
237     for(sal_uInt16 i=0; i < nSize; ++i)
238     {
239         const SwTOXType* pTmp = pSh->GetTOXType(TOX_USER, i);
240         if(pTmp && pTmp->GetTypeName() == rStr)
241             return i;
242     }
243     SwTOXType aUserType(TOX_USER, rStr);
244     pSh->InsertTOXType(aUserType);
245     return nSize;
246 }
247 
248 /*--------------------------------------------------------------------
249     Beschreibung: Traveling zwischen den TOXMarks
250  --------------------------------------------------------------------*/
251 
252 
253 void SwTOXMgr::NextTOXMark(sal_Bool bSame)
254 {
255     ASSERT(pCurTOXMark, "kein aktuelles TOXMark");
256     if( pCurTOXMark )
257     {
258         SwTOXSearch eDir = bSame ? TOX_SAME_NXT : TOX_NXT;
259         pCurTOXMark = (SwTOXMark*)&pSh->GotoTOXMark( *pCurTOXMark, eDir );
260     }
261 }
262 
263 
264 void SwTOXMgr::PrevTOXMark(sal_Bool bSame)
265 {
266     ASSERT(pCurTOXMark, "kein aktuelles TOXMark");
267     if( pCurTOXMark )
268     {
269         SwTOXSearch eDir = bSame ? TOX_SAME_PRV : TOX_PRV;
270         pCurTOXMark = (SwTOXMark*)&pSh->GotoTOXMark(*pCurTOXMark, eDir );
271     }
272 }
273 
274 /*--------------------------------------------------------------------
275     Beschreibung: Stichwortverzeichnis einfuegen
276  --------------------------------------------------------------------*/
277 const SwTOXBase* SwTOXMgr::GetCurTOX()
278 {
279     return pSh->GetCurTOX();
280 }
281 
282 const SwTOXType* SwTOXMgr::GetTOXType(TOXTypes eTyp, sal_uInt16 nId) const
283 {
284     return pSh->GetTOXType(eTyp, nId);
285 }
286 
287 void SwTOXMgr::SetCurTOXMark(sal_uInt16 nId)
288 {
289     pCurTOXMark = (nId < aCurMarks.Count()) ? aCurMarks[nId] : 0;
290 }
291 
292 /* -----------------01.07.99 16:23-------------------
293 
294  --------------------------------------------------*/
295 
296 sal_Bool SwTOXMgr::UpdateOrInsertTOX(const SwTOXDescription& rDesc,
297                                     SwTOXBase** ppBase,
298                                     const SfxItemSet* pSet)
299 {
300     SwWait aWait( *pSh->GetView().GetDocShell(), sal_True );
301     sal_Bool bRet = sal_True;
302     const SwTOXBase* pCurTOX = ppBase && *ppBase ? *ppBase : GetCurTOX();
303     SwTOXBase* pTOX = (SwTOXBase*)pCurTOX;
304 
305     SwTOXBase * pNewTOX = NULL;
306 
307     if (pTOX)
308         pNewTOX = new SwTOXBase(*pTOX);
309 
310     TOXTypes eCurTOXType = rDesc.GetTOXType();
311     if(pCurTOX && !ppBase && pSh->HasSelection())
312         pSh->EnterStdMode();
313 
314     switch(eCurTOXType)
315     {
316         case TOX_INDEX :
317         {
318             if(!pCurTOX || (ppBase && !(*ppBase)))
319             {
320                 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
321                 SwForm aForm(eCurTOXType);
322                 pNewTOX = new SwTOXBase(pType, aForm, nsSwTOXElement::TOX_MARK, pType->GetTypeName());
323             }
324             pNewTOX->SetOptions(rDesc.GetIndexOptions());
325             pNewTOX->SetMainEntryCharStyle(rDesc.GetMainEntryCharStyle());
326             pSh->SetTOIAutoMarkURL(rDesc.GetAutoMarkURL());
327             pSh->ApplyAutoMark();
328         }
329         break;
330         case TOX_CONTENT :
331         {
332             if(!pCurTOX || (ppBase && !(*ppBase)))
333             {
334                 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
335                 SwForm aForm(eCurTOXType);
336                 pNewTOX = new SwTOXBase(pType, aForm, rDesc.GetContentOptions(), pType->GetTypeName());
337             }
338             pNewTOX->SetCreate(rDesc.GetContentOptions());
339             pNewTOX->SetLevel(rDesc.GetLevel());
340         }
341         break;
342         case TOX_USER :
343         {
344             if(!pCurTOX || (ppBase && !(*ppBase)))
345             {
346                 sal_uInt16 nPos  = 0;
347                 sal_uInt16 nSize = pSh->GetTOXTypeCount(eCurTOXType);
348                 for(sal_uInt16 i=0; rDesc.GetTOUName() && i < nSize; ++i)
349                 {   const SwTOXType* pType = pSh->GetTOXType(TOX_USER, i);
350                     if(pType->GetTypeName() == *rDesc.GetTOUName())
351                     {   nPos = i;
352                         break;
353                     }
354                 }
355                 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, nPos);
356 
357                 SwForm aForm(eCurTOXType);
358                 pNewTOX = new SwTOXBase(pType, aForm, rDesc.GetContentOptions(), pType->GetTypeName());
359 
360             }
361             else
362             {
363                 const_cast<SwTOXBase*>( pCurTOX )->SetCreate(rDesc.GetContentOptions());
364             }
365             pNewTOX->SetLevelFromChapter(rDesc.IsLevelFromChapter());
366         }
367         break;
368         case TOX_OBJECTS:
369         case TOX_TABLES:
370         case TOX_AUTHORITIES:
371         case TOX_ILLUSTRATIONS:
372         {
373             //Special handling for TOX_AUTHORITY
374             if(TOX_AUTHORITIES == eCurTOXType)
375             {
376                 SwAuthorityFieldType* pFType = (SwAuthorityFieldType*)
377                                                 pSh->GetFldType(RES_AUTHORITY, aEmptyStr);
378                 if(!pFType)
379                     pFType = (SwAuthorityFieldType*)pSh->InsertFldType(
380                                     SwAuthorityFieldType(pSh->GetDoc()));
381                 pFType->SetPreSuffix(rDesc.GetAuthBrackets().GetChar(0),
382                     rDesc.GetAuthBrackets().GetChar(1));
383                 pFType->SetSequence(rDesc.IsAuthSequence());
384                 SwTOXSortKey rArr[3];
385                 rArr[0] = rDesc.GetSortKey1();
386                 rArr[1] = rDesc.GetSortKey2();
387                 rArr[2] = rDesc.GetSortKey3();
388                 pFType->SetSortKeys(3, rArr);
389                 pFType->SetSortByDocument(rDesc.IsSortByDocument());
390                 pFType->SetLanguage(rDesc.GetLanguage());
391                 pFType->SetSortAlgorithm(rDesc.GetSortAlgorithm());
392 
393                 pFType->UpdateFlds();
394             }
395             // TODO: consider properties of the current TOXType
396             if(!pCurTOX || (ppBase && !(*ppBase)))
397             {
398                 const SwTOXType* pType = pSh->GetTOXType(eCurTOXType, 0);
399                 SwForm aForm(eCurTOXType);
400                 pNewTOX = new SwTOXBase(
401                     pType, aForm,
402                     TOX_AUTHORITIES == eCurTOXType ? nsSwTOXElement::TOX_MARK : 0, pType->GetTypeName());
403             }
404             else
405             {
406                 if((!ppBase || !(*ppBase)) && pSh->HasSelection())
407                     pSh->DelRight();
408                 pNewTOX = (SwTOXBase*)pCurTOX;
409             }
410 //          pTOX->SetOptions(rDesc.GetIndexOptions());
411             pNewTOX->SetFromObjectNames(rDesc.IsCreateFromObjectNames());
412             pNewTOX->SetOLEOptions(rDesc.GetOLEOptions());
413         }
414         break;
415     }
416 
417 
418     DBG_ASSERT(pNewTOX, "no TOXBase created!" );
419     if(!pNewTOX)
420         return sal_False;
421 
422     pNewTOX->SetFromChapter(rDesc.IsFromChapter());
423     pNewTOX->SetSequenceName(rDesc.GetSequenceName());
424     pNewTOX->SetCaptionDisplay(rDesc.GetCaptionDisplay());
425     pNewTOX->SetProtected(rDesc.IsReadonly());
426 
427     for(sal_uInt16 nLevel = 0; nLevel < MAXLEVEL; nLevel++)
428         pNewTOX->SetStyleNames(rDesc.GetStyleNames(nLevel), nLevel);
429 
430     if(rDesc.GetTitle())
431         pNewTOX->SetTitle(*rDesc.GetTitle());
432     if(rDesc.GetForm())
433         pNewTOX->SetTOXForm(*rDesc.GetForm());
434     pNewTOX->SetLanguage(rDesc.GetLanguage());
435     pNewTOX->SetSortAlgorithm(rDesc.GetSortAlgorithm());
436 
437     if(!pCurTOX || (ppBase && !(*ppBase)) )
438     {
439         // wird ppBase uebergeben, dann wird das TOXBase hier nur erzeugt
440         // und dann ueber den Dialog in ein Globaldokument eingefuegt
441         if(ppBase)
442             (*ppBase) = pNewTOX;
443         else
444         {
445             pSh->InsertTableOf(*pNewTOX, pSet);
446             delete pNewTOX;
447         }
448     }
449     else
450     {
451         SwDoc * pDoc = pSh->GetDoc();
452 
453         if (pDoc->GetIDocumentUndoRedo().DoesUndo())
454         {
455             if (pNewTOX != NULL)
456             {
457                 pDoc->GetIDocumentUndoRedo().DelAllUndoObj();
458             }
459 
460             pDoc->GetIDocumentUndoRedo().StartUndo(UNDO_TOXCHANGE, NULL);
461         }
462 
463         if (pNewTOX != NULL) // => pTOX != NULL
464             pDoc->ChgTOX(*pTOX, *pNewTOX);
465 
466         bRet = pSh->UpdateTableOf(*pTOX, pSet);
467 
468         if (pDoc->GetIDocumentUndoRedo().DoesUndo())
469         {
470             pDoc->GetIDocumentUndoRedo().EndUndo(UNDO_TOXCHANGE, NULL);
471 
472             if (pNewTOX == NULL)
473             {
474                 pDoc->GetIDocumentUndoRedo().DelAllUndoObj();
475             }
476         }
477     }
478 
479     return bRet;
480 }
481 /* -----------------20.10.99 14:11-------------------
482 
483  --------------------------------------------------*/
484 void SwTOXDescription::SetSortKeys(SwTOXSortKey eKey1,
485                         SwTOXSortKey eKey2,
486                             SwTOXSortKey eKey3)
487 {
488     SwTOXSortKey aArr[3];
489     sal_uInt16 nPos = 0;
490     if(AUTH_FIELD_END > eKey1.eField)
491         aArr[nPos++] = eKey1;
492     if(AUTH_FIELD_END > eKey2.eField)
493         aArr[nPos++] = eKey2;
494     if(AUTH_FIELD_END > eKey3.eField)
495         aArr[nPos++] = eKey3;
496 
497     eSortKey1 = aArr[0];
498     eSortKey2 = aArr[1];
499     eSortKey3 = aArr[2];
500 }
501 
502 /* -----------------10.09.99 10:02-------------------
503 
504  --------------------------------------------------*/
505 void SwTOXDescription::ApplyTo(SwTOXBase& rTOXBase)
506 {
507     for(sal_uInt16 i = 0; i < MAXLEVEL; i++)
508         rTOXBase.SetStyleNames(GetStyleNames(i), i);
509     rTOXBase.SetTitle(GetTitle() ? *GetTitle() : aEmptyStr);
510     rTOXBase.SetCreate(GetContentOptions());
511 
512     if(GetTOXType() == TOX_INDEX)
513         rTOXBase.SetOptions(GetIndexOptions());
514     if(GetTOXType() != TOX_INDEX)
515         rTOXBase.SetLevel(GetLevel());
516     rTOXBase.SetFromObjectNames(IsCreateFromObjectNames());
517     rTOXBase.SetSequenceName(GetSequenceName());
518     rTOXBase.SetCaptionDisplay(GetCaptionDisplay());
519     rTOXBase.SetFromChapter(IsFromChapter());
520     rTOXBase.SetProtected(IsReadonly());
521     rTOXBase.SetOLEOptions(GetOLEOptions());
522     rTOXBase.SetLevelFromChapter(IsLevelFromChapter());
523     rTOXBase.SetLanguage(eLanguage);
524     rTOXBase.SetSortAlgorithm(sSortAlgorithm);
525 
526 }
527 
528 
529 
530