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