Update ooo320-m1
[ooovba.git] / sw / source / core / edit / edglss.cxx
blob41879086d0bcd6aff9e8bc201f45248fd93834d4
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: edglss.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"
35 #include <osl/endian.h>
36 #include <hintids.hxx>
37 #include <svtools/urihelper.hxx>
38 #include <tools/cachestr.hxx>
39 #include <doc.hxx>
40 #include <pam.hxx>
41 #include <docary.hxx>
42 #include <editsh.hxx>
43 #include <edimp.hxx>
44 #include <frmfmt.hxx>
45 #include <swundo.hxx> // fuer die UndoIds
46 #include <ndtxt.hxx>
47 #include <swtable.hxx> // fuers kopieren von Tabellen
48 #include <shellio.hxx> // SwTextBlocks
49 #include <acorrect.hxx>
50 #include <swerror.h> // SwTextBlocks
52 /******************************************************************************
53 * jetzt mit einem verkappten Reader/Writer/Dokument
54 ******************************************************************************/
56 void SwEditShell::InsertGlossary( SwTextBlocks& rGlossary, const String& rStr )
58 StartAllAction();
59 GetDoc()->InsertGlossary( rGlossary, rStr, *GetCrsr(), this );
60 EndAllAction();
64 /******************************************************************************
65 * aktuelle Selektion zum Textbaustein machen und ins
66 * Textbausteindokument einfuegen, einschliesslich Vorlagen
67 ******************************************************************************/
70 USHORT SwEditShell::MakeGlossary( SwTextBlocks& rBlks, const String& rName, const String& rShortName,
71 BOOL bSaveRelFile, const String* pOnlyTxt )
73 SwDoc* pGDoc = rBlks.GetDoc();
75 String sBase;
76 if(bSaveRelFile)
78 INetURLObject aURL( rBlks.GetFileName() );
79 sBase = aURL.GetMainURL( INetURLObject::NO_DECODE );
81 rBlks.SetBaseURL( sBase );
83 USHORT nRet;
85 if( pOnlyTxt )
86 nRet = rBlks.PutText( rShortName, rName, *pOnlyTxt );
87 else
89 rBlks.ClearDoc();
90 if( rBlks.BeginPutDoc( rShortName, rName ) )
92 rBlks.GetDoc()->SetRedlineMode_intern( nsRedlineMode_t::REDLINE_DELETE_REDLINES );
93 _CopySelToDoc( pGDoc );
94 rBlks.GetDoc()->SetRedlineMode_intern( (RedlineMode_t)0 );
95 nRet = rBlks.PutDoc();
97 else
98 nRet = (USHORT) -1;
101 return nRet;
104 USHORT SwEditShell::SaveGlossaryDoc( SwTextBlocks& rBlock,
105 const String& rName,
106 const String& rShortName,
107 BOOL bSaveRelFile,
108 BOOL bOnlyTxt )
110 StartAllAction();
112 SwDoc* pGDoc = rBlock.GetDoc();
113 SwDoc* pMyDoc = GetDoc();
115 String sBase;
116 if(bSaveRelFile)
118 INetURLObject aURL( rBlock.GetFileName() );
119 sBase = aURL.GetMainURL( INetURLObject::NO_DECODE );
121 rBlock.SetBaseURL( sBase );
122 USHORT nRet = USHRT_MAX;
124 if( bOnlyTxt )
126 KillPams();
128 SwPaM* pCrsr = GetCrsr();
130 SwNodeIndex aStt( pMyDoc->GetNodes().GetEndOfExtras(), 1 );
131 SwCntntNode* pCntntNd = pMyDoc->GetNodes().GoNext( &aStt );
132 const SwNode* pNd = pCntntNd->FindTableNode();
133 if( !pNd )
134 pNd = pCntntNd;
136 pCrsr->GetPoint()->nNode = *pNd;
137 if( pNd == pCntntNd )
138 pCrsr->GetPoint()->nContent.Assign( pCntntNd, 0 );
139 pCrsr->SetMark();
141 // dann bis zum Ende vom Nodes Array
142 pCrsr->GetPoint()->nNode = pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1;
143 pCntntNd = pCrsr->GetCntntNode();
144 if( pCntntNd )
145 pCrsr->GetPoint()->nContent.Assign( pCntntNd, pCntntNd->Len() );
147 String sBuf;
148 if( GetSelectedText( sBuf, GETSELTXT_PARABRK_TO_ONLYCR ) && sBuf.Len() )
149 nRet = rBlock.PutText( rShortName, rName, sBuf );
151 else
153 rBlock.ClearDoc();
154 if( rBlock.BeginPutDoc( rShortName, rName ) )
156 SwNodeIndex aStt( pMyDoc->GetNodes().GetEndOfExtras(), 1 );
157 SwCntntNode* pCntntNd = pMyDoc->GetNodes().GoNext( &aStt );
158 const SwNode* pNd = pCntntNd->FindTableNode();
159 if( !pNd ) pNd = pCntntNd;
160 SwPaM aCpyPam( *pNd );
161 aCpyPam.SetMark();
163 // dann bis zum Ende vom Nodes Array
164 aCpyPam.GetPoint()->nNode = pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1;
165 pCntntNd = aCpyPam.GetCntntNode();
166 aCpyPam.GetPoint()->nContent.Assign( pCntntNd, pCntntNd->Len() );
168 aStt = pGDoc->GetNodes().GetEndOfExtras();
169 pCntntNd = pGDoc->GetNodes().GoNext( &aStt );
170 SwPosition aInsPos( aStt, SwIndex( pCntntNd ));
171 pMyDoc->CopyRange( aCpyPam, aInsPos, false );
173 nRet = rBlock.PutDoc();
176 EndAllAction();
177 return nRet;
180 /******************************************************************************
181 * kopiere alle Selectionen und das Doc
182 ******************************************************************************/
185 BOOL SwEditShell::_CopySelToDoc( SwDoc* pInsDoc, SwNodeIndex* pSttNd )
187 ASSERT( pInsDoc, "kein Ins.Dokument" );
189 SwNodes& rNds = pInsDoc->GetNodes();
191 SwNodeIndex aIdx( rNds.GetEndOfContent(), -1 );
192 SwCntntNode * pNd = aIdx.GetNode().GetCntntNode();
193 SwPosition aPos( aIdx, SwIndex( pNd, pNd->Len() ));
195 // soll der Index auf Anfang returnt werden ?
196 if( pSttNd )
198 *pSttNd = aPos.nNode;
199 (*pSttNd)--;
202 BOOL bRet = FALSE;
203 SET_CURR_SHELL( this );
205 pInsDoc->LockExpFlds();
207 if( IsTableMode() )
209 // kopiere Teile aus einer Tabelle: lege eine Tabelle mit der Breite
210 // von der Originalen an und kopiere die selectierten Boxen.
211 // Die Groessen werden prozentual korrigiert.
213 // lasse ueber das Layout die Boxen suchen
214 SwTableNode* pTblNd;
215 SwSelBoxes aBoxes;
216 GetTblSel( *this, aBoxes );
217 if( aBoxes.Count() && 0 != (pTblNd = (SwTableNode*)aBoxes[0]
218 ->GetSttNd()->FindTableNode() ))
220 // teste ob der TabellenName kopiert werden kann
221 BOOL bCpyTblNm = aBoxes.Count() == pTblNd->GetTable().GetTabSortBoxes().Count();
222 if( bCpyTblNm )
224 const String& rTblName = pTblNd->GetTable().GetFrmFmt()->GetName();
225 const SwFrmFmts& rTblFmts = *pInsDoc->GetTblFrmFmts();
226 for( USHORT n = rTblFmts.Count(); n; )
227 if( rTblFmts[ --n ]->GetName() == rTblName )
229 bCpyTblNm = FALSE;
230 break;
233 bRet = pInsDoc->InsCopyOfTbl( aPos, aBoxes, 0, bCpyTblNm, FALSE );
235 else
236 bRet = FALSE;
238 else
240 bool bColSel = _GetCrsr()->IsColumnSelection();
241 if( bColSel && pInsDoc->IsClipBoard() )
242 pInsDoc->SetColumnSelection( true );
244 FOREACHPAM_START(this)
246 if( !PCURCRSR->HasMark() )
248 if( 0 != (pNd = PCURCRSR->GetCntntNode()) &&
249 ( bColSel || !pNd->GetTxtNode() ) )
251 PCURCRSR->SetMark();
252 PCURCRSR->Move( fnMoveForward, fnGoCntnt );
253 bRet = GetDoc()->CopyRange( *PCURCRSR, aPos, false )
254 || bRet;
255 PCURCRSR->Exchange();
256 PCURCRSR->DeleteMark();
259 else
261 bRet = GetDoc()->CopyRange( *PCURCRSR, aPos, false ) || bRet;
264 FOREACHPAM_END()
268 pInsDoc->UnlockExpFlds();
269 if( !pInsDoc->IsExpFldsLocked() )
270 pInsDoc->UpdateExpFlds(NULL, true);
272 // die gemerkte Node-Position wieder auf den richtigen Node
273 if( bRet && pSttNd )
274 (*pSttNd)++;
277 return bRet;
280 /*------------------------------------------------------------------------
281 Beschreibung: Text innerhalb der Selektion erfragen
282 Returnwert: liefert FALSE, wenn der selektierte Bereich
283 zu gross ist, um in den Stringpuffer kopiert zu werden.
284 ------------------------------------------------------------------------*/
286 BOOL SwEditShell::GetSelectedText( String &rBuf, int nHndlParaBrk )
288 BOOL bRet = FALSE;
289 GetCrsr(); // ggfs. alle Cursor erzeugen lassen
290 if( IsSelOnePara() )
292 rBuf = GetSelTxt();
293 if( GETSELTXT_PARABRK_TO_BLANK == nHndlParaBrk )
295 xub_StrLen nPos = 0;
296 while( STRING_NOTFOUND !=
297 ( nPos = rBuf.SearchAndReplace( 0x0a, ' ', nPos )) )
300 else if( IsSelFullPara() &&
301 GETSELTXT_PARABRK_TO_ONLYCR != nHndlParaBrk )
303 #if defined(UNX)
304 rBuf += '\012';
305 #else
306 rBuf += String::CreateFromAscii(
307 RTL_CONSTASCII_STRINGPARAM( "\015\012" ));
308 #endif
310 bRet = TRUE;
312 else if( IsSelection() )
314 SvCacheStream aStream(20480);
315 #ifdef OSL_BIGENDIAN
316 aStream.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
317 #else
318 aStream.SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
319 #endif
320 WriterRef xWrt;
321 SwReaderWriter::GetWriter( String::CreateFromAscii( FILTER_TEXT ), String(), xWrt );
322 if( xWrt.Is() )
324 // Selektierte Bereiche in ein ASCII Dokument schreiben
325 SwWriter aWriter( aStream, *this);
326 xWrt->SetShowProgress( FALSE );
328 switch( nHndlParaBrk )
330 case GETSELTXT_PARABRK_TO_BLANK:
331 xWrt->bASCII_ParaAsBlanc = TRUE;
332 xWrt->bASCII_NoLastLineEnd = TRUE;
333 break;
335 case GETSELTXT_PARABRK_TO_ONLYCR:
336 xWrt->bASCII_ParaAsCR = TRUE;
337 xWrt->bASCII_NoLastLineEnd = TRUE;
338 break;
341 //JP 09.05.00: write as UNICODE ! (and not as ANSI)
342 SwAsciiOptions aAsciiOpt( xWrt->GetAsciiOptions() );
343 aAsciiOpt.SetCharSet( RTL_TEXTENCODING_UCS2 );
344 xWrt->SetAsciiOptions( aAsciiOpt );
345 xWrt->bUCS2_WithStartChar = FALSE;
347 long lLen;
348 if( !IsError( aWriter.Write( xWrt ) ) &&
349 STRING_MAXLEN > (( lLen = aStream.GetSize() )
350 / sizeof( sal_Unicode )) + 1 )
352 aStream << (sal_Unicode)'\0';
354 const sal_Unicode *p = (sal_Unicode*)aStream.GetBuffer();
355 if( p )
356 rBuf = p;
357 else
359 sal_Unicode* pStrBuf = rBuf.AllocBuffer( xub_StrLen(
360 ( lLen / sizeof( sal_Unicode ))) );
361 aStream.Seek( 0 );
362 aStream.ResetError();
363 aStream.Read( pStrBuf, lLen );
364 pStrBuf[ lLen / sizeof( sal_Unicode ) ] = '\0';
370 return TRUE;