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 :
21 : #include <hintids.hxx>
22 : #include <com/sun/star/util/SearchOptions.hpp>
23 : #include <com/sun/star/util/SearchFlags.hpp>
24 : #include <com/sun/star/i18n/TransliterationModules.hpp>
25 : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
26 : #include <comphelper/string.hxx>
27 : #include <unotools/tempfile.hxx>
28 : #include <tools/urlobj.hxx>
29 : #include <vcl/print.hxx>
30 : #include <vcl/msgbox.hxx>
31 : #include <vcl/wrkwin.hxx>
32 : #include <vcl/metric.hxx>
33 : #include <svtools/ctrltool.hxx>
34 : #include <svl/intitem.hxx>
35 : #include <svl/stritem.hxx>
36 : #include <unotools/pathoptions.hxx>
37 : #include <svl/undo.hxx>
38 : #include <unotools/textsearch.hxx>
39 : #include <svl/eitem.hxx>
40 : #include <svl/whiter.hxx>
41 : #include <unotools/saveopt.hxx>
42 : #include <svtools/transfer.hxx>
43 : #include <svtools/svtools.hrc>
44 : #include <svtools/svtresid.hxx>
45 : #include <svx/svxids.hrc>
46 : #include <svtools/htmlcfg.hxx>
47 : #include <sfx2/app.hxx>
48 : #include <sfx2/objface.hxx>
49 : #include <sfx2/viewfrm.hxx>
50 : #include <sfx2/bindings.hxx>
51 : #include <sfx2/docfilt.hxx>
52 : #include <sfx2/fcontnr.hxx>
53 : #include <sfx2/request.hxx>
54 : #include <sfx2/prnmon.hxx>
55 : #include <sfx2/docfile.hxx>
56 : #include <editeng/fhgtitem.hxx>
57 : #include <svx/srchdlg.hxx>
58 : #include <svl/srchitem.hxx>
59 : #include <editeng/fontitem.hxx>
60 : #include <editeng/flstitem.hxx>
61 : #include "editeng/unolingu.hxx"
62 : #include <sfx2/sfxhtml.hxx>
63 : #include <swtypes.hxx>
64 : #include <swmodule.hxx>
65 : #include <docsh.hxx>
66 : #include <wdocsh.hxx>
67 : #include <srcview.hxx>
68 : #include <viewfunc.hxx>
69 : #include <doc.hxx>
70 : #include <sfx2/msg.hxx>
71 : #include <shellio.hxx>
72 :
73 : #include <cmdid.h> // FN_ ...
74 : #include <helpid.h>
75 : #include <globals.hrc>
76 : #include <shells.hrc>
77 : #include <popup.hrc>
78 : #include <web.hrc>
79 : #include <view.hrc>
80 : #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
81 : #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
82 : #include <sfx2/filedlghelper.hxx>
83 : #define SwSrcView
84 : #include "swslots.hxx"
85 :
86 : #include <unomid.h>
87 :
88 : #include <com/sun/star/document/XDocumentProperties.hpp>
89 : #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
90 :
91 : using namespace ::com::sun::star;
92 : using namespace ::com::sun::star::i18n;
93 : using namespace ::com::sun::star::lang;
94 : using namespace ::com::sun::star::uno;
95 : using namespace ::com::sun::star::ui::dialogs;
96 : using namespace ::sfx2;
97 : using ::com::sun::star::util::SearchOptions;
98 :
99 :
100 : #define SWSRCVIEWFLAGS ( SFX_VIEW_CAN_PRINT|\
101 : SFX_VIEW_NO_NEWWINDOW )
102 :
103 : #define SRC_SEARCHOPTIONS (0xFFFF & ~(SEARCH_OPTIONS_FORMAT|SEARCH_OPTIONS_FAMILIES|SEARCH_OPTIONS_SEARCH_ALL))
104 :
105 : // Printing margins -> like Basic - Ide
106 : #define LMARGPRN 1700
107 : #define RMARGPRN 900
108 : #define TMARGPRN 2000
109 : #define BMARGPRN 1000
110 : #define BORDERPRN 300
111 :
112 64 : SFX_IMPL_NAMED_VIEWFACTORY(SwSrcView, "SourceView")
113 : {
114 32 : SFX_VIEW_REGISTRATION(SwWebDocShell);
115 32 : }
116 :
117 198 : SFX_IMPL_INTERFACE( SwSrcView, SfxViewShell, SW_RES(0) )
118 : {
119 33 : SFX_POPUPMENU_REGISTRATION(SW_RES(MN_SRCVIEW_POPUPMENU));
120 66 : SFX_OBJECTBAR_REGISTRATION( SFX_OBJECTBAR_TOOLS|
121 : SFX_VISIBILITY_STANDARD|SFX_VISIBILITY_SERVER,
122 33 : SW_RES(RID_WEBTOOLS_TOOLBOX) );
123 33 : SFX_CHILDWINDOW_REGISTRATION(SvxSearchDialogWrapper::GetChildWindowId());
124 33 : }
125 :
126 26 : TYPEINIT1(SwSrcView, SfxViewShell)
127 :
128 0 : static void lcl_PrintHeader( OutputDevice &rOutDev, sal_uInt16 nPages, sal_uInt16 nCurPage, const String& rTitle )
129 : {
130 0 : short nLeftMargin = LMARGPRN;
131 0 : Size aSz = rOutDev.GetOutputSize();
132 0 : short nBorder = BORDERPRN;
133 :
134 0 : Color aOldFillColor( rOutDev.GetFillColor() );
135 0 : Font aOldFont( rOutDev.GetFont() );
136 :
137 0 : rOutDev.SetFillColor( Color(COL_TRANSPARENT) );
138 :
139 0 : Font aFont( aOldFont );
140 0 : aFont.SetWeight( WEIGHT_BOLD );
141 0 : aFont.SetAlign( ALIGN_BOTTOM );
142 0 : rOutDev.SetFont( aFont );
143 :
144 0 : long nFontHeight = rOutDev.GetTextHeight();
145 :
146 : // 1.Border => Line, 2+3 Border = Space.
147 0 : long nYTop = TMARGPRN-3*nBorder-nFontHeight;
148 :
149 0 : long nXLeft = nLeftMargin-nBorder;
150 0 : long nXRight = aSz.Width()-RMARGPRN+nBorder;
151 :
152 : rOutDev.DrawRect( Rectangle(
153 : Point( nXLeft, nYTop ),
154 0 : Size( nXRight-nXLeft, aSz.Height() - nYTop - BMARGPRN + nBorder ) ) );
155 :
156 :
157 0 : long nY = TMARGPRN-2*nBorder;
158 0 : Point aPos( nLeftMargin, nY );
159 0 : rOutDev.DrawText( aPos, rTitle );
160 0 : if ( nPages != 1 )
161 : {
162 0 : aFont.SetWeight( WEIGHT_NORMAL );
163 0 : rOutDev.SetFont( aFont );
164 0 : String aPageStr( OUString(" [") );
165 0 : aPageStr += String( SW_RES( STR_PAGE ) );
166 0 : aPageStr += ' ';
167 0 : aPageStr += OUString::number( nCurPage );
168 0 : aPageStr += ']';
169 0 : aPos.X() += rOutDev.GetTextWidth( rTitle );
170 0 : rOutDev.DrawText( aPos, aPageStr );
171 : }
172 :
173 :
174 0 : nY = TMARGPRN-nBorder;
175 :
176 0 : rOutDev.DrawLine( Point( nXLeft, nY ), Point( nXRight, nY ) );
177 :
178 0 : rOutDev.SetFont( aOldFont );
179 0 : rOutDev.SetFillColor( aOldFillColor );
180 0 : }
181 :
182 0 : static rtl_TextEncoding lcl_GetStreamCharSet(rtl_TextEncoding eLoadEncoding)
183 : {
184 0 : rtl_TextEncoding eRet = eLoadEncoding;
185 0 : if(RTL_TEXTENCODING_DONTKNOW == eRet)
186 : {
187 0 : SvxHtmlOptions& rHtmlOptions = SvxHtmlOptions::Get();
188 : const sal_Char *pCharSet =
189 0 : rtl_getBestMimeCharsetFromTextEncoding( rHtmlOptions.GetTextEncoding() );
190 0 : eRet = rtl_getTextEncodingFromMimeCharset( pCharSet );
191 : }
192 0 : return eRet;
193 : }
194 :
195 0 : static void lcl_ConvertTabsToSpaces( String& rLine )
196 : {
197 0 : if ( rLine.Len() )
198 : {
199 0 : sal_uInt16 nPos = 0;
200 0 : sal_uInt16 nMax = rLine.Len();
201 0 : while ( nPos < nMax )
202 : {
203 0 : if ( rLine.GetChar(nPos) == '\t' )
204 : {
205 : // Not 4 blanks, but on 4th TabPos:
206 0 : OUStringBuffer aBlanker;
207 0 : comphelper::string::padToLength(aBlanker, ( 4 - ( nPos % 4 ) ), ' ');
208 0 : rLine.Erase( nPos, 1 );
209 0 : rLine.Insert(aBlanker.makeStringAndClear(), nPos);
210 0 : nMax = rLine.Len();
211 : }
212 0 : nPos++; // Not optimally, if tab, but not wrong...
213 : }
214 : }
215 0 : }
216 :
217 0 : SwSrcView::SwSrcView(SfxViewFrame* pViewFrame, SfxViewShell*) :
218 : SfxViewShell( pViewFrame, SWSRCVIEWFLAGS ),
219 0 : aEditWin( &pViewFrame->GetWindow(), this ),
220 : pSearchItem(0),
221 : bSourceSaved(sal_False),
222 0 : eLoadEncoding(RTL_TEXTENCODING_DONTKNOW)
223 : {
224 0 : Init();
225 0 : }
226 :
227 0 : SwSrcView::~SwSrcView()
228 : {
229 0 : SwDocShell* pDocShell = GetDocShell();
230 : OSL_ENSURE(PTR_CAST(SwWebDocShell, pDocShell), "Why no WebDocShell?");
231 0 : const TextSelection& rSel = aEditWin.GetTextView()->GetSelection();
232 0 : ((SwWebDocShell*)pDocShell)->SetSourcePara( static_cast< sal_uInt16 >( rSel.GetStart().GetPara() ) );
233 :
234 : uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
235 0 : pDocShell->GetModel(), uno::UNO_QUERY_THROW);
236 : uno::Reference<document::XDocumentProperties> xDocProps
237 0 : = xDPS->getDocumentProperties();
238 0 : OUString url = xDocProps->getAutoloadURL();
239 0 : sal_Int32 delay = xDocProps->getAutoloadSecs();
240 : pDocShell->SetAutoLoad(INetURLObject(url), delay,
241 0 : (delay != 0) || !url.isEmpty());
242 0 : EndListening(*pDocShell);
243 0 : delete pSearchItem;
244 0 : }
245 :
246 0 : void SwSrcView::SaveContentTo(SfxMedium& rMed)
247 : {
248 0 : SvStream* pOutStream = rMed.GetOutStream();
249 0 : pOutStream->SetStreamCharSet(lcl_GetStreamCharSet(eLoadEncoding));
250 0 : aEditWin.Write( *pOutStream );
251 0 : }
252 :
253 0 : void SwSrcView::Init()
254 : {
255 0 : SetHelpId(SW_SRC_VIEWSHELL);
256 0 : SetName(OUString("Source"));
257 0 : SetWindow( &aEditWin );
258 0 : SwDocShell* pDocShell = GetDocShell();
259 : // If the doc is still loading, then the DocShell must fire up
260 : // the Load if the loading is completed.
261 0 : if(!pDocShell->IsLoading())
262 0 : Load(pDocShell);
263 : else
264 : {
265 0 : aEditWin.SetReadonly(sal_True);
266 : }
267 :
268 0 : SetNewWindowAllowed( sal_False );
269 0 : StartListening(*pDocShell,sal_True);
270 0 : }
271 :
272 0 : SwDocShell* SwSrcView::GetDocShell()
273 : {
274 0 : SfxObjectShell* pObjShell = GetViewFrame()->GetObjectShell();
275 0 : return PTR_CAST(SwDocShell, pObjShell);
276 : }
277 :
278 0 : void SwSrcView::SaveContent(const String& rTmpFile)
279 : {
280 0 : SfxMedium aMedium( rTmpFile, STREAM_WRITE);
281 0 : SvStream* pOutStream = aMedium.GetOutStream();
282 0 : pOutStream->SetStreamCharSet( lcl_GetStreamCharSet(eLoadEncoding) );
283 0 : aEditWin.Write(*pOutStream);
284 0 : aMedium.Commit();
285 0 : }
286 :
287 0 : void SwSrcView::Execute(SfxRequest& rReq)
288 : {
289 0 : sal_uInt16 nSlot = rReq.GetSlot();
290 0 : TextView* pTextView = aEditWin.GetTextView();
291 0 : switch( nSlot )
292 : {
293 : case SID_SAVEACOPY:
294 : case SID_SAVEASDOC:
295 : {
296 0 : SvtPathOptions aPathOpt;
297 : // filesave dialog with autoextension
298 : FileDialogHelper aDlgHelper(
299 0 : TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
300 0 : uno::Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
301 0 : uno::Reference<XFilterManager> xFltMgr(xFP, UNO_QUERY);
302 :
303 : // search for an html filter for export
304 0 : SfxFilterContainer* pFilterCont = GetObjectShell()->GetFactory().GetFilterContainer();
305 : const SfxFilter* pFilter =
306 0 : pFilterCont->GetFilter4Extension( OUString("html"), SFX_FILTER_EXPORT );
307 0 : if ( pFilter )
308 : {
309 : // filter found -> use its uiname and wildcard
310 0 : const String& rUIName = pFilter->GetUIName();
311 0 : const WildCard& rCard = pFilter->GetWildcard();
312 0 : xFltMgr->appendFilter( rUIName, rCard.getGlob() );
313 0 : xFltMgr->setCurrentFilter( rUIName ) ;
314 : }
315 : else
316 : {
317 : // filter not found
318 0 : OUString sHtml("HTML");
319 0 : xFltMgr->appendFilter( sHtml, OUString("*.html;*.htm") );
320 0 : xFltMgr->setCurrentFilter( sHtml ) ;
321 : }
322 :
323 0 : xFP->setDisplayDirectory( aPathOpt.GetWorkPath() );
324 0 : if( aDlgHelper.Execute() == ERRCODE_NONE)
325 : {
326 0 : SfxMedium aMedium( xFP->getFiles().getConstArray()[0],
327 0 : STREAM_WRITE | STREAM_SHARE_DENYNONE );
328 0 : SvStream* pOutStream = aMedium.GetOutStream();
329 0 : pOutStream->SetStreamCharSet(lcl_GetStreamCharSet(eLoadEncoding));
330 0 : aEditWin.Write( *pOutStream );
331 0 : aMedium.Commit();
332 0 : }
333 : }
334 0 : break;
335 : case SID_SAVEDOC:
336 : {
337 0 : SwDocShell* pDocShell = GetDocShell();
338 0 : SfxMedium* pMed = 0;
339 0 : if(pDocShell->HasName())
340 0 : pMed = pDocShell->GetMedium();
341 : else
342 : {
343 0 : SfxBoolItem* pItem = (SfxBoolItem*)pDocShell->ExecuteSlot(rReq, pDocShell->GetInterface());
344 0 : if(pItem && pItem->GetValue())
345 0 : pMed = pDocShell->GetMedium();
346 : }
347 0 : if(pMed)
348 : {
349 0 : SvStream* pOutStream = pMed->GetOutStream();
350 0 : pOutStream->Seek(0);
351 0 : pOutStream->SetStreamSize(0);
352 0 : pOutStream->SetStreamCharSet(lcl_GetStreamCharSet(eLoadEncoding));
353 0 : aEditWin.Write( *pOutStream );
354 0 : pMed->CloseOutStream();
355 0 : pMed->Commit();
356 0 : pDocShell->GetDoc()->ResetModified();
357 0 : SourceSaved();
358 0 : aEditWin.ClearModifyFlag();
359 : }
360 : }
361 0 : break;
362 : case FID_SEARCH_NOW:
363 : {
364 0 : const SfxItemSet* pTmpArgs = rReq.GetArgs();
365 :
366 0 : sal_uInt16 nWhich = pTmpArgs->GetWhichByPos( 0 );
367 : OSL_ENSURE( nWhich, "Which for SearchItem ?" );
368 0 : const SfxPoolItem& rItem = pTmpArgs->Get( nWhich );
369 0 : SetSearchItem( (const SvxSearchItem&)rItem);
370 0 : StartSearchAndReplace( (const SvxSearchItem&)rItem, sal_False, rReq.IsAPI() );
371 0 : if(aEditWin.IsModified())
372 0 : GetDocShell()->GetDoc()->SetModified();
373 : }
374 0 : break;
375 : case FN_REPEAT_SEARCH:
376 : {
377 0 : SvxSearchItem* pSrchItem = GetSearchItem();
378 0 : if(pSrchItem)
379 : {
380 0 : StartSearchAndReplace( *pSrchItem, sal_False, rReq.IsAPI() );
381 0 : if(aEditWin.IsModified())
382 0 : GetDocShell()->GetDoc()->SetModified();
383 : }
384 : }
385 0 : break;
386 : case SID_PRINTDOC:
387 : case SID_PRINTDOCDIRECT:
388 : {
389 0 : SfxViewShell::ExecuteSlot( rReq, SfxViewShell::GetInterface() );
390 : }
391 0 : break;
392 : case SID_UNDO:
393 0 : pTextView->Undo();
394 0 : GetViewFrame()->GetBindings().InvalidateAll(sal_False);
395 0 : break;
396 : case SID_REDO:
397 0 : pTextView->Redo();
398 0 : GetViewFrame()->GetBindings().InvalidateAll(sal_False);
399 0 : break;
400 : case SID_REPEAT:
401 0 : break;
402 : case SID_CUT:
403 0 : if(pTextView->HasSelection())
404 0 : pTextView->Cut();
405 0 : break;
406 : case SID_COPY:
407 0 : if(pTextView->HasSelection())
408 0 : pTextView->Copy();
409 0 : break;
410 : case SID_PASTE:
411 0 : pTextView->Paste();
412 0 : break;
413 : case SID_SELECTALL:
414 0 : pTextView->SetSelection( TextSelection( TextPaM( 0, 0 ), TextPaM( 0xFFFFFFFF, 0xFFFF ) ) );
415 0 : break;
416 : }
417 0 : aEditWin.Invalidate();
418 0 : }
419 :
420 0 : void SwSrcView::GetState(SfxItemSet& rSet)
421 : {
422 0 : SfxWhichIter aIter(rSet);
423 0 : sal_uInt16 nWhich = aIter.FirstWhich();
424 0 : TextView* pTextView = aEditWin.GetTextView();
425 :
426 0 : while(nWhich)
427 : {
428 0 : switch(nWhich)
429 : {
430 : case SID_SAVEASDOC:
431 0 : rSet.Put(SfxStringItem(nWhich, String(SW_RES(STR_SAVEAS_SRC))));
432 0 : break;
433 : case SID_SAVEACOPY:
434 0 : rSet.Put(SfxStringItem(nWhich, String(SW_RES(STR_SAVEACOPY_SRC))));
435 0 : break;
436 : case SID_SAVEDOC:
437 : {
438 0 : SwDocShell* pDocShell = GetDocShell();
439 0 : if(!pDocShell->IsModified())
440 0 : rSet.DisableItem(nWhich);
441 : }
442 0 : break;
443 : case SID_PRINTDOC:
444 : case SID_PRINTDOCDIRECT:
445 0 : break;
446 : case SID_TABLE_CELL:
447 : {
448 0 : String aPos( SW_RES(STR_SRCVIEW_ROW) );
449 0 : TextSelection aSel = pTextView->GetSelection();
450 0 : aPos += OUString::number( aSel.GetEnd().GetPara()+1 );
451 0 : aPos += OUString(" : ");
452 0 : aPos += String(SW_RES(STR_SRCVIEW_COL));
453 0 : aPos += OUString::number( aSel.GetEnd().GetIndex()+1 );
454 0 : SfxStringItem aItem( nWhich, aPos );
455 0 : rSet.Put( aItem );
456 : }
457 0 : break;
458 : case SID_SEARCH_OPTIONS:
459 : {
460 0 : sal_uInt16 nOpt = SRC_SEARCHOPTIONS;
461 0 : if(GetDocShell()->IsReadOnly())
462 0 : nOpt &= ~(SEARCH_OPTIONS_REPLACE|SEARCH_OPTIONS_REPLACE_ALL);
463 :
464 0 : rSet.Put( SfxUInt16Item( SID_SEARCH_OPTIONS, nOpt) );
465 : }
466 0 : break;
467 : case SID_SEARCH_ITEM:
468 : {
469 0 : String sSelected;
470 0 : if ( !pTextView->HasSelection() )
471 : {
472 0 : const TextSelection& rSel = pTextView->GetSelection();
473 0 : sSelected = aEditWin.GetTextEngine()->GetWord( rSel.GetStart());
474 : }
475 : else
476 : {
477 0 : sSelected = pTextView->GetSelected();
478 : }
479 0 : SvxSearchItem * pSrchItem = GetSearchItem();
480 0 : pSrchItem->SetSearchString( sSelected );
481 0 : rSet.Put( *pSrchItem );
482 : }
483 0 : break;
484 : case FN_REPEAT_SEARCH:
485 : {
486 0 : if(!GetSearchItem())
487 0 : rSet.DisableItem(nWhich);
488 : };
489 0 : break;
490 : case SID_UNDO:
491 : case SID_REDO:
492 : {
493 0 : ::svl::IUndoManager& rMgr = pTextView->GetTextEngine()->GetUndoManager();
494 0 : sal_uInt16 nCount = 0;
495 0 : if(nWhich == SID_UNDO)
496 : {
497 0 : nCount = rMgr.GetUndoActionCount();
498 0 : if(nCount)
499 : {
500 0 : String aStr(SvtResId( STR_UNDO));
501 0 : aStr += rMgr.GetUndoActionComment(--nCount);
502 0 : rSet.Put(SfxStringItem(nWhich, aStr));
503 : }
504 : else
505 0 : rSet.DisableItem(nWhich);
506 : }
507 : else
508 : {
509 0 : nCount = rMgr.GetRedoActionCount();
510 0 : if(nCount)
511 : {
512 0 : String aStr(SvtResId( STR_REDO));
513 0 : aStr += rMgr.GetRedoActionComment(--nCount);
514 0 : rSet.Put(SfxStringItem(nWhich,aStr));
515 : }
516 : else
517 0 : rSet.DisableItem(nWhich);
518 : }
519 : }
520 0 : break;
521 : case SID_MAIL_SENDDOCASPDF:
522 : case SID_MAIL_SENDDOC :
523 : case SID_EXPORTDOCASPDF:
524 : case SID_DIRECTEXPORTDOCASPDF:
525 : case SID_EXPORTDOC:
526 : case SID_REPEAT:
527 : case SID_BROWSER_MODE:
528 : case FN_PRINT_LAYOUT:
529 0 : rSet.DisableItem(nWhich);
530 0 : break;
531 : case SID_CUT:
532 : case SID_COPY:
533 0 : if(!pTextView->HasSelection())
534 0 : rSet.DisableItem(nWhich);
535 0 : break;
536 : case SID_PASTE:
537 : {
538 : TransferableDataHelper aDataHelper(
539 : TransferableDataHelper::CreateFromSystemClipboard(
540 0 : &aEditWin) );
541 0 : bool bDisable = !aDataHelper.GetXTransferable().is() ||
542 0 : 0 == aDataHelper.GetFormatCount();
543 0 : if( bDisable )
544 0 : rSet.DisableItem(nWhich);
545 : }
546 0 : break;
547 : }
548 0 : nWhich = aIter.NextWhich();
549 0 : }
550 0 : }
551 :
552 0 : SvxSearchItem* SwSrcView::GetSearchItem()
553 : {
554 0 : if(!pSearchItem)
555 : {
556 0 : pSearchItem = new SvxSearchItem(SID_SEARCH_ITEM);
557 : }
558 0 : return pSearchItem;
559 : }
560 :
561 0 : void SwSrcView::SetSearchItem( const SvxSearchItem& rItem )
562 : {
563 0 : delete pSearchItem;
564 0 : pSearchItem = (SvxSearchItem*)rItem.Clone();
565 0 : }
566 :
567 0 : sal_uInt16 SwSrcView::StartSearchAndReplace(const SvxSearchItem& rSearchItem,
568 : sal_Bool bFromStart,
569 : sal_Bool bApi,
570 : sal_Bool bRecursive)
571 : {
572 0 : ExtTextView* pTextView = aEditWin.GetTextView();
573 0 : TextSelection aSel;
574 0 : TextPaM aPaM;
575 :
576 0 : sal_Bool bForward = !rSearchItem.GetBackward();
577 0 : bool bAtStart = pTextView->GetSelection() == TextSelection( aPaM, aPaM );
578 :
579 0 : if( !bForward )
580 0 : aPaM = TextPaM( (sal_uLong)-1, (sal_uInt16)-1 );
581 :
582 0 : if( bFromStart )
583 : {
584 0 : aSel = pTextView->GetSelection();
585 0 : pTextView->SetSelection( TextSelection( aPaM, aPaM ));
586 : }
587 :
588 0 : util::SearchOptions aSearchOpt( rSearchItem.GetSearchOptions() );
589 0 : aSearchOpt.Locale = GetAppLanguageTag().getLocale();
590 :
591 : sal_uInt16 nFound;
592 0 : sal_Bool bAll = sal_False;
593 0 : switch( rSearchItem.GetCommand() )
594 : {
595 : case SVX_SEARCHCMD_FIND:
596 : case SVX_SEARCHCMD_FIND_ALL:
597 0 : nFound = pTextView->Search( aSearchOpt, bForward );
598 0 : break;
599 :
600 0 : case SVX_SEARCHCMD_REPLACE_ALL: bAll = sal_True;
601 : case SVX_SEARCHCMD_REPLACE:
602 0 : nFound = pTextView->Replace( aSearchOpt, bAll, bForward );
603 0 : break;
604 :
605 : default:
606 0 : nFound = 0;
607 : }
608 :
609 0 : if( !nFound )
610 : {
611 0 : bool bNotFoundMessage = false;
612 0 : if(!bRecursive)
613 : {
614 0 : if(!bFromStart)
615 : {
616 0 : bNotFoundMessage = bAtStart;
617 : }
618 : else
619 : {
620 0 : bNotFoundMessage = true;
621 0 : pTextView->SetSelection( aSel );
622 : }
623 : }
624 0 : else if(bAtStart)
625 : {
626 0 : bNotFoundMessage = true;
627 : }
628 :
629 :
630 0 : if(!bApi)
631 : {
632 0 : if(bNotFoundMessage)
633 : {
634 0 : InfoBox( 0, SW_RES(MSG_NOT_FOUND)).Execute();
635 : }
636 0 : else if(!bRecursive && RET_YES ==
637 : QueryBox(0, SW_RES( bForward ? MSG_SEARCH_END
638 0 : : MSG_SEARCH_START)).Execute())
639 : {
640 0 : pTextView->SetSelection( TextSelection( aPaM, aPaM ) );
641 0 : StartSearchAndReplace( rSearchItem, sal_False, sal_False, sal_True );
642 : }
643 : }
644 : }
645 0 : return nFound;
646 : }
647 :
648 0 : sal_uInt16 SwSrcView::SetPrinter(SfxPrinter* pNew, sal_uInt16 nDiffFlags, bool )
649 : {
650 0 : SwDocShell* pDocSh = GetDocShell();
651 0 : if ( (SFX_PRINTER_JOBSETUP | SFX_PRINTER_PRINTER) & nDiffFlags )
652 : {
653 0 : pDocSh->GetDoc()->setPrinter( pNew, true, true );
654 0 : if ( nDiffFlags & SFX_PRINTER_PRINTER )
655 0 : pDocSh->SetModified();
656 : }
657 0 : if ( nDiffFlags & SFX_PRINTER_OPTIONS )
658 0 : ::SetPrinter( pDocSh->getIDocumentDeviceAccess(), pNew, sal_True );
659 :
660 0 : const bool bChgOri = nDiffFlags & SFX_PRINTER_CHG_ORIENTATION;
661 0 : const bool bChgSize= nDiffFlags & SFX_PRINTER_CHG_SIZE;
662 0 : if ( bChgOri || bChgSize )
663 : {
664 0 : pDocSh->SetModified();
665 : }
666 0 : return 0;
667 : }
668 :
669 0 : SfxPrinter* SwSrcView::GetPrinter( sal_Bool bCreate )
670 : {
671 0 : return GetDocShell()->GetDoc()->getPrinter( bCreate );
672 : }
673 :
674 0 : sal_Int32 SwSrcView::PrintSource(
675 : OutputDevice *pOutDev,
676 : sal_Int32 nPage,
677 : bool bCalcNumPagesOnly )
678 : {
679 0 : if (!pOutDev || nPage <= 0)
680 0 : return 0;
681 :
682 : //! This logarithm for printing the n-th page is very poor since it
683 : //! needs to go over the text of all previous pages to get to the correct one.
684 : //! But since HTML source code is expected to be just a small number of pages
685 : //! even this poor algorithm should be enough...
686 :
687 0 : pOutDev->Push();
688 :
689 0 : TextEngine* pTextEngine = aEditWin.GetTextEngine();
690 0 : pOutDev->SetMapMode( MAP_100TH_MM );
691 0 : Font aFont( aEditWin.GetOutWin()->GetFont() );
692 0 : Size aSize( aFont.GetSize() );
693 0 : aSize = aEditWin.GetOutWin()->PixelToLogic( aSize, MAP_100TH_MM );
694 0 : aFont.SetSize( aSize );
695 0 : aFont.SetColor( COL_BLACK );
696 0 : pOutDev->SetFont( aFont );
697 :
698 0 : String aTitle( GetViewFrame()->GetWindow().GetText() );
699 :
700 0 : sal_uInt16 nLineHeight = (sal_uInt16) pOutDev->GetTextHeight(); // slightly more
701 0 : sal_uInt16 nParaSpace = 10;
702 :
703 0 : Size aPaperSz = pOutDev->GetOutputSize();
704 0 : aPaperSz.Width() -= (LMARGPRN + RMARGPRN);
705 0 : aPaperSz.Height() -= (TMARGPRN + BMARGPRN);
706 :
707 : // nLinepPage is not true, if lines have to be wrapped...
708 0 : sal_uInt16 nLinespPage = (sal_uInt16) (aPaperSz.Height() / nLineHeight);
709 0 : sal_uInt16 nCharspLine = (sal_uInt16) (aPaperSz.Width() / pOutDev->GetTextWidth(OUString('X')));
710 0 : sal_uInt16 nParas = static_cast< sal_uInt16 >( pTextEngine->GetParagraphCount() );
711 :
712 0 : sal_uInt16 nPages = (sal_uInt16) (nParas / nLinespPage + 1 );
713 0 : sal_uInt16 nCurPage = 1;
714 :
715 : // Print header...
716 0 : if (!bCalcNumPagesOnly && nPage == nCurPage)
717 0 : lcl_PrintHeader( *pOutDev, nPages, nCurPage, aTitle );
718 0 : const Point aStartPos( LMARGPRN, TMARGPRN );
719 0 : Point aPos( aStartPos );
720 0 : for ( sal_uInt16 nPara = 0; nPara < nParas; ++nPara )
721 : {
722 0 : String aLine( pTextEngine->GetText( nPara ) );
723 0 : lcl_ConvertTabsToSpaces( aLine );
724 0 : sal_uInt16 nLines = aLine.Len() / nCharspLine + 1;
725 0 : for ( sal_uInt16 nLine = 0; nLine < nLines; ++nLine )
726 : {
727 0 : String aTmpLine( aLine, nLine * nCharspLine, nCharspLine );
728 0 : aPos.Y() += nLineHeight;
729 0 : if ( aPos.Y() > ( aPaperSz.Height() + TMARGPRN - nLineHeight/2 ) )
730 : {
731 0 : ++nCurPage;
732 0 : if (!bCalcNumPagesOnly && nPage == nCurPage)
733 0 : lcl_PrintHeader( *pOutDev, nPages, nCurPage, aTitle );
734 0 : aPos = aStartPos;
735 : }
736 0 : if (!bCalcNumPagesOnly && nPage == nCurPage)
737 0 : pOutDev->DrawText( aPos, aTmpLine );
738 0 : }
739 0 : aPos.Y() += nParaSpace;
740 0 : }
741 :
742 0 : pOutDev->Pop();
743 :
744 : OSL_ENSURE( bCalcNumPagesOnly || nPage <= nCurPage, "page number out of range" );
745 0 : return nCurPage;
746 : }
747 :
748 0 : void SwSrcView::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
749 : {
750 0 : if ( rHint.ISA(SfxSimpleHint) &&
751 : (
752 0 : ((SfxSimpleHint&) rHint).GetId() == SFX_HINT_MODECHANGED ||
753 : (
754 0 : ((SfxSimpleHint&) rHint).GetId() == SFX_HINT_TITLECHANGED &&
755 0 : !GetDocShell()->IsReadOnly() && aEditWin.IsReadonly()
756 : )
757 : )
758 : )
759 : {
760 : // Broadcast only comes once!
761 0 : const SwDocShell* pDocSh = GetDocShell();
762 0 : const sal_Bool bReadonly = pDocSh->IsReadOnly();
763 0 : aEditWin.SetReadonly(bReadonly);
764 : }
765 0 : SfxViewShell::Notify(rBC, rHint);
766 0 : }
767 :
768 0 : void SwSrcView::Load(SwDocShell* pDocShell)
769 : {
770 0 : SvxHtmlOptions& rHtmlOptions = SvxHtmlOptions::Get();
771 : const sal_Char *pCharSet =
772 0 : rtl_getBestMimeCharsetFromTextEncoding( rHtmlOptions.GetTextEncoding() );
773 0 : rtl_TextEncoding eDestEnc = rtl_getTextEncodingFromMimeCharset( pCharSet );
774 :
775 0 : aEditWin.SetReadonly(pDocShell->IsReadOnly());
776 0 : aEditWin.SetTextEncoding(eDestEnc);
777 0 : SfxMedium* pMedium = pDocShell->GetMedium();
778 :
779 0 : const SfxFilter* pFilter = pMedium->GetFilter();
780 0 : bool bHtml = pFilter && pFilter->GetUserData() == "HTML";
781 0 : sal_Bool bDocModified = pDocShell->IsModified();
782 0 : if(bHtml && !bDocModified && pDocShell->HasName())
783 : {
784 0 : SvStream* pStream = pMedium->GetInStream();
785 0 : if(pStream && 0 == pStream->GetError() )
786 : {
787 : rtl_TextEncoding eHeaderEnc =
788 : SfxHTMLParser::GetEncodingByHttpHeader(
789 0 : pDocShell->GetHeaderAttributes() );
790 0 : if( RTL_TEXTENCODING_DONTKNOW == eHeaderEnc )
791 : {
792 : const sal_Char *pTmpCharSet =
793 0 : rtl_getBestMimeCharsetFromTextEncoding( RTL_TEXTENCODING_ISO_8859_1 );
794 0 : eHeaderEnc = rtl_getTextEncodingFromMimeCharset( pTmpCharSet );
795 : }
796 0 : if( RTL_TEXTENCODING_DONTKNOW != eHeaderEnc &&
797 : eDestEnc != eHeaderEnc )
798 : {
799 0 : eDestEnc = eHeaderEnc;
800 0 : aEditWin.SetTextEncoding(eDestEnc);
801 : }
802 0 : pStream->SetStreamCharSet( eDestEnc );
803 0 : pStream->Seek(0);
804 0 : TextEngine* pTextEngine = aEditWin.GetTextEngine();
805 0 : pTextEngine->EnableUndo(sal_False);
806 0 : aEditWin.Read(*pStream);
807 0 : pTextEngine->EnableUndo(sal_True);
808 : }
809 : else
810 : {
811 0 : Window *pTmpWindow = &GetViewFrame()->GetWindow();
812 0 : InfoBox(pTmpWindow, SW_RES(MSG_ERR_SRCSTREAM)).Execute();
813 : }
814 : }
815 : else
816 : {
817 0 : utl::TempFile aTempFile;
818 0 : aTempFile.EnableKillingFile();
819 0 : String sFileURL( aTempFile.GetURL() );
820 0 : SvtSaveOptions aOpt;
821 :
822 : {
823 0 : SfxMedium aMedium( sFileURL,STREAM_READWRITE );
824 0 : SwWriter aWriter( aMedium, *pDocShell->GetDoc() );
825 0 : WriterRef xWriter;
826 0 : ::GetHTMLWriter(aEmptyStr, aMedium.GetBaseURL( true ), xWriter);
827 0 : String sWriteName;
828 0 : if (pDocShell->HasName())
829 0 : sWriteName = pMedium->GetName();
830 : else
831 0 : sWriteName = sFileURL;
832 0 : sal_uLong nRes = aWriter.Write(xWriter, &sWriteName);
833 0 : if(nRes)
834 : {
835 0 : ErrorHandler::HandleError(ErrCode(nRes));
836 0 : aEditWin.SetReadonly(sal_True);
837 : }
838 0 : aMedium.Commit();
839 0 : SvStream* pInStream = aMedium.GetInStream();
840 0 : pInStream->Seek(0);
841 0 : pInStream->SetStreamCharSet( eDestEnc );
842 :
843 :
844 0 : aEditWin.Read(*pInStream);
845 0 : }
846 : }
847 0 : aEditWin.ClearModifyFlag();
848 :
849 0 : eLoadEncoding = eDestEnc;
850 :
851 0 : if(bDocModified)
852 0 : pDocShell->SetModified();// The flag will be reset in between times.
853 : // Disable AutoLoad
854 0 : pDocShell->SetAutoLoad(INetURLObject(), 0, sal_False);
855 : OSL_ENSURE(PTR_CAST(SwWebDocShell, pDocShell), "Why no WebDocShell?");
856 0 : sal_uInt16 nLine = ((SwWebDocShell*)pDocShell)->GetSourcePara();
857 0 : aEditWin.SetStartLine(nLine);
858 0 : aEditWin.GetTextEngine()->ResetUndo();
859 0 : aEditWin.GetOutWin()->GrabFocus();
860 99 : }
861 :
862 :
863 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|