Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <comphelper/string.hxx>
21 : #include <osl/endian.h>
22 : #include <hintids.hxx>
23 : #include <svl/urihelper.hxx>
24 : #include <tools/cachestr.hxx>
25 : #include <doc.hxx>
26 : #include <pam.hxx>
27 : #include <docary.hxx>
28 : #include <editsh.hxx>
29 : #include <edimp.hxx>
30 : #include <frmfmt.hxx>
31 : #include <swundo.hxx> // fuer die UndoIds
32 : #include <ndtxt.hxx>
33 : #include <swtable.hxx> // fuers kopieren von Tabellen
34 : #include <shellio.hxx> // SwTextBlocks
35 : #include <acorrect.hxx>
36 : #include <swerror.h> // SwTextBlocks
37 :
38 : /******************************************************************************
39 : * jetzt mit einem verkappten Reader/Writer/Dokument
40 : ******************************************************************************/
41 :
42 0 : void SwEditShell::InsertGlossary( SwTextBlocks& rGlossary, const String& rStr )
43 : {
44 0 : StartAllAction();
45 0 : GetDoc()->InsertGlossary( rGlossary, rStr, *GetCrsr(), this );
46 0 : EndAllAction();
47 0 : }
48 :
49 :
50 : /******************************************************************************
51 : * aktuelle Selektion zum Textbaustein machen und ins
52 : * Textbausteindokument einfuegen, einschliesslich Vorlagen
53 : ******************************************************************************/
54 :
55 :
56 0 : sal_uInt16 SwEditShell::MakeGlossary( SwTextBlocks& rBlks, const String& rName, const String& rShortName,
57 : sal_Bool bSaveRelFile, const String* pOnlyTxt )
58 : {
59 0 : SwDoc* pGDoc = rBlks.GetDoc();
60 :
61 0 : String sBase;
62 0 : if(bSaveRelFile)
63 : {
64 0 : INetURLObject aURL( rBlks.GetFileName() );
65 0 : sBase = aURL.GetMainURL( INetURLObject::NO_DECODE );
66 : }
67 0 : rBlks.SetBaseURL( sBase );
68 :
69 : sal_uInt16 nRet;
70 :
71 0 : if( pOnlyTxt )
72 0 : nRet = rBlks.PutText( rShortName, rName, *pOnlyTxt );
73 : else
74 : {
75 0 : rBlks.ClearDoc();
76 0 : if( rBlks.BeginPutDoc( rShortName, rName ) )
77 : {
78 0 : rBlks.GetDoc()->SetRedlineMode_intern( nsRedlineMode_t::REDLINE_DELETE_REDLINES );
79 0 : _CopySelToDoc( pGDoc );
80 0 : rBlks.GetDoc()->SetRedlineMode_intern( (RedlineMode_t)0 );
81 0 : nRet = rBlks.PutDoc();
82 : }
83 : else
84 0 : nRet = (sal_uInt16) -1;
85 : }
86 :
87 0 : return nRet;
88 : }
89 :
90 0 : sal_uInt16 SwEditShell::SaveGlossaryDoc( SwTextBlocks& rBlock,
91 : const String& rName,
92 : const String& rShortName,
93 : sal_Bool bSaveRelFile,
94 : sal_Bool bOnlyTxt )
95 : {
96 0 : StartAllAction();
97 :
98 0 : SwDoc* pGDoc = rBlock.GetDoc();
99 0 : SwDoc* pMyDoc = GetDoc();
100 :
101 0 : String sBase;
102 0 : if(bSaveRelFile)
103 : {
104 0 : INetURLObject aURL( rBlock.GetFileName() );
105 0 : sBase = aURL.GetMainURL( INetURLObject::NO_DECODE );
106 : }
107 0 : rBlock.SetBaseURL( sBase );
108 0 : sal_uInt16 nRet = USHRT_MAX;
109 :
110 0 : if( bOnlyTxt )
111 : {
112 0 : KillPams();
113 :
114 0 : SwPaM* pCrsr = GetCrsr();
115 :
116 0 : SwNodeIndex aStt( pMyDoc->GetNodes().GetEndOfExtras(), 1 );
117 0 : SwCntntNode* pCntntNd = pMyDoc->GetNodes().GoNext( &aStt );
118 0 : const SwNode* pNd = pCntntNd->FindTableNode();
119 0 : if( !pNd )
120 0 : pNd = pCntntNd;
121 :
122 0 : pCrsr->GetPoint()->nNode = *pNd;
123 0 : if( pNd == pCntntNd )
124 0 : pCrsr->GetPoint()->nContent.Assign( pCntntNd, 0 );
125 0 : pCrsr->SetMark();
126 :
127 : // dann bis zum Ende vom Nodes Array
128 0 : pCrsr->GetPoint()->nNode = pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1;
129 0 : pCntntNd = pCrsr->GetCntntNode();
130 0 : if( pCntntNd )
131 0 : pCrsr->GetPoint()->nContent.Assign( pCntntNd, pCntntNd->Len() );
132 :
133 0 : String sBuf;
134 0 : if( GetSelectedText( sBuf, GETSELTXT_PARABRK_TO_ONLYCR ) && sBuf.Len() )
135 0 : nRet = rBlock.PutText( rShortName, rName, sBuf );
136 : }
137 : else
138 : {
139 0 : rBlock.ClearDoc();
140 0 : if( rBlock.BeginPutDoc( rShortName, rName ) )
141 : {
142 0 : SwNodeIndex aStt( pMyDoc->GetNodes().GetEndOfExtras(), 1 );
143 0 : SwCntntNode* pCntntNd = pMyDoc->GetNodes().GoNext( &aStt );
144 0 : const SwNode* pNd = pCntntNd->FindTableNode();
145 0 : if( !pNd ) pNd = pCntntNd;
146 0 : SwPaM aCpyPam( *pNd );
147 0 : aCpyPam.SetMark();
148 :
149 : // dann bis zum Ende vom Nodes Array
150 0 : aCpyPam.GetPoint()->nNode = pMyDoc->GetNodes().GetEndOfContent().GetIndex()-1;
151 0 : pCntntNd = aCpyPam.GetCntntNode();
152 0 : aCpyPam.GetPoint()->nContent.Assign( pCntntNd, pCntntNd->Len() );
153 :
154 0 : aStt = pGDoc->GetNodes().GetEndOfExtras();
155 0 : pCntntNd = pGDoc->GetNodes().GoNext( &aStt );
156 0 : SwPosition aInsPos( aStt, SwIndex( pCntntNd ));
157 0 : pMyDoc->CopyRange( aCpyPam, aInsPos, false );
158 :
159 0 : nRet = rBlock.PutDoc();
160 : }
161 : }
162 0 : EndAllAction();
163 0 : return nRet;
164 : }
165 :
166 : /******************************************************************************
167 : * kopiere alle Selectionen und das Doc
168 : ******************************************************************************/
169 :
170 :
171 0 : sal_Bool SwEditShell::_CopySelToDoc( SwDoc* pInsDoc, SwNodeIndex* pSttNd )
172 : {
173 : OSL_ENSURE( pInsDoc, "kein Ins.Dokument" );
174 :
175 0 : SwNodes& rNds = pInsDoc->GetNodes();
176 :
177 0 : SwNodeIndex aIdx( rNds.GetEndOfContent(), -1 );
178 0 : SwCntntNode * pNd = aIdx.GetNode().GetCntntNode();
179 0 : SwPosition aPos( aIdx, SwIndex( pNd, pNd->Len() ));
180 :
181 : // soll der Index auf Anfang returnt werden ?
182 0 : if( pSttNd )
183 : {
184 0 : *pSttNd = aPos.nNode;
185 0 : (*pSttNd)--;
186 : }
187 :
188 0 : sal_Bool bRet = sal_False;
189 0 : SET_CURR_SHELL( this );
190 :
191 0 : pInsDoc->LockExpFlds();
192 :
193 0 : if( IsTableMode() )
194 : {
195 : // kopiere Teile aus einer Tabelle: lege eine Tabelle mit der Breite
196 : // von der Originalen an und kopiere die selectierten Boxen.
197 : // Die Groessen werden prozentual korrigiert.
198 :
199 : // lasse ueber das Layout die Boxen suchen
200 : SwTableNode* pTblNd;
201 0 : SwSelBoxes aBoxes;
202 0 : GetTblSel( *this, aBoxes );
203 0 : if( !aBoxes.empty() && 0 != (pTblNd = (SwTableNode*)aBoxes[0]
204 0 : ->GetSttNd()->FindTableNode() ))
205 : {
206 : // teste ob der TabellenName kopiert werden kann
207 0 : sal_Bool bCpyTblNm = aBoxes.size() == pTblNd->GetTable().GetTabSortBoxes().size();
208 0 : if( bCpyTblNm )
209 : {
210 0 : const String& rTblName = pTblNd->GetTable().GetFrmFmt()->GetName();
211 0 : const SwFrmFmts& rTblFmts = *pInsDoc->GetTblFrmFmts();
212 0 : for( sal_uInt16 n = rTblFmts.size(); n; )
213 0 : if( rTblFmts[ --n ]->GetName() == rTblName )
214 : {
215 0 : bCpyTblNm = sal_False;
216 0 : break;
217 : }
218 : }
219 0 : bRet = pInsDoc->InsCopyOfTbl( aPos, aBoxes, 0, bCpyTblNm, sal_False );
220 : }
221 : else
222 0 : bRet = sal_False;
223 : }
224 : else
225 : {
226 0 : bool bColSel = _GetCrsr()->IsColumnSelection();
227 0 : if( bColSel && pInsDoc->IsClipBoard() )
228 0 : pInsDoc->SetColumnSelection( true );
229 : {
230 0 : FOREACHPAM_START(this)
231 :
232 0 : if( !PCURCRSR->HasMark() )
233 : {
234 0 : if( 0 != (pNd = PCURCRSR->GetCntntNode()) &&
235 0 : ( bColSel || !pNd->GetTxtNode() ) )
236 : {
237 0 : PCURCRSR->SetMark();
238 0 : PCURCRSR->Move( fnMoveForward, fnGoCntnt );
239 0 : bRet = GetDoc()->CopyRange( *PCURCRSR, aPos, false )
240 0 : || bRet;
241 0 : PCURCRSR->Exchange();
242 0 : PCURCRSR->DeleteMark();
243 : }
244 : }
245 : else
246 : {
247 0 : bRet = GetDoc()->CopyRange( *PCURCRSR, aPos, false ) || bRet;
248 : }
249 :
250 0 : FOREACHPAM_END()
251 : }
252 : }
253 :
254 0 : pInsDoc->UnlockExpFlds();
255 0 : if( !pInsDoc->IsExpFldsLocked() )
256 0 : pInsDoc->UpdateExpFlds(NULL, true);
257 :
258 : // die gemerkte Node-Position wieder auf den richtigen Node
259 0 : if( bRet && pSttNd )
260 0 : ++(*pSttNd);
261 :
262 :
263 0 : return bRet;
264 : }
265 :
266 : /*------------------------------------------------------------------------
267 : Beschreibung: Text innerhalb der Selektion erfragen
268 : Returnwert: liefert sal_False, wenn der selektierte Bereich
269 : zu gross ist, um in den Stringpuffer kopiert zu werden.
270 : ------------------------------------------------------------------------*/
271 :
272 0 : sal_Bool SwEditShell::GetSelectedText( String &rBuf, int nHndlParaBrk )
273 : {
274 0 : GetCrsr(); // ggfs. alle Cursor erzeugen lassen
275 0 : if( IsSelOnePara() )
276 : {
277 0 : rBuf = GetSelTxt();
278 0 : if( GETSELTXT_PARABRK_TO_BLANK == nHndlParaBrk )
279 : {
280 0 : xub_StrLen nPos = 0;
281 0 : while( STRING_NOTFOUND !=
282 0 : ( nPos = rBuf.SearchAndReplace( 0x0a, ' ', nPos )) )
283 : ;
284 : }
285 0 : else if( IsSelFullPara() &&
286 : GETSELTXT_PARABRK_TO_ONLYCR != nHndlParaBrk )
287 : {
288 : #if defined(UNX)
289 0 : rBuf += '\012';
290 : #else
291 : rBuf += rtl::OUString("\015\012");
292 : #endif
293 : }
294 : }
295 0 : else if( IsSelection() )
296 : {
297 0 : SvCacheStream aStream(20480);
298 : #ifdef OSL_BIGENDIAN
299 : aStream.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
300 : #else
301 0 : aStream.SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
302 : #endif
303 0 : WriterRef xWrt;
304 0 : SwReaderWriter::GetWriter( rtl::OUString(FILTER_TEXT), String(), xWrt );
305 0 : if( xWrt.Is() )
306 : {
307 : // Selektierte Bereiche in ein ASCII Dokument schreiben
308 0 : SwWriter aWriter( aStream, *this);
309 0 : xWrt->SetShowProgress( sal_False );
310 :
311 0 : switch( nHndlParaBrk )
312 : {
313 : case GETSELTXT_PARABRK_TO_BLANK:
314 0 : xWrt->bASCII_ParaAsBlanc = sal_True;
315 0 : xWrt->bASCII_NoLastLineEnd = sal_True;
316 0 : break;
317 :
318 : case GETSELTXT_PARABRK_TO_ONLYCR:
319 0 : xWrt->bASCII_ParaAsCR = sal_True;
320 0 : xWrt->bASCII_NoLastLineEnd = sal_True;
321 0 : break;
322 : }
323 :
324 : //JP 09.05.00: write as UNICODE ! (and not as ANSI)
325 0 : SwAsciiOptions aAsciiOpt( xWrt->GetAsciiOptions() );
326 0 : aAsciiOpt.SetCharSet( RTL_TEXTENCODING_UCS2 );
327 0 : xWrt->SetAsciiOptions( aAsciiOpt );
328 0 : xWrt->bUCS2_WithStartChar = sal_False;
329 :
330 : long lLen;
331 0 : if( !IsError( aWriter.Write( xWrt ) ) &&
332 0 : STRING_MAXLEN > (( lLen = aStream.GetSize() )
333 : / sizeof( sal_Unicode )) + 1 )
334 : {
335 0 : aStream << (sal_Unicode)'\0';
336 :
337 0 : const sal_Unicode *p = (sal_Unicode*)aStream.GetBuffer();
338 0 : if( p )
339 0 : rBuf = rtl::OUString(p);
340 : else
341 : {
342 0 : rtl_uString *pStr = rtl_uString_alloc(lLen / sizeof( sal_Unicode ));
343 0 : aStream.Seek( 0 );
344 0 : aStream.ResetError();
345 : //endian specific?, yipes!
346 0 : aStream.Read(pStr->buffer, lLen);
347 0 : rBuf = rtl::OUString(pStr, SAL_NO_ACQUIRE);
348 : }
349 0 : }
350 0 : }
351 : }
352 :
353 0 : return sal_True;
354 : }
355 :
356 :
357 :
358 :
359 :
360 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|