1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: edtox.cxx,v $
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>
43 #include <sfx2/docfile.hxx>
44 #include "svx/unolingu.hxx"
45 #include <swtypes.hxx>
49 #include <viewopt.hxx>
54 #include <txttxmrk.hxx>
61 #include <statstr.hrc>
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();
81 FOREACHPAM_START(this)
83 const SwPosition
*pStt
= PCURCRSR
->Start(),
84 *pEnd
= PCURCRSR
->End();
88 GetDoc()->Insert( aTmp
, rMark
, 0 );
90 else if( *pEnd
!= *pStt
)
91 GetDoc()->Insert( *PCURCRSR
, rMark
, nsSetAttrMode::SETATTR_DONTEXPAND
);
99 void SwEditShell::DeleteTOXMark( SwTOXMark
* pMark
)
101 SET_CURR_SHELL( this );
104 pDoc
->Delete( pMark
);
110 /*--------------------------------------------------------------------
111 Beschreibung: Alle Verzeichnismarkierungen am SPoint zusammensuchen
112 --------------------------------------------------------------------*/
114 USHORT
SwEditShell::GetCurTOXMarks(SwTOXMarks
& rMarks
) const
116 return GetDoc()->GetCurTOXMark( *GetCrsr()->Start(), rMarks
);
119 /* -----------------01.09.99 16:05-------------------
121 --------------------------------------------------*/
122 BOOL
SwEditShell::IsTOXBaseReadonly(const SwTOXBase
& rTOXBase
) const
124 ASSERT( rTOXBase
.ISA( SwTOXBaseSection
), "no TOXBaseSection!" );
125 const SwTOXBaseSection
& rTOXSect
= (const SwTOXBaseSection
&)rTOXBase
;
126 return rTOXSect
.IsProtect();
128 /* -----------------18.10.99 15:53-------------------
130 --------------------------------------------------*/
131 void SwEditShell::SetTOXBaseReadonly(const SwTOXBase
& rTOXBase
, BOOL bReadonly
)
133 ASSERT( rTOXBase
.ISA( SwTOXBaseSection
), "no TOXBaseSection!" );
134 const SwTOXBaseSection
& rTOXSect
= (const SwTOXBaseSection
&)rTOXBase
;
135 ((SwTOXBase
&)rTOXBase
).SetProtected(bReadonly
);
136 ASSERT( rTOXSect
.SwSection::GetType() == TOX_CONTENT_SECTION
, "not a TOXContentSection" );
138 SwSection
aSect(TOX_CONTENT_SECTION
, rTOXSect
.GetName());
140 aSect
.SetProtect(bReadonly
);
141 ChgSection( GetSectionFmtPos( *rTOXSect
.GetFmt() ), aSect
, 0 );
144 /* -----------------02.09.99 07:47-------------------
146 --------------------------------------------------*/
147 const SwTOXBase
* SwEditShell::GetDefaultTOXBase( TOXTypes eTyp
, BOOL bCreate
)
149 return GetDoc()->GetDefaultTOXBase( eTyp
, bCreate
);
151 /* -----------------02.09.99 08:05-------------------
153 --------------------------------------------------*/
154 void SwEditShell::SetDefaultTOXBase(const SwTOXBase
& rBase
)
156 GetDoc()->SetDefaultTOXBase(rBase
);
159 /*--------------------------------------------------------------------
160 Beschreibung: Verzeichnis einfuegen, und Inhalt erzeugen
161 --------------------------------------------------------------------*/
163 void SwEditShell::InsertTableOf( const SwTOXBase
& rTOX
, const SfxItemSet
* pSet
)
165 SET_CURR_SHELL( this );
168 SwDocShell
* pDocSh
= GetDoc()->GetDocShell();
169 ::StartProgress( STR_STATSTR_TOX_INSERT
, 0, 0, pDocSh
);
170 ::SetProgressText( STR_STATSTR_TOX_INSERT
, pDocSh
);
172 // Einfuegen des Verzeichnisses
173 const SwTOXBaseSection
* pTOX
= pDoc
->InsertTableOf(
174 *GetCrsr()->GetPoint(), rTOX
, pSet
, TRUE
);
175 ASSERT(pTOX
, "Kein aktuelles Verzeichnis");
177 // Formatierung anstossen
180 // Seitennummern eintragen
181 ((SwTOXBaseSection
*)pTOX
)->UpdatePageNum();
183 pTOX
->SetPosAtStartEnd( *GetCrsr()->GetPoint() );
185 // Fix fuer leere Verzeichnisse
186 InvalidateWindows( aVisArea
);
187 ::EndProgress( pDocSh
);
191 /*--------------------------------------------------------------------
192 Beschreibung: Verzeichnisinhalt erneuern
193 --------------------------------------------------------------------*/
195 BOOL
SwEditShell::UpdateTableOf( const SwTOXBase
& rTOX
, const SfxItemSet
* pSet
)
199 ASSERT( rTOX
.ISA( SwTOXBaseSection
), "keine TOXBaseSection!" );
200 SwTOXBaseSection
* pTOX
= (SwTOXBaseSection
*)&rTOX
;
201 ASSERT(pTOX
, "Keine aktuelles Verzeichnis");
202 const SwSectionNode
* pSectNd
;
203 if( pTOX
&& 0 != ( pSectNd
= pTOX
->GetFmt()->GetSectionNode() ) )
205 SwDoc
* pMyDoc
= GetDoc();
206 SwDocShell
* pDocSh
= pMyDoc
->GetDocShell();
208 BOOL bInIndex
= pTOX
== GetCurTOX();
209 SET_CURR_SHELL( this );
212 ::StartProgress( STR_STATSTR_TOX_UPDATE
, 0, 0, pDocSh
);
213 ::SetProgressText( STR_STATSTR_TOX_UPDATE
, pDocSh
);
215 pMyDoc
->StartUndo(UNDO_TOXCHANGE
, NULL
);
217 // Verzeichnisrumpf erzeugen
220 // Cursor korrigieren
222 pTOX
->SetPosAtStartEnd( *GetCrsr()->GetPoint() );
224 // Formatierung anstossen
227 // Seitennummern eintragen
228 pTOX
->UpdatePageNum();
230 pMyDoc
->EndUndo(UNDO_TOXCHANGE
, NULL
);
232 ::EndProgress( pDocSh
);
238 BOOL
SwEditShell::UpdateField( sw::mark::IFieldmark
&fieldBM
)
240 // SwDocShell* pDocSh = pDoc->GetDocShell();
241 //@TODO implement me...; add undo etc...
242 if ( pDoc
&& fieldBM
.IsExpanded( ) ) {
243 SwPosition aSttPos
= fieldBM
.GetMarkStart( );
246 SwPosition aEndPos
= fieldBM
.GetMarkEnd( );
249 SwPaM
aPaM( aSttPos
, aEndPos
);
251 pDoc
->Insert(aPaM
, String::CreateFromAscii("Implement me ;-)"), false);
257 /*--------------------------------------------------------------------
258 Beschreibung: Aktuelles Verzeichnis vor oder in dem der Cursor
260 --------------------------------------------------------------------*/
262 const SwTOXBase
* SwEditShell::GetCurTOX() const
264 return GetDoc()->GetCurTOX( *GetCrsr()->GetPoint() );
267 BOOL
SwEditShell::DeleteTOX( const SwTOXBase
& rTOXBase
, BOOL bDelNodes
)
269 return GetDoc()->DeleteTOX( (SwTOXBase
&)rTOXBase
, bDelNodes
);
272 /*--------------------------------------------------------------------
273 Beschreibung: Typen der Verzeichnisse verwalten
274 --------------------------------------------------------------------*/
276 const SwTOXType
* SwEditShell::GetTOXType(TOXTypes eTyp
, USHORT nId
) const
278 return pDoc
->GetTOXType(eTyp
, nId
);
281 /*--------------------------------------------------------------------
282 Beschreibung: Schluessel fuer Stichwortverzeichnisse verwalten
283 --------------------------------------------------------------------*/
285 USHORT
SwEditShell::GetTOIKeys( SwTOIKeyType eTyp
, SvStringsSort
& rArr
) const
287 return GetDoc()->GetTOIKeys( eTyp
, rArr
);
291 USHORT
SwEditShell::GetTOXCount() const
293 const SwSectionFmts
& rFmts
= GetDoc()->GetSections();
295 for( USHORT n
= rFmts
.Count(); n
; )
297 const SwSection
* pSect
= rFmts
[ --n
]->GetSection();
298 if( TOX_CONTENT_SECTION
== pSect
->GetType() &&
299 pSect
->GetFmt()->GetSectionNode() )
306 const SwTOXBase
* SwEditShell::GetTOX( USHORT nPos
) const
308 const SwSectionFmts
& rFmts
= GetDoc()->GetSections();
309 for( USHORT n
= 0, nCnt
= 0; n
< rFmts
.Count(); ++n
)
311 const SwSection
* pSect
= rFmts
[ n
]->GetSection();
312 if( TOX_CONTENT_SECTION
== pSect
->GetType() &&
313 pSect
->GetFmt()->GetSectionNode() &&
316 ASSERT( pSect
->ISA( SwTOXBaseSection
), "keine TOXBaseSection!" );
317 return (SwTOXBaseSection
*)pSect
;
324 // nach einlesen einer Datei alle Verzeichnisse updaten
325 void SwEditShell::SetUpdateTOX( BOOL bFlag
)
327 GetDoc()->SetUpdateTOX( bFlag
);
331 BOOL
SwEditShell::IsUpdateTOX() const
333 return GetDoc()->IsUpdateTOX();
336 /* -----------------26.08.99 13:49-------------------
338 --------------------------------------------------*/
339 const String
& SwEditShell::GetTOIAutoMarkURL() const
341 return GetDoc()->GetTOIAutoMarkURL();
343 /* -----------------26.08.99 13:49-------------------
345 --------------------------------------------------*/
346 void SwEditShell::SetTOIAutoMarkURL(const String
& rSet
)
348 GetDoc()->SetTOIAutoMarkURL(rSet
);
350 /* -----------------26.08.99 09:29-------------------
352 --------------------------------------------------*/
353 void SwEditShell::ApplyAutoMark()
356 BOOL bDoesUndo
= DoesUndo();
358 //1. remove all automatic generated index entries if AutoMarkURL has a
359 // length and the file exists
361 //3. select all occurrences of the searched words
362 //4. apply index entries
364 String
sAutoMarkURL(GetDoc()->GetTOIAutoMarkURL());
365 if( sAutoMarkURL
.Len() && FStatHelper::IsDocument( sAutoMarkURL
))
368 const SwTOXType
* pTOXType
= GetTOXType(TOX_INDEX
, 0);
369 SwClientIter
aIter(*(SwTOXType
*)pTOXType
);
370 SwTOXMark
* pMark
= (SwTOXMark
*)aIter
.First(TYPE(SwTOXMark
));
373 if(pMark
->IsAutoGenerated() && pMark
->GetTxtTOXMark())
374 DeleteTOXMark(pMark
);
375 pMark
= (SwTOXMark
*)aIter
.Next();
379 SfxMedium
aMedium( sAutoMarkURL
, STREAM_STD_READ
, TRUE
);
380 SvStream
& rStrm
= *aMedium
.GetInStream();
381 const String
sZero('0');
383 rtl_TextEncoding eChrSet
= ::gsl_getSystemTextEncoding();
386 // SearchOptions to be used in loop below
388 //SearchAlgorithms eSrchType = SearchAlgorithms_ABSOLUTE;
389 //OUString aSrchStr = rText;
390 BOOL bCaseSensitive
= TRUE
;
391 BOOL bWordOnly
= FALSE
;
392 BOOL bSrchInSel
= FALSE
;
393 BOOL bLEV_Relaxed
= TRUE
;
394 INT32 nLEV_Other
= 2; // -> changedChars;
395 INT32 nLEV_Longer
= 3; //! -> deletedChars;
396 INT32 nLEV_Shorter
= 1; //! -> insertedChars;
397 INT32 nTransliterationFlags
= 0;
399 INT32 nSrchFlags
= 0;
402 nSrchFlags
|= SearchFlags::ALL_IGNORE_CASE
;
403 nTransliterationFlags
|= TransliterationModules_IGNORE_CASE
;
406 nSrchFlags
|= SearchFlags::NORM_WORD_ONLY
;
408 nSrchFlags
|= SearchFlags::LEV_RELAXED
;
410 nSrchFlags
|= (SearchFlags::REG_NOT_BEGINOFLINE
|
411 SearchFlags::REG_NOT_ENDOFLINE
);
413 rtl::OUString sEmpty
;
414 SearchOptions
aSearchOpt(
415 SearchAlgorithms_ABSOLUTE
, nSrchFlags
,
417 SvxCreateLocale( LANGUAGE_SYSTEM
),
418 nLEV_Other
, nLEV_Longer
, nLEV_Shorter
,
419 nTransliterationFlags
);
421 while( !rStrm
.GetError() && !rStrm
.IsEof() )
424 rStrm
.ReadLine( aRdLine
);
427 // ; -> delimiter between entries ->
428 // Format: TextToSearchFor;AlternativeString;PrimaryKey;SecondaryKey;CaseSensitive;WordOnly
429 // Leading and trailing blanks are ignored
430 if( aRdLine
.Len() && '#' != aRdLine
.GetChar(0) )
432 String
sLine( aRdLine
, eChrSet
);
434 xub_StrLen nTokenPos
= 0;
435 String
sToSelect( sLine
.GetToken(0, ';', nTokenPos
) );
436 if( sToSelect
.Len() )
438 String sAlternative
= sLine
.GetToken(0, ';', nTokenPos
);
439 String sPrimary
= sLine
.GetToken(0, ';', nTokenPos
);
440 String sSecondary
= sLine
.GetToken(0, ';', nTokenPos
);
441 String sCase
= sLine
.GetToken(0, ';', nTokenPos
);
442 String sWordOnly
= sLine
.GetToken(0, ';', nTokenPos
);
445 bCaseSensitive
= sCase
.Len() && sCase
!= sZero
;
446 bWordOnly
= sWordOnly
.Len() && sWordOnly
!= sZero
;
450 //nSrchFlags |= SearchFlags::ALL_IGNORE_CASE;
451 aSearchOpt
.transliterateFlags
|=
452 TransliterationModules_IGNORE_CASE
;
456 //aSearchOpt.searchFlag &= ~SearchFlags::ALL_IGNORE_CASE;
457 aSearchOpt
.transliterateFlags
&=
458 ~TransliterationModules_IGNORE_CASE
;
461 aSearchOpt
.searchFlag
|= SearchFlags::NORM_WORD_ONLY
;
463 aSearchOpt
.searchFlag
&= ~SearchFlags::NORM_WORD_ONLY
;
465 aSearchOpt
.searchString
= sToSelect
;
470 // todo/mba: assuming that notes shouldn't be searched
471 BOOL bSearchInNotes
= FALSE
;
472 ULONG nRet
= Find( aSearchOpt
, bSearchInNotes
, DOCPOS_START
, DOCPOS_END
, bCancel
,
473 (FindRanges
)(FND_IN_SELALL
|FND_IN_BODYONLY
),
478 SwTOXMark
* pTmpMark
= new SwTOXMark(pTOXType
);
481 pTmpMark
->SetPrimaryKey( sPrimary
);
482 if( sSecondary
.Len() )
483 pTmpMark
->SetSecondaryKey( sSecondary
);
485 if(sAlternative
.Len())
486 pTmpMark
->SetAlternativeText(sAlternative
);
487 pTmpMark
->SetMainEntry(FALSE
);
488 pTmpMark
->SetAutoGenerated(TRUE
);
490 SwEditShell::Insert(*pTmpMark
);