merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / edit / edtox.cxx
blobe64157ee6a48662fd9b27f5e1765022e78b9307e
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: edtox.cxx,v $
10 * $Revision: 1.19 $
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 <com/sun/star/util/SearchOptions.hpp>
34 #include <com/sun/star/util/SearchFlags.hpp>
35 #include <com/sun/star/i18n/TransliterationModules.hpp>
38 #include <tools/urlobj.hxx>
39 #include <svtools/txtcmp.hxx>
40 #ifndef SVTOOLS_FSTATHELPER_HXX
41 #include <svtools/fstathelper.hxx>
42 #endif
43 #include <sfx2/docfile.hxx>
44 #include "svx/unolingu.hxx"
45 #include <swtypes.hxx>
46 #include <editsh.hxx>
47 #include <doc.hxx>
48 #include <pam.hxx>
49 #include <viewopt.hxx>
50 #include <ndtxt.hxx>
51 #include <errhdl.hxx>
52 #include <swundo.hxx>
53 #include <undobj.hxx>
54 #include <txttxmrk.hxx>
55 #include <edimp.hxx>
56 #include <tox.hxx>
57 #include <doctxm.hxx>
58 #include <docary.hxx>
59 #include <mdiexp.hxx>
60 #ifndef _STATSTR_HRC
61 #include <statstr.hrc>
62 #endif
63 #include <bookmrk.hxx>
64 #include <ecmaflds.hxx>
67 using namespace ::com::sun::star;
68 using namespace ::com::sun::star::i18n;
69 using namespace ::com::sun::star::lang;
70 using namespace ::com::sun::star::util;
72 /*--------------------------------------------------------------------
73 Beschreibung: Verzeichnismarkierung ins Dokument einfuegen/loeschen
74 --------------------------------------------------------------------*/
77 void SwEditShell::Insert(const SwTOXMark& rMark)
79 BOOL bInsAtPos = rMark.IsAlternativeText();
80 StartAllAction();
81 FOREACHPAM_START(this)
83 const SwPosition *pStt = PCURCRSR->Start(),
84 *pEnd = PCURCRSR->End();
85 if( bInsAtPos )
87 SwPaM aTmp( *pStt );
88 GetDoc()->InsertPoolItem( aTmp, rMark, 0 );
90 else if( *pEnd != *pStt )
92 GetDoc()->InsertPoolItem( *PCURCRSR, rMark,
93 nsSetAttrMode::SETATTR_DONTEXPAND );
96 FOREACHPAM_END()
97 EndAllAction();
102 void SwEditShell::DeleteTOXMark( SwTOXMark* pMark )
104 SET_CURR_SHELL( this );
105 StartAllAction();
107 pDoc->DeleteTOXMark( pMark );
109 EndAllAction();
113 /*--------------------------------------------------------------------
114 Beschreibung: Alle Verzeichnismarkierungen am SPoint zusammensuchen
115 --------------------------------------------------------------------*/
117 USHORT SwEditShell::GetCurTOXMarks(SwTOXMarks& rMarks) const
119 return GetDoc()->GetCurTOXMark( *GetCrsr()->Start(), rMarks );
122 /* -----------------01.09.99 16:05-------------------
124 --------------------------------------------------*/
125 BOOL SwEditShell::IsTOXBaseReadonly(const SwTOXBase& rTOXBase) const
127 ASSERT( rTOXBase.ISA( SwTOXBaseSection ), "no TOXBaseSection!" );
128 const SwTOXBaseSection& rTOXSect = (const SwTOXBaseSection&)rTOXBase;
129 return rTOXSect.IsProtect();
131 /* -----------------18.10.99 15:53-------------------
133 --------------------------------------------------*/
134 void SwEditShell::SetTOXBaseReadonly(const SwTOXBase& rTOXBase, BOOL bReadonly)
136 ASSERT( rTOXBase.ISA( SwTOXBaseSection ), "no TOXBaseSection!" );
137 const SwTOXBaseSection& rTOXSect = (const SwTOXBaseSection&)rTOXBase;
138 ((SwTOXBase&)rTOXBase).SetProtected(bReadonly);
139 ASSERT( rTOXSect.SwSection::GetType() == TOX_CONTENT_SECTION, "not a TOXContentSection" );
141 SwSection aSect(TOX_CONTENT_SECTION, rTOXSect.GetName());
142 aSect = rTOXSect;
143 aSect.SetProtect(bReadonly);
144 ChgSection( GetSectionFmtPos( *rTOXSect.GetFmt() ), aSect, 0 );
147 /* -----------------02.09.99 07:47-------------------
149 --------------------------------------------------*/
150 const SwTOXBase* SwEditShell::GetDefaultTOXBase( TOXTypes eTyp, BOOL bCreate )
152 return GetDoc()->GetDefaultTOXBase( eTyp, bCreate );
154 /* -----------------02.09.99 08:05-------------------
156 --------------------------------------------------*/
157 void SwEditShell::SetDefaultTOXBase(const SwTOXBase& rBase)
159 GetDoc()->SetDefaultTOXBase(rBase);
162 /*--------------------------------------------------------------------
163 Beschreibung: Verzeichnis einfuegen, und Inhalt erzeugen
164 --------------------------------------------------------------------*/
166 void SwEditShell::InsertTableOf( const SwTOXBase& rTOX, const SfxItemSet* pSet )
168 SET_CURR_SHELL( this );
169 StartAllAction();
171 SwDocShell* pDocSh = GetDoc()->GetDocShell();
172 ::StartProgress( STR_STATSTR_TOX_INSERT, 0, 0, pDocSh );
173 ::SetProgressText( STR_STATSTR_TOX_INSERT, pDocSh );
175 // Einfuegen des Verzeichnisses
176 const SwTOXBaseSection* pTOX = pDoc->InsertTableOf(
177 *GetCrsr()->GetPoint(), rTOX, pSet, TRUE );
178 ASSERT(pTOX, "Kein aktuelles Verzeichnis");
180 // Formatierung anstossen
181 CalcLayout();
183 // Seitennummern eintragen
184 ((SwTOXBaseSection*)pTOX)->UpdatePageNum();
186 pTOX->SetPosAtStartEnd( *GetCrsr()->GetPoint() );
188 // Fix fuer leere Verzeichnisse
189 InvalidateWindows( aVisArea );
190 ::EndProgress( pDocSh );
191 EndAllAction();
194 /*--------------------------------------------------------------------
195 Beschreibung: Verzeichnisinhalt erneuern
196 --------------------------------------------------------------------*/
198 BOOL SwEditShell::UpdateTableOf( const SwTOXBase& rTOX, const SfxItemSet* pSet )
200 BOOL bRet = FALSE;
202 ASSERT( rTOX.ISA( SwTOXBaseSection ), "keine TOXBaseSection!" );
203 SwTOXBaseSection* pTOX = (SwTOXBaseSection*)&rTOX;
204 ASSERT(pTOX, "Keine aktuelles Verzeichnis");
205 const SwSectionNode* pSectNd;
206 if( pTOX && 0 != ( pSectNd = pTOX->GetFmt()->GetSectionNode() ) )
208 SwDoc* pMyDoc = GetDoc();
209 SwDocShell* pDocSh = pMyDoc->GetDocShell();
211 BOOL bInIndex = pTOX == GetCurTOX();
212 SET_CURR_SHELL( this );
213 StartAllAction();
215 ::StartProgress( STR_STATSTR_TOX_UPDATE, 0, 0, pDocSh );
216 ::SetProgressText( STR_STATSTR_TOX_UPDATE, pDocSh );
218 pMyDoc->StartUndo(UNDO_TOXCHANGE, NULL);
220 // Verzeichnisrumpf erzeugen
221 pTOX->Update(pSet);
223 // Cursor korrigieren
224 if( bInIndex )
225 pTOX->SetPosAtStartEnd( *GetCrsr()->GetPoint() );
227 // Formatierung anstossen
228 CalcLayout();
230 // Seitennummern eintragen
231 pTOX->UpdatePageNum();
233 pMyDoc->EndUndo(UNDO_TOXCHANGE, NULL);
235 ::EndProgress( pDocSh );
236 EndAllAction();
238 return bRet;
241 BOOL SwEditShell::UpdateField( sw::mark::IFieldmark &fieldBM)
243 // SwDocShell* pDocSh = pDoc->GetDocShell();
244 //@TODO implement me...; add undo etc...
245 if ( pDoc && fieldBM.IsExpanded( ) ) {
246 SwPosition aSttPos = fieldBM.GetMarkStart( );
247 aSttPos.nContent++;
249 SwPosition aEndPos = fieldBM.GetMarkEnd( );
250 aEndPos.nContent--;
252 SwPaM aPaM( aSttPos, aEndPos );
253 pDoc->DeleteRange(aPaM);
254 pDoc->InsertString(aPaM, String::CreateFromAscii("Implement me ;-)") );
256 return TRUE;
260 /*--------------------------------------------------------------------
261 Beschreibung: Aktuelles Verzeichnis vor oder in dem der Cursor
262 steht
263 --------------------------------------------------------------------*/
265 const SwTOXBase* SwEditShell::GetCurTOX() const
267 return GetDoc()->GetCurTOX( *GetCrsr()->GetPoint() );
270 BOOL SwEditShell::DeleteTOX( const SwTOXBase& rTOXBase, BOOL bDelNodes )
272 return GetDoc()->DeleteTOX( (SwTOXBase&)rTOXBase, bDelNodes );
275 /*--------------------------------------------------------------------
276 Beschreibung: Typen der Verzeichnisse verwalten
277 --------------------------------------------------------------------*/
279 const SwTOXType* SwEditShell::GetTOXType(TOXTypes eTyp, USHORT nId) const
281 return pDoc->GetTOXType(eTyp, nId);
284 /*--------------------------------------------------------------------
285 Beschreibung: Schluessel fuer Stichwortverzeichnisse verwalten
286 --------------------------------------------------------------------*/
288 USHORT SwEditShell::GetTOIKeys( SwTOIKeyType eTyp, SvStringsSort& rArr ) const
290 return GetDoc()->GetTOIKeys( eTyp, rArr );
294 USHORT SwEditShell::GetTOXCount() const
296 const SwSectionFmts& rFmts = GetDoc()->GetSections();
297 USHORT nRet = 0;
298 for( USHORT n = rFmts.Count(); n; )
300 const SwSection* pSect = rFmts[ --n ]->GetSection();
301 if( TOX_CONTENT_SECTION == pSect->GetType() &&
302 pSect->GetFmt()->GetSectionNode() )
303 ++nRet;
305 return nRet;
309 const SwTOXBase* SwEditShell::GetTOX( USHORT nPos ) const
311 const SwSectionFmts& rFmts = GetDoc()->GetSections();
312 for( USHORT n = 0, nCnt = 0; n < rFmts.Count(); ++n )
314 const SwSection* pSect = rFmts[ n ]->GetSection();
315 if( TOX_CONTENT_SECTION == pSect->GetType() &&
316 pSect->GetFmt()->GetSectionNode() &&
317 nCnt++ == nPos )
319 ASSERT( pSect->ISA( SwTOXBaseSection ), "keine TOXBaseSection!" );
320 return (SwTOXBaseSection*)pSect;
323 return 0;
327 // nach einlesen einer Datei alle Verzeichnisse updaten
328 void SwEditShell::SetUpdateTOX( BOOL bFlag )
330 GetDoc()->SetUpdateTOX( bFlag );
334 BOOL SwEditShell::IsUpdateTOX() const
336 return GetDoc()->IsUpdateTOX();
339 /* -----------------26.08.99 13:49-------------------
341 --------------------------------------------------*/
342 const String& SwEditShell::GetTOIAutoMarkURL() const
344 return GetDoc()->GetTOIAutoMarkURL();
346 /* -----------------26.08.99 13:49-------------------
348 --------------------------------------------------*/
349 void SwEditShell::SetTOIAutoMarkURL(const String& rSet)
351 GetDoc()->SetTOIAutoMarkURL(rSet);
353 /* -----------------26.08.99 09:29-------------------
355 --------------------------------------------------*/
356 void SwEditShell::ApplyAutoMark()
358 StartAllAction();
359 BOOL bDoesUndo = DoesUndo();
360 DoUndo(FALSE);
361 //1. remove all automatic generated index entries if AutoMarkURL has a
362 // length and the file exists
363 //2. load file
364 //3. select all occurrences of the searched words
365 //4. apply index entries
367 String sAutoMarkURL(GetDoc()->GetTOIAutoMarkURL());
368 if( sAutoMarkURL.Len() && FStatHelper::IsDocument( sAutoMarkURL ))
370 //1.
371 const SwTOXType* pTOXType = GetTOXType(TOX_INDEX, 0);
372 SwClientIter aIter(*(SwTOXType*)pTOXType);
373 SwTOXMark* pMark = (SwTOXMark*)aIter.First(TYPE(SwTOXMark));
374 while( pMark )
376 if(pMark->IsAutoGenerated() && pMark->GetTxtTOXMark())
377 DeleteTOXMark(pMark);
378 pMark = (SwTOXMark*)aIter.Next();
381 //2.
382 SfxMedium aMedium( sAutoMarkURL, STREAM_STD_READ, TRUE );
383 SvStream& rStrm = *aMedium.GetInStream();
384 const String sZero('0');
385 Push();
386 rtl_TextEncoding eChrSet = ::gsl_getSystemTextEncoding();
389 // SearchOptions to be used in loop below
391 //SearchAlgorithms eSrchType = SearchAlgorithms_ABSOLUTE;
392 //OUString aSrchStr = rText;
393 BOOL bCaseSensitive = TRUE;
394 BOOL bWordOnly = FALSE;
395 BOOL bSrchInSel = FALSE;
396 BOOL bLEV_Relaxed = TRUE;
397 INT32 nLEV_Other = 2; // -> changedChars;
398 INT32 nLEV_Longer = 3; //! -> deletedChars;
399 INT32 nLEV_Shorter = 1; //! -> insertedChars;
400 INT32 nTransliterationFlags = 0;
402 INT32 nSrchFlags = 0;
403 if (!bCaseSensitive)
405 nSrchFlags |= SearchFlags::ALL_IGNORE_CASE;
406 nTransliterationFlags |= TransliterationModules_IGNORE_CASE;
408 if ( bWordOnly)
409 nSrchFlags |= SearchFlags::NORM_WORD_ONLY;
410 if ( bLEV_Relaxed)
411 nSrchFlags |= SearchFlags::LEV_RELAXED;
412 if ( bSrchInSel)
413 nSrchFlags |= (SearchFlags::REG_NOT_BEGINOFLINE |
414 SearchFlags::REG_NOT_ENDOFLINE );
416 rtl::OUString sEmpty;
417 SearchOptions aSearchOpt(
418 SearchAlgorithms_ABSOLUTE, nSrchFlags,
419 sEmpty, sEmpty,
420 SvxCreateLocale( LANGUAGE_SYSTEM ),
421 nLEV_Other, nLEV_Longer, nLEV_Shorter,
422 nTransliterationFlags );
424 while( !rStrm.GetError() && !rStrm.IsEof() )
426 ByteString aRdLine;
427 rStrm.ReadLine( aRdLine );
429 // # -> comment
430 // ; -> delimiter between entries ->
431 // Format: TextToSearchFor;AlternativeString;PrimaryKey;SecondaryKey;CaseSensitive;WordOnly
432 // Leading and trailing blanks are ignored
433 if( aRdLine.Len() && '#' != aRdLine.GetChar(0) )
435 String sLine( aRdLine, eChrSet );
437 xub_StrLen nTokenPos = 0;
438 String sToSelect( sLine.GetToken(0, ';', nTokenPos ) );
439 if( sToSelect.Len() )
441 String sAlternative = sLine.GetToken(0, ';', nTokenPos);
442 String sPrimary = sLine.GetToken(0, ';', nTokenPos);
443 String sSecondary = sLine.GetToken(0, ';', nTokenPos);
444 String sCase = sLine.GetToken(0, ';', nTokenPos);
445 String sWordOnly = sLine.GetToken(0, ';', nTokenPos);
447 //3.
448 bCaseSensitive = sCase.Len() && sCase != sZero;
449 bWordOnly = sWordOnly.Len() && sWordOnly != sZero;
451 if (!bCaseSensitive)
453 //nSrchFlags |= SearchFlags::ALL_IGNORE_CASE;
454 aSearchOpt.transliterateFlags |=
455 TransliterationModules_IGNORE_CASE;
457 else
459 //aSearchOpt.searchFlag &= ~SearchFlags::ALL_IGNORE_CASE;
460 aSearchOpt.transliterateFlags &=
461 ~TransliterationModules_IGNORE_CASE;
463 if ( bWordOnly)
464 aSearchOpt.searchFlag |= SearchFlags::NORM_WORD_ONLY;
465 else
466 aSearchOpt.searchFlag &= ~SearchFlags::NORM_WORD_ONLY;
468 aSearchOpt.searchString = sToSelect;
470 KillPams();
471 BOOL bCancel;
473 // todo/mba: assuming that notes shouldn't be searched
474 BOOL bSearchInNotes = FALSE;
475 ULONG nRet = Find( aSearchOpt, bSearchInNotes, DOCPOS_START, DOCPOS_END, bCancel,
476 (FindRanges)(FND_IN_SELALL|FND_IN_BODYONLY),
477 FALSE );
479 if(nRet)
481 SwTOXMark* pTmpMark = new SwTOXMark(pTOXType);
482 if( sPrimary.Len() )
484 pTmpMark->SetPrimaryKey( sPrimary );
485 if( sSecondary.Len() )
486 pTmpMark->SetSecondaryKey( sSecondary );
488 if(sAlternative.Len())
489 pTmpMark->SetAlternativeText(sAlternative);
490 pTmpMark->SetMainEntry(FALSE);
491 pTmpMark->SetAutoGenerated(TRUE);
492 //4.
493 SwEditShell::Insert(*pTmpMark);
498 KillPams();
499 Pop(FALSE);
501 DoUndo(bDoesUndo);
502 EndAllAction();